Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1 | //===--- SemaOverload.cpp - C++ Overloading -------------------------------===// |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides Sema routines for C++ overloading. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Overload.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 16 | #include "clang/AST/CXXInheritance.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprCXX.h" |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeOrdering.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Diagnostic.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 23 | #include "clang/Basic/PartialDiagnostic.h" |
David Majnemer | e9f6f33 | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 25 | #include "clang/Lex/Preprocessor.h" |
| 26 | #include "clang/Sema/Initialization.h" |
| 27 | #include "clang/Sema/Lookup.h" |
| 28 | #include "clang/Sema/SemaInternal.h" |
| 29 | #include "clang/Sema/Template.h" |
| 30 | #include "clang/Sema/TemplateDeduction.h" |
Douglas Gregor | 661b493 | 2010-09-12 04:28:07 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/DenseSet.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallPtrSet.h" |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 35 | #include <algorithm> |
| 36 | |
| 37 | namespace clang { |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 38 | using namespace sema; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 39 | |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 40 | /// A convenience routine for creating a decayed reference to a function. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 41 | static ExprResult |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 42 | CreateFunctionRefExpr(Sema &S, FunctionDecl *Fn, NamedDecl *FoundDecl, |
| 43 | bool HadMultipleCandidates, |
Douglas Gregor | 5b8968c | 2011-07-15 16:25:15 +0000 | [diff] [blame] | 44 | SourceLocation Loc = SourceLocation(), |
| 45 | const DeclarationNameLoc &LocInfo = DeclarationNameLoc()){ |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 46 | if (S.DiagnoseUseOfDecl(FoundDecl, Loc)) |
Faisal Vali | d570a92 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 47 | return ExprError(); |
| 48 | // If FoundDecl is different from Fn (such as if one is a template |
| 49 | // and the other a specialization), make sure DiagnoseUseOfDecl is |
| 50 | // called on both. |
| 51 | // FIXME: This would be more comprehensively addressed by modifying |
| 52 | // DiagnoseUseOfDecl to accept both the FoundDecl and the decl |
| 53 | // being used. |
| 54 | if (FoundDecl != Fn && S.DiagnoseUseOfDecl(Fn, Loc)) |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 55 | return ExprError(); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 56 | DeclRefExpr *DRE = new (S.Context) DeclRefExpr(Fn, false, Fn->getType(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 57 | VK_LValue, Loc, LocInfo); |
| 58 | if (HadMultipleCandidates) |
| 59 | DRE->setHadMultipleCandidates(true); |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 60 | |
| 61 | S.MarkDeclRefReferenced(DRE); |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 62 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 63 | ExprResult E = S.Owned(DRE); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 64 | E = S.DefaultFunctionArrayConversion(E.take()); |
| 65 | if (E.isInvalid()) |
| 66 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 67 | return E; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 68 | } |
| 69 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 70 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 71 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 72 | StandardConversionSequence &SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 73 | bool CStyle, |
| 74 | bool AllowObjCWritebackConversion); |
Sam Panzer | d012586 | 2012-08-16 02:38:47 +0000 | [diff] [blame] | 75 | |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 76 | static bool IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 77 | QualType &ToType, |
| 78 | bool InOverloadResolution, |
| 79 | StandardConversionSequence &SCS, |
| 80 | bool CStyle); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 81 | static OverloadingResult |
| 82 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 83 | UserDefinedConversionSequence& User, |
| 84 | OverloadCandidateSet& Conversions, |
| 85 | bool AllowExplicit); |
| 86 | |
| 87 | |
| 88 | static ImplicitConversionSequence::CompareKind |
| 89 | CompareStandardConversionSequences(Sema &S, |
| 90 | const StandardConversionSequence& SCS1, |
| 91 | const StandardConversionSequence& SCS2); |
| 92 | |
| 93 | static ImplicitConversionSequence::CompareKind |
| 94 | CompareQualificationConversions(Sema &S, |
| 95 | const StandardConversionSequence& SCS1, |
| 96 | const StandardConversionSequence& SCS2); |
| 97 | |
| 98 | static ImplicitConversionSequence::CompareKind |
| 99 | CompareDerivedToBaseConversions(Sema &S, |
| 100 | const StandardConversionSequence& SCS1, |
| 101 | const StandardConversionSequence& SCS2); |
| 102 | |
| 103 | |
| 104 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 105 | /// GetConversionCategory - Retrieve the implicit conversion |
| 106 | /// category corresponding to the given implicit conversion kind. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | ImplicitConversionCategory |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 108 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 109 | static const ImplicitConversionCategory |
| 110 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 111 | ICC_Identity, |
| 112 | ICC_Lvalue_Transformation, |
| 113 | ICC_Lvalue_Transformation, |
| 114 | ICC_Lvalue_Transformation, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 115 | ICC_Identity, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 116 | ICC_Qualification_Adjustment, |
| 117 | ICC_Promotion, |
| 118 | ICC_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 119 | ICC_Promotion, |
| 120 | ICC_Conversion, |
| 121 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 122 | ICC_Conversion, |
| 123 | ICC_Conversion, |
| 124 | ICC_Conversion, |
| 125 | ICC_Conversion, |
| 126 | ICC_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 127 | ICC_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 128 | ICC_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 129 | ICC_Conversion, |
| 130 | ICC_Conversion, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 131 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 132 | ICC_Conversion |
| 133 | }; |
| 134 | return Category[(int)Kind]; |
| 135 | } |
| 136 | |
| 137 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 138 | /// corresponding to the given implicit conversion kind. |
| 139 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 140 | static const ImplicitConversionRank |
| 141 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 142 | ICR_Exact_Match, |
| 143 | ICR_Exact_Match, |
| 144 | ICR_Exact_Match, |
| 145 | ICR_Exact_Match, |
| 146 | ICR_Exact_Match, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 147 | ICR_Exact_Match, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 148 | ICR_Promotion, |
| 149 | ICR_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 150 | ICR_Promotion, |
| 151 | ICR_Conversion, |
| 152 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 153 | ICR_Conversion, |
| 154 | ICR_Conversion, |
| 155 | ICR_Conversion, |
| 156 | ICR_Conversion, |
| 157 | ICR_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 158 | ICR_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 159 | ICR_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 160 | ICR_Conversion, |
| 161 | ICR_Conversion, |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 162 | ICR_Complex_Real_Conversion, |
| 163 | ICR_Conversion, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 164 | ICR_Conversion, |
| 165 | ICR_Writeback_Conversion |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 166 | }; |
| 167 | return Rank[(int)Kind]; |
| 168 | } |
| 169 | |
| 170 | /// GetImplicitConversionName - Return the name of this kind of |
| 171 | /// implicit conversion. |
| 172 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | 2550d70 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 173 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 174 | "No conversion", |
| 175 | "Lvalue-to-rvalue", |
| 176 | "Array-to-pointer", |
| 177 | "Function-to-pointer", |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 178 | "Noreturn adjustment", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 179 | "Qualification", |
| 180 | "Integral promotion", |
| 181 | "Floating point promotion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 182 | "Complex promotion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 183 | "Integral conversion", |
| 184 | "Floating conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 185 | "Complex conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 186 | "Floating-integral conversion", |
| 187 | "Pointer conversion", |
| 188 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 189 | "Boolean conversion", |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 190 | "Compatible-types conversion", |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 191 | "Derived-to-base conversion", |
| 192 | "Vector conversion", |
| 193 | "Vector splat", |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 194 | "Complex-real conversion", |
| 195 | "Block Pointer conversion", |
| 196 | "Transparent Union Conversion" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 197 | "Writeback conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 198 | }; |
| 199 | return Name[Kind]; |
| 200 | } |
| 201 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 202 | /// StandardConversionSequence - Set the standard conversion |
| 203 | /// sequence to the identity conversion. |
| 204 | void StandardConversionSequence::setAsIdentityConversion() { |
| 205 | First = ICK_Identity; |
| 206 | Second = ICK_Identity; |
| 207 | Third = ICK_Identity; |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 208 | DeprecatedStringLiteralToCharPtr = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 209 | QualificationIncludesObjCLifetime = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 210 | ReferenceBinding = false; |
| 211 | DirectBinding = false; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 212 | IsLvalueReference = true; |
| 213 | BindsToFunctionLvalue = false; |
| 214 | BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 215 | BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 216 | ObjCLifetimeConversionBinding = false; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 217 | CopyConstructor = 0; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 220 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 221 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 222 | /// implicit conversions. |
| 223 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 224 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 225 | if (GetConversionRank(First) > Rank) |
| 226 | Rank = GetConversionRank(First); |
| 227 | if (GetConversionRank(Second) > Rank) |
| 228 | Rank = GetConversionRank(Second); |
| 229 | if (GetConversionRank(Third) > Rank) |
| 230 | Rank = GetConversionRank(Third); |
| 231 | return Rank; |
| 232 | } |
| 233 | |
| 234 | /// isPointerConversionToBool - Determines whether this conversion is |
| 235 | /// a conversion of a pointer or pointer-to-member to bool. This is |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 237 | /// (C++ 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 239 | // Note that FromType has not necessarily been transformed by the |
| 240 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 241 | // check for their presence as well as checking whether FromType is |
| 242 | // a pointer. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 243 | if (getToType(1)->isBooleanType() && |
John McCall | ddb0ce7 | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 244 | (getFromType()->isPointerType() || |
| 245 | getFromType()->isObjCObjectPointerType() || |
| 246 | getFromType()->isBlockPointerType() || |
Anders Carlsson | c8df0b6 | 2010-11-05 00:12:09 +0000 | [diff] [blame] | 247 | getFromType()->isNullPtrType() || |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 248 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 249 | return true; |
| 250 | |
| 251 | return false; |
| 252 | } |
| 253 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 254 | /// isPointerConversionToVoidPointer - Determines whether this |
| 255 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 256 | /// used as part of the ranking of standard conversion sequences (C++ |
| 257 | /// 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 258 | bool |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 259 | StandardConversionSequence:: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 260 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 261 | QualType FromType = getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 262 | QualType ToType = getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 263 | |
| 264 | // Note that FromType has not necessarily been transformed by the |
| 265 | // array-to-pointer implicit conversion, so check for its presence |
| 266 | // and redo the conversion to get a pointer. |
| 267 | if (First == ICK_Array_To_Pointer) |
| 268 | FromType = Context.getArrayDecayedType(FromType); |
| 269 | |
Douglas Gregor | f9af524 | 2011-04-15 20:45:44 +0000 | [diff] [blame] | 270 | if (Second == ICK_Pointer_Conversion && FromType->isAnyPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 271 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 272 | return ToPtrType->getPointeeType()->isVoidType(); |
| 273 | |
| 274 | return false; |
| 275 | } |
| 276 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 277 | /// Skip any implicit casts which could be either part of a narrowing conversion |
| 278 | /// or after one in an implicit conversion. |
| 279 | static const Expr *IgnoreNarrowingConversion(const Expr *Converted) { |
| 280 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Converted)) { |
| 281 | switch (ICE->getCastKind()) { |
| 282 | case CK_NoOp: |
| 283 | case CK_IntegralCast: |
| 284 | case CK_IntegralToBoolean: |
| 285 | case CK_IntegralToFloating: |
| 286 | case CK_FloatingToIntegral: |
| 287 | case CK_FloatingToBoolean: |
| 288 | case CK_FloatingCast: |
| 289 | Converted = ICE->getSubExpr(); |
| 290 | continue; |
| 291 | |
| 292 | default: |
| 293 | return Converted; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | return Converted; |
| 298 | } |
| 299 | |
| 300 | /// Check if this standard conversion sequence represents a narrowing |
| 301 | /// conversion, according to C++11 [dcl.init.list]p7. |
| 302 | /// |
| 303 | /// \param Ctx The AST context. |
| 304 | /// \param Converted The result of applying this standard conversion sequence. |
| 305 | /// \param ConstantValue If this is an NK_Constant_Narrowing conversion, the |
| 306 | /// value of the expression prior to the narrowing conversion. |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 307 | /// \param ConstantType If this is an NK_Constant_Narrowing conversion, the |
| 308 | /// type of the expression prior to the narrowing conversion. |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 309 | NarrowingKind |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 310 | StandardConversionSequence::getNarrowingKind(ASTContext &Ctx, |
| 311 | const Expr *Converted, |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 312 | APValue &ConstantValue, |
| 313 | QualType &ConstantType) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 314 | assert(Ctx.getLangOpts().CPlusPlus && "narrowing check outside C++"); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 315 | |
| 316 | // C++11 [dcl.init.list]p7: |
| 317 | // A narrowing conversion is an implicit conversion ... |
| 318 | QualType FromType = getToType(0); |
| 319 | QualType ToType = getToType(1); |
| 320 | switch (Second) { |
| 321 | // -- from a floating-point type to an integer type, or |
| 322 | // |
| 323 | // -- from an integer type or unscoped enumeration type to a floating-point |
| 324 | // type, except where the source is a constant expression and the actual |
| 325 | // value after conversion will fit into the target type and will produce |
| 326 | // the original value when converted back to the original type, or |
| 327 | case ICK_Floating_Integral: |
| 328 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
| 329 | return NK_Type_Narrowing; |
| 330 | } else if (FromType->isIntegralType(Ctx) && ToType->isRealFloatingType()) { |
| 331 | llvm::APSInt IntConstantValue; |
| 332 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| 333 | if (Initializer && |
| 334 | Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { |
| 335 | // Convert the integer to the floating type. |
| 336 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
| 337 | Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), |
| 338 | llvm::APFloat::rmNearestTiesToEven); |
| 339 | // And back. |
| 340 | llvm::APSInt ConvertedValue = IntConstantValue; |
| 341 | bool ignored; |
| 342 | Result.convertToInteger(ConvertedValue, |
| 343 | llvm::APFloat::rmTowardZero, &ignored); |
| 344 | // If the resulting value is different, this was a narrowing conversion. |
| 345 | if (IntConstantValue != ConvertedValue) { |
| 346 | ConstantValue = APValue(IntConstantValue); |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 347 | ConstantType = Initializer->getType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 348 | return NK_Constant_Narrowing; |
| 349 | } |
| 350 | } else { |
| 351 | // Variables are always narrowings. |
| 352 | return NK_Variable_Narrowing; |
| 353 | } |
| 354 | } |
| 355 | return NK_Not_Narrowing; |
| 356 | |
| 357 | // -- from long double to double or float, or from double to float, except |
| 358 | // where the source is a constant expression and the actual value after |
| 359 | // conversion is within the range of values that can be represented (even |
| 360 | // if it cannot be represented exactly), or |
| 361 | case ICK_Floating_Conversion: |
| 362 | if (FromType->isRealFloatingType() && ToType->isRealFloatingType() && |
| 363 | Ctx.getFloatingTypeOrder(FromType, ToType) == 1) { |
| 364 | // FromType is larger than ToType. |
| 365 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| 366 | if (Initializer->isCXX11ConstantExpr(Ctx, &ConstantValue)) { |
| 367 | // Constant! |
| 368 | assert(ConstantValue.isFloat()); |
| 369 | llvm::APFloat FloatVal = ConstantValue.getFloat(); |
| 370 | // Convert the source value into the target type. |
| 371 | bool ignored; |
| 372 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
| 373 | Ctx.getFloatTypeSemantics(ToType), |
| 374 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 375 | // If there was no overflow, the source value is within the range of |
| 376 | // values that can be represented. |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 377 | if (ConvertStatus & llvm::APFloat::opOverflow) { |
| 378 | ConstantType = Initializer->getType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 379 | return NK_Constant_Narrowing; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 380 | } |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 381 | } else { |
| 382 | return NK_Variable_Narrowing; |
| 383 | } |
| 384 | } |
| 385 | return NK_Not_Narrowing; |
| 386 | |
| 387 | // -- from an integer type or unscoped enumeration type to an integer type |
| 388 | // that cannot represent all the values of the original type, except where |
| 389 | // the source is a constant expression and the actual value after |
| 390 | // conversion will fit into the target type and will produce the original |
| 391 | // value when converted back to the original type. |
| 392 | case ICK_Boolean_Conversion: // Bools are integers too. |
| 393 | if (!FromType->isIntegralOrUnscopedEnumerationType()) { |
| 394 | // Boolean conversions can be from pointers and pointers to members |
| 395 | // [conv.bool], and those aren't considered narrowing conversions. |
| 396 | return NK_Not_Narrowing; |
| 397 | } // Otherwise, fall through to the integral case. |
| 398 | case ICK_Integral_Conversion: { |
| 399 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
| 400 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
| 401 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
| 402 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
| 403 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
| 404 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
| 405 | |
| 406 | if (FromWidth > ToWidth || |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 407 | (FromWidth == ToWidth && FromSigned != ToSigned) || |
| 408 | (FromSigned && !ToSigned)) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 409 | // Not all values of FromType can be represented in ToType. |
| 410 | llvm::APSInt InitializerValue; |
| 411 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 412 | if (!Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { |
| 413 | // Such conversions on variables are always narrowing. |
| 414 | return NK_Variable_Narrowing; |
Richard Smith | 5d7700e | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 415 | } |
| 416 | bool Narrowing = false; |
| 417 | if (FromWidth < ToWidth) { |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 418 | // Negative -> unsigned is narrowing. Otherwise, more bits is never |
| 419 | // narrowing. |
| 420 | if (InitializerValue.isSigned() && InitializerValue.isNegative()) |
Richard Smith | 5d7700e | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 421 | Narrowing = true; |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 422 | } else { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 423 | // Add a bit to the InitializerValue so we don't have to worry about |
| 424 | // signed vs. unsigned comparisons. |
| 425 | InitializerValue = InitializerValue.extend( |
| 426 | InitializerValue.getBitWidth() + 1); |
| 427 | // Convert the initializer to and from the target width and signed-ness. |
| 428 | llvm::APSInt ConvertedValue = InitializerValue; |
| 429 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
| 430 | ConvertedValue.setIsSigned(ToSigned); |
| 431 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
| 432 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
| 433 | // If the result is different, this was a narrowing conversion. |
Richard Smith | 5d7700e | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 434 | if (ConvertedValue != InitializerValue) |
| 435 | Narrowing = true; |
| 436 | } |
| 437 | if (Narrowing) { |
| 438 | ConstantType = Initializer->getType(); |
| 439 | ConstantValue = APValue(InitializerValue); |
| 440 | return NK_Constant_Narrowing; |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | return NK_Not_Narrowing; |
| 444 | } |
| 445 | |
| 446 | default: |
| 447 | // Other kinds of conversions are not narrowings. |
| 448 | return NK_Not_Narrowing; |
| 449 | } |
| 450 | } |
| 451 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 452 | /// DebugPrint - Print this standard conversion sequence to standard |
| 453 | /// error. Useful for debugging overloading issues. |
| 454 | void StandardConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 455 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 456 | bool PrintedSomething = false; |
| 457 | if (First != ICK_Identity) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 458 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 459 | PrintedSomething = true; |
| 460 | } |
| 461 | |
| 462 | if (Second != ICK_Identity) { |
| 463 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 464 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 465 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 466 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 467 | |
| 468 | if (CopyConstructor) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 469 | OS << " (by copy constructor)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 470 | } else if (DirectBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 471 | OS << " (direct reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 472 | } else if (ReferenceBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 473 | OS << " (reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 474 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 475 | PrintedSomething = true; |
| 476 | } |
| 477 | |
| 478 | if (Third != ICK_Identity) { |
| 479 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 480 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 481 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 482 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 483 | PrintedSomething = true; |
| 484 | } |
| 485 | |
| 486 | if (!PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 487 | OS << "No conversions required"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
| 491 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 492 | /// error. Useful for debugging overloading issues. |
| 493 | void UserDefinedConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 494 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 495 | if (Before.First || Before.Second || Before.Third) { |
| 496 | Before.DebugPrint(); |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 497 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 498 | } |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 499 | if (ConversionFunction) |
| 500 | OS << '\'' << *ConversionFunction << '\''; |
| 501 | else |
| 502 | OS << "aggregate initialization"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 503 | if (After.First || After.Second || After.Third) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 504 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 505 | After.DebugPrint(); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 510 | /// error. Useful for debugging overloading issues. |
| 511 | void ImplicitConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 512 | raw_ostream &OS = llvm::errs(); |
Richard Smith | 798186a | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 513 | if (isStdInitializerListElement()) |
| 514 | OS << "Worst std::initializer_list element conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 515 | switch (ConversionKind) { |
| 516 | case StandardConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 517 | OS << "Standard conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 518 | Standard.DebugPrint(); |
| 519 | break; |
| 520 | case UserDefinedConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 521 | OS << "User-defined conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 522 | UserDefined.DebugPrint(); |
| 523 | break; |
| 524 | case EllipsisConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 525 | OS << "Ellipsis conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 526 | break; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 527 | case AmbiguousConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 528 | OS << "Ambiguous conversion"; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 529 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 530 | case BadConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 531 | OS << "Bad conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 532 | break; |
| 533 | } |
| 534 | |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 535 | OS << "\n"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 536 | } |
| 537 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 538 | void AmbiguousConversionSequence::construct() { |
| 539 | new (&conversions()) ConversionSet(); |
| 540 | } |
| 541 | |
| 542 | void AmbiguousConversionSequence::destruct() { |
| 543 | conversions().~ConversionSet(); |
| 544 | } |
| 545 | |
| 546 | void |
| 547 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 548 | FromTypePtr = O.FromTypePtr; |
| 549 | ToTypePtr = O.ToTypePtr; |
| 550 | new (&conversions()) ConversionSet(O.conversions()); |
| 551 | } |
| 552 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 553 | namespace { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 554 | // Structure used by DeductionFailureInfo to store |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 555 | // template argument information. |
| 556 | struct DFIArguments { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 557 | TemplateArgument FirstArg; |
| 558 | TemplateArgument SecondArg; |
| 559 | }; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 560 | // Structure used by DeductionFailureInfo to store |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 561 | // template parameter and template argument information. |
| 562 | struct DFIParamWithArguments : DFIArguments { |
| 563 | TemplateParameter Param; |
| 564 | }; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 565 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 566 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 567 | /// \brief Convert from Sema's representation of template deduction information |
| 568 | /// to the form used in overload-candidate information. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 569 | DeductionFailureInfo MakeDeductionFailureInfo(ASTContext &Context, |
| 570 | Sema::TemplateDeductionResult TDK, |
| 571 | TemplateDeductionInfo &Info) { |
| 572 | DeductionFailureInfo Result; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 573 | Result.Result = static_cast<unsigned>(TDK); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 574 | Result.HasDiagnostic = false; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 575 | Result.Data = 0; |
| 576 | switch (TDK) { |
| 577 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 578 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 579 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 580 | case Sema::TDK_TooManyArguments: |
| 581 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 582 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 583 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 584 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 585 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 586 | Result.Data = Info.Param.getOpaqueValue(); |
| 587 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 588 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 589 | case Sema::TDK_NonDeducedMismatch: { |
| 590 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 591 | DFIArguments *Saved = new (Context) DFIArguments; |
| 592 | Saved->FirstArg = Info.FirstArg; |
| 593 | Saved->SecondArg = Info.SecondArg; |
| 594 | Result.Data = Saved; |
| 595 | break; |
| 596 | } |
| 597 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 598 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 599 | case Sema::TDK_Underqualified: { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 600 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 601 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 602 | Saved->Param = Info.Param; |
| 603 | Saved->FirstArg = Info.FirstArg; |
| 604 | Saved->SecondArg = Info.SecondArg; |
| 605 | Result.Data = Saved; |
| 606 | break; |
| 607 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 608 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 609 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 610 | Result.Data = Info.take(); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 611 | if (Info.hasSFINAEDiagnostic()) { |
| 612 | PartialDiagnosticAt *Diag = new (Result.Diagnostic) PartialDiagnosticAt( |
| 613 | SourceLocation(), PartialDiagnostic::NullDiagnostic()); |
| 614 | Info.takeSFINAEDiagnostic(*Diag); |
| 615 | Result.HasDiagnostic = true; |
| 616 | } |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 617 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 618 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 619 | case Sema::TDK_FailedOverloadResolution: |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 620 | Result.Data = Info.Expression; |
| 621 | break; |
| 622 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 623 | case Sema::TDK_MiscellaneousDeductionFailure: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 624 | break; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 625 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 626 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 627 | return Result; |
| 628 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 629 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 630 | void DeductionFailureInfo::Destroy() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 631 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 632 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 633 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 634 | case Sema::TDK_InstantiationDepth: |
| 635 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 636 | case Sema::TDK_TooManyArguments: |
| 637 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 638 | case Sema::TDK_InvalidExplicitArguments: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 639 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 640 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 641 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 642 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 643 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 644 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | aaa045d | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 645 | // FIXME: Destroy the data? |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 646 | Data = 0; |
| 647 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 648 | |
| 649 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 650 | // FIXME: Destroy the template argument list? |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 651 | Data = 0; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 652 | if (PartialDiagnosticAt *Diag = getSFINAEDiagnostic()) { |
| 653 | Diag->~PartialDiagnosticAt(); |
| 654 | HasDiagnostic = false; |
| 655 | } |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 656 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 657 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 658 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 659 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 660 | break; |
| 661 | } |
| 662 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 663 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 664 | PartialDiagnosticAt *DeductionFailureInfo::getSFINAEDiagnostic() { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 665 | if (HasDiagnostic) |
| 666 | return static_cast<PartialDiagnosticAt*>(static_cast<void*>(Diagnostic)); |
| 667 | return 0; |
| 668 | } |
| 669 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 670 | TemplateParameter DeductionFailureInfo::getTemplateParameter() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 671 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 672 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 673 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 674 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 675 | case Sema::TDK_TooManyArguments: |
| 676 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 677 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 678 | case Sema::TDK_NonDeducedMismatch: |
| 679 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 680 | return TemplateParameter(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 681 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 682 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 683 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 684 | return TemplateParameter::getFromOpaqueValue(Data); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 685 | |
| 686 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 687 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 688 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 689 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 690 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 691 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 692 | break; |
| 693 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 694 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 695 | return TemplateParameter(); |
| 696 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 697 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 698 | TemplateArgumentList *DeductionFailureInfo::getTemplateArgumentList() { |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 699 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 700 | case Sema::TDK_Success: |
| 701 | case Sema::TDK_Invalid: |
| 702 | case Sema::TDK_InstantiationDepth: |
| 703 | case Sema::TDK_TooManyArguments: |
| 704 | case Sema::TDK_TooFewArguments: |
| 705 | case Sema::TDK_Incomplete: |
| 706 | case Sema::TDK_InvalidExplicitArguments: |
| 707 | case Sema::TDK_Inconsistent: |
| 708 | case Sema::TDK_Underqualified: |
| 709 | case Sema::TDK_NonDeducedMismatch: |
| 710 | case Sema::TDK_FailedOverloadResolution: |
| 711 | return 0; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 712 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 713 | case Sema::TDK_SubstitutionFailure: |
| 714 | return static_cast<TemplateArgumentList*>(Data); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 715 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 716 | // Unhandled |
| 717 | case Sema::TDK_MiscellaneousDeductionFailure: |
| 718 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | return 0; |
| 722 | } |
| 723 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 724 | const TemplateArgument *DeductionFailureInfo::getFirstArg() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 725 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 726 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 727 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 728 | case Sema::TDK_InstantiationDepth: |
| 729 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 730 | case Sema::TDK_TooManyArguments: |
| 731 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 732 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 733 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 734 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 735 | return 0; |
| 736 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 737 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 738 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 739 | case Sema::TDK_NonDeducedMismatch: |
| 740 | return &static_cast<DFIArguments*>(Data)->FirstArg; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 741 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 742 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 743 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 744 | break; |
| 745 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 746 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 747 | return 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 748 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 749 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 750 | const TemplateArgument *DeductionFailureInfo::getSecondArg() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 751 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 752 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 753 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 754 | case Sema::TDK_InstantiationDepth: |
| 755 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 756 | case Sema::TDK_TooManyArguments: |
| 757 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 758 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 759 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 760 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 761 | return 0; |
| 762 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 763 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 764 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 765 | case Sema::TDK_NonDeducedMismatch: |
| 766 | return &static_cast<DFIArguments*>(Data)->SecondArg; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 767 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 768 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 769 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 770 | break; |
| 771 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 772 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 773 | return 0; |
| 774 | } |
| 775 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 776 | Expr *DeductionFailureInfo::getExpr() { |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 777 | if (static_cast<Sema::TemplateDeductionResult>(Result) == |
| 778 | Sema::TDK_FailedOverloadResolution) |
| 779 | return static_cast<Expr*>(Data); |
| 780 | |
| 781 | return 0; |
| 782 | } |
| 783 | |
Benjamin Kramer | f5b132f | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 784 | void OverloadCandidateSet::destroyCandidates() { |
Richard Smith | e3898ac | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 785 | for (iterator i = begin(), e = end(); i != e; ++i) { |
Benjamin Kramer | 9e2822b | 2012-01-14 20:16:52 +0000 | [diff] [blame] | 786 | for (unsigned ii = 0, ie = i->NumConversions; ii != ie; ++ii) |
| 787 | i->Conversions[ii].~ImplicitConversionSequence(); |
Richard Smith | e3898ac | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 788 | if (!i->Viable && i->FailureKind == ovl_fail_bad_deduction) |
| 789 | i->DeductionFailure.Destroy(); |
| 790 | } |
Benjamin Kramer | f5b132f | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | void OverloadCandidateSet::clear() { |
| 794 | destroyCandidates(); |
Benjamin Kramer | 314f554 | 2012-01-14 19:31:39 +0000 | [diff] [blame] | 795 | NumInlineSequences = 0; |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 796 | Candidates.clear(); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 797 | Functions.clear(); |
| 798 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 799 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 800 | namespace { |
| 801 | class UnbridgedCastsSet { |
| 802 | struct Entry { |
| 803 | Expr **Addr; |
| 804 | Expr *Saved; |
| 805 | }; |
| 806 | SmallVector<Entry, 2> Entries; |
| 807 | |
| 808 | public: |
| 809 | void save(Sema &S, Expr *&E) { |
| 810 | assert(E->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 811 | Entry entry = { &E, E }; |
| 812 | Entries.push_back(entry); |
| 813 | E = S.stripARCUnbridgedCast(E); |
| 814 | } |
| 815 | |
| 816 | void restore() { |
| 817 | for (SmallVectorImpl<Entry>::iterator |
| 818 | i = Entries.begin(), e = Entries.end(); i != e; ++i) |
| 819 | *i->Addr = i->Saved; |
| 820 | } |
| 821 | }; |
| 822 | } |
| 823 | |
| 824 | /// checkPlaceholderForOverload - Do any interesting placeholder-like |
| 825 | /// preprocessing on the given expression. |
| 826 | /// |
| 827 | /// \param unbridgedCasts a collection to which to add unbridged casts; |
| 828 | /// without this, they will be immediately diagnosed as errors |
| 829 | /// |
| 830 | /// Return true on unrecoverable error. |
| 831 | static bool checkPlaceholderForOverload(Sema &S, Expr *&E, |
| 832 | UnbridgedCastsSet *unbridgedCasts = 0) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 833 | if (const BuiltinType *placeholder = E->getType()->getAsPlaceholderType()) { |
| 834 | // We can't handle overloaded expressions here because overload |
| 835 | // resolution might reasonably tweak them. |
| 836 | if (placeholder->getKind() == BuiltinType::Overload) return false; |
| 837 | |
| 838 | // If the context potentially accepts unbridged ARC casts, strip |
| 839 | // the unbridged cast and add it to the collection for later restoration. |
| 840 | if (placeholder->getKind() == BuiltinType::ARCUnbridgedCast && |
| 841 | unbridgedCasts) { |
| 842 | unbridgedCasts->save(S, E); |
| 843 | return false; |
| 844 | } |
| 845 | |
| 846 | // Go ahead and check everything else. |
| 847 | ExprResult result = S.CheckPlaceholderExpr(E); |
| 848 | if (result.isInvalid()) |
| 849 | return true; |
| 850 | |
| 851 | E = result.take(); |
| 852 | return false; |
| 853 | } |
| 854 | |
| 855 | // Nothing to do. |
| 856 | return false; |
| 857 | } |
| 858 | |
| 859 | /// checkArgPlaceholdersForOverload - Check a set of call operands for |
| 860 | /// placeholders. |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 861 | static bool checkArgPlaceholdersForOverload(Sema &S, |
| 862 | MultiExprArg Args, |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 863 | UnbridgedCastsSet &unbridged) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 864 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 865 | if (checkPlaceholderForOverload(S, Args[i], &unbridged)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 866 | return true; |
| 867 | |
| 868 | return false; |
| 869 | } |
| 870 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 871 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 872 | // overload of the declarations in Old. This routine returns false if |
| 873 | // New and Old cannot be overloaded, e.g., if New has the same |
| 874 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 875 | // declarations aren't functions (or function templates) at all. When |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 876 | // it does return false, MatchedDecl will point to the decl that New |
| 877 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 878 | // top of the underlying declaration. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 879 | // |
| 880 | // Example: Given the following input: |
| 881 | // |
| 882 | // void f(int, float); // #1 |
| 883 | // void f(int, int); // #2 |
| 884 | // int f(int, int); // #3 |
| 885 | // |
| 886 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 887 | // so IsOverload will not be used. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 888 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 889 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 890 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 891 | // (since they have different signatures), so this routine returns |
| 892 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 893 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 894 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 895 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 896 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 897 | // identical (return types of functions are not part of the |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 898 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 899 | // point to the FunctionDecl for #2. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 900 | // |
| 901 | // 'NewIsUsingShadowDecl' indicates that 'New' is being introduced |
| 902 | // into a class by a using declaration. The rules for whether to hide |
| 903 | // shadow declarations ignore some properties which otherwise figure |
| 904 | // into a function template's signature. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 905 | Sema::OverloadKind |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 906 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
| 907 | NamedDecl *&Match, bool NewIsUsingDecl) { |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 908 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 909 | I != E; ++I) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 910 | NamedDecl *OldD = *I; |
| 911 | |
| 912 | bool OldIsUsingDecl = false; |
| 913 | if (isa<UsingShadowDecl>(OldD)) { |
| 914 | OldIsUsingDecl = true; |
| 915 | |
| 916 | // We can always introduce two using declarations into the same |
| 917 | // context, even if they have identical signatures. |
| 918 | if (NewIsUsingDecl) continue; |
| 919 | |
| 920 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 921 | } |
| 922 | |
| 923 | // If either declaration was introduced by a using declaration, |
| 924 | // we'll need to use slightly different rules for matching. |
| 925 | // Essentially, these rules are the normal rules, except that |
| 926 | // function templates hide function templates with different |
| 927 | // return types or template parameter lists. |
| 928 | bool UseMemberUsingDeclRules = |
John McCall | 78037ac | 2013-04-03 21:19:47 +0000 | [diff] [blame] | 929 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord() && |
| 930 | !New->getFriendObjectKind(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 931 | |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 932 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 933 | if (!IsOverload(New, OldT->getTemplatedDecl(), UseMemberUsingDeclRules)) { |
| 934 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 935 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 936 | continue; |
| 937 | } |
| 938 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 939 | Match = *I; |
| 940 | return Ovl_Match; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 941 | } |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 942 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 943 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
| 944 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 945 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 946 | continue; |
| 947 | } |
| 948 | |
Rafael Espindola | 90cc390 | 2013-04-15 12:49:13 +0000 | [diff] [blame] | 949 | if (!shouldLinkPossiblyHiddenDecl(*I, New)) |
| 950 | continue; |
| 951 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 952 | Match = *I; |
| 953 | return Ovl_Match; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 954 | } |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 955 | } else if (isa<UsingDecl>(OldD)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 956 | // We can overload with these, which can show up when doing |
| 957 | // redeclaration checks for UsingDecls. |
| 958 | assert(Old.getLookupKind() == LookupUsingDeclName); |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 959 | } else if (isa<TagDecl>(OldD)) { |
| 960 | // We can always overload with tags by hiding them. |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 961 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 962 | // Optimistically assume that an unresolved using decl will |
| 963 | // overload; if it doesn't, we'll have to diagnose during |
| 964 | // template instantiation. |
| 965 | } else { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 966 | // (C++ 13p1): |
| 967 | // Only function declarations can be overloaded; object and type |
| 968 | // declarations cannot be overloaded. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 969 | Match = *I; |
| 970 | return Ovl_NonFunction; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 971 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 972 | } |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 973 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 974 | return Ovl_Overload; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 977 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| 978 | bool UseUsingDeclRules) { |
| 979 | // C++ [basic.start.main]p2: This function shall not be overloaded. |
| 980 | if (New->isMain()) |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 981 | return false; |
Rafael Espindola | 7a525ac | 2013-01-12 01:47:40 +0000 | [diff] [blame] | 982 | |
David Majnemer | e9f6f33 | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 983 | // MSVCRT user defined entry points cannot be overloaded. |
| 984 | if (New->isMSVCRTEntryPoint()) |
| 985 | return false; |
| 986 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 987 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 988 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 989 | |
| 990 | // C++ [temp.fct]p2: |
| 991 | // A function template can be overloaded with other function templates |
| 992 | // and with normal (non-template) functions. |
| 993 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 994 | return true; |
| 995 | |
| 996 | // Is the function New an overload of the function Old? |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 997 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 998 | QualType NewQType = Context.getCanonicalType(New->getType()); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 999 | |
| 1000 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 1001 | // determine whether they are overloads. If we find any mismatch |
| 1002 | // in the signature, they are overloads. |
| 1003 | |
| 1004 | // If either of these functions is a K&R-style function (no |
| 1005 | // prototype), then we consider them to have matching signatures. |
| 1006 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 1007 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 1008 | return false; |
| 1009 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1010 | const FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 1011 | const FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1012 | |
| 1013 | // The signature of a function includes the types of its |
| 1014 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 1015 | // of the ellipsis; see C++ DR 357). |
| 1016 | if (OldQType != NewQType && |
| 1017 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 1018 | OldType->isVariadic() != NewType->isVariadic() || |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1019 | !FunctionArgTypesAreEqual(OldType, NewType))) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1020 | return true; |
| 1021 | |
| 1022 | // C++ [temp.over.link]p4: |
| 1023 | // The signature of a function template consists of its function |
| 1024 | // signature, its return type and its template parameter list. The names |
| 1025 | // of the template parameters are significant only for establishing the |
| 1026 | // relationship between the template parameters and the rest of the |
| 1027 | // signature. |
| 1028 | // |
| 1029 | // We check the return type and template parameter lists for function |
| 1030 | // templates first; the remaining checks follow. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 1031 | // |
| 1032 | // However, we don't consider either of these when deciding whether |
| 1033 | // a member introduced by a shadow declaration is hidden. |
| 1034 | if (!UseUsingDeclRules && NewTemplate && |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1035 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 1036 | OldTemplate->getTemplateParameters(), |
| 1037 | false, TPL_TemplateMatch) || |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1038 | OldType->getResultType() != NewType->getResultType())) |
| 1039 | return true; |
| 1040 | |
| 1041 | // If the function is a class member, its signature includes the |
Douglas Gregor | 57c9f4f | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 1042 | // cv-qualifiers (if any) and ref-qualifier (if any) on the function itself. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1043 | // |
| 1044 | // As part of this, also check whether one of the member functions |
| 1045 | // is static, in which case they are not overloads (C++ |
| 1046 | // 13.1p2). While not part of the definition of the signature, |
| 1047 | // this check is important to determine whether these functions |
| 1048 | // can be overloaded. |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1049 | CXXMethodDecl *OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 1050 | CXXMethodDecl *NewMethod = dyn_cast<CXXMethodDecl>(New); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1051 | if (OldMethod && NewMethod && |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1052 | !OldMethod->isStatic() && !NewMethod->isStatic()) { |
| 1053 | if (OldMethod->getRefQualifier() != NewMethod->getRefQualifier()) { |
| 1054 | if (!UseUsingDeclRules && |
| 1055 | (OldMethod->getRefQualifier() == RQ_None || |
| 1056 | NewMethod->getRefQualifier() == RQ_None)) { |
| 1057 | // C++0x [over.load]p2: |
| 1058 | // - Member function declarations with the same name and the same |
| 1059 | // parameter-type-list as well as member function template |
| 1060 | // declarations with the same name, the same parameter-type-list, and |
| 1061 | // the same template parameter lists cannot be overloaded if any of |
| 1062 | // them, but not all, have a ref-qualifier (8.3.5). |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1063 | Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1064 | << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1065 | Diag(OldMethod->getLocation(), diag::note_previous_declaration); |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1066 | } |
| 1067 | return true; |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1068 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1069 | |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1070 | // We may not have applied the implicit const for a constexpr member |
| 1071 | // function yet (because we haven't yet resolved whether this is a static |
| 1072 | // or non-static member function). Add it now, on the assumption that this |
| 1073 | // is a redeclaration of OldMethod. |
| 1074 | unsigned NewQuals = NewMethod->getTypeQualifiers(); |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1075 | if (!getLangOpts().CPlusPlus1y && NewMethod->isConstexpr() && |
Richard Smith | db2fe73 | 2013-06-25 18:46:26 +0000 | [diff] [blame] | 1076 | !isa<CXXConstructorDecl>(NewMethod)) |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1077 | NewQuals |= Qualifiers::Const; |
| 1078 | if (OldMethod->getTypeQualifiers() != NewQuals) |
| 1079 | return true; |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1080 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1081 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1082 | // The signatures match; this is not an overload. |
| 1083 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 1086 | /// \brief Checks availability of the function depending on the current |
| 1087 | /// function context. Inside an unavailable function, unavailability is ignored. |
| 1088 | /// |
| 1089 | /// \returns true if \arg FD is unavailable and current context is inside |
| 1090 | /// an available function, false otherwise. |
| 1091 | bool Sema::isFunctionConsideredUnavailable(FunctionDecl *FD) { |
| 1092 | return FD->isUnavailable() && !cast<Decl>(CurContext)->isUnavailable(); |
| 1093 | } |
| 1094 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1095 | /// \brief Tries a user-defined conversion from From to ToType. |
| 1096 | /// |
| 1097 | /// Produces an implicit conversion sequence for when a standard conversion |
| 1098 | /// is not an option. See TryImplicitConversion for more information. |
| 1099 | static ImplicitConversionSequence |
| 1100 | TryUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 1101 | bool SuppressUserConversions, |
| 1102 | bool AllowExplicit, |
| 1103 | bool InOverloadResolution, |
| 1104 | bool CStyle, |
| 1105 | bool AllowObjCWritebackConversion) { |
| 1106 | ImplicitConversionSequence ICS; |
| 1107 | |
| 1108 | if (SuppressUserConversions) { |
| 1109 | // We're not in the case above, so there is no conversion that |
| 1110 | // we can perform. |
| 1111 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1112 | return ICS; |
| 1113 | } |
| 1114 | |
| 1115 | // Attempt user-defined conversion. |
| 1116 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 1117 | OverloadingResult UserDefResult |
| 1118 | = IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, Conversions, |
| 1119 | AllowExplicit); |
| 1120 | |
| 1121 | if (UserDefResult == OR_Success) { |
| 1122 | ICS.setUserDefined(); |
| 1123 | // C++ [over.ics.user]p4: |
| 1124 | // A conversion of an expression of class type to the same class |
| 1125 | // type is given Exact Match rank, and a conversion of an |
| 1126 | // expression of class type to a base class of that type is |
| 1127 | // given Conversion rank, in spite of the fact that a copy |
| 1128 | // constructor (i.e., a user-defined conversion function) is |
| 1129 | // called for those cases. |
| 1130 | if (CXXConstructorDecl *Constructor |
| 1131 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
| 1132 | QualType FromCanon |
| 1133 | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 1134 | QualType ToCanon |
| 1135 | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 1136 | if (Constructor->isCopyConstructor() && |
| 1137 | (FromCanon == ToCanon || S.IsDerivedFrom(FromCanon, ToCanon))) { |
| 1138 | // Turn this into a "standard" conversion sequence, so that it |
| 1139 | // gets ranked with standard conversion sequences. |
| 1140 | ICS.setStandard(); |
| 1141 | ICS.Standard.setAsIdentityConversion(); |
| 1142 | ICS.Standard.setFromType(From->getType()); |
| 1143 | ICS.Standard.setAllToTypes(ToType); |
| 1144 | ICS.Standard.CopyConstructor = Constructor; |
| 1145 | if (ToCanon != FromCanon) |
| 1146 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | // C++ [over.best.ics]p4: |
| 1151 | // However, when considering the argument of a user-defined |
| 1152 | // conversion function that is a candidate by 13.3.1.3 when |
| 1153 | // invoked for the copying of the temporary in the second step |
| 1154 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 1155 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 1156 | // ellipsis conversion sequences are allowed. |
| 1157 | if (SuppressUserConversions && ICS.isUserDefined()) { |
| 1158 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
| 1159 | } |
| 1160 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
| 1161 | ICS.setAmbiguous(); |
| 1162 | ICS.Ambiguous.setFromType(From->getType()); |
| 1163 | ICS.Ambiguous.setToType(ToType); |
| 1164 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 1165 | Cand != Conversions.end(); ++Cand) |
| 1166 | if (Cand->Viable) |
| 1167 | ICS.Ambiguous.addConversion(Cand->Function); |
| 1168 | } else { |
| 1169 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1170 | } |
| 1171 | |
| 1172 | return ICS; |
| 1173 | } |
| 1174 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1175 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 1176 | /// from the given expression (Expr) to the given type (ToType). This |
| 1177 | /// function returns an implicit conversion sequence that can be used |
| 1178 | /// to perform the initialization. Given |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1179 | /// |
| 1180 | /// void f(float f); |
| 1181 | /// void g(int i) { f(i); } |
| 1182 | /// |
| 1183 | /// this routine would produce an implicit conversion sequence to |
| 1184 | /// describe the initialization of f from i, which will be a standard |
| 1185 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 1186 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 1187 | // |
| 1188 | /// Note that this routine only determines how the conversion can be |
| 1189 | /// performed; it does not actually perform the conversion. As such, |
| 1190 | /// it will not produce any diagnostics if no conversion is available, |
| 1191 | /// but will instead return an implicit conversion sequence of kind |
| 1192 | /// "BadConversion". |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1193 | /// |
| 1194 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 1195 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1196 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 1197 | /// permitted. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1198 | /// |
| 1199 | /// \param AllowObjCWritebackConversion Whether we allow the Objective-C |
| 1200 | /// writeback conversion, which allows __autoreleasing id* parameters to |
| 1201 | /// be initialized with __strong id* or __weak id* arguments. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1202 | static ImplicitConversionSequence |
| 1203 | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
| 1204 | bool SuppressUserConversions, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1205 | bool AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1206 | bool InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1207 | bool CStyle, |
| 1208 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1209 | ImplicitConversionSequence ICS; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1210 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1211 | ICS.Standard, CStyle, AllowObjCWritebackConversion)){ |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1212 | ICS.setStandard(); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1213 | return ICS; |
| 1214 | } |
| 1215 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1216 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 1217 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1218 | return ICS; |
| 1219 | } |
| 1220 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1221 | // C++ [over.ics.user]p4: |
| 1222 | // A conversion of an expression of class type to the same class |
| 1223 | // type is given Exact Match rank, and a conversion of an |
| 1224 | // expression of class type to a base class of that type is |
| 1225 | // given Conversion rank, in spite of the fact that a copy/move |
| 1226 | // constructor (i.e., a user-defined conversion function) is |
| 1227 | // called for those cases. |
| 1228 | QualType FromType = From->getType(); |
| 1229 | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1230 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
| 1231 | S.IsDerivedFrom(FromType, ToType))) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1232 | ICS.setStandard(); |
| 1233 | ICS.Standard.setAsIdentityConversion(); |
| 1234 | ICS.Standard.setFromType(FromType); |
| 1235 | ICS.Standard.setAllToTypes(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1236 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1237 | // We don't actually check at this point whether there is a valid |
| 1238 | // copy/move constructor, since overloading just assumes that it |
| 1239 | // exists. When we actually perform initialization, we'll find the |
| 1240 | // appropriate constructor to copy the returned object, if needed. |
| 1241 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1242 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1243 | // Determine whether this is considered a derived-to-base conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1244 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1245 | ICS.Standard.Second = ICK_Derived_To_Base; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1246 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1247 | return ICS; |
| 1248 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1249 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1250 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 1251 | AllowExplicit, InOverloadResolution, CStyle, |
| 1252 | AllowObjCWritebackConversion); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1255 | ImplicitConversionSequence |
| 1256 | Sema::TryImplicitConversion(Expr *From, QualType ToType, |
| 1257 | bool SuppressUserConversions, |
| 1258 | bool AllowExplicit, |
| 1259 | bool InOverloadResolution, |
| 1260 | bool CStyle, |
| 1261 | bool AllowObjCWritebackConversion) { |
| 1262 | return clang::TryImplicitConversion(*this, From, ToType, |
| 1263 | SuppressUserConversions, AllowExplicit, |
| 1264 | InOverloadResolution, CStyle, |
| 1265 | AllowObjCWritebackConversion); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1268 | /// PerformImplicitConversion - Perform an implicit conversion of the |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1269 | /// expression From to the type ToType. Returns the |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1270 | /// converted expression. Flavor is the kind of conversion we're |
| 1271 | /// performing, used in the error message. If @p AllowExplicit, |
| 1272 | /// explicit user-defined conversions are permitted. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1273 | ExprResult |
| 1274 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1275 | AssignmentAction Action, bool AllowExplicit) { |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1276 | ImplicitConversionSequence ICS; |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1277 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1280 | ExprResult |
| 1281 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1282 | AssignmentAction Action, bool AllowExplicit, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1283 | ImplicitConversionSequence& ICS) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1284 | if (checkPlaceholderForOverload(*this, From)) |
| 1285 | return ExprError(); |
| 1286 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1287 | // Objective-C ARC: Determine whether we will allow the writeback conversion. |
| 1288 | bool AllowObjCWritebackConversion |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1289 | = getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1290 | (Action == AA_Passing || Action == AA_Sending); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1291 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1292 | ICS = clang::TryImplicitConversion(*this, From, ToType, |
| 1293 | /*SuppressUserConversions=*/false, |
| 1294 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1295 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1296 | /*CStyle=*/false, |
| 1297 | AllowObjCWritebackConversion); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1298 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 1299 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1300 | |
| 1301 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1302 | /// conversion that strips "noreturn" off the nested function type. |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1303 | bool Sema::IsNoReturnConversion(QualType FromType, QualType ToType, |
| 1304 | QualType &ResultTy) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1305 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1306 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1307 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1308 | // Permit the conversion F(t __attribute__((noreturn))) -> F(t) |
| 1309 | // where F adds one of the following at most once: |
| 1310 | // - a pointer |
| 1311 | // - a member pointer |
| 1312 | // - a block pointer |
| 1313 | CanQualType CanTo = Context.getCanonicalType(ToType); |
| 1314 | CanQualType CanFrom = Context.getCanonicalType(FromType); |
| 1315 | Type::TypeClass TyClass = CanTo->getTypeClass(); |
| 1316 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1317 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) { |
| 1318 | if (TyClass == Type::Pointer) { |
| 1319 | CanTo = CanTo.getAs<PointerType>()->getPointeeType(); |
| 1320 | CanFrom = CanFrom.getAs<PointerType>()->getPointeeType(); |
| 1321 | } else if (TyClass == Type::BlockPointer) { |
| 1322 | CanTo = CanTo.getAs<BlockPointerType>()->getPointeeType(); |
| 1323 | CanFrom = CanFrom.getAs<BlockPointerType>()->getPointeeType(); |
| 1324 | } else if (TyClass == Type::MemberPointer) { |
| 1325 | CanTo = CanTo.getAs<MemberPointerType>()->getPointeeType(); |
| 1326 | CanFrom = CanFrom.getAs<MemberPointerType>()->getPointeeType(); |
| 1327 | } else { |
| 1328 | return false; |
| 1329 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1330 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1331 | TyClass = CanTo->getTypeClass(); |
| 1332 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1333 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) |
| 1334 | return false; |
| 1335 | } |
| 1336 | |
| 1337 | const FunctionType *FromFn = cast<FunctionType>(CanFrom); |
| 1338 | FunctionType::ExtInfo EInfo = FromFn->getExtInfo(); |
| 1339 | if (!EInfo.getNoReturn()) return false; |
| 1340 | |
| 1341 | FromFn = Context.adjustFunctionType(FromFn, EInfo.withNoReturn(false)); |
| 1342 | assert(QualType(FromFn, 0).isCanonical()); |
| 1343 | if (QualType(FromFn, 0) != CanTo) return false; |
| 1344 | |
| 1345 | ResultTy = ToType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1346 | return true; |
| 1347 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1348 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1349 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 1350 | /// vector conversion. |
| 1351 | /// |
| 1352 | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
| 1353 | /// conversion. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1354 | static bool IsVectorConversion(ASTContext &Context, QualType FromType, |
| 1355 | QualType ToType, ImplicitConversionKind &ICK) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1356 | // We need at least one of these types to be a vector type to have a vector |
| 1357 | // conversion. |
| 1358 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
| 1359 | return false; |
| 1360 | |
| 1361 | // Identical types require no conversions. |
| 1362 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1363 | return false; |
| 1364 | |
| 1365 | // There are no conversions between extended vector types, only identity. |
| 1366 | if (ToType->isExtVectorType()) { |
| 1367 | // There are no conversions between extended vector types other than the |
| 1368 | // identity conversion. |
| 1369 | if (FromType->isExtVectorType()) |
| 1370 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1371 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1372 | // Vector splat from any arithmetic type to a vector. |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 1373 | if (FromType->isArithmeticType()) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1374 | ICK = ICK_Vector_Splat; |
| 1375 | return true; |
| 1376 | } |
| 1377 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1378 | |
| 1379 | // We can perform the conversion between vector types in the following cases: |
| 1380 | // 1)vector types are equivalent AltiVec and GCC vector types |
| 1381 | // 2)lax vector conversions are permitted and the vector types are of the |
| 1382 | // same size |
| 1383 | if (ToType->isVectorType() && FromType->isVectorType()) { |
| 1384 | if (Context.areCompatibleVectorTypes(FromType, ToType) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1385 | (Context.getLangOpts().LaxVectorConversions && |
Chandler Carruth | c45eb9c | 2010-08-08 05:02:51 +0000 | [diff] [blame] | 1386 | (Context.getTypeSize(FromType) == Context.getTypeSize(ToType)))) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1387 | ICK = ICK_Vector_Conversion; |
| 1388 | return true; |
| 1389 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1390 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1391 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1392 | return false; |
| 1393 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1394 | |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1395 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 1396 | bool InOverloadResolution, |
| 1397 | StandardConversionSequence &SCS, |
| 1398 | bool CStyle); |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1399 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1400 | /// IsStandardConversion - Determines whether there is a standard |
| 1401 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 1402 | /// expression From to the type ToType. Standard conversion sequences |
| 1403 | /// only consider non-class types; for conversions that involve class |
| 1404 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 1405 | /// contain the standard conversion sequence required to perform this |
| 1406 | /// conversion and this routine will return true. Otherwise, this |
| 1407 | /// routine will return false and the value of SCS is unspecified. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1408 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 1409 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1410 | StandardConversionSequence &SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1411 | bool CStyle, |
| 1412 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1413 | QualType FromType = From->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1414 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1415 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1416 | SCS.setAsIdentityConversion(); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1417 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1418 | SCS.IncompatibleObjC = false; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1419 | SCS.setFromType(FromType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1420 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1421 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1422 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1423 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1424 | if (FromType->isRecordType() || ToType->isRecordType()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1425 | if (S.getLangOpts().CPlusPlus) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1426 | return false; |
| 1427 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1431 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 1432 | // array-to-pointer conversion, or function-to-pointer conversion |
| 1433 | // (C++ 4p1). |
| 1434 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1435 | if (FromType == S.Context.OverloadTy) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1436 | DeclAccessPair AccessPair; |
| 1437 | if (FunctionDecl *Fn |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1438 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1439 | AccessPair)) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1440 | // We were able to resolve the address of the overloaded function, |
| 1441 | // so we can convert to the type of that function. |
| 1442 | FromType = Fn->getType(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1443 | |
| 1444 | // we can sometimes resolve &foo<int> regardless of ToType, so check |
| 1445 | // if the type matches (identity) or we are converting to bool |
| 1446 | if (!S.Context.hasSameUnqualifiedType( |
| 1447 | S.ExtractUnqualifiedFunctionType(ToType), FromType)) { |
| 1448 | QualType resultTy; |
| 1449 | // if the function type matches except for [[noreturn]], it's ok |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1450 | if (!S.IsNoReturnConversion(FromType, |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1451 | S.ExtractUnqualifiedFunctionType(ToType), resultTy)) |
| 1452 | // otherwise, only a boolean conversion is standard |
| 1453 | if (!ToType->isBooleanType()) |
| 1454 | return false; |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1455 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1456 | |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1457 | // Check if the "from" expression is taking the address of an overloaded |
| 1458 | // function and recompute the FromType accordingly. Take advantage of the |
| 1459 | // fact that non-static member functions *must* have such an address-of |
| 1460 | // expression. |
| 1461 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn); |
| 1462 | if (Method && !Method->isStatic()) { |
| 1463 | assert(isa<UnaryOperator>(From->IgnoreParens()) && |
| 1464 | "Non-unary operator on non-static member address"); |
| 1465 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() |
| 1466 | == UO_AddrOf && |
| 1467 | "Non-address-of operator on non-static member address"); |
| 1468 | const Type *ClassType |
| 1469 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 1470 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
Chandler Carruth | fc5c8fc | 2011-03-29 18:38:10 +0000 | [diff] [blame] | 1471 | } else if (isa<UnaryOperator>(From->IgnoreParens())) { |
| 1472 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() == |
| 1473 | UO_AddrOf && |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1474 | "Non-address-of operator for overloaded function expression"); |
| 1475 | FromType = S.Context.getPointerType(FromType); |
| 1476 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1477 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1478 | // Check that we've computed the proper type after overload resolution. |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1479 | assert(S.Context.hasSameType( |
| 1480 | FromType, |
| 1481 | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1482 | } else { |
| 1483 | return false; |
| 1484 | } |
Anders Carlsson | 2bd6250 | 2010-11-04 05:28:09 +0000 | [diff] [blame] | 1485 | } |
John McCall | 2148011 | 2011-08-30 00:57:29 +0000 | [diff] [blame] | 1486 | // Lvalue-to-rvalue conversion (C++11 4.1): |
| 1487 | // A glvalue (3.10) of a non-function, non-array type T can |
| 1488 | // be converted to a prvalue. |
| 1489 | bool argIsLValue = From->isGLValue(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1490 | if (argIsLValue && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1491 | !FromType->isFunctionType() && !FromType->isArrayType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1492 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1493 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1494 | |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1495 | // C11 6.3.2.1p2: |
| 1496 | // ... if the lvalue has atomic type, the value has the non-atomic version |
| 1497 | // of the type of the lvalue ... |
| 1498 | if (const AtomicType *Atomic = FromType->getAs<AtomicType>()) |
| 1499 | FromType = Atomic->getValueType(); |
| 1500 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1501 | // If T is a non-class type, the type of the rvalue is the |
| 1502 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1503 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 1504 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1505 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1506 | } else if (FromType->isArrayType()) { |
| 1507 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1508 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1509 | |
| 1510 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 1511 | // bound of T" can be converted to an rvalue of type "pointer to |
| 1512 | // T" (C++ 4.2p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1513 | FromType = S.Context.getArrayDecayedType(FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1514 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1515 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1516 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1517 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1518 | |
| 1519 | // For the purpose of ranking in overload resolution |
| 1520 | // (13.3.3.1.1), this conversion is considered an |
| 1521 | // array-to-pointer conversion followed by a qualification |
| 1522 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1523 | SCS.Second = ICK_Identity; |
| 1524 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1525 | SCS.QualificationIncludesObjCLifetime = false; |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1526 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1527 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1528 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1529 | } else if (FromType->isFunctionType() && argIsLValue) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1530 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1531 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1532 | |
| 1533 | // An lvalue of function type T can be converted to an rvalue of |
| 1534 | // type "pointer to T." The result is a pointer to the |
| 1535 | // function. (C++ 4.3p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1536 | FromType = S.Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1537 | } else { |
| 1538 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1539 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1540 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1541 | SCS.setToType(0, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1542 | |
| 1543 | // The second conversion can be an integral promotion, floating |
| 1544 | // point promotion, integral conversion, floating point conversion, |
| 1545 | // floating-integral conversion, pointer conversion, |
| 1546 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1547 | // For overloading in C, this can also be a "compatible-type" |
| 1548 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1549 | bool IncompatibleObjC = false; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1550 | ImplicitConversionKind SecondICK = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1551 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1552 | // The unqualified versions of the types are the same: there's no |
| 1553 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1554 | SCS.Second = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1555 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1556 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1557 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1558 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1559 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1560 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1561 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1562 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1563 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1564 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1565 | SCS.Second = ICK_Complex_Promotion; |
| 1566 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1567 | } else if (ToType->isBooleanType() && |
| 1568 | (FromType->isArithmeticType() || |
| 1569 | FromType->isAnyPointerType() || |
| 1570 | FromType->isBlockPointerType() || |
| 1571 | FromType->isMemberPointerType() || |
| 1572 | FromType->isNullPtrType())) { |
| 1573 | // Boolean conversions (C++ 4.12). |
| 1574 | SCS.Second = ICK_Boolean_Conversion; |
| 1575 | FromType = S.Context.BoolTy; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1576 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1577 | ToType->isIntegralType(S.Context)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1578 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1579 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1580 | FromType = ToType.getUnqualifiedType(); |
Richard Smith | 42860f1 | 2013-05-10 20:29:50 +0000 | [diff] [blame] | 1581 | } else if (FromType->isAnyComplexType() && ToType->isAnyComplexType()) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1582 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1583 | SCS.Second = ICK_Complex_Conversion; |
| 1584 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1585 | } else if ((FromType->isAnyComplexType() && ToType->isArithmeticType()) || |
| 1586 | (ToType->isAnyComplexType() && FromType->isArithmeticType())) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1587 | // Complex-real conversions (C99 6.3.1.7) |
| 1588 | SCS.Second = ICK_Complex_Real; |
| 1589 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1590 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1591 | // Floating point conversions (C++ 4.8). |
| 1592 | SCS.Second = ICK_Floating_Conversion; |
| 1593 | FromType = ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1594 | } else if ((FromType->isRealFloatingType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1595 | ToType->isIntegralType(S.Context)) || |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1596 | (FromType->isIntegralOrUnscopedEnumerationType() && |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1597 | ToType->isRealFloatingType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1598 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1599 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1600 | FromType = ToType.getUnqualifiedType(); |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1601 | } else if (S.IsBlockPointerConversion(FromType, ToType, FromType)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1602 | SCS.Second = ICK_Block_Pointer_Conversion; |
| 1603 | } else if (AllowObjCWritebackConversion && |
| 1604 | S.isObjCWritebackConversion(FromType, ToType, FromType)) { |
| 1605 | SCS.Second = ICK_Writeback_Conversion; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1606 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1607 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1608 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1609 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1610 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1611 | FromType = FromType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1612 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1613 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1614 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1615 | SCS.Second = ICK_Pointer_Member; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1616 | } else if (IsVectorConversion(S.Context, FromType, ToType, SecondICK)) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1617 | SCS.Second = SecondICK; |
| 1618 | FromType = ToType.getUnqualifiedType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1619 | } else if (!S.getLangOpts().CPlusPlus && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1620 | S.Context.typesAreCompatible(ToType, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1621 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1622 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1623 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1624 | } else if (S.IsNoReturnConversion(FromType, ToType, FromType)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1625 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1626 | SCS.Second = ICK_NoReturn_Adjustment; |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1627 | } else if (IsTransparentUnionStandardConversion(S, From, ToType, |
| 1628 | InOverloadResolution, |
| 1629 | SCS, CStyle)) { |
| 1630 | SCS.Second = ICK_TransparentUnionConversion; |
| 1631 | FromType = ToType; |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1632 | } else if (tryAtomicConversion(S, From, ToType, InOverloadResolution, SCS, |
| 1633 | CStyle)) { |
| 1634 | // tryAtomicConversion has updated the standard conversion sequence |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1635 | // appropriately. |
| 1636 | return true; |
Guy Benyei | 6959acd | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 1637 | } else if (ToType->isEventT() && |
| 1638 | From->isIntegerConstantExpr(S.getASTContext()) && |
| 1639 | (From->EvaluateKnownConstInt(S.getASTContext()) == 0)) { |
| 1640 | SCS.Second = ICK_Zero_Event_Conversion; |
| 1641 | FromType = ToType; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1642 | } else { |
| 1643 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1644 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1645 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1646 | SCS.setToType(1, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1647 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1648 | QualType CanonFrom; |
| 1649 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1650 | // The third conversion can be a qualification conversion (C++ 4p1). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1651 | bool ObjCLifetimeConversion; |
| 1652 | if (S.IsQualificationConversion(FromType, ToType, CStyle, |
| 1653 | ObjCLifetimeConversion)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1654 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1655 | SCS.QualificationIncludesObjCLifetime = ObjCLifetimeConversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1656 | FromType = ToType; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1657 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1658 | CanonTo = S.Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1659 | } else { |
| 1660 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1661 | SCS.Third = ICK_Identity; |
| 1662 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1663 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1664 | // [...] Any difference in top-level cv-qualification is |
| 1665 | // subsumed by the initialization itself and does not constitute |
| 1666 | // a conversion. [...] |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1667 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1668 | CanonTo = S.Context.getCanonicalType(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1669 | if (CanonFrom.getLocalUnqualifiedType() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1670 | == CanonTo.getLocalUnqualifiedType() && |
Matt Arsenault | 5509f37 | 2013-02-26 21:15:54 +0000 | [diff] [blame] | 1671 | CanonFrom.getLocalQualifiers() != CanonTo.getLocalQualifiers()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1672 | FromType = ToType; |
| 1673 | CanonFrom = CanonTo; |
| 1674 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1675 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1676 | SCS.setToType(2, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1677 | |
| 1678 | // If we have not converted the argument type to the parameter type, |
| 1679 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1680 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1681 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1682 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1683 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1684 | } |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1685 | |
| 1686 | static bool |
| 1687 | IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 1688 | QualType &ToType, |
| 1689 | bool InOverloadResolution, |
| 1690 | StandardConversionSequence &SCS, |
| 1691 | bool CStyle) { |
| 1692 | |
| 1693 | const RecordType *UT = ToType->getAsUnionType(); |
| 1694 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 1695 | return false; |
| 1696 | // The field to initialize within the transparent union. |
| 1697 | RecordDecl *UD = UT->getDecl(); |
| 1698 | // It's compatible if the expression matches any of the fields. |
| 1699 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1700 | itend = UD->field_end(); |
| 1701 | it != itend; ++it) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1702 | if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS, |
| 1703 | CStyle, /*ObjCWritebackConversion=*/false)) { |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1704 | ToType = it->getType(); |
| 1705 | return true; |
| 1706 | } |
| 1707 | } |
| 1708 | return false; |
| 1709 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1710 | |
| 1711 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1712 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1713 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1714 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1715 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1716 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1717 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1718 | if (!To) { |
| 1719 | return false; |
| 1720 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1721 | |
| 1722 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1723 | // unsigned short int can be converted to an rvalue of type int if |
| 1724 | // int can represent all the values of the source type; otherwise, |
| 1725 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1726 | // int (C++ 4.5p1). |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1727 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1728 | !FromType->isEnumeralType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1729 | if (// We can promote any signed, promotable integer type to an int |
| 1730 | (FromType->isSignedIntegerType() || |
| 1731 | // We can promote any unsigned integer type whose size is |
| 1732 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1733 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1734 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1735 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1738 | return To->getKind() == BuiltinType::UInt; |
| 1739 | } |
| 1740 | |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1741 | // C++11 [conv.prom]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1742 | // A prvalue of an unscoped enumeration type whose underlying type is not |
| 1743 | // fixed (7.2) can be converted to an rvalue a prvalue of the first of the |
| 1744 | // following types that can represent all the values of the enumeration |
| 1745 | // (i.e., the values in the range bmin to bmax as described in 7.2): int, |
| 1746 | // unsigned int, long int, unsigned long int, long long int, or unsigned |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1747 | // 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] | 1748 | // values of the enumeration, an rvalue a prvalue of an unscoped enumeration |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1749 | // 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] | 1750 | // with lowest integer conversion rank (4.13) greater than the rank of long |
| 1751 | // 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] | 1752 | // there are two such extended types, the signed one is chosen. |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1753 | // C++11 [conv.prom]p4: |
| 1754 | // A prvalue of an unscoped enumeration type whose underlying type is fixed |
| 1755 | // can be converted to a prvalue of its underlying type. Moreover, if |
| 1756 | // integral promotion can be applied to its underlying type, a prvalue of an |
| 1757 | // unscoped enumeration type whose underlying type is fixed can also be |
| 1758 | // converted to a prvalue of the promoted underlying type. |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1759 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
| 1760 | // C++0x 7.2p9: Note that this implicit enum to int conversion is not |
| 1761 | // provided for a scoped enumeration. |
| 1762 | if (FromEnumType->getDecl()->isScoped()) |
| 1763 | return false; |
| 1764 | |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1765 | // We can perform an integral promotion to the underlying type of the enum, |
| 1766 | // even if that's not the promoted type. |
| 1767 | if (FromEnumType->getDecl()->isFixed()) { |
| 1768 | QualType Underlying = FromEnumType->getDecl()->getIntegerType(); |
| 1769 | return Context.hasSameUnqualifiedType(Underlying, ToType) || |
| 1770 | IsIntegralPromotion(From, Underlying, ToType); |
| 1771 | } |
| 1772 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1773 | // We have already pre-calculated the promotion type, so this is trivial. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1774 | if (ToType->isIntegerType() && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1775 | !RequireCompleteType(From->getLocStart(), FromType, 0)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1776 | return Context.hasSameUnqualifiedType(ToType, |
| 1777 | FromEnumType->getDecl()->getPromotionType()); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1778 | } |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1779 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1780 | // C++0x [conv.prom]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1781 | // A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted |
| 1782 | // to an rvalue a prvalue of the first of the following types that can |
| 1783 | // represent all the values of its underlying type: int, unsigned int, |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1784 | // 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] | 1785 | // 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] | 1786 | // underlying type, an rvalue a prvalue of type char16_t, char32_t, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1787 | // 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] | 1788 | // type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1789 | if (FromType->isAnyCharacterType() && !FromType->isCharType() && |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1790 | ToType->isIntegerType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1791 | // Determine whether the type we're converting from is signed or |
| 1792 | // unsigned. |
David Majnemer | 0ad9231 | 2011-07-22 21:09:04 +0000 | [diff] [blame] | 1793 | bool FromIsSigned = FromType->isSignedIntegerType(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1794 | uint64_t FromSize = Context.getTypeSize(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1795 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1796 | // The types we'll try to promote to, in the appropriate |
| 1797 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1798 | QualType PromoteTypes[6] = { |
| 1799 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1800 | Context.LongTy, Context.UnsignedLongTy , |
| 1801 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1802 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1803 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1804 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1805 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1806 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1807 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1808 | // We found the type that we can promote to. If this is the |
| 1809 | // type we wanted, we have a promotion. Otherwise, no |
| 1810 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1811 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1812 | } |
| 1813 | } |
| 1814 | } |
| 1815 | |
| 1816 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1817 | // rvalue of type int if int can represent all the values of the |
| 1818 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1819 | // unsigned int can represent all the values of the bit-field. If |
| 1820 | // the bit-field is larger yet, no integral promotion applies to |
| 1821 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1822 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1823 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1824 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1825 | using llvm::APSInt; |
| 1826 | if (From) |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 1827 | if (FieldDecl *MemberDecl = From->getSourceBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1828 | APSInt BitWidth; |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1829 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1830 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1831 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1832 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1833 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1834 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1835 | if (BitWidth < ToSize || |
| 1836 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1837 | return To->getKind() == BuiltinType::Int; |
| 1838 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1839 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1840 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1841 | // that fits into an unsigned int? |
| 1842 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1843 | return To->getKind() == BuiltinType::UInt; |
| 1844 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1845 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1846 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1847 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1848 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1849 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1850 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1851 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1852 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1853 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1854 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1855 | |
| 1856 | return false; |
| 1857 | } |
| 1858 | |
| 1859 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1860 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1861 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1862 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1863 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1864 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1865 | /// An rvalue of type float can be converted to an rvalue of type |
| 1866 | /// double. (C++ 4.6p1). |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1867 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1868 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1869 | return true; |
| 1870 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1871 | // C99 6.3.1.5p1: |
| 1872 | // When a float is promoted to double or long double, or a |
| 1873 | // double is promoted to long double [...]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1874 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1875 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1876 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1877 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1878 | return true; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1879 | |
| 1880 | // Half can be promoted to float. |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 1881 | if (!getLangOpts().NativeHalfType && |
| 1882 | FromBuiltin->getKind() == BuiltinType::Half && |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1883 | ToBuiltin->getKind() == BuiltinType::Float) |
| 1884 | return true; |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1887 | return false; |
| 1888 | } |
| 1889 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1890 | /// \brief Determine if a conversion is a complex promotion. |
| 1891 | /// |
| 1892 | /// A complex promotion is defined as a complex -> complex conversion |
| 1893 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1894 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1895 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1896 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1897 | if (!FromComplex) |
| 1898 | return false; |
| 1899 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1900 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1901 | if (!ToComplex) |
| 1902 | return false; |
| 1903 | |
| 1904 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1905 | ToComplex->getElementType()) || |
| 1906 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1907 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1910 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1911 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1912 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1913 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1914 | /// the right set of qualifiers on its pointee. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1915 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1916 | static QualType |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1917 | BuildSimilarlyQualifiedPointerType(const Type *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1918 | QualType ToPointee, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1919 | ASTContext &Context, |
| 1920 | bool StripObjCLifetime = false) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1921 | assert((FromPtr->getTypeClass() == Type::Pointer || |
| 1922 | FromPtr->getTypeClass() == Type::ObjCObjectPointer) && |
| 1923 | "Invalid similarly-qualified pointer type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1924 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1925 | /// Conversions to 'id' subsume cv-qualifier conversions. |
| 1926 | if (ToType->isObjCIdType() || ToType->isObjCQualifiedIdType()) |
Douglas Gregor | 143c7ac | 2010-12-06 22:09:19 +0000 | [diff] [blame] | 1927 | return ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1928 | |
| 1929 | QualType CanonFromPointee |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1930 | = Context.getCanonicalType(FromPtr->getPointeeType()); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1931 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1932 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1933 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1934 | if (StripObjCLifetime) |
| 1935 | Quals.removeObjCLifetime(); |
| 1936 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1937 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1938 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1939 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1940 | if (!ToType.isNull()) |
Douglas Gregor | af7bea5 | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1941 | return ToType.getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1942 | |
| 1943 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1944 | // already. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1945 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1946 | return Context.getObjCObjectPointerType(ToPointee); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1947 | return Context.getPointerType(ToPointee); |
| 1948 | } |
| 1949 | |
| 1950 | // Just build a canonical type that has the right qualifiers. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1951 | QualType QualifiedCanonToPointee |
| 1952 | = Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1953 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1954 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1955 | return Context.getObjCObjectPointerType(QualifiedCanonToPointee); |
| 1956 | return Context.getPointerType(QualifiedCanonToPointee); |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1957 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1958 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1959 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1960 | bool InOverloadResolution, |
| 1961 | ASTContext &Context) { |
| 1962 | // Handle value-dependent integral null pointer constants correctly. |
| 1963 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1964 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1965 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1966 | return !InOverloadResolution; |
| 1967 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1968 | return Expr->isNullPointerConstant(Context, |
| 1969 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1970 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1971 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1972 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1973 | /// IsPointerConversion - Determines whether the conversion of the |
| 1974 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1975 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1976 | /// 4.10). If so, returns true and places the converted type (that |
| 1977 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1978 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1979 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1980 | /// This routine also supports conversions to and from block pointers |
| 1981 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1982 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1983 | /// appropriate overloading rules for Objective-C, we may want to |
| 1984 | /// split the Objective-C checks into a different routine; however, |
| 1985 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1986 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1987 | /// set if the conversion is an allowed Objective-C conversion that |
| 1988 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1989 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1990 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1991 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1992 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1993 | IncompatibleObjC = false; |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1994 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, |
| 1995 | IncompatibleObjC)) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1996 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1997 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1998 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1999 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2000 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 2001 | ConvertedType = ToType; |
| 2002 | return true; |
| 2003 | } |
| 2004 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2005 | // Blocks: Block pointers can be converted to void*. |
| 2006 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2007 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2008 | ConvertedType = ToType; |
| 2009 | return true; |
| 2010 | } |
| 2011 | // Blocks: A null pointer constant can be converted to a block |
| 2012 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2013 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2014 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2015 | ConvertedType = ToType; |
| 2016 | return true; |
| 2017 | } |
| 2018 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2019 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 2020 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2021 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2022 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2023 | ConvertedType = ToType; |
| 2024 | return true; |
| 2025 | } |
| 2026 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2027 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2028 | if (!ToTypePtr) |
| 2029 | return false; |
| 2030 | |
| 2031 | // 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] | 2032 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2033 | ConvertedType = ToType; |
| 2034 | return true; |
| 2035 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2036 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2037 | // Beyond this point, both types need to be pointers |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2038 | // , including objective-c pointers. |
| 2039 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2040 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2041 | !getLangOpts().ObjCAutoRefCount) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2042 | ConvertedType = BuildSimilarlyQualifiedPointerType( |
| 2043 | FromType->getAs<ObjCObjectPointerType>(), |
| 2044 | ToPointeeType, |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2045 | ToType, Context); |
| 2046 | return true; |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2047 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2048 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2049 | if (!FromTypePtr) |
| 2050 | return false; |
| 2051 | |
| 2052 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2053 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2054 | // 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] | 2055 | // pointer conversion, so don't do all of the work below. |
| 2056 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
| 2057 | return false; |
| 2058 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2059 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 2060 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 2061 | // 4.10p2). |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 2062 | if (FromPointeeType->isIncompleteOrObjectType() && |
| 2063 | ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2064 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2065 | ToPointeeType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2066 | ToType, Context, |
| 2067 | /*StripObjCLifetime=*/true); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2068 | return true; |
| 2069 | } |
| 2070 | |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2071 | // MSVC allows implicit function to void* type conversion. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2072 | if (getLangOpts().MicrosoftExt && FromPointeeType->isFunctionType() && |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2073 | ToPointeeType->isVoidType()) { |
| 2074 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2075 | ToPointeeType, |
| 2076 | ToType, Context); |
| 2077 | return true; |
| 2078 | } |
| 2079 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2080 | // When we're overloading in C, we allow a special kind of pointer |
| 2081 | // conversion for compatible-but-not-identical pointee types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2082 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2083 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2084 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2085 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2086 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2087 | return true; |
| 2088 | } |
| 2089 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2090 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2091 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2092 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 2093 | // can be converted to an rvalue of type "pointer to cv B," where |
| 2094 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 2095 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 2096 | // necessitates this conversion is ill-formed. The result of the |
| 2097 | // conversion is a pointer to the base class sub-object of the |
| 2098 | // derived class object. The null pointer value is converted to |
| 2099 | // the null pointer value of the destination type. |
| 2100 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2101 | // Note that we do not check for ambiguity or inaccessibility |
| 2102 | // here. That is handled by CheckPointerConversion. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2103 | if (getLangOpts().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2104 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | bf1764c | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 2105 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2106 | !RequireCompleteType(From->getLocStart(), FromPointeeType, 0) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2107 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2108 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2109 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2110 | ToType, Context); |
| 2111 | return true; |
| 2112 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2113 | |
Fariborz Jahanian | 5da3c08 | 2011-04-14 20:33:36 +0000 | [diff] [blame] | 2114 | if (FromPointeeType->isVectorType() && ToPointeeType->isVectorType() && |
| 2115 | Context.areCompatibleVectorTypes(FromPointeeType, ToPointeeType)) { |
| 2116 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2117 | ToPointeeType, |
| 2118 | ToType, Context); |
| 2119 | return true; |
| 2120 | } |
| 2121 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2122 | return false; |
| 2123 | } |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2124 | |
| 2125 | /// \brief Adopt the given qualifiers for the given type. |
| 2126 | static QualType AdoptQualifiers(ASTContext &Context, QualType T, Qualifiers Qs){ |
| 2127 | Qualifiers TQs = T.getQualifiers(); |
| 2128 | |
| 2129 | // Check whether qualifiers already match. |
| 2130 | if (TQs == Qs) |
| 2131 | return T; |
| 2132 | |
| 2133 | if (Qs.compatiblyIncludes(TQs)) |
| 2134 | return Context.getQualifiedType(T, Qs); |
| 2135 | |
| 2136 | return Context.getQualifiedType(T.getUnqualifiedType(), Qs); |
| 2137 | } |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2138 | |
| 2139 | /// isObjCPointerConversion - Determines whether this is an |
| 2140 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 2141 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2142 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2143 | QualType& ConvertedType, |
| 2144 | bool &IncompatibleObjC) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2145 | if (!getLangOpts().ObjC1) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2146 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2147 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2148 | // The set of qualifiers on the type we're converting from. |
| 2149 | Qualifiers FromQualifiers = FromType.getQualifiers(); |
| 2150 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2151 | // First, we handle all conversions on ObjC object pointer types. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2152 | const ObjCObjectPointerType* ToObjCPtr = |
| 2153 | ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2154 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2155 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2156 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2157 | if (ToObjCPtr && FromObjCPtr) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2158 | // If the pointee types are the same (ignoring qualifications), |
| 2159 | // then this is not a pointer conversion. |
| 2160 | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
| 2161 | FromObjCPtr->getPointeeType())) |
| 2162 | return false; |
| 2163 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2164 | // Check for compatible |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2165 | // 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] | 2166 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2167 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2168 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2169 | return true; |
| 2170 | } |
| 2171 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2172 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2173 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2174 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 2175 | /*compare=*/false)) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2176 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2177 | return true; |
| 2178 | } |
| 2179 | // Objective C++: We're able to convert from a pointer to an |
| 2180 | // interface to a pointer to a different interface. |
| 2181 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2182 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 2183 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2184 | if (getLangOpts().CPlusPlus && LHS && RHS && |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2185 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 2186 | FromObjCPtr->getPointeeType())) |
| 2187 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2188 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2189 | ToObjCPtr->getPointeeType(), |
| 2190 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2191 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2192 | return true; |
| 2193 | } |
| 2194 | |
| 2195 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 2196 | // Okay: this is some kind of implicit downcast of Objective-C |
| 2197 | // interfaces, which is permitted. However, we're going to |
| 2198 | // complain about it. |
| 2199 | IncompatibleObjC = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2200 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2201 | ToObjCPtr->getPointeeType(), |
| 2202 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2203 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2204 | return true; |
| 2205 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2206 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2207 | // 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] | 2208 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2209 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2210 | ToPointeeType = ToCPtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2211 | else if (const BlockPointerType *ToBlockPtr = |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2212 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 2213 | // 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] | 2214 | // to a block pointer type. |
| 2215 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2216 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2217 | return true; |
| 2218 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2219 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2220 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2221 | else if (FromType->getAs<BlockPointerType>() && |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2222 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2223 | // 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] | 2224 | // pointer to any object. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2225 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2226 | return true; |
| 2227 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2228 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2229 | return false; |
| 2230 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2231 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2232 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2233 | FromPointeeType = FromCPtr->getPointeeType(); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2234 | else if (const BlockPointerType *FromBlockPtr = |
| 2235 | FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2236 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2237 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2238 | return false; |
| 2239 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2240 | // If we have pointers to pointers, recursively check whether this |
| 2241 | // is an Objective-C conversion. |
| 2242 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 2243 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2244 | IncompatibleObjC)) { |
| 2245 | // We always complain about this conversion. |
| 2246 | IncompatibleObjC = true; |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2247 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2248 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2249 | return true; |
| 2250 | } |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2251 | // Allow conversion of pointee being objective-c pointer to another one; |
| 2252 | // as in I* to id. |
| 2253 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 2254 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 2255 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2256 | IncompatibleObjC)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2257 | |
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); |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2260 | return true; |
| 2261 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2262 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2263 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2264 | // differences in the argument and result types are in Objective-C |
| 2265 | // pointer conversions. If so, we permit the conversion (but |
| 2266 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2267 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2268 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2269 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2270 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2271 | if (FromFunctionType && ToFunctionType) { |
| 2272 | // If the function types are exactly the same, this isn't an |
| 2273 | // Objective-C pointer conversion. |
| 2274 | if (Context.getCanonicalType(FromPointeeType) |
| 2275 | == Context.getCanonicalType(ToPointeeType)) |
| 2276 | return false; |
| 2277 | |
| 2278 | // Perform the quick checks that will tell us whether these |
| 2279 | // function types are obviously different. |
| 2280 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2281 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 2282 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 2283 | return false; |
| 2284 | |
| 2285 | bool HasObjCConversion = false; |
| 2286 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 2287 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 2288 | // Okay, the types match exactly. Nothing to do. |
| 2289 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 2290 | ToFunctionType->getResultType(), |
| 2291 | ConvertedType, IncompatibleObjC)) { |
| 2292 | // Okay, we have an Objective-C pointer conversion. |
| 2293 | HasObjCConversion = true; |
| 2294 | } else { |
| 2295 | // Function types are too different. Abort. |
| 2296 | return false; |
| 2297 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2298 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2299 | // Check argument types. |
| 2300 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2301 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2302 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2303 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2304 | if (Context.getCanonicalType(FromArgType) |
| 2305 | == Context.getCanonicalType(ToArgType)) { |
| 2306 | // Okay, the types match exactly. Nothing to do. |
| 2307 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 2308 | ConvertedType, IncompatibleObjC)) { |
| 2309 | // Okay, we have an Objective-C pointer conversion. |
| 2310 | HasObjCConversion = true; |
| 2311 | } else { |
| 2312 | // Argument types are too different. Abort. |
| 2313 | return false; |
| 2314 | } |
| 2315 | } |
| 2316 | |
| 2317 | if (HasObjCConversion) { |
| 2318 | // We had an Objective-C conversion. Allow this pointer |
| 2319 | // conversion, but complain about it. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2320 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2321 | IncompatibleObjC = true; |
| 2322 | return true; |
| 2323 | } |
| 2324 | } |
| 2325 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2326 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2327 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2328 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2329 | /// \brief Determine whether this is an Objective-C writeback conversion, |
| 2330 | /// used for parameter passing when performing automatic reference counting. |
| 2331 | /// |
| 2332 | /// \param FromType The type we're converting form. |
| 2333 | /// |
| 2334 | /// \param ToType The type we're converting to. |
| 2335 | /// |
| 2336 | /// \param ConvertedType The type that will be produced after applying |
| 2337 | /// this conversion. |
| 2338 | bool Sema::isObjCWritebackConversion(QualType FromType, QualType ToType, |
| 2339 | QualType &ConvertedType) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2340 | if (!getLangOpts().ObjCAutoRefCount || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2341 | Context.hasSameUnqualifiedType(FromType, ToType)) |
| 2342 | return false; |
| 2343 | |
| 2344 | // Parameter must be a pointer to __autoreleasing (with no other qualifiers). |
| 2345 | QualType ToPointee; |
| 2346 | if (const PointerType *ToPointer = ToType->getAs<PointerType>()) |
| 2347 | ToPointee = ToPointer->getPointeeType(); |
| 2348 | else |
| 2349 | return false; |
| 2350 | |
| 2351 | Qualifiers ToQuals = ToPointee.getQualifiers(); |
| 2352 | if (!ToPointee->isObjCLifetimeType() || |
| 2353 | ToQuals.getObjCLifetime() != Qualifiers::OCL_Autoreleasing || |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2354 | !ToQuals.withoutObjCLifetime().empty()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2355 | return false; |
| 2356 | |
| 2357 | // Argument must be a pointer to __strong to __weak. |
| 2358 | QualType FromPointee; |
| 2359 | if (const PointerType *FromPointer = FromType->getAs<PointerType>()) |
| 2360 | FromPointee = FromPointer->getPointeeType(); |
| 2361 | else |
| 2362 | return false; |
| 2363 | |
| 2364 | Qualifiers FromQuals = FromPointee.getQualifiers(); |
| 2365 | if (!FromPointee->isObjCLifetimeType() || |
| 2366 | (FromQuals.getObjCLifetime() != Qualifiers::OCL_Strong && |
| 2367 | FromQuals.getObjCLifetime() != Qualifiers::OCL_Weak)) |
| 2368 | return false; |
| 2369 | |
| 2370 | // Make sure that we have compatible qualifiers. |
| 2371 | FromQuals.setObjCLifetime(Qualifiers::OCL_Autoreleasing); |
| 2372 | if (!ToQuals.compatiblyIncludes(FromQuals)) |
| 2373 | return false; |
| 2374 | |
| 2375 | // Remove qualifiers from the pointee type we're converting from; they |
| 2376 | // aren't used in the compatibility check belong, and we'll be adding back |
| 2377 | // qualifiers (with __autoreleasing) if the compatibility check succeeds. |
| 2378 | FromPointee = FromPointee.getUnqualifiedType(); |
| 2379 | |
| 2380 | // The unqualified form of the pointee types must be compatible. |
| 2381 | ToPointee = ToPointee.getUnqualifiedType(); |
| 2382 | bool IncompatibleObjC; |
| 2383 | if (Context.typesAreCompatible(FromPointee, ToPointee)) |
| 2384 | FromPointee = ToPointee; |
| 2385 | else if (!isObjCPointerConversion(FromPointee, ToPointee, FromPointee, |
| 2386 | IncompatibleObjC)) |
| 2387 | return false; |
| 2388 | |
| 2389 | /// \brief Construct the type we're converting to, which is a pointer to |
| 2390 | /// __autoreleasing pointee. |
| 2391 | FromPointee = Context.getQualifiedType(FromPointee, FromQuals); |
| 2392 | ConvertedType = Context.getPointerType(FromPointee); |
| 2393 | return true; |
| 2394 | } |
| 2395 | |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2396 | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
| 2397 | QualType& ConvertedType) { |
| 2398 | QualType ToPointeeType; |
| 2399 | if (const BlockPointerType *ToBlockPtr = |
| 2400 | ToType->getAs<BlockPointerType>()) |
| 2401 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 2402 | else |
| 2403 | return false; |
| 2404 | |
| 2405 | QualType FromPointeeType; |
| 2406 | if (const BlockPointerType *FromBlockPtr = |
| 2407 | FromType->getAs<BlockPointerType>()) |
| 2408 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2409 | else |
| 2410 | return false; |
| 2411 | // We have pointer to blocks, check whether the only |
| 2412 | // differences in the argument and result types are in Objective-C |
| 2413 | // pointer conversions. If so, we permit the conversion. |
| 2414 | |
| 2415 | const FunctionProtoType *FromFunctionType |
| 2416 | = FromPointeeType->getAs<FunctionProtoType>(); |
| 2417 | const FunctionProtoType *ToFunctionType |
| 2418 | = ToPointeeType->getAs<FunctionProtoType>(); |
| 2419 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2420 | if (!FromFunctionType || !ToFunctionType) |
| 2421 | return false; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2422 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2423 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2424 | return true; |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2425 | |
| 2426 | // Perform the quick checks that will tell us whether these |
| 2427 | // function types are obviously different. |
| 2428 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2429 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
| 2430 | return false; |
| 2431 | |
| 2432 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
| 2433 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
| 2434 | if (FromEInfo != ToEInfo) |
| 2435 | return false; |
| 2436 | |
| 2437 | bool IncompatibleObjC = false; |
Fariborz Jahanian | 462dae5 | 2011-02-13 20:11:42 +0000 | [diff] [blame] | 2438 | if (Context.hasSameType(FromFunctionType->getResultType(), |
| 2439 | ToFunctionType->getResultType())) { |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2440 | // Okay, the types match exactly. Nothing to do. |
| 2441 | } else { |
| 2442 | QualType RHS = FromFunctionType->getResultType(); |
| 2443 | QualType LHS = ToFunctionType->getResultType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2444 | if ((!getLangOpts().CPlusPlus || !RHS->isRecordType()) && |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2445 | !RHS.hasQualifiers() && LHS.hasQualifiers()) |
| 2446 | LHS = LHS.getUnqualifiedType(); |
| 2447 | |
| 2448 | if (Context.hasSameType(RHS,LHS)) { |
| 2449 | // OK exact match. |
| 2450 | } else if (isObjCPointerConversion(RHS, LHS, |
| 2451 | ConvertedType, IncompatibleObjC)) { |
| 2452 | if (IncompatibleObjC) |
| 2453 | return false; |
| 2454 | // Okay, we have an Objective-C pointer conversion. |
| 2455 | } |
| 2456 | else |
| 2457 | return false; |
| 2458 | } |
| 2459 | |
| 2460 | // Check argument types. |
| 2461 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2462 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2463 | IncompatibleObjC = false; |
| 2464 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2465 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2466 | if (Context.hasSameType(FromArgType, ToArgType)) { |
| 2467 | // Okay, the types match exactly. Nothing to do. |
| 2468 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
| 2469 | ConvertedType, IncompatibleObjC)) { |
| 2470 | if (IncompatibleObjC) |
| 2471 | return false; |
| 2472 | // Okay, we have an Objective-C pointer conversion. |
| 2473 | } else |
| 2474 | // Argument types are too different. Abort. |
| 2475 | return false; |
| 2476 | } |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 2477 | if (LangOpts.ObjCAutoRefCount && |
| 2478 | !Context.FunctionTypesMatchOnNSConsumedAttrs(FromFunctionType, |
| 2479 | ToFunctionType)) |
| 2480 | return false; |
Fariborz Jahanian | f9d9527 | 2011-09-28 20:22:05 +0000 | [diff] [blame] | 2481 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2482 | ConvertedType = ToType; |
| 2483 | return true; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2484 | } |
| 2485 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2486 | enum { |
| 2487 | ft_default, |
| 2488 | ft_different_class, |
| 2489 | ft_parameter_arity, |
| 2490 | ft_parameter_mismatch, |
| 2491 | ft_return_type, |
| 2492 | ft_qualifer_mismatch |
| 2493 | }; |
| 2494 | |
| 2495 | /// HandleFunctionTypeMismatch - Gives diagnostic information for differeing |
| 2496 | /// function types. Catches different number of parameter, mismatch in |
| 2497 | /// parameter types, and different return types. |
| 2498 | void Sema::HandleFunctionTypeMismatch(PartialDiagnostic &PDiag, |
| 2499 | QualType FromType, QualType ToType) { |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2500 | // If either type is not valid, include no extra info. |
| 2501 | if (FromType.isNull() || ToType.isNull()) { |
| 2502 | PDiag << ft_default; |
| 2503 | return; |
| 2504 | } |
| 2505 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2506 | // Get the function type from the pointers. |
| 2507 | if (FromType->isMemberPointerType() && ToType->isMemberPointerType()) { |
| 2508 | const MemberPointerType *FromMember = FromType->getAs<MemberPointerType>(), |
| 2509 | *ToMember = ToType->getAs<MemberPointerType>(); |
| 2510 | if (FromMember->getClass() != ToMember->getClass()) { |
| 2511 | PDiag << ft_different_class << QualType(ToMember->getClass(), 0) |
| 2512 | << QualType(FromMember->getClass(), 0); |
| 2513 | return; |
| 2514 | } |
| 2515 | FromType = FromMember->getPointeeType(); |
| 2516 | ToType = ToMember->getPointeeType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2517 | } |
| 2518 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2519 | if (FromType->isPointerType()) |
| 2520 | FromType = FromType->getPointeeType(); |
| 2521 | if (ToType->isPointerType()) |
| 2522 | ToType = ToType->getPointeeType(); |
| 2523 | |
| 2524 | // Remove references. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2525 | FromType = FromType.getNonReferenceType(); |
| 2526 | ToType = ToType.getNonReferenceType(); |
| 2527 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2528 | // Don't print extra info for non-specialized template functions. |
| 2529 | if (FromType->isInstantiationDependentType() && |
| 2530 | !FromType->getAs<TemplateSpecializationType>()) { |
| 2531 | PDiag << ft_default; |
| 2532 | return; |
| 2533 | } |
| 2534 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2535 | // No extra info for same types. |
| 2536 | if (Context.hasSameType(FromType, ToType)) { |
| 2537 | PDiag << ft_default; |
| 2538 | return; |
| 2539 | } |
| 2540 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2541 | const FunctionProtoType *FromFunction = FromType->getAs<FunctionProtoType>(), |
| 2542 | *ToFunction = ToType->getAs<FunctionProtoType>(); |
| 2543 | |
| 2544 | // Both types need to be function types. |
| 2545 | if (!FromFunction || !ToFunction) { |
| 2546 | PDiag << ft_default; |
| 2547 | return; |
| 2548 | } |
| 2549 | |
| 2550 | if (FromFunction->getNumArgs() != ToFunction->getNumArgs()) { |
| 2551 | PDiag << ft_parameter_arity << ToFunction->getNumArgs() |
| 2552 | << FromFunction->getNumArgs(); |
| 2553 | return; |
| 2554 | } |
| 2555 | |
| 2556 | // Handle different parameter types. |
| 2557 | unsigned ArgPos; |
| 2558 | if (!FunctionArgTypesAreEqual(FromFunction, ToFunction, &ArgPos)) { |
| 2559 | PDiag << ft_parameter_mismatch << ArgPos + 1 |
| 2560 | << ToFunction->getArgType(ArgPos) |
| 2561 | << FromFunction->getArgType(ArgPos); |
| 2562 | return; |
| 2563 | } |
| 2564 | |
| 2565 | // Handle different return type. |
| 2566 | if (!Context.hasSameType(FromFunction->getResultType(), |
| 2567 | ToFunction->getResultType())) { |
| 2568 | PDiag << ft_return_type << ToFunction->getResultType() |
| 2569 | << FromFunction->getResultType(); |
| 2570 | return; |
| 2571 | } |
| 2572 | |
| 2573 | unsigned FromQuals = FromFunction->getTypeQuals(), |
| 2574 | ToQuals = ToFunction->getTypeQuals(); |
| 2575 | if (FromQuals != ToQuals) { |
| 2576 | PDiag << ft_qualifer_mismatch << ToQuals << FromQuals; |
| 2577 | return; |
| 2578 | } |
| 2579 | |
| 2580 | // Unable to find a difference, so add no extra info. |
| 2581 | PDiag << ft_default; |
| 2582 | } |
| 2583 | |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2584 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
Douglas Gregor | dec1cc4 | 2011-12-15 17:15:07 +0000 | [diff] [blame] | 2585 | /// for equality of their argument types. Caller has already checked that |
Eli Friedman | 0601700 | 2013-06-18 22:41:37 +0000 | [diff] [blame] | 2586 | /// they have same number of arguments. If the parameters are different, |
| 2587 | /// ArgPos will have the parameter index of the first different parameter. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2588 | bool Sema::FunctionArgTypesAreEqual(const FunctionProtoType *OldType, |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2589 | const FunctionProtoType *NewType, |
| 2590 | unsigned *ArgPos) { |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2591 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 2592 | N = NewType->arg_type_begin(), |
| 2593 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
Richard Trieu | 7ea491c | 2013-08-09 21:42:32 +0000 | [diff] [blame] | 2594 | if (!Context.hasSameType(O->getUnqualifiedType(), |
| 2595 | N->getUnqualifiedType())) { |
Larisse Voufo | 3151b7c | 2013-08-06 03:57:41 +0000 | [diff] [blame] | 2596 | if (ArgPos) *ArgPos = O - OldType->arg_type_begin(); |
| 2597 | return false; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2598 | } |
| 2599 | } |
| 2600 | return true; |
| 2601 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2602 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2603 | /// CheckPointerConversion - Check the pointer conversion from the |
| 2604 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2605 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2606 | /// conversions for which IsPointerConversion has already returned |
| 2607 | /// true. It returns true and produces a diagnostic if there was an |
| 2608 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2609 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2610 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2611 | CXXCastPath& BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2612 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2613 | QualType FromType = From->getType(); |
Argyrios Kyrtzidis | b335872 | 2010-09-28 14:54:11 +0000 | [diff] [blame] | 2614 | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2615 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2616 | Kind = CK_BitCast; |
| 2617 | |
David Blaikie | 50800fc | 2012-08-08 17:33:31 +0000 | [diff] [blame] | 2618 | if (!IsCStyleOrFunctionalCast && !FromType->isAnyPointerType() && |
| 2619 | From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull) == |
| 2620 | Expr::NPCK_ZeroExpression) { |
| 2621 | if (Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy)) |
| 2622 | DiagRuntimeBehavior(From->getExprLoc(), From, |
| 2623 | PDiag(diag::warn_impcast_bool_to_null_pointer) |
| 2624 | << ToType << From->getSourceRange()); |
| 2625 | else if (!isUnevaluatedContext()) |
| 2626 | Diag(From->getExprLoc(), diag::warn_non_literal_null_pointer) |
| 2627 | << ToType << From->getSourceRange(); |
| 2628 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2629 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
| 2630 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2631 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 2632 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 2633 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2634 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 2635 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2636 | // We must have a derived-to-base conversion. Check an |
| 2637 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2638 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 2639 | From->getExprLoc(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2640 | From->getSourceRange(), &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2641 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2642 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2643 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2644 | // The conversion was successful. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2645 | Kind = CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2646 | } |
| 2647 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2648 | } else if (const ObjCObjectPointerType *ToPtrType = |
| 2649 | ToType->getAs<ObjCObjectPointerType>()) { |
| 2650 | if (const ObjCObjectPointerType *FromPtrType = |
| 2651 | FromType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2652 | // Objective-C++ conversions are always okay. |
| 2653 | // FIXME: We should have a different class of conversions for the |
| 2654 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2655 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2656 | return false; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2657 | } else if (FromType->isBlockPointerType()) { |
| 2658 | Kind = CK_BlockPointerToObjCPointerCast; |
| 2659 | } else { |
| 2660 | Kind = CK_CPointerToObjCPointerCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2661 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2662 | } else if (ToType->isBlockPointerType()) { |
| 2663 | if (!FromType->isBlockPointerType()) |
| 2664 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2665 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2666 | |
| 2667 | // We shouldn't fall into this case unless it's valid for other |
| 2668 | // reasons. |
| 2669 | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 2670 | Kind = CK_NullToPointer; |
| 2671 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2672 | return false; |
| 2673 | } |
| 2674 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2675 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 2676 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 2677 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 2678 | /// If so, returns true and places the converted type (that might differ from |
| 2679 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 2680 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2681 | QualType ToType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2682 | bool InOverloadResolution, |
| 2683 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2684 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2685 | if (!ToTypePtr) |
| 2686 | return false; |
| 2687 | |
| 2688 | // 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] | 2689 | if (From->isNullPointerConstant(Context, |
| 2690 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 2691 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2692 | ConvertedType = ToType; |
| 2693 | return true; |
| 2694 | } |
| 2695 | |
| 2696 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2697 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2698 | if (!FromTypePtr) |
| 2699 | return false; |
| 2700 | |
| 2701 | // A pointer to member of B can be converted to a pointer to member of D, |
| 2702 | // where D is derived from B (C++ 4.11p2). |
| 2703 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 2704 | QualType ToClass(ToTypePtr->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2705 | |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2706 | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2707 | !RequireCompleteType(From->getLocStart(), ToClass, 0) && |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2708 | IsDerivedFrom(ToClass, FromClass)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2709 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 2710 | ToClass.getTypePtr()); |
| 2711 | return true; |
| 2712 | } |
| 2713 | |
| 2714 | return false; |
| 2715 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2716 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2717 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 2718 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2719 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2720 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 2721 | /// true and produces a diagnostic if there was an error, or returns false |
| 2722 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2723 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2724 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2725 | CXXCastPath &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2726 | bool IgnoreBaseAccess) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2727 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2728 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2729 | if (!FromPtrType) { |
| 2730 | // This must be a null pointer to member pointer conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2731 | assert(From->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2732 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2733 | "Expr must be null pointer constant!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2734 | Kind = CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2735 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2736 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2737 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2738 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2739 | assert(ToPtrType && "No member pointer cast has a target type " |
| 2740 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2741 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2742 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 2743 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2744 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2745 | // FIXME: What about dependent types? |
| 2746 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 2747 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2748 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2749 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 2750 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2751 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 2752 | assert(DerivationOkay && |
| 2753 | "Should not have been called if derivation isn't OK."); |
| 2754 | (void)DerivationOkay; |
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 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 2757 | getUnqualifiedType())) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2758 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 2759 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 2760 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 2761 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2762 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2763 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2764 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2765 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 2766 | << FromClass << ToClass << QualType(VBase, 0) |
| 2767 | << From->getSourceRange(); |
| 2768 | return true; |
| 2769 | } |
| 2770 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2771 | if (!IgnoreBaseAccess) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2772 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 2773 | Paths.front(), |
| 2774 | diag::err_downcast_from_inaccessible_base); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2775 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2776 | // Must be a base to derived member conversion. |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2777 | BuildBasePathArray(Paths, BasePath); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2778 | Kind = CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2779 | return false; |
| 2780 | } |
| 2781 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2782 | /// IsQualificationConversion - Determines whether the conversion from |
| 2783 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 2784 | /// (C++ 4.4). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2785 | /// |
| 2786 | /// \param ObjCLifetimeConversion Output parameter that will be set to indicate |
| 2787 | /// when the qualification conversion involves a change in the Objective-C |
| 2788 | /// object lifetime. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2789 | bool |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2790 | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2791 | bool CStyle, bool &ObjCLifetimeConversion) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2792 | FromType = Context.getCanonicalType(FromType); |
| 2793 | ToType = Context.getCanonicalType(ToType); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2794 | ObjCLifetimeConversion = false; |
| 2795 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2796 | // If FromType and ToType are the same type, this is not a |
| 2797 | // qualification conversion. |
Sebastian Redl | 22c9240 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 2798 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2799 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2800 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2801 | // (C++ 4.4p4): |
| 2802 | // A conversion can add cv-qualifiers at levels other than the first |
| 2803 | // in multi-level pointers, subject to the following rules: [...] |
| 2804 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2805 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2806 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2807 | // Within each iteration of the loop, we check the qualifiers to |
| 2808 | // determine if this still looks like a qualification |
| 2809 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2810 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2811 | // until there are no more pointers or pointers-to-members left to |
| 2812 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2813 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2814 | |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2815 | Qualifiers FromQuals = FromType.getQualifiers(); |
| 2816 | Qualifiers ToQuals = ToType.getQualifiers(); |
| 2817 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2818 | // Objective-C ARC: |
| 2819 | // Check Objective-C lifetime conversions. |
| 2820 | if (FromQuals.getObjCLifetime() != ToQuals.getObjCLifetime() && |
| 2821 | UnwrappedAnyPointer) { |
| 2822 | if (ToQuals.compatiblyIncludesObjCLifetime(FromQuals)) { |
| 2823 | ObjCLifetimeConversion = true; |
| 2824 | FromQuals.removeObjCLifetime(); |
| 2825 | ToQuals.removeObjCLifetime(); |
| 2826 | } else { |
| 2827 | // Qualification conversions cannot cast between different |
| 2828 | // Objective-C lifetime qualifiers. |
| 2829 | return false; |
| 2830 | } |
| 2831 | } |
| 2832 | |
Douglas Gregor | 377e1bd | 2011-05-08 06:09:53 +0000 | [diff] [blame] | 2833 | // Allow addition/removal of GC attributes but not changing GC attributes. |
| 2834 | if (FromQuals.getObjCGCAttr() != ToQuals.getObjCGCAttr() && |
| 2835 | (!FromQuals.hasObjCGCAttr() || !ToQuals.hasObjCGCAttr())) { |
| 2836 | FromQuals.removeObjCGCAttr(); |
| 2837 | ToQuals.removeObjCGCAttr(); |
| 2838 | } |
| 2839 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2840 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 2841 | // 2,j, and similarly for volatile. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2842 | if (!CStyle && !ToQuals.compatiblyIncludes(FromQuals)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2843 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2844 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2845 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 2846 | // every cv for 0 < k < j. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2847 | if (!CStyle && FromQuals.getCVRQualifiers() != ToQuals.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2848 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2849 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2850 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2851 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 2852 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2853 | PreviousToQualsIncludeConst |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2854 | = PreviousToQualsIncludeConst && ToQuals.hasConst(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2855 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2856 | |
| 2857 | // We are left with FromType and ToType being the pointee types |
| 2858 | // after unwrapping the original FromType and ToType the same number |
| 2859 | // of types. If we unwrapped any pointers, and if FromType and |
| 2860 | // ToType have the same unqualified type (since we checked |
| 2861 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2862 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2865 | /// \brief - Determine whether this is a conversion from a scalar type to an |
| 2866 | /// atomic type. |
| 2867 | /// |
| 2868 | /// If successful, updates \c SCS's second and third steps in the conversion |
| 2869 | /// sequence to finish the conversion. |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 2870 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 2871 | bool InOverloadResolution, |
| 2872 | StandardConversionSequence &SCS, |
| 2873 | bool CStyle) { |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2874 | const AtomicType *ToAtomic = ToType->getAs<AtomicType>(); |
| 2875 | if (!ToAtomic) |
| 2876 | return false; |
| 2877 | |
| 2878 | StandardConversionSequence InnerSCS; |
| 2879 | if (!IsStandardConversion(S, From, ToAtomic->getValueType(), |
| 2880 | InOverloadResolution, InnerSCS, |
| 2881 | CStyle, /*AllowObjCWritebackConversion=*/false)) |
| 2882 | return false; |
| 2883 | |
| 2884 | SCS.Second = InnerSCS.Second; |
| 2885 | SCS.setToType(1, InnerSCS.getToType(1)); |
| 2886 | SCS.Third = InnerSCS.Third; |
| 2887 | SCS.QualificationIncludesObjCLifetime |
| 2888 | = InnerSCS.QualificationIncludesObjCLifetime; |
| 2889 | SCS.setToType(2, InnerSCS.getToType(2)); |
| 2890 | return true; |
| 2891 | } |
| 2892 | |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2893 | static bool isFirstArgumentCompatibleWithType(ASTContext &Context, |
| 2894 | CXXConstructorDecl *Constructor, |
| 2895 | QualType Type) { |
| 2896 | const FunctionProtoType *CtorType = |
| 2897 | Constructor->getType()->getAs<FunctionProtoType>(); |
| 2898 | if (CtorType->getNumArgs() > 0) { |
| 2899 | QualType FirstArg = CtorType->getArgType(0); |
| 2900 | if (Context.hasSameUnqualifiedType(Type, FirstArg.getNonReferenceType())) |
| 2901 | return true; |
| 2902 | } |
| 2903 | return false; |
| 2904 | } |
| 2905 | |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2906 | static OverloadingResult |
| 2907 | IsInitializerListConstructorConversion(Sema &S, Expr *From, QualType ToType, |
| 2908 | CXXRecordDecl *To, |
| 2909 | UserDefinedConversionSequence &User, |
| 2910 | OverloadCandidateSet &CandidateSet, |
| 2911 | bool AllowExplicit) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2912 | DeclContext::lookup_result R = S.LookupConstructors(To); |
| 2913 | for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2914 | Con != ConEnd; ++Con) { |
| 2915 | NamedDecl *D = *Con; |
| 2916 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2917 | |
| 2918 | // Find the constructor (which may be a template). |
| 2919 | CXXConstructorDecl *Constructor = 0; |
| 2920 | FunctionTemplateDecl *ConstructorTmpl |
| 2921 | = dyn_cast<FunctionTemplateDecl>(D); |
| 2922 | if (ConstructorTmpl) |
| 2923 | Constructor |
| 2924 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 2925 | else |
| 2926 | Constructor = cast<CXXConstructorDecl>(D); |
| 2927 | |
| 2928 | bool Usable = !Constructor->isInvalidDecl() && |
| 2929 | S.isInitListConstructor(Constructor) && |
| 2930 | (AllowExplicit || !Constructor->isExplicit()); |
| 2931 | if (Usable) { |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2932 | // If the first argument is (a reference to) the target type, |
| 2933 | // suppress conversions. |
| 2934 | bool SuppressUserConversions = |
| 2935 | isFirstArgumentCompatibleWithType(S.Context, Constructor, ToType); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2936 | if (ConstructorTmpl) |
| 2937 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2938 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2939 | From, CandidateSet, |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2940 | SuppressUserConversions); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2941 | else |
| 2942 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2943 | From, CandidateSet, |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2944 | SuppressUserConversions); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2945 | } |
| 2946 | } |
| 2947 | |
| 2948 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 2949 | |
| 2950 | OverloadCandidateSet::iterator Best; |
| 2951 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
| 2952 | case OR_Success: { |
| 2953 | // Record the standard conversion we used and the conversion function. |
| 2954 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2955 | QualType ThisType = Constructor->getThisType(S.Context); |
| 2956 | // Initializer lists don't have conversions as such. |
| 2957 | User.Before.setAsIdentityConversion(); |
| 2958 | User.HadMultipleCandidates = HadMultipleCandidates; |
| 2959 | User.ConversionFunction = Constructor; |
| 2960 | User.FoundConversionFunction = Best->FoundDecl; |
| 2961 | User.After.setAsIdentityConversion(); |
| 2962 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 2963 | User.After.setAllToTypes(ToType); |
| 2964 | return OR_Success; |
| 2965 | } |
| 2966 | |
| 2967 | case OR_No_Viable_Function: |
| 2968 | return OR_No_Viable_Function; |
| 2969 | case OR_Deleted: |
| 2970 | return OR_Deleted; |
| 2971 | case OR_Ambiguous: |
| 2972 | return OR_Ambiguous; |
| 2973 | } |
| 2974 | |
| 2975 | llvm_unreachable("Invalid OverloadResult!"); |
| 2976 | } |
| 2977 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2978 | /// Determines whether there is a user-defined conversion sequence |
| 2979 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 2980 | /// ToType. If such a conversion exists, User will contain the |
| 2981 | /// user-defined conversion sequence that performs such a conversion |
| 2982 | /// and this routine will return true. Otherwise, this routine returns |
| 2983 | /// false and User is unspecified. |
| 2984 | /// |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2985 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 2986 | /// "explicit" conversion functions as well as non-explicit conversion |
| 2987 | /// functions (C++0x [class.conv.fct]p2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2988 | static OverloadingResult |
| 2989 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2990 | UserDefinedConversionSequence &User, |
| 2991 | OverloadCandidateSet &CandidateSet, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2992 | bool AllowExplicit) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2993 | // Whether we will only visit constructors. |
| 2994 | bool ConstructorsOnly = false; |
| 2995 | |
| 2996 | // If the type we are conversion to is a class type, enumerate its |
| 2997 | // constructors. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2998 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2999 | // C++ [over.match.ctor]p1: |
| 3000 | // When objects of class type are direct-initialized (8.5), or |
| 3001 | // copy-initialized from an expression of the same or a |
| 3002 | // derived class type (8.5), overload resolution selects the |
| 3003 | // constructor. [...] For copy-initialization, the candidate |
| 3004 | // functions are all the converting constructors (12.3.1) of |
| 3005 | // that class. The argument list is the expression-list within |
| 3006 | // the parentheses of the initializer. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3007 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3008 | (From->getType()->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3009 | S.IsDerivedFrom(From->getType(), ToType))) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3010 | ConstructorsOnly = true; |
| 3011 | |
Benjamin Kramer | 63b6ebe | 2012-11-23 17:04:52 +0000 | [diff] [blame] | 3012 | S.RequireCompleteType(From->getExprLoc(), ToType, 0); |
Argyrios Kyrtzidis | e36bca6 | 2011-04-22 17:45:37 +0000 | [diff] [blame] | 3013 | // RequireCompleteType may have returned true due to some invalid decl |
| 3014 | // during template instantiation, but ToType may be complete enough now |
| 3015 | // to try to recover. |
| 3016 | if (ToType->isIncompleteType()) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 3017 | // We're not going to find any constructors. |
| 3018 | } else if (CXXRecordDecl *ToRecordDecl |
| 3019 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3020 | |
| 3021 | Expr **Args = &From; |
| 3022 | unsigned NumArgs = 1; |
| 3023 | bool ListInitializing = false; |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3024 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(From)) { |
Benjamin Kramer | e575359 | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 3025 | // 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] | 3026 | OverloadingResult Result = IsInitializerListConstructorConversion( |
| 3027 | S, From, ToType, ToRecordDecl, User, CandidateSet, AllowExplicit); |
| 3028 | if (Result != OR_No_Viable_Function) |
| 3029 | return Result; |
| 3030 | // Never mind. |
| 3031 | CandidateSet.clear(); |
| 3032 | |
| 3033 | // If we're list-initializing, we pass the individual elements as |
| 3034 | // arguments, not the entire list. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3035 | Args = InitList->getInits(); |
| 3036 | NumArgs = InitList->getNumInits(); |
| 3037 | ListInitializing = true; |
| 3038 | } |
| 3039 | |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3040 | DeclContext::lookup_result R = S.LookupConstructors(ToRecordDecl); |
| 3041 | for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3042 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3043 | NamedDecl *D = *Con; |
| 3044 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3045 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3046 | // Find the constructor (which may be a template). |
| 3047 | CXXConstructorDecl *Constructor = 0; |
| 3048 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3049 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3050 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3051 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3052 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 3053 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3054 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3055 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3056 | bool Usable = !Constructor->isInvalidDecl(); |
| 3057 | if (ListInitializing) |
| 3058 | Usable = Usable && (AllowExplicit || !Constructor->isExplicit()); |
| 3059 | else |
| 3060 | Usable = Usable &&Constructor->isConvertingConstructor(AllowExplicit); |
| 3061 | if (Usable) { |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3062 | bool SuppressUserConversions = !ConstructorsOnly; |
| 3063 | if (SuppressUserConversions && ListInitializing) { |
| 3064 | SuppressUserConversions = false; |
| 3065 | if (NumArgs == 1) { |
| 3066 | // If the first argument is (a reference to) the target type, |
| 3067 | // suppress conversions. |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 3068 | SuppressUserConversions = isFirstArgumentCompatibleWithType( |
| 3069 | S.Context, Constructor, ToType); |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3070 | } |
| 3071 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3072 | if (ConstructorTmpl) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3073 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3074 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3075 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3076 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3077 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 3078 | // Allow one user-defined conversion when user specifies a |
| 3079 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3080 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3081 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3082 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3083 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3084 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3085 | } |
| 3086 | } |
| 3087 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3088 | // Enumerate conversion functions, if we're allowed to. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3089 | if (ConstructorsOnly || isa<InitListExpr>(From)) { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3090 | } else if (S.RequireCompleteType(From->getLocStart(), From->getType(), 0)) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3091 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3092 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3093 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3094 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3095 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 3096 | // Add all of the conversion functions as candidates. |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3097 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3098 | CXXRecordDecl::conversion_iterator> |
| 3099 | Conversions = FromRecordDecl->getVisibleConversionFunctions(); |
| 3100 | for (CXXRecordDecl::conversion_iterator |
| 3101 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3102 | DeclAccessPair FoundDecl = I.getPair(); |
| 3103 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3104 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3105 | if (isa<UsingShadowDecl>(D)) |
| 3106 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3107 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3108 | CXXConversionDecl *Conv; |
| 3109 | FunctionTemplateDecl *ConvTemplate; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3110 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 3111 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3112 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3113 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3114 | |
| 3115 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3116 | if (ConvTemplate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3117 | S.AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
| 3118 | ActingContext, From, ToType, |
| 3119 | CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3120 | else |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3121 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, |
| 3122 | From, ToType, CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3123 | } |
| 3124 | } |
| 3125 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3126 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3127 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3128 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3129 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3130 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3131 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3132 | case OR_Success: |
| 3133 | // Record the standard conversion we used and the conversion function. |
| 3134 | if (CXXConstructorDecl *Constructor |
| 3135 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 3136 | // C++ [over.ics.user]p1: |
| 3137 | // If the user-defined conversion is specified by a |
| 3138 | // constructor (12.3.1), the initial standard conversion |
| 3139 | // sequence converts the source type to the type required by |
| 3140 | // the argument of the constructor. |
| 3141 | // |
| 3142 | QualType ThisType = Constructor->getThisType(S.Context); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3143 | if (isa<InitListExpr>(From)) { |
| 3144 | // Initializer lists don't have conversions as such. |
| 3145 | User.Before.setAsIdentityConversion(); |
| 3146 | } else { |
| 3147 | if (Best->Conversions[0].isEllipsis()) |
| 3148 | User.EllipsisConversion = true; |
| 3149 | else { |
| 3150 | User.Before = Best->Conversions[0].Standard; |
| 3151 | User.EllipsisConversion = false; |
| 3152 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3153 | } |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3154 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3155 | User.ConversionFunction = Constructor; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3156 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3157 | User.After.setAsIdentityConversion(); |
| 3158 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 3159 | User.After.setAllToTypes(ToType); |
| 3160 | return OR_Success; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3161 | } |
| 3162 | if (CXXConversionDecl *Conversion |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3163 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 3164 | // C++ [over.ics.user]p1: |
| 3165 | // |
| 3166 | // [...] If the user-defined conversion is specified by a |
| 3167 | // conversion function (12.3.2), the initial standard |
| 3168 | // conversion sequence converts the source type to the |
| 3169 | // implicit object parameter of the conversion function. |
| 3170 | User.Before = Best->Conversions[0].Standard; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3171 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3172 | User.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3173 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3174 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3175 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3176 | // C++ [over.ics.user]p2: |
| 3177 | // The second standard conversion sequence converts the |
| 3178 | // result of the user-defined conversion to the target type |
| 3179 | // for the sequence. Since an implicit conversion sequence |
| 3180 | // is an initialization, the special rules for |
| 3181 | // initialization by user-defined conversion apply when |
| 3182 | // selecting the best user-defined conversion for a |
| 3183 | // user-defined conversion sequence (see 13.3.3 and |
| 3184 | // 13.3.3.1). |
| 3185 | User.After = Best->FinalConversion; |
| 3186 | return OR_Success; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3187 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3188 | llvm_unreachable("Not a constructor or conversion function?"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3189 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3190 | case OR_No_Viable_Function: |
| 3191 | return OR_No_Viable_Function; |
| 3192 | case OR_Deleted: |
| 3193 | // No conversion here! We're done. |
| 3194 | return OR_Deleted; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3195 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3196 | case OR_Ambiguous: |
| 3197 | return OR_Ambiguous; |
| 3198 | } |
| 3199 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3200 | llvm_unreachable("Invalid OverloadResult!"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3201 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3202 | |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3203 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3204 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3205 | ImplicitConversionSequence ICS; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3206 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3207 | OverloadingResult OvResult = |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3208 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3209 | CandidateSet, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3210 | if (OvResult == OR_Ambiguous) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3211 | Diag(From->getLocStart(), |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3212 | diag::err_typecheck_ambiguous_condition) |
| 3213 | << From->getType() << ToType << From->getSourceRange(); |
Larisse Voufo | 7419d01 | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 3214 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) { |
Larisse Voufo | 288f76a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 3215 | if (!RequireCompleteType(From->getLocStart(), ToType, |
Larisse Voufo | 7419d01 | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 3216 | diag::err_typecheck_nonviable_condition_incomplete, |
| 3217 | From->getType(), From->getSourceRange())) |
| 3218 | Diag(From->getLocStart(), |
| 3219 | diag::err_typecheck_nonviable_condition) |
| 3220 | << From->getType() << From->getSourceRange() << ToType; |
| 3221 | } |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3222 | else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3223 | return false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3224 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3225 | return true; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3226 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3227 | |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3228 | /// \brief Compare the user-defined conversion functions or constructors |
| 3229 | /// of two user-defined conversion sequences to determine whether any ordering |
| 3230 | /// is possible. |
| 3231 | static ImplicitConversionSequence::CompareKind |
| 3232 | compareConversionFunctions(Sema &S, |
| 3233 | FunctionDecl *Function1, |
| 3234 | FunctionDecl *Function2) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3235 | if (!S.getLangOpts().ObjC1 || !S.getLangOpts().CPlusPlus11) |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3236 | return ImplicitConversionSequence::Indistinguishable; |
| 3237 | |
| 3238 | // Objective-C++: |
| 3239 | // If both conversion functions are implicitly-declared conversions from |
| 3240 | // a lambda closure type to a function pointer and a block pointer, |
| 3241 | // respectively, always prefer the conversion to a function pointer, |
| 3242 | // because the function pointer is more lightweight and is more likely |
| 3243 | // to keep code working. |
| 3244 | CXXConversionDecl *Conv1 = dyn_cast<CXXConversionDecl>(Function1); |
| 3245 | if (!Conv1) |
| 3246 | return ImplicitConversionSequence::Indistinguishable; |
| 3247 | |
| 3248 | CXXConversionDecl *Conv2 = dyn_cast<CXXConversionDecl>(Function2); |
| 3249 | if (!Conv2) |
| 3250 | return ImplicitConversionSequence::Indistinguishable; |
| 3251 | |
| 3252 | if (Conv1->getParent()->isLambda() && Conv2->getParent()->isLambda()) { |
| 3253 | bool Block1 = Conv1->getConversionType()->isBlockPointerType(); |
| 3254 | bool Block2 = Conv2->getConversionType()->isBlockPointerType(); |
| 3255 | if (Block1 != Block2) |
| 3256 | return Block1? ImplicitConversionSequence::Worse |
| 3257 | : ImplicitConversionSequence::Better; |
| 3258 | } |
| 3259 | |
| 3260 | return ImplicitConversionSequence::Indistinguishable; |
| 3261 | } |
| 3262 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3263 | /// CompareImplicitConversionSequences - Compare two implicit |
| 3264 | /// conversion sequences to determine whether one is better than the |
| 3265 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3266 | static ImplicitConversionSequence::CompareKind |
| 3267 | CompareImplicitConversionSequences(Sema &S, |
| 3268 | const ImplicitConversionSequence& ICS1, |
| 3269 | const ImplicitConversionSequence& ICS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3270 | { |
| 3271 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 3272 | // conversion sequences (as defined in 13.3.3.1) |
| 3273 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 3274 | // conversion sequence than a user-defined conversion sequence or |
| 3275 | // an ellipsis conversion sequence, and |
| 3276 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 3277 | // conversion sequence than an ellipsis conversion sequence |
| 3278 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3279 | // |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3280 | // C++0x [over.best.ics]p10: |
| 3281 | // For the purpose of ranking implicit conversion sequences as |
| 3282 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 3283 | // treated as a user-defined sequence that is indistinguishable |
| 3284 | // from any other user-defined conversion sequence. |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3285 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 3286 | return ImplicitConversionSequence::Better; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3287 | if (ICS2.getKindRank() < ICS1.getKindRank()) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3288 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3289 | |
Benjamin Kramer | b6eee07 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 3290 | // The following checks require both conversion sequences to be of |
| 3291 | // the same kind. |
| 3292 | if (ICS1.getKind() != ICS2.getKind()) |
| 3293 | return ImplicitConversionSequence::Indistinguishable; |
| 3294 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3295 | ImplicitConversionSequence::CompareKind Result = |
| 3296 | ImplicitConversionSequence::Indistinguishable; |
| 3297 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3298 | // Two implicit conversion sequences of the same form are |
| 3299 | // indistinguishable conversion sequences unless one of the |
| 3300 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3301 | if (ICS1.isStandard()) |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3302 | Result = CompareStandardConversionSequences(S, |
| 3303 | ICS1.Standard, ICS2.Standard); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3304 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3305 | // User-defined conversion sequence U1 is a better conversion |
| 3306 | // sequence than another user-defined conversion sequence U2 if |
| 3307 | // they contain the same user-defined conversion function or |
| 3308 | // constructor and if the second standard conversion sequence of |
| 3309 | // U1 is better than the second standard conversion sequence of |
| 3310 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3311 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3312 | ICS2.UserDefined.ConversionFunction) |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3313 | Result = CompareStandardConversionSequences(S, |
| 3314 | ICS1.UserDefined.After, |
| 3315 | ICS2.UserDefined.After); |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3316 | else |
| 3317 | Result = compareConversionFunctions(S, |
| 3318 | ICS1.UserDefined.ConversionFunction, |
| 3319 | ICS2.UserDefined.ConversionFunction); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3322 | // List-initialization sequence L1 is a better conversion sequence than |
| 3323 | // list-initialization sequence L2 if L1 converts to std::initializer_list<X> |
| 3324 | // for some X and L2 does not. |
| 3325 | if (Result == ImplicitConversionSequence::Indistinguishable && |
Richard Smith | 798186a | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 3326 | !ICS1.isBad()) { |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 3327 | if (ICS1.isStdInitializerListElement() && |
| 3328 | !ICS2.isStdInitializerListElement()) |
| 3329 | return ImplicitConversionSequence::Better; |
| 3330 | if (!ICS1.isStdInitializerListElement() && |
| 3331 | ICS2.isStdInitializerListElement()) |
| 3332 | return ImplicitConversionSequence::Worse; |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3333 | } |
| 3334 | |
| 3335 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3336 | } |
| 3337 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3338 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 3339 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 3340 | Qualifiers Quals; |
| 3341 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3342 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3343 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3344 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3345 | return Context.hasSameUnqualifiedType(T1, T2); |
| 3346 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3347 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3348 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 3349 | // determine if one is a proper subset of the other. |
| 3350 | static ImplicitConversionSequence::CompareKind |
| 3351 | compareStandardConversionSubsets(ASTContext &Context, |
| 3352 | const StandardConversionSequence& SCS1, |
| 3353 | const StandardConversionSequence& SCS2) { |
| 3354 | ImplicitConversionSequence::CompareKind Result |
| 3355 | = ImplicitConversionSequence::Indistinguishable; |
| 3356 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3357 | // the identity conversion sequence is considered to be a subsequence of |
Douglas Gregor | ae65f4b | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 3358 | // any non-identity conversion sequence |
Douglas Gregor | 4ae5b72 | 2011-06-05 06:15:20 +0000 | [diff] [blame] | 3359 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 3360 | return ImplicitConversionSequence::Better; |
| 3361 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 3362 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3363 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3364 | if (SCS1.Second != SCS2.Second) { |
| 3365 | if (SCS1.Second == ICK_Identity) |
| 3366 | Result = ImplicitConversionSequence::Better; |
| 3367 | else if (SCS2.Second == ICK_Identity) |
| 3368 | Result = ImplicitConversionSequence::Worse; |
| 3369 | else |
| 3370 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3371 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3372 | return ImplicitConversionSequence::Indistinguishable; |
| 3373 | |
| 3374 | if (SCS1.Third == SCS2.Third) { |
| 3375 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 3376 | : ImplicitConversionSequence::Indistinguishable; |
| 3377 | } |
| 3378 | |
| 3379 | if (SCS1.Third == ICK_Identity) |
| 3380 | return Result == ImplicitConversionSequence::Worse |
| 3381 | ? ImplicitConversionSequence::Indistinguishable |
| 3382 | : ImplicitConversionSequence::Better; |
| 3383 | |
| 3384 | if (SCS2.Third == ICK_Identity) |
| 3385 | return Result == ImplicitConversionSequence::Better |
| 3386 | ? ImplicitConversionSequence::Indistinguishable |
| 3387 | : ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3388 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3389 | return ImplicitConversionSequence::Indistinguishable; |
| 3390 | } |
| 3391 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3392 | /// \brief Determine whether one of the given reference bindings is better |
| 3393 | /// than the other based on what kind of bindings they are. |
| 3394 | static bool isBetterReferenceBindingKind(const StandardConversionSequence &SCS1, |
| 3395 | const StandardConversionSequence &SCS2) { |
| 3396 | // C++0x [over.ics.rank]p3b4: |
| 3397 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 3398 | // implicit object parameter of a non-static member function declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3399 | // without a ref-qualifier, and *either* S1 binds an rvalue reference |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3400 | // 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] | 3401 | // lvalue reference to a function lvalue and S2 binds an rvalue |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3402 | // reference*. |
| 3403 | // |
| 3404 | // FIXME: Rvalue references. We're going rogue with the above edits, |
| 3405 | // because the semantics in the current C++0x working paper (N3225 at the |
| 3406 | // time of this writing) break the standard definition of std::forward |
| 3407 | // and std::reference_wrapper when dealing with references to functions. |
| 3408 | // Proposed wording changes submitted to CWG for consideration. |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3409 | if (SCS1.BindsImplicitObjectArgumentWithoutRefQualifier || |
| 3410 | SCS2.BindsImplicitObjectArgumentWithoutRefQualifier) |
| 3411 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3412 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3413 | return (!SCS1.IsLvalueReference && SCS1.BindsToRvalue && |
| 3414 | SCS2.IsLvalueReference) || |
| 3415 | (SCS1.IsLvalueReference && SCS1.BindsToFunctionLvalue && |
| 3416 | !SCS2.IsLvalueReference); |
| 3417 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3418 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3419 | /// CompareStandardConversionSequences - Compare two standard |
| 3420 | /// conversion sequences to determine whether one is better than the |
| 3421 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3422 | static ImplicitConversionSequence::CompareKind |
| 3423 | CompareStandardConversionSequences(Sema &S, |
| 3424 | const StandardConversionSequence& SCS1, |
| 3425 | const StandardConversionSequence& SCS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3426 | { |
| 3427 | // Standard conversion sequence S1 is a better conversion sequence |
| 3428 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 3429 | |
| 3430 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 3431 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 3432 | // excluding any Lvalue Transformation; the identity conversion |
| 3433 | // sequence is considered to be a subsequence of any |
| 3434 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3435 | if (ImplicitConversionSequence::CompareKind CK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3436 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3437 | return CK; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3438 | |
| 3439 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 3440 | // defined below), or, if not that, |
| 3441 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 3442 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 3443 | if (Rank1 < Rank2) |
| 3444 | return ImplicitConversionSequence::Better; |
| 3445 | else if (Rank2 < Rank1) |
| 3446 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3447 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3448 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 3449 | // are indistinguishable unless one of the following rules |
| 3450 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3451 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3452 | // A conversion that is not a conversion of a pointer, or |
| 3453 | // pointer to member, to bool is better than another conversion |
| 3454 | // that is such a conversion. |
| 3455 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 3456 | return SCS2.isPointerConversionToBool() |
| 3457 | ? ImplicitConversionSequence::Better |
| 3458 | : ImplicitConversionSequence::Worse; |
| 3459 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3460 | // C++ [over.ics.rank]p4b2: |
| 3461 | // |
| 3462 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3463 | // conversion of B* to A* is better than conversion of B* to |
| 3464 | // void*, and conversion of A* to void* is better than conversion |
| 3465 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3466 | bool SCS1ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3467 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3468 | bool SCS2ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3469 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3470 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 3471 | // Exactly one of the conversion sequences is a conversion to |
| 3472 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3473 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 3474 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3475 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 3476 | // Neither conversion sequence converts to a void pointer; compare |
| 3477 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3478 | if (ImplicitConversionSequence::CompareKind DerivedCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3479 | = CompareDerivedToBaseConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3480 | return DerivedCK; |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3481 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid && |
| 3482 | !S.Context.hasSameType(SCS1.getFromType(), SCS2.getFromType())) { |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3483 | // Both conversion sequences are conversions to void |
| 3484 | // pointers. Compare the source types to determine if there's an |
| 3485 | // inheritance relationship in their sources. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3486 | QualType FromType1 = SCS1.getFromType(); |
| 3487 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3488 | |
| 3489 | // Adjust the types we're converting from via the array-to-pointer |
| 3490 | // conversion, if we need to. |
| 3491 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3492 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3493 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3494 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3495 | |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3496 | QualType FromPointee1 = FromType1->getPointeeType().getUnqualifiedType(); |
| 3497 | QualType FromPointee2 = FromType2->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3498 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3499 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3500 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3501 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3502 | return ImplicitConversionSequence::Worse; |
| 3503 | |
| 3504 | // Objective-C++: If one interface is more specific than the |
| 3505 | // other, it is the better one. |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3506 | const ObjCObjectPointerType* FromObjCPtr1 |
| 3507 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3508 | const ObjCObjectPointerType* FromObjCPtr2 |
| 3509 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3510 | if (FromObjCPtr1 && FromObjCPtr2) { |
| 3511 | bool AssignLeft = S.Context.canAssignObjCInterfaces(FromObjCPtr1, |
| 3512 | FromObjCPtr2); |
| 3513 | bool AssignRight = S.Context.canAssignObjCInterfaces(FromObjCPtr2, |
| 3514 | FromObjCPtr1); |
| 3515 | if (AssignLeft != AssignRight) { |
| 3516 | return AssignLeft? ImplicitConversionSequence::Better |
| 3517 | : ImplicitConversionSequence::Worse; |
| 3518 | } |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3519 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3520 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3521 | |
| 3522 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 3523 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3524 | if (ImplicitConversionSequence::CompareKind QualCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3525 | = CompareQualificationConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3526 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3527 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3528 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3529 | // Check for a better reference binding based on the kind of bindings. |
| 3530 | if (isBetterReferenceBindingKind(SCS1, SCS2)) |
| 3531 | return ImplicitConversionSequence::Better; |
| 3532 | else if (isBetterReferenceBindingKind(SCS2, SCS1)) |
| 3533 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3534 | |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 3535 | // C++ [over.ics.rank]p3b4: |
| 3536 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 3537 | // which the references refer are the same type except for |
| 3538 | // top-level cv-qualifiers, and the type to which the reference |
| 3539 | // initialized by S2 refers is more cv-qualified than the type |
| 3540 | // to which the reference initialized by S1 refers. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3541 | QualType T1 = SCS1.getToType(2); |
| 3542 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3543 | T1 = S.Context.getCanonicalType(T1); |
| 3544 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3545 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3546 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3547 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3548 | if (UnqualT1 == UnqualT2) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3549 | // Objective-C++ ARC: If the references refer to objects with different |
| 3550 | // lifetimes, prefer bindings that don't change lifetime. |
| 3551 | if (SCS1.ObjCLifetimeConversionBinding != |
| 3552 | SCS2.ObjCLifetimeConversionBinding) { |
| 3553 | return SCS1.ObjCLifetimeConversionBinding |
| 3554 | ? ImplicitConversionSequence::Worse |
| 3555 | : ImplicitConversionSequence::Better; |
| 3556 | } |
| 3557 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3558 | // If the type is an array type, promote the element qualifiers to the |
| 3559 | // type for comparison. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3560 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3561 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3562 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3563 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3564 | if (T2.isMoreQualifiedThan(T1)) |
| 3565 | return ImplicitConversionSequence::Better; |
| 3566 | else if (T1.isMoreQualifiedThan(T2)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3567 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3568 | } |
| 3569 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3570 | |
Francois Pichet | 1c98d62 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3571 | // In Microsoft mode, prefer an integral conversion to a |
| 3572 | // floating-to-integral conversion if the integral conversion |
| 3573 | // is between types of the same size. |
| 3574 | // For example: |
| 3575 | // void f(float); |
| 3576 | // void f(int); |
| 3577 | // int main { |
| 3578 | // long a; |
| 3579 | // f(a); |
| 3580 | // } |
| 3581 | // Here, MSVC will call f(int) instead of generating a compile error |
| 3582 | // as clang will do in standard mode. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3583 | if (S.getLangOpts().MicrosoftMode && |
Francois Pichet | 1c98d62 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3584 | SCS1.Second == ICK_Integral_Conversion && |
| 3585 | SCS2.Second == ICK_Floating_Integral && |
| 3586 | S.Context.getTypeSize(SCS1.getFromType()) == |
| 3587 | S.Context.getTypeSize(SCS1.getToType(2))) |
| 3588 | return ImplicitConversionSequence::Better; |
| 3589 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3590 | return ImplicitConversionSequence::Indistinguishable; |
| 3591 | } |
| 3592 | |
| 3593 | /// CompareQualificationConversions - Compares two standard conversion |
| 3594 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3595 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 3596 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3597 | CompareQualificationConversions(Sema &S, |
| 3598 | const StandardConversionSequence& SCS1, |
| 3599 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 3600 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3601 | // -- S1 and S2 differ only in their qualification conversion and |
| 3602 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 3603 | // cv-qualification signature of type T1 is a proper subset of |
| 3604 | // the cv-qualification signature of type T2, and S1 is not the |
| 3605 | // deprecated string literal array-to-pointer conversion (4.2). |
| 3606 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 3607 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 3608 | return ImplicitConversionSequence::Indistinguishable; |
| 3609 | |
| 3610 | // FIXME: the example in the standard doesn't use a qualification |
| 3611 | // conversion (!) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3612 | QualType T1 = SCS1.getToType(2); |
| 3613 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3614 | T1 = S.Context.getCanonicalType(T1); |
| 3615 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3616 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3617 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3618 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3619 | |
| 3620 | // If the types are the same, we won't learn anything by unwrapped |
| 3621 | // them. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3622 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3623 | return ImplicitConversionSequence::Indistinguishable; |
| 3624 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3625 | // If the type is an array type, promote the element qualifiers to the type |
| 3626 | // for comparison. |
| 3627 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3628 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3629 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3630 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3631 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3632 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3633 | = ImplicitConversionSequence::Indistinguishable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3634 | |
| 3635 | // Objective-C++ ARC: |
| 3636 | // Prefer qualification conversions not involving a change in lifetime |
| 3637 | // to qualification conversions that do not change lifetime. |
| 3638 | if (SCS1.QualificationIncludesObjCLifetime != |
| 3639 | SCS2.QualificationIncludesObjCLifetime) { |
| 3640 | Result = SCS1.QualificationIncludesObjCLifetime |
| 3641 | ? ImplicitConversionSequence::Worse |
| 3642 | : ImplicitConversionSequence::Better; |
| 3643 | } |
| 3644 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3645 | while (S.Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3646 | // Within each iteration of the loop, we check the qualifiers to |
| 3647 | // determine if this still looks like a qualification |
| 3648 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 3649 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3650 | // until there are no more pointers or pointers-to-members left |
| 3651 | // to unwrap. This essentially mimics what |
| 3652 | // IsQualificationConversion does, but here we're checking for a |
| 3653 | // strict subset of qualifiers. |
| 3654 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 3655 | // The qualifiers are the same, so this doesn't tell us anything |
| 3656 | // about how the sequences rank. |
| 3657 | ; |
| 3658 | else if (T2.isMoreQualifiedThan(T1)) { |
| 3659 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 3660 | if (Result == ImplicitConversionSequence::Worse) |
| 3661 | // Neither has qualifiers that are a subset of the other's |
| 3662 | // qualifiers. |
| 3663 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3664 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3665 | Result = ImplicitConversionSequence::Better; |
| 3666 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 3667 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 3668 | if (Result == ImplicitConversionSequence::Better) |
| 3669 | // Neither has qualifiers that are a subset of the other's |
| 3670 | // qualifiers. |
| 3671 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3672 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3673 | Result = ImplicitConversionSequence::Worse; |
| 3674 | } else { |
| 3675 | // Qualifiers are disjoint. |
| 3676 | return ImplicitConversionSequence::Indistinguishable; |
| 3677 | } |
| 3678 | |
| 3679 | // If the types after this point are equivalent, we're done. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3680 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3681 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3682 | } |
| 3683 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3684 | // Check that the winning standard conversion sequence isn't using |
| 3685 | // the deprecated string literal array to pointer conversion. |
| 3686 | switch (Result) { |
| 3687 | case ImplicitConversionSequence::Better: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3688 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3689 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3690 | break; |
| 3691 | |
| 3692 | case ImplicitConversionSequence::Indistinguishable: |
| 3693 | break; |
| 3694 | |
| 3695 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3696 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3697 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3698 | break; |
| 3699 | } |
| 3700 | |
| 3701 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3702 | } |
| 3703 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3704 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 3705 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3706 | /// various kinds of derived-to-base conversions (C++ |
| 3707 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 3708 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3709 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3710 | CompareDerivedToBaseConversions(Sema &S, |
| 3711 | const StandardConversionSequence& SCS1, |
| 3712 | const StandardConversionSequence& SCS2) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3713 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3714 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3715 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3716 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3717 | |
| 3718 | // Adjust the types we're converting from via the array-to-pointer |
| 3719 | // conversion, if we need to. |
| 3720 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3721 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3722 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3723 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3724 | |
| 3725 | // Canonicalize all of the types. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3726 | FromType1 = S.Context.getCanonicalType(FromType1); |
| 3727 | ToType1 = S.Context.getCanonicalType(ToType1); |
| 3728 | FromType2 = S.Context.getCanonicalType(FromType2); |
| 3729 | ToType2 = S.Context.getCanonicalType(ToType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3730 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3731 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3732 | // |
| 3733 | // If class B is derived directly or indirectly from class A and |
| 3734 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3735 | // |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3736 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3737 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 3738 | SCS2.Second == ICK_Pointer_Conversion && |
| 3739 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 3740 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 3741 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3742 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3743 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3744 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3745 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3746 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3747 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3748 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3749 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3750 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3751 | // -- 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] | 3752 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3753 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3754 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3755 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3756 | return ImplicitConversionSequence::Worse; |
| 3757 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3758 | |
| 3759 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 3760 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3761 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3762 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3763 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3764 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3765 | } |
| 3766 | } else if (SCS1.Second == ICK_Pointer_Conversion && |
| 3767 | SCS2.Second == ICK_Pointer_Conversion) { |
| 3768 | const ObjCObjectPointerType *FromPtr1 |
| 3769 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3770 | const ObjCObjectPointerType *FromPtr2 |
| 3771 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3772 | const ObjCObjectPointerType *ToPtr1 |
| 3773 | = ToType1->getAs<ObjCObjectPointerType>(); |
| 3774 | const ObjCObjectPointerType *ToPtr2 |
| 3775 | = ToType2->getAs<ObjCObjectPointerType>(); |
| 3776 | |
| 3777 | if (FromPtr1 && FromPtr2 && ToPtr1 && ToPtr2) { |
| 3778 | // Apply the same conversion ranking rules for Objective-C pointer types |
| 3779 | // that we do for C++ pointers to class types. However, we employ the |
| 3780 | // Objective-C pseudo-subtyping relationship used for assignment of |
| 3781 | // Objective-C pointer types. |
| 3782 | bool FromAssignLeft |
| 3783 | = S.Context.canAssignObjCInterfaces(FromPtr1, FromPtr2); |
| 3784 | bool FromAssignRight |
| 3785 | = S.Context.canAssignObjCInterfaces(FromPtr2, FromPtr1); |
| 3786 | bool ToAssignLeft |
| 3787 | = S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2); |
| 3788 | bool ToAssignRight |
| 3789 | = S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1); |
| 3790 | |
| 3791 | // A conversion to an a non-id object pointer type or qualified 'id' |
| 3792 | // type is better than a conversion to 'id'. |
| 3793 | if (ToPtr1->isObjCIdType() && |
| 3794 | (ToPtr2->isObjCQualifiedIdType() || ToPtr2->getInterfaceDecl())) |
| 3795 | return ImplicitConversionSequence::Worse; |
| 3796 | if (ToPtr2->isObjCIdType() && |
| 3797 | (ToPtr1->isObjCQualifiedIdType() || ToPtr1->getInterfaceDecl())) |
| 3798 | return ImplicitConversionSequence::Better; |
| 3799 | |
| 3800 | // A conversion to a non-id object pointer type is better than a |
| 3801 | // conversion to a qualified 'id' type |
| 3802 | if (ToPtr1->isObjCQualifiedIdType() && ToPtr2->getInterfaceDecl()) |
| 3803 | return ImplicitConversionSequence::Worse; |
| 3804 | if (ToPtr2->isObjCQualifiedIdType() && ToPtr1->getInterfaceDecl()) |
| 3805 | return ImplicitConversionSequence::Better; |
| 3806 | |
| 3807 | // A conversion to an a non-Class object pointer type or qualified 'Class' |
| 3808 | // type is better than a conversion to 'Class'. |
| 3809 | if (ToPtr1->isObjCClassType() && |
| 3810 | (ToPtr2->isObjCQualifiedClassType() || ToPtr2->getInterfaceDecl())) |
| 3811 | return ImplicitConversionSequence::Worse; |
| 3812 | if (ToPtr2->isObjCClassType() && |
| 3813 | (ToPtr1->isObjCQualifiedClassType() || ToPtr1->getInterfaceDecl())) |
| 3814 | return ImplicitConversionSequence::Better; |
| 3815 | |
| 3816 | // A conversion to a non-Class object pointer type is better than a |
| 3817 | // conversion to a qualified 'Class' type. |
| 3818 | if (ToPtr1->isObjCQualifiedClassType() && ToPtr2->getInterfaceDecl()) |
| 3819 | return ImplicitConversionSequence::Worse; |
| 3820 | if (ToPtr2->isObjCQualifiedClassType() && ToPtr1->getInterfaceDecl()) |
| 3821 | return ImplicitConversionSequence::Better; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3822 | |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3823 | // -- "conversion of C* to B* is better than conversion of C* to A*," |
| 3824 | if (S.Context.hasSameType(FromType1, FromType2) && |
| 3825 | !FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType() && |
| 3826 | (ToAssignLeft != ToAssignRight)) |
| 3827 | return ToAssignLeft? ImplicitConversionSequence::Worse |
| 3828 | : ImplicitConversionSequence::Better; |
| 3829 | |
| 3830 | // -- "conversion of B* to A* is better than conversion of C* to A*," |
| 3831 | if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) && |
| 3832 | (FromAssignLeft != FromAssignRight)) |
| 3833 | return FromAssignLeft? ImplicitConversionSequence::Better |
| 3834 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3835 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3836 | } |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3837 | |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3838 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3839 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 3840 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 3841 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3842 | const MemberPointerType * FromMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3843 | FromType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3844 | const MemberPointerType * ToMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3845 | ToType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3846 | const MemberPointerType * FromMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3847 | FromType2->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3848 | const MemberPointerType * ToMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3849 | ToType2->getAs<MemberPointerType>(); |
| 3850 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 3851 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 3852 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 3853 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 3854 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 3855 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 3856 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 3857 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3858 | // 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] | 3859 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3860 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3861 | return ImplicitConversionSequence::Worse; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3862 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3863 | return ImplicitConversionSequence::Better; |
| 3864 | } |
| 3865 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 3866 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3867 | if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3868 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3869 | else if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3870 | return ImplicitConversionSequence::Worse; |
| 3871 | } |
| 3872 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3873 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3874 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3875 | // -- 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] | 3876 | // -- binding of an expression of type C to a reference of type |
| 3877 | // B& is better than binding an expression of type C to a |
| 3878 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3879 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3880 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3881 | if (S.IsDerivedFrom(ToType1, ToType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3882 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3883 | else if (S.IsDerivedFrom(ToType2, ToType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3884 | return ImplicitConversionSequence::Worse; |
| 3885 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3886 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3887 | // -- 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] | 3888 | // -- binding of an expression of type B to a reference of type |
| 3889 | // A& is better than binding an expression of type C to a |
| 3890 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3891 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3892 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3893 | if (S.IsDerivedFrom(FromType2, FromType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3894 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3895 | else if (S.IsDerivedFrom(FromType1, FromType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3896 | return ImplicitConversionSequence::Worse; |
| 3897 | } |
| 3898 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3899 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3900 | return ImplicitConversionSequence::Indistinguishable; |
| 3901 | } |
| 3902 | |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 3903 | /// \brief Determine whether the given type is valid, e.g., it is not an invalid |
| 3904 | /// C++ class. |
| 3905 | static bool isTypeValid(QualType T) { |
| 3906 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) |
| 3907 | return !Record->isInvalidDecl(); |
| 3908 | |
| 3909 | return true; |
| 3910 | } |
| 3911 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3912 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 3913 | /// determine whether they are reference-related, |
| 3914 | /// reference-compatible, reference-compatible with added |
| 3915 | /// qualification, or incompatible, for use in C++ initialization by |
| 3916 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 3917 | /// type, and the first type (T1) is the pointee type of the reference |
| 3918 | /// type being initialized. |
| 3919 | Sema::ReferenceCompareResult |
| 3920 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 3921 | QualType OrigT1, QualType OrigT2, |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3922 | bool &DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3923 | bool &ObjCConversion, |
| 3924 | bool &ObjCLifetimeConversion) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3925 | assert(!OrigT1->isReferenceType() && |
| 3926 | "T1 must be the pointee type of the reference type"); |
| 3927 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 3928 | |
| 3929 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 3930 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 3931 | Qualifiers T1Quals, T2Quals; |
| 3932 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3933 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 3934 | |
| 3935 | // C++ [dcl.init.ref]p4: |
| 3936 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 3937 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 3938 | // T1 is a base class of T2. |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3939 | DerivedToBase = false; |
| 3940 | ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3941 | ObjCLifetimeConversion = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3942 | if (UnqualT1 == UnqualT2) { |
| 3943 | // Nothing to do. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3944 | } else if (!RequireCompleteType(Loc, OrigT2, 0) && |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 3945 | isTypeValid(UnqualT1) && isTypeValid(UnqualT2) && |
| 3946 | IsDerivedFrom(UnqualT2, UnqualT1)) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3947 | DerivedToBase = true; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3948 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
| 3949 | UnqualT2->isObjCObjectOrInterfaceType() && |
| 3950 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
| 3951 | ObjCConversion = true; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3952 | else |
| 3953 | return Ref_Incompatible; |
| 3954 | |
| 3955 | // At this point, we know that T1 and T2 are reference-related (at |
| 3956 | // least). |
| 3957 | |
| 3958 | // If the type is an array type, promote the element qualifiers to the type |
| 3959 | // for comparison. |
| 3960 | if (isa<ArrayType>(T1) && T1Quals) |
| 3961 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 3962 | if (isa<ArrayType>(T2) && T2Quals) |
| 3963 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 3964 | |
| 3965 | // C++ [dcl.init.ref]p4: |
| 3966 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 3967 | // reference-related to T2 and cv1 is the same cv-qualification |
| 3968 | // as, or greater cv-qualification than, cv2. For purposes of |
| 3969 | // overload resolution, cases for which cv1 is greater |
| 3970 | // cv-qualification than cv2 are identified as |
| 3971 | // reference-compatible with added qualification (see 13.3.3.2). |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 3972 | // |
| 3973 | // Note that we also require equivalence of Objective-C GC and address-space |
| 3974 | // qualifiers when performing these computations, so that e.g., an int in |
| 3975 | // address space 1 is not reference-compatible with an int in address |
| 3976 | // space 2. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3977 | if (T1Quals.getObjCLifetime() != T2Quals.getObjCLifetime() && |
| 3978 | T1Quals.compatiblyIncludesObjCLifetime(T2Quals)) { |
| 3979 | T1Quals.removeObjCLifetime(); |
| 3980 | T2Quals.removeObjCLifetime(); |
| 3981 | ObjCLifetimeConversion = true; |
| 3982 | } |
| 3983 | |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 3984 | if (T1Quals == T2Quals) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3985 | return Ref_Compatible; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3986 | else if (T1Quals.compatiblyIncludes(T2Quals)) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3987 | return Ref_Compatible_With_Added_Qualification; |
| 3988 | else |
| 3989 | return Ref_Related; |
| 3990 | } |
| 3991 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3992 | /// \brief Look for a user-defined conversion to an value reference-compatible |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3993 | /// with DeclType. Return true if something definite is found. |
| 3994 | static bool |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3995 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
| 3996 | QualType DeclType, SourceLocation DeclLoc, |
| 3997 | Expr *Init, QualType T2, bool AllowRvalues, |
| 3998 | bool AllowExplicit) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3999 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 4000 | CXXRecordDecl *T2RecordDecl |
| 4001 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 4002 | |
| 4003 | OverloadCandidateSet CandidateSet(DeclLoc); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 4004 | std::pair<CXXRecordDecl::conversion_iterator, |
| 4005 | CXXRecordDecl::conversion_iterator> |
| 4006 | Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 4007 | for (CXXRecordDecl::conversion_iterator |
| 4008 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4009 | NamedDecl *D = *I; |
| 4010 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4011 | if (isa<UsingShadowDecl>(D)) |
| 4012 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 4013 | |
| 4014 | FunctionTemplateDecl *ConvTemplate |
| 4015 | = dyn_cast<FunctionTemplateDecl>(D); |
| 4016 | CXXConversionDecl *Conv; |
| 4017 | if (ConvTemplate) |
| 4018 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 4019 | else |
| 4020 | Conv = cast<CXXConversionDecl>(D); |
| 4021 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4022 | // 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] | 4023 | // explicit conversions, skip it. |
| 4024 | if (!AllowExplicit && Conv->isExplicit()) |
| 4025 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4026 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4027 | if (AllowRvalues) { |
| 4028 | bool DerivedToBase = false; |
| 4029 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4030 | bool ObjCLifetimeConversion = false; |
Douglas Gregor | 203050c | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4031 | |
| 4032 | // If we are initializing an rvalue reference, don't permit conversion |
| 4033 | // functions that return lvalues. |
| 4034 | if (!ConvTemplate && DeclType->isRValueReferenceType()) { |
| 4035 | const ReferenceType *RefType |
| 4036 | = Conv->getConversionType()->getAs<LValueReferenceType>(); |
| 4037 | if (RefType && !RefType->getPointeeType()->isFunctionType()) |
| 4038 | continue; |
| 4039 | } |
| 4040 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4041 | if (!ConvTemplate && |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4042 | S.CompareReferenceRelationship( |
| 4043 | DeclLoc, |
| 4044 | Conv->getConversionType().getNonReferenceType() |
| 4045 | .getUnqualifiedType(), |
| 4046 | DeclType.getNonReferenceType().getUnqualifiedType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4047 | DerivedToBase, ObjCConversion, ObjCLifetimeConversion) == |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4048 | Sema::Ref_Incompatible) |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4049 | continue; |
| 4050 | } else { |
| 4051 | // If the conversion function doesn't return a reference type, |
| 4052 | // it can't be considered for this conversion. An rvalue reference |
| 4053 | // is only acceptable if its referencee is a function type. |
| 4054 | |
| 4055 | const ReferenceType *RefType = |
| 4056 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 4057 | if (!RefType || |
| 4058 | (!RefType->isLValueReferenceType() && |
| 4059 | !RefType->getPointeeType()->isFunctionType())) |
| 4060 | continue; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4061 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4062 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4063 | if (ConvTemplate) |
| 4064 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4065 | Init, DeclType, CandidateSet); |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4066 | else |
| 4067 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4068 | DeclType, CandidateSet); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4069 | } |
| 4070 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4071 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4072 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4073 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4074 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4075 | case OR_Success: |
| 4076 | // C++ [over.ics.ref]p1: |
| 4077 | // |
| 4078 | // [...] If the parameter binds directly to the result of |
| 4079 | // applying a conversion function to the argument |
| 4080 | // expression, the implicit conversion sequence is a |
| 4081 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 4082 | // second standard conversion sequence either an identity |
| 4083 | // conversion or, if the conversion function returns an |
| 4084 | // entity of a type that is a derived class of the parameter |
| 4085 | // type, a derived-to-base Conversion. |
| 4086 | if (!Best->FinalConversion.DirectBinding) |
| 4087 | return false; |
| 4088 | |
| 4089 | ICS.setUserDefined(); |
| 4090 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 4091 | ICS.UserDefined.After = Best->FinalConversion; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4092 | ICS.UserDefined.HadMultipleCandidates = HadMultipleCandidates; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4093 | ICS.UserDefined.ConversionFunction = Best->Function; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 4094 | ICS.UserDefined.FoundConversionFunction = Best->FoundDecl; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4095 | ICS.UserDefined.EllipsisConversion = false; |
| 4096 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 4097 | ICS.UserDefined.After.DirectBinding && |
| 4098 | "Expected a direct reference binding!"); |
| 4099 | return true; |
| 4100 | |
| 4101 | case OR_Ambiguous: |
| 4102 | ICS.setAmbiguous(); |
| 4103 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4104 | Cand != CandidateSet.end(); ++Cand) |
| 4105 | if (Cand->Viable) |
| 4106 | ICS.Ambiguous.addConversion(Cand->Function); |
| 4107 | return true; |
| 4108 | |
| 4109 | case OR_No_Viable_Function: |
| 4110 | case OR_Deleted: |
| 4111 | // There was no suitable conversion, or we found a deleted |
| 4112 | // conversion; continue with other checks. |
| 4113 | return false; |
| 4114 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4115 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4116 | llvm_unreachable("Invalid OverloadResult!"); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4117 | } |
| 4118 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4119 | /// \brief Compute an implicit conversion sequence for reference |
| 4120 | /// initialization. |
| 4121 | static ImplicitConversionSequence |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4122 | TryReferenceInit(Sema &S, Expr *Init, QualType DeclType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4123 | SourceLocation DeclLoc, |
| 4124 | bool SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 4125 | bool AllowExplicit) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4126 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 4127 | |
| 4128 | // Most paths end in a failed conversion. |
| 4129 | ImplicitConversionSequence ICS; |
| 4130 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4131 | |
| 4132 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 4133 | QualType T2 = Init->getType(); |
| 4134 | |
| 4135 | // If the initializer is the address of an overloaded function, try |
| 4136 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4137 | // type of the resulting function. |
| 4138 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4139 | DeclAccessPair Found; |
| 4140 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 4141 | false, Found)) |
| 4142 | T2 = Fn->getType(); |
| 4143 | } |
| 4144 | |
| 4145 | // Compute some basic properties of the types and the initializer. |
| 4146 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 4147 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4148 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4149 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4150 | Expr::Classification InitCategory = Init->Classify(S.Context); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4151 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4152 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4153 | ObjCConversion, ObjCLifetimeConversion); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4154 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4155 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4156 | // C++0x [dcl.init.ref]p5: |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 4157 | // A reference to type "cv1 T1" is initialized by an expression |
| 4158 | // of type "cv2 T2" as follows: |
| 4159 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4160 | // -- If reference is an lvalue reference and the initializer expression |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4161 | if (!isRValRef) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4162 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 4163 | // reference-compatible with "cv2 T2," or |
| 4164 | // |
| 4165 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 4166 | if (InitCategory.isLValue() && |
| 4167 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4168 | // C++ [over.ics.ref]p1: |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4169 | // When a parameter of reference type binds directly (8.5.3) |
| 4170 | // to an argument expression, the implicit conversion sequence |
| 4171 | // is the identity conversion, unless the argument expression |
| 4172 | // has a type that is a derived class of the parameter type, |
| 4173 | // in which case the implicit conversion sequence is a |
| 4174 | // derived-to-base Conversion (13.3.3.1). |
| 4175 | ICS.setStandard(); |
| 4176 | ICS.Standard.First = ICK_Identity; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4177 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 4178 | : ObjCConversion? ICK_Compatible_Conversion |
| 4179 | : ICK_Identity; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4180 | ICS.Standard.Third = ICK_Identity; |
| 4181 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4182 | ICS.Standard.setToType(0, T2); |
| 4183 | ICS.Standard.setToType(1, T1); |
| 4184 | ICS.Standard.setToType(2, T1); |
| 4185 | ICS.Standard.ReferenceBinding = true; |
| 4186 | ICS.Standard.DirectBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4187 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4188 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4189 | ICS.Standard.BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4190 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4191 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4192 | ICS.Standard.CopyConstructor = 0; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4193 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4194 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 4195 | // derived-to-base conversions is suppressed when we're |
| 4196 | // computing the implicit conversion sequence (C++ |
| 4197 | // [over.best.ics]p2). |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4198 | return ICS; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4199 | } |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4200 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4201 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 4202 | // not reference-related to T2, and can be implicitly |
| 4203 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 4204 | // is reference-compatible with "cv3 T3" 92) (this |
| 4205 | // conversion is selected by enumerating the applicable |
| 4206 | // conversion functions (13.3.1.6) and choosing the best |
| 4207 | // one through overload resolution (13.3)), |
| 4208 | if (!SuppressUserConversions && T2->isRecordType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4209 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4210 | RefRelationship == Sema::Ref_Incompatible) { |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4211 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4212 | Init, T2, /*AllowRvalues=*/false, |
| 4213 | AllowExplicit)) |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4214 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4215 | } |
| 4216 | } |
| 4217 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4218 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 4219 | // 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] | 4220 | // shall be an rvalue reference. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4221 | // |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 4222 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 4223 | // point, which is that, due to p2 (which short-circuits reference |
| 4224 | // binding by only attempting a simple conversion for non-direct |
| 4225 | // bindings) and p3's strange wording, we allow a const volatile |
| 4226 | // reference to bind to an rvalue. Hence the check for the presence |
| 4227 | // of "const" rather than checking for "const" being the only |
| 4228 | // qualifier. |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4229 | // This is also the point where rvalue references and lvalue inits no longer |
| 4230 | // go together. |
Richard Smith | 8ab10aa | 2012-05-24 04:29:20 +0000 | [diff] [blame] | 4231 | if (!isRValRef && (!T1.isConstQualified() || T1.isVolatileQualified())) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4232 | return ICS; |
| 4233 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4234 | // -- If the initializer expression |
| 4235 | // |
| 4236 | // -- is an xvalue, class prvalue, array prvalue or function |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4237 | // lvalue and "cv1 T1" is reference-compatible with "cv2 T2", or |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4238 | if (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification && |
| 4239 | (InitCategory.isXValue() || |
| 4240 | (InitCategory.isPRValue() && (T2->isRecordType() || T2->isArrayType())) || |
| 4241 | (InitCategory.isLValue() && T2->isFunctionType()))) { |
| 4242 | ICS.setStandard(); |
| 4243 | ICS.Standard.First = ICK_Identity; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4244 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4245 | : ObjCConversion? ICK_Compatible_Conversion |
| 4246 | : ICK_Identity; |
| 4247 | ICS.Standard.Third = ICK_Identity; |
| 4248 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4249 | ICS.Standard.setToType(0, T2); |
| 4250 | ICS.Standard.setToType(1, T1); |
| 4251 | ICS.Standard.setToType(2, T1); |
| 4252 | ICS.Standard.ReferenceBinding = true; |
| 4253 | // In C++0x, this is always a direct binding. In C++98/03, it's a direct |
| 4254 | // binding unless we're binding to a class prvalue. |
| 4255 | // Note: Although xvalues wouldn't normally show up in C++98/03 code, we |
| 4256 | // allow the use of rvalue references in C++98/03 for the benefit of |
| 4257 | // standard library implementors; therefore, we need the xvalue check here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4258 | ICS.Standard.DirectBinding = |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4259 | S.getLangOpts().CPlusPlus11 || |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4260 | (InitCategory.isPRValue() && !T2->isRecordType()); |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4261 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4262 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4263 | ICS.Standard.BindsToRvalue = InitCategory.isRValue(); |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4264 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4265 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4266 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4267 | return ICS; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4268 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4269 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4270 | // -- has a class type (i.e., T2 is a class type), where T1 is not |
| 4271 | // reference-related to T2, and can be implicitly converted to |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4272 | // an xvalue, class prvalue, or function lvalue of type |
| 4273 | // "cv3 T3", where "cv1 T1" is reference-compatible with |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4274 | // "cv3 T3", |
| 4275 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4276 | // then the reference is bound to the value of the initializer |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4277 | // expression in the first case and to the result of the conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4278 | // in the second case (or, in either case, to an appropriate base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4279 | // class subobject). |
| 4280 | if (!SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4281 | T2->isRecordType() && !S.RequireCompleteType(DeclLoc, T2, 0) && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4282 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4283 | Init, T2, /*AllowRvalues=*/true, |
| 4284 | AllowExplicit)) { |
| 4285 | // In the second case, if the reference is an rvalue reference |
| 4286 | // and the second standard conversion sequence of the |
| 4287 | // user-defined conversion sequence includes an lvalue-to-rvalue |
| 4288 | // conversion, the program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4289 | if (ICS.isUserDefined() && isRValRef && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4290 | ICS.UserDefined.After.First == ICK_Lvalue_To_Rvalue) |
| 4291 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4292 | |
Douglas Gregor | 68ed68b | 2011-01-21 16:36:05 +0000 | [diff] [blame] | 4293 | return ICS; |
Rafael Espindola | aa5952c | 2011-01-22 15:32:35 +0000 | [diff] [blame] | 4294 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4295 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4296 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 4297 | // initialized from the initializer expression using the |
| 4298 | // rules for a non-reference copy initialization (8.5). The |
| 4299 | // reference is then bound to the temporary. If T1 is |
| 4300 | // reference-related to T2, cv1 must be the same |
| 4301 | // cv-qualification as, or greater cv-qualification than, |
| 4302 | // cv2; otherwise, the program is ill-formed. |
| 4303 | if (RefRelationship == Sema::Ref_Related) { |
| 4304 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 4305 | // we would be reference-compatible or reference-compatible with |
| 4306 | // added qualification. But that wasn't the case, so the reference |
| 4307 | // initialization fails. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4308 | // |
| 4309 | // Note that we only want to check address spaces and cvr-qualifiers here. |
| 4310 | // ObjC GC and lifetime qualifiers aren't important. |
| 4311 | Qualifiers T1Quals = T1.getQualifiers(); |
| 4312 | Qualifiers T2Quals = T2.getQualifiers(); |
| 4313 | T1Quals.removeObjCGCAttr(); |
| 4314 | T1Quals.removeObjCLifetime(); |
| 4315 | T2Quals.removeObjCGCAttr(); |
| 4316 | T2Quals.removeObjCLifetime(); |
| 4317 | if (!T1Quals.compatiblyIncludes(T2Quals)) |
| 4318 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4319 | } |
| 4320 | |
| 4321 | // If at least one of the types is a class type, the types are not |
| 4322 | // related, and we aren't allowed any user conversions, the |
| 4323 | // reference binding fails. This case is important for breaking |
| 4324 | // recursion, since TryImplicitConversion below will attempt to |
| 4325 | // create a temporary through the use of a copy constructor. |
| 4326 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 4327 | (T1->isRecordType() || T2->isRecordType())) |
| 4328 | return ICS; |
| 4329 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4330 | // If T1 is reference-related to T2 and the reference is an rvalue |
| 4331 | // reference, the initializer expression shall not be an lvalue. |
| 4332 | if (RefRelationship >= Sema::Ref_Related && |
| 4333 | isRValRef && Init->Classify(S.Context).isLValue()) |
| 4334 | return ICS; |
| 4335 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4336 | // C++ [over.ics.ref]p2: |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4337 | // When a parameter of reference type is not bound directly to |
| 4338 | // an argument expression, the conversion sequence is the one |
| 4339 | // required to convert the argument expression to the |
| 4340 | // underlying type of the reference according to |
| 4341 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 4342 | // to copy-initializing a temporary of the underlying type with |
| 4343 | // the argument expression. Any difference in top-level |
| 4344 | // cv-qualification is subsumed by the initialization itself |
| 4345 | // and does not constitute a conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4346 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
| 4347 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4348 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4349 | /*CStyle=*/false, |
| 4350 | /*AllowObjCWritebackConversion=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4351 | |
| 4352 | // Of course, that's still a reference binding. |
| 4353 | if (ICS.isStandard()) { |
| 4354 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4355 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4356 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4357 | ICS.Standard.BindsToRvalue = true; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4358 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4359 | ICS.Standard.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4360 | } else if (ICS.isUserDefined()) { |
Douglas Gregor | 203050c | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4361 | // Don't allow rvalue references to bind to lvalues. |
| 4362 | if (DeclType->isRValueReferenceType()) { |
| 4363 | if (const ReferenceType *RefType |
| 4364 | = ICS.UserDefined.ConversionFunction->getResultType() |
| 4365 | ->getAs<LValueReferenceType>()) { |
| 4366 | if (!RefType->getPointeeType()->isFunctionType()) { |
| 4367 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, Init, |
| 4368 | DeclType); |
| 4369 | return ICS; |
| 4370 | } |
| 4371 | } |
| 4372 | } |
| 4373 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4374 | ICS.UserDefined.After.ReferenceBinding = true; |
Douglas Gregor | f20d272 | 2011-08-15 13:59:46 +0000 | [diff] [blame] | 4375 | ICS.UserDefined.After.IsLvalueReference = !isRValRef; |
| 4376 | ICS.UserDefined.After.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4377 | ICS.UserDefined.After.BindsToRvalue = true; |
| 4378 | ICS.UserDefined.After.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4379 | ICS.UserDefined.After.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4380 | } |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4381 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4382 | return ICS; |
| 4383 | } |
| 4384 | |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4385 | static ImplicitConversionSequence |
| 4386 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
| 4387 | bool SuppressUserConversions, |
| 4388 | bool InOverloadResolution, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4389 | bool AllowObjCWritebackConversion, |
| 4390 | bool AllowExplicit = false); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4391 | |
| 4392 | /// TryListConversion - Try to copy-initialize a value of type ToType from the |
| 4393 | /// initializer list From. |
| 4394 | static ImplicitConversionSequence |
| 4395 | TryListConversion(Sema &S, InitListExpr *From, QualType ToType, |
| 4396 | bool SuppressUserConversions, |
| 4397 | bool InOverloadResolution, |
| 4398 | bool AllowObjCWritebackConversion) { |
| 4399 | // C++11 [over.ics.list]p1: |
| 4400 | // When an argument is an initializer list, it is not an expression and |
| 4401 | // special rules apply for converting it to a parameter type. |
| 4402 | |
| 4403 | ImplicitConversionSequence Result; |
| 4404 | Result.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 4405 | |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4406 | // 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] | 4407 | // initialized from init lists. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4408 | if (S.RequireCompleteType(From->getLocStart(), ToType, 0)) |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4409 | return Result; |
| 4410 | |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4411 | // C++11 [over.ics.list]p2: |
| 4412 | // If the parameter type is std::initializer_list<X> or "array of X" and |
| 4413 | // all the elements can be implicitly converted to X, the implicit |
| 4414 | // conversion sequence is the worst conversion necessary to convert an |
| 4415 | // element of the list to X. |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4416 | bool toStdInitializerList = false; |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4417 | QualType X; |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4418 | if (ToType->isArrayType()) |
Richard Smith | 2801d9a | 2012-12-09 06:48:56 +0000 | [diff] [blame] | 4419 | X = S.Context.getAsArrayType(ToType)->getElementType(); |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4420 | else |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4421 | toStdInitializerList = S.isStdInitializerList(ToType, &X); |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4422 | if (!X.isNull()) { |
| 4423 | for (unsigned i = 0, e = From->getNumInits(); i < e; ++i) { |
| 4424 | Expr *Init = From->getInit(i); |
| 4425 | ImplicitConversionSequence ICS = |
| 4426 | TryCopyInitialization(S, Init, X, SuppressUserConversions, |
| 4427 | InOverloadResolution, |
| 4428 | AllowObjCWritebackConversion); |
| 4429 | // If a single element isn't convertible, fail. |
| 4430 | if (ICS.isBad()) { |
| 4431 | Result = ICS; |
| 4432 | break; |
| 4433 | } |
| 4434 | // Otherwise, look for the worst conversion. |
| 4435 | if (Result.isBad() || |
| 4436 | CompareImplicitConversionSequences(S, ICS, Result) == |
| 4437 | ImplicitConversionSequence::Worse) |
| 4438 | Result = ICS; |
| 4439 | } |
Douglas Gregor | 5b4bf13 | 2012-04-04 23:09:20 +0000 | [diff] [blame] | 4440 | |
| 4441 | // For an empty list, we won't have computed any conversion sequence. |
| 4442 | // Introduce the identity conversion sequence. |
| 4443 | if (From->getNumInits() == 0) { |
| 4444 | Result.setStandard(); |
| 4445 | Result.Standard.setAsIdentityConversion(); |
| 4446 | Result.Standard.setFromType(ToType); |
| 4447 | Result.Standard.setAllToTypes(ToType); |
| 4448 | } |
| 4449 | |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4450 | Result.setStdInitializerListElement(toStdInitializerList); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4451 | return Result; |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4452 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4453 | |
| 4454 | // C++11 [over.ics.list]p3: |
| 4455 | // Otherwise, if the parameter is a non-aggregate class X and overload |
| 4456 | // resolution chooses a single best constructor [...] the implicit |
| 4457 | // conversion sequence is a user-defined conversion sequence. If multiple |
| 4458 | // constructors are viable but none is better than the others, the |
| 4459 | // implicit conversion sequence is a user-defined conversion sequence. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4460 | if (ToType->isRecordType() && !ToType->isAggregateType()) { |
| 4461 | // This function can deal with initializer lists. |
Richard Smith | 798186a | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 4462 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 4463 | /*AllowExplicit=*/false, |
| 4464 | InOverloadResolution, /*CStyle=*/false, |
| 4465 | AllowObjCWritebackConversion); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4466 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4467 | |
| 4468 | // C++11 [over.ics.list]p4: |
| 4469 | // Otherwise, if the parameter has an aggregate type which can be |
| 4470 | // initialized from the initializer list [...] the implicit conversion |
| 4471 | // sequence is a user-defined conversion sequence. |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4472 | if (ToType->isAggregateType()) { |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4473 | // Type is an aggregate, argument is an init list. At this point it comes |
| 4474 | // down to checking whether the initialization works. |
| 4475 | // FIXME: Find out whether this parameter is consumed or not. |
| 4476 | InitializedEntity Entity = |
| 4477 | InitializedEntity::InitializeParameter(S.Context, ToType, |
| 4478 | /*Consumed=*/false); |
| 4479 | if (S.CanPerformCopyInitialization(Entity, S.Owned(From))) { |
| 4480 | Result.setUserDefined(); |
| 4481 | Result.UserDefined.Before.setAsIdentityConversion(); |
| 4482 | // Initializer lists don't have a type. |
| 4483 | Result.UserDefined.Before.setFromType(QualType()); |
| 4484 | Result.UserDefined.Before.setAllToTypes(QualType()); |
| 4485 | |
| 4486 | Result.UserDefined.After.setAsIdentityConversion(); |
| 4487 | Result.UserDefined.After.setFromType(ToType); |
| 4488 | Result.UserDefined.After.setAllToTypes(ToType); |
Benjamin Kramer | 83db10e | 2012-02-02 19:35:29 +0000 | [diff] [blame] | 4489 | Result.UserDefined.ConversionFunction = 0; |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4490 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4491 | return Result; |
| 4492 | } |
| 4493 | |
| 4494 | // C++11 [over.ics.list]p5: |
| 4495 | // 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] | 4496 | if (ToType->isReferenceType()) { |
| 4497 | // The standard is notoriously unclear here, since 13.3.3.1.4 doesn't |
| 4498 | // mention initializer lists in any way. So we go by what list- |
| 4499 | // initialization would do and try to extrapolate from that. |
| 4500 | |
| 4501 | QualType T1 = ToType->getAs<ReferenceType>()->getPointeeType(); |
| 4502 | |
| 4503 | // If the initializer list has a single element that is reference-related |
| 4504 | // to the parameter type, we initialize the reference from that. |
| 4505 | if (From->getNumInits() == 1) { |
| 4506 | Expr *Init = From->getInit(0); |
| 4507 | |
| 4508 | QualType T2 = Init->getType(); |
| 4509 | |
| 4510 | // If the initializer is the address of an overloaded function, try |
| 4511 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4512 | // type of the resulting function. |
| 4513 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4514 | DeclAccessPair Found; |
| 4515 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction( |
| 4516 | Init, ToType, false, Found)) |
| 4517 | T2 = Fn->getType(); |
| 4518 | } |
| 4519 | |
| 4520 | // Compute some basic properties of the types and the initializer. |
| 4521 | bool dummy1 = false; |
| 4522 | bool dummy2 = false; |
| 4523 | bool dummy3 = false; |
| 4524 | Sema::ReferenceCompareResult RefRelationship |
| 4525 | = S.CompareReferenceRelationship(From->getLocStart(), T1, T2, dummy1, |
| 4526 | dummy2, dummy3); |
| 4527 | |
Richard Smith | 3082be2 | 2013-09-06 01:22:42 +0000 | [diff] [blame] | 4528 | if (RefRelationship >= Sema::Ref_Related) { |
Richard Smith | 798186a | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 4529 | return TryReferenceInit(S, Init, ToType, /*FIXME*/From->getLocStart(), |
| 4530 | SuppressUserConversions, |
| 4531 | /*AllowExplicit=*/false); |
Richard Smith | 3082be2 | 2013-09-06 01:22:42 +0000 | [diff] [blame] | 4532 | } |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4533 | } |
| 4534 | |
| 4535 | // Otherwise, we bind the reference to a temporary created from the |
| 4536 | // initializer list. |
| 4537 | Result = TryListConversion(S, From, T1, SuppressUserConversions, |
| 4538 | InOverloadResolution, |
| 4539 | AllowObjCWritebackConversion); |
| 4540 | if (Result.isFailure()) |
| 4541 | return Result; |
| 4542 | assert(!Result.isEllipsis() && |
| 4543 | "Sub-initialization cannot result in ellipsis conversion."); |
| 4544 | |
| 4545 | // Can we even bind to a temporary? |
| 4546 | if (ToType->isRValueReferenceType() || |
| 4547 | (T1.isConstQualified() && !T1.isVolatileQualified())) { |
| 4548 | StandardConversionSequence &SCS = Result.isStandard() ? Result.Standard : |
| 4549 | Result.UserDefined.After; |
| 4550 | SCS.ReferenceBinding = true; |
| 4551 | SCS.IsLvalueReference = ToType->isLValueReferenceType(); |
| 4552 | SCS.BindsToRvalue = true; |
| 4553 | SCS.BindsToFunctionLvalue = false; |
| 4554 | SCS.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4555 | SCS.ObjCLifetimeConversionBinding = false; |
| 4556 | } else |
| 4557 | Result.setBad(BadConversionSequence::lvalue_ref_to_rvalue, |
| 4558 | From, ToType); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4559 | return Result; |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4560 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4561 | |
| 4562 | // C++11 [over.ics.list]p6: |
| 4563 | // Otherwise, if the parameter type is not a class: |
| 4564 | if (!ToType->isRecordType()) { |
| 4565 | // - if the initializer list has one element, the implicit conversion |
| 4566 | // sequence is the one required to convert the element to the |
| 4567 | // parameter type. |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4568 | unsigned NumInits = From->getNumInits(); |
| 4569 | if (NumInits == 1) |
| 4570 | Result = TryCopyInitialization(S, From->getInit(0), ToType, |
| 4571 | SuppressUserConversions, |
| 4572 | InOverloadResolution, |
| 4573 | AllowObjCWritebackConversion); |
| 4574 | // - if the initializer list has no elements, the implicit conversion |
| 4575 | // sequence is the identity conversion. |
| 4576 | else if (NumInits == 0) { |
| 4577 | Result.setStandard(); |
| 4578 | Result.Standard.setAsIdentityConversion(); |
John McCall | e14ba2c | 2012-04-04 02:40:27 +0000 | [diff] [blame] | 4579 | Result.Standard.setFromType(ToType); |
| 4580 | Result.Standard.setAllToTypes(ToType); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4581 | } |
| 4582 | return Result; |
| 4583 | } |
| 4584 | |
| 4585 | // C++11 [over.ics.list]p7: |
| 4586 | // In all cases other than those enumerated above, no conversion is possible |
| 4587 | return Result; |
| 4588 | } |
| 4589 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4590 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 4591 | /// ToType from the expression From. Return the implicit conversion |
| 4592 | /// sequence required to pass this argument, which may be a bad |
| 4593 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4594 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | 74e386e | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 4595 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4596 | static ImplicitConversionSequence |
| 4597 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4598 | bool SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4599 | bool InOverloadResolution, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4600 | bool AllowObjCWritebackConversion, |
| 4601 | bool AllowExplicit) { |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4602 | if (InitListExpr *FromInitList = dyn_cast<InitListExpr>(From)) |
| 4603 | return TryListConversion(S, FromInitList, ToType, SuppressUserConversions, |
| 4604 | InOverloadResolution,AllowObjCWritebackConversion); |
| 4605 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4606 | if (ToType->isReferenceType()) |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4607 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4608 | /*FIXME:*/From->getLocStart(), |
| 4609 | SuppressUserConversions, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4610 | AllowExplicit); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4611 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4612 | return TryImplicitConversion(S, From, ToType, |
| 4613 | SuppressUserConversions, |
| 4614 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4615 | InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4616 | /*CStyle=*/false, |
| 4617 | AllowObjCWritebackConversion); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4618 | } |
| 4619 | |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 4620 | static bool TryCopyInitialization(const CanQualType FromQTy, |
| 4621 | const CanQualType ToQTy, |
| 4622 | Sema &S, |
| 4623 | SourceLocation Loc, |
| 4624 | ExprValueKind FromVK) { |
| 4625 | OpaqueValueExpr TmpExpr(Loc, FromQTy, FromVK); |
| 4626 | ImplicitConversionSequence ICS = |
| 4627 | TryCopyInitialization(S, &TmpExpr, ToQTy, true, true, false); |
| 4628 | |
| 4629 | return !ICS.isBad(); |
| 4630 | } |
| 4631 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4632 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 4633 | /// parameter of the given member function (@c Method) from the |
| 4634 | /// expression @p From. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4635 | static ImplicitConversionSequence |
Richard Smith | 98bfbf5 | 2013-01-26 02:07:32 +0000 | [diff] [blame] | 4636 | TryObjectArgumentInitialization(Sema &S, QualType FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4637 | Expr::Classification FromClassification, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4638 | CXXMethodDecl *Method, |
| 4639 | CXXRecordDecl *ActingContext) { |
| 4640 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 4641 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 4642 | // const volatile object. |
| 4643 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 4644 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4645 | QualType ImplicitParamType = S.Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4646 | |
| 4647 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 4648 | // to exit early. |
| 4649 | ImplicitConversionSequence ICS; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4650 | |
| 4651 | // We need to have an object of class type. |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4652 | if (const PointerType *PT = FromType->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4653 | FromType = PT->getPointeeType(); |
| 4654 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4655 | // When we had a pointer, it's implicitly dereferenced, so we |
| 4656 | // better have an lvalue. |
| 4657 | assert(FromClassification.isLValue()); |
| 4658 | } |
| 4659 | |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4660 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4661 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4662 | // C++0x [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4663 | // For non-static member functions, the type of the implicit object |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4664 | // parameter is |
| 4665 | // |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4666 | // - "lvalue reference to cv X" for functions declared without a |
| 4667 | // ref-qualifier or with the & ref-qualifier |
| 4668 | // - "rvalue reference to cv X" for functions declared with the && |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4669 | // ref-qualifier |
| 4670 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4671 | // 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] | 4672 | // cv-qualification on the member function declaration. |
| 4673 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4674 | // However, when finding an implicit conversion sequence for the argument, we |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4675 | // are not allowed to create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4676 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 4677 | // reference binding here, that allows class rvalues to bind to |
| 4678 | // non-constant references. |
| 4679 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4680 | // First check the qualifiers. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4681 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4682 | if (ImplicitParamType.getCVRQualifiers() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4683 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4684 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4685 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
Richard Smith | 98bfbf5 | 2013-01-26 02:07:32 +0000 | [diff] [blame] | 4686 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4687 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4688 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4689 | |
| 4690 | // Check that we have either the same type or a derived type. It |
| 4691 | // affects the conversion rank. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4692 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4693 | ImplicitConversionKind SecondKind; |
| 4694 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 4695 | SecondKind = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4696 | } else if (S.IsDerivedFrom(FromType, ClassType)) |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4697 | SecondKind = ICK_Derived_To_Base; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4698 | else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4699 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 4700 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4701 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4702 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4703 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4704 | // Check the ref-qualifier. |
| 4705 | switch (Method->getRefQualifier()) { |
| 4706 | case RQ_None: |
| 4707 | // Do nothing; we don't care about lvalueness or rvalueness. |
| 4708 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4709 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4710 | case RQ_LValue: |
| 4711 | if (!FromClassification.isLValue() && Quals != Qualifiers::Const) { |
| 4712 | // non-const lvalue reference cannot bind to an rvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4713 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4714 | ImplicitParamType); |
| 4715 | return ICS; |
| 4716 | } |
| 4717 | break; |
| 4718 | |
| 4719 | case RQ_RValue: |
| 4720 | if (!FromClassification.isRValue()) { |
| 4721 | // rvalue reference cannot bind to an lvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4722 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4723 | ImplicitParamType); |
| 4724 | return ICS; |
| 4725 | } |
| 4726 | break; |
| 4727 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4728 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4729 | // Success. Mark this as a reference binding. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4730 | ICS.setStandard(); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4731 | ICS.Standard.setAsIdentityConversion(); |
| 4732 | ICS.Standard.Second = SecondKind; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4733 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 4734 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4735 | ICS.Standard.ReferenceBinding = true; |
| 4736 | ICS.Standard.DirectBinding = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4737 | ICS.Standard.IsLvalueReference = Method->getRefQualifier() != RQ_RValue; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4738 | ICS.Standard.BindsToFunctionLvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4739 | ICS.Standard.BindsToRvalue = FromClassification.isRValue(); |
| 4740 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier |
| 4741 | = (Method->getRefQualifier() == RQ_None); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4742 | return ICS; |
| 4743 | } |
| 4744 | |
| 4745 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 4746 | /// the implicit object parameter for the given Method with the given |
| 4747 | /// expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4748 | ExprResult |
| 4749 | Sema::PerformObjectArgumentInitialization(Expr *From, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4750 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4751 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4752 | CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4753 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4754 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4755 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4756 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4757 | Expr::Classification FromClassification; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4758 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4759 | FromRecordType = PT->getPointeeType(); |
| 4760 | DestType = Method->getThisType(Context); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4761 | FromClassification = Expr::Classification::makeSimpleLValue(); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4762 | } else { |
| 4763 | FromRecordType = From->getType(); |
| 4764 | DestType = ImplicitParamRecordType; |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4765 | FromClassification = From->Classify(Context); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4766 | } |
| 4767 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4768 | // Note that we always use the true parent context when performing |
| 4769 | // the actual argument initialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4770 | ImplicitConversionSequence ICS |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4771 | = TryObjectArgumentInitialization(*this, From->getType(), FromClassification, |
| 4772 | Method, Method->getParent()); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4773 | if (ICS.isBad()) { |
| 4774 | if (ICS.Bad.Kind == BadConversionSequence::bad_qualifiers) { |
| 4775 | Qualifiers FromQs = FromRecordType.getQualifiers(); |
| 4776 | Qualifiers ToQs = DestType.getQualifiers(); |
| 4777 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 4778 | if (CVR) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4779 | Diag(From->getLocStart(), |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4780 | diag::err_member_function_call_bad_cvr) |
| 4781 | << Method->getDeclName() << FromRecordType << (CVR - 1) |
| 4782 | << From->getSourceRange(); |
| 4783 | Diag(Method->getLocation(), diag::note_previous_decl) |
| 4784 | << Method->getDeclName(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4785 | return ExprError(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4786 | } |
| 4787 | } |
| 4788 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4789 | return Diag(From->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4790 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4791 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4792 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4793 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4794 | if (ICS.Standard.Second == ICK_Derived_To_Base) { |
| 4795 | ExprResult FromRes = |
| 4796 | PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
| 4797 | if (FromRes.isInvalid()) |
| 4798 | return ExprError(); |
| 4799 | From = FromRes.take(); |
| 4800 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4801 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4802 | if (!Context.hasSameType(From->getType(), DestType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4803 | From = ImpCastExprToType(From, DestType, CK_NoOp, |
Richard Smith | acdfa4d | 2011-11-10 23:32:36 +0000 | [diff] [blame] | 4804 | From->getValueKind()).take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4805 | return Owned(From); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4806 | } |
| 4807 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4808 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 4809 | /// expression From to bool (C++0x [conv]p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4810 | static ImplicitConversionSequence |
| 4811 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
Douglas Gregor | c6dfe19 | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 4812 | // FIXME: This is pretty broken. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4813 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 4814 | // FIXME: Are these flags correct? |
| 4815 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4816 | /*AllowExplicit=*/true, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4817 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4818 | /*CStyle=*/false, |
| 4819 | /*AllowObjCWritebackConversion=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4820 | } |
| 4821 | |
| 4822 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 4823 | /// of the expression From to bool (C++0x [conv]p3). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4824 | ExprResult Sema::PerformContextuallyConvertToBool(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4825 | if (checkPlaceholderForOverload(*this, From)) |
| 4826 | return ExprError(); |
| 4827 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4828 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4829 | if (!ICS.isBad()) |
| 4830 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4831 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 4832 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4833 | return Diag(From->getLocStart(), |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4834 | diag::err_typecheck_bool_condition) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 4835 | << From->getType() << From->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4836 | return ExprError(); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4837 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4838 | |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4839 | /// Check that the specified conversion is permitted in a converted constant |
| 4840 | /// expression, according to C++11 [expr.const]p3. Return true if the conversion |
| 4841 | /// is acceptable. |
| 4842 | static bool CheckConvertedConstantConversions(Sema &S, |
| 4843 | StandardConversionSequence &SCS) { |
| 4844 | // Since we know that the target type is an integral or unscoped enumeration |
| 4845 | // type, most conversion kinds are impossible. All possible First and Third |
| 4846 | // conversions are fine. |
| 4847 | switch (SCS.Second) { |
| 4848 | case ICK_Identity: |
| 4849 | case ICK_Integral_Promotion: |
| 4850 | case ICK_Integral_Conversion: |
Guy Benyei | 6959acd | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 4851 | case ICK_Zero_Event_Conversion: |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4852 | return true; |
| 4853 | |
| 4854 | case ICK_Boolean_Conversion: |
Richard Smith | 2bcb984 | 2012-09-13 22:00:12 +0000 | [diff] [blame] | 4855 | // Conversion from an integral or unscoped enumeration type to bool is |
| 4856 | // classified as ICK_Boolean_Conversion, but it's also an integral |
| 4857 | // conversion, so it's permitted in a converted constant expression. |
| 4858 | return SCS.getFromType()->isIntegralOrUnscopedEnumerationType() && |
| 4859 | SCS.getToType(2)->isBooleanType(); |
| 4860 | |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4861 | case ICK_Floating_Integral: |
| 4862 | case ICK_Complex_Real: |
| 4863 | return false; |
| 4864 | |
| 4865 | case ICK_Lvalue_To_Rvalue: |
| 4866 | case ICK_Array_To_Pointer: |
| 4867 | case ICK_Function_To_Pointer: |
| 4868 | case ICK_NoReturn_Adjustment: |
| 4869 | case ICK_Qualification: |
| 4870 | case ICK_Compatible_Conversion: |
| 4871 | case ICK_Vector_Conversion: |
| 4872 | case ICK_Vector_Splat: |
| 4873 | case ICK_Derived_To_Base: |
| 4874 | case ICK_Pointer_Conversion: |
| 4875 | case ICK_Pointer_Member: |
| 4876 | case ICK_Block_Pointer_Conversion: |
| 4877 | case ICK_Writeback_Conversion: |
| 4878 | case ICK_Floating_Promotion: |
| 4879 | case ICK_Complex_Promotion: |
| 4880 | case ICK_Complex_Conversion: |
| 4881 | case ICK_Floating_Conversion: |
| 4882 | case ICK_TransparentUnionConversion: |
| 4883 | llvm_unreachable("unexpected second conversion kind"); |
| 4884 | |
| 4885 | case ICK_Num_Conversion_Kinds: |
| 4886 | break; |
| 4887 | } |
| 4888 | |
| 4889 | llvm_unreachable("unknown conversion kind"); |
| 4890 | } |
| 4891 | |
| 4892 | /// CheckConvertedConstantExpression - Check that the expression From is a |
| 4893 | /// converted constant expression of type T, perform the conversion and produce |
| 4894 | /// the converted expression, per C++11 [expr.const]p3. |
| 4895 | ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, |
| 4896 | llvm::APSInt &Value, |
| 4897 | CCEKind CCE) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4898 | assert(LangOpts.CPlusPlus11 && "converted constant expression outside C++11"); |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4899 | assert(T->isIntegralOrEnumerationType() && "unexpected converted const type"); |
| 4900 | |
| 4901 | if (checkPlaceholderForOverload(*this, From)) |
| 4902 | return ExprError(); |
| 4903 | |
| 4904 | // C++11 [expr.const]p3 with proposed wording fixes: |
| 4905 | // A converted constant expression of type T is a core constant expression, |
| 4906 | // implicitly converted to a prvalue of type T, where the converted |
| 4907 | // expression is a literal constant expression and the implicit conversion |
| 4908 | // sequence contains only user-defined conversions, lvalue-to-rvalue |
| 4909 | // conversions, integral promotions, and integral conversions other than |
| 4910 | // narrowing conversions. |
| 4911 | ImplicitConversionSequence ICS = |
| 4912 | TryImplicitConversion(From, T, |
| 4913 | /*SuppressUserConversions=*/false, |
| 4914 | /*AllowExplicit=*/false, |
| 4915 | /*InOverloadResolution=*/false, |
| 4916 | /*CStyle=*/false, |
| 4917 | /*AllowObjcWritebackConversion=*/false); |
| 4918 | StandardConversionSequence *SCS = 0; |
| 4919 | switch (ICS.getKind()) { |
| 4920 | case ImplicitConversionSequence::StandardConversion: |
| 4921 | if (!CheckConvertedConstantConversions(*this, ICS.Standard)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4922 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4923 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 4924 | << From->getType() << From->getSourceRange() << T; |
| 4925 | SCS = &ICS.Standard; |
| 4926 | break; |
| 4927 | case ImplicitConversionSequence::UserDefinedConversion: |
| 4928 | // We are converting from class type to an integral or enumeration type, so |
| 4929 | // the Before sequence must be trivial. |
| 4930 | if (!CheckConvertedConstantConversions(*this, ICS.UserDefined.After)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4931 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4932 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 4933 | << From->getType() << From->getSourceRange() << T; |
| 4934 | SCS = &ICS.UserDefined.After; |
| 4935 | break; |
| 4936 | case ImplicitConversionSequence::AmbiguousConversion: |
| 4937 | case ImplicitConversionSequence::BadConversion: |
| 4938 | if (!DiagnoseMultipleUserDefinedConversion(From, T)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4939 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4940 | diag::err_typecheck_converted_constant_expression) |
| 4941 | << From->getType() << From->getSourceRange() << T; |
| 4942 | return ExprError(); |
| 4943 | |
| 4944 | case ImplicitConversionSequence::EllipsisConversion: |
| 4945 | llvm_unreachable("ellipsis conversion in converted constant expression"); |
| 4946 | } |
| 4947 | |
| 4948 | ExprResult Result = PerformImplicitConversion(From, T, ICS, AA_Converting); |
| 4949 | if (Result.isInvalid()) |
| 4950 | return Result; |
| 4951 | |
| 4952 | // Check for a narrowing implicit conversion. |
| 4953 | APValue PreNarrowingValue; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4954 | QualType PreNarrowingType; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4955 | switch (SCS->getNarrowingKind(Context, Result.get(), PreNarrowingValue, |
| 4956 | PreNarrowingType)) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4957 | case NK_Variable_Narrowing: |
| 4958 | // Implicit conversion to a narrower type, and the value is not a constant |
| 4959 | // expression. We'll diagnose this in a moment. |
| 4960 | case NK_Not_Narrowing: |
| 4961 | break; |
| 4962 | |
| 4963 | case NK_Constant_Narrowing: |
Eli Friedman | 1ef28db | 2012-03-29 23:39:39 +0000 | [diff] [blame] | 4964 | Diag(From->getLocStart(), |
| 4965 | isSFINAEContext() ? diag::err_cce_narrowing_sfinae : |
| 4966 | diag::err_cce_narrowing) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4967 | << CCE << /*Constant*/1 |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4968 | << PreNarrowingValue.getAsString(Context, PreNarrowingType) << T; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4969 | break; |
| 4970 | |
| 4971 | case NK_Type_Narrowing: |
Eli Friedman | 1ef28db | 2012-03-29 23:39:39 +0000 | [diff] [blame] | 4972 | Diag(From->getLocStart(), |
| 4973 | isSFINAEContext() ? diag::err_cce_narrowing_sfinae : |
| 4974 | diag::err_cce_narrowing) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4975 | << CCE << /*Constant*/0 << From->getType() << T; |
| 4976 | break; |
| 4977 | } |
| 4978 | |
| 4979 | // Check the expression is a constant expression. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4980 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4981 | Expr::EvalResult Eval; |
| 4982 | Eval.Diag = &Notes; |
| 4983 | |
Douglas Gregor | 484f6fa | 2013-04-08 23:24:07 +0000 | [diff] [blame] | 4984 | if (!Result.get()->EvaluateAsRValue(Eval, Context) || !Eval.Val.isInt()) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4985 | // The expression can't be folded, so we can't keep it at this position in |
| 4986 | // the AST. |
| 4987 | Result = ExprError(); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 4988 | } else { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4989 | Value = Eval.Val.getInt(); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 4990 | |
| 4991 | if (Notes.empty()) { |
| 4992 | // It's a constant expression. |
| 4993 | return Result; |
| 4994 | } |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4995 | } |
| 4996 | |
| 4997 | // It's not a constant expression. Produce an appropriate diagnostic. |
| 4998 | if (Notes.size() == 1 && |
| 4999 | Notes[0].second.getDiagID() == diag::note_invalid_subexpr_in_const_expr) |
| 5000 | Diag(Notes[0].first, diag::err_expr_not_cce) << CCE; |
| 5001 | else { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5002 | Diag(From->getLocStart(), diag::err_expr_not_cce) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5003 | << CCE << From->getSourceRange(); |
| 5004 | for (unsigned I = 0; I < Notes.size(); ++I) |
| 5005 | Diag(Notes[I].first, Notes[I].second); |
| 5006 | } |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5007 | return Result; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5008 | } |
| 5009 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5010 | /// dropPointerConversions - If the given standard conversion sequence |
| 5011 | /// involves any pointer conversions, remove them. This may change |
| 5012 | /// the result type of the conversion sequence. |
| 5013 | static void dropPointerConversion(StandardConversionSequence &SCS) { |
| 5014 | if (SCS.Second == ICK_Pointer_Conversion) { |
| 5015 | SCS.Second = ICK_Identity; |
| 5016 | SCS.Third = ICK_Identity; |
| 5017 | SCS.ToTypePtrs[2] = SCS.ToTypePtrs[1] = SCS.ToTypePtrs[0]; |
| 5018 | } |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5019 | } |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5020 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5021 | /// TryContextuallyConvertToObjCPointer - Attempt to contextually |
| 5022 | /// convert the expression From to an Objective-C pointer type. |
| 5023 | static ImplicitConversionSequence |
| 5024 | TryContextuallyConvertToObjCPointer(Sema &S, Expr *From) { |
| 5025 | // Do an implicit conversion to 'id'. |
| 5026 | QualType Ty = S.Context.getObjCIdType(); |
| 5027 | ImplicitConversionSequence ICS |
| 5028 | = TryImplicitConversion(S, From, Ty, |
| 5029 | // FIXME: Are these flags correct? |
| 5030 | /*SuppressUserConversions=*/false, |
| 5031 | /*AllowExplicit=*/true, |
| 5032 | /*InOverloadResolution=*/false, |
| 5033 | /*CStyle=*/false, |
| 5034 | /*AllowObjCWritebackConversion=*/false); |
| 5035 | |
| 5036 | // Strip off any final conversions to 'id'. |
| 5037 | switch (ICS.getKind()) { |
| 5038 | case ImplicitConversionSequence::BadConversion: |
| 5039 | case ImplicitConversionSequence::AmbiguousConversion: |
| 5040 | case ImplicitConversionSequence::EllipsisConversion: |
| 5041 | break; |
| 5042 | |
| 5043 | case ImplicitConversionSequence::UserDefinedConversion: |
| 5044 | dropPointerConversion(ICS.UserDefined.After); |
| 5045 | break; |
| 5046 | |
| 5047 | case ImplicitConversionSequence::StandardConversion: |
| 5048 | dropPointerConversion(ICS.Standard); |
| 5049 | break; |
| 5050 | } |
| 5051 | |
| 5052 | return ICS; |
| 5053 | } |
| 5054 | |
| 5055 | /// PerformContextuallyConvertToObjCPointer - Perform a contextual |
| 5056 | /// conversion of the expression From to an Objective-C pointer type. |
| 5057 | ExprResult Sema::PerformContextuallyConvertToObjCPointer(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 5058 | if (checkPlaceholderForOverload(*this, From)) |
| 5059 | return ExprError(); |
| 5060 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5061 | QualType Ty = Context.getObjCIdType(); |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5062 | ImplicitConversionSequence ICS = |
| 5063 | TryContextuallyConvertToObjCPointer(*this, From); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5064 | if (!ICS.isBad()) |
| 5065 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5066 | return ExprError(); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5067 | } |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5068 | |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5069 | /// Determine whether the provided type is an integral type, or an enumeration |
| 5070 | /// type of a permitted flavor. |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5071 | bool Sema::ICEConvertDiagnoser::match(QualType T) { |
| 5072 | return AllowScopedEnumerations ? T->isIntegralOrEnumerationType() |
| 5073 | : T->isIntegralOrUnscopedEnumerationType(); |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5074 | } |
| 5075 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5076 | static ExprResult |
| 5077 | diagnoseAmbiguousConversion(Sema &SemaRef, SourceLocation Loc, Expr *From, |
| 5078 | Sema::ContextualImplicitConverter &Converter, |
| 5079 | QualType T, UnresolvedSetImpl &ViableConversions) { |
| 5080 | |
| 5081 | if (Converter.Suppress) |
| 5082 | return ExprError(); |
| 5083 | |
| 5084 | Converter.diagnoseAmbiguous(SemaRef, Loc, T) << From->getSourceRange(); |
| 5085 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5086 | CXXConversionDecl *Conv = |
| 5087 | cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 5088 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 5089 | Converter.noteAmbiguous(SemaRef, Conv, ConvTy); |
| 5090 | } |
| 5091 | return SemaRef.Owned(From); |
| 5092 | } |
| 5093 | |
| 5094 | static bool |
| 5095 | diagnoseNoViableConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
| 5096 | Sema::ContextualImplicitConverter &Converter, |
| 5097 | QualType T, bool HadMultipleCandidates, |
| 5098 | UnresolvedSetImpl &ExplicitConversions) { |
| 5099 | if (ExplicitConversions.size() == 1 && !Converter.Suppress) { |
| 5100 | DeclAccessPair Found = ExplicitConversions[0]; |
| 5101 | CXXConversionDecl *Conversion = |
| 5102 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 5103 | |
| 5104 | // The user probably meant to invoke the given explicit |
| 5105 | // conversion; use it. |
| 5106 | QualType ConvTy = Conversion->getConversionType().getNonReferenceType(); |
| 5107 | std::string TypeStr; |
| 5108 | ConvTy.getAsStringInternal(TypeStr, SemaRef.getPrintingPolicy()); |
| 5109 | |
| 5110 | Converter.diagnoseExplicitConv(SemaRef, Loc, T, ConvTy) |
| 5111 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 5112 | "static_cast<" + TypeStr + ">(") |
| 5113 | << FixItHint::CreateInsertion( |
| 5114 | SemaRef.PP.getLocForEndOfToken(From->getLocEnd()), ")"); |
| 5115 | Converter.noteExplicitConv(SemaRef, Conversion, ConvTy); |
| 5116 | |
| 5117 | // If we aren't in a SFINAE context, build a call to the |
| 5118 | // explicit conversion function. |
| 5119 | if (SemaRef.isSFINAEContext()) |
| 5120 | return true; |
| 5121 | |
| 5122 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
| 5123 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5124 | HadMultipleCandidates); |
| 5125 | if (Result.isInvalid()) |
| 5126 | return true; |
| 5127 | // Record usage of conversion in an implicit cast. |
| 5128 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
| 5129 | CK_UserDefinedConversion, Result.get(), 0, |
| 5130 | Result.get()->getValueKind()); |
| 5131 | } |
| 5132 | return false; |
| 5133 | } |
| 5134 | |
| 5135 | static bool recordConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
| 5136 | Sema::ContextualImplicitConverter &Converter, |
| 5137 | QualType T, bool HadMultipleCandidates, |
| 5138 | DeclAccessPair &Found) { |
| 5139 | CXXConversionDecl *Conversion = |
| 5140 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 5141 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
| 5142 | |
| 5143 | QualType ToType = Conversion->getConversionType().getNonReferenceType(); |
| 5144 | if (!Converter.SuppressConversion) { |
| 5145 | if (SemaRef.isSFINAEContext()) |
| 5146 | return true; |
| 5147 | |
| 5148 | Converter.diagnoseConversion(SemaRef, Loc, T, ToType) |
| 5149 | << From->getSourceRange(); |
| 5150 | } |
| 5151 | |
| 5152 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5153 | HadMultipleCandidates); |
| 5154 | if (Result.isInvalid()) |
| 5155 | return true; |
| 5156 | // Record usage of conversion in an implicit cast. |
| 5157 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
| 5158 | CK_UserDefinedConversion, Result.get(), 0, |
| 5159 | Result.get()->getValueKind()); |
| 5160 | return false; |
| 5161 | } |
| 5162 | |
| 5163 | static ExprResult finishContextualImplicitConversion( |
| 5164 | Sema &SemaRef, SourceLocation Loc, Expr *From, |
| 5165 | Sema::ContextualImplicitConverter &Converter) { |
| 5166 | if (!Converter.match(From->getType()) && !Converter.Suppress) |
| 5167 | Converter.diagnoseNoMatch(SemaRef, Loc, From->getType()) |
| 5168 | << From->getSourceRange(); |
| 5169 | |
| 5170 | return SemaRef.DefaultLvalueConversion(From); |
| 5171 | } |
| 5172 | |
| 5173 | static void |
| 5174 | collectViableConversionCandidates(Sema &SemaRef, Expr *From, QualType ToType, |
| 5175 | UnresolvedSetImpl &ViableConversions, |
| 5176 | OverloadCandidateSet &CandidateSet) { |
| 5177 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5178 | DeclAccessPair FoundDecl = ViableConversions[I]; |
| 5179 | NamedDecl *D = FoundDecl.getDecl(); |
| 5180 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 5181 | if (isa<UsingShadowDecl>(D)) |
| 5182 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 5183 | |
| 5184 | CXXConversionDecl *Conv; |
| 5185 | FunctionTemplateDecl *ConvTemplate; |
| 5186 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 5187 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 5188 | else |
| 5189 | Conv = cast<CXXConversionDecl>(D); |
| 5190 | |
| 5191 | if (ConvTemplate) |
| 5192 | SemaRef.AddTemplateConversionCandidate( |
| 5193 | ConvTemplate, FoundDecl, ActingContext, From, ToType, CandidateSet); |
| 5194 | else |
| 5195 | SemaRef.AddConversionCandidate(Conv, FoundDecl, ActingContext, From, |
| 5196 | ToType, CandidateSet); |
| 5197 | } |
| 5198 | } |
| 5199 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5200 | /// \brief Attempt to convert the given expression to a type which is accepted |
| 5201 | /// by the given converter. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5202 | /// |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5203 | /// This routine will attempt to convert an expression of class type to a |
| 5204 | /// type accepted by the specified converter. In C++11 and before, the class |
| 5205 | /// must have a single non-explicit conversion function converting to a matching |
| 5206 | /// type. In C++1y, there can be multiple such conversion functions, but only |
| 5207 | /// one target type. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5208 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5209 | /// \param Loc The source location of the construct that requires the |
| 5210 | /// conversion. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5211 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 5212 | /// \param From The expression we're converting from. |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5213 | /// |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5214 | /// \param Converter Used to control and diagnose the conversion process. |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5215 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5216 | /// \returns The expression, converted to an integral or enumeration type if |
| 5217 | /// successful. |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5218 | ExprResult Sema::PerformContextualImplicitConversion( |
| 5219 | SourceLocation Loc, Expr *From, ContextualImplicitConverter &Converter) { |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5220 | // We can't perform any more checking for type-dependent expressions. |
| 5221 | if (From->isTypeDependent()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5222 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5223 | |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5224 | // Process placeholders immediately. |
| 5225 | if (From->hasPlaceholderType()) { |
| 5226 | ExprResult result = CheckPlaceholderExpr(From); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5227 | if (result.isInvalid()) |
| 5228 | return result; |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5229 | From = result.take(); |
| 5230 | } |
| 5231 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5232 | // If the expression already has a matching type, we're golden. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5233 | QualType T = From->getType(); |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5234 | if (Converter.match(T)) |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5235 | return DefaultLvalueConversion(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5236 | |
| 5237 | // FIXME: Check for missing '()' if T is a function type? |
| 5238 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5239 | // We can only perform contextual implicit conversions on objects of class |
| 5240 | // type. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5241 | const RecordType *RecordTy = T->getAs<RecordType>(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5242 | if (!RecordTy || !getLangOpts().CPlusPlus) { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5243 | if (!Converter.Suppress) |
| 5244 | Converter.diagnoseNoMatch(*this, Loc, T) << From->getSourceRange(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5245 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5246 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5247 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5248 | // We must have a complete class type. |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 5249 | struct TypeDiagnoserPartialDiag : TypeDiagnoser { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5250 | ContextualImplicitConverter &Converter; |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5251 | Expr *From; |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5252 | |
| 5253 | TypeDiagnoserPartialDiag(ContextualImplicitConverter &Converter, Expr *From) |
| 5254 | : TypeDiagnoser(Converter.Suppress), Converter(Converter), From(From) {} |
| 5255 | |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5256 | virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5257 | Converter.diagnoseIncomplete(S, Loc, T) << From->getSourceRange(); |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5258 | } |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5259 | } IncompleteDiagnoser(Converter, From); |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5260 | |
| 5261 | if (RequireCompleteType(Loc, T, IncompleteDiagnoser)) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5262 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5263 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5264 | // Look for a conversion to an integral or enumeration type. |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5265 | UnresolvedSet<4> |
| 5266 | ViableConversions; // These are *potentially* viable in C++1y. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5267 | UnresolvedSet<4> ExplicitConversions; |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 5268 | std::pair<CXXRecordDecl::conversion_iterator, |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5269 | CXXRecordDecl::conversion_iterator> Conversions = |
| 5270 | cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5271 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5272 | bool HadMultipleCandidates = |
| 5273 | (std::distance(Conversions.first, Conversions.second) > 1); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5274 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5275 | // To check that there is only one target type, in C++1y: |
| 5276 | QualType ToType; |
| 5277 | bool HasUniqueTargetType = true; |
| 5278 | |
| 5279 | // Collect explicit or viable (potentially in C++1y) conversions. |
| 5280 | for (CXXRecordDecl::conversion_iterator I = Conversions.first, |
| 5281 | E = Conversions.second; |
| 5282 | I != E; ++I) { |
| 5283 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5284 | CXXConversionDecl *Conversion; |
| 5285 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 5286 | if (ConvTemplate) { |
| 5287 | if (getLangOpts().CPlusPlus1y) |
| 5288 | Conversion = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 5289 | else |
| 5290 | continue; // C++11 does not consider conversion operator templates(?). |
| 5291 | } else |
| 5292 | Conversion = cast<CXXConversionDecl>(D); |
| 5293 | |
| 5294 | assert((!ConvTemplate || getLangOpts().CPlusPlus1y) && |
| 5295 | "Conversion operator templates are considered potentially " |
| 5296 | "viable in C++1y"); |
| 5297 | |
| 5298 | QualType CurToType = Conversion->getConversionType().getNonReferenceType(); |
| 5299 | if (Converter.match(CurToType) || ConvTemplate) { |
| 5300 | |
| 5301 | if (Conversion->isExplicit()) { |
| 5302 | // FIXME: For C++1y, do we need this restriction? |
| 5303 | // cf. diagnoseNoViableConversion() |
| 5304 | if (!ConvTemplate) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5305 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5306 | } else { |
| 5307 | if (!ConvTemplate && getLangOpts().CPlusPlus1y) { |
| 5308 | if (ToType.isNull()) |
| 5309 | ToType = CurToType.getUnqualifiedType(); |
| 5310 | else if (HasUniqueTargetType && |
| 5311 | (CurToType.getUnqualifiedType() != ToType)) |
| 5312 | HasUniqueTargetType = false; |
| 5313 | } |
| 5314 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5315 | } |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5316 | } |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5317 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5318 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5319 | if (getLangOpts().CPlusPlus1y) { |
| 5320 | // C++1y [conv]p6: |
| 5321 | // ... An expression e of class type E appearing in such a context |
| 5322 | // is said to be contextually implicitly converted to a specified |
| 5323 | // type T and is well-formed if and only if e can be implicitly |
| 5324 | // 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] | 5325 | // for conversion functions whose return type is cv T or reference to |
| 5326 | // 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] | 5327 | // exactly one such T. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5328 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5329 | // If no unique T is found: |
| 5330 | if (ToType.isNull()) { |
| 5331 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5332 | HadMultipleCandidates, |
| 5333 | ExplicitConversions)) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5334 | return ExprError(); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5335 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5336 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5337 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5338 | // If more than one unique Ts are found: |
| 5339 | if (!HasUniqueTargetType) |
| 5340 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5341 | ViableConversions); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5342 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5343 | // If one unique T is found: |
| 5344 | // First, build a candidate set from the previously recorded |
| 5345 | // potentially viable conversions. |
| 5346 | OverloadCandidateSet CandidateSet(Loc); |
| 5347 | collectViableConversionCandidates(*this, From, ToType, ViableConversions, |
| 5348 | CandidateSet); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5349 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5350 | // Then, perform overload resolution over the candidate set. |
| 5351 | OverloadCandidateSet::iterator Best; |
| 5352 | switch (CandidateSet.BestViableFunction(*this, Loc, Best)) { |
| 5353 | case OR_Success: { |
| 5354 | // Apply this conversion. |
| 5355 | DeclAccessPair Found = |
| 5356 | DeclAccessPair::make(Best->Function, Best->FoundDecl.getAccess()); |
| 5357 | if (recordConversion(*this, Loc, From, Converter, T, |
| 5358 | HadMultipleCandidates, Found)) |
| 5359 | return ExprError(); |
| 5360 | break; |
| 5361 | } |
| 5362 | case OR_Ambiguous: |
| 5363 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5364 | ViableConversions); |
| 5365 | case OR_No_Viable_Function: |
| 5366 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5367 | HadMultipleCandidates, |
| 5368 | ExplicitConversions)) |
| 5369 | return ExprError(); |
| 5370 | // fall through 'OR_Deleted' case. |
| 5371 | case OR_Deleted: |
| 5372 | // We'll complain below about a non-integral condition type. |
| 5373 | break; |
| 5374 | } |
| 5375 | } else { |
| 5376 | switch (ViableConversions.size()) { |
| 5377 | case 0: { |
| 5378 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5379 | HadMultipleCandidates, |
| 5380 | ExplicitConversions)) |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5381 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5382 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5383 | // We'll complain below about a non-integral condition type. |
| 5384 | break; |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5385 | } |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5386 | case 1: { |
| 5387 | // Apply this conversion. |
| 5388 | DeclAccessPair Found = ViableConversions[0]; |
| 5389 | if (recordConversion(*this, Loc, From, Converter, T, |
| 5390 | HadMultipleCandidates, Found)) |
| 5391 | return ExprError(); |
| 5392 | break; |
| 5393 | } |
| 5394 | default: |
| 5395 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5396 | ViableConversions); |
| 5397 | } |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5398 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5399 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5400 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5401 | } |
| 5402 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5403 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 5404 | /// candidate functions, using the given function call arguments. If |
| 5405 | /// @p SuppressUserConversions, then don't allow user-defined |
| 5406 | /// conversions via constructors or conversion operators. |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5407 | /// |
James Dennett | 699c904 | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 5408 | /// \param PartialOverloading true if we are performing "partial" overloading |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5409 | /// based on an incomplete set of function arguments. This feature is used by |
| 5410 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5411 | void |
| 5412 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5413 | DeclAccessPair FoundDecl, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5414 | ArrayRef<Expr *> Args, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 5415 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 5416 | bool SuppressUserConversions, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5417 | bool PartialOverloading, |
| 5418 | bool AllowExplicit) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5419 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5420 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5421 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5422 | assert(!Function->getDescribedFunctionTemplate() && |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5423 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5424 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5425 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5426 | if (!isa<CXXConstructorDecl>(Method)) { |
| 5427 | // If we get here, it's because we're calling a member function |
| 5428 | // that is named without a member access expression (e.g., |
| 5429 | // "this->f") that was either written explicitly or created |
| 5430 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5431 | // function, e.g., X::f(). We use an empty type for the implied |
| 5432 | // object argument (C++ [over.call.func]p3), and the acting context |
| 5433 | // is irrelevant. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5434 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5435 | QualType(), Expr::Classification::makeSimpleLValue(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5436 | Args, CandidateSet, SuppressUserConversions); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5437 | return; |
| 5438 | } |
| 5439 | // We treat a constructor like a non-member function, since its object |
| 5440 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5441 | } |
| 5442 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 5443 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5444 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5445 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5446 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5447 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5448 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5449 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 5450 | // C++ [class.copy]p3: |
| 5451 | // A member function template is never instantiated to perform the copy |
| 5452 | // of a class object to an object of its class type. |
| 5453 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5454 | if (Args.size() == 1 && |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 5455 | Constructor->isSpecializationCopyingObject() && |
Douglas Gregor | 1211606 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 5456 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 5457 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5458 | return; |
| 5459 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5460 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5461 | // Add this candidate |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5462 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5463 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5464 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5465 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5466 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5467 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5468 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5469 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5470 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5471 | |
| 5472 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5473 | // parameters is viable only if it has an ellipsis in its parameter |
| 5474 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5475 | if ((Args.size() + (PartialOverloading && Args.size())) > NumArgsInProto && |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 5476 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5477 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5478 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5479 | return; |
| 5480 | } |
| 5481 | |
| 5482 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5483 | // is viable only if the (m+1)st parameter has a default argument |
| 5484 | // (8.3.6). For the purposes of overload resolution, the |
| 5485 | // parameter list is truncated on the right, so that there are |
| 5486 | // exactly m parameters. |
| 5487 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5488 | if (Args.size() < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5489 | // Not enough arguments. |
| 5490 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5491 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5492 | return; |
| 5493 | } |
| 5494 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5495 | // (CUDA B.1): Check for invalid calls between targets. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5496 | if (getLangOpts().CUDA) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5497 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
| 5498 | if (CheckCUDATarget(Caller, Function)) { |
| 5499 | Candidate.Viable = false; |
| 5500 | Candidate.FailureKind = ovl_fail_bad_target; |
| 5501 | return; |
| 5502 | } |
| 5503 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5504 | // Determine the implicit conversion sequences for each of the |
| 5505 | // arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5506 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5507 | if (ArgIdx < NumArgsInProto) { |
| 5508 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5509 | // exist for each argument an implicit conversion sequence |
| 5510 | // (13.3.3.1) that converts that argument to the corresponding |
| 5511 | // parameter of F. |
| 5512 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5513 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5514 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5515 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5516 | /*InOverloadResolution=*/true, |
| 5517 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5518 | getLangOpts().ObjCAutoRefCount, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5519 | AllowExplicit); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5520 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 5521 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5522 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5523 | break; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5524 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5525 | } else { |
| 5526 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5527 | // argument for which there is no corresponding parameter is |
| 5528 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5529 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5530 | } |
| 5531 | } |
| 5532 | } |
| 5533 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5534 | /// \brief Add all of the function declarations in the given function set to |
| 5535 | /// the overload canddiate set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5536 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5537 | ArrayRef<Expr *> Args, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5538 | OverloadCandidateSet& CandidateSet, |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5539 | bool SuppressUserConversions, |
| 5540 | TemplateArgumentListInfo *ExplicitTemplateArgs) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5541 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5542 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 5543 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5544 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5545 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5546 | cast<CXXMethodDecl>(FD)->getParent(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5547 | Args[0]->getType(), Args[0]->Classify(Context), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5548 | Args.slice(1), CandidateSet, |
| 5549 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5550 | else |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5551 | AddOverloadCandidate(FD, F.getPair(), Args, CandidateSet, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5552 | SuppressUserConversions); |
| 5553 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5554 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5555 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 5556 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5557 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5558 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5559 | ExplicitTemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5560 | Args[0]->getType(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5561 | Args[0]->Classify(Context), Args.slice(1), |
| 5562 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5563 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5564 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5565 | ExplicitTemplateArgs, Args, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5566 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5567 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5568 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5569 | } |
| 5570 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5571 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 5572 | /// method) as a method candidate to the given overload set. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5573 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5574 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5575 | Expr::Classification ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5576 | ArrayRef<Expr *> Args, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5577 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5578 | bool SuppressUserConversions) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5579 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5580 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5581 | |
| 5582 | if (isa<UsingShadowDecl>(Decl)) |
| 5583 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5584 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5585 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 5586 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 5587 | "Expected a member function template"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5588 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 5589 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5590 | ObjectType, ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5591 | Args, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5592 | SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5593 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5594 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5595 | ObjectType, ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5596 | Args, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5597 | CandidateSet, SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5598 | } |
| 5599 | } |
| 5600 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5601 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 5602 | /// of candidate functions, using the given function call arguments |
| 5603 | /// and the object argument (@c Object). For example, in a call |
| 5604 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 5605 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 5606 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5607 | /// operators. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5608 | void |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5609 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5610 | CXXRecordDecl *ActingContext, QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5611 | Expr::Classification ObjectClassification, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5612 | ArrayRef<Expr *> Args, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5613 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5614 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5615 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5616 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5617 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5618 | assert(!isa<CXXConstructorDecl>(Method) && |
| 5619 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5620 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5621 | if (!CandidateSet.isNewCandidate(Method)) |
| 5622 | return; |
| 5623 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5624 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5625 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5626 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5627 | // Add this candidate |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5628 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5629 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5630 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5631 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5632 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5633 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5634 | |
| 5635 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5636 | |
| 5637 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5638 | // parameters is viable only if it has an ellipsis in its parameter |
| 5639 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5640 | if (Args.size() > NumArgsInProto && !Proto->isVariadic()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5641 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5642 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5643 | return; |
| 5644 | } |
| 5645 | |
| 5646 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5647 | // is viable only if the (m+1)st parameter has a default argument |
| 5648 | // (8.3.6). For the purposes of overload resolution, the |
| 5649 | // parameter list is truncated on the right, so that there are |
| 5650 | // exactly m parameters. |
| 5651 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5652 | if (Args.size() < MinRequiredArgs) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5653 | // Not enough arguments. |
| 5654 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5655 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5656 | return; |
| 5657 | } |
| 5658 | |
| 5659 | Candidate.Viable = true; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5660 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5661 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5662 | // The implicit object argument is ignored. |
| 5663 | Candidate.IgnoreObjectArgument = true; |
| 5664 | else { |
| 5665 | // Determine the implicit conversion sequence for the object |
| 5666 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5667 | Candidate.Conversions[0] |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5668 | = TryObjectArgumentInitialization(*this, ObjectType, ObjectClassification, |
| 5669 | Method, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5670 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5671 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5672 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5673 | return; |
| 5674 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5675 | } |
| 5676 | |
| 5677 | // Determine the implicit conversion sequences for each of the |
| 5678 | // arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5679 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5680 | if (ArgIdx < NumArgsInProto) { |
| 5681 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5682 | // exist for each argument an implicit conversion sequence |
| 5683 | // (13.3.3.1) that converts that argument to the corresponding |
| 5684 | // parameter of F. |
| 5685 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5686 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5687 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5688 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5689 | /*InOverloadResolution=*/true, |
| 5690 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5691 | getLangOpts().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5692 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5693 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5694 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5695 | break; |
| 5696 | } |
| 5697 | } else { |
| 5698 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5699 | // argument for which there is no corresponding parameter is |
| 5700 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5701 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5702 | } |
| 5703 | } |
| 5704 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5705 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5706 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 5707 | /// set, using template argument deduction to produce an appropriate member |
| 5708 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5709 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5710 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5711 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5712 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5713 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5714 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5715 | Expr::Classification ObjectClassification, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5716 | ArrayRef<Expr *> Args, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5717 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5718 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5719 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 5720 | return; |
| 5721 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5722 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5723 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5724 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5725 | // 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] | 5726 | // candidate functions in the usual way.113) A given name can refer to one |
| 5727 | // or more function templates and also to a set of overloaded non-template |
| 5728 | // functions. In such a case, the candidate functions generated from each |
| 5729 | // function template are combined with the set of non-template candidate |
| 5730 | // functions. |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5731 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5732 | FunctionDecl *Specialization = 0; |
| 5733 | if (TemplateDeductionResult Result |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5734 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, Args, |
| 5735 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5736 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5737 | Candidate.FoundDecl = FoundDecl; |
| 5738 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 5739 | Candidate.Viable = false; |
| 5740 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 5741 | Candidate.IsSurrogate = false; |
| 5742 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5743 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5744 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5745 | Info); |
| 5746 | return; |
| 5747 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5748 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5749 | // Add the function template specialization produced by template argument |
| 5750 | // deduction as a candidate. |
| 5751 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5752 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5753 | "Specialization is not a member function?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5754 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5755 | ActingContext, ObjectType, ObjectClassification, Args, |
| 5756 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5757 | } |
| 5758 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5759 | /// \brief Add a C++ function template specialization as a candidate |
| 5760 | /// in the candidate set, using template argument deduction to produce |
| 5761 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5762 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5763 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5764 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5765 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5766 | ArrayRef<Expr *> Args, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5767 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5768 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5769 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 5770 | return; |
| 5771 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5772 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5773 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5774 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5775 | // 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] | 5776 | // candidate functions in the usual way.113) A given name can refer to one |
| 5777 | // or more function templates and also to a set of overloaded non-template |
| 5778 | // functions. In such a case, the candidate functions generated from each |
| 5779 | // function template are combined with the set of non-template candidate |
| 5780 | // functions. |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5781 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5782 | FunctionDecl *Specialization = 0; |
| 5783 | if (TemplateDeductionResult Result |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5784 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, Args, |
| 5785 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5786 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5787 | Candidate.FoundDecl = FoundDecl; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5788 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 5789 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5790 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5791 | Candidate.IsSurrogate = false; |
| 5792 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5793 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5794 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5795 | Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5796 | return; |
| 5797 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5798 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5799 | // Add the function template specialization produced by template argument |
| 5800 | // deduction as a candidate. |
| 5801 | assert(Specialization && "Missing function template specialization?"); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5802 | AddOverloadCandidate(Specialization, FoundDecl, Args, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5803 | SuppressUserConversions); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5804 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5805 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5806 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5807 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5808 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5809 | /// 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] | 5810 | /// (which may or may not be the same type as the type that the |
| 5811 | /// conversion function produces). |
| 5812 | void |
| 5813 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5814 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5815 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5816 | Expr *From, QualType ToType, |
| 5817 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5818 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 5819 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5820 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5821 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 5822 | return; |
| 5823 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 5824 | // If the conversion function has an undeduced return type, trigger its |
| 5825 | // deduction now. |
| 5826 | if (getLangOpts().CPlusPlus1y && ConvType->isUndeducedType()) { |
| 5827 | if (DeduceReturnType(Conversion, From->getExprLoc())) |
| 5828 | return; |
| 5829 | ConvType = Conversion->getConversionType().getNonReferenceType(); |
| 5830 | } |
| 5831 | |
Richard Smith | b390e49 | 2013-09-21 21:55:46 +0000 | [diff] [blame^] | 5832 | // Per C++ [over.match.conv]p1, [over.match.ref]p1, an explicit conversion |
| 5833 | // operator is only a candidate if its return type is the target type or |
| 5834 | // can be converted to the target type with a qualification conversion. |
| 5835 | bool ObjCLifetimeConversion; |
| 5836 | QualType ToNonRefType = ToType.getNonReferenceType(); |
| 5837 | if (Conversion->isExplicit() && |
| 5838 | !Context.hasSameUnqualifiedType(ConvType, ToNonRefType) && |
| 5839 | !IsQualificationConversion(ConvType, ToNonRefType, /*CStyle*/false, |
| 5840 | ObjCLifetimeConversion)) |
| 5841 | return; |
| 5842 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5843 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5844 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5845 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5846 | // Add this candidate |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5847 | OverloadCandidate &Candidate = CandidateSet.addCandidate(1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5848 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5849 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5850 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5851 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5852 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5853 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 5854 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5855 | Candidate.Viable = true; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5856 | Candidate.ExplicitCallArguments = 1; |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5857 | |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 5858 | // C++ [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5859 | // For conversion functions, the function is considered to be a member of |
| 5860 | // the class of the implicit implied object argument for the purpose of |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 5861 | // defining the type of the implicit object parameter. |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5862 | // |
| 5863 | // Determine the implicit conversion sequence for the implicit |
| 5864 | // object parameter. |
| 5865 | QualType ImplicitParamType = From->getType(); |
| 5866 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
| 5867 | ImplicitParamType = FromPtrType->getPointeeType(); |
| 5868 | CXXRecordDecl *ConversionContext |
| 5869 | = cast<CXXRecordDecl>(ImplicitParamType->getAs<RecordType>()->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5870 | |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5871 | Candidate.Conversions[0] |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5872 | = TryObjectArgumentInitialization(*this, From->getType(), |
| 5873 | From->Classify(Context), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5874 | Conversion, ConversionContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5875 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5876 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5877 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5878 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5879 | return; |
| 5880 | } |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5881 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5882 | // 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] | 5883 | // derived to base as such conversions are given Conversion Rank. They only |
| 5884 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 5885 | QualType FromCanon |
| 5886 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 5887 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 5888 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 5889 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5890 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 5891 | return; |
| 5892 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5893 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5894 | // To determine what the conversion from the result of calling the |
| 5895 | // conversion function to the type we're eventually trying to |
| 5896 | // convert to (ToType), we need to synthesize a call to the |
| 5897 | // conversion function and attempt copy initialization from it. This |
| 5898 | // makes sure that we get the right semantics with respect to |
| 5899 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 5900 | // call on the stack and we don't need its arguments to be |
| 5901 | // well-formed. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5902 | DeclRefExpr ConversionRef(Conversion, false, Conversion->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5903 | VK_LValue, From->getLocStart()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5904 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 5905 | Context.getPointerType(Conversion->getType()), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5906 | CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5907 | &ConversionRef, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5908 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5909 | QualType ConversionType = Conversion->getConversionType(); |
| 5910 | if (RequireCompleteType(From->getLocStart(), ConversionType, 0)) { |
Douglas Gregor | 7d14d38 | 2010-11-13 19:36:57 +0000 | [diff] [blame] | 5911 | Candidate.Viable = false; |
| 5912 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 5913 | return; |
| 5914 | } |
| 5915 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5916 | ExprValueKind VK = Expr::getValueKindForType(ConversionType); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5917 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5918 | // 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] | 5919 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 5920 | // allocator). |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5921 | QualType CallResultType = ConversionType.getNonLValueExprType(Context); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 5922 | CallExpr Call(Context, &ConversionFn, None, CallResultType, VK, |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 5923 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5924 | ImplicitConversionSequence ICS = |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5925 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 5926 | /*SuppressUserConversions=*/true, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5927 | /*InOverloadResolution=*/false, |
| 5928 | /*AllowObjCWritebackConversion=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5929 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5930 | switch (ICS.getKind()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5931 | case ImplicitConversionSequence::StandardConversion: |
| 5932 | Candidate.FinalConversion = ICS.Standard; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5933 | |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5934 | // C++ [over.ics.user]p3: |
| 5935 | // If the user-defined conversion is specified by a specialization of a |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5936 | // conversion function template, the second standard conversion sequence |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5937 | // shall have exact match rank. |
| 5938 | if (Conversion->getPrimaryTemplate() && |
| 5939 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 5940 | Candidate.Viable = false; |
| 5941 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 5942 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5943 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 5944 | // C++0x [dcl.init.ref]p5: |
| 5945 | // In the second case, if the reference is an rvalue reference and |
| 5946 | // the second standard conversion sequence of the user-defined |
| 5947 | // conversion sequence includes an lvalue-to-rvalue conversion, the |
| 5948 | // program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5949 | if (ToType->isRValueReferenceType() && |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 5950 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 5951 | Candidate.Viable = false; |
| 5952 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 5953 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5954 | break; |
| 5955 | |
| 5956 | case ImplicitConversionSequence::BadConversion: |
| 5957 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5958 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5959 | break; |
| 5960 | |
| 5961 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5962 | llvm_unreachable( |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5963 | "Can only end up with a standard conversion sequence or failure"); |
| 5964 | } |
| 5965 | } |
| 5966 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5967 | /// \brief Adds a conversion function template specialization |
| 5968 | /// candidate to the overload set, using template argument deduction |
| 5969 | /// to deduce the template arguments of the conversion function |
| 5970 | /// template from the type that we are converting to (C++ |
| 5971 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5972 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5973 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5974 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5975 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5976 | Expr *From, QualType ToType, |
| 5977 | OverloadCandidateSet &CandidateSet) { |
| 5978 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 5979 | "Only conversion function templates permitted here"); |
| 5980 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5981 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 5982 | return; |
| 5983 | |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5984 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5985 | CXXConversionDecl *Specialization = 0; |
| 5986 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5987 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5988 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5989 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5990 | Candidate.FoundDecl = FoundDecl; |
| 5991 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 5992 | Candidate.Viable = false; |
| 5993 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 5994 | Candidate.IsSurrogate = false; |
| 5995 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5996 | Candidate.ExplicitCallArguments = 1; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5997 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5998 | Info); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5999 | return; |
| 6000 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6001 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6002 | // Add the conversion function template specialization produced by |
| 6003 | // template argument deduction as a candidate. |
| 6004 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6005 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 6006 | CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6007 | } |
| 6008 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6009 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 6010 | /// converts the given @c Object to a function pointer via the |
| 6011 | /// conversion function @c Conversion, and then attempts to call it |
| 6012 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 6013 | /// the type of function that we'll eventually be calling. |
| 6014 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6015 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6016 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6017 | const FunctionProtoType *Proto, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6018 | Expr *Object, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6019 | ArrayRef<Expr *> Args, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6020 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6021 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 6022 | return; |
| 6023 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6024 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6025 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6026 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6027 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6028 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6029 | Candidate.Function = 0; |
| 6030 | Candidate.Surrogate = Conversion; |
| 6031 | Candidate.Viable = true; |
| 6032 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6033 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6034 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6035 | |
| 6036 | // Determine the implicit conversion sequence for the implicit |
| 6037 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6038 | ImplicitConversionSequence ObjectInit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6039 | = TryObjectArgumentInitialization(*this, Object->getType(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6040 | Object->Classify(Context), |
| 6041 | Conversion, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6042 | if (ObjectInit.isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6043 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6044 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6045 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6046 | return; |
| 6047 | } |
| 6048 | |
| 6049 | // The first conversion is actually a user-defined conversion whose |
| 6050 | // first conversion is ObjectInit's standard conversion (which is |
| 6051 | // effectively a reference binding). Record it as such. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6052 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6053 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 6054 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6055 | Candidate.Conversions[0].UserDefined.HadMultipleCandidates = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6056 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 6057 | Candidate.Conversions[0].UserDefined.FoundConversionFunction = FoundDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6058 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6059 | = Candidate.Conversions[0].UserDefined.Before; |
| 6060 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 6061 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6062 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6063 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 6064 | |
| 6065 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 6066 | // parameters is viable only if it has an ellipsis in its parameter |
| 6067 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6068 | if (Args.size() > NumArgsInProto && !Proto->isVariadic()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6069 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6070 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6071 | return; |
| 6072 | } |
| 6073 | |
| 6074 | // Function types don't have any default arguments, so just check if |
| 6075 | // we have enough arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6076 | if (Args.size() < NumArgsInProto) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6077 | // Not enough arguments. |
| 6078 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6079 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6080 | return; |
| 6081 | } |
| 6082 | |
| 6083 | // Determine the implicit conversion sequences for each of the |
| 6084 | // arguments. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6085 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6086 | if (ArgIdx < NumArgsInProto) { |
| 6087 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 6088 | // exist for each argument an implicit conversion sequence |
| 6089 | // (13.3.3.1) that converts that argument to the corresponding |
| 6090 | // parameter of F. |
| 6091 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6092 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6093 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6094 | /*SuppressUserConversions=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6095 | /*InOverloadResolution=*/false, |
| 6096 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6097 | getLangOpts().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6098 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6099 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6100 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6101 | break; |
| 6102 | } |
| 6103 | } else { |
| 6104 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 6105 | // argument for which there is no corresponding parameter is |
| 6106 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6107 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6108 | } |
| 6109 | } |
| 6110 | } |
| 6111 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6112 | /// \brief Add overload candidates for overloaded operators that are |
| 6113 | /// member functions. |
| 6114 | /// |
| 6115 | /// Add the overloaded operator candidates that are member functions |
| 6116 | /// for the operator Op that was used in an operator expression such |
| 6117 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 6118 | /// CandidateSet will store the added overload candidates. (C++ |
| 6119 | /// [over.match.oper]). |
| 6120 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 6121 | SourceLocation OpLoc, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6122 | ArrayRef<Expr *> Args, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6123 | OverloadCandidateSet& CandidateSet, |
| 6124 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6125 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 6126 | |
| 6127 | // C++ [over.match.oper]p3: |
| 6128 | // For a unary operator @ with an operand of a type whose |
| 6129 | // cv-unqualified version is T1, and for a binary operator @ with |
| 6130 | // a left operand of a type whose cv-unqualified version is T1 and |
| 6131 | // a right operand of a type whose cv-unqualified version is T2, |
| 6132 | // three sets of candidate functions, designated member |
| 6133 | // candidates, non-member candidates and built-in candidates, are |
| 6134 | // constructed as follows: |
| 6135 | QualType T1 = Args[0]->getType(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6136 | |
Richard Smith | e410be9 | 2013-04-20 12:41:22 +0000 | [diff] [blame] | 6137 | // -- If T1 is a complete class type or a class currently being |
| 6138 | // defined, the set of member candidates is the result of the |
| 6139 | // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, |
| 6140 | // the set of member candidates is empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6141 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Richard Smith | e410be9 | 2013-04-20 12:41:22 +0000 | [diff] [blame] | 6142 | // Complete the type if it can be completed. |
| 6143 | RequireCompleteType(OpLoc, T1, 0); |
| 6144 | // If the type is neither complete nor being defined, bail out now. |
| 6145 | if (!T1Rec->getDecl()->getDefinition()) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 6146 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6147 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6148 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 6149 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 6150 | Operators.suppressDiagnostics(); |
| 6151 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6152 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 6153 | OperEnd = Operators.end(); |
| 6154 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6155 | ++Oper) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6156 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 6157 | Args[0]->Classify(Context), |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6158 | Args.slice(1), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6159 | CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6160 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6161 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6162 | } |
| 6163 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6164 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 6165 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 6166 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6167 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 6168 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6169 | /// operator. NumContextualBoolArguments is the number of arguments |
| 6170 | /// (at the beginning of the argument list) that will be contextually |
| 6171 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6172 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6173 | ArrayRef<Expr *> Args, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6174 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6175 | bool IsAssignmentOperator, |
| 6176 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6177 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6178 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6179 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6180 | // Add this candidate |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6181 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6182 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6183 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 6184 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6185 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6186 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6187 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6188 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 6189 | |
| 6190 | // Determine the implicit conversion sequences for each of the |
| 6191 | // arguments. |
| 6192 | Candidate.Viable = true; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6193 | Candidate.ExplicitCallArguments = Args.size(); |
| 6194 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6195 | // C++ [over.match.oper]p4: |
| 6196 | // For the built-in assignment operators, conversions of the |
| 6197 | // left operand are restricted as follows: |
| 6198 | // -- no temporaries are introduced to hold the left operand, and |
| 6199 | // -- no user-defined conversions are applied to the left |
| 6200 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6201 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6202 | // |
| 6203 | // We block these conversions by turning off user-defined |
| 6204 | // conversions, since that is the only way that initialization of |
| 6205 | // a reference to a non-class type can occur from something that |
| 6206 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6207 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6208 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6209 | "Contextual conversion to bool requires bool type"); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6210 | Candidate.Conversions[ArgIdx] |
| 6211 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6212 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6213 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6214 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6215 | ArgIdx == 0 && IsAssignmentOperator, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6216 | /*InOverloadResolution=*/false, |
| 6217 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6218 | getLangOpts().ObjCAutoRefCount); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6219 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6220 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6221 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6222 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6223 | break; |
| 6224 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6225 | } |
| 6226 | } |
| 6227 | |
Craig Topper | fe09f3f | 2013-07-01 06:29:40 +0000 | [diff] [blame] | 6228 | namespace { |
| 6229 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6230 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 6231 | /// candidate operator functions for built-in operators (C++ |
| 6232 | /// [over.built]). The types are separated into pointer types and |
| 6233 | /// enumeration types. |
| 6234 | class BuiltinCandidateTypeSet { |
| 6235 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6236 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6237 | |
| 6238 | /// PointerTypes - The set of pointer types that will be used in the |
| 6239 | /// built-in candidates. |
| 6240 | TypeSet PointerTypes; |
| 6241 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6242 | /// MemberPointerTypes - The set of member pointer types that will be |
| 6243 | /// used in the built-in candidates. |
| 6244 | TypeSet MemberPointerTypes; |
| 6245 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6246 | /// EnumerationTypes - The set of enumeration types that will be |
| 6247 | /// used in the built-in candidates. |
| 6248 | TypeSet EnumerationTypes; |
| 6249 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6250 | /// \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] | 6251 | /// candidates. |
| 6252 | TypeSet VectorTypes; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6253 | |
| 6254 | /// \brief A flag indicating non-record types are viable candidates |
| 6255 | bool HasNonRecordTypes; |
| 6256 | |
| 6257 | /// \brief A flag indicating whether either arithmetic or enumeration types |
| 6258 | /// were present in the candidate set. |
| 6259 | bool HasArithmeticOrEnumeralTypes; |
| 6260 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6261 | /// \brief A flag indicating whether the nullptr type was present in the |
| 6262 | /// candidate set. |
| 6263 | bool HasNullPtrType; |
| 6264 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 6265 | /// Sema - The semantic analysis instance where we are building the |
| 6266 | /// candidate type set. |
| 6267 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6268 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6269 | /// Context - The AST context in which we will build the type sets. |
| 6270 | ASTContext &Context; |
| 6271 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6272 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6273 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6274 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6275 | |
| 6276 | public: |
| 6277 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6278 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6279 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6280 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6281 | : HasNonRecordTypes(false), |
| 6282 | HasArithmeticOrEnumeralTypes(false), |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6283 | HasNullPtrType(false), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6284 | SemaRef(SemaRef), |
| 6285 | Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6286 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6287 | void AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6288 | SourceLocation Loc, |
| 6289 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6290 | bool AllowExplicitConversions, |
| 6291 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6292 | |
| 6293 | /// pointer_begin - First pointer type found; |
| 6294 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 6295 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6296 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6297 | iterator pointer_end() { return PointerTypes.end(); } |
| 6298 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6299 | /// member_pointer_begin - First member pointer type found; |
| 6300 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 6301 | |
| 6302 | /// member_pointer_end - Past the last member pointer type found; |
| 6303 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 6304 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6305 | /// enumeration_begin - First enumeration type found; |
| 6306 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 6307 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6308 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6309 | iterator enumeration_end() { return EnumerationTypes.end(); } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6310 | |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6311 | iterator vector_begin() { return VectorTypes.begin(); } |
| 6312 | iterator vector_end() { return VectorTypes.end(); } |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6313 | |
| 6314 | bool hasNonRecordTypes() { return HasNonRecordTypes; } |
| 6315 | bool hasArithmeticOrEnumeralTypes() { return HasArithmeticOrEnumeralTypes; } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6316 | bool hasNullPtrType() const { return HasNullPtrType; } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6317 | }; |
| 6318 | |
Craig Topper | fe09f3f | 2013-07-01 06:29:40 +0000 | [diff] [blame] | 6319 | } // end anonymous namespace |
| 6320 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6321 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6322 | /// the set of pointer types along with any more-qualified variants of |
| 6323 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6324 | /// will add "int const *", "int const volatile *", "int const |
| 6325 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6326 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6327 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6328 | /// |
| 6329 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6330 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6331 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6332 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6333 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6334 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6335 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6336 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6337 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6338 | QualType PointeeTy; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6339 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6340 | bool buildObjCPtr = false; |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6341 | if (!PointerTy) { |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6342 | const ObjCObjectPointerType *PTy = Ty->castAs<ObjCObjectPointerType>(); |
| 6343 | PointeeTy = PTy->getPointeeType(); |
| 6344 | buildObjCPtr = true; |
| 6345 | } else { |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6346 | PointeeTy = PointerTy->getPointeeType(); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6347 | } |
| 6348 | |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6349 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6350 | // (the qualifier would sink to the element type), and for another, the |
| 6351 | // only overload situation where it matters is subscript or pointer +- int, |
| 6352 | // and those shouldn't have qualifier variants anyway. |
| 6353 | if (PointeeTy->isArrayType()) |
| 6354 | return true; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6355 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6356 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6357 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 6358 | bool hasRestrict = VisibleQuals.hasRestrict(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6359 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6360 | // Iterate through all strict supersets of BaseCVR. |
| 6361 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6362 | if ((CVR | BaseCVR) != CVR) continue; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6363 | // Skip over volatile if no volatile found anywhere in the types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6364 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6365 | |
| 6366 | // Skip over restrict if no restrict found anywhere in the types, or if |
| 6367 | // the type cannot be restrict-qualified. |
| 6368 | if ((CVR & Qualifiers::Restrict) && |
| 6369 | (!hasRestrict || |
| 6370 | (!(PointeeTy->isAnyPointerType() || PointeeTy->isReferenceType())))) |
| 6371 | continue; |
| 6372 | |
| 6373 | // Build qualified pointee type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6374 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6375 | |
| 6376 | // Build qualified pointer type. |
| 6377 | QualType QPointerTy; |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6378 | if (!buildObjCPtr) |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6379 | QPointerTy = Context.getPointerType(QPointeeTy); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6380 | else |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6381 | QPointerTy = Context.getObjCObjectPointerType(QPointeeTy); |
| 6382 | |
| 6383 | // Insert qualified pointer type. |
| 6384 | PointerTypes.insert(QPointerTy); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6385 | } |
| 6386 | |
| 6387 | return true; |
| 6388 | } |
| 6389 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6390 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 6391 | /// to the set of pointer types along with any more-qualified variants of |
| 6392 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6393 | /// will add "int const *", "int const volatile *", "int const |
| 6394 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6395 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6396 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6397 | /// |
| 6398 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6399 | bool |
| 6400 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 6401 | QualType Ty) { |
| 6402 | // Insert this type. |
| 6403 | if (!MemberPointerTypes.insert(Ty)) |
| 6404 | return false; |
| 6405 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6406 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 6407 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6408 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6409 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6410 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6411 | // (the qualifier would sink to the element type), and for another, the |
| 6412 | // only overload situation where it matters is subscript or pointer +- int, |
| 6413 | // and those shouldn't have qualifier variants anyway. |
| 6414 | if (PointeeTy->isArrayType()) |
| 6415 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6416 | const Type *ClassTy = PointerTy->getClass(); |
| 6417 | |
| 6418 | // Iterate through all strict supersets of the pointee type's CVR |
| 6419 | // qualifiers. |
| 6420 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 6421 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6422 | if ((CVR | BaseCVR) != CVR) continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6423 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6424 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6425 | MemberPointerTypes.insert( |
| 6426 | Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6427 | } |
| 6428 | |
| 6429 | return true; |
| 6430 | } |
| 6431 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6432 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 6433 | /// 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] | 6434 | /// primarily interested in pointer types and enumeration types. We also |
| 6435 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6436 | /// AllowUserConversions is true if we should look at the conversion |
| 6437 | /// functions of a class type, and AllowExplicitConversions if we |
| 6438 | /// should also include the explicit conversion functions of a class |
| 6439 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6440 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6441 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6442 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6443 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6444 | bool AllowExplicitConversions, |
| 6445 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6446 | // Only deal with canonical types. |
| 6447 | Ty = Context.getCanonicalType(Ty); |
| 6448 | |
| 6449 | // Look through reference types; they aren't part of the type of an |
| 6450 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6451 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6452 | Ty = RefTy->getPointeeType(); |
| 6453 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 6454 | // If we're dealing with an array type, decay to the pointer. |
| 6455 | if (Ty->isArrayType()) |
| 6456 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 6457 | |
| 6458 | // Otherwise, we don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 6459 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6460 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6461 | // Flag if we ever add a non-record type. |
| 6462 | const RecordType *TyRec = Ty->getAs<RecordType>(); |
| 6463 | HasNonRecordTypes = HasNonRecordTypes || !TyRec; |
| 6464 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6465 | // Flag if we encounter an arithmetic type. |
| 6466 | HasArithmeticOrEnumeralTypes = |
| 6467 | HasArithmeticOrEnumeralTypes || Ty->isArithmeticType(); |
| 6468 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6469 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
| 6470 | PointerTypes.insert(Ty); |
| 6471 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6472 | // Insert our type, and its more-qualified variants, into the set |
| 6473 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6474 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6475 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6476 | } else if (Ty->isMemberPointerType()) { |
| 6477 | // Member pointers are far easier, since the pointee can't be converted. |
| 6478 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 6479 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6480 | } else if (Ty->isEnumeralType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6481 | HasArithmeticOrEnumeralTypes = true; |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6482 | EnumerationTypes.insert(Ty); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6483 | } else if (Ty->isVectorType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6484 | // We treat vector types as arithmetic types in many contexts as an |
| 6485 | // extension. |
| 6486 | HasArithmeticOrEnumeralTypes = true; |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6487 | VectorTypes.insert(Ty); |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6488 | } else if (Ty->isNullPtrType()) { |
| 6489 | HasNullPtrType = true; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6490 | } else if (AllowUserConversions && TyRec) { |
| 6491 | // No conversion functions in incomplete types. |
| 6492 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) |
| 6493 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6494 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6495 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6496 | std::pair<CXXRecordDecl::conversion_iterator, |
| 6497 | CXXRecordDecl::conversion_iterator> |
| 6498 | Conversions = ClassDecl->getVisibleConversionFunctions(); |
| 6499 | for (CXXRecordDecl::conversion_iterator |
| 6500 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6501 | NamedDecl *D = I.getDecl(); |
| 6502 | if (isa<UsingShadowDecl>(D)) |
| 6503 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6504 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6505 | // Skip conversion function templates; they don't tell us anything |
| 6506 | // about which builtin types we can convert to. |
| 6507 | if (isa<FunctionTemplateDecl>(D)) |
| 6508 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6509 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6510 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
| 6511 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
| 6512 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
| 6513 | VisibleQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6514 | } |
| 6515 | } |
| 6516 | } |
| 6517 | } |
| 6518 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6519 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 6520 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 6521 | /// given type to the candidate set. |
| 6522 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 6523 | QualType T, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6524 | ArrayRef<Expr *> Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6525 | OverloadCandidateSet &CandidateSet) { |
| 6526 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6527 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6528 | // T& operator=(T&, T) |
| 6529 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 6530 | ParamTypes[1] = T; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6531 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6532 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6533 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6534 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 6535 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6536 | ParamTypes[0] |
| 6537 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6538 | ParamTypes[1] = T; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6539 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6540 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6541 | } |
| 6542 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6543 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 6544 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 6545 | /// 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] | 6546 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 6547 | Qualifiers VRQuals; |
| 6548 | const RecordType *TyRec; |
| 6549 | if (const MemberPointerType *RHSMPType = |
| 6550 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6551 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6552 | else |
| 6553 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 6554 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6555 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6556 | VRQuals.addVolatile(); |
| 6557 | VRQuals.addRestrict(); |
| 6558 | return VRQuals; |
| 6559 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6560 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6561 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 6562 | if (!ClassDecl->hasDefinition()) |
| 6563 | return VRQuals; |
| 6564 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6565 | std::pair<CXXRecordDecl::conversion_iterator, |
| 6566 | CXXRecordDecl::conversion_iterator> |
| 6567 | Conversions = ClassDecl->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6568 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6569 | for (CXXRecordDecl::conversion_iterator |
| 6570 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 6571 | NamedDecl *D = I.getDecl(); |
| 6572 | if (isa<UsingShadowDecl>(D)) |
| 6573 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6574 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6575 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 6576 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 6577 | CanTy = ResTypeRef->getPointeeType(); |
| 6578 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 6579 | // as see them. |
| 6580 | bool done = false; |
| 6581 | while (!done) { |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6582 | if (CanTy.isRestrictQualified()) |
| 6583 | VRQuals.addRestrict(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6584 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 6585 | CanTy = ResTypePtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6586 | else if (const MemberPointerType *ResTypeMPtr = |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6587 | CanTy->getAs<MemberPointerType>()) |
| 6588 | CanTy = ResTypeMPtr->getPointeeType(); |
| 6589 | else |
| 6590 | done = true; |
| 6591 | if (CanTy.isVolatileQualified()) |
| 6592 | VRQuals.addVolatile(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6593 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 6594 | return VRQuals; |
| 6595 | } |
| 6596 | } |
| 6597 | } |
| 6598 | return VRQuals; |
| 6599 | } |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6600 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6601 | namespace { |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6602 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6603 | /// \brief Helper class to manage the addition of builtin operator overload |
| 6604 | /// candidates. It provides shared state and utility methods used throughout |
| 6605 | /// the process, as well as a helper method to add each group of builtin |
| 6606 | /// operator overloads from the standard to a candidate set. |
| 6607 | class BuiltinOperatorOverloadBuilder { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6608 | // Common instance state available to all overload candidate addition methods. |
| 6609 | Sema &S; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6610 | ArrayRef<Expr *> Args; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6611 | Qualifiers VisibleTypeConversionsQuals; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6612 | bool HasArithmeticOrEnumeralCandidateType; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6613 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6614 | OverloadCandidateSet &CandidateSet; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6615 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6616 | // Define some constants used to index and iterate over the arithemetic types |
| 6617 | // provided via the getArithmeticType() method below. |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6618 | // The "promoted arithmetic types" are the arithmetic |
| 6619 | // types are that preserved by promotion (C++ [over.built]p2). |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6620 | static const unsigned FirstIntegralType = 3; |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6621 | static const unsigned LastIntegralType = 20; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6622 | static const unsigned FirstPromotedIntegralType = 3, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6623 | LastPromotedIntegralType = 11; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6624 | static const unsigned FirstPromotedArithmeticType = 0, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6625 | LastPromotedArithmeticType = 11; |
| 6626 | static const unsigned NumArithmeticTypes = 20; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6627 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6628 | /// \brief Get the canonical type for a given arithmetic type index. |
| 6629 | CanQualType getArithmeticType(unsigned index) { |
| 6630 | assert(index < NumArithmeticTypes); |
| 6631 | static CanQualType ASTContext::* const |
| 6632 | ArithmeticTypes[NumArithmeticTypes] = { |
| 6633 | // Start of promoted types. |
| 6634 | &ASTContext::FloatTy, |
| 6635 | &ASTContext::DoubleTy, |
| 6636 | &ASTContext::LongDoubleTy, |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6637 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6638 | // Start of integral types. |
| 6639 | &ASTContext::IntTy, |
| 6640 | &ASTContext::LongTy, |
| 6641 | &ASTContext::LongLongTy, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6642 | &ASTContext::Int128Ty, |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6643 | &ASTContext::UnsignedIntTy, |
| 6644 | &ASTContext::UnsignedLongTy, |
| 6645 | &ASTContext::UnsignedLongLongTy, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6646 | &ASTContext::UnsignedInt128Ty, |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6647 | // End of promoted types. |
| 6648 | |
| 6649 | &ASTContext::BoolTy, |
| 6650 | &ASTContext::CharTy, |
| 6651 | &ASTContext::WCharTy, |
| 6652 | &ASTContext::Char16Ty, |
| 6653 | &ASTContext::Char32Ty, |
| 6654 | &ASTContext::SignedCharTy, |
| 6655 | &ASTContext::ShortTy, |
| 6656 | &ASTContext::UnsignedCharTy, |
| 6657 | &ASTContext::UnsignedShortTy, |
| 6658 | // End of integral types. |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6659 | // FIXME: What about complex? What about half? |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6660 | }; |
| 6661 | return S.Context.*ArithmeticTypes[index]; |
| 6662 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6663 | |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6664 | /// \brief Gets the canonical type resulting from the usual arithemetic |
| 6665 | /// converions for the given arithmetic types. |
| 6666 | CanQualType getUsualArithmeticConversions(unsigned L, unsigned R) { |
| 6667 | // Accelerator table for performing the usual arithmetic conversions. |
| 6668 | // The rules are basically: |
| 6669 | // - if either is floating-point, use the wider floating-point |
| 6670 | // - if same signedness, use the higher rank |
| 6671 | // - if same size, use unsigned of the higher rank |
| 6672 | // - use the larger type |
| 6673 | // These rules, together with the axiom that higher ranks are |
| 6674 | // never smaller, are sufficient to precompute all of these results |
| 6675 | // *except* when dealing with signed types of higher rank. |
| 6676 | // (we could precompute SLL x UI for all known platforms, but it's |
| 6677 | // better not to make any assumptions). |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6678 | // 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] | 6679 | enum PromotedType { |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6680 | Dep=-1, |
| 6681 | Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6682 | }; |
Nuno Lopes | 79e244f | 2012-04-21 14:45:25 +0000 | [diff] [blame] | 6683 | static const PromotedType ConversionsTable[LastPromotedArithmeticType] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6684 | [LastPromotedArithmeticType] = { |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6685 | /* Flt*/ { Flt, Dbl, LDbl, Flt, Flt, Flt, Flt, Flt, Flt, Flt, Flt }, |
| 6686 | /* Dbl*/ { Dbl, Dbl, LDbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl }, |
| 6687 | /*LDbl*/ { LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl }, |
| 6688 | /* SI*/ { Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 }, |
| 6689 | /* SL*/ { Flt, Dbl, LDbl, SL, SL, SLL, S128, Dep, UL, ULL, U128 }, |
| 6690 | /* SLL*/ { Flt, Dbl, LDbl, SLL, SLL, SLL, S128, Dep, Dep, ULL, U128 }, |
| 6691 | /*S128*/ { Flt, Dbl, LDbl, S128, S128, S128, S128, S128, S128, S128, U128 }, |
| 6692 | /* UI*/ { Flt, Dbl, LDbl, UI, Dep, Dep, S128, UI, UL, ULL, U128 }, |
| 6693 | /* UL*/ { Flt, Dbl, LDbl, UL, UL, Dep, S128, UL, UL, ULL, U128 }, |
| 6694 | /* ULL*/ { Flt, Dbl, LDbl, ULL, ULL, ULL, S128, ULL, ULL, ULL, U128 }, |
| 6695 | /*U128*/ { Flt, Dbl, LDbl, U128, U128, U128, U128, U128, U128, U128, U128 }, |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6696 | }; |
| 6697 | |
| 6698 | assert(L < LastPromotedArithmeticType); |
| 6699 | assert(R < LastPromotedArithmeticType); |
| 6700 | int Idx = ConversionsTable[L][R]; |
| 6701 | |
| 6702 | // Fast path: the table gives us a concrete answer. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6703 | if (Idx != Dep) return getArithmeticType(Idx); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6704 | |
| 6705 | // Slow path: we need to compare widths. |
| 6706 | // An invariant is that the signed type has higher rank. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6707 | CanQualType LT = getArithmeticType(L), |
| 6708 | RT = getArithmeticType(R); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6709 | unsigned LW = S.Context.getIntWidth(LT), |
| 6710 | RW = S.Context.getIntWidth(RT); |
| 6711 | |
| 6712 | // If they're different widths, use the signed type. |
| 6713 | if (LW > RW) return LT; |
| 6714 | else if (LW < RW) return RT; |
| 6715 | |
| 6716 | // Otherwise, use the unsigned type of the signed type's rank. |
| 6717 | if (L == SL || R == SL) return S.Context.UnsignedLongTy; |
| 6718 | assert(L == SLL || R == SLL); |
| 6719 | return S.Context.UnsignedLongLongTy; |
| 6720 | } |
| 6721 | |
Chandler Carruth | 3c69dc4 | 2010-12-12 09:22:45 +0000 | [diff] [blame] | 6722 | /// \brief Helper method to factor out the common pattern of adding overloads |
| 6723 | /// for '++' and '--' builtin operators. |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6724 | void addPlusPlusMinusMinusStyleOverloads(QualType CandidateTy, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6725 | bool HasVolatile, |
| 6726 | bool HasRestrict) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6727 | QualType ParamTypes[2] = { |
| 6728 | S.Context.getLValueReferenceType(CandidateTy), |
| 6729 | S.Context.IntTy |
| 6730 | }; |
| 6731 | |
| 6732 | // Non-volatile version. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6733 | if (Args.size() == 1) |
| 6734 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6735 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6736 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6737 | |
| 6738 | // Use a heuristic to reduce number of builtin candidates in the set: |
| 6739 | // add volatile version only if there are conversions to a volatile type. |
| 6740 | if (HasVolatile) { |
| 6741 | ParamTypes[0] = |
| 6742 | S.Context.getLValueReferenceType( |
| 6743 | S.Context.getVolatileType(CandidateTy)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6744 | if (Args.size() == 1) |
| 6745 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6746 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6747 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6748 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6749 | |
| 6750 | // Add restrict version only if there are conversions to a restrict type |
| 6751 | // and our candidate type is a non-restrict-qualified pointer. |
| 6752 | if (HasRestrict && CandidateTy->isAnyPointerType() && |
| 6753 | !CandidateTy.isRestrictQualified()) { |
| 6754 | ParamTypes[0] |
| 6755 | = S.Context.getLValueReferenceType( |
| 6756 | S.Context.getCVRQualifiedType(CandidateTy, Qualifiers::Restrict)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6757 | if (Args.size() == 1) |
| 6758 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6759 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6760 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6761 | |
| 6762 | if (HasVolatile) { |
| 6763 | ParamTypes[0] |
| 6764 | = S.Context.getLValueReferenceType( |
| 6765 | S.Context.getCVRQualifiedType(CandidateTy, |
| 6766 | (Qualifiers::Volatile | |
| 6767 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6768 | if (Args.size() == 1) |
| 6769 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6770 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6771 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6772 | } |
| 6773 | } |
| 6774 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6775 | } |
| 6776 | |
| 6777 | public: |
| 6778 | BuiltinOperatorOverloadBuilder( |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6779 | Sema &S, ArrayRef<Expr *> Args, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6780 | Qualifiers VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6781 | bool HasArithmeticOrEnumeralCandidateType, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6782 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6783 | OverloadCandidateSet &CandidateSet) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6784 | : S(S), Args(Args), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6785 | VisibleTypeConversionsQuals(VisibleTypeConversionsQuals), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6786 | HasArithmeticOrEnumeralCandidateType( |
| 6787 | HasArithmeticOrEnumeralCandidateType), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6788 | CandidateTypes(CandidateTypes), |
| 6789 | CandidateSet(CandidateSet) { |
| 6790 | // Validate some of our static helper constants in debug builds. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6791 | assert(getArithmeticType(FirstPromotedIntegralType) == S.Context.IntTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6792 | "Invalid first promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6793 | assert(getArithmeticType(LastPromotedIntegralType - 1) |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6794 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6795 | "Invalid last promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6796 | assert(getArithmeticType(FirstPromotedArithmeticType) |
| 6797 | == S.Context.FloatTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6798 | "Invalid first promoted arithmetic type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6799 | assert(getArithmeticType(LastPromotedArithmeticType - 1) |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6800 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6801 | "Invalid last promoted arithmetic type"); |
| 6802 | } |
| 6803 | |
| 6804 | // C++ [over.built]p3: |
| 6805 | // |
| 6806 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 6807 | // is either volatile or empty, there exist candidate operator |
| 6808 | // functions of the form |
| 6809 | // |
| 6810 | // VQ T& operator++(VQ T&); |
| 6811 | // T operator++(VQ T&, int); |
| 6812 | // |
| 6813 | // C++ [over.built]p4: |
| 6814 | // |
| 6815 | // For every pair (T, VQ), where T is an arithmetic type other |
| 6816 | // than bool, and VQ is either volatile or empty, there exist |
| 6817 | // candidate operator functions of the form |
| 6818 | // |
| 6819 | // VQ T& operator--(VQ T&); |
| 6820 | // T operator--(VQ T&, int); |
| 6821 | void addPlusPlusMinusMinusArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6822 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6823 | return; |
| 6824 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6825 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 6826 | Arith < NumArithmeticTypes; ++Arith) { |
| 6827 | addPlusPlusMinusMinusStyleOverloads( |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6828 | getArithmeticType(Arith), |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6829 | VisibleTypeConversionsQuals.hasVolatile(), |
| 6830 | VisibleTypeConversionsQuals.hasRestrict()); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6831 | } |
| 6832 | } |
| 6833 | |
| 6834 | // C++ [over.built]p5: |
| 6835 | // |
| 6836 | // For every pair (T, VQ), where T is a cv-qualified or |
| 6837 | // cv-unqualified object type, and VQ is either volatile or |
| 6838 | // empty, there exist candidate operator functions of the form |
| 6839 | // |
| 6840 | // T*VQ& operator++(T*VQ&); |
| 6841 | // T*VQ& operator--(T*VQ&); |
| 6842 | // T* operator++(T*VQ&, int); |
| 6843 | // T* operator--(T*VQ&, int); |
| 6844 | void addPlusPlusMinusMinusPointerOverloads() { |
| 6845 | for (BuiltinCandidateTypeSet::iterator |
| 6846 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6847 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6848 | Ptr != PtrEnd; ++Ptr) { |
| 6849 | // Skip pointer types that aren't pointers to object types. |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6850 | if (!(*Ptr)->getPointeeType()->isObjectType()) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6851 | continue; |
| 6852 | |
| 6853 | addPlusPlusMinusMinusStyleOverloads(*Ptr, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6854 | (!(*Ptr).isVolatileQualified() && |
| 6855 | VisibleTypeConversionsQuals.hasVolatile()), |
| 6856 | (!(*Ptr).isRestrictQualified() && |
| 6857 | VisibleTypeConversionsQuals.hasRestrict())); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6858 | } |
| 6859 | } |
| 6860 | |
| 6861 | // C++ [over.built]p6: |
| 6862 | // For every cv-qualified or cv-unqualified object type T, there |
| 6863 | // exist candidate operator functions of the form |
| 6864 | // |
| 6865 | // T& operator*(T*); |
| 6866 | // |
| 6867 | // C++ [over.built]p7: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6868 | // 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] | 6869 | // ref-qualifier, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6870 | // T& operator*(T*); |
| 6871 | void addUnaryStarPointerOverloads() { |
| 6872 | for (BuiltinCandidateTypeSet::iterator |
| 6873 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6874 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6875 | Ptr != PtrEnd; ++Ptr) { |
| 6876 | QualType ParamTy = *Ptr; |
| 6877 | QualType PointeeTy = ParamTy->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6878 | if (!PointeeTy->isObjectType() && !PointeeTy->isFunctionType()) |
| 6879 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6880 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6881 | if (const FunctionProtoType *Proto =PointeeTy->getAs<FunctionProtoType>()) |
| 6882 | if (Proto->getTypeQuals() || Proto->getRefQualifier()) |
| 6883 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6884 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6885 | S.AddBuiltinCandidate(S.Context.getLValueReferenceType(PointeeTy), |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6886 | &ParamTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6887 | } |
| 6888 | } |
| 6889 | |
| 6890 | // C++ [over.built]p9: |
| 6891 | // For every promoted arithmetic type T, there exist candidate |
| 6892 | // operator functions of the form |
| 6893 | // |
| 6894 | // T operator+(T); |
| 6895 | // T operator-(T); |
| 6896 | void addUnaryPlusOrMinusArithmeticOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6897 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6898 | return; |
| 6899 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6900 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 6901 | Arith < LastPromotedArithmeticType; ++Arith) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6902 | QualType ArithTy = getArithmeticType(Arith); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6903 | S.AddBuiltinCandidate(ArithTy, &ArithTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6904 | } |
| 6905 | |
| 6906 | // Extension: We also add these operators for vector types. |
| 6907 | for (BuiltinCandidateTypeSet::iterator |
| 6908 | Vec = CandidateTypes[0].vector_begin(), |
| 6909 | VecEnd = CandidateTypes[0].vector_end(); |
| 6910 | Vec != VecEnd; ++Vec) { |
| 6911 | QualType VecTy = *Vec; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6912 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6913 | } |
| 6914 | } |
| 6915 | |
| 6916 | // C++ [over.built]p8: |
| 6917 | // For every type T, there exist candidate operator functions of |
| 6918 | // the form |
| 6919 | // |
| 6920 | // T* operator+(T*); |
| 6921 | void addUnaryPlusPointerOverloads() { |
| 6922 | for (BuiltinCandidateTypeSet::iterator |
| 6923 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6924 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6925 | Ptr != PtrEnd; ++Ptr) { |
| 6926 | QualType ParamTy = *Ptr; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6927 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6928 | } |
| 6929 | } |
| 6930 | |
| 6931 | // C++ [over.built]p10: |
| 6932 | // For every promoted integral type T, there exist candidate |
| 6933 | // operator functions of the form |
| 6934 | // |
| 6935 | // T operator~(T); |
| 6936 | void addUnaryTildePromotedIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6937 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6938 | return; |
| 6939 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6940 | for (unsigned Int = FirstPromotedIntegralType; |
| 6941 | Int < LastPromotedIntegralType; ++Int) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6942 | QualType IntTy = getArithmeticType(Int); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6943 | S.AddBuiltinCandidate(IntTy, &IntTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6944 | } |
| 6945 | |
| 6946 | // Extension: We also add this operator for vector types. |
| 6947 | for (BuiltinCandidateTypeSet::iterator |
| 6948 | Vec = CandidateTypes[0].vector_begin(), |
| 6949 | VecEnd = CandidateTypes[0].vector_end(); |
| 6950 | Vec != VecEnd; ++Vec) { |
| 6951 | QualType VecTy = *Vec; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6952 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6953 | } |
| 6954 | } |
| 6955 | |
| 6956 | // C++ [over.match.oper]p16: |
| 6957 | // For every pointer to member type T, there exist candidate operator |
| 6958 | // functions of the form |
| 6959 | // |
| 6960 | // bool operator==(T,T); |
| 6961 | // bool operator!=(T,T); |
| 6962 | void addEqualEqualOrNotEqualMemberPointerOverloads() { |
| 6963 | /// Set of (canonical) types that we've already handled. |
| 6964 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6965 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6966 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6967 | for (BuiltinCandidateTypeSet::iterator |
| 6968 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 6969 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 6970 | MemPtr != MemPtrEnd; |
| 6971 | ++MemPtr) { |
| 6972 | // Don't add the same builtin candidate twice. |
| 6973 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 6974 | continue; |
| 6975 | |
| 6976 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6977 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6978 | } |
| 6979 | } |
| 6980 | } |
| 6981 | |
| 6982 | // C++ [over.built]p15: |
| 6983 | // |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6984 | // For every T, where T is an enumeration type, a pointer type, or |
| 6985 | // std::nullptr_t, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6986 | // |
| 6987 | // bool operator<(T, T); |
| 6988 | // bool operator>(T, T); |
| 6989 | // bool operator<=(T, T); |
| 6990 | // bool operator>=(T, T); |
| 6991 | // bool operator==(T, T); |
| 6992 | // bool operator!=(T, T); |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6993 | void addRelationalPointerOrEnumeralOverloads() { |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 6994 | // C++ [over.match.oper]p3: |
| 6995 | // [...]the built-in candidates include all of the candidate operator |
| 6996 | // functions defined in 13.6 that, compared to the given operator, [...] |
| 6997 | // do not have the same parameter-type-list as any non-template non-member |
| 6998 | // candidate. |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6999 | // |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 7000 | // Note that in practice, this only affects enumeration types because there |
| 7001 | // aren't any built-in candidates of record type, and a user-defined operator |
| 7002 | // must have an operand of record or enumeration type. Also, the only other |
| 7003 | // overloaded operator with enumeration arguments, operator=, |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7004 | // cannot be overloaded for enumeration types, so this is the only place |
| 7005 | // where we must suppress candidates like this. |
| 7006 | llvm::DenseSet<std::pair<CanQualType, CanQualType> > |
| 7007 | UserDefinedBinaryOperators; |
| 7008 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7009 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7010 | if (CandidateTypes[ArgIdx].enumeration_begin() != |
| 7011 | CandidateTypes[ArgIdx].enumeration_end()) { |
| 7012 | for (OverloadCandidateSet::iterator C = CandidateSet.begin(), |
| 7013 | CEnd = CandidateSet.end(); |
| 7014 | C != CEnd; ++C) { |
| 7015 | if (!C->Viable || !C->Function || C->Function->getNumParams() != 2) |
| 7016 | continue; |
| 7017 | |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 7018 | if (C->Function->isFunctionTemplateSpecialization()) |
| 7019 | continue; |
| 7020 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7021 | QualType FirstParamType = |
| 7022 | C->Function->getParamDecl(0)->getType().getUnqualifiedType(); |
| 7023 | QualType SecondParamType = |
| 7024 | C->Function->getParamDecl(1)->getType().getUnqualifiedType(); |
| 7025 | |
| 7026 | // Skip if either parameter isn't of enumeral type. |
| 7027 | if (!FirstParamType->isEnumeralType() || |
| 7028 | !SecondParamType->isEnumeralType()) |
| 7029 | continue; |
| 7030 | |
| 7031 | // Add this operator to the set of known user-defined operators. |
| 7032 | UserDefinedBinaryOperators.insert( |
| 7033 | std::make_pair(S.Context.getCanonicalType(FirstParamType), |
| 7034 | S.Context.getCanonicalType(SecondParamType))); |
| 7035 | } |
| 7036 | } |
| 7037 | } |
| 7038 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7039 | /// Set of (canonical) types that we've already handled. |
| 7040 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7041 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7042 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7043 | for (BuiltinCandidateTypeSet::iterator |
| 7044 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 7045 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 7046 | Ptr != PtrEnd; ++Ptr) { |
| 7047 | // Don't add the same builtin candidate twice. |
| 7048 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7049 | continue; |
| 7050 | |
| 7051 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7052 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7053 | } |
| 7054 | for (BuiltinCandidateTypeSet::iterator |
| 7055 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7056 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7057 | Enum != EnumEnd; ++Enum) { |
| 7058 | CanQualType CanonType = S.Context.getCanonicalType(*Enum); |
| 7059 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7060 | // Don't add the same builtin candidate twice, or if a user defined |
| 7061 | // candidate exists. |
| 7062 | if (!AddedTypes.insert(CanonType) || |
| 7063 | UserDefinedBinaryOperators.count(std::make_pair(CanonType, |
| 7064 | CanonType))) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7065 | continue; |
| 7066 | |
| 7067 | QualType ParamTypes[2] = { *Enum, *Enum }; |
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 | } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7070 | |
| 7071 | if (CandidateTypes[ArgIdx].hasNullPtrType()) { |
| 7072 | CanQualType NullPtrTy = S.Context.getCanonicalType(S.Context.NullPtrTy); |
| 7073 | if (AddedTypes.insert(NullPtrTy) && |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7074 | !UserDefinedBinaryOperators.count(std::make_pair(NullPtrTy, |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7075 | NullPtrTy))) { |
| 7076 | QualType ParamTypes[2] = { NullPtrTy, NullPtrTy }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7077 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7078 | CandidateSet); |
| 7079 | } |
| 7080 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7081 | } |
| 7082 | } |
| 7083 | |
| 7084 | // C++ [over.built]p13: |
| 7085 | // |
| 7086 | // For every cv-qualified or cv-unqualified object type T |
| 7087 | // there exist candidate operator functions of the form |
| 7088 | // |
| 7089 | // T* operator+(T*, ptrdiff_t); |
| 7090 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 7091 | // T* operator-(T*, ptrdiff_t); |
| 7092 | // T* operator+(ptrdiff_t, T*); |
| 7093 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 7094 | // |
| 7095 | // C++ [over.built]p14: |
| 7096 | // |
| 7097 | // For every T, where T is a pointer to object type, there |
| 7098 | // exist candidate operator functions of the form |
| 7099 | // |
| 7100 | // ptrdiff_t operator-(T, T); |
| 7101 | void addBinaryPlusOrMinusPointerOverloads(OverloadedOperatorKind Op) { |
| 7102 | /// Set of (canonical) types that we've already handled. |
| 7103 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7104 | |
| 7105 | for (int Arg = 0; Arg < 2; ++Arg) { |
| 7106 | QualType AsymetricParamTypes[2] = { |
| 7107 | S.Context.getPointerDiffType(), |
| 7108 | S.Context.getPointerDiffType(), |
| 7109 | }; |
| 7110 | for (BuiltinCandidateTypeSet::iterator |
| 7111 | Ptr = CandidateTypes[Arg].pointer_begin(), |
| 7112 | PtrEnd = CandidateTypes[Arg].pointer_end(); |
| 7113 | Ptr != PtrEnd; ++Ptr) { |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7114 | QualType PointeeTy = (*Ptr)->getPointeeType(); |
| 7115 | if (!PointeeTy->isObjectType()) |
| 7116 | continue; |
| 7117 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7118 | AsymetricParamTypes[Arg] = *Ptr; |
| 7119 | if (Arg == 0 || Op == OO_Plus) { |
| 7120 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 7121 | // T* operator+(ptrdiff_t, T*); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7122 | S.AddBuiltinCandidate(*Ptr, AsymetricParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7123 | } |
| 7124 | if (Op == OO_Minus) { |
| 7125 | // ptrdiff_t operator-(T, T); |
| 7126 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7127 | continue; |
| 7128 | |
| 7129 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 7130 | S.AddBuiltinCandidate(S.Context.getPointerDiffType(), ParamTypes, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7131 | Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7132 | } |
| 7133 | } |
| 7134 | } |
| 7135 | } |
| 7136 | |
| 7137 | // C++ [over.built]p12: |
| 7138 | // |
| 7139 | // For every pair of promoted arithmetic types L and R, there |
| 7140 | // exist candidate operator functions of the form |
| 7141 | // |
| 7142 | // LR operator*(L, R); |
| 7143 | // LR operator/(L, R); |
| 7144 | // LR operator+(L, R); |
| 7145 | // LR operator-(L, R); |
| 7146 | // bool operator<(L, R); |
| 7147 | // bool operator>(L, R); |
| 7148 | // bool operator<=(L, R); |
| 7149 | // bool operator>=(L, R); |
| 7150 | // bool operator==(L, R); |
| 7151 | // bool operator!=(L, R); |
| 7152 | // |
| 7153 | // where LR is the result of the usual arithmetic conversions |
| 7154 | // between types L and R. |
| 7155 | // |
| 7156 | // C++ [over.built]p24: |
| 7157 | // |
| 7158 | // For every pair of promoted arithmetic types L and R, there exist |
| 7159 | // candidate operator functions of the form |
| 7160 | // |
| 7161 | // LR operator?(bool, L, R); |
| 7162 | // |
| 7163 | // where LR is the result of the usual arithmetic conversions |
| 7164 | // between types L and R. |
| 7165 | // Our candidates ignore the first parameter. |
| 7166 | void addGenericBinaryArithmeticOverloads(bool isComparison) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7167 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7168 | return; |
| 7169 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7170 | for (unsigned Left = FirstPromotedArithmeticType; |
| 7171 | Left < LastPromotedArithmeticType; ++Left) { |
| 7172 | for (unsigned Right = FirstPromotedArithmeticType; |
| 7173 | Right < LastPromotedArithmeticType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7174 | QualType LandR[2] = { getArithmeticType(Left), |
| 7175 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7176 | QualType Result = |
| 7177 | isComparison ? S.Context.BoolTy |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7178 | : getUsualArithmeticConversions(Left, Right); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7179 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7180 | } |
| 7181 | } |
| 7182 | |
| 7183 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 7184 | // conditional operator for vector types. |
| 7185 | for (BuiltinCandidateTypeSet::iterator |
| 7186 | Vec1 = CandidateTypes[0].vector_begin(), |
| 7187 | Vec1End = CandidateTypes[0].vector_end(); |
| 7188 | Vec1 != Vec1End; ++Vec1) { |
| 7189 | for (BuiltinCandidateTypeSet::iterator |
| 7190 | Vec2 = CandidateTypes[1].vector_begin(), |
| 7191 | Vec2End = CandidateTypes[1].vector_end(); |
| 7192 | Vec2 != Vec2End; ++Vec2) { |
| 7193 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 7194 | QualType Result = S.Context.BoolTy; |
| 7195 | if (!isComparison) { |
| 7196 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 7197 | Result = *Vec1; |
| 7198 | else |
| 7199 | Result = *Vec2; |
| 7200 | } |
| 7201 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7202 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7203 | } |
| 7204 | } |
| 7205 | } |
| 7206 | |
| 7207 | // C++ [over.built]p17: |
| 7208 | // |
| 7209 | // For every pair of promoted integral types L and R, there |
| 7210 | // exist candidate operator functions of the form |
| 7211 | // |
| 7212 | // LR operator%(L, R); |
| 7213 | // LR operator&(L, R); |
| 7214 | // LR operator^(L, R); |
| 7215 | // LR operator|(L, R); |
| 7216 | // L operator<<(L, R); |
| 7217 | // L operator>>(L, R); |
| 7218 | // |
| 7219 | // where LR is the result of the usual arithmetic conversions |
| 7220 | // between types L and R. |
| 7221 | void addBinaryBitwiseArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7222 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7223 | return; |
| 7224 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7225 | for (unsigned Left = FirstPromotedIntegralType; |
| 7226 | Left < LastPromotedIntegralType; ++Left) { |
| 7227 | for (unsigned Right = FirstPromotedIntegralType; |
| 7228 | Right < LastPromotedIntegralType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7229 | QualType LandR[2] = { getArithmeticType(Left), |
| 7230 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7231 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 7232 | ? LandR[0] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7233 | : getUsualArithmeticConversions(Left, Right); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7234 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7235 | } |
| 7236 | } |
| 7237 | } |
| 7238 | |
| 7239 | // C++ [over.built]p20: |
| 7240 | // |
| 7241 | // For every pair (T, VQ), where T is an enumeration or |
| 7242 | // pointer to member type and VQ is either volatile or |
| 7243 | // empty, there exist candidate operator functions of the form |
| 7244 | // |
| 7245 | // VQ T& operator=(VQ T&, T); |
| 7246 | void addAssignmentMemberPointerOrEnumeralOverloads() { |
| 7247 | /// Set of (canonical) types that we've already handled. |
| 7248 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7249 | |
| 7250 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7251 | for (BuiltinCandidateTypeSet::iterator |
| 7252 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7253 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7254 | Enum != EnumEnd; ++Enum) { |
| 7255 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 7256 | continue; |
| 7257 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7258 | AddBuiltinAssignmentOperatorCandidates(S, *Enum, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7259 | } |
| 7260 | |
| 7261 | for (BuiltinCandidateTypeSet::iterator |
| 7262 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7263 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7264 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7265 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7266 | continue; |
| 7267 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7268 | AddBuiltinAssignmentOperatorCandidates(S, *MemPtr, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7269 | } |
| 7270 | } |
| 7271 | } |
| 7272 | |
| 7273 | // C++ [over.built]p19: |
| 7274 | // |
| 7275 | // For every pair (T, VQ), where T is any type and VQ is either |
| 7276 | // volatile or empty, there exist candidate operator functions |
| 7277 | // of the form |
| 7278 | // |
| 7279 | // T*VQ& operator=(T*VQ&, T*); |
| 7280 | // |
| 7281 | // C++ [over.built]p21: |
| 7282 | // |
| 7283 | // For every pair (T, VQ), where T is a cv-qualified or |
| 7284 | // cv-unqualified object type and VQ is either volatile or |
| 7285 | // empty, there exist candidate operator functions of the form |
| 7286 | // |
| 7287 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 7288 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 7289 | void addAssignmentPointerOverloads(bool isEqualOp) { |
| 7290 | /// Set of (canonical) types that we've already handled. |
| 7291 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7292 | |
| 7293 | for (BuiltinCandidateTypeSet::iterator |
| 7294 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7295 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7296 | Ptr != PtrEnd; ++Ptr) { |
| 7297 | // If this is operator=, keep track of the builtin candidates we added. |
| 7298 | if (isEqualOp) |
| 7299 | AddedTypes.insert(S.Context.getCanonicalType(*Ptr)); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7300 | else if (!(*Ptr)->getPointeeType()->isObjectType()) |
| 7301 | continue; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7302 | |
| 7303 | // non-volatile version |
| 7304 | QualType ParamTypes[2] = { |
| 7305 | S.Context.getLValueReferenceType(*Ptr), |
| 7306 | isEqualOp ? *Ptr : S.Context.getPointerDiffType(), |
| 7307 | }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7308 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7309 | /*IsAssigmentOperator=*/ isEqualOp); |
| 7310 | |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7311 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7312 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7313 | if (NeedVolatile) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7314 | // volatile version |
| 7315 | ParamTypes[0] = |
| 7316 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7317 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7318 | /*IsAssigmentOperator=*/isEqualOp); |
| 7319 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7320 | |
| 7321 | if (!(*Ptr).isRestrictQualified() && |
| 7322 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7323 | // restrict version |
| 7324 | ParamTypes[0] |
| 7325 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7326 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7327 | /*IsAssigmentOperator=*/isEqualOp); |
| 7328 | |
| 7329 | if (NeedVolatile) { |
| 7330 | // volatile restrict version |
| 7331 | ParamTypes[0] |
| 7332 | = S.Context.getLValueReferenceType( |
| 7333 | S.Context.getCVRQualifiedType(*Ptr, |
| 7334 | (Qualifiers::Volatile | |
| 7335 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7336 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7337 | /*IsAssigmentOperator=*/isEqualOp); |
| 7338 | } |
| 7339 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7340 | } |
| 7341 | |
| 7342 | if (isEqualOp) { |
| 7343 | for (BuiltinCandidateTypeSet::iterator |
| 7344 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7345 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7346 | Ptr != PtrEnd; ++Ptr) { |
| 7347 | // Make sure we don't add the same candidate twice. |
| 7348 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7349 | continue; |
| 7350 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7351 | QualType ParamTypes[2] = { |
| 7352 | S.Context.getLValueReferenceType(*Ptr), |
| 7353 | *Ptr, |
| 7354 | }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7355 | |
| 7356 | // non-volatile version |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7357 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7358 | /*IsAssigmentOperator=*/true); |
| 7359 | |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7360 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7361 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7362 | if (NeedVolatile) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7363 | // volatile version |
| 7364 | ParamTypes[0] = |
| 7365 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7366 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7367 | /*IsAssigmentOperator=*/true); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7368 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7369 | |
| 7370 | if (!(*Ptr).isRestrictQualified() && |
| 7371 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7372 | // restrict version |
| 7373 | ParamTypes[0] |
| 7374 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7375 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7376 | /*IsAssigmentOperator=*/true); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7377 | |
| 7378 | if (NeedVolatile) { |
| 7379 | // volatile restrict version |
| 7380 | ParamTypes[0] |
| 7381 | = S.Context.getLValueReferenceType( |
| 7382 | S.Context.getCVRQualifiedType(*Ptr, |
| 7383 | (Qualifiers::Volatile | |
| 7384 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7385 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7386 | /*IsAssigmentOperator=*/true); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7387 | } |
| 7388 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7389 | } |
| 7390 | } |
| 7391 | } |
| 7392 | |
| 7393 | // C++ [over.built]p18: |
| 7394 | // |
| 7395 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 7396 | // VQ is either volatile or empty, and R is a promoted |
| 7397 | // arithmetic type, there exist candidate operator functions of |
| 7398 | // the form |
| 7399 | // |
| 7400 | // VQ L& operator=(VQ L&, R); |
| 7401 | // VQ L& operator*=(VQ L&, R); |
| 7402 | // VQ L& operator/=(VQ L&, R); |
| 7403 | // VQ L& operator+=(VQ L&, R); |
| 7404 | // VQ L& operator-=(VQ L&, R); |
| 7405 | void addAssignmentArithmeticOverloads(bool isEqualOp) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7406 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7407 | return; |
| 7408 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7409 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 7410 | for (unsigned Right = FirstPromotedArithmeticType; |
| 7411 | Right < LastPromotedArithmeticType; ++Right) { |
| 7412 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7413 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7414 | |
| 7415 | // Add this built-in operator as a candidate (VQ is empty). |
| 7416 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7417 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7418 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7419 | /*IsAssigmentOperator=*/isEqualOp); |
| 7420 | |
| 7421 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7422 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7423 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7424 | S.Context.getVolatileType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7425 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7426 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7427 | /*IsAssigmentOperator=*/isEqualOp); |
| 7428 | } |
| 7429 | } |
| 7430 | } |
| 7431 | |
| 7432 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 7433 | for (BuiltinCandidateTypeSet::iterator |
| 7434 | Vec1 = CandidateTypes[0].vector_begin(), |
| 7435 | Vec1End = CandidateTypes[0].vector_end(); |
| 7436 | Vec1 != Vec1End; ++Vec1) { |
| 7437 | for (BuiltinCandidateTypeSet::iterator |
| 7438 | Vec2 = CandidateTypes[1].vector_begin(), |
| 7439 | Vec2End = CandidateTypes[1].vector_end(); |
| 7440 | Vec2 != Vec2End; ++Vec2) { |
| 7441 | QualType ParamTypes[2]; |
| 7442 | ParamTypes[1] = *Vec2; |
| 7443 | // Add this built-in operator as a candidate (VQ is empty). |
| 7444 | ParamTypes[0] = S.Context.getLValueReferenceType(*Vec1); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7445 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7446 | /*IsAssigmentOperator=*/isEqualOp); |
| 7447 | |
| 7448 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7449 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7450 | ParamTypes[0] = S.Context.getVolatileType(*Vec1); |
| 7451 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7452 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7453 | /*IsAssigmentOperator=*/isEqualOp); |
| 7454 | } |
| 7455 | } |
| 7456 | } |
| 7457 | } |
| 7458 | |
| 7459 | // C++ [over.built]p22: |
| 7460 | // |
| 7461 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 7462 | // is either volatile or empty, and R is a promoted integral |
| 7463 | // type, there exist candidate operator functions of the form |
| 7464 | // |
| 7465 | // VQ L& operator%=(VQ L&, R); |
| 7466 | // VQ L& operator<<=(VQ L&, R); |
| 7467 | // VQ L& operator>>=(VQ L&, R); |
| 7468 | // VQ L& operator&=(VQ L&, R); |
| 7469 | // VQ L& operator^=(VQ L&, R); |
| 7470 | // VQ L& operator|=(VQ L&, R); |
| 7471 | void addAssignmentIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7472 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7473 | return; |
| 7474 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7475 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 7476 | for (unsigned Right = FirstPromotedIntegralType; |
| 7477 | Right < LastPromotedIntegralType; ++Right) { |
| 7478 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7479 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7480 | |
| 7481 | // Add this built-in operator as a candidate (VQ is empty). |
| 7482 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7483 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7484 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7485 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7486 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7487 | ParamTypes[0] = getArithmeticType(Left); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7488 | ParamTypes[0] = S.Context.getVolatileType(ParamTypes[0]); |
| 7489 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7490 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7491 | } |
| 7492 | } |
| 7493 | } |
| 7494 | } |
| 7495 | |
| 7496 | // C++ [over.operator]p23: |
| 7497 | // |
| 7498 | // There also exist candidate operator functions of the form |
| 7499 | // |
| 7500 | // bool operator!(bool); |
| 7501 | // bool operator&&(bool, bool); |
| 7502 | // bool operator||(bool, bool); |
| 7503 | void addExclaimOverload() { |
| 7504 | QualType ParamTy = S.Context.BoolTy; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7505 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7506 | /*IsAssignmentOperator=*/false, |
| 7507 | /*NumContextualBoolArguments=*/1); |
| 7508 | } |
| 7509 | void addAmpAmpOrPipePipeOverload() { |
| 7510 | QualType ParamTypes[2] = { S.Context.BoolTy, S.Context.BoolTy }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7511 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7512 | /*IsAssignmentOperator=*/false, |
| 7513 | /*NumContextualBoolArguments=*/2); |
| 7514 | } |
| 7515 | |
| 7516 | // C++ [over.built]p13: |
| 7517 | // |
| 7518 | // For every cv-qualified or cv-unqualified object type T there |
| 7519 | // exist candidate operator functions of the form |
| 7520 | // |
| 7521 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 7522 | // T& operator[](T*, ptrdiff_t); |
| 7523 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 7524 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 7525 | // T& operator[](ptrdiff_t, T*); |
| 7526 | void addSubscriptOverloads() { |
| 7527 | for (BuiltinCandidateTypeSet::iterator |
| 7528 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7529 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7530 | Ptr != PtrEnd; ++Ptr) { |
| 7531 | QualType ParamTypes[2] = { *Ptr, S.Context.getPointerDiffType() }; |
| 7532 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7533 | if (!PointeeType->isObjectType()) |
| 7534 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7535 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7536 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7537 | |
| 7538 | // T& operator[](T*, ptrdiff_t) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7539 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7540 | } |
| 7541 | |
| 7542 | for (BuiltinCandidateTypeSet::iterator |
| 7543 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7544 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7545 | Ptr != PtrEnd; ++Ptr) { |
| 7546 | QualType ParamTypes[2] = { S.Context.getPointerDiffType(), *Ptr }; |
| 7547 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7548 | if (!PointeeType->isObjectType()) |
| 7549 | continue; |
| 7550 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7551 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7552 | |
| 7553 | // T& operator[](ptrdiff_t, T*) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7554 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7555 | } |
| 7556 | } |
| 7557 | |
| 7558 | // C++ [over.built]p11: |
| 7559 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 7560 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 7561 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 7562 | // there exist candidate operator functions of the form |
| 7563 | // |
| 7564 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 7565 | // |
| 7566 | // where CV12 is the union of CV1 and CV2. |
| 7567 | void addArrowStarOverloads() { |
| 7568 | for (BuiltinCandidateTypeSet::iterator |
| 7569 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7570 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7571 | Ptr != PtrEnd; ++Ptr) { |
| 7572 | QualType C1Ty = (*Ptr); |
| 7573 | QualType C1; |
| 7574 | QualifierCollector Q1; |
| 7575 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
| 7576 | if (!isa<RecordType>(C1)) |
| 7577 | continue; |
| 7578 | // heuristic to reduce number of builtin candidates in the set. |
| 7579 | // Add volatile/restrict version only if there are conversions to a |
| 7580 | // volatile/restrict type. |
| 7581 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 7582 | continue; |
| 7583 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 7584 | continue; |
| 7585 | for (BuiltinCandidateTypeSet::iterator |
| 7586 | MemPtr = CandidateTypes[1].member_pointer_begin(), |
| 7587 | MemPtrEnd = CandidateTypes[1].member_pointer_end(); |
| 7588 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7589 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 7590 | QualType C2 = QualType(mptr->getClass(), 0); |
| 7591 | C2 = C2.getUnqualifiedType(); |
| 7592 | if (C1 != C2 && !S.IsDerivedFrom(C1, C2)) |
| 7593 | break; |
| 7594 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 7595 | // build CV12 T& |
| 7596 | QualType T = mptr->getPointeeType(); |
| 7597 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 7598 | T.isVolatileQualified()) |
| 7599 | continue; |
| 7600 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 7601 | T.isRestrictQualified()) |
| 7602 | continue; |
| 7603 | T = Q1.apply(S.Context, T); |
| 7604 | QualType ResultTy = S.Context.getLValueReferenceType(T); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7605 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7606 | } |
| 7607 | } |
| 7608 | } |
| 7609 | |
| 7610 | // Note that we don't consider the first argument, since it has been |
| 7611 | // contextually converted to bool long ago. The candidates below are |
| 7612 | // therefore added as binary. |
| 7613 | // |
| 7614 | // C++ [over.built]p25: |
| 7615 | // For every type T, where T is a pointer, pointer-to-member, or scoped |
| 7616 | // enumeration type, there exist candidate operator functions of the form |
| 7617 | // |
| 7618 | // T operator?(bool, T, T); |
| 7619 | // |
| 7620 | void addConditionalOperatorOverloads() { |
| 7621 | /// Set of (canonical) types that we've already handled. |
| 7622 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7623 | |
| 7624 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7625 | for (BuiltinCandidateTypeSet::iterator |
| 7626 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 7627 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 7628 | Ptr != PtrEnd; ++Ptr) { |
| 7629 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7630 | continue; |
| 7631 | |
| 7632 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7633 | S.AddBuiltinCandidate(*Ptr, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7634 | } |
| 7635 | |
| 7636 | for (BuiltinCandidateTypeSet::iterator |
| 7637 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7638 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7639 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7640 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7641 | continue; |
| 7642 | |
| 7643 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7644 | S.AddBuiltinCandidate(*MemPtr, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7645 | } |
| 7646 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7647 | if (S.getLangOpts().CPlusPlus11) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7648 | for (BuiltinCandidateTypeSet::iterator |
| 7649 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7650 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7651 | Enum != EnumEnd; ++Enum) { |
| 7652 | if (!(*Enum)->getAs<EnumType>()->getDecl()->isScoped()) |
| 7653 | continue; |
| 7654 | |
| 7655 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 7656 | continue; |
| 7657 | |
| 7658 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7659 | S.AddBuiltinCandidate(*Enum, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7660 | } |
| 7661 | } |
| 7662 | } |
| 7663 | } |
| 7664 | }; |
| 7665 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7666 | } // end anonymous namespace |
| 7667 | |
| 7668 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 7669 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 7670 | /// on the operator @p Op and the arguments given. For example, if the |
| 7671 | /// operator is a binary '+', this routine might add "int |
| 7672 | /// operator+(int, int)" to cover integer addition. |
Robert Wilhelm | 834c058 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 7673 | void Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 7674 | SourceLocation OpLoc, |
| 7675 | ArrayRef<Expr *> Args, |
| 7676 | OverloadCandidateSet &CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7677 | // Find all of the types that the arguments can convert to, but only |
| 7678 | // if the operator we're looking at has built-in operator candidates |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7679 | // that make use of these types. Also record whether we encounter non-record |
| 7680 | // candidate types or either arithmetic or enumeral candidate types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7681 | Qualifiers VisibleTypeConversionsQuals; |
| 7682 | VisibleTypeConversionsQuals.addConst(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7683 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 7684 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7685 | |
| 7686 | bool HasNonRecordCandidateType = false; |
| 7687 | bool HasArithmeticOrEnumeralCandidateType = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7688 | SmallVector<BuiltinCandidateTypeSet, 2> CandidateTypes; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7689 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7690 | CandidateTypes.push_back(BuiltinCandidateTypeSet(*this)); |
| 7691 | CandidateTypes[ArgIdx].AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 7692 | OpLoc, |
| 7693 | true, |
| 7694 | (Op == OO_Exclaim || |
| 7695 | Op == OO_AmpAmp || |
| 7696 | Op == OO_PipePipe), |
| 7697 | VisibleTypeConversionsQuals); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7698 | HasNonRecordCandidateType = HasNonRecordCandidateType || |
| 7699 | CandidateTypes[ArgIdx].hasNonRecordTypes(); |
| 7700 | HasArithmeticOrEnumeralCandidateType = |
| 7701 | HasArithmeticOrEnumeralCandidateType || |
| 7702 | CandidateTypes[ArgIdx].hasArithmeticOrEnumeralTypes(); |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7703 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7704 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7705 | // Exit early when no non-record types have been added to the candidate set |
| 7706 | // for any of the arguments to the operator. |
Douglas Gregor | 25aaff9 | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 7707 | // |
| 7708 | // We can't exit early for !, ||, or &&, since there we have always have |
| 7709 | // 'bool' overloads. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7710 | if (!HasNonRecordCandidateType && |
Douglas Gregor | 25aaff9 | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 7711 | !(Op == OO_Exclaim || Op == OO_AmpAmp || Op == OO_PipePipe)) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7712 | return; |
| 7713 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7714 | // Setup an object to manage the common state for building overloads. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7715 | BuiltinOperatorOverloadBuilder OpBuilder(*this, Args, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7716 | VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7717 | HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7718 | CandidateTypes, CandidateSet); |
| 7719 | |
| 7720 | // Dispatch over the operation to add in only those overloads which apply. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7721 | switch (Op) { |
| 7722 | case OO_None: |
| 7723 | case NUM_OVERLOADED_OPERATORS: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7724 | llvm_unreachable("Expected an overloaded operator"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7725 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7726 | case OO_New: |
| 7727 | case OO_Delete: |
| 7728 | case OO_Array_New: |
| 7729 | case OO_Array_Delete: |
| 7730 | case OO_Call: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7731 | llvm_unreachable( |
| 7732 | "Special operators don't use AddBuiltinOperatorCandidates"); |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7733 | |
| 7734 | case OO_Comma: |
| 7735 | case OO_Arrow: |
| 7736 | // C++ [over.match.oper]p3: |
| 7737 | // -- For the operator ',', the unary operator '&', or the |
| 7738 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7739 | break; |
| 7740 | |
| 7741 | case OO_Plus: // '+' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7742 | if (Args.size() == 1) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7743 | OpBuilder.addUnaryPlusPointerOverloads(); |
Chandler Carruth | 32fe0d0 | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 7744 | // Fall through. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7745 | |
| 7746 | case OO_Minus: // '-' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7747 | if (Args.size() == 1) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7748 | OpBuilder.addUnaryPlusOrMinusArithmeticOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7749 | } else { |
| 7750 | OpBuilder.addBinaryPlusOrMinusPointerOverloads(Op); |
| 7751 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7752 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7753 | break; |
| 7754 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7755 | case OO_Star: // '*' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7756 | if (Args.size() == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7757 | OpBuilder.addUnaryStarPointerOverloads(); |
| 7758 | else |
| 7759 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7760 | break; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7761 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7762 | case OO_Slash: |
| 7763 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 7764 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7765 | |
| 7766 | case OO_PlusPlus: |
| 7767 | case OO_MinusMinus: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7768 | OpBuilder.addPlusPlusMinusMinusArithmeticOverloads(Op); |
| 7769 | OpBuilder.addPlusPlusMinusMinusPointerOverloads(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7770 | break; |
| 7771 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 7772 | case OO_EqualEqual: |
| 7773 | case OO_ExclaimEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7774 | OpBuilder.addEqualEqualOrNotEqualMemberPointerOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 7775 | // Fall through. |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 7776 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7777 | case OO_Less: |
| 7778 | case OO_Greater: |
| 7779 | case OO_LessEqual: |
| 7780 | case OO_GreaterEqual: |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7781 | OpBuilder.addRelationalPointerOrEnumeralOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 7782 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/true); |
| 7783 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7784 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7785 | case OO_Percent: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7786 | case OO_Caret: |
| 7787 | case OO_Pipe: |
| 7788 | case OO_LessLess: |
| 7789 | case OO_GreaterGreater: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7790 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7791 | break; |
| 7792 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7793 | case OO_Amp: // '&' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7794 | if (Args.size() == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7795 | // C++ [over.match.oper]p3: |
| 7796 | // -- For the operator ',', the unary operator '&', or the |
| 7797 | // operator '->', the built-in candidates set is empty. |
| 7798 | break; |
| 7799 | |
| 7800 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
| 7801 | break; |
| 7802 | |
| 7803 | case OO_Tilde: |
| 7804 | OpBuilder.addUnaryTildePromotedIntegralOverloads(); |
| 7805 | break; |
| 7806 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7807 | case OO_Equal: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7808 | OpBuilder.addAssignmentMemberPointerOrEnumeralOverloads(); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7809 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7810 | |
| 7811 | case OO_PlusEqual: |
| 7812 | case OO_MinusEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7813 | OpBuilder.addAssignmentPointerOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7814 | // Fall through. |
| 7815 | |
| 7816 | case OO_StarEqual: |
| 7817 | case OO_SlashEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7818 | OpBuilder.addAssignmentArithmeticOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7819 | break; |
| 7820 | |
| 7821 | case OO_PercentEqual: |
| 7822 | case OO_LessLessEqual: |
| 7823 | case OO_GreaterGreaterEqual: |
| 7824 | case OO_AmpEqual: |
| 7825 | case OO_CaretEqual: |
| 7826 | case OO_PipeEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7827 | OpBuilder.addAssignmentIntegralOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7828 | break; |
| 7829 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7830 | case OO_Exclaim: |
| 7831 | OpBuilder.addExclaimOverload(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7832 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7833 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7834 | case OO_AmpAmp: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7835 | case OO_PipePipe: |
| 7836 | OpBuilder.addAmpAmpOrPipePipeOverload(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7837 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7838 | |
| 7839 | case OO_Subscript: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7840 | OpBuilder.addSubscriptOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7841 | break; |
| 7842 | |
| 7843 | case OO_ArrowStar: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7844 | OpBuilder.addArrowStarOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7845 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 7846 | |
| 7847 | case OO_Conditional: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7848 | OpBuilder.addConditionalOperatorOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7849 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7850 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7851 | } |
| 7852 | } |
| 7853 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7854 | /// \brief Add function candidates found via argument-dependent lookup |
| 7855 | /// to the set of overloading candidates. |
| 7856 | /// |
| 7857 | /// This routine performs argument-dependent name lookup based on the |
| 7858 | /// given function name (which may also be an operator name) and adds |
| 7859 | /// all of the overload candidates found by ADL to the overload |
| 7860 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7861 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7862 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
Richard Smith | f5cd5cc | 2012-02-25 06:24:24 +0000 | [diff] [blame] | 7863 | bool Operator, SourceLocation Loc, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 7864 | ArrayRef<Expr *> Args, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 7865 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7866 | OverloadCandidateSet& CandidateSet, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 7867 | bool PartialOverloading) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7868 | ADLResult Fns; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7869 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 7870 | // FIXME: This approach for uniquing ADL results (and removing |
| 7871 | // redundant candidates from the set) relies on pointer-equality, |
| 7872 | // which means we need to key off the canonical decl. However, |
| 7873 | // always going back to the canonical decl might not get us the |
| 7874 | // right set of default arguments. What default arguments are |
| 7875 | // we supposed to consider on ADL candidates, anyway? |
| 7876 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7877 | // FIXME: Pass in the explicit template arguments? |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 7878 | ArgumentDependentLookup(Name, Operator, Loc, Args, Fns); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7879 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7880 | // Erase all of the candidates we already knew about. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7881 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 7882 | CandEnd = CandidateSet.end(); |
| 7883 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7884 | if (Cand->Function) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7885 | Fns.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7886 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7887 | Fns.erase(FunTmpl); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7888 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7889 | |
| 7890 | // For each of the ADL candidates we found, add it to the overload |
| 7891 | // set. |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7892 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7893 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7894 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7895 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7896 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7897 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7898 | AddOverloadCandidate(FD, FoundDecl, Args, CandidateSet, false, |
| 7899 | PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7900 | } else |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7901 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7902 | FoundDecl, ExplicitTemplateArgs, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7903 | Args, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7904 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7905 | } |
| 7906 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7907 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 7908 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7909 | bool |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7910 | isBetterOverloadCandidate(Sema &S, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 7911 | const OverloadCandidate &Cand1, |
| 7912 | const OverloadCandidate &Cand2, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7913 | SourceLocation Loc, |
| 7914 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7915 | // Define viable functions to be better candidates than non-viable |
| 7916 | // functions. |
| 7917 | if (!Cand2.Viable) |
| 7918 | return Cand1.Viable; |
| 7919 | else if (!Cand1.Viable) |
| 7920 | return false; |
| 7921 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7922 | // C++ [over.match.best]p1: |
| 7923 | // |
| 7924 | // -- if F is a static member function, ICS1(F) is defined such |
| 7925 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 7926 | // any function G, and, symmetrically, ICS1(G) is neither |
| 7927 | // better nor worse than ICS1(F). |
| 7928 | unsigned StartArg = 0; |
| 7929 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 7930 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7931 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7932 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7933 | // A viable function F1 is defined to be a better function than another |
| 7934 | // 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] | 7935 | // conversion sequence than ICSi(F2), and then... |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 7936 | unsigned NumArgs = Cand1.NumConversions; |
| 7937 | assert(Cand2.NumConversions == NumArgs && "Overload candidate mismatch"); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7938 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7939 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7940 | switch (CompareImplicitConversionSequences(S, |
| 7941 | Cand1.Conversions[ArgIdx], |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7942 | Cand2.Conversions[ArgIdx])) { |
| 7943 | case ImplicitConversionSequence::Better: |
| 7944 | // Cand1 has a better conversion sequence. |
| 7945 | HasBetterConversion = true; |
| 7946 | break; |
| 7947 | |
| 7948 | case ImplicitConversionSequence::Worse: |
| 7949 | // Cand1 can't be better than Cand2. |
| 7950 | return false; |
| 7951 | |
| 7952 | case ImplicitConversionSequence::Indistinguishable: |
| 7953 | // Do nothing. |
| 7954 | break; |
| 7955 | } |
| 7956 | } |
| 7957 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7958 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7959 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7960 | if (HasBetterConversion) |
| 7961 | return true; |
| 7962 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7963 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7964 | // specialization, or, if not that, |
Douglas Gregor | ccd4713 | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 7965 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7966 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 7967 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7968 | |
| 7969 | // -- F1 and F2 are function template specializations, and the function |
| 7970 | // template for F1 is more specialized than the template for F2 |
| 7971 | // 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] | 7972 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 7973 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7974 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7975 | if (FunctionTemplateDecl *BetterTemplate |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7976 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 7977 | Cand2.Function->getPrimaryTemplate(), |
| 7978 | Loc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7979 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 7980 | : TPOC_Call, |
Richard Smith | 66118c2 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 7981 | Cand1.ExplicitCallArguments, |
| 7982 | Cand2.ExplicitCallArguments)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7983 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7984 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7985 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7986 | // -- the context is an initialization by user-defined conversion |
| 7987 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 7988 | // from the return type of F1 to the destination type (i.e., |
| 7989 | // the type of the entity being initialized) is a better |
| 7990 | // conversion sequence than the standard conversion sequence |
| 7991 | // from the return type of F2 to the destination type. |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7992 | if (UserDefinedConversion && Cand1.Function && Cand2.Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7993 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7994 | isa<CXXConversionDecl>(Cand2.Function)) { |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 7995 | // First check whether we prefer one of the conversion functions over the |
| 7996 | // other. This only distinguishes the results in non-standard, extension |
| 7997 | // cases such as the conversion from a lambda closure type to a function |
| 7998 | // pointer or block. |
| 7999 | ImplicitConversionSequence::CompareKind FuncResult |
| 8000 | = compareConversionFunctions(S, Cand1.Function, Cand2.Function); |
| 8001 | if (FuncResult != ImplicitConversionSequence::Indistinguishable) |
| 8002 | return FuncResult; |
| 8003 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8004 | switch (CompareStandardConversionSequences(S, |
| 8005 | Cand1.FinalConversion, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 8006 | Cand2.FinalConversion)) { |
| 8007 | case ImplicitConversionSequence::Better: |
| 8008 | // Cand1 has a better conversion sequence. |
| 8009 | return true; |
| 8010 | |
| 8011 | case ImplicitConversionSequence::Worse: |
| 8012 | // Cand1 can't be better than Cand2. |
| 8013 | return false; |
| 8014 | |
| 8015 | case ImplicitConversionSequence::Indistinguishable: |
| 8016 | // Do nothing |
| 8017 | break; |
| 8018 | } |
| 8019 | } |
| 8020 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8021 | return false; |
| 8022 | } |
| 8023 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8024 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8025 | /// within an overload candidate set. |
| 8026 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 8027 | /// \param Loc The location of the function name (or operator symbol) for |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8028 | /// which overload resolution occurs. |
| 8029 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 8030 | /// \param Best If overload resolution was successful or found a deleted |
| 8031 | /// function, \p Best points to the candidate function found. |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8032 | /// |
| 8033 | /// \returns The result of overload resolution. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8034 | OverloadingResult |
| 8035 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 8036 | iterator &Best, |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8037 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8038 | // Find the best viable function. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8039 | Best = end(); |
| 8040 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
| 8041 | if (Cand->Viable) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8042 | if (Best == end() || isBetterOverloadCandidate(S, *Cand, *Best, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8043 | UserDefinedConversion)) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8044 | Best = Cand; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8045 | } |
| 8046 | |
| 8047 | // If we didn't find any viable functions, abort. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8048 | if (Best == end()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8049 | return OR_No_Viable_Function; |
| 8050 | |
| 8051 | // Make sure that this function is better than every other viable |
| 8052 | // function. If not, we have an ambiguity. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8053 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8054 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8055 | Cand != Best && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8056 | !isBetterOverloadCandidate(S, *Best, *Cand, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8057 | UserDefinedConversion)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8058 | Best = end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8059 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8060 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8061 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8062 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8063 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8064 | if (Best->Function && |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 8065 | (Best->Function->isDeleted() || |
| 8066 | S.isFunctionConsideredUnavailable(Best->Function))) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8067 | return OR_Deleted; |
| 8068 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8069 | return OR_Success; |
| 8070 | } |
| 8071 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8072 | namespace { |
| 8073 | |
| 8074 | enum OverloadCandidateKind { |
| 8075 | oc_function, |
| 8076 | oc_method, |
| 8077 | oc_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8078 | oc_function_template, |
| 8079 | oc_method_template, |
| 8080 | oc_constructor_template, |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8081 | oc_implicit_default_constructor, |
| 8082 | oc_implicit_copy_constructor, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8083 | oc_implicit_move_constructor, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8084 | oc_implicit_copy_assignment, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8085 | oc_implicit_move_assignment, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8086 | oc_implicit_inherited_constructor |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8087 | }; |
| 8088 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8089 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 8090 | FunctionDecl *Fn, |
| 8091 | std::string &Description) { |
| 8092 | bool isTemplate = false; |
| 8093 | |
| 8094 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 8095 | isTemplate = true; |
| 8096 | Description = S.getTemplateArgumentBindingsText( |
| 8097 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 8098 | } |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8099 | |
| 8100 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8101 | if (!Ctor->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8102 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8103 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8104 | if (Ctor->getInheritedConstructor()) |
| 8105 | return oc_implicit_inherited_constructor; |
| 8106 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8107 | if (Ctor->isDefaultConstructor()) |
| 8108 | return oc_implicit_default_constructor; |
| 8109 | |
| 8110 | if (Ctor->isMoveConstructor()) |
| 8111 | return oc_implicit_move_constructor; |
| 8112 | |
| 8113 | assert(Ctor->isCopyConstructor() && |
| 8114 | "unexpected sort of implicit constructor"); |
| 8115 | return oc_implicit_copy_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8116 | } |
| 8117 | |
| 8118 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 8119 | // This actually gets spelled 'candidate function' for now, but |
| 8120 | // it doesn't hurt to split it out. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8121 | if (!Meth->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8122 | return isTemplate ? oc_method_template : oc_method; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8123 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8124 | if (Meth->isMoveAssignmentOperator()) |
| 8125 | return oc_implicit_move_assignment; |
| 8126 | |
Douglas Gregor | ef7d78b | 2012-02-10 08:36:38 +0000 | [diff] [blame] | 8127 | if (Meth->isCopyAssignmentOperator()) |
| 8128 | return oc_implicit_copy_assignment; |
| 8129 | |
| 8130 | assert(isa<CXXConversionDecl>(Meth) && "expected conversion"); |
| 8131 | return oc_method; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8132 | } |
| 8133 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8134 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8135 | } |
| 8136 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8137 | void MaybeEmitInheritedConstructorNote(Sema &S, Decl *Fn) { |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8138 | const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn); |
| 8139 | if (!Ctor) return; |
| 8140 | |
| 8141 | Ctor = Ctor->getInheritedConstructor(); |
| 8142 | if (!Ctor) return; |
| 8143 | |
| 8144 | S.Diag(Ctor->getLocation(), diag::note_ovl_candidate_inherited_constructor); |
| 8145 | } |
| 8146 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8147 | } // end anonymous namespace |
| 8148 | |
| 8149 | // Notes the location of an overload candidate. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8150 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn, QualType DestType) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8151 | std::string FnDesc; |
| 8152 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8153 | PartialDiagnostic PD = PDiag(diag::note_ovl_candidate) |
| 8154 | << (unsigned) K << FnDesc; |
| 8155 | HandleFunctionTypeMismatch(PD, Fn->getType(), DestType); |
| 8156 | Diag(Fn->getLocation(), PD); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8157 | MaybeEmitInheritedConstructorNote(*this, Fn); |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8158 | } |
| 8159 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8160 | //Notes the location of all overload candidates designated through |
| 8161 | // OverloadedExpr |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8162 | void Sema::NoteAllOverloadCandidates(Expr* OverloadedExpr, QualType DestType) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8163 | assert(OverloadedExpr->getType() == Context.OverloadTy); |
| 8164 | |
| 8165 | OverloadExpr::FindResult Ovl = OverloadExpr::find(OverloadedExpr); |
| 8166 | OverloadExpr *OvlExpr = Ovl.Expression; |
| 8167 | |
| 8168 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 8169 | IEnd = OvlExpr->decls_end(); |
| 8170 | I != IEnd; ++I) { |
| 8171 | if (FunctionTemplateDecl *FunTmpl = |
| 8172 | dyn_cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8173 | NoteOverloadCandidate(FunTmpl->getTemplatedDecl(), DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8174 | } else if (FunctionDecl *Fun |
| 8175 | = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8176 | NoteOverloadCandidate(Fun, DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8177 | } |
| 8178 | } |
| 8179 | } |
| 8180 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8181 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 8182 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 8183 | /// target types of the conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8184 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
| 8185 | Sema &S, |
| 8186 | SourceLocation CaretLoc, |
| 8187 | const PartialDiagnostic &PDiag) const { |
| 8188 | S.Diag(CaretLoc, PDiag) |
| 8189 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
Matt Beaumont-Gay | 45a37da | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 8190 | // FIXME: The note limiting machinery is borrowed from |
| 8191 | // OverloadCandidateSet::NoteCandidates; there's an opportunity for |
| 8192 | // refactoring here. |
| 8193 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
| 8194 | unsigned CandsShown = 0; |
| 8195 | AmbiguousConversionSequence::const_iterator I, E; |
| 8196 | for (I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
| 8197 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
| 8198 | break; |
| 8199 | ++CandsShown; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8200 | S.NoteOverloadCandidate(*I); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8201 | } |
Matt Beaumont-Gay | 45a37da | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 8202 | if (I != E) |
| 8203 | S.Diag(SourceLocation(), diag::note_ovl_too_many_candidates) << int(E - I); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8204 | } |
| 8205 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8206 | namespace { |
| 8207 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8208 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 8209 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 8210 | assert(Conv.isBad()); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8211 | assert(Cand->Function && "for now, candidate must be a function"); |
| 8212 | FunctionDecl *Fn = Cand->Function; |
| 8213 | |
| 8214 | // There's a conversion slot for the object argument if this is a |
| 8215 | // non-constructor method. Note that 'I' corresponds the |
| 8216 | // conversion-slot index. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8217 | bool isObjectArgument = false; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8218 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8219 | if (I == 0) |
| 8220 | isObjectArgument = true; |
| 8221 | else |
| 8222 | I--; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8223 | } |
| 8224 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8225 | std::string FnDesc; |
| 8226 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 8227 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8228 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 8229 | QualType FromTy = Conv.Bad.getFromType(); |
| 8230 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8231 | |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8232 | if (FromTy == S.Context.OverloadTy) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8233 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8234 | Expr *E = FromExpr->IgnoreParens(); |
| 8235 | if (isa<UnaryOperator>(E)) |
| 8236 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 8237 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8238 | |
| 8239 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 8240 | << (unsigned) FnKind << FnDesc |
| 8241 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8242 | << ToTy << Name << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8243 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8244 | return; |
| 8245 | } |
| 8246 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8247 | // Do some hand-waving analysis to see if the non-viability is due |
| 8248 | // to a qualifier mismatch. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8249 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 8250 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 8251 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 8252 | CToTy = RT->getPointeeType(); |
| 8253 | else { |
| 8254 | // TODO: detect and diagnose the full richness of const mismatches. |
| 8255 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 8256 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 8257 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 8258 | } |
| 8259 | |
| 8260 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 8261 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8262 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8263 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8264 | |
| 8265 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 8266 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 8267 | << (unsigned) FnKind << FnDesc |
| 8268 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8269 | << FromTy |
| 8270 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 8271 | << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8272 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8273 | return; |
| 8274 | } |
| 8275 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8276 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 8277 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8278 | << (unsigned) FnKind << FnDesc |
| 8279 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8280 | << FromTy |
| 8281 | << FromQs.getObjCLifetime() << ToQs.getObjCLifetime() |
| 8282 | << (unsigned) isObjectArgument << I+1; |
| 8283 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8284 | return; |
| 8285 | } |
| 8286 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 8287 | if (FromQs.getObjCGCAttr() != ToQs.getObjCGCAttr()) { |
| 8288 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_gc) |
| 8289 | << (unsigned) FnKind << FnDesc |
| 8290 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8291 | << FromTy |
| 8292 | << FromQs.getObjCGCAttr() << ToQs.getObjCGCAttr() |
| 8293 | << (unsigned) isObjectArgument << I+1; |
| 8294 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8295 | return; |
| 8296 | } |
| 8297 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8298 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 8299 | assert(CVR && "unexpected qualifiers mismatch"); |
| 8300 | |
| 8301 | if (isObjectArgument) { |
| 8302 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 8303 | << (unsigned) FnKind << FnDesc |
| 8304 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8305 | << FromTy << (CVR - 1); |
| 8306 | } else { |
| 8307 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 8308 | << (unsigned) FnKind << FnDesc |
| 8309 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8310 | << FromTy << (CVR - 1) << I+1; |
| 8311 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8312 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8313 | return; |
| 8314 | } |
| 8315 | |
Sebastian Redl | fd2a00a | 2011-09-24 17:48:32 +0000 | [diff] [blame] | 8316 | // Special diagnostic for failure to convert an initializer list, since |
| 8317 | // telling the user that it has type void is not useful. |
| 8318 | if (FromExpr && isa<InitListExpr>(FromExpr)) { |
| 8319 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_list_argument) |
| 8320 | << (unsigned) FnKind << FnDesc |
| 8321 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8322 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8323 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8324 | return; |
| 8325 | } |
| 8326 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8327 | // Diagnose references or pointers to incomplete types differently, |
| 8328 | // since it's far from impossible that the incompleteness triggered |
| 8329 | // the failure. |
| 8330 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 8331 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 8332 | TempFromTy = PTy->getPointeeType(); |
| 8333 | if (TempFromTy->isIncompleteType()) { |
| 8334 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 8335 | << (unsigned) FnKind << FnDesc |
| 8336 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8337 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8338 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8339 | return; |
| 8340 | } |
| 8341 | |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8342 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8343 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8344 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 8345 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 8346 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8347 | FromPtrTy->getPointeeType()) && |
| 8348 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 8349 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8350 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8351 | FromPtrTy->getPointeeType())) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8352 | BaseToDerivedConversion = 1; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8353 | } |
| 8354 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 8355 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 8356 | if (const ObjCObjectPointerType *ToPtrTy |
| 8357 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 8358 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 8359 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 8360 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8361 | FromPtrTy->getPointeeType()) && |
| 8362 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8363 | BaseToDerivedConversion = 2; |
| 8364 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8365 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 8366 | !FromTy->isIncompleteType() && |
| 8367 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 8368 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) { |
| 8369 | BaseToDerivedConversion = 3; |
| 8370 | } else if (ToTy->isLValueReferenceType() && !FromExpr->isLValue() && |
| 8371 | ToTy.getNonReferenceType().getCanonicalType() == |
| 8372 | FromTy.getNonReferenceType().getCanonicalType()) { |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8373 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_lvalue) |
| 8374 | << (unsigned) FnKind << FnDesc |
| 8375 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8376 | << (unsigned) isObjectArgument << I + 1; |
| 8377 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8378 | return; |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8379 | } |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8380 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8381 | |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8382 | if (BaseToDerivedConversion) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8383 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8384 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8385 | << (unsigned) FnKind << FnDesc |
| 8386 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8387 | << (BaseToDerivedConversion - 1) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8388 | << FromTy << ToTy << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8389 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8390 | return; |
| 8391 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8392 | |
Fariborz Jahanian | 909bcb3 | 2011-07-20 17:14:09 +0000 | [diff] [blame] | 8393 | if (isa<ObjCObjectPointerType>(CFromTy) && |
| 8394 | isa<PointerType>(CToTy)) { |
| 8395 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8396 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8397 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
| 8398 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_arc_conv) |
| 8399 | << (unsigned) FnKind << FnDesc |
| 8400 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8401 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8402 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8403 | return; |
| 8404 | } |
| 8405 | } |
| 8406 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8407 | // Emit the generic diagnostic and, optionally, add the hints to it. |
| 8408 | PartialDiagnostic FDiag = S.PDiag(diag::note_ovl_candidate_bad_conv); |
| 8409 | FDiag << (unsigned) FnKind << FnDesc |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8410 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8411 | << FromTy << ToTy << (unsigned) isObjectArgument << I + 1 |
| 8412 | << (unsigned) (Cand->Fix.Kind); |
| 8413 | |
| 8414 | // If we can fix the conversion, suggest the FixIts. |
Benjamin Kramer | 1136ef0 | 2012-01-14 21:05:10 +0000 | [diff] [blame] | 8415 | for (std::vector<FixItHint>::iterator HI = Cand->Fix.Hints.begin(), |
| 8416 | HE = Cand->Fix.Hints.end(); HI != HE; ++HI) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8417 | FDiag << *HI; |
| 8418 | S.Diag(Fn->getLocation(), FDiag); |
| 8419 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8420 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8421 | } |
| 8422 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8423 | /// Additional arity mismatch diagnosis specific to a function overload |
| 8424 | /// candidates. This is not covered by the more general DiagnoseArityMismatch() |
| 8425 | /// over a candidate in any candidate set. |
| 8426 | bool CheckArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 8427 | unsigned NumArgs) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8428 | FunctionDecl *Fn = Cand->Function; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8429 | unsigned MinParams = Fn->getMinRequiredArguments(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8430 | |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8431 | // With invalid overloaded operators, it's possible that we think we |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8432 | // 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] | 8433 | // right number of arguments, because only overloaded operators have |
| 8434 | // the weird behavior of overloading member and non-member functions. |
| 8435 | // Just don't report anything. |
| 8436 | if (Fn->isInvalidDecl() && |
| 8437 | Fn->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8438 | return true; |
| 8439 | |
| 8440 | if (NumArgs < MinParams) { |
| 8441 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 8442 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8443 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
| 8444 | } else { |
| 8445 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 8446 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8447 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
| 8448 | } |
| 8449 | |
| 8450 | return false; |
| 8451 | } |
| 8452 | |
| 8453 | /// General arity mismatch diagnosis over a candidate in a candidate set. |
| 8454 | void DiagnoseArityMismatch(Sema &S, Decl *D, unsigned NumFormalArgs) { |
| 8455 | assert(isa<FunctionDecl>(D) && |
| 8456 | "The templated declaration should at least be a function" |
| 8457 | " when diagnosing bad template argument deduction due to too many" |
| 8458 | " or too few arguments"); |
| 8459 | |
| 8460 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| 8461 | |
| 8462 | // TODO: treat calls to a missing default constructor as a special case |
| 8463 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 8464 | unsigned MinParams = Fn->getMinRequiredArguments(); |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8465 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8466 | // at least / at most / exactly |
| 8467 | unsigned mode, modeCount; |
| 8468 | if (NumFormalArgs < MinParams) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8469 | if (MinParams != FnTy->getNumArgs() || |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 8470 | FnTy->isVariadic() || FnTy->isTemplateVariadic()) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8471 | mode = 0; // "at least" |
| 8472 | else |
| 8473 | mode = 2; // "exactly" |
| 8474 | modeCount = MinParams; |
| 8475 | } else { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8476 | if (MinParams != FnTy->getNumArgs()) |
| 8477 | mode = 1; // "at most" |
| 8478 | else |
| 8479 | mode = 2; // "exactly" |
| 8480 | modeCount = FnTy->getNumArgs(); |
| 8481 | } |
| 8482 | |
| 8483 | std::string Description; |
| 8484 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 8485 | |
Richard Smith | f7b8056 | 2012-05-11 05:16:41 +0000 | [diff] [blame] | 8486 | if (modeCount == 1 && Fn->getParamDecl(0)->getDeclName()) |
| 8487 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity_one) |
| 8488 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 8489 | << Fn->getParamDecl(0) << NumFormalArgs; |
| 8490 | else |
| 8491 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
| 8492 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 8493 | << modeCount << NumFormalArgs; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8494 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8495 | } |
| 8496 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8497 | /// Arity mismatch diagnosis specific to a function overload candidate. |
| 8498 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 8499 | unsigned NumFormalArgs) { |
| 8500 | if (!CheckArityMismatch(S, Cand, NumFormalArgs)) |
| 8501 | DiagnoseArityMismatch(S, Cand->Function, NumFormalArgs); |
| 8502 | } |
Larisse Voufo | 8c5d407 | 2013-07-19 22:53:23 +0000 | [diff] [blame] | 8503 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8504 | TemplateDecl *getDescribedTemplate(Decl *Templated) { |
| 8505 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Templated)) |
| 8506 | return FD->getDescribedFunctionTemplate(); |
| 8507 | else if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Templated)) |
| 8508 | return RD->getDescribedClassTemplate(); |
| 8509 | |
| 8510 | llvm_unreachable("Unsupported: Getting the described template declaration" |
| 8511 | " for bad deduction diagnosis"); |
| 8512 | } |
| 8513 | |
| 8514 | /// Diagnose a failed template-argument deduction. |
| 8515 | void DiagnoseBadDeduction(Sema &S, Decl *Templated, |
| 8516 | DeductionFailureInfo &DeductionFailure, |
| 8517 | unsigned NumArgs) { |
| 8518 | TemplateParameter Param = DeductionFailure.getTemplateParameter(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8519 | NamedDecl *ParamD; |
| 8520 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 8521 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 8522 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8523 | switch (DeductionFailure.Result) { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8524 | case Sema::TDK_Success: |
| 8525 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 8526 | |
| 8527 | case Sema::TDK_Incomplete: { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8528 | assert(ParamD && "no parameter found for incomplete deduction result"); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8529 | S.Diag(Templated->getLocation(), |
| 8530 | diag::note_ovl_candidate_incomplete_deduction) |
| 8531 | << ParamD->getDeclName(); |
| 8532 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8533 | return; |
| 8534 | } |
| 8535 | |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8536 | case Sema::TDK_Underqualified: { |
| 8537 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 8538 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 8539 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8540 | QualType Param = DeductionFailure.getFirstArg()->getAsType(); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8541 | |
| 8542 | // Param will have been canonicalized, but it should just be a |
| 8543 | // qualified version of ParamD, so move the qualifiers to that. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 8544 | QualifierCollector Qs; |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8545 | Qs.strip(Param); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 8546 | QualType NonCanonParam = Qs.apply(S.Context, TParam->getTypeForDecl()); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8547 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 8548 | |
| 8549 | // Arg has also been canonicalized, but there's nothing we can do |
| 8550 | // about that. It also doesn't matter as much, because it won't |
| 8551 | // have any template parameters in it (because deduction isn't |
| 8552 | // done on dependent types). |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8553 | QualType Arg = DeductionFailure.getSecondArg()->getAsType(); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8554 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8555 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_underqualified) |
| 8556 | << ParamD->getDeclName() << Arg << NonCanonParam; |
| 8557 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8558 | return; |
| 8559 | } |
| 8560 | |
| 8561 | case Sema::TDK_Inconsistent: { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8562 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8563 | int which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8564 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8565 | which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8566 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8567 | which = 1; |
| 8568 | else { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8569 | which = 2; |
| 8570 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8571 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8572 | S.Diag(Templated->getLocation(), |
| 8573 | diag::note_ovl_candidate_inconsistent_deduction) |
| 8574 | << which << ParamD->getDeclName() << *DeductionFailure.getFirstArg() |
| 8575 | << *DeductionFailure.getSecondArg(); |
| 8576 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8577 | return; |
| 8578 | } |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8579 | |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8580 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8581 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8582 | if (ParamD->getDeclName()) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8583 | S.Diag(Templated->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8584 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8585 | << ParamD->getDeclName(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8586 | else { |
| 8587 | int index = 0; |
| 8588 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 8589 | index = TTP->getIndex(); |
| 8590 | else if (NonTypeTemplateParmDecl *NTTP |
| 8591 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 8592 | index = NTTP->getIndex(); |
| 8593 | else |
| 8594 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8595 | S.Diag(Templated->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8596 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8597 | << (index + 1); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8598 | } |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8599 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8600 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8601 | |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8602 | case Sema::TDK_TooManyArguments: |
| 8603 | case Sema::TDK_TooFewArguments: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8604 | DiagnoseArityMismatch(S, Templated, NumArgs); |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8605 | return; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8606 | |
| 8607 | case Sema::TDK_InstantiationDepth: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8608 | S.Diag(Templated->getLocation(), |
| 8609 | diag::note_ovl_candidate_instantiation_depth); |
| 8610 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8611 | return; |
| 8612 | |
| 8613 | case Sema::TDK_SubstitutionFailure: { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8614 | // Format the template argument list into the argument string. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8615 | SmallString<128> TemplateArgString; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8616 | if (TemplateArgumentList *Args = |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8617 | DeductionFailure.getTemplateArgumentList()) { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8618 | TemplateArgString = " "; |
| 8619 | TemplateArgString += S.getTemplateArgumentBindingsText( |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8620 | getDescribedTemplate(Templated)->getTemplateParameters(), *Args); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8621 | } |
| 8622 | |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8623 | // If this candidate was disabled by enable_if, say so. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8624 | PartialDiagnosticAt *PDiag = DeductionFailure.getSFINAEDiagnostic(); |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8625 | if (PDiag && PDiag->second.getDiagID() == |
| 8626 | diag::err_typename_nested_not_found_enable_if) { |
| 8627 | // FIXME: Use the source range of the condition, and the fully-qualified |
| 8628 | // name of the enable_if template. These are both present in PDiag. |
| 8629 | S.Diag(PDiag->first, diag::note_ovl_candidate_disabled_by_enable_if) |
| 8630 | << "'enable_if'" << TemplateArgString; |
| 8631 | return; |
| 8632 | } |
| 8633 | |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8634 | // Format the SFINAE diagnostic into the argument string. |
| 8635 | // FIXME: Add a general mechanism to include a PartialDiagnostic *'s |
| 8636 | // formatted message in another diagnostic. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8637 | SmallString<128> SFINAEArgString; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8638 | SourceRange R; |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8639 | if (PDiag) { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8640 | SFINAEArgString = ": "; |
| 8641 | R = SourceRange(PDiag->first, PDiag->first); |
| 8642 | PDiag->second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 8643 | } |
| 8644 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8645 | S.Diag(Templated->getLocation(), |
| 8646 | diag::note_ovl_candidate_substitution_failure) |
| 8647 | << TemplateArgString << SFINAEArgString << R; |
| 8648 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8649 | return; |
| 8650 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8651 | |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8652 | case Sema::TDK_FailedOverloadResolution: { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8653 | OverloadExpr::FindResult R = OverloadExpr::find(DeductionFailure.getExpr()); |
| 8654 | S.Diag(Templated->getLocation(), |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8655 | diag::note_ovl_candidate_failed_overload_resolution) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8656 | << R.Expression->getName(); |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8657 | return; |
| 8658 | } |
| 8659 | |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8660 | case Sema::TDK_NonDeducedMismatch: { |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8661 | // FIXME: Provide a source location to indicate what we couldn't match. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8662 | TemplateArgument FirstTA = *DeductionFailure.getFirstArg(); |
| 8663 | TemplateArgument SecondTA = *DeductionFailure.getSecondArg(); |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8664 | if (FirstTA.getKind() == TemplateArgument::Template && |
| 8665 | SecondTA.getKind() == TemplateArgument::Template) { |
| 8666 | TemplateName FirstTN = FirstTA.getAsTemplate(); |
| 8667 | TemplateName SecondTN = SecondTA.getAsTemplate(); |
| 8668 | if (FirstTN.getKind() == TemplateName::Template && |
| 8669 | SecondTN.getKind() == TemplateName::Template) { |
| 8670 | if (FirstTN.getAsTemplateDecl()->getName() == |
| 8671 | SecondTN.getAsTemplateDecl()->getName()) { |
| 8672 | // FIXME: This fixes a bad diagnostic where both templates are named |
| 8673 | // the same. This particular case is a bit difficult since: |
| 8674 | // 1) It is passed as a string to the diagnostic printer. |
| 8675 | // 2) The diagnostic printer only attempts to find a better |
| 8676 | // name for types, not decls. |
| 8677 | // Ideally, this should folded into the diagnostic printer. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8678 | S.Diag(Templated->getLocation(), |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8679 | diag::note_ovl_candidate_non_deduced_mismatch_qualified) |
| 8680 | << FirstTN.getAsTemplateDecl() << SecondTN.getAsTemplateDecl(); |
| 8681 | return; |
| 8682 | } |
| 8683 | } |
| 8684 | } |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8685 | S.Diag(Templated->getLocation(), |
| 8686 | diag::note_ovl_candidate_non_deduced_mismatch) |
| 8687 | << FirstTA << SecondTA; |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8688 | return; |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8689 | } |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8690 | // TODO: diagnose these individually, then kill off |
| 8691 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8692 | case Sema::TDK_MiscellaneousDeductionFailure: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8693 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_bad_deduction); |
| 8694 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8695 | return; |
| 8696 | } |
| 8697 | } |
| 8698 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8699 | /// Diagnose a failed template-argument deduction, for function calls. |
| 8700 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, unsigned NumArgs) { |
| 8701 | unsigned TDK = Cand->DeductionFailure.Result; |
| 8702 | if (TDK == Sema::TDK_TooFewArguments || TDK == Sema::TDK_TooManyArguments) { |
| 8703 | if (CheckArityMismatch(S, Cand, NumArgs)) |
| 8704 | return; |
| 8705 | } |
| 8706 | DiagnoseBadDeduction(S, Cand->Function, // pattern |
| 8707 | Cand->DeductionFailure, NumArgs); |
| 8708 | } |
| 8709 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8710 | /// CUDA: diagnose an invalid call across targets. |
| 8711 | void DiagnoseBadTarget(Sema &S, OverloadCandidate *Cand) { |
| 8712 | FunctionDecl *Caller = cast<FunctionDecl>(S.CurContext); |
| 8713 | FunctionDecl *Callee = Cand->Function; |
| 8714 | |
| 8715 | Sema::CUDAFunctionTarget CallerTarget = S.IdentifyCUDATarget(Caller), |
| 8716 | CalleeTarget = S.IdentifyCUDATarget(Callee); |
| 8717 | |
| 8718 | std::string FnDesc; |
| 8719 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Callee, FnDesc); |
| 8720 | |
| 8721 | S.Diag(Callee->getLocation(), diag::note_ovl_candidate_bad_target) |
| 8722 | << (unsigned) FnKind << CalleeTarget << CallerTarget; |
| 8723 | } |
| 8724 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8725 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 8726 | /// already generated a primary error at the call site. |
| 8727 | /// |
| 8728 | /// It really does need to be a single diagnostic with its caret |
| 8729 | /// pointed at the candidate declaration. Yes, this creates some |
| 8730 | /// major challenges of technical writing. Yes, this makes pointing |
| 8731 | /// out problems with specific arguments quite awkward. It's still |
| 8732 | /// better than generating twenty screens of text for every failed |
| 8733 | /// overload. |
| 8734 | /// |
| 8735 | /// It would be great to be able to express per-candidate problems |
| 8736 | /// more richly for those diagnostic clients that cared, but we'd |
| 8737 | /// still have to be just as careful with the default diagnostics. |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8738 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8739 | unsigned NumArgs) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8740 | FunctionDecl *Fn = Cand->Function; |
| 8741 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8742 | // Note deleted candidates, but only if they're viable. |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 8743 | if (Cand->Viable && (Fn->isDeleted() || |
| 8744 | S.isFunctionConsideredUnavailable(Fn))) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8745 | std::string FnDesc; |
| 8746 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8747 | |
| 8748 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 8749 | << FnKind << FnDesc |
| 8750 | << (Fn->isDeleted() ? (Fn->isDeletedAsWritten() ? 1 : 2) : 0); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8751 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8752 | return; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8753 | } |
| 8754 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8755 | // We don't really have anything else to say about viable candidates. |
| 8756 | if (Cand->Viable) { |
| 8757 | S.NoteOverloadCandidate(Fn); |
| 8758 | return; |
| 8759 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8760 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8761 | switch (Cand->FailureKind) { |
| 8762 | case ovl_fail_too_many_arguments: |
| 8763 | case ovl_fail_too_few_arguments: |
| 8764 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8765 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8766 | case ovl_fail_bad_deduction: |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8767 | return DiagnoseBadDeduction(S, Cand, NumArgs); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8768 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8769 | case ovl_fail_trivial_conversion: |
| 8770 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 8771 | case ovl_fail_final_conversion_not_exact: |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8772 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8773 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8774 | case ovl_fail_bad_conversion: { |
| 8775 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8776 | for (unsigned N = Cand->NumConversions; I != N; ++I) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8777 | if (Cand->Conversions[I].isBad()) |
| 8778 | return DiagnoseBadConversion(S, Cand, I); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8779 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8780 | // FIXME: this currently happens when we're called from SemaInit |
| 8781 | // when user-conversion overload fails. Figure out how to handle |
| 8782 | // those conditions and diagnose them well. |
| 8783 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8784 | } |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8785 | |
| 8786 | case ovl_fail_bad_target: |
| 8787 | return DiagnoseBadTarget(S, Cand); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8788 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8789 | } |
| 8790 | |
| 8791 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 8792 | // Desugar the type of the surrogate down to a function type, |
| 8793 | // retaining as many typedefs as possible while still showing |
| 8794 | // the function type (and, therefore, its parameter types). |
| 8795 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 8796 | bool isLValueReference = false; |
| 8797 | bool isRValueReference = false; |
| 8798 | bool isPointer = false; |
| 8799 | if (const LValueReferenceType *FnTypeRef = |
| 8800 | FnType->getAs<LValueReferenceType>()) { |
| 8801 | FnType = FnTypeRef->getPointeeType(); |
| 8802 | isLValueReference = true; |
| 8803 | } else if (const RValueReferenceType *FnTypeRef = |
| 8804 | FnType->getAs<RValueReferenceType>()) { |
| 8805 | FnType = FnTypeRef->getPointeeType(); |
| 8806 | isRValueReference = true; |
| 8807 | } |
| 8808 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 8809 | FnType = FnTypePtr->getPointeeType(); |
| 8810 | isPointer = true; |
| 8811 | } |
| 8812 | // Desugar down to a function type. |
| 8813 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 8814 | // Reconstruct the pointer/reference as appropriate. |
| 8815 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 8816 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 8817 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 8818 | |
| 8819 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 8820 | << FnType; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8821 | MaybeEmitInheritedConstructorNote(S, Cand->Surrogate); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8822 | } |
| 8823 | |
| 8824 | void NoteBuiltinOperatorCandidate(Sema &S, |
David Blaikie | 0bea863 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 8825 | StringRef Opc, |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8826 | SourceLocation OpLoc, |
| 8827 | OverloadCandidate *Cand) { |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8828 | assert(Cand->NumConversions <= 2 && "builtin operator is not binary"); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8829 | std::string TypeStr("operator"); |
| 8830 | TypeStr += Opc; |
| 8831 | TypeStr += "("; |
| 8832 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8833 | if (Cand->NumConversions == 1) { |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8834 | TypeStr += ")"; |
| 8835 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 8836 | } else { |
| 8837 | TypeStr += ", "; |
| 8838 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 8839 | TypeStr += ")"; |
| 8840 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 8841 | } |
| 8842 | } |
| 8843 | |
| 8844 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 8845 | OverloadCandidate *Cand) { |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8846 | unsigned NoOperands = Cand->NumConversions; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8847 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 8848 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8849 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 8850 | if (!ICS.isAmbiguous()) continue; |
| 8851 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8852 | ICS.DiagnoseAmbiguousConversion(S, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8853 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8854 | } |
| 8855 | } |
| 8856 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8857 | static SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8858 | if (Cand->Function) |
| 8859 | return Cand->Function->getLocation(); |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 8860 | if (Cand->IsSurrogate) |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8861 | return Cand->Surrogate->getLocation(); |
| 8862 | return SourceLocation(); |
| 8863 | } |
| 8864 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8865 | static unsigned RankDeductionFailure(const DeductionFailureInfo &DFI) { |
Chandler Carruth | 78bf680 | 2011-09-10 00:51:24 +0000 | [diff] [blame] | 8866 | switch ((Sema::TemplateDeductionResult)DFI.Result) { |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8867 | case Sema::TDK_Success: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8868 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8869 | |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 8870 | case Sema::TDK_Invalid: |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8871 | case Sema::TDK_Incomplete: |
| 8872 | return 1; |
| 8873 | |
| 8874 | case Sema::TDK_Underqualified: |
| 8875 | case Sema::TDK_Inconsistent: |
| 8876 | return 2; |
| 8877 | |
| 8878 | case Sema::TDK_SubstitutionFailure: |
| 8879 | case Sema::TDK_NonDeducedMismatch: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8880 | case Sema::TDK_MiscellaneousDeductionFailure: |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8881 | return 3; |
| 8882 | |
| 8883 | case Sema::TDK_InstantiationDepth: |
| 8884 | case Sema::TDK_FailedOverloadResolution: |
| 8885 | return 4; |
| 8886 | |
| 8887 | case Sema::TDK_InvalidExplicitArguments: |
| 8888 | return 5; |
| 8889 | |
| 8890 | case Sema::TDK_TooManyArguments: |
| 8891 | case Sema::TDK_TooFewArguments: |
| 8892 | return 6; |
| 8893 | } |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8894 | llvm_unreachable("Unhandled deduction result"); |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8895 | } |
| 8896 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8897 | struct CompareOverloadCandidatesForDisplay { |
| 8898 | Sema &S; |
| 8899 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8900 | |
| 8901 | bool operator()(const OverloadCandidate *L, |
| 8902 | const OverloadCandidate *R) { |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 8903 | // Fast-path this check. |
| 8904 | if (L == R) return false; |
| 8905 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8906 | // Order first by viability. |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8907 | if (L->Viable) { |
| 8908 | if (!R->Viable) return true; |
| 8909 | |
| 8910 | // TODO: introduce a tri-valued comparison for overload |
| 8911 | // candidates. Would be more worthwhile if we had a sort |
| 8912 | // that could exploit it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8913 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation())) return true; |
| 8914 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation())) return false; |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8915 | } else if (R->Viable) |
| 8916 | return false; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8917 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8918 | assert(L->Viable == R->Viable); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8919 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8920 | // Criteria by which we can sort non-viable candidates: |
| 8921 | if (!L->Viable) { |
| 8922 | // 1. Arity mismatches come after other candidates. |
| 8923 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 8924 | L->FailureKind == ovl_fail_too_few_arguments) |
| 8925 | return false; |
| 8926 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 8927 | R->FailureKind == ovl_fail_too_few_arguments) |
| 8928 | return true; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8929 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8930 | // 2. Bad conversions come first and are ordered by the number |
| 8931 | // of bad conversions and quality of good conversions. |
| 8932 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 8933 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 8934 | return true; |
| 8935 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8936 | // The conversion that can be fixed with a smaller number of changes, |
| 8937 | // comes first. |
| 8938 | unsigned numLFixes = L->Fix.NumConversionsFixed; |
| 8939 | unsigned numRFixes = R->Fix.NumConversionsFixed; |
| 8940 | numLFixes = (numLFixes == 0) ? UINT_MAX : numLFixes; |
| 8941 | numRFixes = (numRFixes == 0) ? UINT_MAX : numRFixes; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 8942 | if (numLFixes != numRFixes) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8943 | if (numLFixes < numRFixes) |
| 8944 | return true; |
| 8945 | else |
| 8946 | return false; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 8947 | } |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8948 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8949 | // If there's any ordering between the defined conversions... |
| 8950 | // FIXME: this might not be transitive. |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8951 | assert(L->NumConversions == R->NumConversions); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8952 | |
| 8953 | int leftBetter = 0; |
John McCall | 3a81337 | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 8954 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8955 | for (unsigned E = L->NumConversions; I != E; ++I) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8956 | switch (CompareImplicitConversionSequences(S, |
| 8957 | L->Conversions[I], |
| 8958 | R->Conversions[I])) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8959 | case ImplicitConversionSequence::Better: |
| 8960 | leftBetter++; |
| 8961 | break; |
| 8962 | |
| 8963 | case ImplicitConversionSequence::Worse: |
| 8964 | leftBetter--; |
| 8965 | break; |
| 8966 | |
| 8967 | case ImplicitConversionSequence::Indistinguishable: |
| 8968 | break; |
| 8969 | } |
| 8970 | } |
| 8971 | if (leftBetter > 0) return true; |
| 8972 | if (leftBetter < 0) return false; |
| 8973 | |
| 8974 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 8975 | return false; |
| 8976 | |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8977 | if (L->FailureKind == ovl_fail_bad_deduction) { |
| 8978 | if (R->FailureKind != ovl_fail_bad_deduction) |
| 8979 | return true; |
| 8980 | |
| 8981 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 8982 | return RankDeductionFailure(L->DeductionFailure) |
Eli Friedman | ce1846e | 2011-10-14 23:10:30 +0000 | [diff] [blame] | 8983 | < RankDeductionFailure(R->DeductionFailure); |
Eli Friedman | 1c522f7 | 2011-10-14 21:52:24 +0000 | [diff] [blame] | 8984 | } else if (R->FailureKind == ovl_fail_bad_deduction) |
| 8985 | return false; |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8986 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8987 | // TODO: others? |
| 8988 | } |
| 8989 | |
| 8990 | // Sort everything else by location. |
| 8991 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 8992 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 8993 | |
| 8994 | // Put candidates without locations (e.g. builtins) at the end. |
| 8995 | if (LLoc.isInvalid()) return false; |
| 8996 | if (RLoc.isInvalid()) return true; |
| 8997 | |
| 8998 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8999 | } |
| 9000 | }; |
| 9001 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9002 | /// CompleteNonViableCandidate - Normally, overload resolution only |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9003 | /// computes up to the first. Produces the FixIt set if possible. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9004 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9005 | ArrayRef<Expr *> Args) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9006 | assert(!Cand->Viable); |
| 9007 | |
| 9008 | // Don't do anything on failures other than bad conversion. |
| 9009 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 9010 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9011 | // We only want the FixIts if all the arguments can be corrected. |
| 9012 | bool Unfixable = false; |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9013 | // Use a implicit copy initialization to check conversion fixes. |
| 9014 | Cand->Fix.setConversionChecker(TryCopyInitialization); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9015 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9016 | // Skip forward to the first bad conversion. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9017 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9018 | unsigned ConvCount = Cand->NumConversions; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9019 | while (true) { |
| 9020 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 9021 | ConvIdx++; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9022 | if (Cand->Conversions[ConvIdx - 1].isBad()) { |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9023 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx - 1, S); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9024 | break; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9025 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9026 | } |
| 9027 | |
| 9028 | if (ConvIdx == ConvCount) |
| 9029 | return; |
| 9030 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9031 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 9032 | "remaining conversion is initialized?"); |
| 9033 | |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 9034 | // FIXME: this should probably be preserved from the overload |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9035 | // operation somehow. |
| 9036 | bool SuppressUserConversions = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9037 | |
| 9038 | const FunctionProtoType* Proto; |
| 9039 | unsigned ArgIdx = ConvIdx; |
| 9040 | |
| 9041 | if (Cand->IsSurrogate) { |
| 9042 | QualType ConvType |
| 9043 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 9044 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 9045 | ConvType = ConvPtrType->getPointeeType(); |
| 9046 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 9047 | ArgIdx--; |
| 9048 | } else if (Cand->Function) { |
| 9049 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 9050 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 9051 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 9052 | ArgIdx--; |
| 9053 | } else { |
| 9054 | // Builtin binary operator with a bad first conversion. |
| 9055 | assert(ConvCount <= 3); |
| 9056 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 9057 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 9058 | = TryCopyInitialization(S, Args[ConvIdx], |
| 9059 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9060 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9061 | /*InOverloadResolution*/ true, |
| 9062 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9063 | S.getLangOpts().ObjCAutoRefCount); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9064 | return; |
| 9065 | } |
| 9066 | |
| 9067 | // Fill in the rest of the conversions. |
| 9068 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 9069 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9070 | if (ArgIdx < NumArgsInProto) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9071 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 9072 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9073 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9074 | /*InOverloadResolution=*/true, |
| 9075 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9076 | S.getLangOpts().ObjCAutoRefCount); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9077 | // Store the FixIt in the candidate if it exists. |
| 9078 | if (!Unfixable && Cand->Conversions[ConvIdx].isBad()) |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9079 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx, S); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9080 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9081 | else |
| 9082 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 9083 | } |
| 9084 | } |
| 9085 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9086 | } // end anonymous namespace |
| 9087 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9088 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 9089 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9090 | /// set. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9091 | void OverloadCandidateSet::NoteCandidates(Sema &S, |
| 9092 | OverloadCandidateDisplayKind OCD, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9093 | ArrayRef<Expr *> Args, |
David Blaikie | 0bea863 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 9094 | StringRef Opc, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9095 | SourceLocation OpLoc) { |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9096 | // Sort the candidates by viability and position. Sorting directly would |
| 9097 | // be prohibitive, so we make a set of pointers and sort those. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9098 | SmallVector<OverloadCandidate*, 32> Cands; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9099 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
| 9100 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9101 | if (Cand->Viable) |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9102 | Cands.push_back(Cand); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9103 | else if (OCD == OCD_AllCandidates) { |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9104 | CompleteNonViableCandidate(S, Cand, Args); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9105 | if (Cand->Function || Cand->IsSurrogate) |
| 9106 | Cands.push_back(Cand); |
| 9107 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 9108 | // want to list every possible builtin candidate. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9109 | } |
| 9110 | } |
| 9111 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 9112 | std::sort(Cands.begin(), Cands.end(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9113 | CompareOverloadCandidatesForDisplay(S)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9114 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9115 | bool ReportedAmbiguousConversions = false; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9116 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9117 | SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 9118 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9119 | unsigned CandsShown = 0; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9120 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 9121 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 9122 | |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9123 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 9124 | // the user with. FIXME: This limit should depend on details of the |
| 9125 | // candidate list. |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 9126 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) { |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9127 | break; |
| 9128 | } |
| 9129 | ++CandsShown; |
| 9130 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9131 | if (Cand->Function) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9132 | NoteFunctionCandidate(S, Cand, Args.size()); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9133 | else if (Cand->IsSurrogate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9134 | NoteSurrogateCandidate(S, Cand); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9135 | else { |
| 9136 | assert(Cand->Viable && |
| 9137 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9138 | // Generally we only see ambiguities including viable builtin |
| 9139 | // operators if overload resolution got screwed up by an |
| 9140 | // ambiguous user-defined conversion. |
| 9141 | // |
| 9142 | // FIXME: It's quite possible for different conversions to see |
| 9143 | // different ambiguities, though. |
| 9144 | if (!ReportedAmbiguousConversions) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9145 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9146 | ReportedAmbiguousConversions = true; |
| 9147 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9148 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9149 | // If this is a viable builtin, print it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9150 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 9151 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9152 | } |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9153 | |
| 9154 | if (I != E) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9155 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9156 | } |
| 9157 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9158 | static SourceLocation |
| 9159 | GetLocationForCandidate(const TemplateSpecCandidate *Cand) { |
| 9160 | return Cand->Specialization ? Cand->Specialization->getLocation() |
| 9161 | : SourceLocation(); |
| 9162 | } |
| 9163 | |
| 9164 | struct CompareTemplateSpecCandidatesForDisplay { |
| 9165 | Sema &S; |
| 9166 | CompareTemplateSpecCandidatesForDisplay(Sema &S) : S(S) {} |
| 9167 | |
| 9168 | bool operator()(const TemplateSpecCandidate *L, |
| 9169 | const TemplateSpecCandidate *R) { |
| 9170 | // Fast-path this check. |
| 9171 | if (L == R) |
| 9172 | return false; |
| 9173 | |
| 9174 | // Assuming that both candidates are not matches... |
| 9175 | |
| 9176 | // Sort by the ranking of deduction failures. |
| 9177 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 9178 | return RankDeductionFailure(L->DeductionFailure) < |
| 9179 | RankDeductionFailure(R->DeductionFailure); |
| 9180 | |
| 9181 | // Sort everything else by location. |
| 9182 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 9183 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 9184 | |
| 9185 | // Put candidates without locations (e.g. builtins) at the end. |
| 9186 | if (LLoc.isInvalid()) |
| 9187 | return false; |
| 9188 | if (RLoc.isInvalid()) |
| 9189 | return true; |
| 9190 | |
| 9191 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
| 9192 | } |
| 9193 | }; |
| 9194 | |
| 9195 | /// Diagnose a template argument deduction failure. |
| 9196 | /// We are treating these failures as overload failures due to bad |
| 9197 | /// deductions. |
| 9198 | void TemplateSpecCandidate::NoteDeductionFailure(Sema &S) { |
| 9199 | DiagnoseBadDeduction(S, Specialization, // pattern |
| 9200 | DeductionFailure, /*NumArgs=*/0); |
| 9201 | } |
| 9202 | |
| 9203 | void TemplateSpecCandidateSet::destroyCandidates() { |
| 9204 | for (iterator i = begin(), e = end(); i != e; ++i) { |
| 9205 | i->DeductionFailure.Destroy(); |
| 9206 | } |
| 9207 | } |
| 9208 | |
| 9209 | void TemplateSpecCandidateSet::clear() { |
| 9210 | destroyCandidates(); |
| 9211 | Candidates.clear(); |
| 9212 | } |
| 9213 | |
| 9214 | /// NoteCandidates - When no template specialization match is found, prints |
| 9215 | /// diagnostic messages containing the non-matching specializations that form |
| 9216 | /// the candidate set. |
| 9217 | /// This is analoguous to OverloadCandidateSet::NoteCandidates() with |
| 9218 | /// OCD == OCD_AllCandidates and Cand->Viable == false. |
| 9219 | void TemplateSpecCandidateSet::NoteCandidates(Sema &S, SourceLocation Loc) { |
| 9220 | // Sort the candidates by position (assuming no candidate is a match). |
| 9221 | // Sorting directly would be prohibitive, so we make a set of pointers |
| 9222 | // and sort those. |
| 9223 | SmallVector<TemplateSpecCandidate *, 32> Cands; |
| 9224 | Cands.reserve(size()); |
| 9225 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
| 9226 | if (Cand->Specialization) |
| 9227 | Cands.push_back(Cand); |
| 9228 | // Otherwise, this is a non matching builtin candidate. We do not, |
| 9229 | // in general, want to list every possible builtin candidate. |
| 9230 | } |
| 9231 | |
| 9232 | std::sort(Cands.begin(), Cands.end(), |
| 9233 | CompareTemplateSpecCandidatesForDisplay(S)); |
| 9234 | |
| 9235 | // FIXME: Perhaps rename OverloadsShown and getShowOverloads() |
| 9236 | // for generalization purposes (?). |
| 9237 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
| 9238 | |
| 9239 | SmallVectorImpl<TemplateSpecCandidate *>::iterator I, E; |
| 9240 | unsigned CandsShown = 0; |
| 9241 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 9242 | TemplateSpecCandidate *Cand = *I; |
| 9243 | |
| 9244 | // Set an arbitrary limit on the number of candidates we'll spam |
| 9245 | // the user with. FIXME: This limit should depend on details of the |
| 9246 | // candidate list. |
| 9247 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
| 9248 | break; |
| 9249 | ++CandsShown; |
| 9250 | |
| 9251 | assert(Cand->Specialization && |
| 9252 | "Non-matching built-in candidates are not added to Cands."); |
| 9253 | Cand->NoteDeductionFailure(S); |
| 9254 | } |
| 9255 | |
| 9256 | if (I != E) |
| 9257 | S.Diag(Loc, diag::note_ovl_too_many_candidates) << int(E - I); |
| 9258 | } |
| 9259 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9260 | // [PossiblyAFunctionType] --> [Return] |
| 9261 | // NonFunctionType --> NonFunctionType |
| 9262 | // R (A) --> R(A) |
| 9263 | // R (*)(A) --> R (A) |
| 9264 | // R (&)(A) --> R (A) |
| 9265 | // R (S::*)(A) --> R (A) |
| 9266 | QualType Sema::ExtractUnqualifiedFunctionType(QualType PossiblyAFunctionType) { |
| 9267 | QualType Ret = PossiblyAFunctionType; |
| 9268 | if (const PointerType *ToTypePtr = |
| 9269 | PossiblyAFunctionType->getAs<PointerType>()) |
| 9270 | Ret = ToTypePtr->getPointeeType(); |
| 9271 | else if (const ReferenceType *ToTypeRef = |
| 9272 | PossiblyAFunctionType->getAs<ReferenceType>()) |
| 9273 | Ret = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 9274 | else if (const MemberPointerType *MemTypePtr = |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9275 | PossiblyAFunctionType->getAs<MemberPointerType>()) |
| 9276 | Ret = MemTypePtr->getPointeeType(); |
| 9277 | Ret = |
| 9278 | Context.getCanonicalType(Ret).getUnqualifiedType(); |
| 9279 | return Ret; |
| 9280 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9281 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9282 | // A helper class to help with address of function resolution |
| 9283 | // - allows us to avoid passing around all those ugly parameters |
| 9284 | class AddressOfFunctionResolver |
| 9285 | { |
| 9286 | Sema& S; |
| 9287 | Expr* SourceExpr; |
| 9288 | const QualType& TargetType; |
| 9289 | QualType TargetFunctionType; // Extracted function type from target type |
| 9290 | |
| 9291 | bool Complain; |
| 9292 | //DeclAccessPair& ResultFunctionAccessPair; |
| 9293 | ASTContext& Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9294 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9295 | bool TargetTypeIsNonStaticMemberFunction; |
| 9296 | bool FoundNonTemplateFunction; |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9297 | bool StaticMemberFunctionFromBoundPointer; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9298 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9299 | OverloadExpr::FindResult OvlExprInfo; |
| 9300 | OverloadExpr *OvlExpr; |
| 9301 | TemplateArgumentListInfo OvlExplicitTemplateArgs; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9302 | SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9303 | TemplateSpecCandidateSet FailedCandidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9304 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9305 | public: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9306 | AddressOfFunctionResolver(Sema &S, Expr *SourceExpr, |
| 9307 | const QualType &TargetType, bool Complain) |
| 9308 | : S(S), SourceExpr(SourceExpr), TargetType(TargetType), |
| 9309 | Complain(Complain), Context(S.getASTContext()), |
| 9310 | TargetTypeIsNonStaticMemberFunction( |
| 9311 | !!TargetType->getAs<MemberPointerType>()), |
| 9312 | FoundNonTemplateFunction(false), |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9313 | StaticMemberFunctionFromBoundPointer(false), |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9314 | OvlExprInfo(OverloadExpr::find(SourceExpr)), |
| 9315 | OvlExpr(OvlExprInfo.Expression), |
| 9316 | FailedCandidates(OvlExpr->getNameLoc()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9317 | ExtractUnqualifiedFunctionTypeFromTargetType(); |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9318 | |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9319 | if (TargetFunctionType->isFunctionType()) { |
| 9320 | if (UnresolvedMemberExpr *UME = dyn_cast<UnresolvedMemberExpr>(OvlExpr)) |
| 9321 | if (!UME->isImplicitAccess() && |
| 9322 | !S.ResolveSingleFunctionTemplateSpecialization(UME)) |
| 9323 | StaticMemberFunctionFromBoundPointer = true; |
| 9324 | } else if (OvlExpr->hasExplicitTemplateArgs()) { |
| 9325 | DeclAccessPair dap; |
| 9326 | if (FunctionDecl *Fn = S.ResolveSingleFunctionTemplateSpecialization( |
| 9327 | OvlExpr, false, &dap)) { |
| 9328 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
| 9329 | if (!Method->isStatic()) { |
| 9330 | // If the target type is a non-function type and the function found |
| 9331 | // is a non-static member function, pretend as if that was the |
| 9332 | // target, it's the only possible type to end up with. |
| 9333 | TargetTypeIsNonStaticMemberFunction = true; |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9334 | |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9335 | // And skip adding the function if its not in the proper form. |
| 9336 | // We'll diagnose this due to an empty set of functions. |
| 9337 | if (!OvlExprInfo.HasFormOfMemberPointer) |
| 9338 | return; |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9339 | } |
| 9340 | |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9341 | Matches.push_back(std::make_pair(dap, Fn)); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 9342 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9343 | return; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 9344 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9345 | |
| 9346 | if (OvlExpr->hasExplicitTemplateArgs()) |
| 9347 | OvlExpr->getExplicitTemplateArgs().copyInto(OvlExplicitTemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9348 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9349 | if (FindAllFunctionsThatMatchTargetTypeExactly()) { |
| 9350 | // C++ [over.over]p4: |
| 9351 | // If more than one function is selected, [...] |
| 9352 | if (Matches.size() > 1) { |
| 9353 | if (FoundNonTemplateFunction) |
| 9354 | EliminateAllTemplateMatches(); |
| 9355 | else |
| 9356 | EliminateAllExceptMostSpecializedTemplate(); |
| 9357 | } |
| 9358 | } |
| 9359 | } |
| 9360 | |
| 9361 | private: |
| 9362 | bool isTargetTypeAFunction() const { |
| 9363 | return TargetFunctionType->isFunctionType(); |
| 9364 | } |
| 9365 | |
| 9366 | // [ToType] [Return] |
| 9367 | |
| 9368 | // R (*)(A) --> R (A), IsNonStaticMemberFunction = false |
| 9369 | // R (&)(A) --> R (A), IsNonStaticMemberFunction = false |
| 9370 | // R (S::*)(A) --> R (A), IsNonStaticMemberFunction = true |
| 9371 | void inline ExtractUnqualifiedFunctionTypeFromTargetType() { |
| 9372 | TargetFunctionType = S.ExtractUnqualifiedFunctionType(TargetType); |
| 9373 | } |
| 9374 | |
| 9375 | // return true if any matching specializations were found |
| 9376 | bool AddMatchingTemplateFunction(FunctionTemplateDecl* FunctionTemplate, |
| 9377 | const DeclAccessPair& CurAccessFunPair) { |
| 9378 | if (CXXMethodDecl *Method |
| 9379 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
| 9380 | // Skip non-static function templates when converting to pointer, and |
| 9381 | // static when converting to member pointer. |
| 9382 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 9383 | return false; |
| 9384 | } |
| 9385 | else if (TargetTypeIsNonStaticMemberFunction) |
| 9386 | return false; |
| 9387 | |
| 9388 | // C++ [over.over]p2: |
| 9389 | // If the name is a function template, template argument deduction is |
| 9390 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 9391 | // resulting template argument list is used to generate a single |
| 9392 | // function template specialization, which is added to the set of |
| 9393 | // overloaded functions considered. |
| 9394 | FunctionDecl *Specialization = 0; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9395 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9396 | if (Sema::TemplateDeductionResult Result |
| 9397 | = S.DeduceTemplateArguments(FunctionTemplate, |
| 9398 | &OvlExplicitTemplateArgs, |
| 9399 | TargetFunctionType, Specialization, |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9400 | Info, /*InOverloadResolution=*/true)) { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9401 | // Make a note of the failed deduction for diagnostics. |
| 9402 | FailedCandidates.addCandidate() |
| 9403 | .set(FunctionTemplate->getTemplatedDecl(), |
| 9404 | MakeDeductionFailureInfo(Context, Result, Info)); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9405 | return false; |
| 9406 | } |
| 9407 | |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9408 | // Template argument deduction ensures that we have an exact match or |
| 9409 | // compatible pointer-to-function arguments that would be adjusted by ICS. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9410 | // This function template specicalization works. |
| 9411 | Specialization = cast<FunctionDecl>(Specialization->getCanonicalDecl()); |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9412 | assert(S.isSameOrCompatibleFunctionType( |
| 9413 | Context.getCanonicalType(Specialization->getType()), |
| 9414 | Context.getCanonicalType(TargetFunctionType))); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9415 | Matches.push_back(std::make_pair(CurAccessFunPair, Specialization)); |
| 9416 | return true; |
| 9417 | } |
| 9418 | |
| 9419 | bool AddMatchingNonTemplateFunction(NamedDecl* Fn, |
| 9420 | const DeclAccessPair& CurAccessFunPair) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9421 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 9422 | // Skip non-static functions when converting to pointer, and static |
| 9423 | // when converting to member pointer. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9424 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 9425 | return false; |
| 9426 | } |
| 9427 | else if (TargetTypeIsNonStaticMemberFunction) |
| 9428 | return false; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9429 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9430 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9431 | if (S.getLangOpts().CUDA) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 9432 | if (FunctionDecl *Caller = dyn_cast<FunctionDecl>(S.CurContext)) |
| 9433 | if (S.CheckCUDATarget(Caller, FunDecl)) |
| 9434 | return false; |
| 9435 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9436 | // If any candidate has a placeholder return type, trigger its deduction |
| 9437 | // now. |
| 9438 | if (S.getLangOpts().CPlusPlus1y && |
| 9439 | FunDecl->getResultType()->isUndeducedType() && |
| 9440 | S.DeduceReturnType(FunDecl, SourceExpr->getLocStart(), Complain)) |
| 9441 | return false; |
| 9442 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 9443 | QualType ResultTy; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9444 | if (Context.hasSameUnqualifiedType(TargetFunctionType, |
| 9445 | FunDecl->getType()) || |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 9446 | S.IsNoReturnConversion(FunDecl->getType(), TargetFunctionType, |
| 9447 | ResultTy)) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9448 | Matches.push_back(std::make_pair(CurAccessFunPair, |
| 9449 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 9450 | FoundNonTemplateFunction = true; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9451 | return true; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 9452 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9453 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9454 | |
| 9455 | return false; |
| 9456 | } |
| 9457 | |
| 9458 | bool FindAllFunctionsThatMatchTargetTypeExactly() { |
| 9459 | bool Ret = false; |
| 9460 | |
| 9461 | // If the overload expression doesn't have the form of a pointer to |
| 9462 | // member, don't try to convert it to a pointer-to-member type. |
| 9463 | if (IsInvalidFormOfPointerToMemberFunction()) |
| 9464 | return false; |
| 9465 | |
| 9466 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 9467 | E = OvlExpr->decls_end(); |
| 9468 | I != E; ++I) { |
| 9469 | // Look through any using declarations to find the underlying function. |
| 9470 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 9471 | |
| 9472 | // C++ [over.over]p3: |
| 9473 | // Non-member functions and static member functions match |
| 9474 | // targets of type "pointer-to-function" or "reference-to-function." |
| 9475 | // Nonstatic member functions match targets of |
| 9476 | // type "pointer-to-member-function." |
| 9477 | // Note that according to DR 247, the containing class does not matter. |
| 9478 | if (FunctionTemplateDecl *FunctionTemplate |
| 9479 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
| 9480 | if (AddMatchingTemplateFunction(FunctionTemplate, I.getPair())) |
| 9481 | Ret = true; |
| 9482 | } |
| 9483 | // If we have explicit template arguments supplied, skip non-templates. |
| 9484 | else if (!OvlExpr->hasExplicitTemplateArgs() && |
| 9485 | AddMatchingNonTemplateFunction(Fn, I.getPair())) |
| 9486 | Ret = true; |
| 9487 | } |
| 9488 | assert(Ret || Matches.empty()); |
| 9489 | return Ret; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9490 | } |
| 9491 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9492 | void EliminateAllExceptMostSpecializedTemplate() { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 9493 | // [...] and any given function template specialization F1 is |
| 9494 | // eliminated if the set contains a second function template |
| 9495 | // specialization whose function template is more specialized |
| 9496 | // than the function template of F1 according to the partial |
| 9497 | // ordering rules of 14.5.5.2. |
| 9498 | |
| 9499 | // The algorithm specified above is quadratic. We instead use a |
| 9500 | // two-pass algorithm (similar to the one used to identify the |
| 9501 | // best viable function in an overload set) that identifies the |
| 9502 | // best function template (if it exists). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9503 | |
| 9504 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 9505 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 9506 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9507 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9508 | // TODO: It looks like FailedCandidates does not serve much purpose |
| 9509 | // here, since the no_viable diagnostic has index 0. |
| 9510 | UnresolvedSetIterator Result = S.getMostSpecialized( |
Richard Smith | 4ad09e6 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 9511 | MatchesCopy.begin(), MatchesCopy.end(), FailedCandidates, |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9512 | SourceExpr->getLocStart(), S.PDiag(), |
| 9513 | S.PDiag(diag::err_addr_ovl_ambiguous) << Matches[0] |
| 9514 | .second->getDeclName(), |
| 9515 | S.PDiag(diag::note_ovl_candidate) << (unsigned)oc_function_template, |
| 9516 | Complain, TargetFunctionType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9517 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9518 | if (Result != MatchesCopy.end()) { |
| 9519 | // Make it the first and only element |
| 9520 | Matches[0].first = Matches[Result - MatchesCopy.begin()].first; |
| 9521 | Matches[0].second = cast<FunctionDecl>(*Result); |
| 9522 | Matches.resize(1); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9523 | } |
| 9524 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9525 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9526 | void EliminateAllTemplateMatches() { |
| 9527 | // [...] any function template specializations in the set are |
| 9528 | // eliminated if the set also contains a non-template function, [...] |
| 9529 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
| 9530 | if (Matches[I].second->getPrimaryTemplate() == 0) |
| 9531 | ++I; |
| 9532 | else { |
| 9533 | Matches[I] = Matches[--N]; |
| 9534 | Matches.set_size(N); |
| 9535 | } |
| 9536 | } |
| 9537 | } |
| 9538 | |
| 9539 | public: |
| 9540 | void ComplainNoMatchesFound() const { |
| 9541 | assert(Matches.empty()); |
| 9542 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_no_viable) |
| 9543 | << OvlExpr->getName() << TargetFunctionType |
| 9544 | << OvlExpr->getSourceRange(); |
Richard Smith | 72a36a1 | 2013-08-14 00:00:44 +0000 | [diff] [blame] | 9545 | if (FailedCandidates.empty()) |
| 9546 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
| 9547 | else { |
| 9548 | // We have some deduction failure messages. Use them to diagnose |
| 9549 | // the function templates, and diagnose the non-template candidates |
| 9550 | // normally. |
| 9551 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 9552 | IEnd = OvlExpr->decls_end(); |
| 9553 | I != IEnd; ++I) |
| 9554 | if (FunctionDecl *Fun = |
| 9555 | dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl())) |
| 9556 | S.NoteOverloadCandidate(Fun, TargetFunctionType); |
| 9557 | FailedCandidates.NoteCandidates(S, OvlExpr->getLocStart()); |
| 9558 | } |
| 9559 | } |
| 9560 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9561 | bool IsInvalidFormOfPointerToMemberFunction() const { |
| 9562 | return TargetTypeIsNonStaticMemberFunction && |
| 9563 | !OvlExprInfo.HasFormOfMemberPointer; |
| 9564 | } |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9565 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9566 | void ComplainIsInvalidFormOfPointerToMemberFunction() const { |
| 9567 | // TODO: Should we condition this on whether any functions might |
| 9568 | // have matched, or is it more appropriate to do that in callers? |
| 9569 | // TODO: a fixit wouldn't hurt. |
| 9570 | S.Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
| 9571 | << TargetType << OvlExpr->getSourceRange(); |
| 9572 | } |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9573 | |
| 9574 | bool IsStaticMemberFunctionFromBoundPointer() const { |
| 9575 | return StaticMemberFunctionFromBoundPointer; |
| 9576 | } |
| 9577 | |
| 9578 | void ComplainIsStaticMemberFunctionFromBoundPointer() const { |
| 9579 | S.Diag(OvlExpr->getLocStart(), |
| 9580 | diag::err_invalid_form_pointer_member_function) |
| 9581 | << OvlExpr->getSourceRange(); |
| 9582 | } |
| 9583 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9584 | void ComplainOfInvalidConversion() const { |
| 9585 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 9586 | << OvlExpr->getName() << TargetType; |
| 9587 | } |
| 9588 | |
| 9589 | void ComplainMultipleMatchesFound() const { |
| 9590 | assert(Matches.size() > 1); |
| 9591 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 9592 | << OvlExpr->getName() |
| 9593 | << OvlExpr->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9594 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9595 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9596 | |
| 9597 | bool hadMultipleCandidates() const { return (OvlExpr->getNumDecls() > 1); } |
| 9598 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9599 | int getNumMatches() const { return Matches.size(); } |
| 9600 | |
| 9601 | FunctionDecl* getMatchingFunctionDecl() const { |
| 9602 | if (Matches.size() != 1) return 0; |
| 9603 | return Matches[0].second; |
| 9604 | } |
| 9605 | |
| 9606 | const DeclAccessPair* getMatchingFunctionAccessPair() const { |
| 9607 | if (Matches.size() != 1) return 0; |
| 9608 | return &Matches[0].first; |
| 9609 | } |
| 9610 | }; |
| 9611 | |
| 9612 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 9613 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 9614 | /// expression with overloaded function type and @p ToType is the type |
| 9615 | /// we're trying to resolve to. For example: |
| 9616 | /// |
| 9617 | /// @code |
| 9618 | /// int f(double); |
| 9619 | /// int f(int); |
| 9620 | /// |
| 9621 | /// int (*pfd)(double) = f; // selects f(double) |
| 9622 | /// @endcode |
| 9623 | /// |
| 9624 | /// This routine returns the resulting FunctionDecl if it could be |
| 9625 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 9626 | /// routine will emit diagnostics if there is an error. |
| 9627 | FunctionDecl * |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9628 | Sema::ResolveAddressOfOverloadedFunction(Expr *AddressOfExpr, |
| 9629 | QualType TargetType, |
| 9630 | bool Complain, |
| 9631 | DeclAccessPair &FoundResult, |
| 9632 | bool *pHadMultipleCandidates) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9633 | assert(AddressOfExpr->getType() == Context.OverloadTy); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9634 | |
| 9635 | AddressOfFunctionResolver Resolver(*this, AddressOfExpr, TargetType, |
| 9636 | Complain); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9637 | int NumMatches = Resolver.getNumMatches(); |
| 9638 | FunctionDecl* Fn = 0; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9639 | if (NumMatches == 0 && Complain) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9640 | if (Resolver.IsInvalidFormOfPointerToMemberFunction()) |
| 9641 | Resolver.ComplainIsInvalidFormOfPointerToMemberFunction(); |
| 9642 | else |
| 9643 | Resolver.ComplainNoMatchesFound(); |
| 9644 | } |
| 9645 | else if (NumMatches > 1 && Complain) |
| 9646 | Resolver.ComplainMultipleMatchesFound(); |
| 9647 | else if (NumMatches == 1) { |
| 9648 | Fn = Resolver.getMatchingFunctionDecl(); |
| 9649 | assert(Fn); |
| 9650 | FoundResult = *Resolver.getMatchingFunctionAccessPair(); |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9651 | if (Complain) { |
| 9652 | if (Resolver.IsStaticMemberFunctionFromBoundPointer()) |
| 9653 | Resolver.ComplainIsStaticMemberFunctionFromBoundPointer(); |
| 9654 | else |
| 9655 | CheckAddressOfMemberAccess(AddressOfExpr, FoundResult); |
| 9656 | } |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 9657 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9658 | |
| 9659 | if (pHadMultipleCandidates) |
| 9660 | *pHadMultipleCandidates = Resolver.hadMultipleCandidates(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9661 | return Fn; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9662 | } |
| 9663 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9664 | /// \brief Given an expression that refers to an overloaded function, try to |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9665 | /// resolve that overloaded function expression down to a single function. |
| 9666 | /// |
| 9667 | /// This routine can only resolve template-ids that refer to a single function |
| 9668 | /// template, where that template-id refers to a single template whose template |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9669 | /// arguments are either provided by the template-id or have defaults, |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9670 | /// as described in C++0x [temp.arg.explicit]p3. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9671 | FunctionDecl * |
| 9672 | Sema::ResolveSingleFunctionTemplateSpecialization(OverloadExpr *ovl, |
| 9673 | bool Complain, |
| 9674 | DeclAccessPair *FoundResult) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9675 | // C++ [over.over]p1: |
| 9676 | // [...] [Note: any redundant set of parentheses surrounding the |
| 9677 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9678 | // C++ [over.over]p1: |
| 9679 | // [...] The overloaded function name can be preceded by the & |
| 9680 | // operator. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9681 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9682 | // If we didn't actually find any template-ids, we're done. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9683 | if (!ovl->hasExplicitTemplateArgs()) |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9684 | return 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 9685 | |
| 9686 | TemplateArgumentListInfo ExplicitTemplateArgs; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9687 | ovl->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9688 | TemplateSpecCandidateSet FailedCandidates(ovl->getNameLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9689 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9690 | // Look through all of the overloaded functions, searching for one |
| 9691 | // whose type matches exactly. |
| 9692 | FunctionDecl *Matched = 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9693 | for (UnresolvedSetIterator I = ovl->decls_begin(), |
| 9694 | E = ovl->decls_end(); I != E; ++I) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9695 | // C++0x [temp.arg.explicit]p3: |
| 9696 | // [...] In contexts where deduction is done and fails, or in contexts |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9697 | // where deduction is not done, if a template argument list is |
| 9698 | // specified and it, along with any default template arguments, |
| 9699 | // identifies a single function template specialization, then the |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9700 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | 66a8c9a | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 9701 | FunctionTemplateDecl *FunctionTemplate |
| 9702 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9703 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9704 | // C++ [over.over]p2: |
| 9705 | // If the name is a function template, template argument deduction is |
| 9706 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 9707 | // resulting template argument list is used to generate a single |
| 9708 | // function template specialization, which is added to the set of |
| 9709 | // overloaded functions considered. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9710 | FunctionDecl *Specialization = 0; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9711 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9712 | if (TemplateDeductionResult Result |
| 9713 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9714 | Specialization, Info, |
| 9715 | /*InOverloadResolution=*/true)) { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9716 | // Make a note of the failed deduction for diagnostics. |
| 9717 | // TODO: Actually use the failed-deduction info? |
| 9718 | FailedCandidates.addCandidate() |
| 9719 | .set(FunctionTemplate->getTemplatedDecl(), |
| 9720 | MakeDeductionFailureInfo(Context, Result, Info)); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9721 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9722 | } |
| 9723 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9724 | assert(Specialization && "no specialization and no error?"); |
| 9725 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9726 | // Multiple matches; we can't resolve to a single declaration. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9727 | if (Matched) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9728 | if (Complain) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9729 | Diag(ovl->getExprLoc(), diag::err_addr_ovl_ambiguous) |
| 9730 | << ovl->getName(); |
| 9731 | NoteAllOverloadCandidates(ovl); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9732 | } |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9733 | return 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9734 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9735 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9736 | Matched = Specialization; |
| 9737 | if (FoundResult) *FoundResult = I.getPair(); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9738 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9739 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9740 | if (Matched && getLangOpts().CPlusPlus1y && |
| 9741 | Matched->getResultType()->isUndeducedType() && |
| 9742 | DeduceReturnType(Matched, ovl->getExprLoc(), Complain)) |
| 9743 | return 0; |
| 9744 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9745 | return Matched; |
| 9746 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9747 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9748 | |
| 9749 | |
| 9750 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9751 | // Resolve and fix an overloaded expression that can be resolved |
| 9752 | // because it identifies a single function template specialization. |
| 9753 | // |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9754 | // Last three arguments should only be supplied if Complain = true |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9755 | // |
| 9756 | // Return true if it was logically possible to so resolve the |
| 9757 | // expression, regardless of whether or not it succeeded. Always |
| 9758 | // returns true if 'complain' is set. |
| 9759 | bool Sema::ResolveAndFixSingleFunctionTemplateSpecialization( |
| 9760 | ExprResult &SrcExpr, bool doFunctionPointerConverion, |
| 9761 | bool complain, const SourceRange& OpRangeForComplaining, |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9762 | QualType DestTypeForComplaining, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9763 | unsigned DiagIDForComplaining) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9764 | assert(SrcExpr.get()->getType() == Context.OverloadTy); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9765 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9766 | OverloadExpr::FindResult ovl = OverloadExpr::find(SrcExpr.get()); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9767 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9768 | DeclAccessPair found; |
| 9769 | ExprResult SingleFunctionExpression; |
| 9770 | if (FunctionDecl *fn = ResolveSingleFunctionTemplateSpecialization( |
| 9771 | ovl.Expression, /*complain*/ false, &found)) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9772 | if (DiagnoseUseOfDecl(fn, SrcExpr.get()->getLocStart())) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9773 | SrcExpr = ExprError(); |
| 9774 | return true; |
| 9775 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9776 | |
| 9777 | // It is only correct to resolve to an instance method if we're |
| 9778 | // resolving a form that's permitted to be a pointer to member. |
| 9779 | // Otherwise we'll end up making a bound member expression, which |
| 9780 | // is illegal in all the contexts we resolve like this. |
| 9781 | if (!ovl.HasFormOfMemberPointer && |
| 9782 | isa<CXXMethodDecl>(fn) && |
| 9783 | cast<CXXMethodDecl>(fn)->isInstance()) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9784 | if (!complain) return false; |
| 9785 | |
| 9786 | Diag(ovl.Expression->getExprLoc(), |
| 9787 | diag::err_bound_member_function) |
| 9788 | << 0 << ovl.Expression->getSourceRange(); |
| 9789 | |
| 9790 | // TODO: I believe we only end up here if there's a mix of |
| 9791 | // static and non-static candidates (otherwise the expression |
| 9792 | // would have 'bound member' type, not 'overload' type). |
| 9793 | // Ideally we would note which candidate was chosen and why |
| 9794 | // the static candidates were rejected. |
| 9795 | SrcExpr = ExprError(); |
| 9796 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9797 | } |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 9798 | |
Sylvestre Ledru | 43e3dee | 2012-07-31 06:56:50 +0000 | [diff] [blame] | 9799 | // Fix the expression to refer to 'fn'. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9800 | SingleFunctionExpression = |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9801 | Owned(FixOverloadedFunctionReference(SrcExpr.take(), found, fn)); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9802 | |
| 9803 | // If desired, do function-to-pointer decay. |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9804 | if (doFunctionPointerConverion) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9805 | SingleFunctionExpression = |
| 9806 | DefaultFunctionArrayLvalueConversion(SingleFunctionExpression.take()); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9807 | if (SingleFunctionExpression.isInvalid()) { |
| 9808 | SrcExpr = ExprError(); |
| 9809 | return true; |
| 9810 | } |
| 9811 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9812 | } |
| 9813 | |
| 9814 | if (!SingleFunctionExpression.isUsable()) { |
| 9815 | if (complain) { |
| 9816 | Diag(OpRangeForComplaining.getBegin(), DiagIDForComplaining) |
| 9817 | << ovl.Expression->getName() |
| 9818 | << DestTypeForComplaining |
| 9819 | << OpRangeForComplaining |
| 9820 | << ovl.Expression->getQualifierLoc().getSourceRange(); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9821 | NoteAllOverloadCandidates(SrcExpr.get()); |
| 9822 | |
| 9823 | SrcExpr = ExprError(); |
| 9824 | return true; |
| 9825 | } |
| 9826 | |
| 9827 | return false; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9828 | } |
| 9829 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9830 | SrcExpr = SingleFunctionExpression; |
| 9831 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9832 | } |
| 9833 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9834 | /// \brief Add a single candidate to the overload set. |
| 9835 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9836 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 9837 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9838 | ArrayRef<Expr *> Args, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9839 | OverloadCandidateSet &CandidateSet, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9840 | bool PartialOverloading, |
| 9841 | bool KnownValid) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9842 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9843 | if (isa<UsingShadowDecl>(Callee)) |
| 9844 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 9845 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9846 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9847 | if (ExplicitTemplateArgs) { |
| 9848 | assert(!KnownValid && "Explicit template arguments?"); |
| 9849 | return; |
| 9850 | } |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9851 | S.AddOverloadCandidate(Func, FoundDecl, Args, CandidateSet, false, |
| 9852 | PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9853 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9854 | } |
| 9855 | |
| 9856 | if (FunctionTemplateDecl *FuncTemplate |
| 9857 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9858 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9859 | ExplicitTemplateArgs, Args, CandidateSet); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9860 | return; |
| 9861 | } |
| 9862 | |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9863 | assert(!KnownValid && "unhandled case in overloaded call candidate"); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9864 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9865 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9866 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 9867 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9868 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9869 | ArrayRef<Expr *> Args, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9870 | OverloadCandidateSet &CandidateSet, |
| 9871 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9872 | |
| 9873 | #ifndef NDEBUG |
| 9874 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 9875 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9876 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9877 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 9878 | // and let Y be the lookup set produced by argument dependent |
| 9879 | // lookup (defined as follows). If X contains |
| 9880 | // |
| 9881 | // -- a declaration of a class member, or |
| 9882 | // |
| 9883 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9884 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9885 | // |
| 9886 | // -- a declaration that is neither a function or a function |
| 9887 | // template |
| 9888 | // |
| 9889 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9890 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9891 | if (ULE->requiresADL()) { |
| 9892 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 9893 | E = ULE->decls_end(); I != E; ++I) { |
| 9894 | assert(!(*I)->getDeclContext()->isRecord()); |
| 9895 | assert(isa<UsingShadowDecl>(*I) || |
| 9896 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 9897 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9898 | } |
| 9899 | } |
| 9900 | #endif |
| 9901 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9902 | // It would be nice to avoid this copy. |
| 9903 | TemplateArgumentListInfo TABuffer; |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 9904 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9905 | if (ULE->hasExplicitTemplateArgs()) { |
| 9906 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 9907 | ExplicitTemplateArgs = &TABuffer; |
| 9908 | } |
| 9909 | |
| 9910 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 9911 | E = ULE->decls_end(); I != E; ++I) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9912 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, Args, |
| 9913 | CandidateSet, PartialOverloading, |
| 9914 | /*KnownValid*/ true); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9915 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9916 | if (ULE->requiresADL()) |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9917 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
Richard Smith | f5cd5cc | 2012-02-25 06:24:24 +0000 | [diff] [blame] | 9918 | ULE->getExprLoc(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9919 | Args, ExplicitTemplateArgs, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 9920 | CandidateSet, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9921 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9922 | |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 9923 | /// Determine whether a declaration with the specified name could be moved into |
| 9924 | /// a different namespace. |
| 9925 | static bool canBeDeclaredInNamespace(const DeclarationName &Name) { |
| 9926 | switch (Name.getCXXOverloadedOperator()) { |
| 9927 | case OO_New: case OO_Array_New: |
| 9928 | case OO_Delete: case OO_Array_Delete: |
| 9929 | return false; |
| 9930 | |
| 9931 | default: |
| 9932 | return true; |
| 9933 | } |
| 9934 | } |
| 9935 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9936 | /// Attempt to recover from an ill-formed use of a non-dependent name in a |
| 9937 | /// template, where the non-dependent name was declared after the template |
| 9938 | /// was defined. This is common in code written for a compilers which do not |
| 9939 | /// correctly implement two-stage name lookup. |
| 9940 | /// |
| 9941 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 9942 | static bool |
| 9943 | DiagnoseTwoPhaseLookup(Sema &SemaRef, SourceLocation FnLoc, |
| 9944 | const CXXScopeSpec &SS, LookupResult &R, |
| 9945 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9946 | ArrayRef<Expr *> Args) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9947 | if (SemaRef.ActiveTemplateInstantiations.empty() || !SS.isEmpty()) |
| 9948 | return false; |
| 9949 | |
| 9950 | for (DeclContext *DC = SemaRef.CurContext; DC; DC = DC->getParent()) { |
Nick Lewycky | 5a7120c | 2012-03-14 20:41:00 +0000 | [diff] [blame] | 9951 | if (DC->isTransparentContext()) |
| 9952 | continue; |
| 9953 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9954 | SemaRef.LookupQualifiedName(R, DC); |
| 9955 | |
| 9956 | if (!R.empty()) { |
| 9957 | R.suppressDiagnostics(); |
| 9958 | |
| 9959 | if (isa<CXXRecordDecl>(DC)) { |
| 9960 | // Don't diagnose names we find in classes; we get much better |
| 9961 | // diagnostics for these from DiagnoseEmptyLookup. |
| 9962 | R.clear(); |
| 9963 | return false; |
| 9964 | } |
| 9965 | |
| 9966 | OverloadCandidateSet Candidates(FnLoc); |
| 9967 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 9968 | AddOverloadedCallCandidate(SemaRef, I.getPair(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9969 | ExplicitTemplateArgs, Args, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9970 | Candidates, false, /*KnownValid*/ false); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9971 | |
| 9972 | OverloadCandidateSet::iterator Best; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9973 | if (Candidates.BestViableFunction(SemaRef, FnLoc, Best) != OR_Success) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9974 | // No viable functions. Don't bother the user with notes for functions |
| 9975 | // which don't work and shouldn't be found anyway. |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9976 | R.clear(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9977 | return false; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9978 | } |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9979 | |
| 9980 | // Find the namespaces where ADL would have looked, and suggest |
| 9981 | // declaring the function there instead. |
| 9982 | Sema::AssociatedNamespaceSet AssociatedNamespaces; |
| 9983 | Sema::AssociatedClassSet AssociatedClasses; |
John McCall | 42f48fb | 2012-08-24 20:38:34 +0000 | [diff] [blame] | 9984 | SemaRef.FindAssociatedClassesAndNamespaces(FnLoc, Args, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9985 | AssociatedNamespaces, |
| 9986 | AssociatedClasses); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9987 | Sema::AssociatedNamespaceSet SuggestedNamespaces; |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 9988 | if (canBeDeclaredInNamespace(R.getLookupName())) { |
| 9989 | DeclContext *Std = SemaRef.getStdNamespace(); |
| 9990 | for (Sema::AssociatedNamespaceSet::iterator |
| 9991 | it = AssociatedNamespaces.begin(), |
| 9992 | end = AssociatedNamespaces.end(); it != end; ++it) { |
| 9993 | // Never suggest declaring a function within namespace 'std'. |
| 9994 | if (Std && Std->Encloses(*it)) |
| 9995 | continue; |
Richard Smith | 19e0d95 | 2012-12-22 02:46:14 +0000 | [diff] [blame] | 9996 | |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 9997 | // Never suggest declaring a function within a namespace with a |
| 9998 | // reserved name, like __gnu_cxx. |
| 9999 | NamespaceDecl *NS = dyn_cast<NamespaceDecl>(*it); |
| 10000 | if (NS && |
| 10001 | NS->getQualifiedNameAsString().find("__") != std::string::npos) |
| 10002 | continue; |
| 10003 | |
| 10004 | SuggestedNamespaces.insert(*it); |
| 10005 | } |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10006 | } |
| 10007 | |
| 10008 | SemaRef.Diag(R.getNameLoc(), diag::err_not_found_by_two_phase_lookup) |
| 10009 | << R.getLookupName(); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10010 | if (SuggestedNamespaces.empty()) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10011 | SemaRef.Diag(Best->Function->getLocation(), |
| 10012 | diag::note_not_found_by_two_phase_lookup) |
| 10013 | << R.getLookupName() << 0; |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10014 | } else if (SuggestedNamespaces.size() == 1) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10015 | SemaRef.Diag(Best->Function->getLocation(), |
| 10016 | diag::note_not_found_by_two_phase_lookup) |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10017 | << R.getLookupName() << 1 << *SuggestedNamespaces.begin(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10018 | } else { |
| 10019 | // FIXME: It would be useful to list the associated namespaces here, |
| 10020 | // but the diagnostics infrastructure doesn't provide a way to produce |
| 10021 | // a localized representation of a list of items. |
| 10022 | SemaRef.Diag(Best->Function->getLocation(), |
| 10023 | diag::note_not_found_by_two_phase_lookup) |
| 10024 | << R.getLookupName() << 2; |
| 10025 | } |
| 10026 | |
| 10027 | // Try to recover by calling this function. |
| 10028 | return true; |
| 10029 | } |
| 10030 | |
| 10031 | R.clear(); |
| 10032 | } |
| 10033 | |
| 10034 | return false; |
| 10035 | } |
| 10036 | |
| 10037 | /// Attempt to recover from ill-formed use of a non-dependent operator in a |
| 10038 | /// template, where the non-dependent operator was declared after the template |
| 10039 | /// was defined. |
| 10040 | /// |
| 10041 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 10042 | static bool |
| 10043 | DiagnoseTwoPhaseOperatorLookup(Sema &SemaRef, OverloadedOperatorKind Op, |
| 10044 | SourceLocation OpLoc, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10045 | ArrayRef<Expr *> Args) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10046 | DeclarationName OpName = |
| 10047 | SemaRef.Context.DeclarationNames.getCXXOperatorName(Op); |
| 10048 | LookupResult R(SemaRef, OpName, OpLoc, Sema::LookupOperatorName); |
| 10049 | return DiagnoseTwoPhaseLookup(SemaRef, OpLoc, CXXScopeSpec(), R, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10050 | /*ExplicitTemplateArgs=*/0, Args); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10051 | } |
| 10052 | |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 10053 | namespace { |
Richard Smith | e49ff3e | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 10054 | class BuildRecoveryCallExprRAII { |
| 10055 | Sema &SemaRef; |
| 10056 | public: |
| 10057 | BuildRecoveryCallExprRAII(Sema &S) : SemaRef(S) { |
| 10058 | assert(SemaRef.IsBuildingRecoveryCallExpr == false); |
| 10059 | SemaRef.IsBuildingRecoveryCallExpr = true; |
| 10060 | } |
| 10061 | |
| 10062 | ~BuildRecoveryCallExprRAII() { |
| 10063 | SemaRef.IsBuildingRecoveryCallExpr = false; |
| 10064 | } |
| 10065 | }; |
| 10066 | |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 10067 | } |
| 10068 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10069 | /// Attempts to recover from a call where no functions were found. |
| 10070 | /// |
| 10071 | /// Returns true if new candidates were found. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10072 | static ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 10073 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10074 | UnresolvedLookupExpr *ULE, |
| 10075 | SourceLocation LParenLoc, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10076 | llvm::MutableArrayRef<Expr *> Args, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10077 | SourceLocation RParenLoc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10078 | bool EmptyLookup, bool AllowTypoCorrection) { |
Richard Smith | e49ff3e | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 10079 | // Do not try to recover if it is already building a recovery call. |
| 10080 | // This stops infinite loops for template instantiations like |
| 10081 | // |
| 10082 | // template <typename T> auto foo(T t) -> decltype(foo(t)) {} |
| 10083 | // template <typename T> auto foo(T t) -> decltype(foo(&t)) {} |
| 10084 | // |
| 10085 | if (SemaRef.IsBuildingRecoveryCallExpr) |
| 10086 | return ExprError(); |
| 10087 | BuildRecoveryCallExprRAII RCE(SemaRef); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10088 | |
| 10089 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10090 | SS.Adopt(ULE->getQualifierLoc()); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10091 | SourceLocation TemplateKWLoc = ULE->getTemplateKeywordLoc(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10092 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10093 | TemplateArgumentListInfo TABuffer; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10094 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10095 | if (ULE->hasExplicitTemplateArgs()) { |
| 10096 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 10097 | ExplicitTemplateArgs = &TABuffer; |
| 10098 | } |
| 10099 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10100 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 10101 | Sema::LookupOrdinaryName); |
Kaelyn Uhrain | 761695f | 2013-07-08 23:13:39 +0000 | [diff] [blame] | 10102 | FunctionCallFilterCCC Validator(SemaRef, Args.size(), |
| 10103 | ExplicitTemplateArgs != 0); |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10104 | NoTypoCorrectionCCC RejectAll; |
| 10105 | CorrectionCandidateCallback *CCC = AllowTypoCorrection ? |
| 10106 | (CorrectionCandidateCallback*)&Validator : |
| 10107 | (CorrectionCandidateCallback*)&RejectAll; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10108 | if (!DiagnoseTwoPhaseLookup(SemaRef, Fn->getExprLoc(), SS, R, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10109 | ExplicitTemplateArgs, Args) && |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10110 | (!EmptyLookup || |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10111 | SemaRef.DiagnoseEmptyLookup(S, SS, R, *CCC, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10112 | ExplicitTemplateArgs, Args))) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10113 | return ExprError(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10114 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10115 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 10116 | |
| 10117 | // Build an implicit member call if appropriate. Just drop the |
| 10118 | // casts and such from the call, we don't really care. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10119 | ExprResult NewFn = ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10120 | if ((*R.begin())->isCXXClassMember()) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10121 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, |
| 10122 | R, ExplicitTemplateArgs); |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 10123 | else if (ExplicitTemplateArgs || TemplateKWLoc.isValid()) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10124 | NewFn = SemaRef.BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 10125 | ExplicitTemplateArgs); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10126 | else |
| 10127 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 10128 | |
| 10129 | if (NewFn.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10130 | return ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10131 | |
| 10132 | // This shouldn't cause an infinite loop because we're giving it |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10133 | // an expression with viable lookup results, which should never |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10134 | // end up here. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10135 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, NewFn.take(), LParenLoc, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10136 | MultiExprArg(Args.data(), Args.size()), |
| 10137 | RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10138 | } |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 10139 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10140 | /// \brief Constructs and populates an OverloadedCandidateSet from |
| 10141 | /// the given function. |
| 10142 | /// \returns true when an the ExprResult output parameter has been set. |
| 10143 | bool Sema::buildOverloadedCallSet(Scope *S, Expr *Fn, |
| 10144 | UnresolvedLookupExpr *ULE, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10145 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10146 | SourceLocation RParenLoc, |
| 10147 | OverloadCandidateSet *CandidateSet, |
| 10148 | ExprResult *Result) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10149 | #ifndef NDEBUG |
| 10150 | if (ULE->requiresADL()) { |
| 10151 | // To do ADL, we must have found an unqualified name. |
| 10152 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 10153 | |
| 10154 | // We don't perform ADL for implicit declarations of builtins. |
| 10155 | // Verify that this was correctly set up. |
| 10156 | FunctionDecl *F; |
| 10157 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 10158 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 10159 | F->getBuiltinID() && F->isImplicit()) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 10160 | llvm_unreachable("performing ADL for builtin"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10161 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10162 | // We don't perform ADL in C. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10163 | assert(getLangOpts().CPlusPlus && "ADL enabled in C"); |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 10164 | } |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10165 | #endif |
| 10166 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10167 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10168 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) { |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10169 | *Result = ExprError(); |
| 10170 | return true; |
| 10171 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 10172 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10173 | // Add the functions denoted by the callee to the set of candidate |
| 10174 | // functions, including those from argument-dependent lookup. |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10175 | AddOverloadedCallCandidates(ULE, Args, *CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10176 | |
| 10177 | // If we found nothing, try to recover. |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10178 | // BuildRecoveryCallExpr diagnoses the error itself, so we just bail |
| 10179 | // out if it fails. |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10180 | if (CandidateSet->empty()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10181 | // In Microsoft mode, if we are inside a template class member function then |
| 10182 | // create a type dependent CallExpr. The goal is to postpone name lookup |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 10183 | // to instantiation time to be able to search into type dependent base |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10184 | // classes. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10185 | if (getLangOpts().MicrosoftMode && CurContext->isDependentContext() && |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 10186 | (isa<FunctionDecl>(CurContext) || isa<CXXRecordDecl>(CurContext))) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10187 | CallExpr *CE = new (Context) CallExpr(Context, Fn, Args, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10188 | Context.DependentTy, VK_RValue, |
| 10189 | RParenLoc); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10190 | CE->setTypeDependent(true); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10191 | *Result = Owned(CE); |
| 10192 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10193 | } |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10194 | return false; |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 10195 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10196 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10197 | UnbridgedCasts.restore(); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10198 | return false; |
| 10199 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10200 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10201 | /// FinishOverloadedCallExpr - given an OverloadCandidateSet, builds and returns |
| 10202 | /// the completed call expression. If overload resolution fails, emits |
| 10203 | /// diagnostics and returns ExprError() |
| 10204 | static ExprResult FinishOverloadedCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
| 10205 | UnresolvedLookupExpr *ULE, |
| 10206 | SourceLocation LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10207 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10208 | SourceLocation RParenLoc, |
| 10209 | Expr *ExecConfig, |
| 10210 | OverloadCandidateSet *CandidateSet, |
| 10211 | OverloadCandidateSet::iterator *Best, |
| 10212 | OverloadingResult OverloadResult, |
| 10213 | bool AllowTypoCorrection) { |
| 10214 | if (CandidateSet->empty()) |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10215 | return BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10216 | RParenLoc, /*EmptyLookup=*/true, |
| 10217 | AllowTypoCorrection); |
| 10218 | |
| 10219 | switch (OverloadResult) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10220 | case OR_Success: { |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10221 | FunctionDecl *FDecl = (*Best)->Function; |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10222 | SemaRef.CheckUnresolvedLookupAccess(ULE, (*Best)->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 10223 | if (SemaRef.DiagnoseUseOfDecl(FDecl, ULE->getNameLoc())) |
| 10224 | return ExprError(); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10225 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10226 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
| 10227 | ExecConfig); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10228 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10229 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10230 | case OR_No_Viable_Function: { |
| 10231 | // Try to recover by looking for viable functions which the user might |
| 10232 | // have meant to call. |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10233 | ExprResult Recovery = BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10234 | Args, RParenLoc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10235 | /*EmptyLookup=*/false, |
| 10236 | AllowTypoCorrection); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10237 | if (!Recovery.isInvalid()) |
| 10238 | return Recovery; |
| 10239 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10240 | SemaRef.Diag(Fn->getLocStart(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10241 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10242 | << ULE->getName() << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10243 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, Args); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10244 | break; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10245 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10246 | |
| 10247 | case OR_Ambiguous: |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10248 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10249 | << ULE->getName() << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10250 | CandidateSet->NoteCandidates(SemaRef, OCD_ViableCandidates, Args); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10251 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10252 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10253 | case OR_Deleted: { |
| 10254 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_deleted_call) |
| 10255 | << (*Best)->Function->isDeleted() |
| 10256 | << ULE->getName() |
| 10257 | << SemaRef.getDeletedOrUnavailableSuffix((*Best)->Function) |
| 10258 | << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10259 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, Args); |
Argyrios Kyrtzidis | 0d579b6 | 2011-11-04 15:58:13 +0000 | [diff] [blame] | 10260 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10261 | // We emitted an error for the unvailable/deleted function call but keep |
| 10262 | // the call in the AST. |
| 10263 | FunctionDecl *FDecl = (*Best)->Function; |
| 10264 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10265 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
| 10266 | ExecConfig); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10267 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10268 | } |
| 10269 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 10270 | // Overload resolution failed. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10271 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10272 | } |
| 10273 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10274 | /// BuildOverloadedCallExpr - Given the call expression that calls Fn |
| 10275 | /// (which eventually refers to the declaration Func) and the call |
| 10276 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 10277 | /// to a specific function. If overload resolution succeeds, returns |
| 10278 | /// the call expression produced by overload resolution. |
| 10279 | /// Otherwise, emits diagnostics and returns ExprError. |
| 10280 | ExprResult Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, |
| 10281 | UnresolvedLookupExpr *ULE, |
| 10282 | SourceLocation LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10283 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10284 | SourceLocation RParenLoc, |
| 10285 | Expr *ExecConfig, |
| 10286 | bool AllowTypoCorrection) { |
| 10287 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
| 10288 | ExprResult result; |
| 10289 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10290 | if (buildOverloadedCallSet(S, Fn, ULE, Args, LParenLoc, &CandidateSet, |
| 10291 | &result)) |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10292 | return result; |
| 10293 | |
| 10294 | OverloadCandidateSet::iterator Best; |
| 10295 | OverloadingResult OverloadResult = |
| 10296 | CandidateSet.BestViableFunction(*this, Fn->getLocStart(), Best); |
| 10297 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10298 | return FinishOverloadedCallExpr(*this, S, Fn, ULE, LParenLoc, Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10299 | RParenLoc, ExecConfig, &CandidateSet, |
| 10300 | &Best, OverloadResult, |
| 10301 | AllowTypoCorrection); |
| 10302 | } |
| 10303 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10304 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 10305 | return Functions.size() > 1 || |
| 10306 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 10307 | } |
| 10308 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10309 | /// \brief Create a unary operation that may resolve to an overloaded |
| 10310 | /// operator. |
| 10311 | /// |
| 10312 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 10313 | /// |
| 10314 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 10315 | /// operator. |
| 10316 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 10317 | /// \param Fns The set of non-member functions that will be |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10318 | /// considered by overload resolution. The caller needs to build this |
| 10319 | /// set based on the context using, e.g., |
| 10320 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 10321 | /// set should not contain any member functions; those will be added |
| 10322 | /// by CreateOverloadedUnaryOp(). |
| 10323 | /// |
James Dennett | 8da1687 | 2012-06-22 10:32:46 +0000 | [diff] [blame] | 10324 | /// \param Input The input argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10325 | ExprResult |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10326 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 10327 | const UnresolvedSetImpl &Fns, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10328 | Expr *Input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10329 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10330 | |
| 10331 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 10332 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 10333 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10334 | // TODO: provide better source location info. |
| 10335 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10336 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10337 | if (checkPlaceholderForOverload(*this, Input)) |
| 10338 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10339 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10340 | Expr *Args[2] = { Input, 0 }; |
| 10341 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10342 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10343 | // For post-increment and post-decrement, add the implicit '0' as |
| 10344 | // the second argument, so that we know this is a post-increment or |
| 10345 | // post-decrement. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10346 | if (Opc == UO_PostInc || Opc == UO_PostDec) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10347 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 10348 | Args[1] = IntegerLiteral::Create(Context, Zero, Context.IntTy, |
| 10349 | SourceLocation()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10350 | NumArgs = 2; |
| 10351 | } |
| 10352 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10353 | ArrayRef<Expr *> ArgsArray(Args, NumArgs); |
| 10354 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10355 | if (Input->isTypeDependent()) { |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10356 | if (Fns.empty()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10357 | return Owned(new (Context) UnaryOperator(Input, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10358 | Opc, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10359 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10360 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10361 | OpLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10362 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10363 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10364 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 10365 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10366 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10367 | /*ADL*/ true, IsOverloaded(Fns), |
| 10368 | Fns.begin(), Fns.end()); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10369 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, ArgsArray, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10370 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10371 | VK_RValue, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10372 | OpLoc, false)); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10373 | } |
| 10374 | |
| 10375 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10376 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10377 | |
| 10378 | // Add the candidates from the given function set. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10379 | AddFunctionCandidates(Fns, ArgsArray, CandidateSet, false); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10380 | |
| 10381 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10382 | AddMemberOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10383 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10384 | // Add candidates from ADL. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10385 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, OpLoc, |
| 10386 | ArgsArray, /*ExplicitTemplateArgs*/ 0, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10387 | CandidateSet); |
| 10388 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10389 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10390 | AddBuiltinOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10391 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10392 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10393 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10394 | // Perform overload resolution. |
| 10395 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10396 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10397 | case OR_Success: { |
| 10398 | // We found a built-in operator or an overloaded operator. |
| 10399 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10400 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10401 | if (FnDecl) { |
| 10402 | // We matched an overloaded operator. Build a call to that |
| 10403 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10404 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10405 | // Convert the arguments. |
| 10406 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10407 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10408 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10409 | ExprResult InputRes = |
| 10410 | PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 10411 | Best->FoundDecl, Method); |
| 10412 | if (InputRes.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10413 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10414 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10415 | } else { |
| 10416 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10417 | ExprResult InputInit |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 10418 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10419 | Context, |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 10420 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10421 | SourceLocation(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10422 | Input); |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 10423 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10424 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10425 | Input = InputInit.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10426 | } |
| 10427 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10428 | // Determine the result type. |
| 10429 | QualType ResultTy = FnDecl->getResultType(); |
| 10430 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10431 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10432 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10433 | // Build the actual expression node. |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10434 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10435 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10436 | if (FnExpr.isInvalid()) |
| 10437 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10438 | |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 10439 | Args[0] = Input; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10440 | CallExpr *TheCall = |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10441 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), ArgsArray, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10442 | ResultTy, VK, OpLoc, false); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10443 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10444 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 10445 | FnDecl)) |
| 10446 | return ExprError(); |
| 10447 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10448 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10449 | } else { |
| 10450 | // We matched a built-in operator. Convert the arguments, then |
| 10451 | // break out so that we will build the appropriate built-in |
| 10452 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10453 | ExprResult InputRes = |
| 10454 | PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 10455 | Best->Conversions[0], AA_Passing); |
| 10456 | if (InputRes.isInvalid()) |
| 10457 | return ExprError(); |
| 10458 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10459 | break; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10460 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10461 | } |
| 10462 | |
| 10463 | case OR_No_Viable_Function: |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10464 | // This is an erroneous use of an operator which can be overloaded by |
| 10465 | // a non-member function. Check for non-member operators which were |
| 10466 | // defined too late to be candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10467 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, ArgsArray)) |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10468 | // FIXME: Recover by calling the found function. |
| 10469 | return ExprError(); |
| 10470 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10471 | // No viable function; fall through to handling this as a |
| 10472 | // built-in operator, which will produce an error message for us. |
| 10473 | break; |
| 10474 | |
| 10475 | case OR_Ambiguous: |
| 10476 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 10477 | << UnaryOperator::getOpcodeStr(Opc) |
| 10478 | << Input->getType() |
| 10479 | << Input->getSourceRange(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10480 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, ArgsArray, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10481 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
| 10482 | return ExprError(); |
| 10483 | |
| 10484 | case OR_Deleted: |
| 10485 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 10486 | << Best->Function->isDeleted() |
| 10487 | << UnaryOperator::getOpcodeStr(Opc) |
| 10488 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 10489 | << Input->getSourceRange(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10490 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, ArgsArray, |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 10491 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10492 | return ExprError(); |
| 10493 | } |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10494 | |
| 10495 | // Either we found no viable overloaded operator or we matched a |
| 10496 | // built-in operator. In either case, fall through to trying to |
| 10497 | // build a built-in operation. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10498 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10499 | } |
| 10500 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10501 | /// \brief Create a binary operation that may resolve to an overloaded |
| 10502 | /// operator. |
| 10503 | /// |
| 10504 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 10505 | /// |
| 10506 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 10507 | /// operator. |
| 10508 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 10509 | /// \param Fns The set of non-member functions that will be |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10510 | /// considered by overload resolution. The caller needs to build this |
| 10511 | /// set based on the context using, e.g., |
| 10512 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 10513 | /// set should not contain any member functions; those will be added |
| 10514 | /// by CreateOverloadedBinOp(). |
| 10515 | /// |
| 10516 | /// \param LHS Left-hand argument. |
| 10517 | /// \param RHS Right-hand argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10518 | ExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10519 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10520 | unsigned OpcIn, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10521 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10522 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10523 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10524 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10525 | |
| 10526 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 10527 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 10528 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 10529 | |
| 10530 | // If either side is type-dependent, create an appropriate dependent |
| 10531 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10532 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10533 | if (Fns.empty()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10534 | // If there are no functions to store, just build a dependent |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10535 | // BinaryOperator or CompoundAssignment. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10536 | if (Opc <= BO_Assign || Opc > BO_OrAssign) |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10537 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10538 | Context.DependentTy, |
| 10539 | VK_RValue, OK_Ordinary, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10540 | OpLoc, |
| 10541 | FPFeatures.fp_contract)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10542 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10543 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 10544 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10545 | VK_LValue, |
| 10546 | OK_Ordinary, |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10547 | Context.DependentTy, |
| 10548 | Context.DependentTy, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10549 | OpLoc, |
| 10550 | FPFeatures.fp_contract)); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10551 | } |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10552 | |
| 10553 | // FIXME: save results of ADL from here? |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10554 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10555 | // TODO: provide better source location info in DNLoc component. |
| 10556 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10557 | UnresolvedLookupExpr *Fn |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10558 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
| 10559 | NestedNameSpecifierLoc(), OpNameInfo, |
| 10560 | /*ADL*/ true, IsOverloaded(Fns), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10561 | Fns.begin(), Fns.end()); |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10562 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, Args, |
| 10563 | Context.DependentTy, VK_RValue, |
| 10564 | OpLoc, FPFeatures.fp_contract)); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10565 | } |
| 10566 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10567 | // Always do placeholder-like conversions on the RHS. |
| 10568 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 10569 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10570 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 10571 | // Do placeholder-like conversion on the LHS; note that we should |
| 10572 | // not get here with a PseudoObject LHS. |
| 10573 | assert(Args[0]->getObjectKind() != OK_ObjCProperty); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10574 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 10575 | return ExprError(); |
| 10576 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 10577 | // If this is the assignment operator, we only perform overload resolution |
| 10578 | // if the left-hand side is a class or enumeration type. This is actually |
| 10579 | // a hack. The standard requires that we do overload resolution between the |
| 10580 | // various built-in candidates, but as DR507 points out, this can lead to |
| 10581 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 10582 | // Note that we go the traditional code path for compound assignment forms. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10583 | if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10584 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10585 | |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10586 | // If this is the .* operator, which is not overloadable, just |
| 10587 | // create a built-in binary operator. |
| 10588 | if (Opc == BO_PtrMemD) |
| 10589 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 10590 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10591 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10592 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10593 | |
| 10594 | // Add the candidates from the given function set. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10595 | AddFunctionCandidates(Fns, Args, CandidateSet, false); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10596 | |
| 10597 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10598 | AddMemberOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10599 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10600 | // Add candidates from ADL. |
| 10601 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10602 | OpLoc, Args, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10603 | /*ExplicitTemplateArgs*/ 0, |
| 10604 | CandidateSet); |
| 10605 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10606 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10607 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10608 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10609 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10610 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10611 | // Perform overload resolution. |
| 10612 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10613 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 10614 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10615 | // We found a built-in operator or an overloaded operator. |
| 10616 | FunctionDecl *FnDecl = Best->Function; |
| 10617 | |
| 10618 | if (FnDecl) { |
| 10619 | // We matched an overloaded operator. Build a call to that |
| 10620 | // operator. |
| 10621 | |
| 10622 | // Convert the arguments. |
| 10623 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10624 | // Best->Access is only meaningful for class members. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10625 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10626 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10627 | ExprResult Arg1 = |
| 10628 | PerformCopyInitialization( |
| 10629 | InitializedEntity::InitializeParameter(Context, |
| 10630 | FnDecl->getParamDecl(0)), |
| 10631 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10632 | if (Arg1.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10633 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10634 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10635 | ExprResult Arg0 = |
| 10636 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 10637 | Best->FoundDecl, Method); |
| 10638 | if (Arg0.isInvalid()) |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10639 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10640 | Args[0] = Arg0.takeAs<Expr>(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10641 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10642 | } else { |
| 10643 | // Convert the arguments. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10644 | ExprResult Arg0 = PerformCopyInitialization( |
| 10645 | InitializedEntity::InitializeParameter(Context, |
| 10646 | FnDecl->getParamDecl(0)), |
| 10647 | SourceLocation(), Owned(Args[0])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10648 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10649 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10650 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10651 | ExprResult Arg1 = |
| 10652 | PerformCopyInitialization( |
| 10653 | InitializedEntity::InitializeParameter(Context, |
| 10654 | FnDecl->getParamDecl(1)), |
| 10655 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10656 | if (Arg1.isInvalid()) |
| 10657 | return ExprError(); |
| 10658 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 10659 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10660 | } |
| 10661 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10662 | // Determine the result type. |
| 10663 | QualType ResultTy = FnDecl->getResultType(); |
| 10664 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10665 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10666 | |
| 10667 | // Build the actual expression node. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10668 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10669 | Best->FoundDecl, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10670 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10671 | if (FnExpr.isInvalid()) |
| 10672 | return ExprError(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10673 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10674 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10675 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10676 | Args, ResultTy, VK, OpLoc, |
| 10677 | FPFeatures.fp_contract); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10678 | |
| 10679 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 10680 | FnDecl)) |
| 10681 | return ExprError(); |
| 10682 | |
Nick Lewycky | 9b7ea0d | 2013-01-24 02:03:08 +0000 | [diff] [blame] | 10683 | ArrayRef<const Expr *> ArgsArray(Args, 2); |
| 10684 | // Cut off the implicit 'this'. |
| 10685 | if (isa<CXXMethodDecl>(FnDecl)) |
| 10686 | ArgsArray = ArgsArray.slice(1); |
| 10687 | checkCall(FnDecl, ArgsArray, 0, isa<CXXMethodDecl>(FnDecl), OpLoc, |
| 10688 | TheCall->getSourceRange(), VariadicDoesNotApply); |
| 10689 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10690 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10691 | } else { |
| 10692 | // We matched a built-in operator. Convert the arguments, then |
| 10693 | // break out so that we will build the appropriate built-in |
| 10694 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10695 | ExprResult ArgsRes0 = |
| 10696 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 10697 | Best->Conversions[0], AA_Passing); |
| 10698 | if (ArgsRes0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10699 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10700 | Args[0] = ArgsRes0.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10701 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10702 | ExprResult ArgsRes1 = |
| 10703 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 10704 | Best->Conversions[1], AA_Passing); |
| 10705 | if (ArgsRes1.isInvalid()) |
| 10706 | return ExprError(); |
| 10707 | Args[1] = ArgsRes1.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10708 | break; |
| 10709 | } |
| 10710 | } |
| 10711 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10712 | case OR_No_Viable_Function: { |
| 10713 | // C++ [over.match.oper]p9: |
| 10714 | // If the operator is the operator , [...] and there are no |
| 10715 | // viable functions, then the operator is assumed to be the |
| 10716 | // built-in operator and interpreted according to clause 5. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10717 | if (Opc == BO_Comma) |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10718 | break; |
| 10719 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10720 | // For class as left operand for assignment or compound assigment |
| 10721 | // operator do not fall through to handling in built-in, but report that |
| 10722 | // no overloaded assignment operator found |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10723 | ExprResult Result = ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10724 | if (Args[0]->getType()->isRecordType() && |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10725 | Opc >= BO_Assign && Opc <= BO_OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 10726 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 10727 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10728 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Eli Friedman | 3f93d4c | 2013-08-28 20:35:35 +0000 | [diff] [blame] | 10729 | if (Args[0]->getType()->isIncompleteType()) { |
| 10730 | Diag(OpLoc, diag::note_assign_lhs_incomplete) |
| 10731 | << Args[0]->getType() |
| 10732 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10733 | } |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10734 | } else { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10735 | // This is an erroneous use of an operator which can be overloaded by |
| 10736 | // a non-member function. Check for non-member operators which were |
| 10737 | // defined too late to be candidates. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10738 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, Args)) |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10739 | // FIXME: Recover by calling the found function. |
| 10740 | return ExprError(); |
| 10741 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10742 | // No viable function; try to create a built-in operation, which will |
| 10743 | // produce an error. Then, show the non-viable candidates. |
| 10744 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 10745 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10746 | assert(Result.isInvalid() && |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10747 | "C++ binary operator overloading is missing candidates!"); |
| 10748 | if (Result.isInvalid()) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10749 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10750 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10751 | return Result; |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10752 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10753 | |
| 10754 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10755 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_binary) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10756 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10757 | << Args[0]->getType() << Args[1]->getType() |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10758 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10759 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10760 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10761 | return ExprError(); |
| 10762 | |
| 10763 | case OR_Deleted: |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10764 | if (isImplicitlyDeleted(Best->Function)) { |
| 10765 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
| 10766 | Diag(OpLoc, diag::err_ovl_deleted_special_oper) |
Richard Smith | 0f46e64 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 10767 | << Context.getRecordType(Method->getParent()) |
| 10768 | << getSpecialMember(Method); |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 10769 | |
Richard Smith | 0f46e64 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 10770 | // The user probably meant to call this special member. Just |
| 10771 | // explain why it's deleted. |
| 10772 | NoteDeletedFunction(Method); |
| 10773 | return ExprError(); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10774 | } else { |
| 10775 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 10776 | << Best->Function->isDeleted() |
| 10777 | << BinaryOperator::getOpcodeStr(Opc) |
| 10778 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 10779 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10780 | } |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10781 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 10782 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10783 | return ExprError(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 10784 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10785 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10786 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10787 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10788 | } |
| 10789 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10790 | ExprResult |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10791 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 10792 | SourceLocation RLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10793 | Expr *Base, Expr *Idx) { |
| 10794 | Expr *Args[2] = { Base, Idx }; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10795 | DeclarationName OpName = |
| 10796 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 10797 | |
| 10798 | // If either side is type-dependent, create an appropriate dependent |
| 10799 | // expression. |
| 10800 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 10801 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10802 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10803 | // CHECKME: no 'operator' keyword? |
| 10804 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
| 10805 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10806 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 10807 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10808 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10809 | /*ADL*/ true, /*Overloaded*/ false, |
| 10810 | UnresolvedSetIterator(), |
| 10811 | UnresolvedSetIterator()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 10812 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10813 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10814 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10815 | Args, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10816 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10817 | VK_RValue, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10818 | RLoc, false)); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10819 | } |
| 10820 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10821 | // Handle placeholders on both operands. |
| 10822 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 10823 | return ExprError(); |
| 10824 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 10825 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10826 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10827 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10828 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10829 | |
| 10830 | // Subscript can only be overloaded as a member function. |
| 10831 | |
| 10832 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10833 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10834 | |
| 10835 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10836 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10837 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10838 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10839 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10840 | // Perform overload resolution. |
| 10841 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10842 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10843 | case OR_Success: { |
| 10844 | // We found a built-in operator or an overloaded operator. |
| 10845 | FunctionDecl *FnDecl = Best->Function; |
| 10846 | |
| 10847 | if (FnDecl) { |
| 10848 | // We matched an overloaded operator. Build a call to that |
| 10849 | // operator. |
| 10850 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10851 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10852 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10853 | // Convert the arguments. |
| 10854 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10855 | ExprResult Arg0 = |
| 10856 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 10857 | Best->FoundDecl, Method); |
| 10858 | if (Arg0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10859 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10860 | Args[0] = Arg0.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10861 | |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10862 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10863 | ExprResult InputInit |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10864 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10865 | Context, |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10866 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10867 | SourceLocation(), |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10868 | Owned(Args[1])); |
| 10869 | if (InputInit.isInvalid()) |
| 10870 | return ExprError(); |
| 10871 | |
| 10872 | Args[1] = InputInit.takeAs<Expr>(); |
| 10873 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10874 | // Determine the result type |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10875 | QualType ResultTy = FnDecl->getResultType(); |
| 10876 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10877 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10878 | |
| 10879 | // Build the actual expression node. |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10880 | DeclarationNameInfo OpLocInfo(OpName, LLoc); |
| 10881 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10882 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10883 | Best->FoundDecl, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10884 | HadMultipleCandidates, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10885 | OpLocInfo.getLoc(), |
| 10886 | OpLocInfo.getInfo()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10887 | if (FnExpr.isInvalid()) |
| 10888 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10889 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10890 | CXXOperatorCallExpr *TheCall = |
| 10891 | new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10892 | FnExpr.take(), Args, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10893 | ResultTy, VK, RLoc, |
| 10894 | false); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10895 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10896 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10897 | FnDecl)) |
| 10898 | return ExprError(); |
| 10899 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10900 | return MaybeBindToTemporary(TheCall); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10901 | } else { |
| 10902 | // We matched a built-in operator. Convert the arguments, then |
| 10903 | // break out so that we will build the appropriate built-in |
| 10904 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10905 | ExprResult ArgsRes0 = |
| 10906 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 10907 | Best->Conversions[0], AA_Passing); |
| 10908 | if (ArgsRes0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10909 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10910 | Args[0] = ArgsRes0.take(); |
| 10911 | |
| 10912 | ExprResult ArgsRes1 = |
| 10913 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 10914 | Best->Conversions[1], AA_Passing); |
| 10915 | if (ArgsRes1.isInvalid()) |
| 10916 | return ExprError(); |
| 10917 | Args[1] = ArgsRes1.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10918 | |
| 10919 | break; |
| 10920 | } |
| 10921 | } |
| 10922 | |
| 10923 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10924 | if (CandidateSet.empty()) |
| 10925 | Diag(LLoc, diag::err_ovl_no_oper) |
| 10926 | << Args[0]->getType() << /*subscript*/ 0 |
| 10927 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10928 | else |
| 10929 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 10930 | << Args[0]->getType() |
| 10931 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10932 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10933 | "[]", LLoc); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10934 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10935 | } |
| 10936 | |
| 10937 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10938 | Diag(LLoc, diag::err_ovl_ambiguous_oper_binary) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10939 | << "[]" |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10940 | << Args[0]->getType() << Args[1]->getType() |
| 10941 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10942 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10943 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10944 | return ExprError(); |
| 10945 | |
| 10946 | case OR_Deleted: |
| 10947 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 10948 | << Best->Function->isDeleted() << "[]" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 10949 | << getDeletedOrUnavailableSuffix(Best->Function) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10950 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10951 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10952 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10953 | return ExprError(); |
| 10954 | } |
| 10955 | |
| 10956 | // We matched a built-in operator; build it. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10957 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10958 | } |
| 10959 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10960 | /// BuildCallToMemberFunction - Build a call to a member |
| 10961 | /// function. MemExpr is the expression that refers to the member |
| 10962 | /// function (and includes the object parameter), Args/NumArgs are the |
| 10963 | /// arguments to the function call (not including the object |
| 10964 | /// parameter). The caller needs to validate that the member |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10965 | /// expression refers to a non-static member function or an overloaded |
| 10966 | /// member function. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10967 | ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10968 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10969 | SourceLocation LParenLoc, |
| 10970 | MultiExprArg Args, |
| 10971 | SourceLocation RParenLoc) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10972 | assert(MemExprE->getType() == Context.BoundMemberTy || |
| 10973 | MemExprE->getType() == Context.OverloadTy); |
| 10974 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10975 | // Dig out the member expression. This holds both the object |
| 10976 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10977 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10978 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10979 | // Determine whether this is a call to a pointer-to-member function. |
| 10980 | if (BinaryOperator *op = dyn_cast<BinaryOperator>(NakedMemExpr)) { |
| 10981 | assert(op->getType() == Context.BoundMemberTy); |
| 10982 | assert(op->getOpcode() == BO_PtrMemD || op->getOpcode() == BO_PtrMemI); |
| 10983 | |
| 10984 | QualType fnType = |
| 10985 | op->getRHS()->getType()->castAs<MemberPointerType>()->getPointeeType(); |
| 10986 | |
| 10987 | const FunctionProtoType *proto = fnType->castAs<FunctionProtoType>(); |
| 10988 | QualType resultType = proto->getCallResultType(Context); |
| 10989 | ExprValueKind valueKind = Expr::getValueKindForType(proto->getResultType()); |
| 10990 | |
| 10991 | // Check that the object type isn't more qualified than the |
| 10992 | // member function we're calling. |
| 10993 | Qualifiers funcQuals = Qualifiers::fromCVRMask(proto->getTypeQuals()); |
| 10994 | |
| 10995 | QualType objectType = op->getLHS()->getType(); |
| 10996 | if (op->getOpcode() == BO_PtrMemI) |
| 10997 | objectType = objectType->castAs<PointerType>()->getPointeeType(); |
| 10998 | Qualifiers objectQuals = objectType.getQualifiers(); |
| 10999 | |
| 11000 | Qualifiers difference = objectQuals - funcQuals; |
| 11001 | difference.removeObjCGCAttr(); |
| 11002 | difference.removeAddressSpace(); |
| 11003 | if (difference) { |
| 11004 | std::string qualsString = difference.getAsString(); |
| 11005 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
| 11006 | << fnType.getUnqualifiedType() |
| 11007 | << qualsString |
| 11008 | << (qualsString.find(' ') == std::string::npos ? 1 : 2); |
| 11009 | } |
| 11010 | |
| 11011 | CXXMemberCallExpr *call |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11012 | = new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11013 | resultType, valueKind, RParenLoc); |
| 11014 | |
| 11015 | if (CheckCallReturnType(proto->getResultType(), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11016 | op->getRHS()->getLocStart(), |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11017 | call, 0)) |
| 11018 | return ExprError(); |
| 11019 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11020 | if (ConvertArgumentsForCall(call, op, 0, proto, Args, RParenLoc)) |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11021 | return ExprError(); |
| 11022 | |
Richard Trieu | e2a90b8 | 2013-06-22 02:30:38 +0000 | [diff] [blame] | 11023 | if (CheckOtherCall(call, proto)) |
| 11024 | return ExprError(); |
| 11025 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11026 | return MaybeBindToTemporary(call); |
| 11027 | } |
| 11028 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11029 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11030 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11031 | return ExprError(); |
| 11032 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11033 | MemberExpr *MemExpr; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11034 | CXXMethodDecl *Method = 0; |
John McCall | bb6fb46 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 11035 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11036 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11037 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 11038 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11039 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11040 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11041 | Qualifier = MemExpr->getQualifier(); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11042 | UnbridgedCasts.restore(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11043 | } else { |
| 11044 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11045 | Qualifier = UnresExpr->getQualifier(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11046 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11047 | QualType ObjectType = UnresExpr->getBaseType(); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11048 | Expr::Classification ObjectClassification |
| 11049 | = UnresExpr->isArrow()? Expr::Classification::makeSimpleLValue() |
| 11050 | : UnresExpr->getBase()->Classify(Context); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11051 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11052 | // Add overload candidates |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11053 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11054 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11055 | // FIXME: avoid copy. |
| 11056 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 11057 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 11058 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11059 | TemplateArgs = &TemplateArgsBuffer; |
| 11060 | } |
| 11061 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11062 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 11063 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 11064 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11065 | NamedDecl *Func = *I; |
| 11066 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 11067 | if (isa<UsingShadowDecl>(Func)) |
| 11068 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 11069 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11070 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 11071 | // Microsoft supports direct constructor calls. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11072 | if (getLangOpts().MicrosoftExt && isa<CXXConstructorDecl>(Func)) { |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11073 | AddOverloadCandidate(cast<CXXConstructorDecl>(Func), I.getPair(), |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11074 | Args, CandidateSet); |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 11075 | } else if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 11076 | // If explicit template arguments were provided, we can't call a |
| 11077 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11078 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 11079 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11080 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11081 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11082 | ObjectClassification, Args, CandidateSet, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11083 | /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11084 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11085 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11086 | I.getPair(), ActingDC, TemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11087 | ObjectType, ObjectClassification, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11088 | Args, CandidateSet, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 11089 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11090 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 11091 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11092 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11093 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 11094 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11095 | UnbridgedCasts.restore(); |
| 11096 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11097 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11098 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getLocStart(), |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 11099 | Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11100 | case OR_Success: |
| 11101 | Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11102 | FoundDecl = Best->FoundDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11103 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 11104 | if (DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc())) |
| 11105 | return ExprError(); |
Faisal Vali | d570a92 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 11106 | // If FoundDecl is different from Method (such as if one is a template |
| 11107 | // and the other a specialization), make sure DiagnoseUseOfDecl is |
| 11108 | // called on both. |
| 11109 | // FIXME: This would be more comprehensively addressed by modifying |
| 11110 | // DiagnoseUseOfDecl to accept both the FoundDecl and the decl |
| 11111 | // being used. |
| 11112 | if (Method != FoundDecl.getDecl() && |
| 11113 | DiagnoseUseOfDecl(Method, UnresExpr->getNameLoc())) |
| 11114 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11115 | break; |
| 11116 | |
| 11117 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11118 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11119 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 11120 | << DeclName << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11121 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11122 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11123 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11124 | |
| 11125 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11126 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 11127 | << DeclName << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11128 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11129 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11130 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11131 | |
| 11132 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11133 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11134 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11135 | << DeclName |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11136 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11137 | << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11138 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11139 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11140 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11141 | } |
| 11142 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11143 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11144 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11145 | // If overload resolution picked a static member, build a |
| 11146 | // non-member call based on that function. |
| 11147 | if (Method->isStatic()) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11148 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, Args, |
| 11149 | RParenLoc); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11150 | } |
| 11151 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11152 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11153 | } |
| 11154 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11155 | QualType ResultType = Method->getResultType(); |
| 11156 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 11157 | ResultType = ResultType.getNonLValueExprType(Context); |
| 11158 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11159 | assert(Method && "Member call to something that isn't a method?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11160 | CXXMemberCallExpr *TheCall = |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11161 | new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11162 | ResultType, VK, RParenLoc); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11163 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 11164 | // Check for a valid return type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11165 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11166 | TheCall, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11167 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11168 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11169 | // Convert the object argument (for a non-static member function call). |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11170 | // We only need to do this if there was actually an overload; otherwise |
| 11171 | // it was done at lookup. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11172 | if (!Method->isStatic()) { |
| 11173 | ExprResult ObjectArg = |
| 11174 | PerformObjectArgumentInitialization(MemExpr->getBase(), Qualifier, |
| 11175 | FoundDecl, Method); |
| 11176 | if (ObjectArg.isInvalid()) |
| 11177 | return ExprError(); |
| 11178 | MemExpr->setBase(ObjectArg.take()); |
| 11179 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11180 | |
| 11181 | // Convert the rest of the arguments |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11182 | const FunctionProtoType *Proto = |
| 11183 | Method->getType()->getAs<FunctionProtoType>(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11184 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11185 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11186 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11187 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11188 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11189 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11190 | if (CheckFunctionCall(Method, TheCall, Proto)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11191 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 11192 | |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11193 | if ((isa<CXXConstructorDecl>(CurContext) || |
| 11194 | isa<CXXDestructorDecl>(CurContext)) && |
| 11195 | TheCall->getMethodDecl()->isPure()) { |
| 11196 | const CXXMethodDecl *MD = TheCall->getMethodDecl(); |
| 11197 | |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 11198 | if (isa<CXXThisExpr>(MemExpr->getBase()->IgnoreParenCasts())) { |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11199 | Diag(MemExpr->getLocStart(), |
| 11200 | diag::warn_call_to_pure_virtual_member_function_from_ctor_dtor) |
| 11201 | << MD->getDeclName() << isa<CXXDestructorDecl>(CurContext) |
| 11202 | << MD->getParent()->getDeclName(); |
| 11203 | |
| 11204 | Diag(MD->getLocStart(), diag::note_previous_decl) << MD->getDeclName(); |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 11205 | } |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11206 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11207 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11208 | } |
| 11209 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11210 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 11211 | /// type (C++ [over.call.object]), which can end up invoking an |
| 11212 | /// overloaded function call operator (@c operator()) or performing a |
| 11213 | /// user-defined conversion on the object argument. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 11214 | ExprResult |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11215 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 11216 | SourceLocation LParenLoc, |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11217 | MultiExprArg Args, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11218 | SourceLocation RParenLoc) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11219 | if (checkPlaceholderForOverload(*this, Obj)) |
| 11220 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11221 | ExprResult Object = Owned(Obj); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11222 | |
| 11223 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11224 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11225 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11226 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11227 | assert(Object.get()->getType()->isRecordType() && "Requires object type argument"); |
| 11228 | const RecordType *Record = Object.get()->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11229 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11230 | // C++ [over.call.object]p1: |
| 11231 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 11232 | // 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] | 11233 | // candidate functions includes at least the function call |
| 11234 | // operators of T. The function call operators of T are obtained by |
| 11235 | // ordinary lookup of the name operator() in the context of |
| 11236 | // (E).operator(). |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11237 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 11238 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11239 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11240 | if (RequireCompleteType(LParenLoc, Object.get()->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11241 | diag::err_incomplete_object_call, Object.get())) |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11242 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11243 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 11244 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 11245 | LookupQualifiedName(R, Record->getDecl()); |
| 11246 | R.suppressDiagnostics(); |
| 11247 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11248 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 11249 | Oper != OperEnd; ++Oper) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11250 | AddMethodCandidate(Oper.getPair(), Object.get()->getType(), |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11251 | Object.get()->Classify(Context), |
| 11252 | Args, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 11253 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 11254 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11255 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11256 | // C++ [over.call.object]p2: |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11257 | // In addition, for each (non-explicit in C++0x) conversion function |
| 11258 | // declared in T of the form |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11259 | // |
| 11260 | // operator conversion-type-id () cv-qualifier; |
| 11261 | // |
| 11262 | // where cv-qualifier is the same cv-qualification as, or a |
| 11263 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 11264 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 11265 | // R", or the type "reference to pointer to function of |
| 11266 | // (P1,...,Pn) returning R", or the type "reference to function |
| 11267 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11268 | // is also considered as a candidate function. Similarly, |
| 11269 | // surrogate call functions are added to the set of candidate |
| 11270 | // functions for each conversion function declared in an |
| 11271 | // accessible base class provided the function is not hidden |
| 11272 | // within T by another intervening declaration. |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 11273 | std::pair<CXXRecordDecl::conversion_iterator, |
| 11274 | CXXRecordDecl::conversion_iterator> Conversions |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 11275 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 11276 | for (CXXRecordDecl::conversion_iterator |
| 11277 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11278 | NamedDecl *D = *I; |
| 11279 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 11280 | if (isa<UsingShadowDecl>(D)) |
| 11281 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11282 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 11283 | // Skip over templated conversion functions; they aren't |
| 11284 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11285 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 11286 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 11287 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11288 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11289 | if (!Conv->isExplicit()) { |
| 11290 | // Strip the reference type (if any) and then the pointer type (if |
| 11291 | // any) to get down to what might be a function type. |
| 11292 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 11293 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 11294 | ConvType = ConvPtrType->getPointeeType(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11295 | |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11296 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
| 11297 | { |
| 11298 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11299 | Object.get(), Args, CandidateSet); |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11300 | } |
| 11301 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11302 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11303 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11304 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11305 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11306 | // Perform overload resolution. |
| 11307 | OverloadCandidateSet::iterator Best; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11308 | switch (CandidateSet.BestViableFunction(*this, Object.get()->getLocStart(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11309 | Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11310 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11311 | // Overload resolution succeeded; we'll build the appropriate call |
| 11312 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11313 | break; |
| 11314 | |
| 11315 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11316 | if (CandidateSet.empty()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11317 | Diag(Object.get()->getLocStart(), diag::err_ovl_no_oper) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11318 | << Object.get()->getType() << /*call*/ 1 |
| 11319 | << Object.get()->getSourceRange(); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11320 | else |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11321 | Diag(Object.get()->getLocStart(), |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11322 | diag::err_ovl_no_viable_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11323 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11324 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11325 | break; |
| 11326 | |
| 11327 | case OR_Ambiguous: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11328 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11329 | diag::err_ovl_ambiguous_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11330 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11331 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11332 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11333 | |
| 11334 | case OR_Deleted: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11335 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11336 | diag::err_ovl_deleted_object_call) |
| 11337 | << Best->Function->isDeleted() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11338 | << Object.get()->getType() |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11339 | << getDeletedOrUnavailableSuffix(Best->Function) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11340 | << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11341 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11342 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11343 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11344 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 11345 | if (Best == CandidateSet.end()) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11346 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11347 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11348 | UnbridgedCasts.restore(); |
| 11349 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11350 | if (Best->Function == 0) { |
| 11351 | // Since there is no function declaration, this is one of the |
| 11352 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11353 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11354 | = cast<CXXConversionDecl>( |
| 11355 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 11356 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11357 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 11358 | if (DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc)) |
| 11359 | return ExprError(); |
Faisal Vali | d570a92 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 11360 | assert(Conv == Best->FoundDecl.getDecl() && |
| 11361 | "Found Decl & conversion-to-functionptr should be same, right?!"); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11362 | // We selected one of the surrogate functions that converts the |
| 11363 | // object parameter to a function pointer. Perform the conversion |
| 11364 | // on the object argument, then let ActOnCallExpr finish the job. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11365 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 11366 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 11367 | // and then call it. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11368 | ExprResult Call = BuildCXXMemberCallExpr(Object.get(), Best->FoundDecl, |
| 11369 | Conv, HadMultipleCandidates); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 11370 | if (Call.isInvalid()) |
| 11371 | return ExprError(); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 11372 | // Record usage of conversion in an implicit cast. |
| 11373 | Call = Owned(ImplicitCastExpr::Create(Context, Call.get()->getType(), |
| 11374 | CK_UserDefinedConversion, |
| 11375 | Call.get(), 0, VK_RValue)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11376 | |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11377 | return ActOnCallExpr(S, Call.get(), LParenLoc, Args, RParenLoc); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11378 | } |
| 11379 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11380 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 11381 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11382 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 11383 | // that calls this method, using Object for the implicit object |
| 11384 | // parameter and passing along the remaining arguments. |
| 11385 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Nico Weber | e0ff690 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 11386 | |
| 11387 | // An error diagnostic has already been printed when parsing the declaration. |
Nico Weber | 1a52a4d | 2012-11-09 08:38:04 +0000 | [diff] [blame] | 11388 | if (Method->isInvalidDecl()) |
Nico Weber | e0ff690 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 11389 | return ExprError(); |
| 11390 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11391 | const FunctionProtoType *Proto = |
| 11392 | Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11393 | |
| 11394 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11395 | unsigned NumArgsToCheck = Args.size(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11396 | |
| 11397 | // Build the full argument list for the method call (the |
| 11398 | // implicit object parameter is placed at the beginning of the |
| 11399 | // list). |
| 11400 | Expr **MethodArgs; |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11401 | if (Args.size() < NumArgsInProto) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11402 | NumArgsToCheck = NumArgsInProto; |
| 11403 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 11404 | } else { |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11405 | MethodArgs = new Expr*[Args.size() + 1]; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11406 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11407 | MethodArgs[0] = Object.get(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11408 | for (unsigned ArgIdx = 0, e = Args.size(); ArgIdx != e; ++ArgIdx) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11409 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11410 | |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11411 | DeclarationNameInfo OpLocInfo( |
| 11412 | Context.DeclarationNames.getCXXOperatorName(OO_Call), LParenLoc); |
| 11413 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LParenLoc, RParenLoc)); |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11414 | ExprResult NewFn = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11415 | HadMultipleCandidates, |
| 11416 | OpLocInfo.getLoc(), |
| 11417 | OpLocInfo.getInfo()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11418 | if (NewFn.isInvalid()) |
| 11419 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11420 | |
| 11421 | // Once we've built TheCall, all of the expressions are properly |
| 11422 | // owned. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11423 | QualType ResultTy = Method->getResultType(); |
| 11424 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11425 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11426 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11427 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11428 | new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn.take(), |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11429 | llvm::makeArrayRef(MethodArgs, Args.size()+1), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 11430 | ResultTy, VK, RParenLoc, false); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11431 | delete [] MethodArgs; |
| 11432 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11433 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall, |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 11434 | Method)) |
| 11435 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11436 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11437 | // We may have default arguments. If so, we need to allocate more |
| 11438 | // slots in the call for them. |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11439 | if (Args.size() < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 11440 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11441 | else if (Args.size() > NumArgsInProto) |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11442 | NumArgsToCheck = NumArgsInProto; |
| 11443 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11444 | bool IsError = false; |
| 11445 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11446 | // Initialize the implicit object parameter. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11447 | ExprResult ObjRes = |
| 11448 | PerformObjectArgumentInitialization(Object.get(), /*Qualifier=*/0, |
| 11449 | Best->FoundDecl, Method); |
| 11450 | if (ObjRes.isInvalid()) |
| 11451 | IsError = true; |
| 11452 | else |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11453 | Object = ObjRes; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11454 | TheCall->setArg(0, Object.take()); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11455 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11456 | // Check the argument types. |
| 11457 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11458 | Expr *Arg; |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11459 | if (i < Args.size()) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11460 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11461 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11462 | // Pass the argument. |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11463 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11464 | ExprResult InputInit |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11465 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 11466 | Context, |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11467 | Method->getParamDecl(i)), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11468 | SourceLocation(), Arg); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11469 | |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11470 | IsError |= InputInit.isInvalid(); |
| 11471 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11472 | } else { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11473 | ExprResult DefArg |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 11474 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 11475 | if (DefArg.isInvalid()) { |
| 11476 | IsError = true; |
| 11477 | break; |
| 11478 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11479 | |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 11480 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11481 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11482 | |
| 11483 | TheCall->setArg(i + 1, Arg); |
| 11484 | } |
| 11485 | |
| 11486 | // If this is a variadic call, handle args passed through "...". |
| 11487 | if (Proto->isVariadic()) { |
| 11488 | // Promote the arguments (C99 6.5.2.2p7). |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11489 | for (unsigned i = NumArgsInProto, e = Args.size(); i < e; i++) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11490 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0); |
| 11491 | IsError |= Arg.isInvalid(); |
| 11492 | TheCall->setArg(i + 1, Arg.take()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11493 | } |
| 11494 | } |
| 11495 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11496 | if (IsError) return true; |
| 11497 | |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11498 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11499 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11500 | if (CheckFunctionCall(Method, TheCall, Proto)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 11501 | return true; |
| 11502 | |
John McCall | 182f709 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 11503 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11504 | } |
| 11505 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11506 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11507 | /// (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] | 11508 | /// @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] | 11509 | ExprResult |
Kaelyn Uhrain | baaeb85 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 11510 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc, |
| 11511 | bool *NoArrowOperatorFound) { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11512 | assert(Base->getType()->isRecordType() && |
| 11513 | "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11514 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11515 | if (checkPlaceholderForOverload(*this, Base)) |
| 11516 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11517 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11518 | SourceLocation Loc = Base->getExprLoc(); |
| 11519 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11520 | // C++ [over.ref]p1: |
| 11521 | // |
| 11522 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 11523 | // for a class object x of type T if T::operator->() exists and if |
| 11524 | // the operator is selected as the best match function by the |
| 11525 | // overload resolution mechanism (13.3). |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11526 | DeclarationName OpName = |
| 11527 | Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11528 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 11529 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11530 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11531 | if (RequireCompleteType(Loc, Base->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11532 | diag::err_typecheck_incomplete_tag, Base)) |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 11533 | return ExprError(); |
| 11534 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 11535 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 11536 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 11537 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 11538 | |
| 11539 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11540 | Oper != OperEnd; ++Oper) { |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11541 | AddMethodCandidate(Oper.getPair(), Base->getType(), Base->Classify(Context), |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 11542 | None, CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11543 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11544 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11545 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11546 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11547 | // Perform overload resolution. |
| 11548 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11549 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11550 | case OR_Success: |
| 11551 | // Overload resolution succeeded; we'll build the call below. |
| 11552 | break; |
| 11553 | |
| 11554 | case OR_No_Viable_Function: |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11555 | if (CandidateSet.empty()) { |
| 11556 | QualType BaseType = Base->getType(); |
Kaelyn Uhrain | baaeb85 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 11557 | if (NoArrowOperatorFound) { |
| 11558 | // Report this specific error to the caller instead of emitting a |
| 11559 | // diagnostic, as requested. |
| 11560 | *NoArrowOperatorFound = true; |
| 11561 | return ExprError(); |
| 11562 | } |
Kaelyn Uhrain | d422434 | 2013-07-15 19:54:54 +0000 | [diff] [blame] | 11563 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
| 11564 | << BaseType << Base->getSourceRange(); |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11565 | if (BaseType->isRecordType() && !BaseType->isPointerType()) { |
Kaelyn Uhrain | d422434 | 2013-07-15 19:54:54 +0000 | [diff] [blame] | 11566 | Diag(OpLoc, diag::note_typecheck_member_reference_suggestion) |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11567 | << FixItHint::CreateReplacement(OpLoc, "."); |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11568 | } |
| 11569 | } else |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11570 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11571 | << "operator->" << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11572 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11573 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11574 | |
| 11575 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 11576 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 11577 | << "->" << Base->getType() << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11578 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11579 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11580 | |
| 11581 | case OR_Deleted: |
| 11582 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 11583 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11584 | << "->" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11585 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11586 | << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11587 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11588 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11589 | } |
| 11590 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11591 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
| 11592 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11593 | // Convert the object parameter. |
| 11594 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11595 | ExprResult BaseResult = |
| 11596 | PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 11597 | Best->FoundDecl, Method); |
| 11598 | if (BaseResult.isInvalid()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11599 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11600 | Base = BaseResult.take(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 11601 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11602 | // Build the operator call. |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11603 | ExprResult FnExpr = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11604 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11605 | if (FnExpr.isInvalid()) |
| 11606 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11607 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11608 | QualType ResultTy = Method->getResultType(); |
| 11609 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11610 | ResultTy = ResultTy.getNonLValueExprType(Context); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11611 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11612 | new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr.take(), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 11613 | Base, ResultTy, VK, OpLoc, false); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 11614 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11615 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 11616 | Method)) |
| 11617 | return ExprError(); |
Eli Friedman | d593190 | 2011-04-04 01:18:25 +0000 | [diff] [blame] | 11618 | |
| 11619 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11620 | } |
| 11621 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11622 | /// BuildLiteralOperatorCall - Build a UserDefinedLiteral by creating a call to |
| 11623 | /// a literal operator described by the provided lookup results. |
| 11624 | ExprResult Sema::BuildLiteralOperatorCall(LookupResult &R, |
| 11625 | DeclarationNameInfo &SuffixInfo, |
| 11626 | ArrayRef<Expr*> Args, |
| 11627 | SourceLocation LitEndLoc, |
| 11628 | TemplateArgumentListInfo *TemplateArgs) { |
| 11629 | SourceLocation UDSuffixLoc = SuffixInfo.getCXXLiteralOperatorNameLoc(); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11630 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11631 | OverloadCandidateSet CandidateSet(UDSuffixLoc); |
| 11632 | AddFunctionCandidates(R.asUnresolvedSet(), Args, CandidateSet, true, |
| 11633 | TemplateArgs); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11634 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11635 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11636 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11637 | // Perform overload resolution. This will usually be trivial, but might need |
| 11638 | // to perform substitutions for a literal operator template. |
| 11639 | OverloadCandidateSet::iterator Best; |
| 11640 | switch (CandidateSet.BestViableFunction(*this, UDSuffixLoc, Best)) { |
| 11641 | case OR_Success: |
| 11642 | case OR_Deleted: |
| 11643 | break; |
| 11644 | |
| 11645 | case OR_No_Viable_Function: |
| 11646 | Diag(UDSuffixLoc, diag::err_ovl_no_viable_function_in_call) |
| 11647 | << R.getLookupName(); |
| 11648 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
| 11649 | return ExprError(); |
| 11650 | |
| 11651 | case OR_Ambiguous: |
| 11652 | Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) << R.getLookupName(); |
| 11653 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
| 11654 | return ExprError(); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11655 | } |
| 11656 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11657 | FunctionDecl *FD = Best->Function; |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11658 | ExprResult Fn = CreateFunctionRefExpr(*this, FD, Best->FoundDecl, |
| 11659 | HadMultipleCandidates, |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11660 | SuffixInfo.getLoc(), |
| 11661 | SuffixInfo.getInfo()); |
| 11662 | if (Fn.isInvalid()) |
| 11663 | return true; |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11664 | |
| 11665 | // Check the argument types. This should almost always be a no-op, except |
| 11666 | // that array-to-pointer decay is applied to string literals. |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11667 | Expr *ConvArgs[2]; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 11668 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11669 | ExprResult InputInit = PerformCopyInitialization( |
| 11670 | InitializedEntity::InitializeParameter(Context, FD->getParamDecl(ArgIdx)), |
| 11671 | SourceLocation(), Args[ArgIdx]); |
| 11672 | if (InputInit.isInvalid()) |
| 11673 | return true; |
| 11674 | ConvArgs[ArgIdx] = InputInit.take(); |
| 11675 | } |
| 11676 | |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11677 | QualType ResultTy = FD->getResultType(); |
| 11678 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11679 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11680 | |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11681 | UserDefinedLiteral *UDL = |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 11682 | new (Context) UserDefinedLiteral(Context, Fn.take(), |
| 11683 | llvm::makeArrayRef(ConvArgs, Args.size()), |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11684 | ResultTy, VK, LitEndLoc, UDSuffixLoc); |
| 11685 | |
| 11686 | if (CheckCallReturnType(FD->getResultType(), UDSuffixLoc, UDL, FD)) |
| 11687 | return ExprError(); |
| 11688 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11689 | if (CheckFunctionCall(FD, UDL, NULL)) |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11690 | return ExprError(); |
| 11691 | |
| 11692 | return MaybeBindToTemporary(UDL); |
| 11693 | } |
| 11694 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11695 | /// Build a call to 'begin' or 'end' for a C++11 for-range statement. If the |
| 11696 | /// given LookupResult is non-empty, it is assumed to describe a member which |
| 11697 | /// will be invoked. Otherwise, the function will be found via argument |
| 11698 | /// dependent lookup. |
| 11699 | /// CallExpr is set to a valid expression and FRS_Success returned on success, |
| 11700 | /// otherwise CallExpr is set to ExprError() and some non-success value |
| 11701 | /// is returned. |
| 11702 | Sema::ForRangeStatus |
| 11703 | Sema::BuildForRangeBeginEndCall(Scope *S, SourceLocation Loc, |
| 11704 | SourceLocation RangeLoc, VarDecl *Decl, |
| 11705 | BeginEndFunction BEF, |
| 11706 | const DeclarationNameInfo &NameInfo, |
| 11707 | LookupResult &MemberLookup, |
| 11708 | OverloadCandidateSet *CandidateSet, |
| 11709 | Expr *Range, ExprResult *CallExpr) { |
| 11710 | CandidateSet->clear(); |
| 11711 | if (!MemberLookup.empty()) { |
| 11712 | ExprResult MemberRef = |
| 11713 | BuildMemberReferenceExpr(Range, Range->getType(), Loc, |
| 11714 | /*IsPtr=*/false, CXXScopeSpec(), |
| 11715 | /*TemplateKWLoc=*/SourceLocation(), |
| 11716 | /*FirstQualifierInScope=*/0, |
| 11717 | MemberLookup, |
| 11718 | /*TemplateArgs=*/0); |
| 11719 | if (MemberRef.isInvalid()) { |
| 11720 | *CallExpr = ExprError(); |
| 11721 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11722 | << RangeLoc << BEF << Range->getType(); |
| 11723 | return FRS_DiagnosticIssued; |
| 11724 | } |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 11725 | *CallExpr = ActOnCallExpr(S, MemberRef.get(), Loc, None, Loc, 0); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11726 | if (CallExpr->isInvalid()) { |
| 11727 | *CallExpr = ExprError(); |
| 11728 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11729 | << RangeLoc << BEF << Range->getType(); |
| 11730 | return FRS_DiagnosticIssued; |
| 11731 | } |
| 11732 | } else { |
| 11733 | UnresolvedSet<0> FoundNames; |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11734 | UnresolvedLookupExpr *Fn = |
| 11735 | UnresolvedLookupExpr::Create(Context, /*NamingClass=*/0, |
| 11736 | NestedNameSpecifierLoc(), NameInfo, |
| 11737 | /*NeedsADL=*/true, /*Overloaded=*/false, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 11738 | FoundNames.begin(), FoundNames.end()); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11739 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11740 | bool CandidateSetError = buildOverloadedCallSet(S, Fn, Fn, Range, Loc, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11741 | CandidateSet, CallExpr); |
| 11742 | if (CandidateSet->empty() || CandidateSetError) { |
| 11743 | *CallExpr = ExprError(); |
| 11744 | return FRS_NoViableFunction; |
| 11745 | } |
| 11746 | OverloadCandidateSet::iterator Best; |
| 11747 | OverloadingResult OverloadResult = |
| 11748 | CandidateSet->BestViableFunction(*this, Fn->getLocStart(), Best); |
| 11749 | |
| 11750 | if (OverloadResult == OR_No_Viable_Function) { |
| 11751 | *CallExpr = ExprError(); |
| 11752 | return FRS_NoViableFunction; |
| 11753 | } |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11754 | *CallExpr = FinishOverloadedCallExpr(*this, S, Fn, Fn, Loc, Range, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11755 | Loc, 0, CandidateSet, &Best, |
| 11756 | OverloadResult, |
| 11757 | /*AllowTypoCorrection=*/false); |
| 11758 | if (CallExpr->isInvalid() || OverloadResult != OR_Success) { |
| 11759 | *CallExpr = ExprError(); |
| 11760 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11761 | << RangeLoc << BEF << Range->getType(); |
| 11762 | return FRS_DiagnosticIssued; |
| 11763 | } |
| 11764 | } |
| 11765 | return FRS_Success; |
| 11766 | } |
| 11767 | |
| 11768 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11769 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 11770 | /// a C++ overloaded function (possibly with some parentheses and |
| 11771 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 11772 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 11773 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 11774 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11775 | FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11776 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11777 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 11778 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11779 | if (SubExpr == PE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11780 | return PE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11781 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11782 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11783 | } |
| 11784 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11785 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11786 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 11787 | Found, Fn); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11788 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11789 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 11790 | "Implicit cast type cannot be determined from overload"); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 11791 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11792 | if (SubExpr == ICE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11793 | return ICE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11794 | |
| 11795 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 11796 | ICE->getCastKind(), |
| 11797 | SubExpr, 0, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 11798 | ICE->getValueKind()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11799 | } |
| 11800 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11801 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11802 | assert(UnOp->getOpcode() == UO_AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11803 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 11804 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 11805 | if (Method->isStatic()) { |
| 11806 | // Do nothing: static member functions aren't any different |
| 11807 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11808 | } else { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11809 | // Fix the sub expression, which really has to be an |
| 11810 | // UnresolvedLookupExpr holding an overloaded member function |
| 11811 | // or template. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11812 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 11813 | Found, Fn); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11814 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11815 | return UnOp; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11816 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11817 | assert(isa<DeclRefExpr>(SubExpr) |
| 11818 | && "fixed to something other than a decl ref"); |
| 11819 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 11820 | && "fixed to a member ref with no nested name qualifier"); |
| 11821 | |
| 11822 | // We have taken the address of a pointer to member |
| 11823 | // function. Perform the computation here so that we get the |
| 11824 | // appropriate pointer to member type. |
| 11825 | QualType ClassType |
| 11826 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 11827 | QualType MemPtrType |
| 11828 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 11829 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11830 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, MemPtrType, |
| 11831 | VK_RValue, OK_Ordinary, |
| 11832 | UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 11833 | } |
| 11834 | } |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11835 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 11836 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11837 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11838 | return UnOp; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11839 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11840 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11841 | Context.getPointerType(SubExpr->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11842 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11843 | UnOp->getOperatorLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11844 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11845 | |
| 11846 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11847 | // FIXME: avoid copy. |
| 11848 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11849 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11850 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11851 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11852 | } |
| 11853 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11854 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 11855 | ULE->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11856 | ULE->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11857 | Fn, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 11858 | /*enclosing*/ false, // FIXME? |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11859 | ULE->getNameLoc(), |
| 11860 | Fn->getType(), |
| 11861 | VK_LValue, |
| 11862 | Found.getDecl(), |
| 11863 | TemplateArgs); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 11864 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11865 | DRE->setHadMultipleCandidates(ULE->getNumDecls() > 1); |
| 11866 | return DRE; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11867 | } |
| 11868 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11869 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11870 | // FIXME: avoid copy. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11871 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 11872 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 11873 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11874 | TemplateArgs = &TemplateArgsBuffer; |
| 11875 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11876 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11877 | Expr *Base; |
| 11878 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11879 | // If we're filling in a static method where we used to have an |
| 11880 | // implicit member access, rewrite to a simple decl ref. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11881 | if (MemExpr->isImplicitAccess()) { |
| 11882 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11883 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 11884 | MemExpr->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11885 | MemExpr->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11886 | Fn, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 11887 | /*enclosing*/ false, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11888 | MemExpr->getMemberLoc(), |
| 11889 | Fn->getType(), |
| 11890 | VK_LValue, |
| 11891 | Found.getDecl(), |
| 11892 | TemplateArgs); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 11893 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11894 | DRE->setHadMultipleCandidates(MemExpr->getNumDecls() > 1); |
| 11895 | return DRE; |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 11896 | } else { |
| 11897 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 11898 | if (MemExpr->getQualifier()) |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 11899 | Loc = MemExpr->getQualifierLoc().getBeginLoc(); |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 11900 | CheckCXXThisCapture(Loc); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 11901 | Base = new (Context) CXXThisExpr(Loc, |
| 11902 | MemExpr->getBaseType(), |
| 11903 | /*isImplicit=*/true); |
| 11904 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11905 | } else |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11906 | Base = MemExpr->getBase(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11907 | |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11908 | ExprValueKind valueKind; |
| 11909 | QualType type; |
| 11910 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 11911 | valueKind = VK_LValue; |
| 11912 | type = Fn->getType(); |
| 11913 | } else { |
| 11914 | valueKind = VK_RValue; |
| 11915 | type = Context.BoundMemberTy; |
| 11916 | } |
| 11917 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11918 | MemberExpr *ME = MemberExpr::Create(Context, Base, |
| 11919 | MemExpr->isArrow(), |
| 11920 | MemExpr->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11921 | MemExpr->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11922 | Fn, |
| 11923 | Found, |
| 11924 | MemExpr->getMemberNameInfo(), |
| 11925 | TemplateArgs, |
| 11926 | type, valueKind, OK_Ordinary); |
| 11927 | ME->setHadMultipleCandidates(true); |
Richard Smith | 4a03022 | 2012-11-14 07:06:31 +0000 | [diff] [blame] | 11928 | MarkMemberReferenced(ME); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11929 | return ME; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11930 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11931 | |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11932 | llvm_unreachable("Invalid reference to overloaded function"); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11933 | } |
| 11934 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11935 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11936 | DeclAccessPair Found, |
| 11937 | FunctionDecl *Fn) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11938 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 11939 | } |
| 11940 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 11941 | } // end namespace clang |