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" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Lex/Preprocessor.h" |
| 25 | #include "clang/Sema/Initialization.h" |
| 26 | #include "clang/Sema/Lookup.h" |
| 27 | #include "clang/Sema/SemaInternal.h" |
| 28 | #include "clang/Sema/Template.h" |
| 29 | #include "clang/Sema/TemplateDeduction.h" |
Douglas Gregor | 661b493 | 2010-09-12 04:28:07 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/DenseSet.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallPtrSet.h" |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 34 | #include <algorithm> |
| 35 | |
| 36 | namespace clang { |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 37 | using namespace sema; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 38 | |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 39 | /// A convenience routine for creating a decayed reference to a function. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 40 | static ExprResult |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 41 | CreateFunctionRefExpr(Sema &S, FunctionDecl *Fn, NamedDecl *FoundDecl, |
| 42 | bool HadMultipleCandidates, |
Douglas Gregor | 5b8968c | 2011-07-15 16:25:15 +0000 | [diff] [blame] | 43 | SourceLocation Loc = SourceLocation(), |
| 44 | const DeclarationNameLoc &LocInfo = DeclarationNameLoc()){ |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 45 | if (S.DiagnoseUseOfDecl(FoundDecl, Loc)) |
Faisal Vali | d570a92 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 46 | return ExprError(); |
| 47 | // If FoundDecl is different from Fn (such as if one is a template |
| 48 | // and the other a specialization), make sure DiagnoseUseOfDecl is |
| 49 | // called on both. |
| 50 | // FIXME: This would be more comprehensively addressed by modifying |
| 51 | // DiagnoseUseOfDecl to accept both the FoundDecl and the decl |
| 52 | // being used. |
| 53 | if (FoundDecl != Fn && S.DiagnoseUseOfDecl(Fn, Loc)) |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 54 | return ExprError(); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 55 | DeclRefExpr *DRE = new (S.Context) DeclRefExpr(Fn, false, Fn->getType(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 56 | VK_LValue, Loc, LocInfo); |
| 57 | if (HadMultipleCandidates) |
| 58 | DRE->setHadMultipleCandidates(true); |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 59 | |
| 60 | S.MarkDeclRefReferenced(DRE); |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 61 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 62 | ExprResult E = S.Owned(DRE); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 63 | E = S.DefaultFunctionArrayConversion(E.take()); |
| 64 | if (E.isInvalid()) |
| 65 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 66 | return E; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 67 | } |
| 68 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 69 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 70 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 71 | StandardConversionSequence &SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 72 | bool CStyle, |
| 73 | bool AllowObjCWritebackConversion); |
Sam Panzer | d012586 | 2012-08-16 02:38:47 +0000 | [diff] [blame] | 74 | |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 75 | static bool IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 76 | QualType &ToType, |
| 77 | bool InOverloadResolution, |
| 78 | StandardConversionSequence &SCS, |
| 79 | bool CStyle); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 80 | static OverloadingResult |
| 81 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 82 | UserDefinedConversionSequence& User, |
| 83 | OverloadCandidateSet& Conversions, |
| 84 | bool AllowExplicit); |
| 85 | |
| 86 | |
| 87 | static ImplicitConversionSequence::CompareKind |
| 88 | CompareStandardConversionSequences(Sema &S, |
| 89 | const StandardConversionSequence& SCS1, |
| 90 | const StandardConversionSequence& SCS2); |
| 91 | |
| 92 | static ImplicitConversionSequence::CompareKind |
| 93 | CompareQualificationConversions(Sema &S, |
| 94 | const StandardConversionSequence& SCS1, |
| 95 | const StandardConversionSequence& SCS2); |
| 96 | |
| 97 | static ImplicitConversionSequence::CompareKind |
| 98 | CompareDerivedToBaseConversions(Sema &S, |
| 99 | const StandardConversionSequence& SCS1, |
| 100 | const StandardConversionSequence& SCS2); |
| 101 | |
| 102 | |
| 103 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 104 | /// GetConversionCategory - Retrieve the implicit conversion |
| 105 | /// category corresponding to the given implicit conversion kind. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | ImplicitConversionCategory |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 107 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 108 | static const ImplicitConversionCategory |
| 109 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 110 | ICC_Identity, |
| 111 | ICC_Lvalue_Transformation, |
| 112 | ICC_Lvalue_Transformation, |
| 113 | ICC_Lvalue_Transformation, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 114 | ICC_Identity, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 115 | ICC_Qualification_Adjustment, |
| 116 | ICC_Promotion, |
| 117 | ICC_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 118 | ICC_Promotion, |
| 119 | ICC_Conversion, |
| 120 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 121 | ICC_Conversion, |
| 122 | ICC_Conversion, |
| 123 | ICC_Conversion, |
| 124 | ICC_Conversion, |
| 125 | ICC_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 126 | ICC_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 127 | ICC_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 128 | ICC_Conversion, |
| 129 | ICC_Conversion, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 130 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 131 | ICC_Conversion |
| 132 | }; |
| 133 | return Category[(int)Kind]; |
| 134 | } |
| 135 | |
| 136 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 137 | /// corresponding to the given implicit conversion kind. |
| 138 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 139 | static const ImplicitConversionRank |
| 140 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 141 | ICR_Exact_Match, |
| 142 | ICR_Exact_Match, |
| 143 | ICR_Exact_Match, |
| 144 | ICR_Exact_Match, |
| 145 | ICR_Exact_Match, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 146 | ICR_Exact_Match, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 147 | ICR_Promotion, |
| 148 | ICR_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 149 | ICR_Promotion, |
| 150 | ICR_Conversion, |
| 151 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 152 | ICR_Conversion, |
| 153 | ICR_Conversion, |
| 154 | ICR_Conversion, |
| 155 | ICR_Conversion, |
| 156 | ICR_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 157 | ICR_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 158 | ICR_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 159 | ICR_Conversion, |
| 160 | ICR_Conversion, |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 161 | ICR_Complex_Real_Conversion, |
| 162 | ICR_Conversion, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 163 | ICR_Conversion, |
| 164 | ICR_Writeback_Conversion |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 165 | }; |
| 166 | return Rank[(int)Kind]; |
| 167 | } |
| 168 | |
| 169 | /// GetImplicitConversionName - Return the name of this kind of |
| 170 | /// implicit conversion. |
| 171 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | 2550d70 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 172 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 173 | "No conversion", |
| 174 | "Lvalue-to-rvalue", |
| 175 | "Array-to-pointer", |
| 176 | "Function-to-pointer", |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 177 | "Noreturn adjustment", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 178 | "Qualification", |
| 179 | "Integral promotion", |
| 180 | "Floating point promotion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 181 | "Complex promotion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 182 | "Integral conversion", |
| 183 | "Floating conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 184 | "Complex conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 185 | "Floating-integral conversion", |
| 186 | "Pointer conversion", |
| 187 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 188 | "Boolean conversion", |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 189 | "Compatible-types conversion", |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 190 | "Derived-to-base conversion", |
| 191 | "Vector conversion", |
| 192 | "Vector splat", |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 193 | "Complex-real conversion", |
| 194 | "Block Pointer conversion", |
| 195 | "Transparent Union Conversion" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 196 | "Writeback conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 197 | }; |
| 198 | return Name[Kind]; |
| 199 | } |
| 200 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 201 | /// StandardConversionSequence - Set the standard conversion |
| 202 | /// sequence to the identity conversion. |
| 203 | void StandardConversionSequence::setAsIdentityConversion() { |
| 204 | First = ICK_Identity; |
| 205 | Second = ICK_Identity; |
| 206 | Third = ICK_Identity; |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 207 | DeprecatedStringLiteralToCharPtr = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 208 | QualificationIncludesObjCLifetime = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 209 | ReferenceBinding = false; |
| 210 | DirectBinding = false; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 211 | IsLvalueReference = true; |
| 212 | BindsToFunctionLvalue = false; |
| 213 | BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 214 | BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 215 | ObjCLifetimeConversionBinding = false; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 216 | CopyConstructor = 0; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 219 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 220 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 221 | /// implicit conversions. |
| 222 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 223 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 224 | if (GetConversionRank(First) > Rank) |
| 225 | Rank = GetConversionRank(First); |
| 226 | if (GetConversionRank(Second) > Rank) |
| 227 | Rank = GetConversionRank(Second); |
| 228 | if (GetConversionRank(Third) > Rank) |
| 229 | Rank = GetConversionRank(Third); |
| 230 | return Rank; |
| 231 | } |
| 232 | |
| 233 | /// isPointerConversionToBool - Determines whether this conversion is |
| 234 | /// 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] | 235 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 236 | /// (C++ 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 237 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 238 | // Note that FromType has not necessarily been transformed by the |
| 239 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 240 | // check for their presence as well as checking whether FromType is |
| 241 | // a pointer. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 242 | if (getToType(1)->isBooleanType() && |
John McCall | ddb0ce7 | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 243 | (getFromType()->isPointerType() || |
| 244 | getFromType()->isObjCObjectPointerType() || |
| 245 | getFromType()->isBlockPointerType() || |
Anders Carlsson | c8df0b6 | 2010-11-05 00:12:09 +0000 | [diff] [blame] | 246 | getFromType()->isNullPtrType() || |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 247 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 248 | return true; |
| 249 | |
| 250 | return false; |
| 251 | } |
| 252 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 253 | /// isPointerConversionToVoidPointer - Determines whether this |
| 254 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 255 | /// used as part of the ranking of standard conversion sequences (C++ |
| 256 | /// 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 257 | bool |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 258 | StandardConversionSequence:: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 260 | QualType FromType = getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 261 | QualType ToType = getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 262 | |
| 263 | // Note that FromType has not necessarily been transformed by the |
| 264 | // array-to-pointer implicit conversion, so check for its presence |
| 265 | // and redo the conversion to get a pointer. |
| 266 | if (First == ICK_Array_To_Pointer) |
| 267 | FromType = Context.getArrayDecayedType(FromType); |
| 268 | |
Douglas Gregor | f9af524 | 2011-04-15 20:45:44 +0000 | [diff] [blame] | 269 | if (Second == ICK_Pointer_Conversion && FromType->isAnyPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 270 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 271 | return ToPtrType->getPointeeType()->isVoidType(); |
| 272 | |
| 273 | return false; |
| 274 | } |
| 275 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 276 | /// Skip any implicit casts which could be either part of a narrowing conversion |
| 277 | /// or after one in an implicit conversion. |
| 278 | static const Expr *IgnoreNarrowingConversion(const Expr *Converted) { |
| 279 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Converted)) { |
| 280 | switch (ICE->getCastKind()) { |
| 281 | case CK_NoOp: |
| 282 | case CK_IntegralCast: |
| 283 | case CK_IntegralToBoolean: |
| 284 | case CK_IntegralToFloating: |
| 285 | case CK_FloatingToIntegral: |
| 286 | case CK_FloatingToBoolean: |
| 287 | case CK_FloatingCast: |
| 288 | Converted = ICE->getSubExpr(); |
| 289 | continue; |
| 290 | |
| 291 | default: |
| 292 | return Converted; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | return Converted; |
| 297 | } |
| 298 | |
| 299 | /// Check if this standard conversion sequence represents a narrowing |
| 300 | /// conversion, according to C++11 [dcl.init.list]p7. |
| 301 | /// |
| 302 | /// \param Ctx The AST context. |
| 303 | /// \param Converted The result of applying this standard conversion sequence. |
| 304 | /// \param ConstantValue If this is an NK_Constant_Narrowing conversion, the |
| 305 | /// value of the expression prior to the narrowing conversion. |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 306 | /// \param ConstantType If this is an NK_Constant_Narrowing conversion, the |
| 307 | /// type of the expression prior to the narrowing conversion. |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 308 | NarrowingKind |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 309 | StandardConversionSequence::getNarrowingKind(ASTContext &Ctx, |
| 310 | const Expr *Converted, |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 311 | APValue &ConstantValue, |
| 312 | QualType &ConstantType) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 313 | assert(Ctx.getLangOpts().CPlusPlus && "narrowing check outside C++"); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 314 | |
| 315 | // C++11 [dcl.init.list]p7: |
| 316 | // A narrowing conversion is an implicit conversion ... |
| 317 | QualType FromType = getToType(0); |
| 318 | QualType ToType = getToType(1); |
| 319 | switch (Second) { |
| 320 | // -- from a floating-point type to an integer type, or |
| 321 | // |
| 322 | // -- from an integer type or unscoped enumeration type to a floating-point |
| 323 | // type, except where the source is a constant expression and the actual |
| 324 | // value after conversion will fit into the target type and will produce |
| 325 | // the original value when converted back to the original type, or |
| 326 | case ICK_Floating_Integral: |
| 327 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
| 328 | return NK_Type_Narrowing; |
| 329 | } else if (FromType->isIntegralType(Ctx) && ToType->isRealFloatingType()) { |
| 330 | llvm::APSInt IntConstantValue; |
| 331 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| 332 | if (Initializer && |
| 333 | Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { |
| 334 | // Convert the integer to the floating type. |
| 335 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
| 336 | Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), |
| 337 | llvm::APFloat::rmNearestTiesToEven); |
| 338 | // And back. |
| 339 | llvm::APSInt ConvertedValue = IntConstantValue; |
| 340 | bool ignored; |
| 341 | Result.convertToInteger(ConvertedValue, |
| 342 | llvm::APFloat::rmTowardZero, &ignored); |
| 343 | // If the resulting value is different, this was a narrowing conversion. |
| 344 | if (IntConstantValue != ConvertedValue) { |
| 345 | ConstantValue = APValue(IntConstantValue); |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 346 | ConstantType = Initializer->getType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 347 | return NK_Constant_Narrowing; |
| 348 | } |
| 349 | } else { |
| 350 | // Variables are always narrowings. |
| 351 | return NK_Variable_Narrowing; |
| 352 | } |
| 353 | } |
| 354 | return NK_Not_Narrowing; |
| 355 | |
| 356 | // -- from long double to double or float, or from double to float, except |
| 357 | // where the source is a constant expression and the actual value after |
| 358 | // conversion is within the range of values that can be represented (even |
| 359 | // if it cannot be represented exactly), or |
| 360 | case ICK_Floating_Conversion: |
| 361 | if (FromType->isRealFloatingType() && ToType->isRealFloatingType() && |
| 362 | Ctx.getFloatingTypeOrder(FromType, ToType) == 1) { |
| 363 | // FromType is larger than ToType. |
| 364 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| 365 | if (Initializer->isCXX11ConstantExpr(Ctx, &ConstantValue)) { |
| 366 | // Constant! |
| 367 | assert(ConstantValue.isFloat()); |
| 368 | llvm::APFloat FloatVal = ConstantValue.getFloat(); |
| 369 | // Convert the source value into the target type. |
| 370 | bool ignored; |
| 371 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
| 372 | Ctx.getFloatTypeSemantics(ToType), |
| 373 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 374 | // If there was no overflow, the source value is within the range of |
| 375 | // values that can be represented. |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 376 | if (ConvertStatus & llvm::APFloat::opOverflow) { |
| 377 | ConstantType = Initializer->getType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 378 | return NK_Constant_Narrowing; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 379 | } |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 380 | } else { |
| 381 | return NK_Variable_Narrowing; |
| 382 | } |
| 383 | } |
| 384 | return NK_Not_Narrowing; |
| 385 | |
| 386 | // -- from an integer type or unscoped enumeration type to an integer type |
| 387 | // that cannot represent all the values of the original type, except where |
| 388 | // the source is a constant expression and the actual value after |
| 389 | // conversion will fit into the target type and will produce the original |
| 390 | // value when converted back to the original type. |
| 391 | case ICK_Boolean_Conversion: // Bools are integers too. |
| 392 | if (!FromType->isIntegralOrUnscopedEnumerationType()) { |
| 393 | // Boolean conversions can be from pointers and pointers to members |
| 394 | // [conv.bool], and those aren't considered narrowing conversions. |
| 395 | return NK_Not_Narrowing; |
| 396 | } // Otherwise, fall through to the integral case. |
| 397 | case ICK_Integral_Conversion: { |
| 398 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
| 399 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
| 400 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
| 401 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
| 402 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
| 403 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
| 404 | |
| 405 | if (FromWidth > ToWidth || |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 406 | (FromWidth == ToWidth && FromSigned != ToSigned) || |
| 407 | (FromSigned && !ToSigned)) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 408 | // Not all values of FromType can be represented in ToType. |
| 409 | llvm::APSInt InitializerValue; |
| 410 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 411 | if (!Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { |
| 412 | // Such conversions on variables are always narrowing. |
| 413 | return NK_Variable_Narrowing; |
Richard Smith | 5d7700e | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 414 | } |
| 415 | bool Narrowing = false; |
| 416 | if (FromWidth < ToWidth) { |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 417 | // Negative -> unsigned is narrowing. Otherwise, more bits is never |
| 418 | // narrowing. |
| 419 | if (InitializerValue.isSigned() && InitializerValue.isNegative()) |
Richard Smith | 5d7700e | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 420 | Narrowing = true; |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 421 | } else { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 422 | // Add a bit to the InitializerValue so we don't have to worry about |
| 423 | // signed vs. unsigned comparisons. |
| 424 | InitializerValue = InitializerValue.extend( |
| 425 | InitializerValue.getBitWidth() + 1); |
| 426 | // Convert the initializer to and from the target width and signed-ness. |
| 427 | llvm::APSInt ConvertedValue = InitializerValue; |
| 428 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
| 429 | ConvertedValue.setIsSigned(ToSigned); |
| 430 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
| 431 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
| 432 | // If the result is different, this was a narrowing conversion. |
Richard Smith | 5d7700e | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 433 | if (ConvertedValue != InitializerValue) |
| 434 | Narrowing = true; |
| 435 | } |
| 436 | if (Narrowing) { |
| 437 | ConstantType = Initializer->getType(); |
| 438 | ConstantValue = APValue(InitializerValue); |
| 439 | return NK_Constant_Narrowing; |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | return NK_Not_Narrowing; |
| 443 | } |
| 444 | |
| 445 | default: |
| 446 | // Other kinds of conversions are not narrowings. |
| 447 | return NK_Not_Narrowing; |
| 448 | } |
| 449 | } |
| 450 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 451 | /// DebugPrint - Print this standard conversion sequence to standard |
| 452 | /// error. Useful for debugging overloading issues. |
| 453 | void StandardConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 454 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 455 | bool PrintedSomething = false; |
| 456 | if (First != ICK_Identity) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 457 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 458 | PrintedSomething = true; |
| 459 | } |
| 460 | |
| 461 | if (Second != ICK_Identity) { |
| 462 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 463 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 464 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 465 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 466 | |
| 467 | if (CopyConstructor) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 468 | OS << " (by copy constructor)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 469 | } else if (DirectBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 470 | OS << " (direct reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 471 | } else if (ReferenceBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 472 | OS << " (reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 473 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 474 | PrintedSomething = true; |
| 475 | } |
| 476 | |
| 477 | if (Third != ICK_Identity) { |
| 478 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 479 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 480 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 481 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 482 | PrintedSomething = true; |
| 483 | } |
| 484 | |
| 485 | if (!PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 486 | OS << "No conversions required"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
| 490 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 491 | /// error. Useful for debugging overloading issues. |
| 492 | void UserDefinedConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 493 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 494 | if (Before.First || Before.Second || Before.Third) { |
| 495 | Before.DebugPrint(); |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 496 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 497 | } |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 498 | if (ConversionFunction) |
| 499 | OS << '\'' << *ConversionFunction << '\''; |
| 500 | else |
| 501 | OS << "aggregate initialization"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 502 | if (After.First || After.Second || After.Third) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 503 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 504 | After.DebugPrint(); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 509 | /// error. Useful for debugging overloading issues. |
| 510 | void ImplicitConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 511 | raw_ostream &OS = llvm::errs(); |
Richard Smith | 3082be2 | 2013-09-06 01:22:42 +0000 | [diff] [blame] | 512 | if (isListInitializationSequence()) { |
| 513 | OS << "List-initialization sequence: "; |
| 514 | if (isStdInitializerListElement()) |
| 515 | OS << "Worst std::initializer_list element conversion: "; |
| 516 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 517 | switch (ConversionKind) { |
| 518 | case StandardConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 519 | OS << "Standard conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 520 | Standard.DebugPrint(); |
| 521 | break; |
| 522 | case UserDefinedConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 523 | OS << "User-defined conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 524 | UserDefined.DebugPrint(); |
| 525 | break; |
| 526 | case EllipsisConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 527 | OS << "Ellipsis conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 528 | break; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 529 | case AmbiguousConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 530 | OS << "Ambiguous conversion"; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 531 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 532 | case BadConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 533 | OS << "Bad conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 534 | break; |
| 535 | } |
| 536 | |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 537 | OS << "\n"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 538 | } |
| 539 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 540 | void AmbiguousConversionSequence::construct() { |
| 541 | new (&conversions()) ConversionSet(); |
| 542 | } |
| 543 | |
| 544 | void AmbiguousConversionSequence::destruct() { |
| 545 | conversions().~ConversionSet(); |
| 546 | } |
| 547 | |
| 548 | void |
| 549 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 550 | FromTypePtr = O.FromTypePtr; |
| 551 | ToTypePtr = O.ToTypePtr; |
| 552 | new (&conversions()) ConversionSet(O.conversions()); |
| 553 | } |
| 554 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 555 | namespace { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 556 | // Structure used by DeductionFailureInfo to store |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 557 | // template argument information. |
| 558 | struct DFIArguments { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 559 | TemplateArgument FirstArg; |
| 560 | TemplateArgument SecondArg; |
| 561 | }; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 562 | // Structure used by DeductionFailureInfo to store |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 563 | // template parameter and template argument information. |
| 564 | struct DFIParamWithArguments : DFIArguments { |
| 565 | TemplateParameter Param; |
| 566 | }; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 567 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 568 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 569 | /// \brief Convert from Sema's representation of template deduction information |
| 570 | /// to the form used in overload-candidate information. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 571 | DeductionFailureInfo MakeDeductionFailureInfo(ASTContext &Context, |
| 572 | Sema::TemplateDeductionResult TDK, |
| 573 | TemplateDeductionInfo &Info) { |
| 574 | DeductionFailureInfo Result; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 575 | Result.Result = static_cast<unsigned>(TDK); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 576 | Result.HasDiagnostic = false; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 577 | Result.Data = 0; |
| 578 | switch (TDK) { |
| 579 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 580 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 581 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 582 | case Sema::TDK_TooManyArguments: |
| 583 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 584 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 585 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 586 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 587 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 588 | Result.Data = Info.Param.getOpaqueValue(); |
| 589 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 590 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 591 | case Sema::TDK_NonDeducedMismatch: { |
| 592 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 593 | DFIArguments *Saved = new (Context) DFIArguments; |
| 594 | Saved->FirstArg = Info.FirstArg; |
| 595 | Saved->SecondArg = Info.SecondArg; |
| 596 | Result.Data = Saved; |
| 597 | break; |
| 598 | } |
| 599 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 600 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 601 | case Sema::TDK_Underqualified: { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 602 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 603 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 604 | Saved->Param = Info.Param; |
| 605 | Saved->FirstArg = Info.FirstArg; |
| 606 | Saved->SecondArg = Info.SecondArg; |
| 607 | Result.Data = Saved; |
| 608 | break; |
| 609 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 610 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 611 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 612 | Result.Data = Info.take(); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 613 | if (Info.hasSFINAEDiagnostic()) { |
| 614 | PartialDiagnosticAt *Diag = new (Result.Diagnostic) PartialDiagnosticAt( |
| 615 | SourceLocation(), PartialDiagnostic::NullDiagnostic()); |
| 616 | Info.takeSFINAEDiagnostic(*Diag); |
| 617 | Result.HasDiagnostic = true; |
| 618 | } |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 619 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 620 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 621 | case Sema::TDK_FailedOverloadResolution: |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 622 | Result.Data = Info.Expression; |
| 623 | break; |
| 624 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 625 | case Sema::TDK_MiscellaneousDeductionFailure: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 626 | break; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 627 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 628 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 629 | return Result; |
| 630 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 631 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 632 | void DeductionFailureInfo::Destroy() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 633 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 634 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 635 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 636 | case Sema::TDK_InstantiationDepth: |
| 637 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 638 | case Sema::TDK_TooManyArguments: |
| 639 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 640 | case Sema::TDK_InvalidExplicitArguments: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 641 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 642 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 643 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 644 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 645 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 646 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | aaa045d | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 647 | // FIXME: Destroy the data? |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 648 | Data = 0; |
| 649 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 650 | |
| 651 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 652 | // FIXME: Destroy the template argument list? |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 653 | Data = 0; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 654 | if (PartialDiagnosticAt *Diag = getSFINAEDiagnostic()) { |
| 655 | Diag->~PartialDiagnosticAt(); |
| 656 | HasDiagnostic = false; |
| 657 | } |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 658 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 659 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 660 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 661 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 662 | break; |
| 663 | } |
| 664 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 665 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 666 | PartialDiagnosticAt *DeductionFailureInfo::getSFINAEDiagnostic() { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 667 | if (HasDiagnostic) |
| 668 | return static_cast<PartialDiagnosticAt*>(static_cast<void*>(Diagnostic)); |
| 669 | return 0; |
| 670 | } |
| 671 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 672 | TemplateParameter DeductionFailureInfo::getTemplateParameter() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 673 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 674 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 675 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 676 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 677 | case Sema::TDK_TooManyArguments: |
| 678 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 679 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 680 | case Sema::TDK_NonDeducedMismatch: |
| 681 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 682 | return TemplateParameter(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 683 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 684 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 685 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 686 | return TemplateParameter::getFromOpaqueValue(Data); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 687 | |
| 688 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 689 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 690 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 691 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 692 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 693 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 694 | break; |
| 695 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 696 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 697 | return TemplateParameter(); |
| 698 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 699 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 700 | TemplateArgumentList *DeductionFailureInfo::getTemplateArgumentList() { |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 701 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 702 | case Sema::TDK_Success: |
| 703 | case Sema::TDK_Invalid: |
| 704 | case Sema::TDK_InstantiationDepth: |
| 705 | case Sema::TDK_TooManyArguments: |
| 706 | case Sema::TDK_TooFewArguments: |
| 707 | case Sema::TDK_Incomplete: |
| 708 | case Sema::TDK_InvalidExplicitArguments: |
| 709 | case Sema::TDK_Inconsistent: |
| 710 | case Sema::TDK_Underqualified: |
| 711 | case Sema::TDK_NonDeducedMismatch: |
| 712 | case Sema::TDK_FailedOverloadResolution: |
| 713 | return 0; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 714 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 715 | case Sema::TDK_SubstitutionFailure: |
| 716 | return static_cast<TemplateArgumentList*>(Data); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 717 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 718 | // Unhandled |
| 719 | case Sema::TDK_MiscellaneousDeductionFailure: |
| 720 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | return 0; |
| 724 | } |
| 725 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 726 | const TemplateArgument *DeductionFailureInfo::getFirstArg() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 727 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 728 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 729 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 730 | case Sema::TDK_InstantiationDepth: |
| 731 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 732 | case Sema::TDK_TooManyArguments: |
| 733 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 734 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 735 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 736 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 737 | return 0; |
| 738 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 739 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 740 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 741 | case Sema::TDK_NonDeducedMismatch: |
| 742 | return &static_cast<DFIArguments*>(Data)->FirstArg; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 743 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 744 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 745 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 746 | break; |
| 747 | } |
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 | return 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 750 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 751 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 752 | const TemplateArgument *DeductionFailureInfo::getSecondArg() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 753 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 754 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 755 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 756 | case Sema::TDK_InstantiationDepth: |
| 757 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 758 | case Sema::TDK_TooManyArguments: |
| 759 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 760 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 761 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 762 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 763 | return 0; |
| 764 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 765 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 766 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 767 | case Sema::TDK_NonDeducedMismatch: |
| 768 | return &static_cast<DFIArguments*>(Data)->SecondArg; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 769 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 770 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 771 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 772 | break; |
| 773 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 774 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 775 | return 0; |
| 776 | } |
| 777 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 778 | Expr *DeductionFailureInfo::getExpr() { |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 779 | if (static_cast<Sema::TemplateDeductionResult>(Result) == |
| 780 | Sema::TDK_FailedOverloadResolution) |
| 781 | return static_cast<Expr*>(Data); |
| 782 | |
| 783 | return 0; |
| 784 | } |
| 785 | |
Benjamin Kramer | f5b132f | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 786 | void OverloadCandidateSet::destroyCandidates() { |
Richard Smith | e3898ac | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 787 | for (iterator i = begin(), e = end(); i != e; ++i) { |
Benjamin Kramer | 9e2822b | 2012-01-14 20:16:52 +0000 | [diff] [blame] | 788 | for (unsigned ii = 0, ie = i->NumConversions; ii != ie; ++ii) |
| 789 | i->Conversions[ii].~ImplicitConversionSequence(); |
Richard Smith | e3898ac | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 790 | if (!i->Viable && i->FailureKind == ovl_fail_bad_deduction) |
| 791 | i->DeductionFailure.Destroy(); |
| 792 | } |
Benjamin Kramer | f5b132f | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | void OverloadCandidateSet::clear() { |
| 796 | destroyCandidates(); |
Benjamin Kramer | 314f554 | 2012-01-14 19:31:39 +0000 | [diff] [blame] | 797 | NumInlineSequences = 0; |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 798 | Candidates.clear(); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 799 | Functions.clear(); |
| 800 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 801 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 802 | namespace { |
| 803 | class UnbridgedCastsSet { |
| 804 | struct Entry { |
| 805 | Expr **Addr; |
| 806 | Expr *Saved; |
| 807 | }; |
| 808 | SmallVector<Entry, 2> Entries; |
| 809 | |
| 810 | public: |
| 811 | void save(Sema &S, Expr *&E) { |
| 812 | assert(E->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 813 | Entry entry = { &E, E }; |
| 814 | Entries.push_back(entry); |
| 815 | E = S.stripARCUnbridgedCast(E); |
| 816 | } |
| 817 | |
| 818 | void restore() { |
| 819 | for (SmallVectorImpl<Entry>::iterator |
| 820 | i = Entries.begin(), e = Entries.end(); i != e; ++i) |
| 821 | *i->Addr = i->Saved; |
| 822 | } |
| 823 | }; |
| 824 | } |
| 825 | |
| 826 | /// checkPlaceholderForOverload - Do any interesting placeholder-like |
| 827 | /// preprocessing on the given expression. |
| 828 | /// |
| 829 | /// \param unbridgedCasts a collection to which to add unbridged casts; |
| 830 | /// without this, they will be immediately diagnosed as errors |
| 831 | /// |
| 832 | /// Return true on unrecoverable error. |
| 833 | static bool checkPlaceholderForOverload(Sema &S, Expr *&E, |
| 834 | UnbridgedCastsSet *unbridgedCasts = 0) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 835 | if (const BuiltinType *placeholder = E->getType()->getAsPlaceholderType()) { |
| 836 | // We can't handle overloaded expressions here because overload |
| 837 | // resolution might reasonably tweak them. |
| 838 | if (placeholder->getKind() == BuiltinType::Overload) return false; |
| 839 | |
| 840 | // If the context potentially accepts unbridged ARC casts, strip |
| 841 | // the unbridged cast and add it to the collection for later restoration. |
| 842 | if (placeholder->getKind() == BuiltinType::ARCUnbridgedCast && |
| 843 | unbridgedCasts) { |
| 844 | unbridgedCasts->save(S, E); |
| 845 | return false; |
| 846 | } |
| 847 | |
| 848 | // Go ahead and check everything else. |
| 849 | ExprResult result = S.CheckPlaceholderExpr(E); |
| 850 | if (result.isInvalid()) |
| 851 | return true; |
| 852 | |
| 853 | E = result.take(); |
| 854 | return false; |
| 855 | } |
| 856 | |
| 857 | // Nothing to do. |
| 858 | return false; |
| 859 | } |
| 860 | |
| 861 | /// checkArgPlaceholdersForOverload - Check a set of call operands for |
| 862 | /// placeholders. |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 863 | static bool checkArgPlaceholdersForOverload(Sema &S, |
| 864 | MultiExprArg Args, |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 865 | UnbridgedCastsSet &unbridged) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 866 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 867 | if (checkPlaceholderForOverload(S, Args[i], &unbridged)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 868 | return true; |
| 869 | |
| 870 | return false; |
| 871 | } |
| 872 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 873 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 874 | // overload of the declarations in Old. This routine returns false if |
| 875 | // New and Old cannot be overloaded, e.g., if New has the same |
| 876 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 877 | // declarations aren't functions (or function templates) at all. When |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 878 | // it does return false, MatchedDecl will point to the decl that New |
| 879 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 880 | // top of the underlying declaration. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 881 | // |
| 882 | // Example: Given the following input: |
| 883 | // |
| 884 | // void f(int, float); // #1 |
| 885 | // void f(int, int); // #2 |
| 886 | // int f(int, int); // #3 |
| 887 | // |
| 888 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 889 | // so IsOverload will not be used. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 890 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 891 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 892 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 893 | // (since they have different signatures), so this routine returns |
| 894 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 895 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 896 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 897 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 898 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 899 | // identical (return types of functions are not part of the |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 900 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 901 | // point to the FunctionDecl for #2. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 902 | // |
| 903 | // 'NewIsUsingShadowDecl' indicates that 'New' is being introduced |
| 904 | // into a class by a using declaration. The rules for whether to hide |
| 905 | // shadow declarations ignore some properties which otherwise figure |
| 906 | // into a function template's signature. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 907 | Sema::OverloadKind |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 908 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
| 909 | NamedDecl *&Match, bool NewIsUsingDecl) { |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 910 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 911 | I != E; ++I) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 912 | NamedDecl *OldD = *I; |
| 913 | |
| 914 | bool OldIsUsingDecl = false; |
| 915 | if (isa<UsingShadowDecl>(OldD)) { |
| 916 | OldIsUsingDecl = true; |
| 917 | |
| 918 | // We can always introduce two using declarations into the same |
| 919 | // context, even if they have identical signatures. |
| 920 | if (NewIsUsingDecl) continue; |
| 921 | |
| 922 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 923 | } |
| 924 | |
| 925 | // If either declaration was introduced by a using declaration, |
| 926 | // we'll need to use slightly different rules for matching. |
| 927 | // Essentially, these rules are the normal rules, except that |
| 928 | // function templates hide function templates with different |
| 929 | // return types or template parameter lists. |
| 930 | bool UseMemberUsingDeclRules = |
John McCall | 78037ac | 2013-04-03 21:19:47 +0000 | [diff] [blame] | 931 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord() && |
| 932 | !New->getFriendObjectKind(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 933 | |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 934 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 935 | if (!IsOverload(New, OldT->getTemplatedDecl(), UseMemberUsingDeclRules)) { |
| 936 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 937 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 938 | continue; |
| 939 | } |
| 940 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 941 | Match = *I; |
| 942 | return Ovl_Match; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 943 | } |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 944 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 945 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
| 946 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 947 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 948 | continue; |
| 949 | } |
| 950 | |
Rafael Espindola | 90cc390 | 2013-04-15 12:49:13 +0000 | [diff] [blame] | 951 | if (!shouldLinkPossiblyHiddenDecl(*I, New)) |
| 952 | continue; |
| 953 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 954 | Match = *I; |
| 955 | return Ovl_Match; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 956 | } |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 957 | } else if (isa<UsingDecl>(OldD)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 958 | // We can overload with these, which can show up when doing |
| 959 | // redeclaration checks for UsingDecls. |
| 960 | assert(Old.getLookupKind() == LookupUsingDeclName); |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 961 | } else if (isa<TagDecl>(OldD)) { |
| 962 | // We can always overload with tags by hiding them. |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 963 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 964 | // Optimistically assume that an unresolved using decl will |
| 965 | // overload; if it doesn't, we'll have to diagnose during |
| 966 | // template instantiation. |
| 967 | } else { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 968 | // (C++ 13p1): |
| 969 | // Only function declarations can be overloaded; object and type |
| 970 | // declarations cannot be overloaded. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 971 | Match = *I; |
| 972 | return Ovl_NonFunction; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 973 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 974 | } |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 975 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 976 | return Ovl_Overload; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 979 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| 980 | bool UseUsingDeclRules) { |
| 981 | // C++ [basic.start.main]p2: This function shall not be overloaded. |
| 982 | if (New->isMain()) |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 983 | return false; |
Rafael Espindola | 7a525ac | 2013-01-12 01:47:40 +0000 | [diff] [blame] | 984 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 985 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 986 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 987 | |
| 988 | // C++ [temp.fct]p2: |
| 989 | // A function template can be overloaded with other function templates |
| 990 | // and with normal (non-template) functions. |
| 991 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 992 | return true; |
| 993 | |
| 994 | // Is the function New an overload of the function Old? |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 995 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 996 | QualType NewQType = Context.getCanonicalType(New->getType()); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 997 | |
| 998 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 999 | // determine whether they are overloads. If we find any mismatch |
| 1000 | // in the signature, they are overloads. |
| 1001 | |
| 1002 | // If either of these functions is a K&R-style function (no |
| 1003 | // prototype), then we consider them to have matching signatures. |
| 1004 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 1005 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 1006 | return false; |
| 1007 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1008 | const FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 1009 | const FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1010 | |
| 1011 | // The signature of a function includes the types of its |
| 1012 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 1013 | // of the ellipsis; see C++ DR 357). |
| 1014 | if (OldQType != NewQType && |
| 1015 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 1016 | OldType->isVariadic() != NewType->isVariadic() || |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1017 | !FunctionArgTypesAreEqual(OldType, NewType))) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1018 | return true; |
| 1019 | |
| 1020 | // C++ [temp.over.link]p4: |
| 1021 | // The signature of a function template consists of its function |
| 1022 | // signature, its return type and its template parameter list. The names |
| 1023 | // of the template parameters are significant only for establishing the |
| 1024 | // relationship between the template parameters and the rest of the |
| 1025 | // signature. |
| 1026 | // |
| 1027 | // We check the return type and template parameter lists for function |
| 1028 | // templates first; the remaining checks follow. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 1029 | // |
| 1030 | // However, we don't consider either of these when deciding whether |
| 1031 | // a member introduced by a shadow declaration is hidden. |
| 1032 | if (!UseUsingDeclRules && NewTemplate && |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1033 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 1034 | OldTemplate->getTemplateParameters(), |
| 1035 | false, TPL_TemplateMatch) || |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1036 | OldType->getResultType() != NewType->getResultType())) |
| 1037 | return true; |
| 1038 | |
| 1039 | // If the function is a class member, its signature includes the |
Douglas Gregor | 57c9f4f | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 1040 | // 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] | 1041 | // |
| 1042 | // As part of this, also check whether one of the member functions |
| 1043 | // is static, in which case they are not overloads (C++ |
| 1044 | // 13.1p2). While not part of the definition of the signature, |
| 1045 | // this check is important to determine whether these functions |
| 1046 | // can be overloaded. |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1047 | CXXMethodDecl *OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 1048 | CXXMethodDecl *NewMethod = dyn_cast<CXXMethodDecl>(New); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1049 | if (OldMethod && NewMethod && |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1050 | !OldMethod->isStatic() && !NewMethod->isStatic()) { |
| 1051 | if (OldMethod->getRefQualifier() != NewMethod->getRefQualifier()) { |
| 1052 | if (!UseUsingDeclRules && |
| 1053 | (OldMethod->getRefQualifier() == RQ_None || |
| 1054 | NewMethod->getRefQualifier() == RQ_None)) { |
| 1055 | // C++0x [over.load]p2: |
| 1056 | // - Member function declarations with the same name and the same |
| 1057 | // parameter-type-list as well as member function template |
| 1058 | // declarations with the same name, the same parameter-type-list, and |
| 1059 | // the same template parameter lists cannot be overloaded if any of |
| 1060 | // them, but not all, have a ref-qualifier (8.3.5). |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1061 | Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1062 | << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1063 | Diag(OldMethod->getLocation(), diag::note_previous_declaration); |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1064 | } |
| 1065 | return true; |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1066 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1067 | |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1068 | // We may not have applied the implicit const for a constexpr member |
| 1069 | // function yet (because we haven't yet resolved whether this is a static |
| 1070 | // or non-static member function). Add it now, on the assumption that this |
| 1071 | // is a redeclaration of OldMethod. |
| 1072 | unsigned NewQuals = NewMethod->getTypeQualifiers(); |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1073 | if (!getLangOpts().CPlusPlus1y && NewMethod->isConstexpr() && |
Richard Smith | db2fe73 | 2013-06-25 18:46:26 +0000 | [diff] [blame] | 1074 | !isa<CXXConstructorDecl>(NewMethod)) |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1075 | NewQuals |= Qualifiers::Const; |
| 1076 | if (OldMethod->getTypeQualifiers() != NewQuals) |
| 1077 | return true; |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1078 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1079 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1080 | // The signatures match; this is not an overload. |
| 1081 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 1084 | /// \brief Checks availability of the function depending on the current |
| 1085 | /// function context. Inside an unavailable function, unavailability is ignored. |
| 1086 | /// |
| 1087 | /// \returns true if \arg FD is unavailable and current context is inside |
| 1088 | /// an available function, false otherwise. |
| 1089 | bool Sema::isFunctionConsideredUnavailable(FunctionDecl *FD) { |
| 1090 | return FD->isUnavailable() && !cast<Decl>(CurContext)->isUnavailable(); |
| 1091 | } |
| 1092 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1093 | /// \brief Tries a user-defined conversion from From to ToType. |
| 1094 | /// |
| 1095 | /// Produces an implicit conversion sequence for when a standard conversion |
| 1096 | /// is not an option. See TryImplicitConversion for more information. |
| 1097 | static ImplicitConversionSequence |
| 1098 | TryUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 1099 | bool SuppressUserConversions, |
| 1100 | bool AllowExplicit, |
| 1101 | bool InOverloadResolution, |
| 1102 | bool CStyle, |
| 1103 | bool AllowObjCWritebackConversion) { |
| 1104 | ImplicitConversionSequence ICS; |
| 1105 | |
| 1106 | if (SuppressUserConversions) { |
| 1107 | // We're not in the case above, so there is no conversion that |
| 1108 | // we can perform. |
| 1109 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1110 | return ICS; |
| 1111 | } |
| 1112 | |
| 1113 | // Attempt user-defined conversion. |
| 1114 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 1115 | OverloadingResult UserDefResult |
| 1116 | = IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, Conversions, |
| 1117 | AllowExplicit); |
| 1118 | |
| 1119 | if (UserDefResult == OR_Success) { |
| 1120 | ICS.setUserDefined(); |
| 1121 | // C++ [over.ics.user]p4: |
| 1122 | // A conversion of an expression of class type to the same class |
| 1123 | // type is given Exact Match rank, and a conversion of an |
| 1124 | // expression of class type to a base class of that type is |
| 1125 | // given Conversion rank, in spite of the fact that a copy |
| 1126 | // constructor (i.e., a user-defined conversion function) is |
| 1127 | // called for those cases. |
| 1128 | if (CXXConstructorDecl *Constructor |
| 1129 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
| 1130 | QualType FromCanon |
| 1131 | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 1132 | QualType ToCanon |
| 1133 | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 1134 | if (Constructor->isCopyConstructor() && |
| 1135 | (FromCanon == ToCanon || S.IsDerivedFrom(FromCanon, ToCanon))) { |
| 1136 | // Turn this into a "standard" conversion sequence, so that it |
| 1137 | // gets ranked with standard conversion sequences. |
| 1138 | ICS.setStandard(); |
| 1139 | ICS.Standard.setAsIdentityConversion(); |
| 1140 | ICS.Standard.setFromType(From->getType()); |
| 1141 | ICS.Standard.setAllToTypes(ToType); |
| 1142 | ICS.Standard.CopyConstructor = Constructor; |
| 1143 | if (ToCanon != FromCanon) |
| 1144 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | // C++ [over.best.ics]p4: |
| 1149 | // However, when considering the argument of a user-defined |
| 1150 | // conversion function that is a candidate by 13.3.1.3 when |
| 1151 | // invoked for the copying of the temporary in the second step |
| 1152 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 1153 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 1154 | // ellipsis conversion sequences are allowed. |
| 1155 | if (SuppressUserConversions && ICS.isUserDefined()) { |
| 1156 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
| 1157 | } |
| 1158 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
| 1159 | ICS.setAmbiguous(); |
| 1160 | ICS.Ambiguous.setFromType(From->getType()); |
| 1161 | ICS.Ambiguous.setToType(ToType); |
| 1162 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 1163 | Cand != Conversions.end(); ++Cand) |
| 1164 | if (Cand->Viable) |
| 1165 | ICS.Ambiguous.addConversion(Cand->Function); |
| 1166 | } else { |
| 1167 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1168 | } |
| 1169 | |
| 1170 | return ICS; |
| 1171 | } |
| 1172 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1173 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 1174 | /// from the given expression (Expr) to the given type (ToType). This |
| 1175 | /// function returns an implicit conversion sequence that can be used |
| 1176 | /// to perform the initialization. Given |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1177 | /// |
| 1178 | /// void f(float f); |
| 1179 | /// void g(int i) { f(i); } |
| 1180 | /// |
| 1181 | /// this routine would produce an implicit conversion sequence to |
| 1182 | /// describe the initialization of f from i, which will be a standard |
| 1183 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 1184 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 1185 | // |
| 1186 | /// Note that this routine only determines how the conversion can be |
| 1187 | /// performed; it does not actually perform the conversion. As such, |
| 1188 | /// it will not produce any diagnostics if no conversion is available, |
| 1189 | /// but will instead return an implicit conversion sequence of kind |
| 1190 | /// "BadConversion". |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1191 | /// |
| 1192 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 1193 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1194 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 1195 | /// permitted. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1196 | /// |
| 1197 | /// \param AllowObjCWritebackConversion Whether we allow the Objective-C |
| 1198 | /// writeback conversion, which allows __autoreleasing id* parameters to |
| 1199 | /// be initialized with __strong id* or __weak id* arguments. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1200 | static ImplicitConversionSequence |
| 1201 | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
| 1202 | bool SuppressUserConversions, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1203 | bool AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1204 | bool InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1205 | bool CStyle, |
| 1206 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1207 | ImplicitConversionSequence ICS; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1208 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1209 | ICS.Standard, CStyle, AllowObjCWritebackConversion)){ |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1210 | ICS.setStandard(); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1211 | return ICS; |
| 1212 | } |
| 1213 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1214 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 1215 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1216 | return ICS; |
| 1217 | } |
| 1218 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1219 | // C++ [over.ics.user]p4: |
| 1220 | // A conversion of an expression of class type to the same class |
| 1221 | // type is given Exact Match rank, and a conversion of an |
| 1222 | // expression of class type to a base class of that type is |
| 1223 | // given Conversion rank, in spite of the fact that a copy/move |
| 1224 | // constructor (i.e., a user-defined conversion function) is |
| 1225 | // called for those cases. |
| 1226 | QualType FromType = From->getType(); |
| 1227 | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1228 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
| 1229 | S.IsDerivedFrom(FromType, ToType))) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1230 | ICS.setStandard(); |
| 1231 | ICS.Standard.setAsIdentityConversion(); |
| 1232 | ICS.Standard.setFromType(FromType); |
| 1233 | ICS.Standard.setAllToTypes(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1234 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1235 | // We don't actually check at this point whether there is a valid |
| 1236 | // copy/move constructor, since overloading just assumes that it |
| 1237 | // exists. When we actually perform initialization, we'll find the |
| 1238 | // appropriate constructor to copy the returned object, if needed. |
| 1239 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1240 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1241 | // Determine whether this is considered a derived-to-base conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1242 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1243 | ICS.Standard.Second = ICK_Derived_To_Base; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1244 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1245 | return ICS; |
| 1246 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1247 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1248 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 1249 | AllowExplicit, InOverloadResolution, CStyle, |
| 1250 | AllowObjCWritebackConversion); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1253 | ImplicitConversionSequence |
| 1254 | Sema::TryImplicitConversion(Expr *From, QualType ToType, |
| 1255 | bool SuppressUserConversions, |
| 1256 | bool AllowExplicit, |
| 1257 | bool InOverloadResolution, |
| 1258 | bool CStyle, |
| 1259 | bool AllowObjCWritebackConversion) { |
| 1260 | return clang::TryImplicitConversion(*this, From, ToType, |
| 1261 | SuppressUserConversions, AllowExplicit, |
| 1262 | InOverloadResolution, CStyle, |
| 1263 | AllowObjCWritebackConversion); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1266 | /// PerformImplicitConversion - Perform an implicit conversion of the |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1267 | /// expression From to the type ToType. Returns the |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1268 | /// converted expression. Flavor is the kind of conversion we're |
| 1269 | /// performing, used in the error message. If @p AllowExplicit, |
| 1270 | /// explicit user-defined conversions are permitted. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1271 | ExprResult |
| 1272 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1273 | AssignmentAction Action, bool AllowExplicit) { |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1274 | ImplicitConversionSequence ICS; |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1275 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1278 | ExprResult |
| 1279 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1280 | AssignmentAction Action, bool AllowExplicit, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1281 | ImplicitConversionSequence& ICS) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1282 | if (checkPlaceholderForOverload(*this, From)) |
| 1283 | return ExprError(); |
| 1284 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1285 | // Objective-C ARC: Determine whether we will allow the writeback conversion. |
| 1286 | bool AllowObjCWritebackConversion |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1287 | = getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1288 | (Action == AA_Passing || Action == AA_Sending); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1289 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1290 | ICS = clang::TryImplicitConversion(*this, From, ToType, |
| 1291 | /*SuppressUserConversions=*/false, |
| 1292 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1293 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1294 | /*CStyle=*/false, |
| 1295 | AllowObjCWritebackConversion); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1296 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 1297 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1298 | |
| 1299 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1300 | /// conversion that strips "noreturn" off the nested function type. |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1301 | bool Sema::IsNoReturnConversion(QualType FromType, QualType ToType, |
| 1302 | QualType &ResultTy) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1303 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1304 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1305 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1306 | // Permit the conversion F(t __attribute__((noreturn))) -> F(t) |
| 1307 | // where F adds one of the following at most once: |
| 1308 | // - a pointer |
| 1309 | // - a member pointer |
| 1310 | // - a block pointer |
| 1311 | CanQualType CanTo = Context.getCanonicalType(ToType); |
| 1312 | CanQualType CanFrom = Context.getCanonicalType(FromType); |
| 1313 | Type::TypeClass TyClass = CanTo->getTypeClass(); |
| 1314 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1315 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) { |
| 1316 | if (TyClass == Type::Pointer) { |
| 1317 | CanTo = CanTo.getAs<PointerType>()->getPointeeType(); |
| 1318 | CanFrom = CanFrom.getAs<PointerType>()->getPointeeType(); |
| 1319 | } else if (TyClass == Type::BlockPointer) { |
| 1320 | CanTo = CanTo.getAs<BlockPointerType>()->getPointeeType(); |
| 1321 | CanFrom = CanFrom.getAs<BlockPointerType>()->getPointeeType(); |
| 1322 | } else if (TyClass == Type::MemberPointer) { |
| 1323 | CanTo = CanTo.getAs<MemberPointerType>()->getPointeeType(); |
| 1324 | CanFrom = CanFrom.getAs<MemberPointerType>()->getPointeeType(); |
| 1325 | } else { |
| 1326 | return false; |
| 1327 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1328 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1329 | TyClass = CanTo->getTypeClass(); |
| 1330 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1331 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) |
| 1332 | return false; |
| 1333 | } |
| 1334 | |
| 1335 | const FunctionType *FromFn = cast<FunctionType>(CanFrom); |
| 1336 | FunctionType::ExtInfo EInfo = FromFn->getExtInfo(); |
| 1337 | if (!EInfo.getNoReturn()) return false; |
| 1338 | |
| 1339 | FromFn = Context.adjustFunctionType(FromFn, EInfo.withNoReturn(false)); |
| 1340 | assert(QualType(FromFn, 0).isCanonical()); |
| 1341 | if (QualType(FromFn, 0) != CanTo) return false; |
| 1342 | |
| 1343 | ResultTy = ToType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1344 | return true; |
| 1345 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1346 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1347 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 1348 | /// vector conversion. |
| 1349 | /// |
| 1350 | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
| 1351 | /// conversion. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1352 | static bool IsVectorConversion(ASTContext &Context, QualType FromType, |
| 1353 | QualType ToType, ImplicitConversionKind &ICK) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1354 | // We need at least one of these types to be a vector type to have a vector |
| 1355 | // conversion. |
| 1356 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
| 1357 | return false; |
| 1358 | |
| 1359 | // Identical types require no conversions. |
| 1360 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1361 | return false; |
| 1362 | |
| 1363 | // There are no conversions between extended vector types, only identity. |
| 1364 | if (ToType->isExtVectorType()) { |
| 1365 | // There are no conversions between extended vector types other than the |
| 1366 | // identity conversion. |
| 1367 | if (FromType->isExtVectorType()) |
| 1368 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1369 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1370 | // Vector splat from any arithmetic type to a vector. |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 1371 | if (FromType->isArithmeticType()) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1372 | ICK = ICK_Vector_Splat; |
| 1373 | return true; |
| 1374 | } |
| 1375 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1376 | |
| 1377 | // We can perform the conversion between vector types in the following cases: |
| 1378 | // 1)vector types are equivalent AltiVec and GCC vector types |
| 1379 | // 2)lax vector conversions are permitted and the vector types are of the |
| 1380 | // same size |
| 1381 | if (ToType->isVectorType() && FromType->isVectorType()) { |
| 1382 | if (Context.areCompatibleVectorTypes(FromType, ToType) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1383 | (Context.getLangOpts().LaxVectorConversions && |
Chandler Carruth | c45eb9c | 2010-08-08 05:02:51 +0000 | [diff] [blame] | 1384 | (Context.getTypeSize(FromType) == Context.getTypeSize(ToType)))) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1385 | ICK = ICK_Vector_Conversion; |
| 1386 | return true; |
| 1387 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1388 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1389 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1390 | return false; |
| 1391 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1392 | |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1393 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 1394 | bool InOverloadResolution, |
| 1395 | StandardConversionSequence &SCS, |
| 1396 | bool CStyle); |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1397 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1398 | /// IsStandardConversion - Determines whether there is a standard |
| 1399 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 1400 | /// expression From to the type ToType. Standard conversion sequences |
| 1401 | /// only consider non-class types; for conversions that involve class |
| 1402 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 1403 | /// contain the standard conversion sequence required to perform this |
| 1404 | /// conversion and this routine will return true. Otherwise, this |
| 1405 | /// routine will return false and the value of SCS is unspecified. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1406 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 1407 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1408 | StandardConversionSequence &SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1409 | bool CStyle, |
| 1410 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1411 | QualType FromType = From->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1412 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1413 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1414 | SCS.setAsIdentityConversion(); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1415 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1416 | SCS.IncompatibleObjC = false; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1417 | SCS.setFromType(FromType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1418 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1419 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1420 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1421 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1422 | if (FromType->isRecordType() || ToType->isRecordType()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1423 | if (S.getLangOpts().CPlusPlus) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1424 | return false; |
| 1425 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1426 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1429 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 1430 | // array-to-pointer conversion, or function-to-pointer conversion |
| 1431 | // (C++ 4p1). |
| 1432 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1433 | if (FromType == S.Context.OverloadTy) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1434 | DeclAccessPair AccessPair; |
| 1435 | if (FunctionDecl *Fn |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1436 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1437 | AccessPair)) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1438 | // We were able to resolve the address of the overloaded function, |
| 1439 | // so we can convert to the type of that function. |
| 1440 | FromType = Fn->getType(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1441 | |
| 1442 | // we can sometimes resolve &foo<int> regardless of ToType, so check |
| 1443 | // if the type matches (identity) or we are converting to bool |
| 1444 | if (!S.Context.hasSameUnqualifiedType( |
| 1445 | S.ExtractUnqualifiedFunctionType(ToType), FromType)) { |
| 1446 | QualType resultTy; |
| 1447 | // if the function type matches except for [[noreturn]], it's ok |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1448 | if (!S.IsNoReturnConversion(FromType, |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1449 | S.ExtractUnqualifiedFunctionType(ToType), resultTy)) |
| 1450 | // otherwise, only a boolean conversion is standard |
| 1451 | if (!ToType->isBooleanType()) |
| 1452 | return false; |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1453 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1454 | |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1455 | // Check if the "from" expression is taking the address of an overloaded |
| 1456 | // function and recompute the FromType accordingly. Take advantage of the |
| 1457 | // fact that non-static member functions *must* have such an address-of |
| 1458 | // expression. |
| 1459 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn); |
| 1460 | if (Method && !Method->isStatic()) { |
| 1461 | assert(isa<UnaryOperator>(From->IgnoreParens()) && |
| 1462 | "Non-unary operator on non-static member address"); |
| 1463 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() |
| 1464 | == UO_AddrOf && |
| 1465 | "Non-address-of operator on non-static member address"); |
| 1466 | const Type *ClassType |
| 1467 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 1468 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
Chandler Carruth | fc5c8fc | 2011-03-29 18:38:10 +0000 | [diff] [blame] | 1469 | } else if (isa<UnaryOperator>(From->IgnoreParens())) { |
| 1470 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() == |
| 1471 | UO_AddrOf && |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1472 | "Non-address-of operator for overloaded function expression"); |
| 1473 | FromType = S.Context.getPointerType(FromType); |
| 1474 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1475 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1476 | // Check that we've computed the proper type after overload resolution. |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1477 | assert(S.Context.hasSameType( |
| 1478 | FromType, |
| 1479 | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1480 | } else { |
| 1481 | return false; |
| 1482 | } |
Anders Carlsson | 2bd6250 | 2010-11-04 05:28:09 +0000 | [diff] [blame] | 1483 | } |
John McCall | 2148011 | 2011-08-30 00:57:29 +0000 | [diff] [blame] | 1484 | // Lvalue-to-rvalue conversion (C++11 4.1): |
| 1485 | // A glvalue (3.10) of a non-function, non-array type T can |
| 1486 | // be converted to a prvalue. |
| 1487 | bool argIsLValue = From->isGLValue(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1488 | if (argIsLValue && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1489 | !FromType->isFunctionType() && !FromType->isArrayType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1490 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1491 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1492 | |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1493 | // C11 6.3.2.1p2: |
| 1494 | // ... if the lvalue has atomic type, the value has the non-atomic version |
| 1495 | // of the type of the lvalue ... |
| 1496 | if (const AtomicType *Atomic = FromType->getAs<AtomicType>()) |
| 1497 | FromType = Atomic->getValueType(); |
| 1498 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1499 | // If T is a non-class type, the type of the rvalue is the |
| 1500 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1501 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 1502 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1503 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1504 | } else if (FromType->isArrayType()) { |
| 1505 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1506 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1507 | |
| 1508 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 1509 | // bound of T" can be converted to an rvalue of type "pointer to |
| 1510 | // T" (C++ 4.2p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1511 | FromType = S.Context.getArrayDecayedType(FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1512 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1513 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1514 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1515 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1516 | |
| 1517 | // For the purpose of ranking in overload resolution |
| 1518 | // (13.3.3.1.1), this conversion is considered an |
| 1519 | // array-to-pointer conversion followed by a qualification |
| 1520 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1521 | SCS.Second = ICK_Identity; |
| 1522 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1523 | SCS.QualificationIncludesObjCLifetime = false; |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1524 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1525 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1526 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1527 | } else if (FromType->isFunctionType() && argIsLValue) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1528 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1529 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1530 | |
| 1531 | // An lvalue of function type T can be converted to an rvalue of |
| 1532 | // type "pointer to T." The result is a pointer to the |
| 1533 | // function. (C++ 4.3p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1534 | FromType = S.Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1535 | } else { |
| 1536 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1537 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1538 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1539 | SCS.setToType(0, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1540 | |
| 1541 | // The second conversion can be an integral promotion, floating |
| 1542 | // point promotion, integral conversion, floating point conversion, |
| 1543 | // floating-integral conversion, pointer conversion, |
| 1544 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1545 | // For overloading in C, this can also be a "compatible-type" |
| 1546 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1547 | bool IncompatibleObjC = false; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1548 | ImplicitConversionKind SecondICK = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1549 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1550 | // The unqualified versions of the types are the same: there's no |
| 1551 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1552 | SCS.Second = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1553 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1554 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1555 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1556 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1557 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1558 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1559 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1560 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1561 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1562 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1563 | SCS.Second = ICK_Complex_Promotion; |
| 1564 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1565 | } else if (ToType->isBooleanType() && |
| 1566 | (FromType->isArithmeticType() || |
| 1567 | FromType->isAnyPointerType() || |
| 1568 | FromType->isBlockPointerType() || |
| 1569 | FromType->isMemberPointerType() || |
| 1570 | FromType->isNullPtrType())) { |
| 1571 | // Boolean conversions (C++ 4.12). |
| 1572 | SCS.Second = ICK_Boolean_Conversion; |
| 1573 | FromType = S.Context.BoolTy; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1574 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1575 | ToType->isIntegralType(S.Context)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1576 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1577 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1578 | FromType = ToType.getUnqualifiedType(); |
Richard Smith | 42860f1 | 2013-05-10 20:29:50 +0000 | [diff] [blame] | 1579 | } else if (FromType->isAnyComplexType() && ToType->isAnyComplexType()) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1580 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1581 | SCS.Second = ICK_Complex_Conversion; |
| 1582 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1583 | } else if ((FromType->isAnyComplexType() && ToType->isArithmeticType()) || |
| 1584 | (ToType->isAnyComplexType() && FromType->isArithmeticType())) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1585 | // Complex-real conversions (C99 6.3.1.7) |
| 1586 | SCS.Second = ICK_Complex_Real; |
| 1587 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1588 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1589 | // Floating point conversions (C++ 4.8). |
| 1590 | SCS.Second = ICK_Floating_Conversion; |
| 1591 | FromType = ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1592 | } else if ((FromType->isRealFloatingType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1593 | ToType->isIntegralType(S.Context)) || |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1594 | (FromType->isIntegralOrUnscopedEnumerationType() && |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1595 | ToType->isRealFloatingType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1596 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1597 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1598 | FromType = ToType.getUnqualifiedType(); |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1599 | } else if (S.IsBlockPointerConversion(FromType, ToType, FromType)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1600 | SCS.Second = ICK_Block_Pointer_Conversion; |
| 1601 | } else if (AllowObjCWritebackConversion && |
| 1602 | S.isObjCWritebackConversion(FromType, ToType, FromType)) { |
| 1603 | SCS.Second = ICK_Writeback_Conversion; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1604 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1605 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1606 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1607 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1608 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1609 | FromType = FromType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1610 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1611 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1612 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1613 | SCS.Second = ICK_Pointer_Member; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1614 | } else if (IsVectorConversion(S.Context, FromType, ToType, SecondICK)) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1615 | SCS.Second = SecondICK; |
| 1616 | FromType = ToType.getUnqualifiedType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1617 | } else if (!S.getLangOpts().CPlusPlus && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1618 | S.Context.typesAreCompatible(ToType, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1619 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1620 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1621 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1622 | } else if (S.IsNoReturnConversion(FromType, ToType, FromType)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1623 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1624 | SCS.Second = ICK_NoReturn_Adjustment; |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1625 | } else if (IsTransparentUnionStandardConversion(S, From, ToType, |
| 1626 | InOverloadResolution, |
| 1627 | SCS, CStyle)) { |
| 1628 | SCS.Second = ICK_TransparentUnionConversion; |
| 1629 | FromType = ToType; |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1630 | } else if (tryAtomicConversion(S, From, ToType, InOverloadResolution, SCS, |
| 1631 | CStyle)) { |
| 1632 | // tryAtomicConversion has updated the standard conversion sequence |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1633 | // appropriately. |
| 1634 | return true; |
Guy Benyei | 6959acd | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 1635 | } else if (ToType->isEventT() && |
| 1636 | From->isIntegerConstantExpr(S.getASTContext()) && |
| 1637 | (From->EvaluateKnownConstInt(S.getASTContext()) == 0)) { |
| 1638 | SCS.Second = ICK_Zero_Event_Conversion; |
| 1639 | FromType = ToType; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1640 | } else { |
| 1641 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1642 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1643 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1644 | SCS.setToType(1, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1645 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1646 | QualType CanonFrom; |
| 1647 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1648 | // The third conversion can be a qualification conversion (C++ 4p1). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1649 | bool ObjCLifetimeConversion; |
| 1650 | if (S.IsQualificationConversion(FromType, ToType, CStyle, |
| 1651 | ObjCLifetimeConversion)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1652 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1653 | SCS.QualificationIncludesObjCLifetime = ObjCLifetimeConversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1654 | FromType = ToType; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1655 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1656 | CanonTo = S.Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1657 | } else { |
| 1658 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1659 | SCS.Third = ICK_Identity; |
| 1660 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1661 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1662 | // [...] Any difference in top-level cv-qualification is |
| 1663 | // subsumed by the initialization itself and does not constitute |
| 1664 | // a conversion. [...] |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1665 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1666 | CanonTo = S.Context.getCanonicalType(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1667 | if (CanonFrom.getLocalUnqualifiedType() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1668 | == CanonTo.getLocalUnqualifiedType() && |
Matt Arsenault | 5509f37 | 2013-02-26 21:15:54 +0000 | [diff] [blame] | 1669 | CanonFrom.getLocalQualifiers() != CanonTo.getLocalQualifiers()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1670 | FromType = ToType; |
| 1671 | CanonFrom = CanonTo; |
| 1672 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1673 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1674 | SCS.setToType(2, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1675 | |
| 1676 | // If we have not converted the argument type to the parameter type, |
| 1677 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1678 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1679 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1680 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1681 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1682 | } |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1683 | |
| 1684 | static bool |
| 1685 | IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 1686 | QualType &ToType, |
| 1687 | bool InOverloadResolution, |
| 1688 | StandardConversionSequence &SCS, |
| 1689 | bool CStyle) { |
| 1690 | |
| 1691 | const RecordType *UT = ToType->getAsUnionType(); |
| 1692 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 1693 | return false; |
| 1694 | // The field to initialize within the transparent union. |
| 1695 | RecordDecl *UD = UT->getDecl(); |
| 1696 | // It's compatible if the expression matches any of the fields. |
| 1697 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1698 | itend = UD->field_end(); |
| 1699 | it != itend; ++it) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1700 | if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS, |
| 1701 | CStyle, /*ObjCWritebackConversion=*/false)) { |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1702 | ToType = it->getType(); |
| 1703 | return true; |
| 1704 | } |
| 1705 | } |
| 1706 | return false; |
| 1707 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1708 | |
| 1709 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1710 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1711 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1712 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1713 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1714 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1715 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1716 | if (!To) { |
| 1717 | return false; |
| 1718 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1719 | |
| 1720 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1721 | // unsigned short int can be converted to an rvalue of type int if |
| 1722 | // int can represent all the values of the source type; otherwise, |
| 1723 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1724 | // int (C++ 4.5p1). |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1725 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1726 | !FromType->isEnumeralType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1727 | if (// We can promote any signed, promotable integer type to an int |
| 1728 | (FromType->isSignedIntegerType() || |
| 1729 | // We can promote any unsigned integer type whose size is |
| 1730 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1731 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1732 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1733 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1736 | return To->getKind() == BuiltinType::UInt; |
| 1737 | } |
| 1738 | |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1739 | // C++11 [conv.prom]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1740 | // A prvalue of an unscoped enumeration type whose underlying type is not |
| 1741 | // fixed (7.2) can be converted to an rvalue a prvalue of the first of the |
| 1742 | // following types that can represent all the values of the enumeration |
| 1743 | // (i.e., the values in the range bmin to bmax as described in 7.2): int, |
| 1744 | // unsigned int, long int, unsigned long int, long long int, or unsigned |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1745 | // 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] | 1746 | // values of the enumeration, an rvalue a prvalue of an unscoped enumeration |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1747 | // 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] | 1748 | // with lowest integer conversion rank (4.13) greater than the rank of long |
| 1749 | // 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] | 1750 | // there are two such extended types, the signed one is chosen. |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1751 | // C++11 [conv.prom]p4: |
| 1752 | // A prvalue of an unscoped enumeration type whose underlying type is fixed |
| 1753 | // can be converted to a prvalue of its underlying type. Moreover, if |
| 1754 | // integral promotion can be applied to its underlying type, a prvalue of an |
| 1755 | // unscoped enumeration type whose underlying type is fixed can also be |
| 1756 | // converted to a prvalue of the promoted underlying type. |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1757 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
| 1758 | // C++0x 7.2p9: Note that this implicit enum to int conversion is not |
| 1759 | // provided for a scoped enumeration. |
| 1760 | if (FromEnumType->getDecl()->isScoped()) |
| 1761 | return false; |
| 1762 | |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1763 | // We can perform an integral promotion to the underlying type of the enum, |
| 1764 | // even if that's not the promoted type. |
| 1765 | if (FromEnumType->getDecl()->isFixed()) { |
| 1766 | QualType Underlying = FromEnumType->getDecl()->getIntegerType(); |
| 1767 | return Context.hasSameUnqualifiedType(Underlying, ToType) || |
| 1768 | IsIntegralPromotion(From, Underlying, ToType); |
| 1769 | } |
| 1770 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1771 | // We have already pre-calculated the promotion type, so this is trivial. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1772 | if (ToType->isIntegerType() && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1773 | !RequireCompleteType(From->getLocStart(), FromType, 0)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1774 | return Context.hasSameUnqualifiedType(ToType, |
| 1775 | FromEnumType->getDecl()->getPromotionType()); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1776 | } |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1777 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1778 | // C++0x [conv.prom]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1779 | // A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted |
| 1780 | // to an rvalue a prvalue of the first of the following types that can |
| 1781 | // represent all the values of its underlying type: int, unsigned int, |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1782 | // 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] | 1783 | // 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] | 1784 | // underlying type, an rvalue a prvalue of type char16_t, char32_t, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1785 | // 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] | 1786 | // type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1787 | if (FromType->isAnyCharacterType() && !FromType->isCharType() && |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1788 | ToType->isIntegerType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1789 | // Determine whether the type we're converting from is signed or |
| 1790 | // unsigned. |
David Majnemer | 0ad9231 | 2011-07-22 21:09:04 +0000 | [diff] [blame] | 1791 | bool FromIsSigned = FromType->isSignedIntegerType(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1792 | uint64_t FromSize = Context.getTypeSize(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1793 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1794 | // The types we'll try to promote to, in the appropriate |
| 1795 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1796 | QualType PromoteTypes[6] = { |
| 1797 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1798 | Context.LongTy, Context.UnsignedLongTy , |
| 1799 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1800 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1801 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1802 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1803 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1804 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1805 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1806 | // We found the type that we can promote to. If this is the |
| 1807 | // type we wanted, we have a promotion. Otherwise, no |
| 1808 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1809 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1810 | } |
| 1811 | } |
| 1812 | } |
| 1813 | |
| 1814 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1815 | // rvalue of type int if int can represent all the values of the |
| 1816 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1817 | // unsigned int can represent all the values of the bit-field. If |
| 1818 | // the bit-field is larger yet, no integral promotion applies to |
| 1819 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1820 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1821 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1822 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1823 | using llvm::APSInt; |
| 1824 | if (From) |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 1825 | if (FieldDecl *MemberDecl = From->getSourceBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1826 | APSInt BitWidth; |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1827 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1828 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1829 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1830 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1831 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1832 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1833 | if (BitWidth < ToSize || |
| 1834 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1835 | return To->getKind() == BuiltinType::Int; |
| 1836 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1837 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1838 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1839 | // that fits into an unsigned int? |
| 1840 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1841 | return To->getKind() == BuiltinType::UInt; |
| 1842 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1843 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1844 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1845 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1846 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1847 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1848 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1849 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1850 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1851 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1852 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1853 | |
| 1854 | return false; |
| 1855 | } |
| 1856 | |
| 1857 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1858 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1859 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1860 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1861 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1862 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1863 | /// An rvalue of type float can be converted to an rvalue of type |
| 1864 | /// double. (C++ 4.6p1). |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1865 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1866 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1867 | return true; |
| 1868 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1869 | // C99 6.3.1.5p1: |
| 1870 | // When a float is promoted to double or long double, or a |
| 1871 | // double is promoted to long double [...]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1872 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1873 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1874 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1875 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1876 | return true; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1877 | |
| 1878 | // Half can be promoted to float. |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 1879 | if (!getLangOpts().NativeHalfType && |
| 1880 | FromBuiltin->getKind() == BuiltinType::Half && |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1881 | ToBuiltin->getKind() == BuiltinType::Float) |
| 1882 | return true; |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1885 | return false; |
| 1886 | } |
| 1887 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1888 | /// \brief Determine if a conversion is a complex promotion. |
| 1889 | /// |
| 1890 | /// A complex promotion is defined as a complex -> complex conversion |
| 1891 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1892 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1893 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1894 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1895 | if (!FromComplex) |
| 1896 | return false; |
| 1897 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1898 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1899 | if (!ToComplex) |
| 1900 | return false; |
| 1901 | |
| 1902 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1903 | ToComplex->getElementType()) || |
| 1904 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1905 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1908 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1909 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1910 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1911 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1912 | /// the right set of qualifiers on its pointee. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1913 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1914 | static QualType |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1915 | BuildSimilarlyQualifiedPointerType(const Type *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1916 | QualType ToPointee, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1917 | ASTContext &Context, |
| 1918 | bool StripObjCLifetime = false) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1919 | assert((FromPtr->getTypeClass() == Type::Pointer || |
| 1920 | FromPtr->getTypeClass() == Type::ObjCObjectPointer) && |
| 1921 | "Invalid similarly-qualified pointer type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1922 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1923 | /// Conversions to 'id' subsume cv-qualifier conversions. |
| 1924 | if (ToType->isObjCIdType() || ToType->isObjCQualifiedIdType()) |
Douglas Gregor | 143c7ac | 2010-12-06 22:09:19 +0000 | [diff] [blame] | 1925 | return ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1926 | |
| 1927 | QualType CanonFromPointee |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1928 | = Context.getCanonicalType(FromPtr->getPointeeType()); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1929 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1930 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1931 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1932 | if (StripObjCLifetime) |
| 1933 | Quals.removeObjCLifetime(); |
| 1934 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1935 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1936 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1937 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1938 | if (!ToType.isNull()) |
Douglas Gregor | af7bea5 | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1939 | return ToType.getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1940 | |
| 1941 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1942 | // already. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1943 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1944 | return Context.getObjCObjectPointerType(ToPointee); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1945 | return Context.getPointerType(ToPointee); |
| 1946 | } |
| 1947 | |
| 1948 | // Just build a canonical type that has the right qualifiers. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1949 | QualType QualifiedCanonToPointee |
| 1950 | = Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1951 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1952 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1953 | return Context.getObjCObjectPointerType(QualifiedCanonToPointee); |
| 1954 | return Context.getPointerType(QualifiedCanonToPointee); |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1955 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1956 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1957 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1958 | bool InOverloadResolution, |
| 1959 | ASTContext &Context) { |
| 1960 | // Handle value-dependent integral null pointer constants correctly. |
| 1961 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1962 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1963 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1964 | return !InOverloadResolution; |
| 1965 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1966 | return Expr->isNullPointerConstant(Context, |
| 1967 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1968 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1969 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1970 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1971 | /// IsPointerConversion - Determines whether the conversion of the |
| 1972 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1973 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1974 | /// 4.10). If so, returns true and places the converted type (that |
| 1975 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1976 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1977 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1978 | /// This routine also supports conversions to and from block pointers |
| 1979 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1980 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1981 | /// appropriate overloading rules for Objective-C, we may want to |
| 1982 | /// split the Objective-C checks into a different routine; however, |
| 1983 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1984 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1985 | /// set if the conversion is an allowed Objective-C conversion that |
| 1986 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1987 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1988 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1989 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1990 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1991 | IncompatibleObjC = false; |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1992 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, |
| 1993 | IncompatibleObjC)) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1994 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1995 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1996 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1997 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1998 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1999 | ConvertedType = ToType; |
| 2000 | return true; |
| 2001 | } |
| 2002 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2003 | // Blocks: Block pointers can be converted to void*. |
| 2004 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2005 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2006 | ConvertedType = ToType; |
| 2007 | return true; |
| 2008 | } |
| 2009 | // Blocks: A null pointer constant can be converted to a block |
| 2010 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2011 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2012 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2013 | ConvertedType = ToType; |
| 2014 | return true; |
| 2015 | } |
| 2016 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2017 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 2018 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2019 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2020 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2021 | ConvertedType = ToType; |
| 2022 | return true; |
| 2023 | } |
| 2024 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2025 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2026 | if (!ToTypePtr) |
| 2027 | return false; |
| 2028 | |
| 2029 | // 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] | 2030 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2031 | ConvertedType = ToType; |
| 2032 | return true; |
| 2033 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2034 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2035 | // Beyond this point, both types need to be pointers |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2036 | // , including objective-c pointers. |
| 2037 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2038 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2039 | !getLangOpts().ObjCAutoRefCount) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2040 | ConvertedType = BuildSimilarlyQualifiedPointerType( |
| 2041 | FromType->getAs<ObjCObjectPointerType>(), |
| 2042 | ToPointeeType, |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2043 | ToType, Context); |
| 2044 | return true; |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2045 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2046 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2047 | if (!FromTypePtr) |
| 2048 | return false; |
| 2049 | |
| 2050 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2051 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2052 | // 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] | 2053 | // pointer conversion, so don't do all of the work below. |
| 2054 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
| 2055 | return false; |
| 2056 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2057 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 2058 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 2059 | // 4.10p2). |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 2060 | if (FromPointeeType->isIncompleteOrObjectType() && |
| 2061 | ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2062 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2063 | ToPointeeType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2064 | ToType, Context, |
| 2065 | /*StripObjCLifetime=*/true); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2066 | return true; |
| 2067 | } |
| 2068 | |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2069 | // MSVC allows implicit function to void* type conversion. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2070 | if (getLangOpts().MicrosoftExt && FromPointeeType->isFunctionType() && |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2071 | ToPointeeType->isVoidType()) { |
| 2072 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2073 | ToPointeeType, |
| 2074 | ToType, Context); |
| 2075 | return true; |
| 2076 | } |
| 2077 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2078 | // When we're overloading in C, we allow a special kind of pointer |
| 2079 | // conversion for compatible-but-not-identical pointee types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2080 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2081 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2082 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2083 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2084 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2085 | return true; |
| 2086 | } |
| 2087 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2088 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2089 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2090 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 2091 | // can be converted to an rvalue of type "pointer to cv B," where |
| 2092 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 2093 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 2094 | // necessitates this conversion is ill-formed. The result of the |
| 2095 | // conversion is a pointer to the base class sub-object of the |
| 2096 | // derived class object. The null pointer value is converted to |
| 2097 | // the null pointer value of the destination type. |
| 2098 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2099 | // Note that we do not check for ambiguity or inaccessibility |
| 2100 | // here. That is handled by CheckPointerConversion. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2101 | if (getLangOpts().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2102 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | bf1764c | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 2103 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2104 | !RequireCompleteType(From->getLocStart(), FromPointeeType, 0) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2105 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2106 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2107 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2108 | ToType, Context); |
| 2109 | return true; |
| 2110 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2111 | |
Fariborz Jahanian | 5da3c08 | 2011-04-14 20:33:36 +0000 | [diff] [blame] | 2112 | if (FromPointeeType->isVectorType() && ToPointeeType->isVectorType() && |
| 2113 | Context.areCompatibleVectorTypes(FromPointeeType, ToPointeeType)) { |
| 2114 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2115 | ToPointeeType, |
| 2116 | ToType, Context); |
| 2117 | return true; |
| 2118 | } |
| 2119 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2120 | return false; |
| 2121 | } |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2122 | |
| 2123 | /// \brief Adopt the given qualifiers for the given type. |
| 2124 | static QualType AdoptQualifiers(ASTContext &Context, QualType T, Qualifiers Qs){ |
| 2125 | Qualifiers TQs = T.getQualifiers(); |
| 2126 | |
| 2127 | // Check whether qualifiers already match. |
| 2128 | if (TQs == Qs) |
| 2129 | return T; |
| 2130 | |
| 2131 | if (Qs.compatiblyIncludes(TQs)) |
| 2132 | return Context.getQualifiedType(T, Qs); |
| 2133 | |
| 2134 | return Context.getQualifiedType(T.getUnqualifiedType(), Qs); |
| 2135 | } |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2136 | |
| 2137 | /// isObjCPointerConversion - Determines whether this is an |
| 2138 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 2139 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2140 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2141 | QualType& ConvertedType, |
| 2142 | bool &IncompatibleObjC) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2143 | if (!getLangOpts().ObjC1) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2144 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2145 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2146 | // The set of qualifiers on the type we're converting from. |
| 2147 | Qualifiers FromQualifiers = FromType.getQualifiers(); |
| 2148 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2149 | // First, we handle all conversions on ObjC object pointer types. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2150 | const ObjCObjectPointerType* ToObjCPtr = |
| 2151 | ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2152 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2153 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2154 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2155 | if (ToObjCPtr && FromObjCPtr) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2156 | // If the pointee types are the same (ignoring qualifications), |
| 2157 | // then this is not a pointer conversion. |
| 2158 | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
| 2159 | FromObjCPtr->getPointeeType())) |
| 2160 | return false; |
| 2161 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2162 | // Check for compatible |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2163 | // 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] | 2164 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2165 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2166 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2167 | return true; |
| 2168 | } |
| 2169 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2170 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2171 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2172 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 2173 | /*compare=*/false)) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2174 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2175 | return true; |
| 2176 | } |
| 2177 | // Objective C++: We're able to convert from a pointer to an |
| 2178 | // interface to a pointer to a different interface. |
| 2179 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2180 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 2181 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2182 | if (getLangOpts().CPlusPlus && LHS && RHS && |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2183 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 2184 | FromObjCPtr->getPointeeType())) |
| 2185 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2186 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2187 | ToObjCPtr->getPointeeType(), |
| 2188 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2189 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2190 | return true; |
| 2191 | } |
| 2192 | |
| 2193 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 2194 | // Okay: this is some kind of implicit downcast of Objective-C |
| 2195 | // interfaces, which is permitted. However, we're going to |
| 2196 | // complain about it. |
| 2197 | IncompatibleObjC = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2198 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2199 | ToObjCPtr->getPointeeType(), |
| 2200 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2201 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2202 | return true; |
| 2203 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2204 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2205 | // 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] | 2206 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2207 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2208 | ToPointeeType = ToCPtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2209 | else if (const BlockPointerType *ToBlockPtr = |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2210 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 2211 | // 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] | 2212 | // to a block pointer type. |
| 2213 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2214 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2215 | return true; |
| 2216 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2217 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2218 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2219 | else if (FromType->getAs<BlockPointerType>() && |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2220 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2221 | // 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] | 2222 | // pointer to any object. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2223 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2224 | return true; |
| 2225 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2226 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2227 | return false; |
| 2228 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2229 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2230 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2231 | FromPointeeType = FromCPtr->getPointeeType(); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2232 | else if (const BlockPointerType *FromBlockPtr = |
| 2233 | FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2234 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2235 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2236 | return false; |
| 2237 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2238 | // If we have pointers to pointers, recursively check whether this |
| 2239 | // is an Objective-C conversion. |
| 2240 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 2241 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2242 | IncompatibleObjC)) { |
| 2243 | // We always complain about this conversion. |
| 2244 | IncompatibleObjC = true; |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2245 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2246 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2247 | return true; |
| 2248 | } |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2249 | // Allow conversion of pointee being objective-c pointer to another one; |
| 2250 | // as in I* to id. |
| 2251 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 2252 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 2253 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2254 | IncompatibleObjC)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2255 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2256 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2257 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2258 | return true; |
| 2259 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2260 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2261 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2262 | // differences in the argument and result types are in Objective-C |
| 2263 | // pointer conversions. If so, we permit the conversion (but |
| 2264 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2265 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2266 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2267 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2268 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2269 | if (FromFunctionType && ToFunctionType) { |
| 2270 | // If the function types are exactly the same, this isn't an |
| 2271 | // Objective-C pointer conversion. |
| 2272 | if (Context.getCanonicalType(FromPointeeType) |
| 2273 | == Context.getCanonicalType(ToPointeeType)) |
| 2274 | return false; |
| 2275 | |
| 2276 | // Perform the quick checks that will tell us whether these |
| 2277 | // function types are obviously different. |
| 2278 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2279 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 2280 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 2281 | return false; |
| 2282 | |
| 2283 | bool HasObjCConversion = false; |
| 2284 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 2285 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 2286 | // Okay, the types match exactly. Nothing to do. |
| 2287 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 2288 | ToFunctionType->getResultType(), |
| 2289 | ConvertedType, IncompatibleObjC)) { |
| 2290 | // Okay, we have an Objective-C pointer conversion. |
| 2291 | HasObjCConversion = true; |
| 2292 | } else { |
| 2293 | // Function types are too different. Abort. |
| 2294 | return false; |
| 2295 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2296 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2297 | // Check argument types. |
| 2298 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2299 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2300 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2301 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2302 | if (Context.getCanonicalType(FromArgType) |
| 2303 | == Context.getCanonicalType(ToArgType)) { |
| 2304 | // Okay, the types match exactly. Nothing to do. |
| 2305 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 2306 | ConvertedType, IncompatibleObjC)) { |
| 2307 | // Okay, we have an Objective-C pointer conversion. |
| 2308 | HasObjCConversion = true; |
| 2309 | } else { |
| 2310 | // Argument types are too different. Abort. |
| 2311 | return false; |
| 2312 | } |
| 2313 | } |
| 2314 | |
| 2315 | if (HasObjCConversion) { |
| 2316 | // We had an Objective-C conversion. Allow this pointer |
| 2317 | // conversion, but complain about it. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2318 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2319 | IncompatibleObjC = true; |
| 2320 | return true; |
| 2321 | } |
| 2322 | } |
| 2323 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2324 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2325 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2326 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2327 | /// \brief Determine whether this is an Objective-C writeback conversion, |
| 2328 | /// used for parameter passing when performing automatic reference counting. |
| 2329 | /// |
| 2330 | /// \param FromType The type we're converting form. |
| 2331 | /// |
| 2332 | /// \param ToType The type we're converting to. |
| 2333 | /// |
| 2334 | /// \param ConvertedType The type that will be produced after applying |
| 2335 | /// this conversion. |
| 2336 | bool Sema::isObjCWritebackConversion(QualType FromType, QualType ToType, |
| 2337 | QualType &ConvertedType) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2338 | if (!getLangOpts().ObjCAutoRefCount || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2339 | Context.hasSameUnqualifiedType(FromType, ToType)) |
| 2340 | return false; |
| 2341 | |
| 2342 | // Parameter must be a pointer to __autoreleasing (with no other qualifiers). |
| 2343 | QualType ToPointee; |
| 2344 | if (const PointerType *ToPointer = ToType->getAs<PointerType>()) |
| 2345 | ToPointee = ToPointer->getPointeeType(); |
| 2346 | else |
| 2347 | return false; |
| 2348 | |
| 2349 | Qualifiers ToQuals = ToPointee.getQualifiers(); |
| 2350 | if (!ToPointee->isObjCLifetimeType() || |
| 2351 | ToQuals.getObjCLifetime() != Qualifiers::OCL_Autoreleasing || |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2352 | !ToQuals.withoutObjCLifetime().empty()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2353 | return false; |
| 2354 | |
| 2355 | // Argument must be a pointer to __strong to __weak. |
| 2356 | QualType FromPointee; |
| 2357 | if (const PointerType *FromPointer = FromType->getAs<PointerType>()) |
| 2358 | FromPointee = FromPointer->getPointeeType(); |
| 2359 | else |
| 2360 | return false; |
| 2361 | |
| 2362 | Qualifiers FromQuals = FromPointee.getQualifiers(); |
| 2363 | if (!FromPointee->isObjCLifetimeType() || |
| 2364 | (FromQuals.getObjCLifetime() != Qualifiers::OCL_Strong && |
| 2365 | FromQuals.getObjCLifetime() != Qualifiers::OCL_Weak)) |
| 2366 | return false; |
| 2367 | |
| 2368 | // Make sure that we have compatible qualifiers. |
| 2369 | FromQuals.setObjCLifetime(Qualifiers::OCL_Autoreleasing); |
| 2370 | if (!ToQuals.compatiblyIncludes(FromQuals)) |
| 2371 | return false; |
| 2372 | |
| 2373 | // Remove qualifiers from the pointee type we're converting from; they |
| 2374 | // aren't used in the compatibility check belong, and we'll be adding back |
| 2375 | // qualifiers (with __autoreleasing) if the compatibility check succeeds. |
| 2376 | FromPointee = FromPointee.getUnqualifiedType(); |
| 2377 | |
| 2378 | // The unqualified form of the pointee types must be compatible. |
| 2379 | ToPointee = ToPointee.getUnqualifiedType(); |
| 2380 | bool IncompatibleObjC; |
| 2381 | if (Context.typesAreCompatible(FromPointee, ToPointee)) |
| 2382 | FromPointee = ToPointee; |
| 2383 | else if (!isObjCPointerConversion(FromPointee, ToPointee, FromPointee, |
| 2384 | IncompatibleObjC)) |
| 2385 | return false; |
| 2386 | |
| 2387 | /// \brief Construct the type we're converting to, which is a pointer to |
| 2388 | /// __autoreleasing pointee. |
| 2389 | FromPointee = Context.getQualifiedType(FromPointee, FromQuals); |
| 2390 | ConvertedType = Context.getPointerType(FromPointee); |
| 2391 | return true; |
| 2392 | } |
| 2393 | |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2394 | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
| 2395 | QualType& ConvertedType) { |
| 2396 | QualType ToPointeeType; |
| 2397 | if (const BlockPointerType *ToBlockPtr = |
| 2398 | ToType->getAs<BlockPointerType>()) |
| 2399 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 2400 | else |
| 2401 | return false; |
| 2402 | |
| 2403 | QualType FromPointeeType; |
| 2404 | if (const BlockPointerType *FromBlockPtr = |
| 2405 | FromType->getAs<BlockPointerType>()) |
| 2406 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2407 | else |
| 2408 | return false; |
| 2409 | // We have pointer to blocks, check whether the only |
| 2410 | // differences in the argument and result types are in Objective-C |
| 2411 | // pointer conversions. If so, we permit the conversion. |
| 2412 | |
| 2413 | const FunctionProtoType *FromFunctionType |
| 2414 | = FromPointeeType->getAs<FunctionProtoType>(); |
| 2415 | const FunctionProtoType *ToFunctionType |
| 2416 | = ToPointeeType->getAs<FunctionProtoType>(); |
| 2417 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2418 | if (!FromFunctionType || !ToFunctionType) |
| 2419 | return false; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2420 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2421 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2422 | return true; |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2423 | |
| 2424 | // Perform the quick checks that will tell us whether these |
| 2425 | // function types are obviously different. |
| 2426 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2427 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
| 2428 | return false; |
| 2429 | |
| 2430 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
| 2431 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
| 2432 | if (FromEInfo != ToEInfo) |
| 2433 | return false; |
| 2434 | |
| 2435 | bool IncompatibleObjC = false; |
Fariborz Jahanian | 462dae5 | 2011-02-13 20:11:42 +0000 | [diff] [blame] | 2436 | if (Context.hasSameType(FromFunctionType->getResultType(), |
| 2437 | ToFunctionType->getResultType())) { |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2438 | // Okay, the types match exactly. Nothing to do. |
| 2439 | } else { |
| 2440 | QualType RHS = FromFunctionType->getResultType(); |
| 2441 | QualType LHS = ToFunctionType->getResultType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2442 | if ((!getLangOpts().CPlusPlus || !RHS->isRecordType()) && |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2443 | !RHS.hasQualifiers() && LHS.hasQualifiers()) |
| 2444 | LHS = LHS.getUnqualifiedType(); |
| 2445 | |
| 2446 | if (Context.hasSameType(RHS,LHS)) { |
| 2447 | // OK exact match. |
| 2448 | } else if (isObjCPointerConversion(RHS, LHS, |
| 2449 | ConvertedType, IncompatibleObjC)) { |
| 2450 | if (IncompatibleObjC) |
| 2451 | return false; |
| 2452 | // Okay, we have an Objective-C pointer conversion. |
| 2453 | } |
| 2454 | else |
| 2455 | return false; |
| 2456 | } |
| 2457 | |
| 2458 | // Check argument types. |
| 2459 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2460 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2461 | IncompatibleObjC = false; |
| 2462 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2463 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2464 | if (Context.hasSameType(FromArgType, ToArgType)) { |
| 2465 | // Okay, the types match exactly. Nothing to do. |
| 2466 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
| 2467 | ConvertedType, IncompatibleObjC)) { |
| 2468 | if (IncompatibleObjC) |
| 2469 | return false; |
| 2470 | // Okay, we have an Objective-C pointer conversion. |
| 2471 | } else |
| 2472 | // Argument types are too different. Abort. |
| 2473 | return false; |
| 2474 | } |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 2475 | if (LangOpts.ObjCAutoRefCount && |
| 2476 | !Context.FunctionTypesMatchOnNSConsumedAttrs(FromFunctionType, |
| 2477 | ToFunctionType)) |
| 2478 | return false; |
Fariborz Jahanian | f9d9527 | 2011-09-28 20:22:05 +0000 | [diff] [blame] | 2479 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2480 | ConvertedType = ToType; |
| 2481 | return true; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2484 | enum { |
| 2485 | ft_default, |
| 2486 | ft_different_class, |
| 2487 | ft_parameter_arity, |
| 2488 | ft_parameter_mismatch, |
| 2489 | ft_return_type, |
| 2490 | ft_qualifer_mismatch |
| 2491 | }; |
| 2492 | |
| 2493 | /// HandleFunctionTypeMismatch - Gives diagnostic information for differeing |
| 2494 | /// function types. Catches different number of parameter, mismatch in |
| 2495 | /// parameter types, and different return types. |
| 2496 | void Sema::HandleFunctionTypeMismatch(PartialDiagnostic &PDiag, |
| 2497 | QualType FromType, QualType ToType) { |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2498 | // If either type is not valid, include no extra info. |
| 2499 | if (FromType.isNull() || ToType.isNull()) { |
| 2500 | PDiag << ft_default; |
| 2501 | return; |
| 2502 | } |
| 2503 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2504 | // Get the function type from the pointers. |
| 2505 | if (FromType->isMemberPointerType() && ToType->isMemberPointerType()) { |
| 2506 | const MemberPointerType *FromMember = FromType->getAs<MemberPointerType>(), |
| 2507 | *ToMember = ToType->getAs<MemberPointerType>(); |
| 2508 | if (FromMember->getClass() != ToMember->getClass()) { |
| 2509 | PDiag << ft_different_class << QualType(ToMember->getClass(), 0) |
| 2510 | << QualType(FromMember->getClass(), 0); |
| 2511 | return; |
| 2512 | } |
| 2513 | FromType = FromMember->getPointeeType(); |
| 2514 | ToType = ToMember->getPointeeType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2517 | if (FromType->isPointerType()) |
| 2518 | FromType = FromType->getPointeeType(); |
| 2519 | if (ToType->isPointerType()) |
| 2520 | ToType = ToType->getPointeeType(); |
| 2521 | |
| 2522 | // Remove references. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2523 | FromType = FromType.getNonReferenceType(); |
| 2524 | ToType = ToType.getNonReferenceType(); |
| 2525 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2526 | // Don't print extra info for non-specialized template functions. |
| 2527 | if (FromType->isInstantiationDependentType() && |
| 2528 | !FromType->getAs<TemplateSpecializationType>()) { |
| 2529 | PDiag << ft_default; |
| 2530 | return; |
| 2531 | } |
| 2532 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2533 | // No extra info for same types. |
| 2534 | if (Context.hasSameType(FromType, ToType)) { |
| 2535 | PDiag << ft_default; |
| 2536 | return; |
| 2537 | } |
| 2538 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2539 | const FunctionProtoType *FromFunction = FromType->getAs<FunctionProtoType>(), |
| 2540 | *ToFunction = ToType->getAs<FunctionProtoType>(); |
| 2541 | |
| 2542 | // Both types need to be function types. |
| 2543 | if (!FromFunction || !ToFunction) { |
| 2544 | PDiag << ft_default; |
| 2545 | return; |
| 2546 | } |
| 2547 | |
| 2548 | if (FromFunction->getNumArgs() != ToFunction->getNumArgs()) { |
| 2549 | PDiag << ft_parameter_arity << ToFunction->getNumArgs() |
| 2550 | << FromFunction->getNumArgs(); |
| 2551 | return; |
| 2552 | } |
| 2553 | |
| 2554 | // Handle different parameter types. |
| 2555 | unsigned ArgPos; |
| 2556 | if (!FunctionArgTypesAreEqual(FromFunction, ToFunction, &ArgPos)) { |
| 2557 | PDiag << ft_parameter_mismatch << ArgPos + 1 |
| 2558 | << ToFunction->getArgType(ArgPos) |
| 2559 | << FromFunction->getArgType(ArgPos); |
| 2560 | return; |
| 2561 | } |
| 2562 | |
| 2563 | // Handle different return type. |
| 2564 | if (!Context.hasSameType(FromFunction->getResultType(), |
| 2565 | ToFunction->getResultType())) { |
| 2566 | PDiag << ft_return_type << ToFunction->getResultType() |
| 2567 | << FromFunction->getResultType(); |
| 2568 | return; |
| 2569 | } |
| 2570 | |
| 2571 | unsigned FromQuals = FromFunction->getTypeQuals(), |
| 2572 | ToQuals = ToFunction->getTypeQuals(); |
| 2573 | if (FromQuals != ToQuals) { |
| 2574 | PDiag << ft_qualifer_mismatch << ToQuals << FromQuals; |
| 2575 | return; |
| 2576 | } |
| 2577 | |
| 2578 | // Unable to find a difference, so add no extra info. |
| 2579 | PDiag << ft_default; |
| 2580 | } |
| 2581 | |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2582 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
Douglas Gregor | dec1cc4 | 2011-12-15 17:15:07 +0000 | [diff] [blame] | 2583 | /// for equality of their argument types. Caller has already checked that |
Eli Friedman | 0601700 | 2013-06-18 22:41:37 +0000 | [diff] [blame] | 2584 | /// they have same number of arguments. If the parameters are different, |
| 2585 | /// ArgPos will have the parameter index of the first different parameter. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2586 | bool Sema::FunctionArgTypesAreEqual(const FunctionProtoType *OldType, |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2587 | const FunctionProtoType *NewType, |
| 2588 | unsigned *ArgPos) { |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2589 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 2590 | N = NewType->arg_type_begin(), |
| 2591 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
Richard Trieu | 7ea491c | 2013-08-09 21:42:32 +0000 | [diff] [blame] | 2592 | if (!Context.hasSameType(O->getUnqualifiedType(), |
| 2593 | N->getUnqualifiedType())) { |
Larisse Voufo | 3151b7c | 2013-08-06 03:57:41 +0000 | [diff] [blame] | 2594 | if (ArgPos) *ArgPos = O - OldType->arg_type_begin(); |
| 2595 | return false; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2596 | } |
| 2597 | } |
| 2598 | return true; |
| 2599 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2600 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2601 | /// CheckPointerConversion - Check the pointer conversion from the |
| 2602 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2603 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2604 | /// conversions for which IsPointerConversion has already returned |
| 2605 | /// true. It returns true and produces a diagnostic if there was an |
| 2606 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2607 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2608 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2609 | CXXCastPath& BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2610 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2611 | QualType FromType = From->getType(); |
Argyrios Kyrtzidis | b335872 | 2010-09-28 14:54:11 +0000 | [diff] [blame] | 2612 | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2613 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2614 | Kind = CK_BitCast; |
| 2615 | |
David Blaikie | 50800fc | 2012-08-08 17:33:31 +0000 | [diff] [blame] | 2616 | if (!IsCStyleOrFunctionalCast && !FromType->isAnyPointerType() && |
| 2617 | From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull) == |
| 2618 | Expr::NPCK_ZeroExpression) { |
| 2619 | if (Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy)) |
| 2620 | DiagRuntimeBehavior(From->getExprLoc(), From, |
| 2621 | PDiag(diag::warn_impcast_bool_to_null_pointer) |
| 2622 | << ToType << From->getSourceRange()); |
| 2623 | else if (!isUnevaluatedContext()) |
| 2624 | Diag(From->getExprLoc(), diag::warn_non_literal_null_pointer) |
| 2625 | << ToType << From->getSourceRange(); |
| 2626 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2627 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
| 2628 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2629 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 2630 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 2631 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2632 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 2633 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2634 | // We must have a derived-to-base conversion. Check an |
| 2635 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2636 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 2637 | From->getExprLoc(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2638 | From->getSourceRange(), &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2639 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2640 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2641 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2642 | // The conversion was successful. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2643 | Kind = CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2644 | } |
| 2645 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2646 | } else if (const ObjCObjectPointerType *ToPtrType = |
| 2647 | ToType->getAs<ObjCObjectPointerType>()) { |
| 2648 | if (const ObjCObjectPointerType *FromPtrType = |
| 2649 | FromType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2650 | // Objective-C++ conversions are always okay. |
| 2651 | // FIXME: We should have a different class of conversions for the |
| 2652 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2653 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2654 | return false; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2655 | } else if (FromType->isBlockPointerType()) { |
| 2656 | Kind = CK_BlockPointerToObjCPointerCast; |
| 2657 | } else { |
| 2658 | Kind = CK_CPointerToObjCPointerCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2659 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2660 | } else if (ToType->isBlockPointerType()) { |
| 2661 | if (!FromType->isBlockPointerType()) |
| 2662 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2663 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2664 | |
| 2665 | // We shouldn't fall into this case unless it's valid for other |
| 2666 | // reasons. |
| 2667 | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 2668 | Kind = CK_NullToPointer; |
| 2669 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2670 | return false; |
| 2671 | } |
| 2672 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2673 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 2674 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 2675 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 2676 | /// If so, returns true and places the converted type (that might differ from |
| 2677 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 2678 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2679 | QualType ToType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2680 | bool InOverloadResolution, |
| 2681 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2682 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2683 | if (!ToTypePtr) |
| 2684 | return false; |
| 2685 | |
| 2686 | // 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] | 2687 | if (From->isNullPointerConstant(Context, |
| 2688 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 2689 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2690 | ConvertedType = ToType; |
| 2691 | return true; |
| 2692 | } |
| 2693 | |
| 2694 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2695 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2696 | if (!FromTypePtr) |
| 2697 | return false; |
| 2698 | |
| 2699 | // A pointer to member of B can be converted to a pointer to member of D, |
| 2700 | // where D is derived from B (C++ 4.11p2). |
| 2701 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 2702 | QualType ToClass(ToTypePtr->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2703 | |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2704 | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2705 | !RequireCompleteType(From->getLocStart(), ToClass, 0) && |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2706 | IsDerivedFrom(ToClass, FromClass)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2707 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 2708 | ToClass.getTypePtr()); |
| 2709 | return true; |
| 2710 | } |
| 2711 | |
| 2712 | return false; |
| 2713 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2714 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2715 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 2716 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2717 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2718 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 2719 | /// true and produces a diagnostic if there was an error, or returns false |
| 2720 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2721 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2722 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2723 | CXXCastPath &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2724 | bool IgnoreBaseAccess) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2725 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2726 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2727 | if (!FromPtrType) { |
| 2728 | // This must be a null pointer to member pointer conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2729 | assert(From->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2730 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2731 | "Expr must be null pointer constant!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2732 | Kind = CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2733 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2734 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2735 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2736 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2737 | assert(ToPtrType && "No member pointer cast has a target type " |
| 2738 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2739 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2740 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 2741 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2742 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2743 | // FIXME: What about dependent types? |
| 2744 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 2745 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2746 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2747 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 2748 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2749 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 2750 | assert(DerivationOkay && |
| 2751 | "Should not have been called if derivation isn't OK."); |
| 2752 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2753 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2754 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 2755 | getUnqualifiedType())) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2756 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 2757 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 2758 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 2759 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2760 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2761 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2762 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2763 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 2764 | << FromClass << ToClass << QualType(VBase, 0) |
| 2765 | << From->getSourceRange(); |
| 2766 | return true; |
| 2767 | } |
| 2768 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2769 | if (!IgnoreBaseAccess) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2770 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 2771 | Paths.front(), |
| 2772 | diag::err_downcast_from_inaccessible_base); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2773 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2774 | // Must be a base to derived member conversion. |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2775 | BuildBasePathArray(Paths, BasePath); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2776 | Kind = CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2777 | return false; |
| 2778 | } |
| 2779 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2780 | /// IsQualificationConversion - Determines whether the conversion from |
| 2781 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 2782 | /// (C++ 4.4). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2783 | /// |
| 2784 | /// \param ObjCLifetimeConversion Output parameter that will be set to indicate |
| 2785 | /// when the qualification conversion involves a change in the Objective-C |
| 2786 | /// object lifetime. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2787 | bool |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2788 | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2789 | bool CStyle, bool &ObjCLifetimeConversion) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2790 | FromType = Context.getCanonicalType(FromType); |
| 2791 | ToType = Context.getCanonicalType(ToType); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2792 | ObjCLifetimeConversion = false; |
| 2793 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2794 | // If FromType and ToType are the same type, this is not a |
| 2795 | // qualification conversion. |
Sebastian Redl | 22c9240 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 2796 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2797 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2798 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2799 | // (C++ 4.4p4): |
| 2800 | // A conversion can add cv-qualifiers at levels other than the first |
| 2801 | // in multi-level pointers, subject to the following rules: [...] |
| 2802 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2803 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2804 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2805 | // Within each iteration of the loop, we check the qualifiers to |
| 2806 | // determine if this still looks like a qualification |
| 2807 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2808 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2809 | // until there are no more pointers or pointers-to-members left to |
| 2810 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2811 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2812 | |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2813 | Qualifiers FromQuals = FromType.getQualifiers(); |
| 2814 | Qualifiers ToQuals = ToType.getQualifiers(); |
| 2815 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2816 | // Objective-C ARC: |
| 2817 | // Check Objective-C lifetime conversions. |
| 2818 | if (FromQuals.getObjCLifetime() != ToQuals.getObjCLifetime() && |
| 2819 | UnwrappedAnyPointer) { |
| 2820 | if (ToQuals.compatiblyIncludesObjCLifetime(FromQuals)) { |
| 2821 | ObjCLifetimeConversion = true; |
| 2822 | FromQuals.removeObjCLifetime(); |
| 2823 | ToQuals.removeObjCLifetime(); |
| 2824 | } else { |
| 2825 | // Qualification conversions cannot cast between different |
| 2826 | // Objective-C lifetime qualifiers. |
| 2827 | return false; |
| 2828 | } |
| 2829 | } |
| 2830 | |
Douglas Gregor | 377e1bd | 2011-05-08 06:09:53 +0000 | [diff] [blame] | 2831 | // Allow addition/removal of GC attributes but not changing GC attributes. |
| 2832 | if (FromQuals.getObjCGCAttr() != ToQuals.getObjCGCAttr() && |
| 2833 | (!FromQuals.hasObjCGCAttr() || !ToQuals.hasObjCGCAttr())) { |
| 2834 | FromQuals.removeObjCGCAttr(); |
| 2835 | ToQuals.removeObjCGCAttr(); |
| 2836 | } |
| 2837 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2838 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 2839 | // 2,j, and similarly for volatile. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2840 | if (!CStyle && !ToQuals.compatiblyIncludes(FromQuals)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2841 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2842 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2843 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 2844 | // every cv for 0 < k < j. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2845 | if (!CStyle && FromQuals.getCVRQualifiers() != ToQuals.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2846 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2847 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2848 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2849 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 2850 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2851 | PreviousToQualsIncludeConst |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2852 | = PreviousToQualsIncludeConst && ToQuals.hasConst(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2853 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2854 | |
| 2855 | // We are left with FromType and ToType being the pointee types |
| 2856 | // after unwrapping the original FromType and ToType the same number |
| 2857 | // of types. If we unwrapped any pointers, and if FromType and |
| 2858 | // ToType have the same unqualified type (since we checked |
| 2859 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2860 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2863 | /// \brief - Determine whether this is a conversion from a scalar type to an |
| 2864 | /// atomic type. |
| 2865 | /// |
| 2866 | /// If successful, updates \c SCS's second and third steps in the conversion |
| 2867 | /// sequence to finish the conversion. |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 2868 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 2869 | bool InOverloadResolution, |
| 2870 | StandardConversionSequence &SCS, |
| 2871 | bool CStyle) { |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2872 | const AtomicType *ToAtomic = ToType->getAs<AtomicType>(); |
| 2873 | if (!ToAtomic) |
| 2874 | return false; |
| 2875 | |
| 2876 | StandardConversionSequence InnerSCS; |
| 2877 | if (!IsStandardConversion(S, From, ToAtomic->getValueType(), |
| 2878 | InOverloadResolution, InnerSCS, |
| 2879 | CStyle, /*AllowObjCWritebackConversion=*/false)) |
| 2880 | return false; |
| 2881 | |
| 2882 | SCS.Second = InnerSCS.Second; |
| 2883 | SCS.setToType(1, InnerSCS.getToType(1)); |
| 2884 | SCS.Third = InnerSCS.Third; |
| 2885 | SCS.QualificationIncludesObjCLifetime |
| 2886 | = InnerSCS.QualificationIncludesObjCLifetime; |
| 2887 | SCS.setToType(2, InnerSCS.getToType(2)); |
| 2888 | return true; |
| 2889 | } |
| 2890 | |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2891 | static bool isFirstArgumentCompatibleWithType(ASTContext &Context, |
| 2892 | CXXConstructorDecl *Constructor, |
| 2893 | QualType Type) { |
| 2894 | const FunctionProtoType *CtorType = |
| 2895 | Constructor->getType()->getAs<FunctionProtoType>(); |
| 2896 | if (CtorType->getNumArgs() > 0) { |
| 2897 | QualType FirstArg = CtorType->getArgType(0); |
| 2898 | if (Context.hasSameUnqualifiedType(Type, FirstArg.getNonReferenceType())) |
| 2899 | return true; |
| 2900 | } |
| 2901 | return false; |
| 2902 | } |
| 2903 | |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2904 | static OverloadingResult |
| 2905 | IsInitializerListConstructorConversion(Sema &S, Expr *From, QualType ToType, |
| 2906 | CXXRecordDecl *To, |
| 2907 | UserDefinedConversionSequence &User, |
| 2908 | OverloadCandidateSet &CandidateSet, |
| 2909 | bool AllowExplicit) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2910 | DeclContext::lookup_result R = S.LookupConstructors(To); |
| 2911 | for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2912 | Con != ConEnd; ++Con) { |
| 2913 | NamedDecl *D = *Con; |
| 2914 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2915 | |
| 2916 | // Find the constructor (which may be a template). |
| 2917 | CXXConstructorDecl *Constructor = 0; |
| 2918 | FunctionTemplateDecl *ConstructorTmpl |
| 2919 | = dyn_cast<FunctionTemplateDecl>(D); |
| 2920 | if (ConstructorTmpl) |
| 2921 | Constructor |
| 2922 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 2923 | else |
| 2924 | Constructor = cast<CXXConstructorDecl>(D); |
| 2925 | |
| 2926 | bool Usable = !Constructor->isInvalidDecl() && |
| 2927 | S.isInitListConstructor(Constructor) && |
| 2928 | (AllowExplicit || !Constructor->isExplicit()); |
| 2929 | if (Usable) { |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2930 | // If the first argument is (a reference to) the target type, |
| 2931 | // suppress conversions. |
| 2932 | bool SuppressUserConversions = |
| 2933 | isFirstArgumentCompatibleWithType(S.Context, Constructor, ToType); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2934 | if (ConstructorTmpl) |
| 2935 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2936 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2937 | From, CandidateSet, |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2938 | SuppressUserConversions); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2939 | else |
| 2940 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2941 | From, CandidateSet, |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2942 | SuppressUserConversions); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2943 | } |
| 2944 | } |
| 2945 | |
| 2946 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 2947 | |
| 2948 | OverloadCandidateSet::iterator Best; |
| 2949 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
| 2950 | case OR_Success: { |
| 2951 | // Record the standard conversion we used and the conversion function. |
| 2952 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2953 | QualType ThisType = Constructor->getThisType(S.Context); |
| 2954 | // Initializer lists don't have conversions as such. |
| 2955 | User.Before.setAsIdentityConversion(); |
| 2956 | User.HadMultipleCandidates = HadMultipleCandidates; |
| 2957 | User.ConversionFunction = Constructor; |
| 2958 | User.FoundConversionFunction = Best->FoundDecl; |
| 2959 | User.After.setAsIdentityConversion(); |
| 2960 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 2961 | User.After.setAllToTypes(ToType); |
| 2962 | return OR_Success; |
| 2963 | } |
| 2964 | |
| 2965 | case OR_No_Viable_Function: |
| 2966 | return OR_No_Viable_Function; |
| 2967 | case OR_Deleted: |
| 2968 | return OR_Deleted; |
| 2969 | case OR_Ambiguous: |
| 2970 | return OR_Ambiguous; |
| 2971 | } |
| 2972 | |
| 2973 | llvm_unreachable("Invalid OverloadResult!"); |
| 2974 | } |
| 2975 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2976 | /// Determines whether there is a user-defined conversion sequence |
| 2977 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 2978 | /// ToType. If such a conversion exists, User will contain the |
| 2979 | /// user-defined conversion sequence that performs such a conversion |
| 2980 | /// and this routine will return true. Otherwise, this routine returns |
| 2981 | /// false and User is unspecified. |
| 2982 | /// |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2983 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 2984 | /// "explicit" conversion functions as well as non-explicit conversion |
| 2985 | /// functions (C++0x [class.conv.fct]p2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2986 | static OverloadingResult |
| 2987 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2988 | UserDefinedConversionSequence &User, |
| 2989 | OverloadCandidateSet &CandidateSet, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2990 | bool AllowExplicit) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2991 | // Whether we will only visit constructors. |
| 2992 | bool ConstructorsOnly = false; |
| 2993 | |
| 2994 | // If the type we are conversion to is a class type, enumerate its |
| 2995 | // constructors. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2996 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2997 | // C++ [over.match.ctor]p1: |
| 2998 | // When objects of class type are direct-initialized (8.5), or |
| 2999 | // copy-initialized from an expression of the same or a |
| 3000 | // derived class type (8.5), overload resolution selects the |
| 3001 | // constructor. [...] For copy-initialization, the candidate |
| 3002 | // functions are all the converting constructors (12.3.1) of |
| 3003 | // that class. The argument list is the expression-list within |
| 3004 | // the parentheses of the initializer. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3005 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3006 | (From->getType()->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3007 | S.IsDerivedFrom(From->getType(), ToType))) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3008 | ConstructorsOnly = true; |
| 3009 | |
Benjamin Kramer | 63b6ebe | 2012-11-23 17:04:52 +0000 | [diff] [blame] | 3010 | S.RequireCompleteType(From->getExprLoc(), ToType, 0); |
Argyrios Kyrtzidis | e36bca6 | 2011-04-22 17:45:37 +0000 | [diff] [blame] | 3011 | // RequireCompleteType may have returned true due to some invalid decl |
| 3012 | // during template instantiation, but ToType may be complete enough now |
| 3013 | // to try to recover. |
| 3014 | if (ToType->isIncompleteType()) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 3015 | // We're not going to find any constructors. |
| 3016 | } else if (CXXRecordDecl *ToRecordDecl |
| 3017 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3018 | |
| 3019 | Expr **Args = &From; |
| 3020 | unsigned NumArgs = 1; |
| 3021 | bool ListInitializing = false; |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3022 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(From)) { |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3023 | // But first, see if there is an init-list-contructor that will work. |
| 3024 | OverloadingResult Result = IsInitializerListConstructorConversion( |
| 3025 | S, From, ToType, ToRecordDecl, User, CandidateSet, AllowExplicit); |
| 3026 | if (Result != OR_No_Viable_Function) |
| 3027 | return Result; |
| 3028 | // Never mind. |
| 3029 | CandidateSet.clear(); |
| 3030 | |
| 3031 | // If we're list-initializing, we pass the individual elements as |
| 3032 | // arguments, not the entire list. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3033 | Args = InitList->getInits(); |
| 3034 | NumArgs = InitList->getNumInits(); |
| 3035 | ListInitializing = true; |
| 3036 | } |
| 3037 | |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3038 | DeclContext::lookup_result R = S.LookupConstructors(ToRecordDecl); |
| 3039 | for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3040 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3041 | NamedDecl *D = *Con; |
| 3042 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3043 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3044 | // Find the constructor (which may be a template). |
| 3045 | CXXConstructorDecl *Constructor = 0; |
| 3046 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3047 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3048 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3049 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3050 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 3051 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3052 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3053 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3054 | bool Usable = !Constructor->isInvalidDecl(); |
| 3055 | if (ListInitializing) |
| 3056 | Usable = Usable && (AllowExplicit || !Constructor->isExplicit()); |
| 3057 | else |
| 3058 | Usable = Usable &&Constructor->isConvertingConstructor(AllowExplicit); |
| 3059 | if (Usable) { |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3060 | bool SuppressUserConversions = !ConstructorsOnly; |
| 3061 | if (SuppressUserConversions && ListInitializing) { |
| 3062 | SuppressUserConversions = false; |
| 3063 | if (NumArgs == 1) { |
| 3064 | // If the first argument is (a reference to) the target type, |
| 3065 | // suppress conversions. |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 3066 | SuppressUserConversions = isFirstArgumentCompatibleWithType( |
| 3067 | S.Context, Constructor, ToType); |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3068 | } |
| 3069 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3070 | if (ConstructorTmpl) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3071 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3072 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3073 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3074 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3075 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 3076 | // Allow one user-defined conversion when user specifies a |
| 3077 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3078 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3079 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3080 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3081 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3082 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3083 | } |
| 3084 | } |
| 3085 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3086 | // Enumerate conversion functions, if we're allowed to. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3087 | if (ConstructorsOnly || isa<InitListExpr>(From)) { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3088 | } else if (S.RequireCompleteType(From->getLocStart(), From->getType(), 0)) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3089 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3090 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3091 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3092 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3093 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 3094 | // Add all of the conversion functions as candidates. |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3095 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3096 | CXXRecordDecl::conversion_iterator> |
| 3097 | Conversions = FromRecordDecl->getVisibleConversionFunctions(); |
| 3098 | for (CXXRecordDecl::conversion_iterator |
| 3099 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3100 | DeclAccessPair FoundDecl = I.getPair(); |
| 3101 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3102 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3103 | if (isa<UsingShadowDecl>(D)) |
| 3104 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3105 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3106 | CXXConversionDecl *Conv; |
| 3107 | FunctionTemplateDecl *ConvTemplate; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3108 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 3109 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3110 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3111 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3112 | |
| 3113 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3114 | if (ConvTemplate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3115 | S.AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
| 3116 | ActingContext, From, ToType, |
| 3117 | CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3118 | else |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3119 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, |
| 3120 | From, ToType, CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3121 | } |
| 3122 | } |
| 3123 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3124 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3125 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3126 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3127 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3128 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3129 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3130 | case OR_Success: |
| 3131 | // Record the standard conversion we used and the conversion function. |
| 3132 | if (CXXConstructorDecl *Constructor |
| 3133 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 3134 | // C++ [over.ics.user]p1: |
| 3135 | // If the user-defined conversion is specified by a |
| 3136 | // constructor (12.3.1), the initial standard conversion |
| 3137 | // sequence converts the source type to the type required by |
| 3138 | // the argument of the constructor. |
| 3139 | // |
| 3140 | QualType ThisType = Constructor->getThisType(S.Context); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3141 | if (isa<InitListExpr>(From)) { |
| 3142 | // Initializer lists don't have conversions as such. |
| 3143 | User.Before.setAsIdentityConversion(); |
| 3144 | } else { |
| 3145 | if (Best->Conversions[0].isEllipsis()) |
| 3146 | User.EllipsisConversion = true; |
| 3147 | else { |
| 3148 | User.Before = Best->Conversions[0].Standard; |
| 3149 | User.EllipsisConversion = false; |
| 3150 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3151 | } |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3152 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3153 | User.ConversionFunction = Constructor; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3154 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3155 | User.After.setAsIdentityConversion(); |
| 3156 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 3157 | User.After.setAllToTypes(ToType); |
| 3158 | return OR_Success; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3159 | } |
| 3160 | if (CXXConversionDecl *Conversion |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3161 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 3162 | // C++ [over.ics.user]p1: |
| 3163 | // |
| 3164 | // [...] If the user-defined conversion is specified by a |
| 3165 | // conversion function (12.3.2), the initial standard |
| 3166 | // conversion sequence converts the source type to the |
| 3167 | // implicit object parameter of the conversion function. |
| 3168 | User.Before = Best->Conversions[0].Standard; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3169 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3170 | User.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3171 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3172 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3173 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3174 | // C++ [over.ics.user]p2: |
| 3175 | // The second standard conversion sequence converts the |
| 3176 | // result of the user-defined conversion to the target type |
| 3177 | // for the sequence. Since an implicit conversion sequence |
| 3178 | // is an initialization, the special rules for |
| 3179 | // initialization by user-defined conversion apply when |
| 3180 | // selecting the best user-defined conversion for a |
| 3181 | // user-defined conversion sequence (see 13.3.3 and |
| 3182 | // 13.3.3.1). |
| 3183 | User.After = Best->FinalConversion; |
| 3184 | return OR_Success; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3185 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3186 | llvm_unreachable("Not a constructor or conversion function?"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3187 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3188 | case OR_No_Viable_Function: |
| 3189 | return OR_No_Viable_Function; |
| 3190 | case OR_Deleted: |
| 3191 | // No conversion here! We're done. |
| 3192 | return OR_Deleted; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3193 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3194 | case OR_Ambiguous: |
| 3195 | return OR_Ambiguous; |
| 3196 | } |
| 3197 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3198 | llvm_unreachable("Invalid OverloadResult!"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3199 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3200 | |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3201 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3202 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3203 | ImplicitConversionSequence ICS; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3204 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3205 | OverloadingResult OvResult = |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3206 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3207 | CandidateSet, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3208 | if (OvResult == OR_Ambiguous) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3209 | Diag(From->getLocStart(), |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3210 | diag::err_typecheck_ambiguous_condition) |
| 3211 | << From->getType() << ToType << From->getSourceRange(); |
Larisse Voufo | 7419d01 | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 3212 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) { |
Larisse Voufo | 288f76a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 3213 | if (!RequireCompleteType(From->getLocStart(), ToType, |
Larisse Voufo | 7419d01 | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 3214 | diag::err_typecheck_nonviable_condition_incomplete, |
| 3215 | From->getType(), From->getSourceRange())) |
| 3216 | Diag(From->getLocStart(), |
| 3217 | diag::err_typecheck_nonviable_condition) |
| 3218 | << From->getType() << From->getSourceRange() << ToType; |
| 3219 | } |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3220 | else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3221 | return false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3222 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3223 | return true; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3224 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3225 | |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3226 | /// \brief Compare the user-defined conversion functions or constructors |
| 3227 | /// of two user-defined conversion sequences to determine whether any ordering |
| 3228 | /// is possible. |
| 3229 | static ImplicitConversionSequence::CompareKind |
| 3230 | compareConversionFunctions(Sema &S, |
| 3231 | FunctionDecl *Function1, |
| 3232 | FunctionDecl *Function2) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3233 | if (!S.getLangOpts().ObjC1 || !S.getLangOpts().CPlusPlus11) |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3234 | return ImplicitConversionSequence::Indistinguishable; |
| 3235 | |
| 3236 | // Objective-C++: |
| 3237 | // If both conversion functions are implicitly-declared conversions from |
| 3238 | // a lambda closure type to a function pointer and a block pointer, |
| 3239 | // respectively, always prefer the conversion to a function pointer, |
| 3240 | // because the function pointer is more lightweight and is more likely |
| 3241 | // to keep code working. |
| 3242 | CXXConversionDecl *Conv1 = dyn_cast<CXXConversionDecl>(Function1); |
| 3243 | if (!Conv1) |
| 3244 | return ImplicitConversionSequence::Indistinguishable; |
| 3245 | |
| 3246 | CXXConversionDecl *Conv2 = dyn_cast<CXXConversionDecl>(Function2); |
| 3247 | if (!Conv2) |
| 3248 | return ImplicitConversionSequence::Indistinguishable; |
| 3249 | |
| 3250 | if (Conv1->getParent()->isLambda() && Conv2->getParent()->isLambda()) { |
| 3251 | bool Block1 = Conv1->getConversionType()->isBlockPointerType(); |
| 3252 | bool Block2 = Conv2->getConversionType()->isBlockPointerType(); |
| 3253 | if (Block1 != Block2) |
| 3254 | return Block1? ImplicitConversionSequence::Worse |
| 3255 | : ImplicitConversionSequence::Better; |
| 3256 | } |
| 3257 | |
| 3258 | return ImplicitConversionSequence::Indistinguishable; |
| 3259 | } |
| 3260 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3261 | /// CompareImplicitConversionSequences - Compare two implicit |
| 3262 | /// conversion sequences to determine whether one is better than the |
| 3263 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3264 | static ImplicitConversionSequence::CompareKind |
| 3265 | CompareImplicitConversionSequences(Sema &S, |
| 3266 | const ImplicitConversionSequence& ICS1, |
| 3267 | const ImplicitConversionSequence& ICS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3268 | { |
| 3269 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 3270 | // conversion sequences (as defined in 13.3.3.1) |
| 3271 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 3272 | // conversion sequence than a user-defined conversion sequence or |
| 3273 | // an ellipsis conversion sequence, and |
| 3274 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 3275 | // conversion sequence than an ellipsis conversion sequence |
| 3276 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3277 | // |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3278 | // C++0x [over.best.ics]p10: |
| 3279 | // For the purpose of ranking implicit conversion sequences as |
| 3280 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 3281 | // treated as a user-defined sequence that is indistinguishable |
| 3282 | // from any other user-defined conversion sequence. |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3283 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 3284 | return ImplicitConversionSequence::Better; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3285 | if (ICS2.getKindRank() < ICS1.getKindRank()) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3286 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3287 | |
Benjamin Kramer | b6eee07 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 3288 | // The following checks require both conversion sequences to be of |
| 3289 | // the same kind. |
| 3290 | if (ICS1.getKind() != ICS2.getKind()) |
| 3291 | return ImplicitConversionSequence::Indistinguishable; |
| 3292 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3293 | ImplicitConversionSequence::CompareKind Result = |
| 3294 | ImplicitConversionSequence::Indistinguishable; |
| 3295 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3296 | // Two implicit conversion sequences of the same form are |
| 3297 | // indistinguishable conversion sequences unless one of the |
| 3298 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3299 | if (ICS1.isStandard()) |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3300 | Result = CompareStandardConversionSequences(S, |
| 3301 | ICS1.Standard, ICS2.Standard); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3302 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3303 | // User-defined conversion sequence U1 is a better conversion |
| 3304 | // sequence than another user-defined conversion sequence U2 if |
| 3305 | // they contain the same user-defined conversion function or |
| 3306 | // constructor and if the second standard conversion sequence of |
| 3307 | // U1 is better than the second standard conversion sequence of |
| 3308 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3309 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3310 | ICS2.UserDefined.ConversionFunction) |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3311 | Result = CompareStandardConversionSequences(S, |
| 3312 | ICS1.UserDefined.After, |
| 3313 | ICS2.UserDefined.After); |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3314 | else |
| 3315 | Result = compareConversionFunctions(S, |
| 3316 | ICS1.UserDefined.ConversionFunction, |
| 3317 | ICS2.UserDefined.ConversionFunction); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3318 | } |
| 3319 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3320 | // List-initialization sequence L1 is a better conversion sequence than |
| 3321 | // list-initialization sequence L2 if L1 converts to std::initializer_list<X> |
| 3322 | // for some X and L2 does not. |
| 3323 | if (Result == ImplicitConversionSequence::Indistinguishable && |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 3324 | !ICS1.isBad() && |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3325 | ICS1.isListInitializationSequence() && |
| 3326 | ICS2.isListInitializationSequence()) { |
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); |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4405 | Result.setListInitializationSequence(); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4406 | |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4407 | // 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] | 4408 | // initialized from init lists. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4409 | if (S.RequireCompleteType(From->getLocStart(), ToType, 0)) |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4410 | return Result; |
| 4411 | |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4412 | // C++11 [over.ics.list]p2: |
| 4413 | // If the parameter type is std::initializer_list<X> or "array of X" and |
| 4414 | // all the elements can be implicitly converted to X, the implicit |
| 4415 | // conversion sequence is the worst conversion necessary to convert an |
| 4416 | // element of the list to X. |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4417 | bool toStdInitializerList = false; |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4418 | QualType X; |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4419 | if (ToType->isArrayType()) |
Richard Smith | 2801d9a | 2012-12-09 06:48:56 +0000 | [diff] [blame] | 4420 | X = S.Context.getAsArrayType(ToType)->getElementType(); |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4421 | else |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4422 | toStdInitializerList = S.isStdInitializerList(ToType, &X); |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4423 | if (!X.isNull()) { |
| 4424 | for (unsigned i = 0, e = From->getNumInits(); i < e; ++i) { |
| 4425 | Expr *Init = From->getInit(i); |
| 4426 | ImplicitConversionSequence ICS = |
| 4427 | TryCopyInitialization(S, Init, X, SuppressUserConversions, |
| 4428 | InOverloadResolution, |
| 4429 | AllowObjCWritebackConversion); |
| 4430 | // If a single element isn't convertible, fail. |
| 4431 | if (ICS.isBad()) { |
| 4432 | Result = ICS; |
| 4433 | break; |
| 4434 | } |
| 4435 | // Otherwise, look for the worst conversion. |
| 4436 | if (Result.isBad() || |
| 4437 | CompareImplicitConversionSequences(S, ICS, Result) == |
| 4438 | ImplicitConversionSequence::Worse) |
| 4439 | Result = ICS; |
| 4440 | } |
Douglas Gregor | 5b4bf13 | 2012-04-04 23:09:20 +0000 | [diff] [blame] | 4441 | |
| 4442 | // For an empty list, we won't have computed any conversion sequence. |
| 4443 | // Introduce the identity conversion sequence. |
| 4444 | if (From->getNumInits() == 0) { |
| 4445 | Result.setStandard(); |
| 4446 | Result.Standard.setAsIdentityConversion(); |
| 4447 | Result.Standard.setFromType(ToType); |
| 4448 | Result.Standard.setAllToTypes(ToType); |
| 4449 | } |
| 4450 | |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4451 | Result.setListInitializationSequence(); |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4452 | Result.setStdInitializerListElement(toStdInitializerList); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4453 | return Result; |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4454 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4455 | |
| 4456 | // C++11 [over.ics.list]p3: |
| 4457 | // Otherwise, if the parameter is a non-aggregate class X and overload |
| 4458 | // resolution chooses a single best constructor [...] the implicit |
| 4459 | // conversion sequence is a user-defined conversion sequence. If multiple |
| 4460 | // constructors are viable but none is better than the others, the |
| 4461 | // implicit conversion sequence is a user-defined conversion sequence. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4462 | if (ToType->isRecordType() && !ToType->isAggregateType()) { |
| 4463 | // This function can deal with initializer lists. |
| 4464 | Result = TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 4465 | /*AllowExplicit=*/false, |
| 4466 | InOverloadResolution, /*CStyle=*/false, |
| 4467 | AllowObjCWritebackConversion); |
| 4468 | Result.setListInitializationSequence(); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4469 | return Result; |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4470 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4471 | |
| 4472 | // C++11 [over.ics.list]p4: |
| 4473 | // Otherwise, if the parameter has an aggregate type which can be |
| 4474 | // initialized from the initializer list [...] the implicit conversion |
| 4475 | // sequence is a user-defined conversion sequence. |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4476 | if (ToType->isAggregateType()) { |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4477 | // Type is an aggregate, argument is an init list. At this point it comes |
| 4478 | // down to checking whether the initialization works. |
| 4479 | // FIXME: Find out whether this parameter is consumed or not. |
| 4480 | InitializedEntity Entity = |
| 4481 | InitializedEntity::InitializeParameter(S.Context, ToType, |
| 4482 | /*Consumed=*/false); |
| 4483 | if (S.CanPerformCopyInitialization(Entity, S.Owned(From))) { |
| 4484 | Result.setUserDefined(); |
| 4485 | Result.UserDefined.Before.setAsIdentityConversion(); |
| 4486 | // Initializer lists don't have a type. |
| 4487 | Result.UserDefined.Before.setFromType(QualType()); |
| 4488 | Result.UserDefined.Before.setAllToTypes(QualType()); |
| 4489 | |
| 4490 | Result.UserDefined.After.setAsIdentityConversion(); |
| 4491 | Result.UserDefined.After.setFromType(ToType); |
| 4492 | Result.UserDefined.After.setAllToTypes(ToType); |
Benjamin Kramer | 83db10e | 2012-02-02 19:35:29 +0000 | [diff] [blame] | 4493 | Result.UserDefined.ConversionFunction = 0; |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4494 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4495 | return Result; |
| 4496 | } |
| 4497 | |
| 4498 | // C++11 [over.ics.list]p5: |
| 4499 | // 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] | 4500 | if (ToType->isReferenceType()) { |
| 4501 | // The standard is notoriously unclear here, since 13.3.3.1.4 doesn't |
| 4502 | // mention initializer lists in any way. So we go by what list- |
| 4503 | // initialization would do and try to extrapolate from that. |
| 4504 | |
| 4505 | QualType T1 = ToType->getAs<ReferenceType>()->getPointeeType(); |
| 4506 | |
| 4507 | // If the initializer list has a single element that is reference-related |
| 4508 | // to the parameter type, we initialize the reference from that. |
| 4509 | if (From->getNumInits() == 1) { |
| 4510 | Expr *Init = From->getInit(0); |
| 4511 | |
| 4512 | QualType T2 = Init->getType(); |
| 4513 | |
| 4514 | // If the initializer is the address of an overloaded function, try |
| 4515 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4516 | // type of the resulting function. |
| 4517 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4518 | DeclAccessPair Found; |
| 4519 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction( |
| 4520 | Init, ToType, false, Found)) |
| 4521 | T2 = Fn->getType(); |
| 4522 | } |
| 4523 | |
| 4524 | // Compute some basic properties of the types and the initializer. |
| 4525 | bool dummy1 = false; |
| 4526 | bool dummy2 = false; |
| 4527 | bool dummy3 = false; |
| 4528 | Sema::ReferenceCompareResult RefRelationship |
| 4529 | = S.CompareReferenceRelationship(From->getLocStart(), T1, T2, dummy1, |
| 4530 | dummy2, dummy3); |
| 4531 | |
Richard Smith | 3082be2 | 2013-09-06 01:22:42 +0000 | [diff] [blame] | 4532 | if (RefRelationship >= Sema::Ref_Related) { |
| 4533 | Result = TryReferenceInit(S, Init, ToType, /*FIXME*/From->getLocStart(), |
| 4534 | SuppressUserConversions, |
| 4535 | /*AllowExplicit=*/false); |
| 4536 | Result.setListInitializationSequence(); |
| 4537 | return Result; |
| 4538 | } |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4539 | } |
| 4540 | |
| 4541 | // Otherwise, we bind the reference to a temporary created from the |
| 4542 | // initializer list. |
| 4543 | Result = TryListConversion(S, From, T1, SuppressUserConversions, |
| 4544 | InOverloadResolution, |
| 4545 | AllowObjCWritebackConversion); |
| 4546 | if (Result.isFailure()) |
| 4547 | return Result; |
| 4548 | assert(!Result.isEllipsis() && |
| 4549 | "Sub-initialization cannot result in ellipsis conversion."); |
| 4550 | |
| 4551 | // Can we even bind to a temporary? |
| 4552 | if (ToType->isRValueReferenceType() || |
| 4553 | (T1.isConstQualified() && !T1.isVolatileQualified())) { |
| 4554 | StandardConversionSequence &SCS = Result.isStandard() ? Result.Standard : |
| 4555 | Result.UserDefined.After; |
| 4556 | SCS.ReferenceBinding = true; |
| 4557 | SCS.IsLvalueReference = ToType->isLValueReferenceType(); |
| 4558 | SCS.BindsToRvalue = true; |
| 4559 | SCS.BindsToFunctionLvalue = false; |
| 4560 | SCS.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4561 | SCS.ObjCLifetimeConversionBinding = false; |
| 4562 | } else |
| 4563 | Result.setBad(BadConversionSequence::lvalue_ref_to_rvalue, |
| 4564 | From, ToType); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4565 | return Result; |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4566 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4567 | |
| 4568 | // C++11 [over.ics.list]p6: |
| 4569 | // Otherwise, if the parameter type is not a class: |
| 4570 | if (!ToType->isRecordType()) { |
| 4571 | // - if the initializer list has one element, the implicit conversion |
| 4572 | // sequence is the one required to convert the element to the |
| 4573 | // parameter type. |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4574 | unsigned NumInits = From->getNumInits(); |
| 4575 | if (NumInits == 1) |
| 4576 | Result = TryCopyInitialization(S, From->getInit(0), ToType, |
| 4577 | SuppressUserConversions, |
| 4578 | InOverloadResolution, |
| 4579 | AllowObjCWritebackConversion); |
| 4580 | // - if the initializer list has no elements, the implicit conversion |
| 4581 | // sequence is the identity conversion. |
| 4582 | else if (NumInits == 0) { |
| 4583 | Result.setStandard(); |
| 4584 | Result.Standard.setAsIdentityConversion(); |
John McCall | e14ba2c | 2012-04-04 02:40:27 +0000 | [diff] [blame] | 4585 | Result.Standard.setFromType(ToType); |
| 4586 | Result.Standard.setAllToTypes(ToType); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4587 | } |
Sebastian Redl | 2422e82 | 2012-02-28 23:36:38 +0000 | [diff] [blame] | 4588 | Result.setListInitializationSequence(); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4589 | return Result; |
| 4590 | } |
| 4591 | |
| 4592 | // C++11 [over.ics.list]p7: |
| 4593 | // In all cases other than those enumerated above, no conversion is possible |
| 4594 | return Result; |
| 4595 | } |
| 4596 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4597 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 4598 | /// ToType from the expression From. Return the implicit conversion |
| 4599 | /// sequence required to pass this argument, which may be a bad |
| 4600 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4601 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | 74e386e | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 4602 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4603 | static ImplicitConversionSequence |
| 4604 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4605 | bool SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4606 | bool InOverloadResolution, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4607 | bool AllowObjCWritebackConversion, |
| 4608 | bool AllowExplicit) { |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4609 | if (InitListExpr *FromInitList = dyn_cast<InitListExpr>(From)) |
| 4610 | return TryListConversion(S, FromInitList, ToType, SuppressUserConversions, |
| 4611 | InOverloadResolution,AllowObjCWritebackConversion); |
| 4612 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4613 | if (ToType->isReferenceType()) |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4614 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4615 | /*FIXME:*/From->getLocStart(), |
| 4616 | SuppressUserConversions, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4617 | AllowExplicit); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4618 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4619 | return TryImplicitConversion(S, From, ToType, |
| 4620 | SuppressUserConversions, |
| 4621 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4622 | InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4623 | /*CStyle=*/false, |
| 4624 | AllowObjCWritebackConversion); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4625 | } |
| 4626 | |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 4627 | static bool TryCopyInitialization(const CanQualType FromQTy, |
| 4628 | const CanQualType ToQTy, |
| 4629 | Sema &S, |
| 4630 | SourceLocation Loc, |
| 4631 | ExprValueKind FromVK) { |
| 4632 | OpaqueValueExpr TmpExpr(Loc, FromQTy, FromVK); |
| 4633 | ImplicitConversionSequence ICS = |
| 4634 | TryCopyInitialization(S, &TmpExpr, ToQTy, true, true, false); |
| 4635 | |
| 4636 | return !ICS.isBad(); |
| 4637 | } |
| 4638 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4639 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 4640 | /// parameter of the given member function (@c Method) from the |
| 4641 | /// expression @p From. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4642 | static ImplicitConversionSequence |
Richard Smith | 98bfbf5 | 2013-01-26 02:07:32 +0000 | [diff] [blame] | 4643 | TryObjectArgumentInitialization(Sema &S, QualType FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4644 | Expr::Classification FromClassification, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4645 | CXXMethodDecl *Method, |
| 4646 | CXXRecordDecl *ActingContext) { |
| 4647 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 4648 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 4649 | // const volatile object. |
| 4650 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 4651 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4652 | QualType ImplicitParamType = S.Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4653 | |
| 4654 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 4655 | // to exit early. |
| 4656 | ImplicitConversionSequence ICS; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4657 | |
| 4658 | // We need to have an object of class type. |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4659 | if (const PointerType *PT = FromType->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4660 | FromType = PT->getPointeeType(); |
| 4661 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4662 | // When we had a pointer, it's implicitly dereferenced, so we |
| 4663 | // better have an lvalue. |
| 4664 | assert(FromClassification.isLValue()); |
| 4665 | } |
| 4666 | |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4667 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4668 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4669 | // C++0x [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4670 | // For non-static member functions, the type of the implicit object |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4671 | // parameter is |
| 4672 | // |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4673 | // - "lvalue reference to cv X" for functions declared without a |
| 4674 | // ref-qualifier or with the & ref-qualifier |
| 4675 | // - "rvalue reference to cv X" for functions declared with the && |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4676 | // ref-qualifier |
| 4677 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4678 | // 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] | 4679 | // cv-qualification on the member function declaration. |
| 4680 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4681 | // However, when finding an implicit conversion sequence for the argument, we |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4682 | // are not allowed to create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4683 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 4684 | // reference binding here, that allows class rvalues to bind to |
| 4685 | // non-constant references. |
| 4686 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4687 | // First check the qualifiers. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4688 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4689 | if (ImplicitParamType.getCVRQualifiers() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4690 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4691 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4692 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
Richard Smith | 98bfbf5 | 2013-01-26 02:07:32 +0000 | [diff] [blame] | 4693 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4694 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4695 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4696 | |
| 4697 | // Check that we have either the same type or a derived type. It |
| 4698 | // affects the conversion rank. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4699 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4700 | ImplicitConversionKind SecondKind; |
| 4701 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 4702 | SecondKind = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4703 | } else if (S.IsDerivedFrom(FromType, ClassType)) |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4704 | SecondKind = ICK_Derived_To_Base; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4705 | else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4706 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 4707 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4708 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4709 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4710 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4711 | // Check the ref-qualifier. |
| 4712 | switch (Method->getRefQualifier()) { |
| 4713 | case RQ_None: |
| 4714 | // Do nothing; we don't care about lvalueness or rvalueness. |
| 4715 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4716 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4717 | case RQ_LValue: |
| 4718 | if (!FromClassification.isLValue() && Quals != Qualifiers::Const) { |
| 4719 | // non-const lvalue reference cannot bind to an rvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4720 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4721 | ImplicitParamType); |
| 4722 | return ICS; |
| 4723 | } |
| 4724 | break; |
| 4725 | |
| 4726 | case RQ_RValue: |
| 4727 | if (!FromClassification.isRValue()) { |
| 4728 | // rvalue reference cannot bind to an lvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4729 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4730 | ImplicitParamType); |
| 4731 | return ICS; |
| 4732 | } |
| 4733 | break; |
| 4734 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4735 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4736 | // Success. Mark this as a reference binding. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4737 | ICS.setStandard(); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4738 | ICS.Standard.setAsIdentityConversion(); |
| 4739 | ICS.Standard.Second = SecondKind; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4740 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 4741 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4742 | ICS.Standard.ReferenceBinding = true; |
| 4743 | ICS.Standard.DirectBinding = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4744 | ICS.Standard.IsLvalueReference = Method->getRefQualifier() != RQ_RValue; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4745 | ICS.Standard.BindsToFunctionLvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4746 | ICS.Standard.BindsToRvalue = FromClassification.isRValue(); |
| 4747 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier |
| 4748 | = (Method->getRefQualifier() == RQ_None); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4749 | return ICS; |
| 4750 | } |
| 4751 | |
| 4752 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 4753 | /// the implicit object parameter for the given Method with the given |
| 4754 | /// expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4755 | ExprResult |
| 4756 | Sema::PerformObjectArgumentInitialization(Expr *From, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4757 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4758 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4759 | CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4760 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4761 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4762 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4763 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4764 | Expr::Classification FromClassification; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4765 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4766 | FromRecordType = PT->getPointeeType(); |
| 4767 | DestType = Method->getThisType(Context); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4768 | FromClassification = Expr::Classification::makeSimpleLValue(); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4769 | } else { |
| 4770 | FromRecordType = From->getType(); |
| 4771 | DestType = ImplicitParamRecordType; |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4772 | FromClassification = From->Classify(Context); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4773 | } |
| 4774 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4775 | // Note that we always use the true parent context when performing |
| 4776 | // the actual argument initialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4777 | ImplicitConversionSequence ICS |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4778 | = TryObjectArgumentInitialization(*this, From->getType(), FromClassification, |
| 4779 | Method, Method->getParent()); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4780 | if (ICS.isBad()) { |
| 4781 | if (ICS.Bad.Kind == BadConversionSequence::bad_qualifiers) { |
| 4782 | Qualifiers FromQs = FromRecordType.getQualifiers(); |
| 4783 | Qualifiers ToQs = DestType.getQualifiers(); |
| 4784 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 4785 | if (CVR) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4786 | Diag(From->getLocStart(), |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4787 | diag::err_member_function_call_bad_cvr) |
| 4788 | << Method->getDeclName() << FromRecordType << (CVR - 1) |
| 4789 | << From->getSourceRange(); |
| 4790 | Diag(Method->getLocation(), diag::note_previous_decl) |
| 4791 | << Method->getDeclName(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4792 | return ExprError(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4793 | } |
| 4794 | } |
| 4795 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4796 | return Diag(From->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4797 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4798 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4799 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4800 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4801 | if (ICS.Standard.Second == ICK_Derived_To_Base) { |
| 4802 | ExprResult FromRes = |
| 4803 | PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
| 4804 | if (FromRes.isInvalid()) |
| 4805 | return ExprError(); |
| 4806 | From = FromRes.take(); |
| 4807 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4808 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4809 | if (!Context.hasSameType(From->getType(), DestType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4810 | From = ImpCastExprToType(From, DestType, CK_NoOp, |
Richard Smith | acdfa4d | 2011-11-10 23:32:36 +0000 | [diff] [blame] | 4811 | From->getValueKind()).take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4812 | return Owned(From); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4813 | } |
| 4814 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4815 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 4816 | /// expression From to bool (C++0x [conv]p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4817 | static ImplicitConversionSequence |
| 4818 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
Douglas Gregor | c6dfe19 | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 4819 | // FIXME: This is pretty broken. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4820 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 4821 | // FIXME: Are these flags correct? |
| 4822 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4823 | /*AllowExplicit=*/true, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4824 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4825 | /*CStyle=*/false, |
| 4826 | /*AllowObjCWritebackConversion=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4827 | } |
| 4828 | |
| 4829 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 4830 | /// of the expression From to bool (C++0x [conv]p3). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4831 | ExprResult Sema::PerformContextuallyConvertToBool(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4832 | if (checkPlaceholderForOverload(*this, From)) |
| 4833 | return ExprError(); |
| 4834 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4835 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4836 | if (!ICS.isBad()) |
| 4837 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4838 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 4839 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4840 | return Diag(From->getLocStart(), |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4841 | diag::err_typecheck_bool_condition) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 4842 | << From->getType() << From->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4843 | return ExprError(); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4844 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4845 | |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4846 | /// Check that the specified conversion is permitted in a converted constant |
| 4847 | /// expression, according to C++11 [expr.const]p3. Return true if the conversion |
| 4848 | /// is acceptable. |
| 4849 | static bool CheckConvertedConstantConversions(Sema &S, |
| 4850 | StandardConversionSequence &SCS) { |
| 4851 | // Since we know that the target type is an integral or unscoped enumeration |
| 4852 | // type, most conversion kinds are impossible. All possible First and Third |
| 4853 | // conversions are fine. |
| 4854 | switch (SCS.Second) { |
| 4855 | case ICK_Identity: |
| 4856 | case ICK_Integral_Promotion: |
| 4857 | case ICK_Integral_Conversion: |
Guy Benyei | 6959acd | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 4858 | case ICK_Zero_Event_Conversion: |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4859 | return true; |
| 4860 | |
| 4861 | case ICK_Boolean_Conversion: |
Richard Smith | 2bcb984 | 2012-09-13 22:00:12 +0000 | [diff] [blame] | 4862 | // Conversion from an integral or unscoped enumeration type to bool is |
| 4863 | // classified as ICK_Boolean_Conversion, but it's also an integral |
| 4864 | // conversion, so it's permitted in a converted constant expression. |
| 4865 | return SCS.getFromType()->isIntegralOrUnscopedEnumerationType() && |
| 4866 | SCS.getToType(2)->isBooleanType(); |
| 4867 | |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4868 | case ICK_Floating_Integral: |
| 4869 | case ICK_Complex_Real: |
| 4870 | return false; |
| 4871 | |
| 4872 | case ICK_Lvalue_To_Rvalue: |
| 4873 | case ICK_Array_To_Pointer: |
| 4874 | case ICK_Function_To_Pointer: |
| 4875 | case ICK_NoReturn_Adjustment: |
| 4876 | case ICK_Qualification: |
| 4877 | case ICK_Compatible_Conversion: |
| 4878 | case ICK_Vector_Conversion: |
| 4879 | case ICK_Vector_Splat: |
| 4880 | case ICK_Derived_To_Base: |
| 4881 | case ICK_Pointer_Conversion: |
| 4882 | case ICK_Pointer_Member: |
| 4883 | case ICK_Block_Pointer_Conversion: |
| 4884 | case ICK_Writeback_Conversion: |
| 4885 | case ICK_Floating_Promotion: |
| 4886 | case ICK_Complex_Promotion: |
| 4887 | case ICK_Complex_Conversion: |
| 4888 | case ICK_Floating_Conversion: |
| 4889 | case ICK_TransparentUnionConversion: |
| 4890 | llvm_unreachable("unexpected second conversion kind"); |
| 4891 | |
| 4892 | case ICK_Num_Conversion_Kinds: |
| 4893 | break; |
| 4894 | } |
| 4895 | |
| 4896 | llvm_unreachable("unknown conversion kind"); |
| 4897 | } |
| 4898 | |
| 4899 | /// CheckConvertedConstantExpression - Check that the expression From is a |
| 4900 | /// converted constant expression of type T, perform the conversion and produce |
| 4901 | /// the converted expression, per C++11 [expr.const]p3. |
| 4902 | ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, |
| 4903 | llvm::APSInt &Value, |
| 4904 | CCEKind CCE) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4905 | assert(LangOpts.CPlusPlus11 && "converted constant expression outside C++11"); |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4906 | assert(T->isIntegralOrEnumerationType() && "unexpected converted const type"); |
| 4907 | |
| 4908 | if (checkPlaceholderForOverload(*this, From)) |
| 4909 | return ExprError(); |
| 4910 | |
| 4911 | // C++11 [expr.const]p3 with proposed wording fixes: |
| 4912 | // A converted constant expression of type T is a core constant expression, |
| 4913 | // implicitly converted to a prvalue of type T, where the converted |
| 4914 | // expression is a literal constant expression and the implicit conversion |
| 4915 | // sequence contains only user-defined conversions, lvalue-to-rvalue |
| 4916 | // conversions, integral promotions, and integral conversions other than |
| 4917 | // narrowing conversions. |
| 4918 | ImplicitConversionSequence ICS = |
| 4919 | TryImplicitConversion(From, T, |
| 4920 | /*SuppressUserConversions=*/false, |
| 4921 | /*AllowExplicit=*/false, |
| 4922 | /*InOverloadResolution=*/false, |
| 4923 | /*CStyle=*/false, |
| 4924 | /*AllowObjcWritebackConversion=*/false); |
| 4925 | StandardConversionSequence *SCS = 0; |
| 4926 | switch (ICS.getKind()) { |
| 4927 | case ImplicitConversionSequence::StandardConversion: |
| 4928 | if (!CheckConvertedConstantConversions(*this, ICS.Standard)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4929 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4930 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 4931 | << From->getType() << From->getSourceRange() << T; |
| 4932 | SCS = &ICS.Standard; |
| 4933 | break; |
| 4934 | case ImplicitConversionSequence::UserDefinedConversion: |
| 4935 | // We are converting from class type to an integral or enumeration type, so |
| 4936 | // the Before sequence must be trivial. |
| 4937 | if (!CheckConvertedConstantConversions(*this, ICS.UserDefined.After)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4938 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4939 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 4940 | << From->getType() << From->getSourceRange() << T; |
| 4941 | SCS = &ICS.UserDefined.After; |
| 4942 | break; |
| 4943 | case ImplicitConversionSequence::AmbiguousConversion: |
| 4944 | case ImplicitConversionSequence::BadConversion: |
| 4945 | if (!DiagnoseMultipleUserDefinedConversion(From, T)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4946 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4947 | diag::err_typecheck_converted_constant_expression) |
| 4948 | << From->getType() << From->getSourceRange() << T; |
| 4949 | return ExprError(); |
| 4950 | |
| 4951 | case ImplicitConversionSequence::EllipsisConversion: |
| 4952 | llvm_unreachable("ellipsis conversion in converted constant expression"); |
| 4953 | } |
| 4954 | |
| 4955 | ExprResult Result = PerformImplicitConversion(From, T, ICS, AA_Converting); |
| 4956 | if (Result.isInvalid()) |
| 4957 | return Result; |
| 4958 | |
| 4959 | // Check for a narrowing implicit conversion. |
| 4960 | APValue PreNarrowingValue; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4961 | QualType PreNarrowingType; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4962 | switch (SCS->getNarrowingKind(Context, Result.get(), PreNarrowingValue, |
| 4963 | PreNarrowingType)) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4964 | case NK_Variable_Narrowing: |
| 4965 | // Implicit conversion to a narrower type, and the value is not a constant |
| 4966 | // expression. We'll diagnose this in a moment. |
| 4967 | case NK_Not_Narrowing: |
| 4968 | break; |
| 4969 | |
| 4970 | case NK_Constant_Narrowing: |
Eli Friedman | 1ef28db | 2012-03-29 23:39:39 +0000 | [diff] [blame] | 4971 | Diag(From->getLocStart(), |
| 4972 | isSFINAEContext() ? diag::err_cce_narrowing_sfinae : |
| 4973 | diag::err_cce_narrowing) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4974 | << CCE << /*Constant*/1 |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4975 | << PreNarrowingValue.getAsString(Context, PreNarrowingType) << T; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4976 | break; |
| 4977 | |
| 4978 | case NK_Type_Narrowing: |
Eli Friedman | 1ef28db | 2012-03-29 23:39:39 +0000 | [diff] [blame] | 4979 | Diag(From->getLocStart(), |
| 4980 | isSFINAEContext() ? diag::err_cce_narrowing_sfinae : |
| 4981 | diag::err_cce_narrowing) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4982 | << CCE << /*Constant*/0 << From->getType() << T; |
| 4983 | break; |
| 4984 | } |
| 4985 | |
| 4986 | // Check the expression is a constant expression. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4987 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4988 | Expr::EvalResult Eval; |
| 4989 | Eval.Diag = &Notes; |
| 4990 | |
Douglas Gregor | 484f6fa | 2013-04-08 23:24:07 +0000 | [diff] [blame] | 4991 | if (!Result.get()->EvaluateAsRValue(Eval, Context) || !Eval.Val.isInt()) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4992 | // The expression can't be folded, so we can't keep it at this position in |
| 4993 | // the AST. |
| 4994 | Result = ExprError(); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 4995 | } else { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4996 | Value = Eval.Val.getInt(); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 4997 | |
| 4998 | if (Notes.empty()) { |
| 4999 | // It's a constant expression. |
| 5000 | return Result; |
| 5001 | } |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5002 | } |
| 5003 | |
| 5004 | // It's not a constant expression. Produce an appropriate diagnostic. |
| 5005 | if (Notes.size() == 1 && |
| 5006 | Notes[0].second.getDiagID() == diag::note_invalid_subexpr_in_const_expr) |
| 5007 | Diag(Notes[0].first, diag::err_expr_not_cce) << CCE; |
| 5008 | else { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5009 | Diag(From->getLocStart(), diag::err_expr_not_cce) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5010 | << CCE << From->getSourceRange(); |
| 5011 | for (unsigned I = 0; I < Notes.size(); ++I) |
| 5012 | Diag(Notes[I].first, Notes[I].second); |
| 5013 | } |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5014 | return Result; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5015 | } |
| 5016 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5017 | /// dropPointerConversions - If the given standard conversion sequence |
| 5018 | /// involves any pointer conversions, remove them. This may change |
| 5019 | /// the result type of the conversion sequence. |
| 5020 | static void dropPointerConversion(StandardConversionSequence &SCS) { |
| 5021 | if (SCS.Second == ICK_Pointer_Conversion) { |
| 5022 | SCS.Second = ICK_Identity; |
| 5023 | SCS.Third = ICK_Identity; |
| 5024 | SCS.ToTypePtrs[2] = SCS.ToTypePtrs[1] = SCS.ToTypePtrs[0]; |
| 5025 | } |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5026 | } |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5027 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5028 | /// TryContextuallyConvertToObjCPointer - Attempt to contextually |
| 5029 | /// convert the expression From to an Objective-C pointer type. |
| 5030 | static ImplicitConversionSequence |
| 5031 | TryContextuallyConvertToObjCPointer(Sema &S, Expr *From) { |
| 5032 | // Do an implicit conversion to 'id'. |
| 5033 | QualType Ty = S.Context.getObjCIdType(); |
| 5034 | ImplicitConversionSequence ICS |
| 5035 | = TryImplicitConversion(S, From, Ty, |
| 5036 | // FIXME: Are these flags correct? |
| 5037 | /*SuppressUserConversions=*/false, |
| 5038 | /*AllowExplicit=*/true, |
| 5039 | /*InOverloadResolution=*/false, |
| 5040 | /*CStyle=*/false, |
| 5041 | /*AllowObjCWritebackConversion=*/false); |
| 5042 | |
| 5043 | // Strip off any final conversions to 'id'. |
| 5044 | switch (ICS.getKind()) { |
| 5045 | case ImplicitConversionSequence::BadConversion: |
| 5046 | case ImplicitConversionSequence::AmbiguousConversion: |
| 5047 | case ImplicitConversionSequence::EllipsisConversion: |
| 5048 | break; |
| 5049 | |
| 5050 | case ImplicitConversionSequence::UserDefinedConversion: |
| 5051 | dropPointerConversion(ICS.UserDefined.After); |
| 5052 | break; |
| 5053 | |
| 5054 | case ImplicitConversionSequence::StandardConversion: |
| 5055 | dropPointerConversion(ICS.Standard); |
| 5056 | break; |
| 5057 | } |
| 5058 | |
| 5059 | return ICS; |
| 5060 | } |
| 5061 | |
| 5062 | /// PerformContextuallyConvertToObjCPointer - Perform a contextual |
| 5063 | /// conversion of the expression From to an Objective-C pointer type. |
| 5064 | ExprResult Sema::PerformContextuallyConvertToObjCPointer(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 5065 | if (checkPlaceholderForOverload(*this, From)) |
| 5066 | return ExprError(); |
| 5067 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5068 | QualType Ty = Context.getObjCIdType(); |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5069 | ImplicitConversionSequence ICS = |
| 5070 | TryContextuallyConvertToObjCPointer(*this, From); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5071 | if (!ICS.isBad()) |
| 5072 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5073 | return ExprError(); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5074 | } |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5075 | |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5076 | /// Determine whether the provided type is an integral type, or an enumeration |
| 5077 | /// type of a permitted flavor. |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5078 | bool Sema::ICEConvertDiagnoser::match(QualType T) { |
| 5079 | return AllowScopedEnumerations ? T->isIntegralOrEnumerationType() |
| 5080 | : T->isIntegralOrUnscopedEnumerationType(); |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5081 | } |
| 5082 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5083 | static ExprResult |
| 5084 | diagnoseAmbiguousConversion(Sema &SemaRef, SourceLocation Loc, Expr *From, |
| 5085 | Sema::ContextualImplicitConverter &Converter, |
| 5086 | QualType T, UnresolvedSetImpl &ViableConversions) { |
| 5087 | |
| 5088 | if (Converter.Suppress) |
| 5089 | return ExprError(); |
| 5090 | |
| 5091 | Converter.diagnoseAmbiguous(SemaRef, Loc, T) << From->getSourceRange(); |
| 5092 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5093 | CXXConversionDecl *Conv = |
| 5094 | cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 5095 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 5096 | Converter.noteAmbiguous(SemaRef, Conv, ConvTy); |
| 5097 | } |
| 5098 | return SemaRef.Owned(From); |
| 5099 | } |
| 5100 | |
| 5101 | static bool |
| 5102 | diagnoseNoViableConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
| 5103 | Sema::ContextualImplicitConverter &Converter, |
| 5104 | QualType T, bool HadMultipleCandidates, |
| 5105 | UnresolvedSetImpl &ExplicitConversions) { |
| 5106 | if (ExplicitConversions.size() == 1 && !Converter.Suppress) { |
| 5107 | DeclAccessPair Found = ExplicitConversions[0]; |
| 5108 | CXXConversionDecl *Conversion = |
| 5109 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 5110 | |
| 5111 | // The user probably meant to invoke the given explicit |
| 5112 | // conversion; use it. |
| 5113 | QualType ConvTy = Conversion->getConversionType().getNonReferenceType(); |
| 5114 | std::string TypeStr; |
| 5115 | ConvTy.getAsStringInternal(TypeStr, SemaRef.getPrintingPolicy()); |
| 5116 | |
| 5117 | Converter.diagnoseExplicitConv(SemaRef, Loc, T, ConvTy) |
| 5118 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 5119 | "static_cast<" + TypeStr + ">(") |
| 5120 | << FixItHint::CreateInsertion( |
| 5121 | SemaRef.PP.getLocForEndOfToken(From->getLocEnd()), ")"); |
| 5122 | Converter.noteExplicitConv(SemaRef, Conversion, ConvTy); |
| 5123 | |
| 5124 | // If we aren't in a SFINAE context, build a call to the |
| 5125 | // explicit conversion function. |
| 5126 | if (SemaRef.isSFINAEContext()) |
| 5127 | return true; |
| 5128 | |
| 5129 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
| 5130 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5131 | HadMultipleCandidates); |
| 5132 | if (Result.isInvalid()) |
| 5133 | return true; |
| 5134 | // Record usage of conversion in an implicit cast. |
| 5135 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
| 5136 | CK_UserDefinedConversion, Result.get(), 0, |
| 5137 | Result.get()->getValueKind()); |
| 5138 | } |
| 5139 | return false; |
| 5140 | } |
| 5141 | |
| 5142 | static bool recordConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
| 5143 | Sema::ContextualImplicitConverter &Converter, |
| 5144 | QualType T, bool HadMultipleCandidates, |
| 5145 | DeclAccessPair &Found) { |
| 5146 | CXXConversionDecl *Conversion = |
| 5147 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 5148 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
| 5149 | |
| 5150 | QualType ToType = Conversion->getConversionType().getNonReferenceType(); |
| 5151 | if (!Converter.SuppressConversion) { |
| 5152 | if (SemaRef.isSFINAEContext()) |
| 5153 | return true; |
| 5154 | |
| 5155 | Converter.diagnoseConversion(SemaRef, Loc, T, ToType) |
| 5156 | << From->getSourceRange(); |
| 5157 | } |
| 5158 | |
| 5159 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5160 | HadMultipleCandidates); |
| 5161 | if (Result.isInvalid()) |
| 5162 | return true; |
| 5163 | // Record usage of conversion in an implicit cast. |
| 5164 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
| 5165 | CK_UserDefinedConversion, Result.get(), 0, |
| 5166 | Result.get()->getValueKind()); |
| 5167 | return false; |
| 5168 | } |
| 5169 | |
| 5170 | static ExprResult finishContextualImplicitConversion( |
| 5171 | Sema &SemaRef, SourceLocation Loc, Expr *From, |
| 5172 | Sema::ContextualImplicitConverter &Converter) { |
| 5173 | if (!Converter.match(From->getType()) && !Converter.Suppress) |
| 5174 | Converter.diagnoseNoMatch(SemaRef, Loc, From->getType()) |
| 5175 | << From->getSourceRange(); |
| 5176 | |
| 5177 | return SemaRef.DefaultLvalueConversion(From); |
| 5178 | } |
| 5179 | |
| 5180 | static void |
| 5181 | collectViableConversionCandidates(Sema &SemaRef, Expr *From, QualType ToType, |
| 5182 | UnresolvedSetImpl &ViableConversions, |
| 5183 | OverloadCandidateSet &CandidateSet) { |
| 5184 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5185 | DeclAccessPair FoundDecl = ViableConversions[I]; |
| 5186 | NamedDecl *D = FoundDecl.getDecl(); |
| 5187 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 5188 | if (isa<UsingShadowDecl>(D)) |
| 5189 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 5190 | |
| 5191 | CXXConversionDecl *Conv; |
| 5192 | FunctionTemplateDecl *ConvTemplate; |
| 5193 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 5194 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 5195 | else |
| 5196 | Conv = cast<CXXConversionDecl>(D); |
| 5197 | |
| 5198 | if (ConvTemplate) |
| 5199 | SemaRef.AddTemplateConversionCandidate( |
| 5200 | ConvTemplate, FoundDecl, ActingContext, From, ToType, CandidateSet); |
| 5201 | else |
| 5202 | SemaRef.AddConversionCandidate(Conv, FoundDecl, ActingContext, From, |
| 5203 | ToType, CandidateSet); |
| 5204 | } |
| 5205 | } |
| 5206 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5207 | /// \brief Attempt to convert the given expression to a type which is accepted |
| 5208 | /// by the given converter. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5209 | /// |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5210 | /// This routine will attempt to convert an expression of class type to a |
| 5211 | /// type accepted by the specified converter. In C++11 and before, the class |
| 5212 | /// must have a single non-explicit conversion function converting to a matching |
| 5213 | /// type. In C++1y, there can be multiple such conversion functions, but only |
| 5214 | /// one target type. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5215 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5216 | /// \param Loc The source location of the construct that requires the |
| 5217 | /// conversion. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5218 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 5219 | /// \param From The expression we're converting from. |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5220 | /// |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5221 | /// \param Converter Used to control and diagnose the conversion process. |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5222 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5223 | /// \returns The expression, converted to an integral or enumeration type if |
| 5224 | /// successful. |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5225 | ExprResult Sema::PerformContextualImplicitConversion( |
| 5226 | SourceLocation Loc, Expr *From, ContextualImplicitConverter &Converter) { |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5227 | // We can't perform any more checking for type-dependent expressions. |
| 5228 | if (From->isTypeDependent()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5229 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5230 | |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5231 | // Process placeholders immediately. |
| 5232 | if (From->hasPlaceholderType()) { |
| 5233 | ExprResult result = CheckPlaceholderExpr(From); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5234 | if (result.isInvalid()) |
| 5235 | return result; |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5236 | From = result.take(); |
| 5237 | } |
| 5238 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5239 | // If the expression already has a matching type, we're golden. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5240 | QualType T = From->getType(); |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5241 | if (Converter.match(T)) |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5242 | return DefaultLvalueConversion(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5243 | |
| 5244 | // FIXME: Check for missing '()' if T is a function type? |
| 5245 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5246 | // We can only perform contextual implicit conversions on objects of class |
| 5247 | // type. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5248 | const RecordType *RecordTy = T->getAs<RecordType>(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5249 | if (!RecordTy || !getLangOpts().CPlusPlus) { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5250 | if (!Converter.Suppress) |
| 5251 | Converter.diagnoseNoMatch(*this, Loc, T) << From->getSourceRange(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5252 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5253 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5254 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5255 | // We must have a complete class type. |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 5256 | struct TypeDiagnoserPartialDiag : TypeDiagnoser { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5257 | ContextualImplicitConverter &Converter; |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5258 | Expr *From; |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5259 | |
| 5260 | TypeDiagnoserPartialDiag(ContextualImplicitConverter &Converter, Expr *From) |
| 5261 | : TypeDiagnoser(Converter.Suppress), Converter(Converter), From(From) {} |
| 5262 | |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5263 | virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5264 | Converter.diagnoseIncomplete(S, Loc, T) << From->getSourceRange(); |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5265 | } |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5266 | } IncompleteDiagnoser(Converter, From); |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5267 | |
| 5268 | if (RequireCompleteType(Loc, T, IncompleteDiagnoser)) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5269 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5270 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5271 | // Look for a conversion to an integral or enumeration type. |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5272 | UnresolvedSet<4> |
| 5273 | ViableConversions; // These are *potentially* viable in C++1y. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5274 | UnresolvedSet<4> ExplicitConversions; |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 5275 | std::pair<CXXRecordDecl::conversion_iterator, |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5276 | CXXRecordDecl::conversion_iterator> Conversions = |
| 5277 | cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5278 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5279 | bool HadMultipleCandidates = |
| 5280 | (std::distance(Conversions.first, Conversions.second) > 1); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5281 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5282 | // To check that there is only one target type, in C++1y: |
| 5283 | QualType ToType; |
| 5284 | bool HasUniqueTargetType = true; |
| 5285 | |
| 5286 | // Collect explicit or viable (potentially in C++1y) conversions. |
| 5287 | for (CXXRecordDecl::conversion_iterator I = Conversions.first, |
| 5288 | E = Conversions.second; |
| 5289 | I != E; ++I) { |
| 5290 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5291 | CXXConversionDecl *Conversion; |
| 5292 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 5293 | if (ConvTemplate) { |
| 5294 | if (getLangOpts().CPlusPlus1y) |
| 5295 | Conversion = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 5296 | else |
| 5297 | continue; // C++11 does not consider conversion operator templates(?). |
| 5298 | } else |
| 5299 | Conversion = cast<CXXConversionDecl>(D); |
| 5300 | |
| 5301 | assert((!ConvTemplate || getLangOpts().CPlusPlus1y) && |
| 5302 | "Conversion operator templates are considered potentially " |
| 5303 | "viable in C++1y"); |
| 5304 | |
| 5305 | QualType CurToType = Conversion->getConversionType().getNonReferenceType(); |
| 5306 | if (Converter.match(CurToType) || ConvTemplate) { |
| 5307 | |
| 5308 | if (Conversion->isExplicit()) { |
| 5309 | // FIXME: For C++1y, do we need this restriction? |
| 5310 | // cf. diagnoseNoViableConversion() |
| 5311 | if (!ConvTemplate) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5312 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5313 | } else { |
| 5314 | if (!ConvTemplate && getLangOpts().CPlusPlus1y) { |
| 5315 | if (ToType.isNull()) |
| 5316 | ToType = CurToType.getUnqualifiedType(); |
| 5317 | else if (HasUniqueTargetType && |
| 5318 | (CurToType.getUnqualifiedType() != ToType)) |
| 5319 | HasUniqueTargetType = false; |
| 5320 | } |
| 5321 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5322 | } |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5323 | } |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5324 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5325 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5326 | if (getLangOpts().CPlusPlus1y) { |
| 5327 | // C++1y [conv]p6: |
| 5328 | // ... An expression e of class type E appearing in such a context |
| 5329 | // is said to be contextually implicitly converted to a specified |
| 5330 | // type T and is well-formed if and only if e can be implicitly |
| 5331 | // 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] | 5332 | // for conversion functions whose return type is cv T or reference to |
| 5333 | // 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] | 5334 | // exactly one such T. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5335 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5336 | // If no unique T is found: |
| 5337 | if (ToType.isNull()) { |
| 5338 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5339 | HadMultipleCandidates, |
| 5340 | ExplicitConversions)) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5341 | return ExprError(); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5342 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5343 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5344 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5345 | // If more than one unique Ts are found: |
| 5346 | if (!HasUniqueTargetType) |
| 5347 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5348 | ViableConversions); |
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 | // If one unique T is found: |
| 5351 | // First, build a candidate set from the previously recorded |
| 5352 | // potentially viable conversions. |
| 5353 | OverloadCandidateSet CandidateSet(Loc); |
| 5354 | collectViableConversionCandidates(*this, From, ToType, ViableConversions, |
| 5355 | CandidateSet); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5356 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5357 | // Then, perform overload resolution over the candidate set. |
| 5358 | OverloadCandidateSet::iterator Best; |
| 5359 | switch (CandidateSet.BestViableFunction(*this, Loc, Best)) { |
| 5360 | case OR_Success: { |
| 5361 | // Apply this conversion. |
| 5362 | DeclAccessPair Found = |
| 5363 | DeclAccessPair::make(Best->Function, Best->FoundDecl.getAccess()); |
| 5364 | if (recordConversion(*this, Loc, From, Converter, T, |
| 5365 | HadMultipleCandidates, Found)) |
| 5366 | return ExprError(); |
| 5367 | break; |
| 5368 | } |
| 5369 | case OR_Ambiguous: |
| 5370 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5371 | ViableConversions); |
| 5372 | case OR_No_Viable_Function: |
| 5373 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5374 | HadMultipleCandidates, |
| 5375 | ExplicitConversions)) |
| 5376 | return ExprError(); |
| 5377 | // fall through 'OR_Deleted' case. |
| 5378 | case OR_Deleted: |
| 5379 | // We'll complain below about a non-integral condition type. |
| 5380 | break; |
| 5381 | } |
| 5382 | } else { |
| 5383 | switch (ViableConversions.size()) { |
| 5384 | case 0: { |
| 5385 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5386 | HadMultipleCandidates, |
| 5387 | ExplicitConversions)) |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5388 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5389 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5390 | // We'll complain below about a non-integral condition type. |
| 5391 | break; |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5392 | } |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5393 | case 1: { |
| 5394 | // Apply this conversion. |
| 5395 | DeclAccessPair Found = ViableConversions[0]; |
| 5396 | if (recordConversion(*this, Loc, From, Converter, T, |
| 5397 | HadMultipleCandidates, Found)) |
| 5398 | return ExprError(); |
| 5399 | break; |
| 5400 | } |
| 5401 | default: |
| 5402 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5403 | ViableConversions); |
| 5404 | } |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5405 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5406 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5407 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5408 | } |
| 5409 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5410 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 5411 | /// candidate functions, using the given function call arguments. If |
| 5412 | /// @p SuppressUserConversions, then don't allow user-defined |
| 5413 | /// conversions via constructors or conversion operators. |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5414 | /// |
James Dennett | 699c904 | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 5415 | /// \param PartialOverloading true if we are performing "partial" overloading |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5416 | /// based on an incomplete set of function arguments. This feature is used by |
| 5417 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5418 | void |
| 5419 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5420 | DeclAccessPair FoundDecl, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5421 | ArrayRef<Expr *> Args, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 5422 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 5423 | bool SuppressUserConversions, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5424 | bool PartialOverloading, |
| 5425 | bool AllowExplicit) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5426 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5427 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5428 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5429 | assert(!Function->getDescribedFunctionTemplate() && |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5430 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5431 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5432 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5433 | if (!isa<CXXConstructorDecl>(Method)) { |
| 5434 | // If we get here, it's because we're calling a member function |
| 5435 | // that is named without a member access expression (e.g., |
| 5436 | // "this->f") that was either written explicitly or created |
| 5437 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5438 | // function, e.g., X::f(). We use an empty type for the implied |
| 5439 | // object argument (C++ [over.call.func]p3), and the acting context |
| 5440 | // is irrelevant. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5441 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5442 | QualType(), Expr::Classification::makeSimpleLValue(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5443 | Args, CandidateSet, SuppressUserConversions); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5444 | return; |
| 5445 | } |
| 5446 | // We treat a constructor like a non-member function, since its object |
| 5447 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5448 | } |
| 5449 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 5450 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5451 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5452 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5453 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5454 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5455 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5456 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 5457 | // C++ [class.copy]p3: |
| 5458 | // A member function template is never instantiated to perform the copy |
| 5459 | // of a class object to an object of its class type. |
| 5460 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5461 | if (Args.size() == 1 && |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 5462 | Constructor->isSpecializationCopyingObject() && |
Douglas Gregor | 1211606 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 5463 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 5464 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5465 | return; |
| 5466 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5467 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5468 | // Add this candidate |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5469 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5470 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5471 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5472 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5473 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5474 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5475 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5476 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5477 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5478 | |
| 5479 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5480 | // parameters is viable only if it has an ellipsis in its parameter |
| 5481 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5482 | if ((Args.size() + (PartialOverloading && Args.size())) > NumArgsInProto && |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 5483 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5484 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5485 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5486 | return; |
| 5487 | } |
| 5488 | |
| 5489 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5490 | // is viable only if the (m+1)st parameter has a default argument |
| 5491 | // (8.3.6). For the purposes of overload resolution, the |
| 5492 | // parameter list is truncated on the right, so that there are |
| 5493 | // exactly m parameters. |
| 5494 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5495 | if (Args.size() < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5496 | // Not enough arguments. |
| 5497 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5498 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5499 | return; |
| 5500 | } |
| 5501 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5502 | // (CUDA B.1): Check for invalid calls between targets. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5503 | if (getLangOpts().CUDA) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5504 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
| 5505 | if (CheckCUDATarget(Caller, Function)) { |
| 5506 | Candidate.Viable = false; |
| 5507 | Candidate.FailureKind = ovl_fail_bad_target; |
| 5508 | return; |
| 5509 | } |
| 5510 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5511 | // Determine the implicit conversion sequences for each of the |
| 5512 | // arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5513 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5514 | if (ArgIdx < NumArgsInProto) { |
| 5515 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5516 | // exist for each argument an implicit conversion sequence |
| 5517 | // (13.3.3.1) that converts that argument to the corresponding |
| 5518 | // parameter of F. |
| 5519 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5520 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5521 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5522 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5523 | /*InOverloadResolution=*/true, |
| 5524 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5525 | getLangOpts().ObjCAutoRefCount, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5526 | AllowExplicit); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5527 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 5528 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5529 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5530 | break; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5531 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5532 | } else { |
| 5533 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5534 | // argument for which there is no corresponding parameter is |
| 5535 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5536 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5537 | } |
| 5538 | } |
| 5539 | } |
| 5540 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5541 | /// \brief Add all of the function declarations in the given function set to |
| 5542 | /// the overload canddiate set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5543 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5544 | ArrayRef<Expr *> Args, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5545 | OverloadCandidateSet& CandidateSet, |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5546 | bool SuppressUserConversions, |
| 5547 | TemplateArgumentListInfo *ExplicitTemplateArgs) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5548 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5549 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 5550 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5551 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5552 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5553 | cast<CXXMethodDecl>(FD)->getParent(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5554 | Args[0]->getType(), Args[0]->Classify(Context), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5555 | Args.slice(1), CandidateSet, |
| 5556 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5557 | else |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5558 | AddOverloadCandidate(FD, F.getPair(), Args, CandidateSet, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5559 | SuppressUserConversions); |
| 5560 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5561 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5562 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 5563 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5564 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5565 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5566 | ExplicitTemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5567 | Args[0]->getType(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5568 | Args[0]->Classify(Context), Args.slice(1), |
| 5569 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5570 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5571 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5572 | ExplicitTemplateArgs, Args, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5573 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5574 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5575 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5576 | } |
| 5577 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5578 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 5579 | /// method) as a method candidate to the given overload set. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5580 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5581 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5582 | Expr::Classification ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5583 | ArrayRef<Expr *> Args, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5584 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5585 | bool SuppressUserConversions) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5586 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5587 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5588 | |
| 5589 | if (isa<UsingShadowDecl>(Decl)) |
| 5590 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5591 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5592 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 5593 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 5594 | "Expected a member function template"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5595 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 5596 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5597 | ObjectType, ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5598 | Args, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5599 | SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5600 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5601 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5602 | ObjectType, ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5603 | Args, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5604 | CandidateSet, SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5605 | } |
| 5606 | } |
| 5607 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5608 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 5609 | /// of candidate functions, using the given function call arguments |
| 5610 | /// and the object argument (@c Object). For example, in a call |
| 5611 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 5612 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 5613 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5614 | /// operators. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5615 | void |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5616 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5617 | CXXRecordDecl *ActingContext, QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5618 | Expr::Classification ObjectClassification, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5619 | ArrayRef<Expr *> Args, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5620 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5621 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5622 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5623 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5624 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5625 | assert(!isa<CXXConstructorDecl>(Method) && |
| 5626 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5627 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5628 | if (!CandidateSet.isNewCandidate(Method)) |
| 5629 | return; |
| 5630 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5631 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5632 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5633 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5634 | // Add this candidate |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5635 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5636 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5637 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5638 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5639 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5640 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5641 | |
| 5642 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5643 | |
| 5644 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5645 | // parameters is viable only if it has an ellipsis in its parameter |
| 5646 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5647 | if (Args.size() > NumArgsInProto && !Proto->isVariadic()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5648 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5649 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5650 | return; |
| 5651 | } |
| 5652 | |
| 5653 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5654 | // is viable only if the (m+1)st parameter has a default argument |
| 5655 | // (8.3.6). For the purposes of overload resolution, the |
| 5656 | // parameter list is truncated on the right, so that there are |
| 5657 | // exactly m parameters. |
| 5658 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5659 | if (Args.size() < MinRequiredArgs) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5660 | // Not enough arguments. |
| 5661 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5662 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5663 | return; |
| 5664 | } |
| 5665 | |
| 5666 | Candidate.Viable = true; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5667 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5668 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5669 | // The implicit object argument is ignored. |
| 5670 | Candidate.IgnoreObjectArgument = true; |
| 5671 | else { |
| 5672 | // Determine the implicit conversion sequence for the object |
| 5673 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5674 | Candidate.Conversions[0] |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5675 | = TryObjectArgumentInitialization(*this, ObjectType, ObjectClassification, |
| 5676 | Method, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5677 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5678 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5679 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5680 | return; |
| 5681 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5682 | } |
| 5683 | |
| 5684 | // Determine the implicit conversion sequences for each of the |
| 5685 | // arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5686 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5687 | if (ArgIdx < NumArgsInProto) { |
| 5688 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5689 | // exist for each argument an implicit conversion sequence |
| 5690 | // (13.3.3.1) that converts that argument to the corresponding |
| 5691 | // parameter of F. |
| 5692 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5693 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5694 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5695 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5696 | /*InOverloadResolution=*/true, |
| 5697 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5698 | getLangOpts().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5699 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5700 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5701 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5702 | break; |
| 5703 | } |
| 5704 | } else { |
| 5705 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5706 | // argument for which there is no corresponding parameter is |
| 5707 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5708 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5709 | } |
| 5710 | } |
| 5711 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5712 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5713 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 5714 | /// set, using template argument deduction to produce an appropriate member |
| 5715 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5716 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5717 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5718 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5719 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5720 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5721 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5722 | Expr::Classification ObjectClassification, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5723 | ArrayRef<Expr *> Args, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5724 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5725 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5726 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 5727 | return; |
| 5728 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5729 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5730 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5731 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5732 | // 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] | 5733 | // candidate functions in the usual way.113) A given name can refer to one |
| 5734 | // or more function templates and also to a set of overloaded non-template |
| 5735 | // functions. In such a case, the candidate functions generated from each |
| 5736 | // function template are combined with the set of non-template candidate |
| 5737 | // functions. |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5738 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5739 | FunctionDecl *Specialization = 0; |
| 5740 | if (TemplateDeductionResult Result |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5741 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, Args, |
| 5742 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5743 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5744 | Candidate.FoundDecl = FoundDecl; |
| 5745 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 5746 | Candidate.Viable = false; |
| 5747 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 5748 | Candidate.IsSurrogate = false; |
| 5749 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5750 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5751 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5752 | Info); |
| 5753 | return; |
| 5754 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5755 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5756 | // Add the function template specialization produced by template argument |
| 5757 | // deduction as a candidate. |
| 5758 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5759 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5760 | "Specialization is not a member function?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5761 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5762 | ActingContext, ObjectType, ObjectClassification, Args, |
| 5763 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5764 | } |
| 5765 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5766 | /// \brief Add a C++ function template specialization as a candidate |
| 5767 | /// in the candidate set, using template argument deduction to produce |
| 5768 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5769 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5770 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5771 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5772 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5773 | ArrayRef<Expr *> Args, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5774 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5775 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5776 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 5777 | return; |
| 5778 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5779 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5780 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5781 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5782 | // 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] | 5783 | // candidate functions in the usual way.113) A given name can refer to one |
| 5784 | // or more function templates and also to a set of overloaded non-template |
| 5785 | // functions. In such a case, the candidate functions generated from each |
| 5786 | // function template are combined with the set of non-template candidate |
| 5787 | // functions. |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5788 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5789 | FunctionDecl *Specialization = 0; |
| 5790 | if (TemplateDeductionResult Result |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5791 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, Args, |
| 5792 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5793 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5794 | Candidate.FoundDecl = FoundDecl; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5795 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 5796 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5797 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5798 | Candidate.IsSurrogate = false; |
| 5799 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5800 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5801 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5802 | Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5803 | return; |
| 5804 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5805 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5806 | // Add the function template specialization produced by template argument |
| 5807 | // deduction as a candidate. |
| 5808 | assert(Specialization && "Missing function template specialization?"); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5809 | AddOverloadCandidate(Specialization, FoundDecl, Args, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5810 | SuppressUserConversions); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5811 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5812 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5813 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5814 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5815 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5816 | /// 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] | 5817 | /// (which may or may not be the same type as the type that the |
| 5818 | /// conversion function produces). |
| 5819 | void |
| 5820 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5821 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5822 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5823 | Expr *From, QualType ToType, |
| 5824 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5825 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 5826 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5827 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5828 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 5829 | return; |
| 5830 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 5831 | // If the conversion function has an undeduced return type, trigger its |
| 5832 | // deduction now. |
| 5833 | if (getLangOpts().CPlusPlus1y && ConvType->isUndeducedType()) { |
| 5834 | if (DeduceReturnType(Conversion, From->getExprLoc())) |
| 5835 | return; |
| 5836 | ConvType = Conversion->getConversionType().getNonReferenceType(); |
| 5837 | } |
| 5838 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5839 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5840 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5841 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5842 | // Add this candidate |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5843 | OverloadCandidate &Candidate = CandidateSet.addCandidate(1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5844 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5845 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5846 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5847 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5848 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5849 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 5850 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5851 | Candidate.Viable = true; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5852 | Candidate.ExplicitCallArguments = 1; |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5853 | |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 5854 | // C++ [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5855 | // For conversion functions, the function is considered to be a member of |
| 5856 | // the class of the implicit implied object argument for the purpose of |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 5857 | // defining the type of the implicit object parameter. |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5858 | // |
| 5859 | // Determine the implicit conversion sequence for the implicit |
| 5860 | // object parameter. |
| 5861 | QualType ImplicitParamType = From->getType(); |
| 5862 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
| 5863 | ImplicitParamType = FromPtrType->getPointeeType(); |
| 5864 | CXXRecordDecl *ConversionContext |
| 5865 | = cast<CXXRecordDecl>(ImplicitParamType->getAs<RecordType>()->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5866 | |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5867 | Candidate.Conversions[0] |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5868 | = TryObjectArgumentInitialization(*this, From->getType(), |
| 5869 | From->Classify(Context), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5870 | Conversion, ConversionContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5871 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5872 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5873 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5874 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5875 | return; |
| 5876 | } |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5877 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5878 | // 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] | 5879 | // derived to base as such conversions are given Conversion Rank. They only |
| 5880 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 5881 | QualType FromCanon |
| 5882 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 5883 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 5884 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 5885 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5886 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 5887 | return; |
| 5888 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5889 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5890 | // To determine what the conversion from the result of calling the |
| 5891 | // conversion function to the type we're eventually trying to |
| 5892 | // convert to (ToType), we need to synthesize a call to the |
| 5893 | // conversion function and attempt copy initialization from it. This |
| 5894 | // makes sure that we get the right semantics with respect to |
| 5895 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 5896 | // call on the stack and we don't need its arguments to be |
| 5897 | // well-formed. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5898 | DeclRefExpr ConversionRef(Conversion, false, Conversion->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5899 | VK_LValue, From->getLocStart()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5900 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 5901 | Context.getPointerType(Conversion->getType()), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5902 | CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5903 | &ConversionRef, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5904 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5905 | QualType ConversionType = Conversion->getConversionType(); |
| 5906 | if (RequireCompleteType(From->getLocStart(), ConversionType, 0)) { |
Douglas Gregor | 7d14d38 | 2010-11-13 19:36:57 +0000 | [diff] [blame] | 5907 | Candidate.Viable = false; |
| 5908 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 5909 | return; |
| 5910 | } |
| 5911 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5912 | ExprValueKind VK = Expr::getValueKindForType(ConversionType); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5913 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5914 | // 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] | 5915 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 5916 | // allocator). |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5917 | QualType CallResultType = ConversionType.getNonLValueExprType(Context); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 5918 | CallExpr Call(Context, &ConversionFn, None, CallResultType, VK, |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 5919 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5920 | ImplicitConversionSequence ICS = |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5921 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 5922 | /*SuppressUserConversions=*/true, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5923 | /*InOverloadResolution=*/false, |
| 5924 | /*AllowObjCWritebackConversion=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5925 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5926 | switch (ICS.getKind()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5927 | case ImplicitConversionSequence::StandardConversion: |
| 5928 | Candidate.FinalConversion = ICS.Standard; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5929 | |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5930 | // C++ [over.ics.user]p3: |
| 5931 | // If the user-defined conversion is specified by a specialization of a |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5932 | // conversion function template, the second standard conversion sequence |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5933 | // shall have exact match rank. |
| 5934 | if (Conversion->getPrimaryTemplate() && |
| 5935 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 5936 | Candidate.Viable = false; |
| 5937 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 5938 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5939 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 5940 | // C++0x [dcl.init.ref]p5: |
| 5941 | // In the second case, if the reference is an rvalue reference and |
| 5942 | // the second standard conversion sequence of the user-defined |
| 5943 | // conversion sequence includes an lvalue-to-rvalue conversion, the |
| 5944 | // program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5945 | if (ToType->isRValueReferenceType() && |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 5946 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 5947 | Candidate.Viable = false; |
| 5948 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 5949 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5950 | break; |
| 5951 | |
| 5952 | case ImplicitConversionSequence::BadConversion: |
| 5953 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5954 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5955 | break; |
| 5956 | |
| 5957 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5958 | llvm_unreachable( |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5959 | "Can only end up with a standard conversion sequence or failure"); |
| 5960 | } |
| 5961 | } |
| 5962 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5963 | /// \brief Adds a conversion function template specialization |
| 5964 | /// candidate to the overload set, using template argument deduction |
| 5965 | /// to deduce the template arguments of the conversion function |
| 5966 | /// template from the type that we are converting to (C++ |
| 5967 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5968 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5969 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5970 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5971 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5972 | Expr *From, QualType ToType, |
| 5973 | OverloadCandidateSet &CandidateSet) { |
| 5974 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 5975 | "Only conversion function templates permitted here"); |
| 5976 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5977 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 5978 | return; |
| 5979 | |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5980 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5981 | CXXConversionDecl *Specialization = 0; |
| 5982 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5983 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5984 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5985 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5986 | Candidate.FoundDecl = FoundDecl; |
| 5987 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 5988 | Candidate.Viable = false; |
| 5989 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 5990 | Candidate.IsSurrogate = false; |
| 5991 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5992 | Candidate.ExplicitCallArguments = 1; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5993 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5994 | Info); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5995 | return; |
| 5996 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5997 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5998 | // Add the conversion function template specialization produced by |
| 5999 | // template argument deduction as a candidate. |
| 6000 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6001 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 6002 | CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6003 | } |
| 6004 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6005 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 6006 | /// converts the given @c Object to a function pointer via the |
| 6007 | /// conversion function @c Conversion, and then attempts to call it |
| 6008 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 6009 | /// the type of function that we'll eventually be calling. |
| 6010 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6011 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6012 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6013 | const FunctionProtoType *Proto, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6014 | Expr *Object, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6015 | ArrayRef<Expr *> Args, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6016 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6017 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 6018 | return; |
| 6019 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6020 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6021 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6022 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6023 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6024 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6025 | Candidate.Function = 0; |
| 6026 | Candidate.Surrogate = Conversion; |
| 6027 | Candidate.Viable = true; |
| 6028 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6029 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6030 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6031 | |
| 6032 | // Determine the implicit conversion sequence for the implicit |
| 6033 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6034 | ImplicitConversionSequence ObjectInit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6035 | = TryObjectArgumentInitialization(*this, Object->getType(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6036 | Object->Classify(Context), |
| 6037 | Conversion, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6038 | if (ObjectInit.isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6039 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6040 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6041 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6042 | return; |
| 6043 | } |
| 6044 | |
| 6045 | // The first conversion is actually a user-defined conversion whose |
| 6046 | // first conversion is ObjectInit's standard conversion (which is |
| 6047 | // effectively a reference binding). Record it as such. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6048 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6049 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 6050 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6051 | Candidate.Conversions[0].UserDefined.HadMultipleCandidates = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6052 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 6053 | Candidate.Conversions[0].UserDefined.FoundConversionFunction = FoundDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6054 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6055 | = Candidate.Conversions[0].UserDefined.Before; |
| 6056 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 6057 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6058 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6059 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 6060 | |
| 6061 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 6062 | // parameters is viable only if it has an ellipsis in its parameter |
| 6063 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6064 | if (Args.size() > NumArgsInProto && !Proto->isVariadic()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6065 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6066 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6067 | return; |
| 6068 | } |
| 6069 | |
| 6070 | // Function types don't have any default arguments, so just check if |
| 6071 | // we have enough arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6072 | if (Args.size() < NumArgsInProto) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6073 | // Not enough arguments. |
| 6074 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6075 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6076 | return; |
| 6077 | } |
| 6078 | |
| 6079 | // Determine the implicit conversion sequences for each of the |
| 6080 | // arguments. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6081 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6082 | if (ArgIdx < NumArgsInProto) { |
| 6083 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 6084 | // exist for each argument an implicit conversion sequence |
| 6085 | // (13.3.3.1) that converts that argument to the corresponding |
| 6086 | // parameter of F. |
| 6087 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6088 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6089 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6090 | /*SuppressUserConversions=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6091 | /*InOverloadResolution=*/false, |
| 6092 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6093 | getLangOpts().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6094 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6095 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6096 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6097 | break; |
| 6098 | } |
| 6099 | } else { |
| 6100 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 6101 | // argument for which there is no corresponding parameter is |
| 6102 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6103 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6104 | } |
| 6105 | } |
| 6106 | } |
| 6107 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6108 | /// \brief Add overload candidates for overloaded operators that are |
| 6109 | /// member functions. |
| 6110 | /// |
| 6111 | /// Add the overloaded operator candidates that are member functions |
| 6112 | /// for the operator Op that was used in an operator expression such |
| 6113 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 6114 | /// CandidateSet will store the added overload candidates. (C++ |
| 6115 | /// [over.match.oper]). |
| 6116 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 6117 | SourceLocation OpLoc, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6118 | ArrayRef<Expr *> Args, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6119 | OverloadCandidateSet& CandidateSet, |
| 6120 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6121 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 6122 | |
| 6123 | // C++ [over.match.oper]p3: |
| 6124 | // For a unary operator @ with an operand of a type whose |
| 6125 | // cv-unqualified version is T1, and for a binary operator @ with |
| 6126 | // a left operand of a type whose cv-unqualified version is T1 and |
| 6127 | // a right operand of a type whose cv-unqualified version is T2, |
| 6128 | // three sets of candidate functions, designated member |
| 6129 | // candidates, non-member candidates and built-in candidates, are |
| 6130 | // constructed as follows: |
| 6131 | QualType T1 = Args[0]->getType(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6132 | |
Richard Smith | e410be9 | 2013-04-20 12:41:22 +0000 | [diff] [blame] | 6133 | // -- If T1 is a complete class type or a class currently being |
| 6134 | // defined, the set of member candidates is the result of the |
| 6135 | // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, |
| 6136 | // the set of member candidates is empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6137 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Richard Smith | e410be9 | 2013-04-20 12:41:22 +0000 | [diff] [blame] | 6138 | // Complete the type if it can be completed. |
| 6139 | RequireCompleteType(OpLoc, T1, 0); |
| 6140 | // If the type is neither complete nor being defined, bail out now. |
| 6141 | if (!T1Rec->getDecl()->getDefinition()) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 6142 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6143 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6144 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 6145 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 6146 | Operators.suppressDiagnostics(); |
| 6147 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6148 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 6149 | OperEnd = Operators.end(); |
| 6150 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6151 | ++Oper) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6152 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 6153 | Args[0]->Classify(Context), |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6154 | Args.slice(1), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6155 | CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6156 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6157 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6158 | } |
| 6159 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6160 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 6161 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 6162 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6163 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 6164 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6165 | /// operator. NumContextualBoolArguments is the number of arguments |
| 6166 | /// (at the beginning of the argument list) that will be contextually |
| 6167 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6168 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6169 | ArrayRef<Expr *> Args, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6170 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6171 | bool IsAssignmentOperator, |
| 6172 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6173 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6174 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6175 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6176 | // Add this candidate |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6177 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6178 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6179 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 6180 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6181 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6182 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6183 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6184 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 6185 | |
| 6186 | // Determine the implicit conversion sequences for each of the |
| 6187 | // arguments. |
| 6188 | Candidate.Viable = true; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6189 | Candidate.ExplicitCallArguments = Args.size(); |
| 6190 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6191 | // C++ [over.match.oper]p4: |
| 6192 | // For the built-in assignment operators, conversions of the |
| 6193 | // left operand are restricted as follows: |
| 6194 | // -- no temporaries are introduced to hold the left operand, and |
| 6195 | // -- no user-defined conversions are applied to the left |
| 6196 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6197 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6198 | // |
| 6199 | // We block these conversions by turning off user-defined |
| 6200 | // conversions, since that is the only way that initialization of |
| 6201 | // a reference to a non-class type can occur from something that |
| 6202 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6203 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6204 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6205 | "Contextual conversion to bool requires bool type"); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6206 | Candidate.Conversions[ArgIdx] |
| 6207 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6208 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6209 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6210 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6211 | ArgIdx == 0 && IsAssignmentOperator, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6212 | /*InOverloadResolution=*/false, |
| 6213 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6214 | getLangOpts().ObjCAutoRefCount); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6215 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6216 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6217 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6218 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6219 | break; |
| 6220 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6221 | } |
| 6222 | } |
| 6223 | |
Craig Topper | fe09f3f | 2013-07-01 06:29:40 +0000 | [diff] [blame] | 6224 | namespace { |
| 6225 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6226 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 6227 | /// candidate operator functions for built-in operators (C++ |
| 6228 | /// [over.built]). The types are separated into pointer types and |
| 6229 | /// enumeration types. |
| 6230 | class BuiltinCandidateTypeSet { |
| 6231 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6232 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6233 | |
| 6234 | /// PointerTypes - The set of pointer types that will be used in the |
| 6235 | /// built-in candidates. |
| 6236 | TypeSet PointerTypes; |
| 6237 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6238 | /// MemberPointerTypes - The set of member pointer types that will be |
| 6239 | /// used in the built-in candidates. |
| 6240 | TypeSet MemberPointerTypes; |
| 6241 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6242 | /// EnumerationTypes - The set of enumeration types that will be |
| 6243 | /// used in the built-in candidates. |
| 6244 | TypeSet EnumerationTypes; |
| 6245 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6246 | /// \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] | 6247 | /// candidates. |
| 6248 | TypeSet VectorTypes; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6249 | |
| 6250 | /// \brief A flag indicating non-record types are viable candidates |
| 6251 | bool HasNonRecordTypes; |
| 6252 | |
| 6253 | /// \brief A flag indicating whether either arithmetic or enumeration types |
| 6254 | /// were present in the candidate set. |
| 6255 | bool HasArithmeticOrEnumeralTypes; |
| 6256 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6257 | /// \brief A flag indicating whether the nullptr type was present in the |
| 6258 | /// candidate set. |
| 6259 | bool HasNullPtrType; |
| 6260 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 6261 | /// Sema - The semantic analysis instance where we are building the |
| 6262 | /// candidate type set. |
| 6263 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6264 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6265 | /// Context - The AST context in which we will build the type sets. |
| 6266 | ASTContext &Context; |
| 6267 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6268 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6269 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6270 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6271 | |
| 6272 | public: |
| 6273 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6274 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6275 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6276 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6277 | : HasNonRecordTypes(false), |
| 6278 | HasArithmeticOrEnumeralTypes(false), |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6279 | HasNullPtrType(false), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6280 | SemaRef(SemaRef), |
| 6281 | Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6282 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6283 | void AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6284 | SourceLocation Loc, |
| 6285 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6286 | bool AllowExplicitConversions, |
| 6287 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6288 | |
| 6289 | /// pointer_begin - First pointer type found; |
| 6290 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 6291 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6292 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6293 | iterator pointer_end() { return PointerTypes.end(); } |
| 6294 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6295 | /// member_pointer_begin - First member pointer type found; |
| 6296 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 6297 | |
| 6298 | /// member_pointer_end - Past the last member pointer type found; |
| 6299 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 6300 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6301 | /// enumeration_begin - First enumeration type found; |
| 6302 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 6303 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6304 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6305 | iterator enumeration_end() { return EnumerationTypes.end(); } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6306 | |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6307 | iterator vector_begin() { return VectorTypes.begin(); } |
| 6308 | iterator vector_end() { return VectorTypes.end(); } |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6309 | |
| 6310 | bool hasNonRecordTypes() { return HasNonRecordTypes; } |
| 6311 | bool hasArithmeticOrEnumeralTypes() { return HasArithmeticOrEnumeralTypes; } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6312 | bool hasNullPtrType() const { return HasNullPtrType; } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6313 | }; |
| 6314 | |
Craig Topper | fe09f3f | 2013-07-01 06:29:40 +0000 | [diff] [blame] | 6315 | } // end anonymous namespace |
| 6316 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6317 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6318 | /// the set of pointer types along with any more-qualified variants of |
| 6319 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6320 | /// will add "int const *", "int const volatile *", "int const |
| 6321 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6322 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6323 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6324 | /// |
| 6325 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6326 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6327 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6328 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6329 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6330 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6331 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6332 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6333 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6334 | QualType PointeeTy; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6335 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6336 | bool buildObjCPtr = false; |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6337 | if (!PointerTy) { |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6338 | const ObjCObjectPointerType *PTy = Ty->castAs<ObjCObjectPointerType>(); |
| 6339 | PointeeTy = PTy->getPointeeType(); |
| 6340 | buildObjCPtr = true; |
| 6341 | } else { |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6342 | PointeeTy = PointerTy->getPointeeType(); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6343 | } |
| 6344 | |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6345 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6346 | // (the qualifier would sink to the element type), and for another, the |
| 6347 | // only overload situation where it matters is subscript or pointer +- int, |
| 6348 | // and those shouldn't have qualifier variants anyway. |
| 6349 | if (PointeeTy->isArrayType()) |
| 6350 | return true; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6351 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6352 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6353 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 6354 | bool hasRestrict = VisibleQuals.hasRestrict(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6355 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6356 | // Iterate through all strict supersets of BaseCVR. |
| 6357 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6358 | if ((CVR | BaseCVR) != CVR) continue; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6359 | // Skip over volatile if no volatile found anywhere in the types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6360 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6361 | |
| 6362 | // Skip over restrict if no restrict found anywhere in the types, or if |
| 6363 | // the type cannot be restrict-qualified. |
| 6364 | if ((CVR & Qualifiers::Restrict) && |
| 6365 | (!hasRestrict || |
| 6366 | (!(PointeeTy->isAnyPointerType() || PointeeTy->isReferenceType())))) |
| 6367 | continue; |
| 6368 | |
| 6369 | // Build qualified pointee type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6370 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6371 | |
| 6372 | // Build qualified pointer type. |
| 6373 | QualType QPointerTy; |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6374 | if (!buildObjCPtr) |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6375 | QPointerTy = Context.getPointerType(QPointeeTy); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6376 | else |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6377 | QPointerTy = Context.getObjCObjectPointerType(QPointeeTy); |
| 6378 | |
| 6379 | // Insert qualified pointer type. |
| 6380 | PointerTypes.insert(QPointerTy); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6381 | } |
| 6382 | |
| 6383 | return true; |
| 6384 | } |
| 6385 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6386 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 6387 | /// to the set of pointer types along with any more-qualified variants of |
| 6388 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6389 | /// will add "int const *", "int const volatile *", "int const |
| 6390 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6391 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6392 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6393 | /// |
| 6394 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6395 | bool |
| 6396 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 6397 | QualType Ty) { |
| 6398 | // Insert this type. |
| 6399 | if (!MemberPointerTypes.insert(Ty)) |
| 6400 | return false; |
| 6401 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6402 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 6403 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6404 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6405 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6406 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6407 | // (the qualifier would sink to the element type), and for another, the |
| 6408 | // only overload situation where it matters is subscript or pointer +- int, |
| 6409 | // and those shouldn't have qualifier variants anyway. |
| 6410 | if (PointeeTy->isArrayType()) |
| 6411 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6412 | const Type *ClassTy = PointerTy->getClass(); |
| 6413 | |
| 6414 | // Iterate through all strict supersets of the pointee type's CVR |
| 6415 | // qualifiers. |
| 6416 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 6417 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6418 | if ((CVR | BaseCVR) != CVR) continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6419 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6420 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6421 | MemberPointerTypes.insert( |
| 6422 | Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6423 | } |
| 6424 | |
| 6425 | return true; |
| 6426 | } |
| 6427 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6428 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 6429 | /// 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] | 6430 | /// primarily interested in pointer types and enumeration types. We also |
| 6431 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6432 | /// AllowUserConversions is true if we should look at the conversion |
| 6433 | /// functions of a class type, and AllowExplicitConversions if we |
| 6434 | /// should also include the explicit conversion functions of a class |
| 6435 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6436 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6437 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6438 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6439 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6440 | bool AllowExplicitConversions, |
| 6441 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6442 | // Only deal with canonical types. |
| 6443 | Ty = Context.getCanonicalType(Ty); |
| 6444 | |
| 6445 | // Look through reference types; they aren't part of the type of an |
| 6446 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6447 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6448 | Ty = RefTy->getPointeeType(); |
| 6449 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 6450 | // If we're dealing with an array type, decay to the pointer. |
| 6451 | if (Ty->isArrayType()) |
| 6452 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 6453 | |
| 6454 | // Otherwise, we don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 6455 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6456 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6457 | // Flag if we ever add a non-record type. |
| 6458 | const RecordType *TyRec = Ty->getAs<RecordType>(); |
| 6459 | HasNonRecordTypes = HasNonRecordTypes || !TyRec; |
| 6460 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6461 | // Flag if we encounter an arithmetic type. |
| 6462 | HasArithmeticOrEnumeralTypes = |
| 6463 | HasArithmeticOrEnumeralTypes || Ty->isArithmeticType(); |
| 6464 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6465 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
| 6466 | PointerTypes.insert(Ty); |
| 6467 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6468 | // Insert our type, and its more-qualified variants, into the set |
| 6469 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6470 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6471 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6472 | } else if (Ty->isMemberPointerType()) { |
| 6473 | // Member pointers are far easier, since the pointee can't be converted. |
| 6474 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 6475 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6476 | } else if (Ty->isEnumeralType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6477 | HasArithmeticOrEnumeralTypes = true; |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6478 | EnumerationTypes.insert(Ty); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6479 | } else if (Ty->isVectorType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6480 | // We treat vector types as arithmetic types in many contexts as an |
| 6481 | // extension. |
| 6482 | HasArithmeticOrEnumeralTypes = true; |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6483 | VectorTypes.insert(Ty); |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6484 | } else if (Ty->isNullPtrType()) { |
| 6485 | HasNullPtrType = true; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6486 | } else if (AllowUserConversions && TyRec) { |
| 6487 | // No conversion functions in incomplete types. |
| 6488 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) |
| 6489 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6490 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6491 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6492 | std::pair<CXXRecordDecl::conversion_iterator, |
| 6493 | CXXRecordDecl::conversion_iterator> |
| 6494 | Conversions = ClassDecl->getVisibleConversionFunctions(); |
| 6495 | for (CXXRecordDecl::conversion_iterator |
| 6496 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6497 | NamedDecl *D = I.getDecl(); |
| 6498 | if (isa<UsingShadowDecl>(D)) |
| 6499 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6500 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6501 | // Skip conversion function templates; they don't tell us anything |
| 6502 | // about which builtin types we can convert to. |
| 6503 | if (isa<FunctionTemplateDecl>(D)) |
| 6504 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6505 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6506 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
| 6507 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
| 6508 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
| 6509 | VisibleQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6510 | } |
| 6511 | } |
| 6512 | } |
| 6513 | } |
| 6514 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6515 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 6516 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 6517 | /// given type to the candidate set. |
| 6518 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 6519 | QualType T, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6520 | ArrayRef<Expr *> Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6521 | OverloadCandidateSet &CandidateSet) { |
| 6522 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6523 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6524 | // T& operator=(T&, T) |
| 6525 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 6526 | ParamTypes[1] = T; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6527 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6528 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6529 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6530 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 6531 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6532 | ParamTypes[0] |
| 6533 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6534 | ParamTypes[1] = T; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6535 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6536 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6537 | } |
| 6538 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6539 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 6540 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 6541 | /// 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] | 6542 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 6543 | Qualifiers VRQuals; |
| 6544 | const RecordType *TyRec; |
| 6545 | if (const MemberPointerType *RHSMPType = |
| 6546 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6547 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6548 | else |
| 6549 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 6550 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6551 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6552 | VRQuals.addVolatile(); |
| 6553 | VRQuals.addRestrict(); |
| 6554 | return VRQuals; |
| 6555 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6556 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6557 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 6558 | if (!ClassDecl->hasDefinition()) |
| 6559 | return VRQuals; |
| 6560 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6561 | std::pair<CXXRecordDecl::conversion_iterator, |
| 6562 | CXXRecordDecl::conversion_iterator> |
| 6563 | Conversions = ClassDecl->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6564 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6565 | for (CXXRecordDecl::conversion_iterator |
| 6566 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 6567 | NamedDecl *D = I.getDecl(); |
| 6568 | if (isa<UsingShadowDecl>(D)) |
| 6569 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6570 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6571 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 6572 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 6573 | CanTy = ResTypeRef->getPointeeType(); |
| 6574 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 6575 | // as see them. |
| 6576 | bool done = false; |
| 6577 | while (!done) { |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6578 | if (CanTy.isRestrictQualified()) |
| 6579 | VRQuals.addRestrict(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6580 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 6581 | CanTy = ResTypePtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6582 | else if (const MemberPointerType *ResTypeMPtr = |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6583 | CanTy->getAs<MemberPointerType>()) |
| 6584 | CanTy = ResTypeMPtr->getPointeeType(); |
| 6585 | else |
| 6586 | done = true; |
| 6587 | if (CanTy.isVolatileQualified()) |
| 6588 | VRQuals.addVolatile(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6589 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 6590 | return VRQuals; |
| 6591 | } |
| 6592 | } |
| 6593 | } |
| 6594 | return VRQuals; |
| 6595 | } |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6596 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6597 | namespace { |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6598 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6599 | /// \brief Helper class to manage the addition of builtin operator overload |
| 6600 | /// candidates. It provides shared state and utility methods used throughout |
| 6601 | /// the process, as well as a helper method to add each group of builtin |
| 6602 | /// operator overloads from the standard to a candidate set. |
| 6603 | class BuiltinOperatorOverloadBuilder { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6604 | // Common instance state available to all overload candidate addition methods. |
| 6605 | Sema &S; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6606 | ArrayRef<Expr *> Args; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6607 | Qualifiers VisibleTypeConversionsQuals; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6608 | bool HasArithmeticOrEnumeralCandidateType; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6609 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6610 | OverloadCandidateSet &CandidateSet; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6611 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6612 | // Define some constants used to index and iterate over the arithemetic types |
| 6613 | // provided via the getArithmeticType() method below. |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6614 | // The "promoted arithmetic types" are the arithmetic |
| 6615 | // types are that preserved by promotion (C++ [over.built]p2). |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6616 | static const unsigned FirstIntegralType = 3; |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6617 | static const unsigned LastIntegralType = 20; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6618 | static const unsigned FirstPromotedIntegralType = 3, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6619 | LastPromotedIntegralType = 11; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6620 | static const unsigned FirstPromotedArithmeticType = 0, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6621 | LastPromotedArithmeticType = 11; |
| 6622 | static const unsigned NumArithmeticTypes = 20; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6623 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6624 | /// \brief Get the canonical type for a given arithmetic type index. |
| 6625 | CanQualType getArithmeticType(unsigned index) { |
| 6626 | assert(index < NumArithmeticTypes); |
| 6627 | static CanQualType ASTContext::* const |
| 6628 | ArithmeticTypes[NumArithmeticTypes] = { |
| 6629 | // Start of promoted types. |
| 6630 | &ASTContext::FloatTy, |
| 6631 | &ASTContext::DoubleTy, |
| 6632 | &ASTContext::LongDoubleTy, |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6633 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6634 | // Start of integral types. |
| 6635 | &ASTContext::IntTy, |
| 6636 | &ASTContext::LongTy, |
| 6637 | &ASTContext::LongLongTy, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6638 | &ASTContext::Int128Ty, |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6639 | &ASTContext::UnsignedIntTy, |
| 6640 | &ASTContext::UnsignedLongTy, |
| 6641 | &ASTContext::UnsignedLongLongTy, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6642 | &ASTContext::UnsignedInt128Ty, |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6643 | // End of promoted types. |
| 6644 | |
| 6645 | &ASTContext::BoolTy, |
| 6646 | &ASTContext::CharTy, |
| 6647 | &ASTContext::WCharTy, |
| 6648 | &ASTContext::Char16Ty, |
| 6649 | &ASTContext::Char32Ty, |
| 6650 | &ASTContext::SignedCharTy, |
| 6651 | &ASTContext::ShortTy, |
| 6652 | &ASTContext::UnsignedCharTy, |
| 6653 | &ASTContext::UnsignedShortTy, |
| 6654 | // End of integral types. |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6655 | // FIXME: What about complex? What about half? |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6656 | }; |
| 6657 | return S.Context.*ArithmeticTypes[index]; |
| 6658 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6659 | |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6660 | /// \brief Gets the canonical type resulting from the usual arithemetic |
| 6661 | /// converions for the given arithmetic types. |
| 6662 | CanQualType getUsualArithmeticConversions(unsigned L, unsigned R) { |
| 6663 | // Accelerator table for performing the usual arithmetic conversions. |
| 6664 | // The rules are basically: |
| 6665 | // - if either is floating-point, use the wider floating-point |
| 6666 | // - if same signedness, use the higher rank |
| 6667 | // - if same size, use unsigned of the higher rank |
| 6668 | // - use the larger type |
| 6669 | // These rules, together with the axiom that higher ranks are |
| 6670 | // never smaller, are sufficient to precompute all of these results |
| 6671 | // *except* when dealing with signed types of higher rank. |
| 6672 | // (we could precompute SLL x UI for all known platforms, but it's |
| 6673 | // better not to make any assumptions). |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6674 | // 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] | 6675 | enum PromotedType { |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6676 | Dep=-1, |
| 6677 | Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6678 | }; |
Nuno Lopes | 79e244f | 2012-04-21 14:45:25 +0000 | [diff] [blame] | 6679 | static const PromotedType ConversionsTable[LastPromotedArithmeticType] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6680 | [LastPromotedArithmeticType] = { |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6681 | /* Flt*/ { Flt, Dbl, LDbl, Flt, Flt, Flt, Flt, Flt, Flt, Flt, Flt }, |
| 6682 | /* Dbl*/ { Dbl, Dbl, LDbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl }, |
| 6683 | /*LDbl*/ { LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl }, |
| 6684 | /* SI*/ { Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 }, |
| 6685 | /* SL*/ { Flt, Dbl, LDbl, SL, SL, SLL, S128, Dep, UL, ULL, U128 }, |
| 6686 | /* SLL*/ { Flt, Dbl, LDbl, SLL, SLL, SLL, S128, Dep, Dep, ULL, U128 }, |
| 6687 | /*S128*/ { Flt, Dbl, LDbl, S128, S128, S128, S128, S128, S128, S128, U128 }, |
| 6688 | /* UI*/ { Flt, Dbl, LDbl, UI, Dep, Dep, S128, UI, UL, ULL, U128 }, |
| 6689 | /* UL*/ { Flt, Dbl, LDbl, UL, UL, Dep, S128, UL, UL, ULL, U128 }, |
| 6690 | /* ULL*/ { Flt, Dbl, LDbl, ULL, ULL, ULL, S128, ULL, ULL, ULL, U128 }, |
| 6691 | /*U128*/ { Flt, Dbl, LDbl, U128, U128, U128, U128, U128, U128, U128, U128 }, |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6692 | }; |
| 6693 | |
| 6694 | assert(L < LastPromotedArithmeticType); |
| 6695 | assert(R < LastPromotedArithmeticType); |
| 6696 | int Idx = ConversionsTable[L][R]; |
| 6697 | |
| 6698 | // Fast path: the table gives us a concrete answer. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6699 | if (Idx != Dep) return getArithmeticType(Idx); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6700 | |
| 6701 | // Slow path: we need to compare widths. |
| 6702 | // An invariant is that the signed type has higher rank. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6703 | CanQualType LT = getArithmeticType(L), |
| 6704 | RT = getArithmeticType(R); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6705 | unsigned LW = S.Context.getIntWidth(LT), |
| 6706 | RW = S.Context.getIntWidth(RT); |
| 6707 | |
| 6708 | // If they're different widths, use the signed type. |
| 6709 | if (LW > RW) return LT; |
| 6710 | else if (LW < RW) return RT; |
| 6711 | |
| 6712 | // Otherwise, use the unsigned type of the signed type's rank. |
| 6713 | if (L == SL || R == SL) return S.Context.UnsignedLongTy; |
| 6714 | assert(L == SLL || R == SLL); |
| 6715 | return S.Context.UnsignedLongLongTy; |
| 6716 | } |
| 6717 | |
Chandler Carruth | 3c69dc4 | 2010-12-12 09:22:45 +0000 | [diff] [blame] | 6718 | /// \brief Helper method to factor out the common pattern of adding overloads |
| 6719 | /// for '++' and '--' builtin operators. |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6720 | void addPlusPlusMinusMinusStyleOverloads(QualType CandidateTy, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6721 | bool HasVolatile, |
| 6722 | bool HasRestrict) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6723 | QualType ParamTypes[2] = { |
| 6724 | S.Context.getLValueReferenceType(CandidateTy), |
| 6725 | S.Context.IntTy |
| 6726 | }; |
| 6727 | |
| 6728 | // Non-volatile version. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6729 | if (Args.size() == 1) |
| 6730 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6731 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6732 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6733 | |
| 6734 | // Use a heuristic to reduce number of builtin candidates in the set: |
| 6735 | // add volatile version only if there are conversions to a volatile type. |
| 6736 | if (HasVolatile) { |
| 6737 | ParamTypes[0] = |
| 6738 | S.Context.getLValueReferenceType( |
| 6739 | S.Context.getVolatileType(CandidateTy)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6740 | if (Args.size() == 1) |
| 6741 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6742 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6743 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6744 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6745 | |
| 6746 | // Add restrict version only if there are conversions to a restrict type |
| 6747 | // and our candidate type is a non-restrict-qualified pointer. |
| 6748 | if (HasRestrict && CandidateTy->isAnyPointerType() && |
| 6749 | !CandidateTy.isRestrictQualified()) { |
| 6750 | ParamTypes[0] |
| 6751 | = S.Context.getLValueReferenceType( |
| 6752 | S.Context.getCVRQualifiedType(CandidateTy, Qualifiers::Restrict)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6753 | if (Args.size() == 1) |
| 6754 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6755 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6756 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6757 | |
| 6758 | if (HasVolatile) { |
| 6759 | ParamTypes[0] |
| 6760 | = S.Context.getLValueReferenceType( |
| 6761 | S.Context.getCVRQualifiedType(CandidateTy, |
| 6762 | (Qualifiers::Volatile | |
| 6763 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6764 | if (Args.size() == 1) |
| 6765 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6766 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6767 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6768 | } |
| 6769 | } |
| 6770 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6771 | } |
| 6772 | |
| 6773 | public: |
| 6774 | BuiltinOperatorOverloadBuilder( |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6775 | Sema &S, ArrayRef<Expr *> Args, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6776 | Qualifiers VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6777 | bool HasArithmeticOrEnumeralCandidateType, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6778 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6779 | OverloadCandidateSet &CandidateSet) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6780 | : S(S), Args(Args), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6781 | VisibleTypeConversionsQuals(VisibleTypeConversionsQuals), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6782 | HasArithmeticOrEnumeralCandidateType( |
| 6783 | HasArithmeticOrEnumeralCandidateType), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6784 | CandidateTypes(CandidateTypes), |
| 6785 | CandidateSet(CandidateSet) { |
| 6786 | // Validate some of our static helper constants in debug builds. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6787 | assert(getArithmeticType(FirstPromotedIntegralType) == S.Context.IntTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6788 | "Invalid first promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6789 | assert(getArithmeticType(LastPromotedIntegralType - 1) |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6790 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6791 | "Invalid last promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6792 | assert(getArithmeticType(FirstPromotedArithmeticType) |
| 6793 | == S.Context.FloatTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6794 | "Invalid first promoted arithmetic type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6795 | assert(getArithmeticType(LastPromotedArithmeticType - 1) |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6796 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6797 | "Invalid last promoted arithmetic type"); |
| 6798 | } |
| 6799 | |
| 6800 | // C++ [over.built]p3: |
| 6801 | // |
| 6802 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 6803 | // is either volatile or empty, there exist candidate operator |
| 6804 | // functions of the form |
| 6805 | // |
| 6806 | // VQ T& operator++(VQ T&); |
| 6807 | // T operator++(VQ T&, int); |
| 6808 | // |
| 6809 | // C++ [over.built]p4: |
| 6810 | // |
| 6811 | // For every pair (T, VQ), where T is an arithmetic type other |
| 6812 | // than bool, and VQ is either volatile or empty, there exist |
| 6813 | // candidate operator functions of the form |
| 6814 | // |
| 6815 | // VQ T& operator--(VQ T&); |
| 6816 | // T operator--(VQ T&, int); |
| 6817 | void addPlusPlusMinusMinusArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6818 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6819 | return; |
| 6820 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6821 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 6822 | Arith < NumArithmeticTypes; ++Arith) { |
| 6823 | addPlusPlusMinusMinusStyleOverloads( |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6824 | getArithmeticType(Arith), |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6825 | VisibleTypeConversionsQuals.hasVolatile(), |
| 6826 | VisibleTypeConversionsQuals.hasRestrict()); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6827 | } |
| 6828 | } |
| 6829 | |
| 6830 | // C++ [over.built]p5: |
| 6831 | // |
| 6832 | // For every pair (T, VQ), where T is a cv-qualified or |
| 6833 | // cv-unqualified object type, and VQ is either volatile or |
| 6834 | // empty, there exist candidate operator functions of the form |
| 6835 | // |
| 6836 | // T*VQ& operator++(T*VQ&); |
| 6837 | // T*VQ& operator--(T*VQ&); |
| 6838 | // T* operator++(T*VQ&, int); |
| 6839 | // T* operator--(T*VQ&, int); |
| 6840 | void addPlusPlusMinusMinusPointerOverloads() { |
| 6841 | for (BuiltinCandidateTypeSet::iterator |
| 6842 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6843 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6844 | Ptr != PtrEnd; ++Ptr) { |
| 6845 | // Skip pointer types that aren't pointers to object types. |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6846 | if (!(*Ptr)->getPointeeType()->isObjectType()) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6847 | continue; |
| 6848 | |
| 6849 | addPlusPlusMinusMinusStyleOverloads(*Ptr, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6850 | (!(*Ptr).isVolatileQualified() && |
| 6851 | VisibleTypeConversionsQuals.hasVolatile()), |
| 6852 | (!(*Ptr).isRestrictQualified() && |
| 6853 | VisibleTypeConversionsQuals.hasRestrict())); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6854 | } |
| 6855 | } |
| 6856 | |
| 6857 | // C++ [over.built]p6: |
| 6858 | // For every cv-qualified or cv-unqualified object type T, there |
| 6859 | // exist candidate operator functions of the form |
| 6860 | // |
| 6861 | // T& operator*(T*); |
| 6862 | // |
| 6863 | // C++ [over.built]p7: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6864 | // 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] | 6865 | // ref-qualifier, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6866 | // T& operator*(T*); |
| 6867 | void addUnaryStarPointerOverloads() { |
| 6868 | for (BuiltinCandidateTypeSet::iterator |
| 6869 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6870 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6871 | Ptr != PtrEnd; ++Ptr) { |
| 6872 | QualType ParamTy = *Ptr; |
| 6873 | QualType PointeeTy = ParamTy->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6874 | if (!PointeeTy->isObjectType() && !PointeeTy->isFunctionType()) |
| 6875 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6876 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6877 | if (const FunctionProtoType *Proto =PointeeTy->getAs<FunctionProtoType>()) |
| 6878 | if (Proto->getTypeQuals() || Proto->getRefQualifier()) |
| 6879 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6880 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6881 | S.AddBuiltinCandidate(S.Context.getLValueReferenceType(PointeeTy), |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6882 | &ParamTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6883 | } |
| 6884 | } |
| 6885 | |
| 6886 | // C++ [over.built]p9: |
| 6887 | // For every promoted arithmetic type T, there exist candidate |
| 6888 | // operator functions of the form |
| 6889 | // |
| 6890 | // T operator+(T); |
| 6891 | // T operator-(T); |
| 6892 | void addUnaryPlusOrMinusArithmeticOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6893 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6894 | return; |
| 6895 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6896 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 6897 | Arith < LastPromotedArithmeticType; ++Arith) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6898 | QualType ArithTy = getArithmeticType(Arith); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6899 | S.AddBuiltinCandidate(ArithTy, &ArithTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6900 | } |
| 6901 | |
| 6902 | // Extension: We also add these operators for vector types. |
| 6903 | for (BuiltinCandidateTypeSet::iterator |
| 6904 | Vec = CandidateTypes[0].vector_begin(), |
| 6905 | VecEnd = CandidateTypes[0].vector_end(); |
| 6906 | Vec != VecEnd; ++Vec) { |
| 6907 | QualType VecTy = *Vec; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6908 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6909 | } |
| 6910 | } |
| 6911 | |
| 6912 | // C++ [over.built]p8: |
| 6913 | // For every type T, there exist candidate operator functions of |
| 6914 | // the form |
| 6915 | // |
| 6916 | // T* operator+(T*); |
| 6917 | void addUnaryPlusPointerOverloads() { |
| 6918 | for (BuiltinCandidateTypeSet::iterator |
| 6919 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6920 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6921 | Ptr != PtrEnd; ++Ptr) { |
| 6922 | QualType ParamTy = *Ptr; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6923 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6924 | } |
| 6925 | } |
| 6926 | |
| 6927 | // C++ [over.built]p10: |
| 6928 | // For every promoted integral type T, there exist candidate |
| 6929 | // operator functions of the form |
| 6930 | // |
| 6931 | // T operator~(T); |
| 6932 | void addUnaryTildePromotedIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6933 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6934 | return; |
| 6935 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6936 | for (unsigned Int = FirstPromotedIntegralType; |
| 6937 | Int < LastPromotedIntegralType; ++Int) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6938 | QualType IntTy = getArithmeticType(Int); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6939 | S.AddBuiltinCandidate(IntTy, &IntTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6940 | } |
| 6941 | |
| 6942 | // Extension: We also add this operator for vector types. |
| 6943 | for (BuiltinCandidateTypeSet::iterator |
| 6944 | Vec = CandidateTypes[0].vector_begin(), |
| 6945 | VecEnd = CandidateTypes[0].vector_end(); |
| 6946 | Vec != VecEnd; ++Vec) { |
| 6947 | QualType VecTy = *Vec; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6948 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6949 | } |
| 6950 | } |
| 6951 | |
| 6952 | // C++ [over.match.oper]p16: |
| 6953 | // For every pointer to member type T, there exist candidate operator |
| 6954 | // functions of the form |
| 6955 | // |
| 6956 | // bool operator==(T,T); |
| 6957 | // bool operator!=(T,T); |
| 6958 | void addEqualEqualOrNotEqualMemberPointerOverloads() { |
| 6959 | /// Set of (canonical) types that we've already handled. |
| 6960 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6961 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6962 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6963 | for (BuiltinCandidateTypeSet::iterator |
| 6964 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 6965 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 6966 | MemPtr != MemPtrEnd; |
| 6967 | ++MemPtr) { |
| 6968 | // Don't add the same builtin candidate twice. |
| 6969 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 6970 | continue; |
| 6971 | |
| 6972 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6973 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6974 | } |
| 6975 | } |
| 6976 | } |
| 6977 | |
| 6978 | // C++ [over.built]p15: |
| 6979 | // |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6980 | // For every T, where T is an enumeration type, a pointer type, or |
| 6981 | // std::nullptr_t, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6982 | // |
| 6983 | // bool operator<(T, T); |
| 6984 | // bool operator>(T, T); |
| 6985 | // bool operator<=(T, T); |
| 6986 | // bool operator>=(T, T); |
| 6987 | // bool operator==(T, T); |
| 6988 | // bool operator!=(T, T); |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6989 | void addRelationalPointerOrEnumeralOverloads() { |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 6990 | // C++ [over.match.oper]p3: |
| 6991 | // [...]the built-in candidates include all of the candidate operator |
| 6992 | // functions defined in 13.6 that, compared to the given operator, [...] |
| 6993 | // do not have the same parameter-type-list as any non-template non-member |
| 6994 | // candidate. |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6995 | // |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 6996 | // Note that in practice, this only affects enumeration types because there |
| 6997 | // aren't any built-in candidates of record type, and a user-defined operator |
| 6998 | // must have an operand of record or enumeration type. Also, the only other |
| 6999 | // overloaded operator with enumeration arguments, operator=, |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7000 | // cannot be overloaded for enumeration types, so this is the only place |
| 7001 | // where we must suppress candidates like this. |
| 7002 | llvm::DenseSet<std::pair<CanQualType, CanQualType> > |
| 7003 | UserDefinedBinaryOperators; |
| 7004 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7005 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7006 | if (CandidateTypes[ArgIdx].enumeration_begin() != |
| 7007 | CandidateTypes[ArgIdx].enumeration_end()) { |
| 7008 | for (OverloadCandidateSet::iterator C = CandidateSet.begin(), |
| 7009 | CEnd = CandidateSet.end(); |
| 7010 | C != CEnd; ++C) { |
| 7011 | if (!C->Viable || !C->Function || C->Function->getNumParams() != 2) |
| 7012 | continue; |
| 7013 | |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 7014 | if (C->Function->isFunctionTemplateSpecialization()) |
| 7015 | continue; |
| 7016 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7017 | QualType FirstParamType = |
| 7018 | C->Function->getParamDecl(0)->getType().getUnqualifiedType(); |
| 7019 | QualType SecondParamType = |
| 7020 | C->Function->getParamDecl(1)->getType().getUnqualifiedType(); |
| 7021 | |
| 7022 | // Skip if either parameter isn't of enumeral type. |
| 7023 | if (!FirstParamType->isEnumeralType() || |
| 7024 | !SecondParamType->isEnumeralType()) |
| 7025 | continue; |
| 7026 | |
| 7027 | // Add this operator to the set of known user-defined operators. |
| 7028 | UserDefinedBinaryOperators.insert( |
| 7029 | std::make_pair(S.Context.getCanonicalType(FirstParamType), |
| 7030 | S.Context.getCanonicalType(SecondParamType))); |
| 7031 | } |
| 7032 | } |
| 7033 | } |
| 7034 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7035 | /// Set of (canonical) types that we've already handled. |
| 7036 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7037 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7038 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7039 | for (BuiltinCandidateTypeSet::iterator |
| 7040 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 7041 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 7042 | Ptr != PtrEnd; ++Ptr) { |
| 7043 | // Don't add the same builtin candidate twice. |
| 7044 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7045 | continue; |
| 7046 | |
| 7047 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7048 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7049 | } |
| 7050 | for (BuiltinCandidateTypeSet::iterator |
| 7051 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7052 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7053 | Enum != EnumEnd; ++Enum) { |
| 7054 | CanQualType CanonType = S.Context.getCanonicalType(*Enum); |
| 7055 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7056 | // Don't add the same builtin candidate twice, or if a user defined |
| 7057 | // candidate exists. |
| 7058 | if (!AddedTypes.insert(CanonType) || |
| 7059 | UserDefinedBinaryOperators.count(std::make_pair(CanonType, |
| 7060 | CanonType))) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7061 | continue; |
| 7062 | |
| 7063 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7064 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7065 | } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7066 | |
| 7067 | if (CandidateTypes[ArgIdx].hasNullPtrType()) { |
| 7068 | CanQualType NullPtrTy = S.Context.getCanonicalType(S.Context.NullPtrTy); |
| 7069 | if (AddedTypes.insert(NullPtrTy) && |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7070 | !UserDefinedBinaryOperators.count(std::make_pair(NullPtrTy, |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7071 | NullPtrTy))) { |
| 7072 | QualType ParamTypes[2] = { NullPtrTy, NullPtrTy }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7073 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7074 | CandidateSet); |
| 7075 | } |
| 7076 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7077 | } |
| 7078 | } |
| 7079 | |
| 7080 | // C++ [over.built]p13: |
| 7081 | // |
| 7082 | // For every cv-qualified or cv-unqualified object type T |
| 7083 | // there exist candidate operator functions of the form |
| 7084 | // |
| 7085 | // T* operator+(T*, ptrdiff_t); |
| 7086 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 7087 | // T* operator-(T*, ptrdiff_t); |
| 7088 | // T* operator+(ptrdiff_t, T*); |
| 7089 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 7090 | // |
| 7091 | // C++ [over.built]p14: |
| 7092 | // |
| 7093 | // For every T, where T is a pointer to object type, there |
| 7094 | // exist candidate operator functions of the form |
| 7095 | // |
| 7096 | // ptrdiff_t operator-(T, T); |
| 7097 | void addBinaryPlusOrMinusPointerOverloads(OverloadedOperatorKind Op) { |
| 7098 | /// Set of (canonical) types that we've already handled. |
| 7099 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7100 | |
| 7101 | for (int Arg = 0; Arg < 2; ++Arg) { |
| 7102 | QualType AsymetricParamTypes[2] = { |
| 7103 | S.Context.getPointerDiffType(), |
| 7104 | S.Context.getPointerDiffType(), |
| 7105 | }; |
| 7106 | for (BuiltinCandidateTypeSet::iterator |
| 7107 | Ptr = CandidateTypes[Arg].pointer_begin(), |
| 7108 | PtrEnd = CandidateTypes[Arg].pointer_end(); |
| 7109 | Ptr != PtrEnd; ++Ptr) { |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7110 | QualType PointeeTy = (*Ptr)->getPointeeType(); |
| 7111 | if (!PointeeTy->isObjectType()) |
| 7112 | continue; |
| 7113 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7114 | AsymetricParamTypes[Arg] = *Ptr; |
| 7115 | if (Arg == 0 || Op == OO_Plus) { |
| 7116 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 7117 | // T* operator+(ptrdiff_t, T*); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7118 | S.AddBuiltinCandidate(*Ptr, AsymetricParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7119 | } |
| 7120 | if (Op == OO_Minus) { |
| 7121 | // ptrdiff_t operator-(T, T); |
| 7122 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7123 | continue; |
| 7124 | |
| 7125 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 7126 | S.AddBuiltinCandidate(S.Context.getPointerDiffType(), ParamTypes, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7127 | Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7128 | } |
| 7129 | } |
| 7130 | } |
| 7131 | } |
| 7132 | |
| 7133 | // C++ [over.built]p12: |
| 7134 | // |
| 7135 | // For every pair of promoted arithmetic types L and R, there |
| 7136 | // exist candidate operator functions of the form |
| 7137 | // |
| 7138 | // LR operator*(L, R); |
| 7139 | // LR operator/(L, R); |
| 7140 | // LR operator+(L, R); |
| 7141 | // LR operator-(L, R); |
| 7142 | // bool operator<(L, R); |
| 7143 | // bool operator>(L, R); |
| 7144 | // bool operator<=(L, R); |
| 7145 | // bool operator>=(L, R); |
| 7146 | // bool operator==(L, R); |
| 7147 | // bool operator!=(L, R); |
| 7148 | // |
| 7149 | // where LR is the result of the usual arithmetic conversions |
| 7150 | // between types L and R. |
| 7151 | // |
| 7152 | // C++ [over.built]p24: |
| 7153 | // |
| 7154 | // For every pair of promoted arithmetic types L and R, there exist |
| 7155 | // candidate operator functions of the form |
| 7156 | // |
| 7157 | // LR operator?(bool, L, R); |
| 7158 | // |
| 7159 | // where LR is the result of the usual arithmetic conversions |
| 7160 | // between types L and R. |
| 7161 | // Our candidates ignore the first parameter. |
| 7162 | void addGenericBinaryArithmeticOverloads(bool isComparison) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7163 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7164 | return; |
| 7165 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7166 | for (unsigned Left = FirstPromotedArithmeticType; |
| 7167 | Left < LastPromotedArithmeticType; ++Left) { |
| 7168 | for (unsigned Right = FirstPromotedArithmeticType; |
| 7169 | Right < LastPromotedArithmeticType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7170 | QualType LandR[2] = { getArithmeticType(Left), |
| 7171 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7172 | QualType Result = |
| 7173 | isComparison ? S.Context.BoolTy |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7174 | : getUsualArithmeticConversions(Left, Right); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7175 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7176 | } |
| 7177 | } |
| 7178 | |
| 7179 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 7180 | // conditional operator for vector types. |
| 7181 | for (BuiltinCandidateTypeSet::iterator |
| 7182 | Vec1 = CandidateTypes[0].vector_begin(), |
| 7183 | Vec1End = CandidateTypes[0].vector_end(); |
| 7184 | Vec1 != Vec1End; ++Vec1) { |
| 7185 | for (BuiltinCandidateTypeSet::iterator |
| 7186 | Vec2 = CandidateTypes[1].vector_begin(), |
| 7187 | Vec2End = CandidateTypes[1].vector_end(); |
| 7188 | Vec2 != Vec2End; ++Vec2) { |
| 7189 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 7190 | QualType Result = S.Context.BoolTy; |
| 7191 | if (!isComparison) { |
| 7192 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 7193 | Result = *Vec1; |
| 7194 | else |
| 7195 | Result = *Vec2; |
| 7196 | } |
| 7197 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7198 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7199 | } |
| 7200 | } |
| 7201 | } |
| 7202 | |
| 7203 | // C++ [over.built]p17: |
| 7204 | // |
| 7205 | // For every pair of promoted integral types L and R, there |
| 7206 | // exist candidate operator functions of the form |
| 7207 | // |
| 7208 | // LR operator%(L, R); |
| 7209 | // LR operator&(L, R); |
| 7210 | // LR operator^(L, R); |
| 7211 | // LR operator|(L, R); |
| 7212 | // L operator<<(L, R); |
| 7213 | // L operator>>(L, R); |
| 7214 | // |
| 7215 | // where LR is the result of the usual arithmetic conversions |
| 7216 | // between types L and R. |
| 7217 | void addBinaryBitwiseArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7218 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7219 | return; |
| 7220 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7221 | for (unsigned Left = FirstPromotedIntegralType; |
| 7222 | Left < LastPromotedIntegralType; ++Left) { |
| 7223 | for (unsigned Right = FirstPromotedIntegralType; |
| 7224 | Right < LastPromotedIntegralType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7225 | QualType LandR[2] = { getArithmeticType(Left), |
| 7226 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7227 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 7228 | ? LandR[0] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7229 | : getUsualArithmeticConversions(Left, Right); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7230 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7231 | } |
| 7232 | } |
| 7233 | } |
| 7234 | |
| 7235 | // C++ [over.built]p20: |
| 7236 | // |
| 7237 | // For every pair (T, VQ), where T is an enumeration or |
| 7238 | // pointer to member type and VQ is either volatile or |
| 7239 | // empty, there exist candidate operator functions of the form |
| 7240 | // |
| 7241 | // VQ T& operator=(VQ T&, T); |
| 7242 | void addAssignmentMemberPointerOrEnumeralOverloads() { |
| 7243 | /// Set of (canonical) types that we've already handled. |
| 7244 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7245 | |
| 7246 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7247 | for (BuiltinCandidateTypeSet::iterator |
| 7248 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7249 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7250 | Enum != EnumEnd; ++Enum) { |
| 7251 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 7252 | continue; |
| 7253 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7254 | AddBuiltinAssignmentOperatorCandidates(S, *Enum, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7255 | } |
| 7256 | |
| 7257 | for (BuiltinCandidateTypeSet::iterator |
| 7258 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7259 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7260 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7261 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7262 | continue; |
| 7263 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7264 | AddBuiltinAssignmentOperatorCandidates(S, *MemPtr, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7265 | } |
| 7266 | } |
| 7267 | } |
| 7268 | |
| 7269 | // C++ [over.built]p19: |
| 7270 | // |
| 7271 | // For every pair (T, VQ), where T is any type and VQ is either |
| 7272 | // volatile or empty, there exist candidate operator functions |
| 7273 | // of the form |
| 7274 | // |
| 7275 | // T*VQ& operator=(T*VQ&, T*); |
| 7276 | // |
| 7277 | // C++ [over.built]p21: |
| 7278 | // |
| 7279 | // For every pair (T, VQ), where T is a cv-qualified or |
| 7280 | // cv-unqualified object type and VQ is either volatile or |
| 7281 | // empty, there exist candidate operator functions of the form |
| 7282 | // |
| 7283 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 7284 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 7285 | void addAssignmentPointerOverloads(bool isEqualOp) { |
| 7286 | /// Set of (canonical) types that we've already handled. |
| 7287 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7288 | |
| 7289 | for (BuiltinCandidateTypeSet::iterator |
| 7290 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7291 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7292 | Ptr != PtrEnd; ++Ptr) { |
| 7293 | // If this is operator=, keep track of the builtin candidates we added. |
| 7294 | if (isEqualOp) |
| 7295 | AddedTypes.insert(S.Context.getCanonicalType(*Ptr)); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7296 | else if (!(*Ptr)->getPointeeType()->isObjectType()) |
| 7297 | continue; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7298 | |
| 7299 | // non-volatile version |
| 7300 | QualType ParamTypes[2] = { |
| 7301 | S.Context.getLValueReferenceType(*Ptr), |
| 7302 | isEqualOp ? *Ptr : S.Context.getPointerDiffType(), |
| 7303 | }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7304 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7305 | /*IsAssigmentOperator=*/ isEqualOp); |
| 7306 | |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7307 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7308 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7309 | if (NeedVolatile) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7310 | // volatile version |
| 7311 | ParamTypes[0] = |
| 7312 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7313 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7314 | /*IsAssigmentOperator=*/isEqualOp); |
| 7315 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7316 | |
| 7317 | if (!(*Ptr).isRestrictQualified() && |
| 7318 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7319 | // restrict version |
| 7320 | ParamTypes[0] |
| 7321 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7322 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7323 | /*IsAssigmentOperator=*/isEqualOp); |
| 7324 | |
| 7325 | if (NeedVolatile) { |
| 7326 | // volatile restrict version |
| 7327 | ParamTypes[0] |
| 7328 | = S.Context.getLValueReferenceType( |
| 7329 | S.Context.getCVRQualifiedType(*Ptr, |
| 7330 | (Qualifiers::Volatile | |
| 7331 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7332 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7333 | /*IsAssigmentOperator=*/isEqualOp); |
| 7334 | } |
| 7335 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7336 | } |
| 7337 | |
| 7338 | if (isEqualOp) { |
| 7339 | for (BuiltinCandidateTypeSet::iterator |
| 7340 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7341 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7342 | Ptr != PtrEnd; ++Ptr) { |
| 7343 | // Make sure we don't add the same candidate twice. |
| 7344 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7345 | continue; |
| 7346 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7347 | QualType ParamTypes[2] = { |
| 7348 | S.Context.getLValueReferenceType(*Ptr), |
| 7349 | *Ptr, |
| 7350 | }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7351 | |
| 7352 | // non-volatile version |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7353 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7354 | /*IsAssigmentOperator=*/true); |
| 7355 | |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7356 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7357 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7358 | if (NeedVolatile) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7359 | // volatile version |
| 7360 | ParamTypes[0] = |
| 7361 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7362 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7363 | /*IsAssigmentOperator=*/true); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7364 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7365 | |
| 7366 | if (!(*Ptr).isRestrictQualified() && |
| 7367 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7368 | // restrict version |
| 7369 | ParamTypes[0] |
| 7370 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7371 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7372 | /*IsAssigmentOperator=*/true); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7373 | |
| 7374 | if (NeedVolatile) { |
| 7375 | // volatile restrict version |
| 7376 | ParamTypes[0] |
| 7377 | = S.Context.getLValueReferenceType( |
| 7378 | S.Context.getCVRQualifiedType(*Ptr, |
| 7379 | (Qualifiers::Volatile | |
| 7380 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7381 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7382 | /*IsAssigmentOperator=*/true); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7383 | } |
| 7384 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7385 | } |
| 7386 | } |
| 7387 | } |
| 7388 | |
| 7389 | // C++ [over.built]p18: |
| 7390 | // |
| 7391 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 7392 | // VQ is either volatile or empty, and R is a promoted |
| 7393 | // arithmetic type, there exist candidate operator functions of |
| 7394 | // the form |
| 7395 | // |
| 7396 | // VQ L& operator=(VQ L&, R); |
| 7397 | // VQ L& operator*=(VQ L&, R); |
| 7398 | // VQ L& operator/=(VQ L&, R); |
| 7399 | // VQ L& operator+=(VQ L&, R); |
| 7400 | // VQ L& operator-=(VQ L&, R); |
| 7401 | void addAssignmentArithmeticOverloads(bool isEqualOp) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7402 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7403 | return; |
| 7404 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7405 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 7406 | for (unsigned Right = FirstPromotedArithmeticType; |
| 7407 | Right < LastPromotedArithmeticType; ++Right) { |
| 7408 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7409 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7410 | |
| 7411 | // Add this built-in operator as a candidate (VQ is empty). |
| 7412 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7413 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7414 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7415 | /*IsAssigmentOperator=*/isEqualOp); |
| 7416 | |
| 7417 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7418 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7419 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7420 | S.Context.getVolatileType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7421 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7422 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7423 | /*IsAssigmentOperator=*/isEqualOp); |
| 7424 | } |
| 7425 | } |
| 7426 | } |
| 7427 | |
| 7428 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 7429 | for (BuiltinCandidateTypeSet::iterator |
| 7430 | Vec1 = CandidateTypes[0].vector_begin(), |
| 7431 | Vec1End = CandidateTypes[0].vector_end(); |
| 7432 | Vec1 != Vec1End; ++Vec1) { |
| 7433 | for (BuiltinCandidateTypeSet::iterator |
| 7434 | Vec2 = CandidateTypes[1].vector_begin(), |
| 7435 | Vec2End = CandidateTypes[1].vector_end(); |
| 7436 | Vec2 != Vec2End; ++Vec2) { |
| 7437 | QualType ParamTypes[2]; |
| 7438 | ParamTypes[1] = *Vec2; |
| 7439 | // Add this built-in operator as a candidate (VQ is empty). |
| 7440 | ParamTypes[0] = S.Context.getLValueReferenceType(*Vec1); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7441 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7442 | /*IsAssigmentOperator=*/isEqualOp); |
| 7443 | |
| 7444 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7445 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7446 | ParamTypes[0] = S.Context.getVolatileType(*Vec1); |
| 7447 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7448 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7449 | /*IsAssigmentOperator=*/isEqualOp); |
| 7450 | } |
| 7451 | } |
| 7452 | } |
| 7453 | } |
| 7454 | |
| 7455 | // C++ [over.built]p22: |
| 7456 | // |
| 7457 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 7458 | // is either volatile or empty, and R is a promoted integral |
| 7459 | // type, there exist candidate operator functions of the form |
| 7460 | // |
| 7461 | // VQ L& operator%=(VQ L&, R); |
| 7462 | // VQ L& operator<<=(VQ L&, R); |
| 7463 | // VQ L& operator>>=(VQ L&, R); |
| 7464 | // VQ L& operator&=(VQ L&, R); |
| 7465 | // VQ L& operator^=(VQ L&, R); |
| 7466 | // VQ L& operator|=(VQ L&, R); |
| 7467 | void addAssignmentIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7468 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7469 | return; |
| 7470 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7471 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 7472 | for (unsigned Right = FirstPromotedIntegralType; |
| 7473 | Right < LastPromotedIntegralType; ++Right) { |
| 7474 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7475 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7476 | |
| 7477 | // Add this built-in operator as a candidate (VQ is empty). |
| 7478 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7479 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7480 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7481 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7482 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7483 | ParamTypes[0] = getArithmeticType(Left); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7484 | ParamTypes[0] = S.Context.getVolatileType(ParamTypes[0]); |
| 7485 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7486 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7487 | } |
| 7488 | } |
| 7489 | } |
| 7490 | } |
| 7491 | |
| 7492 | // C++ [over.operator]p23: |
| 7493 | // |
| 7494 | // There also exist candidate operator functions of the form |
| 7495 | // |
| 7496 | // bool operator!(bool); |
| 7497 | // bool operator&&(bool, bool); |
| 7498 | // bool operator||(bool, bool); |
| 7499 | void addExclaimOverload() { |
| 7500 | QualType ParamTy = S.Context.BoolTy; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7501 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7502 | /*IsAssignmentOperator=*/false, |
| 7503 | /*NumContextualBoolArguments=*/1); |
| 7504 | } |
| 7505 | void addAmpAmpOrPipePipeOverload() { |
| 7506 | QualType ParamTypes[2] = { S.Context.BoolTy, S.Context.BoolTy }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7507 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7508 | /*IsAssignmentOperator=*/false, |
| 7509 | /*NumContextualBoolArguments=*/2); |
| 7510 | } |
| 7511 | |
| 7512 | // C++ [over.built]p13: |
| 7513 | // |
| 7514 | // For every cv-qualified or cv-unqualified object type T there |
| 7515 | // exist candidate operator functions of the form |
| 7516 | // |
| 7517 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 7518 | // T& operator[](T*, ptrdiff_t); |
| 7519 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 7520 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 7521 | // T& operator[](ptrdiff_t, T*); |
| 7522 | void addSubscriptOverloads() { |
| 7523 | for (BuiltinCandidateTypeSet::iterator |
| 7524 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7525 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7526 | Ptr != PtrEnd; ++Ptr) { |
| 7527 | QualType ParamTypes[2] = { *Ptr, S.Context.getPointerDiffType() }; |
| 7528 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7529 | if (!PointeeType->isObjectType()) |
| 7530 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7531 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7532 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7533 | |
| 7534 | // T& operator[](T*, ptrdiff_t) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7535 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7536 | } |
| 7537 | |
| 7538 | for (BuiltinCandidateTypeSet::iterator |
| 7539 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7540 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7541 | Ptr != PtrEnd; ++Ptr) { |
| 7542 | QualType ParamTypes[2] = { S.Context.getPointerDiffType(), *Ptr }; |
| 7543 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7544 | if (!PointeeType->isObjectType()) |
| 7545 | continue; |
| 7546 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7547 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7548 | |
| 7549 | // T& operator[](ptrdiff_t, T*) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7550 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7551 | } |
| 7552 | } |
| 7553 | |
| 7554 | // C++ [over.built]p11: |
| 7555 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 7556 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 7557 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 7558 | // there exist candidate operator functions of the form |
| 7559 | // |
| 7560 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 7561 | // |
| 7562 | // where CV12 is the union of CV1 and CV2. |
| 7563 | void addArrowStarOverloads() { |
| 7564 | for (BuiltinCandidateTypeSet::iterator |
| 7565 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7566 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7567 | Ptr != PtrEnd; ++Ptr) { |
| 7568 | QualType C1Ty = (*Ptr); |
| 7569 | QualType C1; |
| 7570 | QualifierCollector Q1; |
| 7571 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
| 7572 | if (!isa<RecordType>(C1)) |
| 7573 | continue; |
| 7574 | // heuristic to reduce number of builtin candidates in the set. |
| 7575 | // Add volatile/restrict version only if there are conversions to a |
| 7576 | // volatile/restrict type. |
| 7577 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 7578 | continue; |
| 7579 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 7580 | continue; |
| 7581 | for (BuiltinCandidateTypeSet::iterator |
| 7582 | MemPtr = CandidateTypes[1].member_pointer_begin(), |
| 7583 | MemPtrEnd = CandidateTypes[1].member_pointer_end(); |
| 7584 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7585 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 7586 | QualType C2 = QualType(mptr->getClass(), 0); |
| 7587 | C2 = C2.getUnqualifiedType(); |
| 7588 | if (C1 != C2 && !S.IsDerivedFrom(C1, C2)) |
| 7589 | break; |
| 7590 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 7591 | // build CV12 T& |
| 7592 | QualType T = mptr->getPointeeType(); |
| 7593 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 7594 | T.isVolatileQualified()) |
| 7595 | continue; |
| 7596 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 7597 | T.isRestrictQualified()) |
| 7598 | continue; |
| 7599 | T = Q1.apply(S.Context, T); |
| 7600 | QualType ResultTy = S.Context.getLValueReferenceType(T); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7601 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7602 | } |
| 7603 | } |
| 7604 | } |
| 7605 | |
| 7606 | // Note that we don't consider the first argument, since it has been |
| 7607 | // contextually converted to bool long ago. The candidates below are |
| 7608 | // therefore added as binary. |
| 7609 | // |
| 7610 | // C++ [over.built]p25: |
| 7611 | // For every type T, where T is a pointer, pointer-to-member, or scoped |
| 7612 | // enumeration type, there exist candidate operator functions of the form |
| 7613 | // |
| 7614 | // T operator?(bool, T, T); |
| 7615 | // |
| 7616 | void addConditionalOperatorOverloads() { |
| 7617 | /// Set of (canonical) types that we've already handled. |
| 7618 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7619 | |
| 7620 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7621 | for (BuiltinCandidateTypeSet::iterator |
| 7622 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 7623 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 7624 | Ptr != PtrEnd; ++Ptr) { |
| 7625 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7626 | continue; |
| 7627 | |
| 7628 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7629 | S.AddBuiltinCandidate(*Ptr, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7630 | } |
| 7631 | |
| 7632 | for (BuiltinCandidateTypeSet::iterator |
| 7633 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7634 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7635 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7636 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7637 | continue; |
| 7638 | |
| 7639 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7640 | S.AddBuiltinCandidate(*MemPtr, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7641 | } |
| 7642 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7643 | if (S.getLangOpts().CPlusPlus11) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7644 | for (BuiltinCandidateTypeSet::iterator |
| 7645 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7646 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7647 | Enum != EnumEnd; ++Enum) { |
| 7648 | if (!(*Enum)->getAs<EnumType>()->getDecl()->isScoped()) |
| 7649 | continue; |
| 7650 | |
| 7651 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 7652 | continue; |
| 7653 | |
| 7654 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7655 | S.AddBuiltinCandidate(*Enum, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7656 | } |
| 7657 | } |
| 7658 | } |
| 7659 | } |
| 7660 | }; |
| 7661 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7662 | } // end anonymous namespace |
| 7663 | |
| 7664 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 7665 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 7666 | /// on the operator @p Op and the arguments given. For example, if the |
| 7667 | /// operator is a binary '+', this routine might add "int |
| 7668 | /// operator+(int, int)" to cover integer addition. |
Robert Wilhelm | 834c058 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 7669 | void Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 7670 | SourceLocation OpLoc, |
| 7671 | ArrayRef<Expr *> Args, |
| 7672 | OverloadCandidateSet &CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7673 | // Find all of the types that the arguments can convert to, but only |
| 7674 | // if the operator we're looking at has built-in operator candidates |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7675 | // that make use of these types. Also record whether we encounter non-record |
| 7676 | // candidate types or either arithmetic or enumeral candidate types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7677 | Qualifiers VisibleTypeConversionsQuals; |
| 7678 | VisibleTypeConversionsQuals.addConst(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7679 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 7680 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7681 | |
| 7682 | bool HasNonRecordCandidateType = false; |
| 7683 | bool HasArithmeticOrEnumeralCandidateType = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7684 | SmallVector<BuiltinCandidateTypeSet, 2> CandidateTypes; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7685 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7686 | CandidateTypes.push_back(BuiltinCandidateTypeSet(*this)); |
| 7687 | CandidateTypes[ArgIdx].AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 7688 | OpLoc, |
| 7689 | true, |
| 7690 | (Op == OO_Exclaim || |
| 7691 | Op == OO_AmpAmp || |
| 7692 | Op == OO_PipePipe), |
| 7693 | VisibleTypeConversionsQuals); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7694 | HasNonRecordCandidateType = HasNonRecordCandidateType || |
| 7695 | CandidateTypes[ArgIdx].hasNonRecordTypes(); |
| 7696 | HasArithmeticOrEnumeralCandidateType = |
| 7697 | HasArithmeticOrEnumeralCandidateType || |
| 7698 | CandidateTypes[ArgIdx].hasArithmeticOrEnumeralTypes(); |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7699 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7700 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7701 | // Exit early when no non-record types have been added to the candidate set |
| 7702 | // for any of the arguments to the operator. |
Douglas Gregor | 25aaff9 | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 7703 | // |
| 7704 | // We can't exit early for !, ||, or &&, since there we have always have |
| 7705 | // 'bool' overloads. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7706 | if (!HasNonRecordCandidateType && |
Douglas Gregor | 25aaff9 | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 7707 | !(Op == OO_Exclaim || Op == OO_AmpAmp || Op == OO_PipePipe)) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7708 | return; |
| 7709 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7710 | // Setup an object to manage the common state for building overloads. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7711 | BuiltinOperatorOverloadBuilder OpBuilder(*this, Args, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7712 | VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7713 | HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7714 | CandidateTypes, CandidateSet); |
| 7715 | |
| 7716 | // Dispatch over the operation to add in only those overloads which apply. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7717 | switch (Op) { |
| 7718 | case OO_None: |
| 7719 | case NUM_OVERLOADED_OPERATORS: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7720 | llvm_unreachable("Expected an overloaded operator"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7721 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7722 | case OO_New: |
| 7723 | case OO_Delete: |
| 7724 | case OO_Array_New: |
| 7725 | case OO_Array_Delete: |
| 7726 | case OO_Call: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7727 | llvm_unreachable( |
| 7728 | "Special operators don't use AddBuiltinOperatorCandidates"); |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7729 | |
| 7730 | case OO_Comma: |
| 7731 | case OO_Arrow: |
| 7732 | // C++ [over.match.oper]p3: |
| 7733 | // -- For the operator ',', the unary operator '&', or the |
| 7734 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7735 | break; |
| 7736 | |
| 7737 | case OO_Plus: // '+' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7738 | if (Args.size() == 1) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7739 | OpBuilder.addUnaryPlusPointerOverloads(); |
Chandler Carruth | 32fe0d0 | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 7740 | // Fall through. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7741 | |
| 7742 | case OO_Minus: // '-' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7743 | if (Args.size() == 1) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7744 | OpBuilder.addUnaryPlusOrMinusArithmeticOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7745 | } else { |
| 7746 | OpBuilder.addBinaryPlusOrMinusPointerOverloads(Op); |
| 7747 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7748 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7749 | break; |
| 7750 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7751 | case OO_Star: // '*' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7752 | if (Args.size() == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7753 | OpBuilder.addUnaryStarPointerOverloads(); |
| 7754 | else |
| 7755 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7756 | break; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7757 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7758 | case OO_Slash: |
| 7759 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 7760 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7761 | |
| 7762 | case OO_PlusPlus: |
| 7763 | case OO_MinusMinus: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7764 | OpBuilder.addPlusPlusMinusMinusArithmeticOverloads(Op); |
| 7765 | OpBuilder.addPlusPlusMinusMinusPointerOverloads(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7766 | break; |
| 7767 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 7768 | case OO_EqualEqual: |
| 7769 | case OO_ExclaimEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7770 | OpBuilder.addEqualEqualOrNotEqualMemberPointerOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 7771 | // Fall through. |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 7772 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7773 | case OO_Less: |
| 7774 | case OO_Greater: |
| 7775 | case OO_LessEqual: |
| 7776 | case OO_GreaterEqual: |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7777 | OpBuilder.addRelationalPointerOrEnumeralOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 7778 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/true); |
| 7779 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7780 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7781 | case OO_Percent: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7782 | case OO_Caret: |
| 7783 | case OO_Pipe: |
| 7784 | case OO_LessLess: |
| 7785 | case OO_GreaterGreater: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7786 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7787 | break; |
| 7788 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7789 | case OO_Amp: // '&' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7790 | if (Args.size() == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7791 | // C++ [over.match.oper]p3: |
| 7792 | // -- For the operator ',', the unary operator '&', or the |
| 7793 | // operator '->', the built-in candidates set is empty. |
| 7794 | break; |
| 7795 | |
| 7796 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
| 7797 | break; |
| 7798 | |
| 7799 | case OO_Tilde: |
| 7800 | OpBuilder.addUnaryTildePromotedIntegralOverloads(); |
| 7801 | break; |
| 7802 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7803 | case OO_Equal: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7804 | OpBuilder.addAssignmentMemberPointerOrEnumeralOverloads(); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7805 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7806 | |
| 7807 | case OO_PlusEqual: |
| 7808 | case OO_MinusEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7809 | OpBuilder.addAssignmentPointerOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7810 | // Fall through. |
| 7811 | |
| 7812 | case OO_StarEqual: |
| 7813 | case OO_SlashEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7814 | OpBuilder.addAssignmentArithmeticOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7815 | break; |
| 7816 | |
| 7817 | case OO_PercentEqual: |
| 7818 | case OO_LessLessEqual: |
| 7819 | case OO_GreaterGreaterEqual: |
| 7820 | case OO_AmpEqual: |
| 7821 | case OO_CaretEqual: |
| 7822 | case OO_PipeEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7823 | OpBuilder.addAssignmentIntegralOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7824 | break; |
| 7825 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7826 | case OO_Exclaim: |
| 7827 | OpBuilder.addExclaimOverload(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7828 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7829 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7830 | case OO_AmpAmp: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7831 | case OO_PipePipe: |
| 7832 | OpBuilder.addAmpAmpOrPipePipeOverload(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7833 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7834 | |
| 7835 | case OO_Subscript: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7836 | OpBuilder.addSubscriptOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7837 | break; |
| 7838 | |
| 7839 | case OO_ArrowStar: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7840 | OpBuilder.addArrowStarOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7841 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 7842 | |
| 7843 | case OO_Conditional: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7844 | OpBuilder.addConditionalOperatorOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7845 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7846 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7847 | } |
| 7848 | } |
| 7849 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7850 | /// \brief Add function candidates found via argument-dependent lookup |
| 7851 | /// to the set of overloading candidates. |
| 7852 | /// |
| 7853 | /// This routine performs argument-dependent name lookup based on the |
| 7854 | /// given function name (which may also be an operator name) and adds |
| 7855 | /// all of the overload candidates found by ADL to the overload |
| 7856 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7857 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7858 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
Richard Smith | f5cd5cc | 2012-02-25 06:24:24 +0000 | [diff] [blame] | 7859 | bool Operator, SourceLocation Loc, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 7860 | ArrayRef<Expr *> Args, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 7861 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7862 | OverloadCandidateSet& CandidateSet, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 7863 | bool PartialOverloading) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7864 | ADLResult Fns; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7865 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 7866 | // FIXME: This approach for uniquing ADL results (and removing |
| 7867 | // redundant candidates from the set) relies on pointer-equality, |
| 7868 | // which means we need to key off the canonical decl. However, |
| 7869 | // always going back to the canonical decl might not get us the |
| 7870 | // right set of default arguments. What default arguments are |
| 7871 | // we supposed to consider on ADL candidates, anyway? |
| 7872 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7873 | // FIXME: Pass in the explicit template arguments? |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 7874 | ArgumentDependentLookup(Name, Operator, Loc, Args, Fns); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7875 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7876 | // Erase all of the candidates we already knew about. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7877 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 7878 | CandEnd = CandidateSet.end(); |
| 7879 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7880 | if (Cand->Function) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7881 | Fns.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7882 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7883 | Fns.erase(FunTmpl); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7884 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7885 | |
| 7886 | // For each of the ADL candidates we found, add it to the overload |
| 7887 | // set. |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7888 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7889 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7890 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7891 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7892 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7893 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7894 | AddOverloadCandidate(FD, FoundDecl, Args, CandidateSet, false, |
| 7895 | PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7896 | } else |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7897 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7898 | FoundDecl, ExplicitTemplateArgs, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7899 | Args, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7900 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7901 | } |
| 7902 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7903 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 7904 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7905 | bool |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7906 | isBetterOverloadCandidate(Sema &S, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 7907 | const OverloadCandidate &Cand1, |
| 7908 | const OverloadCandidate &Cand2, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7909 | SourceLocation Loc, |
| 7910 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7911 | // Define viable functions to be better candidates than non-viable |
| 7912 | // functions. |
| 7913 | if (!Cand2.Viable) |
| 7914 | return Cand1.Viable; |
| 7915 | else if (!Cand1.Viable) |
| 7916 | return false; |
| 7917 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7918 | // C++ [over.match.best]p1: |
| 7919 | // |
| 7920 | // -- if F is a static member function, ICS1(F) is defined such |
| 7921 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 7922 | // any function G, and, symmetrically, ICS1(G) is neither |
| 7923 | // better nor worse than ICS1(F). |
| 7924 | unsigned StartArg = 0; |
| 7925 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 7926 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7927 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7928 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7929 | // A viable function F1 is defined to be a better function than another |
| 7930 | // 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] | 7931 | // conversion sequence than ICSi(F2), and then... |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 7932 | unsigned NumArgs = Cand1.NumConversions; |
| 7933 | assert(Cand2.NumConversions == NumArgs && "Overload candidate mismatch"); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7934 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7935 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7936 | switch (CompareImplicitConversionSequences(S, |
| 7937 | Cand1.Conversions[ArgIdx], |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7938 | Cand2.Conversions[ArgIdx])) { |
| 7939 | case ImplicitConversionSequence::Better: |
| 7940 | // Cand1 has a better conversion sequence. |
| 7941 | HasBetterConversion = true; |
| 7942 | break; |
| 7943 | |
| 7944 | case ImplicitConversionSequence::Worse: |
| 7945 | // Cand1 can't be better than Cand2. |
| 7946 | return false; |
| 7947 | |
| 7948 | case ImplicitConversionSequence::Indistinguishable: |
| 7949 | // Do nothing. |
| 7950 | break; |
| 7951 | } |
| 7952 | } |
| 7953 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7954 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7955 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7956 | if (HasBetterConversion) |
| 7957 | return true; |
| 7958 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7959 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7960 | // specialization, or, if not that, |
Douglas Gregor | ccd4713 | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 7961 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7962 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 7963 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7964 | |
| 7965 | // -- F1 and F2 are function template specializations, and the function |
| 7966 | // template for F1 is more specialized than the template for F2 |
| 7967 | // 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] | 7968 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 7969 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7970 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7971 | if (FunctionTemplateDecl *BetterTemplate |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7972 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 7973 | Cand2.Function->getPrimaryTemplate(), |
| 7974 | Loc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7975 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 7976 | : TPOC_Call, |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7977 | Cand1.ExplicitCallArguments)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7978 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7979 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7980 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7981 | // -- the context is an initialization by user-defined conversion |
| 7982 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 7983 | // from the return type of F1 to the destination type (i.e., |
| 7984 | // the type of the entity being initialized) is a better |
| 7985 | // conversion sequence than the standard conversion sequence |
| 7986 | // from the return type of F2 to the destination type. |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7987 | if (UserDefinedConversion && Cand1.Function && Cand2.Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7988 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7989 | isa<CXXConversionDecl>(Cand2.Function)) { |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 7990 | // First check whether we prefer one of the conversion functions over the |
| 7991 | // other. This only distinguishes the results in non-standard, extension |
| 7992 | // cases such as the conversion from a lambda closure type to a function |
| 7993 | // pointer or block. |
| 7994 | ImplicitConversionSequence::CompareKind FuncResult |
| 7995 | = compareConversionFunctions(S, Cand1.Function, Cand2.Function); |
| 7996 | if (FuncResult != ImplicitConversionSequence::Indistinguishable) |
| 7997 | return FuncResult; |
| 7998 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7999 | switch (CompareStandardConversionSequences(S, |
| 8000 | Cand1.FinalConversion, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 8001 | Cand2.FinalConversion)) { |
| 8002 | case ImplicitConversionSequence::Better: |
| 8003 | // Cand1 has a better conversion sequence. |
| 8004 | return true; |
| 8005 | |
| 8006 | case ImplicitConversionSequence::Worse: |
| 8007 | // Cand1 can't be better than Cand2. |
| 8008 | return false; |
| 8009 | |
| 8010 | case ImplicitConversionSequence::Indistinguishable: |
| 8011 | // Do nothing |
| 8012 | break; |
| 8013 | } |
| 8014 | } |
| 8015 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8016 | return false; |
| 8017 | } |
| 8018 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8019 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8020 | /// within an overload candidate set. |
| 8021 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 8022 | /// \param Loc The location of the function name (or operator symbol) for |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8023 | /// which overload resolution occurs. |
| 8024 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 8025 | /// \param Best If overload resolution was successful or found a deleted |
| 8026 | /// function, \p Best points to the candidate function found. |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8027 | /// |
| 8028 | /// \returns The result of overload resolution. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8029 | OverloadingResult |
| 8030 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 8031 | iterator &Best, |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8032 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8033 | // Find the best viable function. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8034 | Best = end(); |
| 8035 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
| 8036 | if (Cand->Viable) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8037 | if (Best == end() || isBetterOverloadCandidate(S, *Cand, *Best, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8038 | UserDefinedConversion)) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8039 | Best = Cand; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8040 | } |
| 8041 | |
| 8042 | // If we didn't find any viable functions, abort. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8043 | if (Best == end()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8044 | return OR_No_Viable_Function; |
| 8045 | |
| 8046 | // Make sure that this function is better than every other viable |
| 8047 | // function. If not, we have an ambiguity. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8048 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8049 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8050 | Cand != Best && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8051 | !isBetterOverloadCandidate(S, *Best, *Cand, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8052 | UserDefinedConversion)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8053 | Best = end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8054 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8055 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8056 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8057 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8058 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8059 | if (Best->Function && |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 8060 | (Best->Function->isDeleted() || |
| 8061 | S.isFunctionConsideredUnavailable(Best->Function))) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8062 | return OR_Deleted; |
| 8063 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8064 | return OR_Success; |
| 8065 | } |
| 8066 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8067 | namespace { |
| 8068 | |
| 8069 | enum OverloadCandidateKind { |
| 8070 | oc_function, |
| 8071 | oc_method, |
| 8072 | oc_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8073 | oc_function_template, |
| 8074 | oc_method_template, |
| 8075 | oc_constructor_template, |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8076 | oc_implicit_default_constructor, |
| 8077 | oc_implicit_copy_constructor, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8078 | oc_implicit_move_constructor, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8079 | oc_implicit_copy_assignment, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8080 | oc_implicit_move_assignment, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8081 | oc_implicit_inherited_constructor |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8082 | }; |
| 8083 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8084 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 8085 | FunctionDecl *Fn, |
| 8086 | std::string &Description) { |
| 8087 | bool isTemplate = false; |
| 8088 | |
| 8089 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 8090 | isTemplate = true; |
| 8091 | Description = S.getTemplateArgumentBindingsText( |
| 8092 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 8093 | } |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8094 | |
| 8095 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8096 | if (!Ctor->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8097 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8098 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8099 | if (Ctor->getInheritedConstructor()) |
| 8100 | return oc_implicit_inherited_constructor; |
| 8101 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8102 | if (Ctor->isDefaultConstructor()) |
| 8103 | return oc_implicit_default_constructor; |
| 8104 | |
| 8105 | if (Ctor->isMoveConstructor()) |
| 8106 | return oc_implicit_move_constructor; |
| 8107 | |
| 8108 | assert(Ctor->isCopyConstructor() && |
| 8109 | "unexpected sort of implicit constructor"); |
| 8110 | return oc_implicit_copy_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8111 | } |
| 8112 | |
| 8113 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 8114 | // This actually gets spelled 'candidate function' for now, but |
| 8115 | // it doesn't hurt to split it out. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8116 | if (!Meth->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8117 | return isTemplate ? oc_method_template : oc_method; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8118 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8119 | if (Meth->isMoveAssignmentOperator()) |
| 8120 | return oc_implicit_move_assignment; |
| 8121 | |
Douglas Gregor | ef7d78b | 2012-02-10 08:36:38 +0000 | [diff] [blame] | 8122 | if (Meth->isCopyAssignmentOperator()) |
| 8123 | return oc_implicit_copy_assignment; |
| 8124 | |
| 8125 | assert(isa<CXXConversionDecl>(Meth) && "expected conversion"); |
| 8126 | return oc_method; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8127 | } |
| 8128 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8129 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8130 | } |
| 8131 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8132 | void MaybeEmitInheritedConstructorNote(Sema &S, Decl *Fn) { |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8133 | const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn); |
| 8134 | if (!Ctor) return; |
| 8135 | |
| 8136 | Ctor = Ctor->getInheritedConstructor(); |
| 8137 | if (!Ctor) return; |
| 8138 | |
| 8139 | S.Diag(Ctor->getLocation(), diag::note_ovl_candidate_inherited_constructor); |
| 8140 | } |
| 8141 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8142 | } // end anonymous namespace |
| 8143 | |
| 8144 | // Notes the location of an overload candidate. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8145 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn, QualType DestType) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8146 | std::string FnDesc; |
| 8147 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8148 | PartialDiagnostic PD = PDiag(diag::note_ovl_candidate) |
| 8149 | << (unsigned) K << FnDesc; |
| 8150 | HandleFunctionTypeMismatch(PD, Fn->getType(), DestType); |
| 8151 | Diag(Fn->getLocation(), PD); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8152 | MaybeEmitInheritedConstructorNote(*this, Fn); |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8153 | } |
| 8154 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8155 | //Notes the location of all overload candidates designated through |
| 8156 | // OverloadedExpr |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8157 | void Sema::NoteAllOverloadCandidates(Expr* OverloadedExpr, QualType DestType) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8158 | assert(OverloadedExpr->getType() == Context.OverloadTy); |
| 8159 | |
| 8160 | OverloadExpr::FindResult Ovl = OverloadExpr::find(OverloadedExpr); |
| 8161 | OverloadExpr *OvlExpr = Ovl.Expression; |
| 8162 | |
| 8163 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 8164 | IEnd = OvlExpr->decls_end(); |
| 8165 | I != IEnd; ++I) { |
| 8166 | if (FunctionTemplateDecl *FunTmpl = |
| 8167 | dyn_cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8168 | NoteOverloadCandidate(FunTmpl->getTemplatedDecl(), DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8169 | } else if (FunctionDecl *Fun |
| 8170 | = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8171 | NoteOverloadCandidate(Fun, DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8172 | } |
| 8173 | } |
| 8174 | } |
| 8175 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8176 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 8177 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 8178 | /// target types of the conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8179 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
| 8180 | Sema &S, |
| 8181 | SourceLocation CaretLoc, |
| 8182 | const PartialDiagnostic &PDiag) const { |
| 8183 | S.Diag(CaretLoc, PDiag) |
| 8184 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
Matt Beaumont-Gay | 45a37da | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 8185 | // FIXME: The note limiting machinery is borrowed from |
| 8186 | // OverloadCandidateSet::NoteCandidates; there's an opportunity for |
| 8187 | // refactoring here. |
| 8188 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
| 8189 | unsigned CandsShown = 0; |
| 8190 | AmbiguousConversionSequence::const_iterator I, E; |
| 8191 | for (I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
| 8192 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
| 8193 | break; |
| 8194 | ++CandsShown; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8195 | S.NoteOverloadCandidate(*I); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8196 | } |
Matt Beaumont-Gay | 45a37da | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 8197 | if (I != E) |
| 8198 | S.Diag(SourceLocation(), diag::note_ovl_too_many_candidates) << int(E - I); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8199 | } |
| 8200 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8201 | namespace { |
| 8202 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8203 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 8204 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 8205 | assert(Conv.isBad()); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8206 | assert(Cand->Function && "for now, candidate must be a function"); |
| 8207 | FunctionDecl *Fn = Cand->Function; |
| 8208 | |
| 8209 | // There's a conversion slot for the object argument if this is a |
| 8210 | // non-constructor method. Note that 'I' corresponds the |
| 8211 | // conversion-slot index. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8212 | bool isObjectArgument = false; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8213 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8214 | if (I == 0) |
| 8215 | isObjectArgument = true; |
| 8216 | else |
| 8217 | I--; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8218 | } |
| 8219 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8220 | std::string FnDesc; |
| 8221 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 8222 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8223 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 8224 | QualType FromTy = Conv.Bad.getFromType(); |
| 8225 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8226 | |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8227 | if (FromTy == S.Context.OverloadTy) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8228 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8229 | Expr *E = FromExpr->IgnoreParens(); |
| 8230 | if (isa<UnaryOperator>(E)) |
| 8231 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 8232 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8233 | |
| 8234 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 8235 | << (unsigned) FnKind << FnDesc |
| 8236 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8237 | << ToTy << Name << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8238 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8239 | return; |
| 8240 | } |
| 8241 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8242 | // Do some hand-waving analysis to see if the non-viability is due |
| 8243 | // to a qualifier mismatch. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8244 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 8245 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 8246 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 8247 | CToTy = RT->getPointeeType(); |
| 8248 | else { |
| 8249 | // TODO: detect and diagnose the full richness of const mismatches. |
| 8250 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 8251 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 8252 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 8253 | } |
| 8254 | |
| 8255 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 8256 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8257 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8258 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8259 | |
| 8260 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 8261 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 8262 | << (unsigned) FnKind << FnDesc |
| 8263 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8264 | << FromTy |
| 8265 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 8266 | << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8267 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8268 | return; |
| 8269 | } |
| 8270 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8271 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 8272 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8273 | << (unsigned) FnKind << FnDesc |
| 8274 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8275 | << FromTy |
| 8276 | << FromQs.getObjCLifetime() << ToQs.getObjCLifetime() |
| 8277 | << (unsigned) isObjectArgument << I+1; |
| 8278 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8279 | return; |
| 8280 | } |
| 8281 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 8282 | if (FromQs.getObjCGCAttr() != ToQs.getObjCGCAttr()) { |
| 8283 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_gc) |
| 8284 | << (unsigned) FnKind << FnDesc |
| 8285 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8286 | << FromTy |
| 8287 | << FromQs.getObjCGCAttr() << ToQs.getObjCGCAttr() |
| 8288 | << (unsigned) isObjectArgument << I+1; |
| 8289 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8290 | return; |
| 8291 | } |
| 8292 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8293 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 8294 | assert(CVR && "unexpected qualifiers mismatch"); |
| 8295 | |
| 8296 | if (isObjectArgument) { |
| 8297 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 8298 | << (unsigned) FnKind << FnDesc |
| 8299 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8300 | << FromTy << (CVR - 1); |
| 8301 | } else { |
| 8302 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 8303 | << (unsigned) FnKind << FnDesc |
| 8304 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8305 | << FromTy << (CVR - 1) << I+1; |
| 8306 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8307 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8308 | return; |
| 8309 | } |
| 8310 | |
Sebastian Redl | fd2a00a | 2011-09-24 17:48:32 +0000 | [diff] [blame] | 8311 | // Special diagnostic for failure to convert an initializer list, since |
| 8312 | // telling the user that it has type void is not useful. |
| 8313 | if (FromExpr && isa<InitListExpr>(FromExpr)) { |
| 8314 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_list_argument) |
| 8315 | << (unsigned) FnKind << FnDesc |
| 8316 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8317 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8318 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8319 | return; |
| 8320 | } |
| 8321 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8322 | // Diagnose references or pointers to incomplete types differently, |
| 8323 | // since it's far from impossible that the incompleteness triggered |
| 8324 | // the failure. |
| 8325 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 8326 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 8327 | TempFromTy = PTy->getPointeeType(); |
| 8328 | if (TempFromTy->isIncompleteType()) { |
| 8329 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 8330 | << (unsigned) FnKind << FnDesc |
| 8331 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8332 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8333 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8334 | return; |
| 8335 | } |
| 8336 | |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8337 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8338 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8339 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 8340 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 8341 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8342 | FromPtrTy->getPointeeType()) && |
| 8343 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 8344 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8345 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8346 | FromPtrTy->getPointeeType())) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8347 | BaseToDerivedConversion = 1; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8348 | } |
| 8349 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 8350 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 8351 | if (const ObjCObjectPointerType *ToPtrTy |
| 8352 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 8353 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 8354 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 8355 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8356 | FromPtrTy->getPointeeType()) && |
| 8357 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8358 | BaseToDerivedConversion = 2; |
| 8359 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8360 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 8361 | !FromTy->isIncompleteType() && |
| 8362 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 8363 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) { |
| 8364 | BaseToDerivedConversion = 3; |
| 8365 | } else if (ToTy->isLValueReferenceType() && !FromExpr->isLValue() && |
| 8366 | ToTy.getNonReferenceType().getCanonicalType() == |
| 8367 | FromTy.getNonReferenceType().getCanonicalType()) { |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8368 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_lvalue) |
| 8369 | << (unsigned) FnKind << FnDesc |
| 8370 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8371 | << (unsigned) isObjectArgument << I + 1; |
| 8372 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8373 | return; |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8374 | } |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8375 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8376 | |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8377 | if (BaseToDerivedConversion) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8378 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8379 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8380 | << (unsigned) FnKind << FnDesc |
| 8381 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8382 | << (BaseToDerivedConversion - 1) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8383 | << FromTy << ToTy << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8384 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8385 | return; |
| 8386 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8387 | |
Fariborz Jahanian | 909bcb3 | 2011-07-20 17:14:09 +0000 | [diff] [blame] | 8388 | if (isa<ObjCObjectPointerType>(CFromTy) && |
| 8389 | isa<PointerType>(CToTy)) { |
| 8390 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8391 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8392 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
| 8393 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_arc_conv) |
| 8394 | << (unsigned) FnKind << FnDesc |
| 8395 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8396 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8397 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8398 | return; |
| 8399 | } |
| 8400 | } |
| 8401 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8402 | // Emit the generic diagnostic and, optionally, add the hints to it. |
| 8403 | PartialDiagnostic FDiag = S.PDiag(diag::note_ovl_candidate_bad_conv); |
| 8404 | FDiag << (unsigned) FnKind << FnDesc |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8405 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8406 | << FromTy << ToTy << (unsigned) isObjectArgument << I + 1 |
| 8407 | << (unsigned) (Cand->Fix.Kind); |
| 8408 | |
| 8409 | // If we can fix the conversion, suggest the FixIts. |
Benjamin Kramer | 1136ef0 | 2012-01-14 21:05:10 +0000 | [diff] [blame] | 8410 | for (std::vector<FixItHint>::iterator HI = Cand->Fix.Hints.begin(), |
| 8411 | HE = Cand->Fix.Hints.end(); HI != HE; ++HI) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8412 | FDiag << *HI; |
| 8413 | S.Diag(Fn->getLocation(), FDiag); |
| 8414 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8415 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8416 | } |
| 8417 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8418 | /// Additional arity mismatch diagnosis specific to a function overload |
| 8419 | /// candidates. This is not covered by the more general DiagnoseArityMismatch() |
| 8420 | /// over a candidate in any candidate set. |
| 8421 | bool CheckArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 8422 | unsigned NumArgs) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8423 | FunctionDecl *Fn = Cand->Function; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8424 | unsigned MinParams = Fn->getMinRequiredArguments(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8425 | |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8426 | // With invalid overloaded operators, it's possible that we think we |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8427 | // 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] | 8428 | // right number of arguments, because only overloaded operators have |
| 8429 | // the weird behavior of overloading member and non-member functions. |
| 8430 | // Just don't report anything. |
| 8431 | if (Fn->isInvalidDecl() && |
| 8432 | Fn->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8433 | return true; |
| 8434 | |
| 8435 | if (NumArgs < MinParams) { |
| 8436 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 8437 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8438 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
| 8439 | } else { |
| 8440 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 8441 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8442 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
| 8443 | } |
| 8444 | |
| 8445 | return false; |
| 8446 | } |
| 8447 | |
| 8448 | /// General arity mismatch diagnosis over a candidate in a candidate set. |
| 8449 | void DiagnoseArityMismatch(Sema &S, Decl *D, unsigned NumFormalArgs) { |
| 8450 | assert(isa<FunctionDecl>(D) && |
| 8451 | "The templated declaration should at least be a function" |
| 8452 | " when diagnosing bad template argument deduction due to too many" |
| 8453 | " or too few arguments"); |
| 8454 | |
| 8455 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| 8456 | |
| 8457 | // TODO: treat calls to a missing default constructor as a special case |
| 8458 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 8459 | unsigned MinParams = Fn->getMinRequiredArguments(); |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8460 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8461 | // at least / at most / exactly |
| 8462 | unsigned mode, modeCount; |
| 8463 | if (NumFormalArgs < MinParams) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8464 | if (MinParams != FnTy->getNumArgs() || |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 8465 | FnTy->isVariadic() || FnTy->isTemplateVariadic()) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8466 | mode = 0; // "at least" |
| 8467 | else |
| 8468 | mode = 2; // "exactly" |
| 8469 | modeCount = MinParams; |
| 8470 | } else { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8471 | if (MinParams != FnTy->getNumArgs()) |
| 8472 | mode = 1; // "at most" |
| 8473 | else |
| 8474 | mode = 2; // "exactly" |
| 8475 | modeCount = FnTy->getNumArgs(); |
| 8476 | } |
| 8477 | |
| 8478 | std::string Description; |
| 8479 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 8480 | |
Richard Smith | f7b8056 | 2012-05-11 05:16:41 +0000 | [diff] [blame] | 8481 | if (modeCount == 1 && Fn->getParamDecl(0)->getDeclName()) |
| 8482 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity_one) |
| 8483 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 8484 | << Fn->getParamDecl(0) << NumFormalArgs; |
| 8485 | else |
| 8486 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
| 8487 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 8488 | << modeCount << NumFormalArgs; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8489 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8490 | } |
| 8491 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8492 | /// Arity mismatch diagnosis specific to a function overload candidate. |
| 8493 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 8494 | unsigned NumFormalArgs) { |
| 8495 | if (!CheckArityMismatch(S, Cand, NumFormalArgs)) |
| 8496 | DiagnoseArityMismatch(S, Cand->Function, NumFormalArgs); |
| 8497 | } |
Larisse Voufo | 8c5d407 | 2013-07-19 22:53:23 +0000 | [diff] [blame] | 8498 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8499 | TemplateDecl *getDescribedTemplate(Decl *Templated) { |
| 8500 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Templated)) |
| 8501 | return FD->getDescribedFunctionTemplate(); |
| 8502 | else if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Templated)) |
| 8503 | return RD->getDescribedClassTemplate(); |
| 8504 | |
| 8505 | llvm_unreachable("Unsupported: Getting the described template declaration" |
| 8506 | " for bad deduction diagnosis"); |
| 8507 | } |
| 8508 | |
| 8509 | /// Diagnose a failed template-argument deduction. |
| 8510 | void DiagnoseBadDeduction(Sema &S, Decl *Templated, |
| 8511 | DeductionFailureInfo &DeductionFailure, |
| 8512 | unsigned NumArgs) { |
| 8513 | TemplateParameter Param = DeductionFailure.getTemplateParameter(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8514 | NamedDecl *ParamD; |
| 8515 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 8516 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 8517 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8518 | switch (DeductionFailure.Result) { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8519 | case Sema::TDK_Success: |
| 8520 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 8521 | |
| 8522 | case Sema::TDK_Incomplete: { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8523 | assert(ParamD && "no parameter found for incomplete deduction result"); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8524 | S.Diag(Templated->getLocation(), |
| 8525 | diag::note_ovl_candidate_incomplete_deduction) |
| 8526 | << ParamD->getDeclName(); |
| 8527 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8528 | return; |
| 8529 | } |
| 8530 | |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8531 | case Sema::TDK_Underqualified: { |
| 8532 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 8533 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 8534 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8535 | QualType Param = DeductionFailure.getFirstArg()->getAsType(); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8536 | |
| 8537 | // Param will have been canonicalized, but it should just be a |
| 8538 | // qualified version of ParamD, so move the qualifiers to that. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 8539 | QualifierCollector Qs; |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8540 | Qs.strip(Param); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 8541 | QualType NonCanonParam = Qs.apply(S.Context, TParam->getTypeForDecl()); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8542 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 8543 | |
| 8544 | // Arg has also been canonicalized, but there's nothing we can do |
| 8545 | // about that. It also doesn't matter as much, because it won't |
| 8546 | // have any template parameters in it (because deduction isn't |
| 8547 | // done on dependent types). |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8548 | QualType Arg = DeductionFailure.getSecondArg()->getAsType(); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8549 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8550 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_underqualified) |
| 8551 | << ParamD->getDeclName() << Arg << NonCanonParam; |
| 8552 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8553 | return; |
| 8554 | } |
| 8555 | |
| 8556 | case Sema::TDK_Inconsistent: { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8557 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8558 | int which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8559 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8560 | which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8561 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8562 | which = 1; |
| 8563 | else { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8564 | which = 2; |
| 8565 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8566 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8567 | S.Diag(Templated->getLocation(), |
| 8568 | diag::note_ovl_candidate_inconsistent_deduction) |
| 8569 | << which << ParamD->getDeclName() << *DeductionFailure.getFirstArg() |
| 8570 | << *DeductionFailure.getSecondArg(); |
| 8571 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8572 | return; |
| 8573 | } |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8574 | |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8575 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8576 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8577 | if (ParamD->getDeclName()) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8578 | S.Diag(Templated->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8579 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8580 | << ParamD->getDeclName(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8581 | else { |
| 8582 | int index = 0; |
| 8583 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 8584 | index = TTP->getIndex(); |
| 8585 | else if (NonTypeTemplateParmDecl *NTTP |
| 8586 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 8587 | index = NTTP->getIndex(); |
| 8588 | else |
| 8589 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8590 | S.Diag(Templated->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8591 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8592 | << (index + 1); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8593 | } |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8594 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8595 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8596 | |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8597 | case Sema::TDK_TooManyArguments: |
| 8598 | case Sema::TDK_TooFewArguments: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8599 | DiagnoseArityMismatch(S, Templated, NumArgs); |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8600 | return; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8601 | |
| 8602 | case Sema::TDK_InstantiationDepth: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8603 | S.Diag(Templated->getLocation(), |
| 8604 | diag::note_ovl_candidate_instantiation_depth); |
| 8605 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8606 | return; |
| 8607 | |
| 8608 | case Sema::TDK_SubstitutionFailure: { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8609 | // Format the template argument list into the argument string. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8610 | SmallString<128> TemplateArgString; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8611 | if (TemplateArgumentList *Args = |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8612 | DeductionFailure.getTemplateArgumentList()) { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8613 | TemplateArgString = " "; |
| 8614 | TemplateArgString += S.getTemplateArgumentBindingsText( |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8615 | getDescribedTemplate(Templated)->getTemplateParameters(), *Args); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8616 | } |
| 8617 | |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8618 | // If this candidate was disabled by enable_if, say so. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8619 | PartialDiagnosticAt *PDiag = DeductionFailure.getSFINAEDiagnostic(); |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8620 | if (PDiag && PDiag->second.getDiagID() == |
| 8621 | diag::err_typename_nested_not_found_enable_if) { |
| 8622 | // FIXME: Use the source range of the condition, and the fully-qualified |
| 8623 | // name of the enable_if template. These are both present in PDiag. |
| 8624 | S.Diag(PDiag->first, diag::note_ovl_candidate_disabled_by_enable_if) |
| 8625 | << "'enable_if'" << TemplateArgString; |
| 8626 | return; |
| 8627 | } |
| 8628 | |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8629 | // Format the SFINAE diagnostic into the argument string. |
| 8630 | // FIXME: Add a general mechanism to include a PartialDiagnostic *'s |
| 8631 | // formatted message in another diagnostic. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8632 | SmallString<128> SFINAEArgString; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8633 | SourceRange R; |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8634 | if (PDiag) { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8635 | SFINAEArgString = ": "; |
| 8636 | R = SourceRange(PDiag->first, PDiag->first); |
| 8637 | PDiag->second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 8638 | } |
| 8639 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8640 | S.Diag(Templated->getLocation(), |
| 8641 | diag::note_ovl_candidate_substitution_failure) |
| 8642 | << TemplateArgString << SFINAEArgString << R; |
| 8643 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8644 | return; |
| 8645 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8646 | |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8647 | case Sema::TDK_FailedOverloadResolution: { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8648 | OverloadExpr::FindResult R = OverloadExpr::find(DeductionFailure.getExpr()); |
| 8649 | S.Diag(Templated->getLocation(), |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8650 | diag::note_ovl_candidate_failed_overload_resolution) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8651 | << R.Expression->getName(); |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8652 | return; |
| 8653 | } |
| 8654 | |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8655 | case Sema::TDK_NonDeducedMismatch: { |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8656 | // FIXME: Provide a source location to indicate what we couldn't match. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8657 | TemplateArgument FirstTA = *DeductionFailure.getFirstArg(); |
| 8658 | TemplateArgument SecondTA = *DeductionFailure.getSecondArg(); |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8659 | if (FirstTA.getKind() == TemplateArgument::Template && |
| 8660 | SecondTA.getKind() == TemplateArgument::Template) { |
| 8661 | TemplateName FirstTN = FirstTA.getAsTemplate(); |
| 8662 | TemplateName SecondTN = SecondTA.getAsTemplate(); |
| 8663 | if (FirstTN.getKind() == TemplateName::Template && |
| 8664 | SecondTN.getKind() == TemplateName::Template) { |
| 8665 | if (FirstTN.getAsTemplateDecl()->getName() == |
| 8666 | SecondTN.getAsTemplateDecl()->getName()) { |
| 8667 | // FIXME: This fixes a bad diagnostic where both templates are named |
| 8668 | // the same. This particular case is a bit difficult since: |
| 8669 | // 1) It is passed as a string to the diagnostic printer. |
| 8670 | // 2) The diagnostic printer only attempts to find a better |
| 8671 | // name for types, not decls. |
| 8672 | // Ideally, this should folded into the diagnostic printer. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8673 | S.Diag(Templated->getLocation(), |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8674 | diag::note_ovl_candidate_non_deduced_mismatch_qualified) |
| 8675 | << FirstTN.getAsTemplateDecl() << SecondTN.getAsTemplateDecl(); |
| 8676 | return; |
| 8677 | } |
| 8678 | } |
| 8679 | } |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8680 | S.Diag(Templated->getLocation(), |
| 8681 | diag::note_ovl_candidate_non_deduced_mismatch) |
| 8682 | << FirstTA << SecondTA; |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8683 | return; |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8684 | } |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8685 | // TODO: diagnose these individually, then kill off |
| 8686 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8687 | case Sema::TDK_MiscellaneousDeductionFailure: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8688 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_bad_deduction); |
| 8689 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8690 | return; |
| 8691 | } |
| 8692 | } |
| 8693 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8694 | /// Diagnose a failed template-argument deduction, for function calls. |
| 8695 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, unsigned NumArgs) { |
| 8696 | unsigned TDK = Cand->DeductionFailure.Result; |
| 8697 | if (TDK == Sema::TDK_TooFewArguments || TDK == Sema::TDK_TooManyArguments) { |
| 8698 | if (CheckArityMismatch(S, Cand, NumArgs)) |
| 8699 | return; |
| 8700 | } |
| 8701 | DiagnoseBadDeduction(S, Cand->Function, // pattern |
| 8702 | Cand->DeductionFailure, NumArgs); |
| 8703 | } |
| 8704 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8705 | /// CUDA: diagnose an invalid call across targets. |
| 8706 | void DiagnoseBadTarget(Sema &S, OverloadCandidate *Cand) { |
| 8707 | FunctionDecl *Caller = cast<FunctionDecl>(S.CurContext); |
| 8708 | FunctionDecl *Callee = Cand->Function; |
| 8709 | |
| 8710 | Sema::CUDAFunctionTarget CallerTarget = S.IdentifyCUDATarget(Caller), |
| 8711 | CalleeTarget = S.IdentifyCUDATarget(Callee); |
| 8712 | |
| 8713 | std::string FnDesc; |
| 8714 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Callee, FnDesc); |
| 8715 | |
| 8716 | S.Diag(Callee->getLocation(), diag::note_ovl_candidate_bad_target) |
| 8717 | << (unsigned) FnKind << CalleeTarget << CallerTarget; |
| 8718 | } |
| 8719 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8720 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 8721 | /// already generated a primary error at the call site. |
| 8722 | /// |
| 8723 | /// It really does need to be a single diagnostic with its caret |
| 8724 | /// pointed at the candidate declaration. Yes, this creates some |
| 8725 | /// major challenges of technical writing. Yes, this makes pointing |
| 8726 | /// out problems with specific arguments quite awkward. It's still |
| 8727 | /// better than generating twenty screens of text for every failed |
| 8728 | /// overload. |
| 8729 | /// |
| 8730 | /// It would be great to be able to express per-candidate problems |
| 8731 | /// more richly for those diagnostic clients that cared, but we'd |
| 8732 | /// still have to be just as careful with the default diagnostics. |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8733 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8734 | unsigned NumArgs) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8735 | FunctionDecl *Fn = Cand->Function; |
| 8736 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8737 | // Note deleted candidates, but only if they're viable. |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 8738 | if (Cand->Viable && (Fn->isDeleted() || |
| 8739 | S.isFunctionConsideredUnavailable(Fn))) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8740 | std::string FnDesc; |
| 8741 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8742 | |
| 8743 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 8744 | << FnKind << FnDesc |
| 8745 | << (Fn->isDeleted() ? (Fn->isDeletedAsWritten() ? 1 : 2) : 0); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8746 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8747 | return; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8748 | } |
| 8749 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8750 | // We don't really have anything else to say about viable candidates. |
| 8751 | if (Cand->Viable) { |
| 8752 | S.NoteOverloadCandidate(Fn); |
| 8753 | return; |
| 8754 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8755 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8756 | switch (Cand->FailureKind) { |
| 8757 | case ovl_fail_too_many_arguments: |
| 8758 | case ovl_fail_too_few_arguments: |
| 8759 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8760 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8761 | case ovl_fail_bad_deduction: |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8762 | return DiagnoseBadDeduction(S, Cand, NumArgs); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8763 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8764 | case ovl_fail_trivial_conversion: |
| 8765 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 8766 | case ovl_fail_final_conversion_not_exact: |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8767 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8768 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8769 | case ovl_fail_bad_conversion: { |
| 8770 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8771 | for (unsigned N = Cand->NumConversions; I != N; ++I) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8772 | if (Cand->Conversions[I].isBad()) |
| 8773 | return DiagnoseBadConversion(S, Cand, I); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8774 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8775 | // FIXME: this currently happens when we're called from SemaInit |
| 8776 | // when user-conversion overload fails. Figure out how to handle |
| 8777 | // those conditions and diagnose them well. |
| 8778 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8779 | } |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8780 | |
| 8781 | case ovl_fail_bad_target: |
| 8782 | return DiagnoseBadTarget(S, Cand); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8783 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8784 | } |
| 8785 | |
| 8786 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 8787 | // Desugar the type of the surrogate down to a function type, |
| 8788 | // retaining as many typedefs as possible while still showing |
| 8789 | // the function type (and, therefore, its parameter types). |
| 8790 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 8791 | bool isLValueReference = false; |
| 8792 | bool isRValueReference = false; |
| 8793 | bool isPointer = false; |
| 8794 | if (const LValueReferenceType *FnTypeRef = |
| 8795 | FnType->getAs<LValueReferenceType>()) { |
| 8796 | FnType = FnTypeRef->getPointeeType(); |
| 8797 | isLValueReference = true; |
| 8798 | } else if (const RValueReferenceType *FnTypeRef = |
| 8799 | FnType->getAs<RValueReferenceType>()) { |
| 8800 | FnType = FnTypeRef->getPointeeType(); |
| 8801 | isRValueReference = true; |
| 8802 | } |
| 8803 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 8804 | FnType = FnTypePtr->getPointeeType(); |
| 8805 | isPointer = true; |
| 8806 | } |
| 8807 | // Desugar down to a function type. |
| 8808 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 8809 | // Reconstruct the pointer/reference as appropriate. |
| 8810 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 8811 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 8812 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 8813 | |
| 8814 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 8815 | << FnType; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8816 | MaybeEmitInheritedConstructorNote(S, Cand->Surrogate); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8817 | } |
| 8818 | |
| 8819 | void NoteBuiltinOperatorCandidate(Sema &S, |
David Blaikie | 0bea863 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 8820 | StringRef Opc, |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8821 | SourceLocation OpLoc, |
| 8822 | OverloadCandidate *Cand) { |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8823 | assert(Cand->NumConversions <= 2 && "builtin operator is not binary"); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8824 | std::string TypeStr("operator"); |
| 8825 | TypeStr += Opc; |
| 8826 | TypeStr += "("; |
| 8827 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8828 | if (Cand->NumConversions == 1) { |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8829 | TypeStr += ")"; |
| 8830 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 8831 | } else { |
| 8832 | TypeStr += ", "; |
| 8833 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 8834 | TypeStr += ")"; |
| 8835 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 8836 | } |
| 8837 | } |
| 8838 | |
| 8839 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 8840 | OverloadCandidate *Cand) { |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8841 | unsigned NoOperands = Cand->NumConversions; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8842 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 8843 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8844 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 8845 | if (!ICS.isAmbiguous()) continue; |
| 8846 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8847 | ICS.DiagnoseAmbiguousConversion(S, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8848 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8849 | } |
| 8850 | } |
| 8851 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8852 | static SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8853 | if (Cand->Function) |
| 8854 | return Cand->Function->getLocation(); |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 8855 | if (Cand->IsSurrogate) |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8856 | return Cand->Surrogate->getLocation(); |
| 8857 | return SourceLocation(); |
| 8858 | } |
| 8859 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8860 | static unsigned RankDeductionFailure(const DeductionFailureInfo &DFI) { |
Chandler Carruth | 78bf680 | 2011-09-10 00:51:24 +0000 | [diff] [blame] | 8861 | switch ((Sema::TemplateDeductionResult)DFI.Result) { |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8862 | case Sema::TDK_Success: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8863 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8864 | |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 8865 | case Sema::TDK_Invalid: |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8866 | case Sema::TDK_Incomplete: |
| 8867 | return 1; |
| 8868 | |
| 8869 | case Sema::TDK_Underqualified: |
| 8870 | case Sema::TDK_Inconsistent: |
| 8871 | return 2; |
| 8872 | |
| 8873 | case Sema::TDK_SubstitutionFailure: |
| 8874 | case Sema::TDK_NonDeducedMismatch: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8875 | case Sema::TDK_MiscellaneousDeductionFailure: |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8876 | return 3; |
| 8877 | |
| 8878 | case Sema::TDK_InstantiationDepth: |
| 8879 | case Sema::TDK_FailedOverloadResolution: |
| 8880 | return 4; |
| 8881 | |
| 8882 | case Sema::TDK_InvalidExplicitArguments: |
| 8883 | return 5; |
| 8884 | |
| 8885 | case Sema::TDK_TooManyArguments: |
| 8886 | case Sema::TDK_TooFewArguments: |
| 8887 | return 6; |
| 8888 | } |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8889 | llvm_unreachable("Unhandled deduction result"); |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8890 | } |
| 8891 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8892 | struct CompareOverloadCandidatesForDisplay { |
| 8893 | Sema &S; |
| 8894 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8895 | |
| 8896 | bool operator()(const OverloadCandidate *L, |
| 8897 | const OverloadCandidate *R) { |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 8898 | // Fast-path this check. |
| 8899 | if (L == R) return false; |
| 8900 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8901 | // Order first by viability. |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8902 | if (L->Viable) { |
| 8903 | if (!R->Viable) return true; |
| 8904 | |
| 8905 | // TODO: introduce a tri-valued comparison for overload |
| 8906 | // candidates. Would be more worthwhile if we had a sort |
| 8907 | // that could exploit it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8908 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation())) return true; |
| 8909 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation())) return false; |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8910 | } else if (R->Viable) |
| 8911 | return false; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8912 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8913 | assert(L->Viable == R->Viable); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8914 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8915 | // Criteria by which we can sort non-viable candidates: |
| 8916 | if (!L->Viable) { |
| 8917 | // 1. Arity mismatches come after other candidates. |
| 8918 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 8919 | L->FailureKind == ovl_fail_too_few_arguments) |
| 8920 | return false; |
| 8921 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 8922 | R->FailureKind == ovl_fail_too_few_arguments) |
| 8923 | return true; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8924 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8925 | // 2. Bad conversions come first and are ordered by the number |
| 8926 | // of bad conversions and quality of good conversions. |
| 8927 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 8928 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 8929 | return true; |
| 8930 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8931 | // The conversion that can be fixed with a smaller number of changes, |
| 8932 | // comes first. |
| 8933 | unsigned numLFixes = L->Fix.NumConversionsFixed; |
| 8934 | unsigned numRFixes = R->Fix.NumConversionsFixed; |
| 8935 | numLFixes = (numLFixes == 0) ? UINT_MAX : numLFixes; |
| 8936 | numRFixes = (numRFixes == 0) ? UINT_MAX : numRFixes; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 8937 | if (numLFixes != numRFixes) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8938 | if (numLFixes < numRFixes) |
| 8939 | return true; |
| 8940 | else |
| 8941 | return false; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 8942 | } |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8943 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8944 | // If there's any ordering between the defined conversions... |
| 8945 | // FIXME: this might not be transitive. |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8946 | assert(L->NumConversions == R->NumConversions); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8947 | |
| 8948 | int leftBetter = 0; |
John McCall | 3a81337 | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 8949 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8950 | for (unsigned E = L->NumConversions; I != E; ++I) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8951 | switch (CompareImplicitConversionSequences(S, |
| 8952 | L->Conversions[I], |
| 8953 | R->Conversions[I])) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8954 | case ImplicitConversionSequence::Better: |
| 8955 | leftBetter++; |
| 8956 | break; |
| 8957 | |
| 8958 | case ImplicitConversionSequence::Worse: |
| 8959 | leftBetter--; |
| 8960 | break; |
| 8961 | |
| 8962 | case ImplicitConversionSequence::Indistinguishable: |
| 8963 | break; |
| 8964 | } |
| 8965 | } |
| 8966 | if (leftBetter > 0) return true; |
| 8967 | if (leftBetter < 0) return false; |
| 8968 | |
| 8969 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 8970 | return false; |
| 8971 | |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8972 | if (L->FailureKind == ovl_fail_bad_deduction) { |
| 8973 | if (R->FailureKind != ovl_fail_bad_deduction) |
| 8974 | return true; |
| 8975 | |
| 8976 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 8977 | return RankDeductionFailure(L->DeductionFailure) |
Eli Friedman | ce1846e | 2011-10-14 23:10:30 +0000 | [diff] [blame] | 8978 | < RankDeductionFailure(R->DeductionFailure); |
Eli Friedman | 1c522f7 | 2011-10-14 21:52:24 +0000 | [diff] [blame] | 8979 | } else if (R->FailureKind == ovl_fail_bad_deduction) |
| 8980 | return false; |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8981 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8982 | // TODO: others? |
| 8983 | } |
| 8984 | |
| 8985 | // Sort everything else by location. |
| 8986 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 8987 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 8988 | |
| 8989 | // Put candidates without locations (e.g. builtins) at the end. |
| 8990 | if (LLoc.isInvalid()) return false; |
| 8991 | if (RLoc.isInvalid()) return true; |
| 8992 | |
| 8993 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8994 | } |
| 8995 | }; |
| 8996 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8997 | /// CompleteNonViableCandidate - Normally, overload resolution only |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8998 | /// computes up to the first. Produces the FixIt set if possible. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8999 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9000 | ArrayRef<Expr *> Args) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9001 | assert(!Cand->Viable); |
| 9002 | |
| 9003 | // Don't do anything on failures other than bad conversion. |
| 9004 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 9005 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9006 | // We only want the FixIts if all the arguments can be corrected. |
| 9007 | bool Unfixable = false; |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9008 | // Use a implicit copy initialization to check conversion fixes. |
| 9009 | Cand->Fix.setConversionChecker(TryCopyInitialization); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9010 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9011 | // Skip forward to the first bad conversion. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9012 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9013 | unsigned ConvCount = Cand->NumConversions; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9014 | while (true) { |
| 9015 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 9016 | ConvIdx++; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9017 | if (Cand->Conversions[ConvIdx - 1].isBad()) { |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9018 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx - 1, S); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9019 | break; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9020 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9021 | } |
| 9022 | |
| 9023 | if (ConvIdx == ConvCount) |
| 9024 | return; |
| 9025 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9026 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 9027 | "remaining conversion is initialized?"); |
| 9028 | |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 9029 | // FIXME: this should probably be preserved from the overload |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9030 | // operation somehow. |
| 9031 | bool SuppressUserConversions = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9032 | |
| 9033 | const FunctionProtoType* Proto; |
| 9034 | unsigned ArgIdx = ConvIdx; |
| 9035 | |
| 9036 | if (Cand->IsSurrogate) { |
| 9037 | QualType ConvType |
| 9038 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 9039 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 9040 | ConvType = ConvPtrType->getPointeeType(); |
| 9041 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 9042 | ArgIdx--; |
| 9043 | } else if (Cand->Function) { |
| 9044 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 9045 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 9046 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 9047 | ArgIdx--; |
| 9048 | } else { |
| 9049 | // Builtin binary operator with a bad first conversion. |
| 9050 | assert(ConvCount <= 3); |
| 9051 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 9052 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 9053 | = TryCopyInitialization(S, Args[ConvIdx], |
| 9054 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9055 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9056 | /*InOverloadResolution*/ true, |
| 9057 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9058 | S.getLangOpts().ObjCAutoRefCount); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9059 | return; |
| 9060 | } |
| 9061 | |
| 9062 | // Fill in the rest of the conversions. |
| 9063 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 9064 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9065 | if (ArgIdx < NumArgsInProto) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9066 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 9067 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9068 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9069 | /*InOverloadResolution=*/true, |
| 9070 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9071 | S.getLangOpts().ObjCAutoRefCount); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9072 | // Store the FixIt in the candidate if it exists. |
| 9073 | if (!Unfixable && Cand->Conversions[ConvIdx].isBad()) |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9074 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx, S); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9075 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9076 | else |
| 9077 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 9078 | } |
| 9079 | } |
| 9080 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9081 | } // end anonymous namespace |
| 9082 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9083 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 9084 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9085 | /// set. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9086 | void OverloadCandidateSet::NoteCandidates(Sema &S, |
| 9087 | OverloadCandidateDisplayKind OCD, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9088 | ArrayRef<Expr *> Args, |
David Blaikie | 0bea863 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 9089 | StringRef Opc, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9090 | SourceLocation OpLoc) { |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9091 | // Sort the candidates by viability and position. Sorting directly would |
| 9092 | // be prohibitive, so we make a set of pointers and sort those. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9093 | SmallVector<OverloadCandidate*, 32> Cands; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9094 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
| 9095 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9096 | if (Cand->Viable) |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9097 | Cands.push_back(Cand); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9098 | else if (OCD == OCD_AllCandidates) { |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9099 | CompleteNonViableCandidate(S, Cand, Args); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9100 | if (Cand->Function || Cand->IsSurrogate) |
| 9101 | Cands.push_back(Cand); |
| 9102 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 9103 | // want to list every possible builtin candidate. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9104 | } |
| 9105 | } |
| 9106 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 9107 | std::sort(Cands.begin(), Cands.end(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9108 | CompareOverloadCandidatesForDisplay(S)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9109 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9110 | bool ReportedAmbiguousConversions = false; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9111 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9112 | SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 9113 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9114 | unsigned CandsShown = 0; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9115 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 9116 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 9117 | |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9118 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 9119 | // the user with. FIXME: This limit should depend on details of the |
| 9120 | // candidate list. |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 9121 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) { |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9122 | break; |
| 9123 | } |
| 9124 | ++CandsShown; |
| 9125 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9126 | if (Cand->Function) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9127 | NoteFunctionCandidate(S, Cand, Args.size()); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9128 | else if (Cand->IsSurrogate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9129 | NoteSurrogateCandidate(S, Cand); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9130 | else { |
| 9131 | assert(Cand->Viable && |
| 9132 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9133 | // Generally we only see ambiguities including viable builtin |
| 9134 | // operators if overload resolution got screwed up by an |
| 9135 | // ambiguous user-defined conversion. |
| 9136 | // |
| 9137 | // FIXME: It's quite possible for different conversions to see |
| 9138 | // different ambiguities, though. |
| 9139 | if (!ReportedAmbiguousConversions) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9140 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9141 | ReportedAmbiguousConversions = true; |
| 9142 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9143 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9144 | // If this is a viable builtin, print it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9145 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 9146 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9147 | } |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9148 | |
| 9149 | if (I != E) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9150 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9151 | } |
| 9152 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9153 | static SourceLocation |
| 9154 | GetLocationForCandidate(const TemplateSpecCandidate *Cand) { |
| 9155 | return Cand->Specialization ? Cand->Specialization->getLocation() |
| 9156 | : SourceLocation(); |
| 9157 | } |
| 9158 | |
| 9159 | struct CompareTemplateSpecCandidatesForDisplay { |
| 9160 | Sema &S; |
| 9161 | CompareTemplateSpecCandidatesForDisplay(Sema &S) : S(S) {} |
| 9162 | |
| 9163 | bool operator()(const TemplateSpecCandidate *L, |
| 9164 | const TemplateSpecCandidate *R) { |
| 9165 | // Fast-path this check. |
| 9166 | if (L == R) |
| 9167 | return false; |
| 9168 | |
| 9169 | // Assuming that both candidates are not matches... |
| 9170 | |
| 9171 | // Sort by the ranking of deduction failures. |
| 9172 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 9173 | return RankDeductionFailure(L->DeductionFailure) < |
| 9174 | RankDeductionFailure(R->DeductionFailure); |
| 9175 | |
| 9176 | // Sort everything else by location. |
| 9177 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 9178 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 9179 | |
| 9180 | // Put candidates without locations (e.g. builtins) at the end. |
| 9181 | if (LLoc.isInvalid()) |
| 9182 | return false; |
| 9183 | if (RLoc.isInvalid()) |
| 9184 | return true; |
| 9185 | |
| 9186 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
| 9187 | } |
| 9188 | }; |
| 9189 | |
| 9190 | /// Diagnose a template argument deduction failure. |
| 9191 | /// We are treating these failures as overload failures due to bad |
| 9192 | /// deductions. |
| 9193 | void TemplateSpecCandidate::NoteDeductionFailure(Sema &S) { |
| 9194 | DiagnoseBadDeduction(S, Specialization, // pattern |
| 9195 | DeductionFailure, /*NumArgs=*/0); |
| 9196 | } |
| 9197 | |
| 9198 | void TemplateSpecCandidateSet::destroyCandidates() { |
| 9199 | for (iterator i = begin(), e = end(); i != e; ++i) { |
| 9200 | i->DeductionFailure.Destroy(); |
| 9201 | } |
| 9202 | } |
| 9203 | |
| 9204 | void TemplateSpecCandidateSet::clear() { |
| 9205 | destroyCandidates(); |
| 9206 | Candidates.clear(); |
| 9207 | } |
| 9208 | |
| 9209 | /// NoteCandidates - When no template specialization match is found, prints |
| 9210 | /// diagnostic messages containing the non-matching specializations that form |
| 9211 | /// the candidate set. |
| 9212 | /// This is analoguous to OverloadCandidateSet::NoteCandidates() with |
| 9213 | /// OCD == OCD_AllCandidates and Cand->Viable == false. |
| 9214 | void TemplateSpecCandidateSet::NoteCandidates(Sema &S, SourceLocation Loc) { |
| 9215 | // Sort the candidates by position (assuming no candidate is a match). |
| 9216 | // Sorting directly would be prohibitive, so we make a set of pointers |
| 9217 | // and sort those. |
| 9218 | SmallVector<TemplateSpecCandidate *, 32> Cands; |
| 9219 | Cands.reserve(size()); |
| 9220 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
| 9221 | if (Cand->Specialization) |
| 9222 | Cands.push_back(Cand); |
| 9223 | // Otherwise, this is a non matching builtin candidate. We do not, |
| 9224 | // in general, want to list every possible builtin candidate. |
| 9225 | } |
| 9226 | |
| 9227 | std::sort(Cands.begin(), Cands.end(), |
| 9228 | CompareTemplateSpecCandidatesForDisplay(S)); |
| 9229 | |
| 9230 | // FIXME: Perhaps rename OverloadsShown and getShowOverloads() |
| 9231 | // for generalization purposes (?). |
| 9232 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
| 9233 | |
| 9234 | SmallVectorImpl<TemplateSpecCandidate *>::iterator I, E; |
| 9235 | unsigned CandsShown = 0; |
| 9236 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 9237 | TemplateSpecCandidate *Cand = *I; |
| 9238 | |
| 9239 | // Set an arbitrary limit on the number of candidates we'll spam |
| 9240 | // the user with. FIXME: This limit should depend on details of the |
| 9241 | // candidate list. |
| 9242 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
| 9243 | break; |
| 9244 | ++CandsShown; |
| 9245 | |
| 9246 | assert(Cand->Specialization && |
| 9247 | "Non-matching built-in candidates are not added to Cands."); |
| 9248 | Cand->NoteDeductionFailure(S); |
| 9249 | } |
| 9250 | |
| 9251 | if (I != E) |
| 9252 | S.Diag(Loc, diag::note_ovl_too_many_candidates) << int(E - I); |
| 9253 | } |
| 9254 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9255 | // [PossiblyAFunctionType] --> [Return] |
| 9256 | // NonFunctionType --> NonFunctionType |
| 9257 | // R (A) --> R(A) |
| 9258 | // R (*)(A) --> R (A) |
| 9259 | // R (&)(A) --> R (A) |
| 9260 | // R (S::*)(A) --> R (A) |
| 9261 | QualType Sema::ExtractUnqualifiedFunctionType(QualType PossiblyAFunctionType) { |
| 9262 | QualType Ret = PossiblyAFunctionType; |
| 9263 | if (const PointerType *ToTypePtr = |
| 9264 | PossiblyAFunctionType->getAs<PointerType>()) |
| 9265 | Ret = ToTypePtr->getPointeeType(); |
| 9266 | else if (const ReferenceType *ToTypeRef = |
| 9267 | PossiblyAFunctionType->getAs<ReferenceType>()) |
| 9268 | Ret = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 9269 | else if (const MemberPointerType *MemTypePtr = |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9270 | PossiblyAFunctionType->getAs<MemberPointerType>()) |
| 9271 | Ret = MemTypePtr->getPointeeType(); |
| 9272 | Ret = |
| 9273 | Context.getCanonicalType(Ret).getUnqualifiedType(); |
| 9274 | return Ret; |
| 9275 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9276 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9277 | // A helper class to help with address of function resolution |
| 9278 | // - allows us to avoid passing around all those ugly parameters |
| 9279 | class AddressOfFunctionResolver |
| 9280 | { |
| 9281 | Sema& S; |
| 9282 | Expr* SourceExpr; |
| 9283 | const QualType& TargetType; |
| 9284 | QualType TargetFunctionType; // Extracted function type from target type |
| 9285 | |
| 9286 | bool Complain; |
| 9287 | //DeclAccessPair& ResultFunctionAccessPair; |
| 9288 | ASTContext& Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9289 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9290 | bool TargetTypeIsNonStaticMemberFunction; |
| 9291 | bool FoundNonTemplateFunction; |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9292 | bool StaticMemberFunctionFromBoundPointer; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9293 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9294 | OverloadExpr::FindResult OvlExprInfo; |
| 9295 | OverloadExpr *OvlExpr; |
| 9296 | TemplateArgumentListInfo OvlExplicitTemplateArgs; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9297 | SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9298 | TemplateSpecCandidateSet FailedCandidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9299 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9300 | public: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9301 | AddressOfFunctionResolver(Sema &S, Expr *SourceExpr, |
| 9302 | const QualType &TargetType, bool Complain) |
| 9303 | : S(S), SourceExpr(SourceExpr), TargetType(TargetType), |
| 9304 | Complain(Complain), Context(S.getASTContext()), |
| 9305 | TargetTypeIsNonStaticMemberFunction( |
| 9306 | !!TargetType->getAs<MemberPointerType>()), |
| 9307 | FoundNonTemplateFunction(false), |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9308 | StaticMemberFunctionFromBoundPointer(false), |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9309 | OvlExprInfo(OverloadExpr::find(SourceExpr)), |
| 9310 | OvlExpr(OvlExprInfo.Expression), |
| 9311 | FailedCandidates(OvlExpr->getNameLoc()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9312 | ExtractUnqualifiedFunctionTypeFromTargetType(); |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9313 | |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9314 | if (TargetFunctionType->isFunctionType()) { |
| 9315 | if (UnresolvedMemberExpr *UME = dyn_cast<UnresolvedMemberExpr>(OvlExpr)) |
| 9316 | if (!UME->isImplicitAccess() && |
| 9317 | !S.ResolveSingleFunctionTemplateSpecialization(UME)) |
| 9318 | StaticMemberFunctionFromBoundPointer = true; |
| 9319 | } else if (OvlExpr->hasExplicitTemplateArgs()) { |
| 9320 | DeclAccessPair dap; |
| 9321 | if (FunctionDecl *Fn = S.ResolveSingleFunctionTemplateSpecialization( |
| 9322 | OvlExpr, false, &dap)) { |
| 9323 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
| 9324 | if (!Method->isStatic()) { |
| 9325 | // If the target type is a non-function type and the function found |
| 9326 | // is a non-static member function, pretend as if that was the |
| 9327 | // target, it's the only possible type to end up with. |
| 9328 | TargetTypeIsNonStaticMemberFunction = true; |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9329 | |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9330 | // And skip adding the function if its not in the proper form. |
| 9331 | // We'll diagnose this due to an empty set of functions. |
| 9332 | if (!OvlExprInfo.HasFormOfMemberPointer) |
| 9333 | return; |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9334 | } |
| 9335 | |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9336 | Matches.push_back(std::make_pair(dap, Fn)); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 9337 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9338 | return; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 9339 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9340 | |
| 9341 | if (OvlExpr->hasExplicitTemplateArgs()) |
| 9342 | OvlExpr->getExplicitTemplateArgs().copyInto(OvlExplicitTemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9343 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9344 | if (FindAllFunctionsThatMatchTargetTypeExactly()) { |
| 9345 | // C++ [over.over]p4: |
| 9346 | // If more than one function is selected, [...] |
| 9347 | if (Matches.size() > 1) { |
| 9348 | if (FoundNonTemplateFunction) |
| 9349 | EliminateAllTemplateMatches(); |
| 9350 | else |
| 9351 | EliminateAllExceptMostSpecializedTemplate(); |
| 9352 | } |
| 9353 | } |
| 9354 | } |
| 9355 | |
| 9356 | private: |
| 9357 | bool isTargetTypeAFunction() const { |
| 9358 | return TargetFunctionType->isFunctionType(); |
| 9359 | } |
| 9360 | |
| 9361 | // [ToType] [Return] |
| 9362 | |
| 9363 | // R (*)(A) --> R (A), IsNonStaticMemberFunction = false |
| 9364 | // R (&)(A) --> R (A), IsNonStaticMemberFunction = false |
| 9365 | // R (S::*)(A) --> R (A), IsNonStaticMemberFunction = true |
| 9366 | void inline ExtractUnqualifiedFunctionTypeFromTargetType() { |
| 9367 | TargetFunctionType = S.ExtractUnqualifiedFunctionType(TargetType); |
| 9368 | } |
| 9369 | |
| 9370 | // return true if any matching specializations were found |
| 9371 | bool AddMatchingTemplateFunction(FunctionTemplateDecl* FunctionTemplate, |
| 9372 | const DeclAccessPair& CurAccessFunPair) { |
| 9373 | if (CXXMethodDecl *Method |
| 9374 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
| 9375 | // Skip non-static function templates when converting to pointer, and |
| 9376 | // static when converting to member pointer. |
| 9377 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 9378 | return false; |
| 9379 | } |
| 9380 | else if (TargetTypeIsNonStaticMemberFunction) |
| 9381 | return false; |
| 9382 | |
| 9383 | // C++ [over.over]p2: |
| 9384 | // If the name is a function template, template argument deduction is |
| 9385 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 9386 | // resulting template argument list is used to generate a single |
| 9387 | // function template specialization, which is added to the set of |
| 9388 | // overloaded functions considered. |
| 9389 | FunctionDecl *Specialization = 0; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9390 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9391 | if (Sema::TemplateDeductionResult Result |
| 9392 | = S.DeduceTemplateArguments(FunctionTemplate, |
| 9393 | &OvlExplicitTemplateArgs, |
| 9394 | TargetFunctionType, Specialization, |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9395 | Info, /*InOverloadResolution=*/true)) { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9396 | // Make a note of the failed deduction for diagnostics. |
| 9397 | FailedCandidates.addCandidate() |
| 9398 | .set(FunctionTemplate->getTemplatedDecl(), |
| 9399 | MakeDeductionFailureInfo(Context, Result, Info)); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9400 | return false; |
| 9401 | } |
| 9402 | |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9403 | // Template argument deduction ensures that we have an exact match or |
| 9404 | // compatible pointer-to-function arguments that would be adjusted by ICS. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9405 | // This function template specicalization works. |
| 9406 | Specialization = cast<FunctionDecl>(Specialization->getCanonicalDecl()); |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9407 | assert(S.isSameOrCompatibleFunctionType( |
| 9408 | Context.getCanonicalType(Specialization->getType()), |
| 9409 | Context.getCanonicalType(TargetFunctionType))); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9410 | Matches.push_back(std::make_pair(CurAccessFunPair, Specialization)); |
| 9411 | return true; |
| 9412 | } |
| 9413 | |
| 9414 | bool AddMatchingNonTemplateFunction(NamedDecl* Fn, |
| 9415 | const DeclAccessPair& CurAccessFunPair) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9416 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 9417 | // Skip non-static functions when converting to pointer, and static |
| 9418 | // when converting to member pointer. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9419 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 9420 | return false; |
| 9421 | } |
| 9422 | else if (TargetTypeIsNonStaticMemberFunction) |
| 9423 | return false; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9424 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9425 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9426 | if (S.getLangOpts().CUDA) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 9427 | if (FunctionDecl *Caller = dyn_cast<FunctionDecl>(S.CurContext)) |
| 9428 | if (S.CheckCUDATarget(Caller, FunDecl)) |
| 9429 | return false; |
| 9430 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9431 | // If any candidate has a placeholder return type, trigger its deduction |
| 9432 | // now. |
| 9433 | if (S.getLangOpts().CPlusPlus1y && |
| 9434 | FunDecl->getResultType()->isUndeducedType() && |
| 9435 | S.DeduceReturnType(FunDecl, SourceExpr->getLocStart(), Complain)) |
| 9436 | return false; |
| 9437 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 9438 | QualType ResultTy; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9439 | if (Context.hasSameUnqualifiedType(TargetFunctionType, |
| 9440 | FunDecl->getType()) || |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 9441 | S.IsNoReturnConversion(FunDecl->getType(), TargetFunctionType, |
| 9442 | ResultTy)) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9443 | Matches.push_back(std::make_pair(CurAccessFunPair, |
| 9444 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 9445 | FoundNonTemplateFunction = true; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9446 | return true; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 9447 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9448 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9449 | |
| 9450 | return false; |
| 9451 | } |
| 9452 | |
| 9453 | bool FindAllFunctionsThatMatchTargetTypeExactly() { |
| 9454 | bool Ret = false; |
| 9455 | |
| 9456 | // If the overload expression doesn't have the form of a pointer to |
| 9457 | // member, don't try to convert it to a pointer-to-member type. |
| 9458 | if (IsInvalidFormOfPointerToMemberFunction()) |
| 9459 | return false; |
| 9460 | |
| 9461 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 9462 | E = OvlExpr->decls_end(); |
| 9463 | I != E; ++I) { |
| 9464 | // Look through any using declarations to find the underlying function. |
| 9465 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 9466 | |
| 9467 | // C++ [over.over]p3: |
| 9468 | // Non-member functions and static member functions match |
| 9469 | // targets of type "pointer-to-function" or "reference-to-function." |
| 9470 | // Nonstatic member functions match targets of |
| 9471 | // type "pointer-to-member-function." |
| 9472 | // Note that according to DR 247, the containing class does not matter. |
| 9473 | if (FunctionTemplateDecl *FunctionTemplate |
| 9474 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
| 9475 | if (AddMatchingTemplateFunction(FunctionTemplate, I.getPair())) |
| 9476 | Ret = true; |
| 9477 | } |
| 9478 | // If we have explicit template arguments supplied, skip non-templates. |
| 9479 | else if (!OvlExpr->hasExplicitTemplateArgs() && |
| 9480 | AddMatchingNonTemplateFunction(Fn, I.getPair())) |
| 9481 | Ret = true; |
| 9482 | } |
| 9483 | assert(Ret || Matches.empty()); |
| 9484 | return Ret; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9485 | } |
| 9486 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9487 | void EliminateAllExceptMostSpecializedTemplate() { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 9488 | // [...] and any given function template specialization F1 is |
| 9489 | // eliminated if the set contains a second function template |
| 9490 | // specialization whose function template is more specialized |
| 9491 | // than the function template of F1 according to the partial |
| 9492 | // ordering rules of 14.5.5.2. |
| 9493 | |
| 9494 | // The algorithm specified above is quadratic. We instead use a |
| 9495 | // two-pass algorithm (similar to the one used to identify the |
| 9496 | // best viable function in an overload set) that identifies the |
| 9497 | // best function template (if it exists). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9498 | |
| 9499 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 9500 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 9501 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9502 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9503 | // TODO: It looks like FailedCandidates does not serve much purpose |
| 9504 | // here, since the no_viable diagnostic has index 0. |
| 9505 | UnresolvedSetIterator Result = S.getMostSpecialized( |
| 9506 | MatchesCopy.begin(), MatchesCopy.end(), FailedCandidates, TPOC_Other, 0, |
| 9507 | SourceExpr->getLocStart(), S.PDiag(), |
| 9508 | S.PDiag(diag::err_addr_ovl_ambiguous) << Matches[0] |
| 9509 | .second->getDeclName(), |
| 9510 | S.PDiag(diag::note_ovl_candidate) << (unsigned)oc_function_template, |
| 9511 | Complain, TargetFunctionType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9512 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9513 | if (Result != MatchesCopy.end()) { |
| 9514 | // Make it the first and only element |
| 9515 | Matches[0].first = Matches[Result - MatchesCopy.begin()].first; |
| 9516 | Matches[0].second = cast<FunctionDecl>(*Result); |
| 9517 | Matches.resize(1); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9518 | } |
| 9519 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9520 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9521 | void EliminateAllTemplateMatches() { |
| 9522 | // [...] any function template specializations in the set are |
| 9523 | // eliminated if the set also contains a non-template function, [...] |
| 9524 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
| 9525 | if (Matches[I].second->getPrimaryTemplate() == 0) |
| 9526 | ++I; |
| 9527 | else { |
| 9528 | Matches[I] = Matches[--N]; |
| 9529 | Matches.set_size(N); |
| 9530 | } |
| 9531 | } |
| 9532 | } |
| 9533 | |
| 9534 | public: |
| 9535 | void ComplainNoMatchesFound() const { |
| 9536 | assert(Matches.empty()); |
| 9537 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_no_viable) |
| 9538 | << OvlExpr->getName() << TargetFunctionType |
| 9539 | << OvlExpr->getSourceRange(); |
Richard Smith | 72a36a1 | 2013-08-14 00:00:44 +0000 | [diff] [blame] | 9540 | if (FailedCandidates.empty()) |
| 9541 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
| 9542 | else { |
| 9543 | // We have some deduction failure messages. Use them to diagnose |
| 9544 | // the function templates, and diagnose the non-template candidates |
| 9545 | // normally. |
| 9546 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 9547 | IEnd = OvlExpr->decls_end(); |
| 9548 | I != IEnd; ++I) |
| 9549 | if (FunctionDecl *Fun = |
| 9550 | dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl())) |
| 9551 | S.NoteOverloadCandidate(Fun, TargetFunctionType); |
| 9552 | FailedCandidates.NoteCandidates(S, OvlExpr->getLocStart()); |
| 9553 | } |
| 9554 | } |
| 9555 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9556 | bool IsInvalidFormOfPointerToMemberFunction() const { |
| 9557 | return TargetTypeIsNonStaticMemberFunction && |
| 9558 | !OvlExprInfo.HasFormOfMemberPointer; |
| 9559 | } |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9560 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9561 | void ComplainIsInvalidFormOfPointerToMemberFunction() const { |
| 9562 | // TODO: Should we condition this on whether any functions might |
| 9563 | // have matched, or is it more appropriate to do that in callers? |
| 9564 | // TODO: a fixit wouldn't hurt. |
| 9565 | S.Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
| 9566 | << TargetType << OvlExpr->getSourceRange(); |
| 9567 | } |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9568 | |
| 9569 | bool IsStaticMemberFunctionFromBoundPointer() const { |
| 9570 | return StaticMemberFunctionFromBoundPointer; |
| 9571 | } |
| 9572 | |
| 9573 | void ComplainIsStaticMemberFunctionFromBoundPointer() const { |
| 9574 | S.Diag(OvlExpr->getLocStart(), |
| 9575 | diag::err_invalid_form_pointer_member_function) |
| 9576 | << OvlExpr->getSourceRange(); |
| 9577 | } |
| 9578 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9579 | void ComplainOfInvalidConversion() const { |
| 9580 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 9581 | << OvlExpr->getName() << TargetType; |
| 9582 | } |
| 9583 | |
| 9584 | void ComplainMultipleMatchesFound() const { |
| 9585 | assert(Matches.size() > 1); |
| 9586 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 9587 | << OvlExpr->getName() |
| 9588 | << OvlExpr->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9589 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9590 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9591 | |
| 9592 | bool hadMultipleCandidates() const { return (OvlExpr->getNumDecls() > 1); } |
| 9593 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9594 | int getNumMatches() const { return Matches.size(); } |
| 9595 | |
| 9596 | FunctionDecl* getMatchingFunctionDecl() const { |
| 9597 | if (Matches.size() != 1) return 0; |
| 9598 | return Matches[0].second; |
| 9599 | } |
| 9600 | |
| 9601 | const DeclAccessPair* getMatchingFunctionAccessPair() const { |
| 9602 | if (Matches.size() != 1) return 0; |
| 9603 | return &Matches[0].first; |
| 9604 | } |
| 9605 | }; |
| 9606 | |
| 9607 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 9608 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 9609 | /// expression with overloaded function type and @p ToType is the type |
| 9610 | /// we're trying to resolve to. For example: |
| 9611 | /// |
| 9612 | /// @code |
| 9613 | /// int f(double); |
| 9614 | /// int f(int); |
| 9615 | /// |
| 9616 | /// int (*pfd)(double) = f; // selects f(double) |
| 9617 | /// @endcode |
| 9618 | /// |
| 9619 | /// This routine returns the resulting FunctionDecl if it could be |
| 9620 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 9621 | /// routine will emit diagnostics if there is an error. |
| 9622 | FunctionDecl * |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9623 | Sema::ResolveAddressOfOverloadedFunction(Expr *AddressOfExpr, |
| 9624 | QualType TargetType, |
| 9625 | bool Complain, |
| 9626 | DeclAccessPair &FoundResult, |
| 9627 | bool *pHadMultipleCandidates) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9628 | assert(AddressOfExpr->getType() == Context.OverloadTy); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9629 | |
| 9630 | AddressOfFunctionResolver Resolver(*this, AddressOfExpr, TargetType, |
| 9631 | Complain); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9632 | int NumMatches = Resolver.getNumMatches(); |
| 9633 | FunctionDecl* Fn = 0; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9634 | if (NumMatches == 0 && Complain) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9635 | if (Resolver.IsInvalidFormOfPointerToMemberFunction()) |
| 9636 | Resolver.ComplainIsInvalidFormOfPointerToMemberFunction(); |
| 9637 | else |
| 9638 | Resolver.ComplainNoMatchesFound(); |
| 9639 | } |
| 9640 | else if (NumMatches > 1 && Complain) |
| 9641 | Resolver.ComplainMultipleMatchesFound(); |
| 9642 | else if (NumMatches == 1) { |
| 9643 | Fn = Resolver.getMatchingFunctionDecl(); |
| 9644 | assert(Fn); |
| 9645 | FoundResult = *Resolver.getMatchingFunctionAccessPair(); |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9646 | if (Complain) { |
| 9647 | if (Resolver.IsStaticMemberFunctionFromBoundPointer()) |
| 9648 | Resolver.ComplainIsStaticMemberFunctionFromBoundPointer(); |
| 9649 | else |
| 9650 | CheckAddressOfMemberAccess(AddressOfExpr, FoundResult); |
| 9651 | } |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 9652 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9653 | |
| 9654 | if (pHadMultipleCandidates) |
| 9655 | *pHadMultipleCandidates = Resolver.hadMultipleCandidates(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9656 | return Fn; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9657 | } |
| 9658 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9659 | /// \brief Given an expression that refers to an overloaded function, try to |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9660 | /// resolve that overloaded function expression down to a single function. |
| 9661 | /// |
| 9662 | /// This routine can only resolve template-ids that refer to a single function |
| 9663 | /// template, where that template-id refers to a single template whose template |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9664 | /// arguments are either provided by the template-id or have defaults, |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9665 | /// as described in C++0x [temp.arg.explicit]p3. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9666 | FunctionDecl * |
| 9667 | Sema::ResolveSingleFunctionTemplateSpecialization(OverloadExpr *ovl, |
| 9668 | bool Complain, |
| 9669 | DeclAccessPair *FoundResult) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9670 | // C++ [over.over]p1: |
| 9671 | // [...] [Note: any redundant set of parentheses surrounding the |
| 9672 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9673 | // C++ [over.over]p1: |
| 9674 | // [...] The overloaded function name can be preceded by the & |
| 9675 | // operator. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9676 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9677 | // If we didn't actually find any template-ids, we're done. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9678 | if (!ovl->hasExplicitTemplateArgs()) |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9679 | return 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 9680 | |
| 9681 | TemplateArgumentListInfo ExplicitTemplateArgs; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9682 | ovl->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9683 | TemplateSpecCandidateSet FailedCandidates(ovl->getNameLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9684 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9685 | // Look through all of the overloaded functions, searching for one |
| 9686 | // whose type matches exactly. |
| 9687 | FunctionDecl *Matched = 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9688 | for (UnresolvedSetIterator I = ovl->decls_begin(), |
| 9689 | E = ovl->decls_end(); I != E; ++I) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9690 | // C++0x [temp.arg.explicit]p3: |
| 9691 | // [...] In contexts where deduction is done and fails, or in contexts |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9692 | // where deduction is not done, if a template argument list is |
| 9693 | // specified and it, along with any default template arguments, |
| 9694 | // identifies a single function template specialization, then the |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9695 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | 66a8c9a | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 9696 | FunctionTemplateDecl *FunctionTemplate |
| 9697 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9698 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9699 | // C++ [over.over]p2: |
| 9700 | // If the name is a function template, template argument deduction is |
| 9701 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 9702 | // resulting template argument list is used to generate a single |
| 9703 | // function template specialization, which is added to the set of |
| 9704 | // overloaded functions considered. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9705 | FunctionDecl *Specialization = 0; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9706 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9707 | if (TemplateDeductionResult Result |
| 9708 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9709 | Specialization, Info, |
| 9710 | /*InOverloadResolution=*/true)) { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9711 | // Make a note of the failed deduction for diagnostics. |
| 9712 | // TODO: Actually use the failed-deduction info? |
| 9713 | FailedCandidates.addCandidate() |
| 9714 | .set(FunctionTemplate->getTemplatedDecl(), |
| 9715 | MakeDeductionFailureInfo(Context, Result, Info)); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9716 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9717 | } |
| 9718 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9719 | assert(Specialization && "no specialization and no error?"); |
| 9720 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9721 | // Multiple matches; we can't resolve to a single declaration. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9722 | if (Matched) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9723 | if (Complain) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9724 | Diag(ovl->getExprLoc(), diag::err_addr_ovl_ambiguous) |
| 9725 | << ovl->getName(); |
| 9726 | NoteAllOverloadCandidates(ovl); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9727 | } |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9728 | return 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9729 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9730 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9731 | Matched = Specialization; |
| 9732 | if (FoundResult) *FoundResult = I.getPair(); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9733 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9734 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9735 | if (Matched && getLangOpts().CPlusPlus1y && |
| 9736 | Matched->getResultType()->isUndeducedType() && |
| 9737 | DeduceReturnType(Matched, ovl->getExprLoc(), Complain)) |
| 9738 | return 0; |
| 9739 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9740 | return Matched; |
| 9741 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9742 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9743 | |
| 9744 | |
| 9745 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9746 | // Resolve and fix an overloaded expression that can be resolved |
| 9747 | // because it identifies a single function template specialization. |
| 9748 | // |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9749 | // Last three arguments should only be supplied if Complain = true |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9750 | // |
| 9751 | // Return true if it was logically possible to so resolve the |
| 9752 | // expression, regardless of whether or not it succeeded. Always |
| 9753 | // returns true if 'complain' is set. |
| 9754 | bool Sema::ResolveAndFixSingleFunctionTemplateSpecialization( |
| 9755 | ExprResult &SrcExpr, bool doFunctionPointerConverion, |
| 9756 | bool complain, const SourceRange& OpRangeForComplaining, |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9757 | QualType DestTypeForComplaining, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9758 | unsigned DiagIDForComplaining) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9759 | assert(SrcExpr.get()->getType() == Context.OverloadTy); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9760 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9761 | OverloadExpr::FindResult ovl = OverloadExpr::find(SrcExpr.get()); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9762 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9763 | DeclAccessPair found; |
| 9764 | ExprResult SingleFunctionExpression; |
| 9765 | if (FunctionDecl *fn = ResolveSingleFunctionTemplateSpecialization( |
| 9766 | ovl.Expression, /*complain*/ false, &found)) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9767 | if (DiagnoseUseOfDecl(fn, SrcExpr.get()->getLocStart())) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9768 | SrcExpr = ExprError(); |
| 9769 | return true; |
| 9770 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9771 | |
| 9772 | // It is only correct to resolve to an instance method if we're |
| 9773 | // resolving a form that's permitted to be a pointer to member. |
| 9774 | // Otherwise we'll end up making a bound member expression, which |
| 9775 | // is illegal in all the contexts we resolve like this. |
| 9776 | if (!ovl.HasFormOfMemberPointer && |
| 9777 | isa<CXXMethodDecl>(fn) && |
| 9778 | cast<CXXMethodDecl>(fn)->isInstance()) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9779 | if (!complain) return false; |
| 9780 | |
| 9781 | Diag(ovl.Expression->getExprLoc(), |
| 9782 | diag::err_bound_member_function) |
| 9783 | << 0 << ovl.Expression->getSourceRange(); |
| 9784 | |
| 9785 | // TODO: I believe we only end up here if there's a mix of |
| 9786 | // static and non-static candidates (otherwise the expression |
| 9787 | // would have 'bound member' type, not 'overload' type). |
| 9788 | // Ideally we would note which candidate was chosen and why |
| 9789 | // the static candidates were rejected. |
| 9790 | SrcExpr = ExprError(); |
| 9791 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9792 | } |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 9793 | |
Sylvestre Ledru | 43e3dee | 2012-07-31 06:56:50 +0000 | [diff] [blame] | 9794 | // Fix the expression to refer to 'fn'. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9795 | SingleFunctionExpression = |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9796 | Owned(FixOverloadedFunctionReference(SrcExpr.take(), found, fn)); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9797 | |
| 9798 | // If desired, do function-to-pointer decay. |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9799 | if (doFunctionPointerConverion) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9800 | SingleFunctionExpression = |
| 9801 | DefaultFunctionArrayLvalueConversion(SingleFunctionExpression.take()); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9802 | if (SingleFunctionExpression.isInvalid()) { |
| 9803 | SrcExpr = ExprError(); |
| 9804 | return true; |
| 9805 | } |
| 9806 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9807 | } |
| 9808 | |
| 9809 | if (!SingleFunctionExpression.isUsable()) { |
| 9810 | if (complain) { |
| 9811 | Diag(OpRangeForComplaining.getBegin(), DiagIDForComplaining) |
| 9812 | << ovl.Expression->getName() |
| 9813 | << DestTypeForComplaining |
| 9814 | << OpRangeForComplaining |
| 9815 | << ovl.Expression->getQualifierLoc().getSourceRange(); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9816 | NoteAllOverloadCandidates(SrcExpr.get()); |
| 9817 | |
| 9818 | SrcExpr = ExprError(); |
| 9819 | return true; |
| 9820 | } |
| 9821 | |
| 9822 | return false; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9823 | } |
| 9824 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9825 | SrcExpr = SingleFunctionExpression; |
| 9826 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9827 | } |
| 9828 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9829 | /// \brief Add a single candidate to the overload set. |
| 9830 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9831 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 9832 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9833 | ArrayRef<Expr *> Args, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9834 | OverloadCandidateSet &CandidateSet, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9835 | bool PartialOverloading, |
| 9836 | bool KnownValid) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9837 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9838 | if (isa<UsingShadowDecl>(Callee)) |
| 9839 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 9840 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9841 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9842 | if (ExplicitTemplateArgs) { |
| 9843 | assert(!KnownValid && "Explicit template arguments?"); |
| 9844 | return; |
| 9845 | } |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9846 | S.AddOverloadCandidate(Func, FoundDecl, Args, CandidateSet, false, |
| 9847 | PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9848 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9849 | } |
| 9850 | |
| 9851 | if (FunctionTemplateDecl *FuncTemplate |
| 9852 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9853 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9854 | ExplicitTemplateArgs, Args, CandidateSet); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9855 | return; |
| 9856 | } |
| 9857 | |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9858 | assert(!KnownValid && "unhandled case in overloaded call candidate"); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9859 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9860 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9861 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 9862 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9863 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9864 | ArrayRef<Expr *> Args, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9865 | OverloadCandidateSet &CandidateSet, |
| 9866 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9867 | |
| 9868 | #ifndef NDEBUG |
| 9869 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 9870 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9871 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9872 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 9873 | // and let Y be the lookup set produced by argument dependent |
| 9874 | // lookup (defined as follows). If X contains |
| 9875 | // |
| 9876 | // -- a declaration of a class member, or |
| 9877 | // |
| 9878 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9879 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9880 | // |
| 9881 | // -- a declaration that is neither a function or a function |
| 9882 | // template |
| 9883 | // |
| 9884 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9885 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9886 | if (ULE->requiresADL()) { |
| 9887 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 9888 | E = ULE->decls_end(); I != E; ++I) { |
| 9889 | assert(!(*I)->getDeclContext()->isRecord()); |
| 9890 | assert(isa<UsingShadowDecl>(*I) || |
| 9891 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 9892 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9893 | } |
| 9894 | } |
| 9895 | #endif |
| 9896 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9897 | // It would be nice to avoid this copy. |
| 9898 | TemplateArgumentListInfo TABuffer; |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 9899 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9900 | if (ULE->hasExplicitTemplateArgs()) { |
| 9901 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 9902 | ExplicitTemplateArgs = &TABuffer; |
| 9903 | } |
| 9904 | |
| 9905 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 9906 | E = ULE->decls_end(); I != E; ++I) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9907 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, Args, |
| 9908 | CandidateSet, PartialOverloading, |
| 9909 | /*KnownValid*/ true); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9910 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9911 | if (ULE->requiresADL()) |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9912 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
Richard Smith | f5cd5cc | 2012-02-25 06:24:24 +0000 | [diff] [blame] | 9913 | ULE->getExprLoc(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9914 | Args, ExplicitTemplateArgs, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 9915 | CandidateSet, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9916 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9917 | |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 9918 | /// Determine whether a declaration with the specified name could be moved into |
| 9919 | /// a different namespace. |
| 9920 | static bool canBeDeclaredInNamespace(const DeclarationName &Name) { |
| 9921 | switch (Name.getCXXOverloadedOperator()) { |
| 9922 | case OO_New: case OO_Array_New: |
| 9923 | case OO_Delete: case OO_Array_Delete: |
| 9924 | return false; |
| 9925 | |
| 9926 | default: |
| 9927 | return true; |
| 9928 | } |
| 9929 | } |
| 9930 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9931 | /// Attempt to recover from an ill-formed use of a non-dependent name in a |
| 9932 | /// template, where the non-dependent name was declared after the template |
| 9933 | /// was defined. This is common in code written for a compilers which do not |
| 9934 | /// correctly implement two-stage name lookup. |
| 9935 | /// |
| 9936 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 9937 | static bool |
| 9938 | DiagnoseTwoPhaseLookup(Sema &SemaRef, SourceLocation FnLoc, |
| 9939 | const CXXScopeSpec &SS, LookupResult &R, |
| 9940 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9941 | ArrayRef<Expr *> Args) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9942 | if (SemaRef.ActiveTemplateInstantiations.empty() || !SS.isEmpty()) |
| 9943 | return false; |
| 9944 | |
| 9945 | for (DeclContext *DC = SemaRef.CurContext; DC; DC = DC->getParent()) { |
Nick Lewycky | 5a7120c | 2012-03-14 20:41:00 +0000 | [diff] [blame] | 9946 | if (DC->isTransparentContext()) |
| 9947 | continue; |
| 9948 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9949 | SemaRef.LookupQualifiedName(R, DC); |
| 9950 | |
| 9951 | if (!R.empty()) { |
| 9952 | R.suppressDiagnostics(); |
| 9953 | |
| 9954 | if (isa<CXXRecordDecl>(DC)) { |
| 9955 | // Don't diagnose names we find in classes; we get much better |
| 9956 | // diagnostics for these from DiagnoseEmptyLookup. |
| 9957 | R.clear(); |
| 9958 | return false; |
| 9959 | } |
| 9960 | |
| 9961 | OverloadCandidateSet Candidates(FnLoc); |
| 9962 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 9963 | AddOverloadedCallCandidate(SemaRef, I.getPair(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9964 | ExplicitTemplateArgs, Args, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9965 | Candidates, false, /*KnownValid*/ false); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9966 | |
| 9967 | OverloadCandidateSet::iterator Best; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9968 | if (Candidates.BestViableFunction(SemaRef, FnLoc, Best) != OR_Success) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9969 | // No viable functions. Don't bother the user with notes for functions |
| 9970 | // which don't work and shouldn't be found anyway. |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9971 | R.clear(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9972 | return false; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9973 | } |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9974 | |
| 9975 | // Find the namespaces where ADL would have looked, and suggest |
| 9976 | // declaring the function there instead. |
| 9977 | Sema::AssociatedNamespaceSet AssociatedNamespaces; |
| 9978 | Sema::AssociatedClassSet AssociatedClasses; |
John McCall | 42f48fb | 2012-08-24 20:38:34 +0000 | [diff] [blame] | 9979 | SemaRef.FindAssociatedClassesAndNamespaces(FnLoc, Args, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9980 | AssociatedNamespaces, |
| 9981 | AssociatedClasses); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9982 | Sema::AssociatedNamespaceSet SuggestedNamespaces; |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 9983 | if (canBeDeclaredInNamespace(R.getLookupName())) { |
| 9984 | DeclContext *Std = SemaRef.getStdNamespace(); |
| 9985 | for (Sema::AssociatedNamespaceSet::iterator |
| 9986 | it = AssociatedNamespaces.begin(), |
| 9987 | end = AssociatedNamespaces.end(); it != end; ++it) { |
| 9988 | // Never suggest declaring a function within namespace 'std'. |
| 9989 | if (Std && Std->Encloses(*it)) |
| 9990 | continue; |
Richard Smith | 19e0d95 | 2012-12-22 02:46:14 +0000 | [diff] [blame] | 9991 | |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 9992 | // Never suggest declaring a function within a namespace with a |
| 9993 | // reserved name, like __gnu_cxx. |
| 9994 | NamespaceDecl *NS = dyn_cast<NamespaceDecl>(*it); |
| 9995 | if (NS && |
| 9996 | NS->getQualifiedNameAsString().find("__") != std::string::npos) |
| 9997 | continue; |
| 9998 | |
| 9999 | SuggestedNamespaces.insert(*it); |
| 10000 | } |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10001 | } |
| 10002 | |
| 10003 | SemaRef.Diag(R.getNameLoc(), diag::err_not_found_by_two_phase_lookup) |
| 10004 | << R.getLookupName(); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10005 | if (SuggestedNamespaces.empty()) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10006 | SemaRef.Diag(Best->Function->getLocation(), |
| 10007 | diag::note_not_found_by_two_phase_lookup) |
| 10008 | << R.getLookupName() << 0; |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10009 | } else if (SuggestedNamespaces.size() == 1) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10010 | SemaRef.Diag(Best->Function->getLocation(), |
| 10011 | diag::note_not_found_by_two_phase_lookup) |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10012 | << R.getLookupName() << 1 << *SuggestedNamespaces.begin(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10013 | } else { |
| 10014 | // FIXME: It would be useful to list the associated namespaces here, |
| 10015 | // but the diagnostics infrastructure doesn't provide a way to produce |
| 10016 | // a localized representation of a list of items. |
| 10017 | SemaRef.Diag(Best->Function->getLocation(), |
| 10018 | diag::note_not_found_by_two_phase_lookup) |
| 10019 | << R.getLookupName() << 2; |
| 10020 | } |
| 10021 | |
| 10022 | // Try to recover by calling this function. |
| 10023 | return true; |
| 10024 | } |
| 10025 | |
| 10026 | R.clear(); |
| 10027 | } |
| 10028 | |
| 10029 | return false; |
| 10030 | } |
| 10031 | |
| 10032 | /// Attempt to recover from ill-formed use of a non-dependent operator in a |
| 10033 | /// template, where the non-dependent operator was declared after the template |
| 10034 | /// was defined. |
| 10035 | /// |
| 10036 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 10037 | static bool |
| 10038 | DiagnoseTwoPhaseOperatorLookup(Sema &SemaRef, OverloadedOperatorKind Op, |
| 10039 | SourceLocation OpLoc, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10040 | ArrayRef<Expr *> Args) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10041 | DeclarationName OpName = |
| 10042 | SemaRef.Context.DeclarationNames.getCXXOperatorName(Op); |
| 10043 | LookupResult R(SemaRef, OpName, OpLoc, Sema::LookupOperatorName); |
| 10044 | return DiagnoseTwoPhaseLookup(SemaRef, OpLoc, CXXScopeSpec(), R, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10045 | /*ExplicitTemplateArgs=*/0, Args); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10046 | } |
| 10047 | |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 10048 | namespace { |
Richard Smith | e49ff3e | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 10049 | class BuildRecoveryCallExprRAII { |
| 10050 | Sema &SemaRef; |
| 10051 | public: |
| 10052 | BuildRecoveryCallExprRAII(Sema &S) : SemaRef(S) { |
| 10053 | assert(SemaRef.IsBuildingRecoveryCallExpr == false); |
| 10054 | SemaRef.IsBuildingRecoveryCallExpr = true; |
| 10055 | } |
| 10056 | |
| 10057 | ~BuildRecoveryCallExprRAII() { |
| 10058 | SemaRef.IsBuildingRecoveryCallExpr = false; |
| 10059 | } |
| 10060 | }; |
| 10061 | |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 10062 | } |
| 10063 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10064 | /// Attempts to recover from a call where no functions were found. |
| 10065 | /// |
| 10066 | /// Returns true if new candidates were found. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10067 | static ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 10068 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10069 | UnresolvedLookupExpr *ULE, |
| 10070 | SourceLocation LParenLoc, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10071 | llvm::MutableArrayRef<Expr *> Args, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10072 | SourceLocation RParenLoc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10073 | bool EmptyLookup, bool AllowTypoCorrection) { |
Richard Smith | e49ff3e | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 10074 | // Do not try to recover if it is already building a recovery call. |
| 10075 | // This stops infinite loops for template instantiations like |
| 10076 | // |
| 10077 | // template <typename T> auto foo(T t) -> decltype(foo(t)) {} |
| 10078 | // template <typename T> auto foo(T t) -> decltype(foo(&t)) {} |
| 10079 | // |
| 10080 | if (SemaRef.IsBuildingRecoveryCallExpr) |
| 10081 | return ExprError(); |
| 10082 | BuildRecoveryCallExprRAII RCE(SemaRef); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10083 | |
| 10084 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10085 | SS.Adopt(ULE->getQualifierLoc()); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10086 | SourceLocation TemplateKWLoc = ULE->getTemplateKeywordLoc(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10087 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10088 | TemplateArgumentListInfo TABuffer; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10089 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10090 | if (ULE->hasExplicitTemplateArgs()) { |
| 10091 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 10092 | ExplicitTemplateArgs = &TABuffer; |
| 10093 | } |
| 10094 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10095 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 10096 | Sema::LookupOrdinaryName); |
Kaelyn Uhrain | 761695f | 2013-07-08 23:13:39 +0000 | [diff] [blame] | 10097 | FunctionCallFilterCCC Validator(SemaRef, Args.size(), |
| 10098 | ExplicitTemplateArgs != 0); |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10099 | NoTypoCorrectionCCC RejectAll; |
| 10100 | CorrectionCandidateCallback *CCC = AllowTypoCorrection ? |
| 10101 | (CorrectionCandidateCallback*)&Validator : |
| 10102 | (CorrectionCandidateCallback*)&RejectAll; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10103 | if (!DiagnoseTwoPhaseLookup(SemaRef, Fn->getExprLoc(), SS, R, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10104 | ExplicitTemplateArgs, Args) && |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10105 | (!EmptyLookup || |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10106 | SemaRef.DiagnoseEmptyLookup(S, SS, R, *CCC, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10107 | ExplicitTemplateArgs, Args))) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10108 | return ExprError(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10109 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10110 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 10111 | |
| 10112 | // Build an implicit member call if appropriate. Just drop the |
| 10113 | // casts and such from the call, we don't really care. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10114 | ExprResult NewFn = ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10115 | if ((*R.begin())->isCXXClassMember()) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10116 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, |
| 10117 | R, ExplicitTemplateArgs); |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 10118 | else if (ExplicitTemplateArgs || TemplateKWLoc.isValid()) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10119 | NewFn = SemaRef.BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 10120 | ExplicitTemplateArgs); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10121 | else |
| 10122 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 10123 | |
| 10124 | if (NewFn.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10125 | return ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10126 | |
| 10127 | // This shouldn't cause an infinite loop because we're giving it |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10128 | // an expression with viable lookup results, which should never |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10129 | // end up here. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10130 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, NewFn.take(), LParenLoc, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10131 | MultiExprArg(Args.data(), Args.size()), |
| 10132 | RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10133 | } |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 10134 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10135 | /// \brief Constructs and populates an OverloadedCandidateSet from |
| 10136 | /// the given function. |
| 10137 | /// \returns true when an the ExprResult output parameter has been set. |
| 10138 | bool Sema::buildOverloadedCallSet(Scope *S, Expr *Fn, |
| 10139 | UnresolvedLookupExpr *ULE, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10140 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10141 | SourceLocation RParenLoc, |
| 10142 | OverloadCandidateSet *CandidateSet, |
| 10143 | ExprResult *Result) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10144 | #ifndef NDEBUG |
| 10145 | if (ULE->requiresADL()) { |
| 10146 | // To do ADL, we must have found an unqualified name. |
| 10147 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 10148 | |
| 10149 | // We don't perform ADL for implicit declarations of builtins. |
| 10150 | // Verify that this was correctly set up. |
| 10151 | FunctionDecl *F; |
| 10152 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 10153 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 10154 | F->getBuiltinID() && F->isImplicit()) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 10155 | llvm_unreachable("performing ADL for builtin"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10156 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10157 | // We don't perform ADL in C. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10158 | assert(getLangOpts().CPlusPlus && "ADL enabled in C"); |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 10159 | } |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10160 | #endif |
| 10161 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10162 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10163 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) { |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10164 | *Result = ExprError(); |
| 10165 | return true; |
| 10166 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 10167 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10168 | // Add the functions denoted by the callee to the set of candidate |
| 10169 | // functions, including those from argument-dependent lookup. |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10170 | AddOverloadedCallCandidates(ULE, Args, *CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10171 | |
| 10172 | // If we found nothing, try to recover. |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10173 | // BuildRecoveryCallExpr diagnoses the error itself, so we just bail |
| 10174 | // out if it fails. |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10175 | if (CandidateSet->empty()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10176 | // In Microsoft mode, if we are inside a template class member function then |
| 10177 | // create a type dependent CallExpr. The goal is to postpone name lookup |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 10178 | // to instantiation time to be able to search into type dependent base |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10179 | // classes. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10180 | if (getLangOpts().MicrosoftMode && CurContext->isDependentContext() && |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 10181 | (isa<FunctionDecl>(CurContext) || isa<CXXRecordDecl>(CurContext))) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10182 | CallExpr *CE = new (Context) CallExpr(Context, Fn, Args, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10183 | Context.DependentTy, VK_RValue, |
| 10184 | RParenLoc); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10185 | CE->setTypeDependent(true); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10186 | *Result = Owned(CE); |
| 10187 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10188 | } |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10189 | return false; |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 10190 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10191 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10192 | UnbridgedCasts.restore(); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10193 | return false; |
| 10194 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10195 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10196 | /// FinishOverloadedCallExpr - given an OverloadCandidateSet, builds and returns |
| 10197 | /// the completed call expression. If overload resolution fails, emits |
| 10198 | /// diagnostics and returns ExprError() |
| 10199 | static ExprResult FinishOverloadedCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
| 10200 | UnresolvedLookupExpr *ULE, |
| 10201 | SourceLocation LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10202 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10203 | SourceLocation RParenLoc, |
| 10204 | Expr *ExecConfig, |
| 10205 | OverloadCandidateSet *CandidateSet, |
| 10206 | OverloadCandidateSet::iterator *Best, |
| 10207 | OverloadingResult OverloadResult, |
| 10208 | bool AllowTypoCorrection) { |
| 10209 | if (CandidateSet->empty()) |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10210 | return BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10211 | RParenLoc, /*EmptyLookup=*/true, |
| 10212 | AllowTypoCorrection); |
| 10213 | |
| 10214 | switch (OverloadResult) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10215 | case OR_Success: { |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10216 | FunctionDecl *FDecl = (*Best)->Function; |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10217 | SemaRef.CheckUnresolvedLookupAccess(ULE, (*Best)->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 10218 | if (SemaRef.DiagnoseUseOfDecl(FDecl, ULE->getNameLoc())) |
| 10219 | return ExprError(); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10220 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10221 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
| 10222 | ExecConfig); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10223 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10224 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10225 | case OR_No_Viable_Function: { |
| 10226 | // Try to recover by looking for viable functions which the user might |
| 10227 | // have meant to call. |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10228 | ExprResult Recovery = BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10229 | Args, RParenLoc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10230 | /*EmptyLookup=*/false, |
| 10231 | AllowTypoCorrection); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10232 | if (!Recovery.isInvalid()) |
| 10233 | return Recovery; |
| 10234 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10235 | SemaRef.Diag(Fn->getLocStart(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10236 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10237 | << ULE->getName() << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10238 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, Args); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10239 | break; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10240 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10241 | |
| 10242 | case OR_Ambiguous: |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10243 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10244 | << ULE->getName() << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10245 | CandidateSet->NoteCandidates(SemaRef, OCD_ViableCandidates, Args); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10246 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10247 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10248 | case OR_Deleted: { |
| 10249 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_deleted_call) |
| 10250 | << (*Best)->Function->isDeleted() |
| 10251 | << ULE->getName() |
| 10252 | << SemaRef.getDeletedOrUnavailableSuffix((*Best)->Function) |
| 10253 | << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10254 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, Args); |
Argyrios Kyrtzidis | 0d579b6 | 2011-11-04 15:58:13 +0000 | [diff] [blame] | 10255 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10256 | // We emitted an error for the unvailable/deleted function call but keep |
| 10257 | // the call in the AST. |
| 10258 | FunctionDecl *FDecl = (*Best)->Function; |
| 10259 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10260 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
| 10261 | ExecConfig); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10262 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10263 | } |
| 10264 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 10265 | // Overload resolution failed. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10266 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10267 | } |
| 10268 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10269 | /// BuildOverloadedCallExpr - Given the call expression that calls Fn |
| 10270 | /// (which eventually refers to the declaration Func) and the call |
| 10271 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 10272 | /// to a specific function. If overload resolution succeeds, returns |
| 10273 | /// the call expression produced by overload resolution. |
| 10274 | /// Otherwise, emits diagnostics and returns ExprError. |
| 10275 | ExprResult Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, |
| 10276 | UnresolvedLookupExpr *ULE, |
| 10277 | SourceLocation LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10278 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10279 | SourceLocation RParenLoc, |
| 10280 | Expr *ExecConfig, |
| 10281 | bool AllowTypoCorrection) { |
| 10282 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
| 10283 | ExprResult result; |
| 10284 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10285 | if (buildOverloadedCallSet(S, Fn, ULE, Args, LParenLoc, &CandidateSet, |
| 10286 | &result)) |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10287 | return result; |
| 10288 | |
| 10289 | OverloadCandidateSet::iterator Best; |
| 10290 | OverloadingResult OverloadResult = |
| 10291 | CandidateSet.BestViableFunction(*this, Fn->getLocStart(), Best); |
| 10292 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10293 | return FinishOverloadedCallExpr(*this, S, Fn, ULE, LParenLoc, Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10294 | RParenLoc, ExecConfig, &CandidateSet, |
| 10295 | &Best, OverloadResult, |
| 10296 | AllowTypoCorrection); |
| 10297 | } |
| 10298 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10299 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 10300 | return Functions.size() > 1 || |
| 10301 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 10302 | } |
| 10303 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10304 | /// \brief Create a unary operation that may resolve to an overloaded |
| 10305 | /// operator. |
| 10306 | /// |
| 10307 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 10308 | /// |
| 10309 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 10310 | /// operator. |
| 10311 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 10312 | /// \param Fns The set of non-member functions that will be |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10313 | /// considered by overload resolution. The caller needs to build this |
| 10314 | /// set based on the context using, e.g., |
| 10315 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 10316 | /// set should not contain any member functions; those will be added |
| 10317 | /// by CreateOverloadedUnaryOp(). |
| 10318 | /// |
James Dennett | 8da1687 | 2012-06-22 10:32:46 +0000 | [diff] [blame] | 10319 | /// \param Input The input argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10320 | ExprResult |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10321 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 10322 | const UnresolvedSetImpl &Fns, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10323 | Expr *Input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10324 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10325 | |
| 10326 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 10327 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 10328 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10329 | // TODO: provide better source location info. |
| 10330 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10331 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10332 | if (checkPlaceholderForOverload(*this, Input)) |
| 10333 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10334 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10335 | Expr *Args[2] = { Input, 0 }; |
| 10336 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10337 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10338 | // For post-increment and post-decrement, add the implicit '0' as |
| 10339 | // the second argument, so that we know this is a post-increment or |
| 10340 | // post-decrement. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10341 | if (Opc == UO_PostInc || Opc == UO_PostDec) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10342 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 10343 | Args[1] = IntegerLiteral::Create(Context, Zero, Context.IntTy, |
| 10344 | SourceLocation()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10345 | NumArgs = 2; |
| 10346 | } |
| 10347 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10348 | ArrayRef<Expr *> ArgsArray(Args, NumArgs); |
| 10349 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10350 | if (Input->isTypeDependent()) { |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10351 | if (Fns.empty()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10352 | return Owned(new (Context) UnaryOperator(Input, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10353 | Opc, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10354 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10355 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10356 | OpLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10357 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10358 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10359 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 10360 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10361 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10362 | /*ADL*/ true, IsOverloaded(Fns), |
| 10363 | Fns.begin(), Fns.end()); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10364 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, ArgsArray, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10365 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10366 | VK_RValue, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10367 | OpLoc, false)); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10368 | } |
| 10369 | |
| 10370 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10371 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10372 | |
| 10373 | // Add the candidates from the given function set. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10374 | AddFunctionCandidates(Fns, ArgsArray, CandidateSet, false); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10375 | |
| 10376 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10377 | AddMemberOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10378 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10379 | // Add candidates from ADL. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10380 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, OpLoc, |
| 10381 | ArgsArray, /*ExplicitTemplateArgs*/ 0, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10382 | CandidateSet); |
| 10383 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10384 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10385 | AddBuiltinOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10386 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10387 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10388 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10389 | // Perform overload resolution. |
| 10390 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10391 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10392 | case OR_Success: { |
| 10393 | // We found a built-in operator or an overloaded operator. |
| 10394 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10395 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10396 | if (FnDecl) { |
| 10397 | // We matched an overloaded operator. Build a call to that |
| 10398 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10399 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10400 | // Convert the arguments. |
| 10401 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10402 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10403 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10404 | ExprResult InputRes = |
| 10405 | PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 10406 | Best->FoundDecl, Method); |
| 10407 | if (InputRes.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10408 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10409 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10410 | } else { |
| 10411 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10412 | ExprResult InputInit |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 10413 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10414 | Context, |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 10415 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10416 | SourceLocation(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10417 | Input); |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 10418 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10419 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10420 | Input = InputInit.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10421 | } |
| 10422 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10423 | // Determine the result type. |
| 10424 | QualType ResultTy = FnDecl->getResultType(); |
| 10425 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10426 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10427 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10428 | // Build the actual expression node. |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10429 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10430 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10431 | if (FnExpr.isInvalid()) |
| 10432 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10433 | |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 10434 | Args[0] = Input; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10435 | CallExpr *TheCall = |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10436 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), ArgsArray, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10437 | ResultTy, VK, OpLoc, false); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10438 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10439 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 10440 | FnDecl)) |
| 10441 | return ExprError(); |
| 10442 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10443 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10444 | } else { |
| 10445 | // We matched a built-in operator. Convert the arguments, then |
| 10446 | // break out so that we will build the appropriate built-in |
| 10447 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10448 | ExprResult InputRes = |
| 10449 | PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 10450 | Best->Conversions[0], AA_Passing); |
| 10451 | if (InputRes.isInvalid()) |
| 10452 | return ExprError(); |
| 10453 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10454 | break; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10455 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10456 | } |
| 10457 | |
| 10458 | case OR_No_Viable_Function: |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10459 | // This is an erroneous use of an operator which can be overloaded by |
| 10460 | // a non-member function. Check for non-member operators which were |
| 10461 | // defined too late to be candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10462 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, ArgsArray)) |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10463 | // FIXME: Recover by calling the found function. |
| 10464 | return ExprError(); |
| 10465 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10466 | // No viable function; fall through to handling this as a |
| 10467 | // built-in operator, which will produce an error message for us. |
| 10468 | break; |
| 10469 | |
| 10470 | case OR_Ambiguous: |
| 10471 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 10472 | << UnaryOperator::getOpcodeStr(Opc) |
| 10473 | << Input->getType() |
| 10474 | << Input->getSourceRange(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10475 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, ArgsArray, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10476 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
| 10477 | return ExprError(); |
| 10478 | |
| 10479 | case OR_Deleted: |
| 10480 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 10481 | << Best->Function->isDeleted() |
| 10482 | << UnaryOperator::getOpcodeStr(Opc) |
| 10483 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 10484 | << Input->getSourceRange(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10485 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, ArgsArray, |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 10486 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10487 | return ExprError(); |
| 10488 | } |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10489 | |
| 10490 | // Either we found no viable overloaded operator or we matched a |
| 10491 | // built-in operator. In either case, fall through to trying to |
| 10492 | // build a built-in operation. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10493 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10494 | } |
| 10495 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10496 | /// \brief Create a binary operation that may resolve to an overloaded |
| 10497 | /// operator. |
| 10498 | /// |
| 10499 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 10500 | /// |
| 10501 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 10502 | /// operator. |
| 10503 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 10504 | /// \param Fns The set of non-member functions that will be |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10505 | /// considered by overload resolution. The caller needs to build this |
| 10506 | /// set based on the context using, e.g., |
| 10507 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 10508 | /// set should not contain any member functions; those will be added |
| 10509 | /// by CreateOverloadedBinOp(). |
| 10510 | /// |
| 10511 | /// \param LHS Left-hand argument. |
| 10512 | /// \param RHS Right-hand argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10513 | ExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10514 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10515 | unsigned OpcIn, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10516 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10517 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10518 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10519 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10520 | |
| 10521 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 10522 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 10523 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 10524 | |
| 10525 | // If either side is type-dependent, create an appropriate dependent |
| 10526 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10527 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10528 | if (Fns.empty()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10529 | // If there are no functions to store, just build a dependent |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10530 | // BinaryOperator or CompoundAssignment. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10531 | if (Opc <= BO_Assign || Opc > BO_OrAssign) |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10532 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10533 | Context.DependentTy, |
| 10534 | VK_RValue, OK_Ordinary, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10535 | OpLoc, |
| 10536 | FPFeatures.fp_contract)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10537 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10538 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 10539 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10540 | VK_LValue, |
| 10541 | OK_Ordinary, |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10542 | Context.DependentTy, |
| 10543 | Context.DependentTy, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10544 | OpLoc, |
| 10545 | FPFeatures.fp_contract)); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10546 | } |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10547 | |
| 10548 | // FIXME: save results of ADL from here? |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10549 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10550 | // TODO: provide better source location info in DNLoc component. |
| 10551 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10552 | UnresolvedLookupExpr *Fn |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10553 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
| 10554 | NestedNameSpecifierLoc(), OpNameInfo, |
| 10555 | /*ADL*/ true, IsOverloaded(Fns), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10556 | Fns.begin(), Fns.end()); |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10557 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, Args, |
| 10558 | Context.DependentTy, VK_RValue, |
| 10559 | OpLoc, FPFeatures.fp_contract)); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10560 | } |
| 10561 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10562 | // Always do placeholder-like conversions on the RHS. |
| 10563 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 10564 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10565 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 10566 | // Do placeholder-like conversion on the LHS; note that we should |
| 10567 | // not get here with a PseudoObject LHS. |
| 10568 | assert(Args[0]->getObjectKind() != OK_ObjCProperty); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10569 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 10570 | return ExprError(); |
| 10571 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 10572 | // If this is the assignment operator, we only perform overload resolution |
| 10573 | // if the left-hand side is a class or enumeration type. This is actually |
| 10574 | // a hack. The standard requires that we do overload resolution between the |
| 10575 | // various built-in candidates, but as DR507 points out, this can lead to |
| 10576 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 10577 | // Note that we go the traditional code path for compound assignment forms. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10578 | if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10579 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10580 | |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10581 | // If this is the .* operator, which is not overloadable, just |
| 10582 | // create a built-in binary operator. |
| 10583 | if (Opc == BO_PtrMemD) |
| 10584 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 10585 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10586 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10587 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10588 | |
| 10589 | // Add the candidates from the given function set. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10590 | AddFunctionCandidates(Fns, Args, CandidateSet, false); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10591 | |
| 10592 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10593 | AddMemberOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10594 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10595 | // Add candidates from ADL. |
| 10596 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10597 | OpLoc, Args, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10598 | /*ExplicitTemplateArgs*/ 0, |
| 10599 | CandidateSet); |
| 10600 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10601 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10602 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10603 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10604 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10605 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10606 | // Perform overload resolution. |
| 10607 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10608 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 10609 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10610 | // We found a built-in operator or an overloaded operator. |
| 10611 | FunctionDecl *FnDecl = Best->Function; |
| 10612 | |
| 10613 | if (FnDecl) { |
| 10614 | // We matched an overloaded operator. Build a call to that |
| 10615 | // operator. |
| 10616 | |
| 10617 | // Convert the arguments. |
| 10618 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10619 | // Best->Access is only meaningful for class members. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10620 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10621 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10622 | ExprResult Arg1 = |
| 10623 | PerformCopyInitialization( |
| 10624 | InitializedEntity::InitializeParameter(Context, |
| 10625 | FnDecl->getParamDecl(0)), |
| 10626 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10627 | if (Arg1.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10628 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10629 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10630 | ExprResult Arg0 = |
| 10631 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 10632 | Best->FoundDecl, Method); |
| 10633 | if (Arg0.isInvalid()) |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10634 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10635 | Args[0] = Arg0.takeAs<Expr>(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10636 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10637 | } else { |
| 10638 | // Convert the arguments. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10639 | ExprResult Arg0 = PerformCopyInitialization( |
| 10640 | InitializedEntity::InitializeParameter(Context, |
| 10641 | FnDecl->getParamDecl(0)), |
| 10642 | SourceLocation(), Owned(Args[0])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10643 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10644 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10645 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10646 | ExprResult Arg1 = |
| 10647 | PerformCopyInitialization( |
| 10648 | InitializedEntity::InitializeParameter(Context, |
| 10649 | FnDecl->getParamDecl(1)), |
| 10650 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10651 | if (Arg1.isInvalid()) |
| 10652 | return ExprError(); |
| 10653 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 10654 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10655 | } |
| 10656 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10657 | // Determine the result type. |
| 10658 | QualType ResultTy = FnDecl->getResultType(); |
| 10659 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10660 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10661 | |
| 10662 | // Build the actual expression node. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10663 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10664 | Best->FoundDecl, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10665 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10666 | if (FnExpr.isInvalid()) |
| 10667 | return ExprError(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10668 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10669 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10670 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10671 | Args, ResultTy, VK, OpLoc, |
| 10672 | FPFeatures.fp_contract); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10673 | |
| 10674 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 10675 | FnDecl)) |
| 10676 | return ExprError(); |
| 10677 | |
Nick Lewycky | 9b7ea0d | 2013-01-24 02:03:08 +0000 | [diff] [blame] | 10678 | ArrayRef<const Expr *> ArgsArray(Args, 2); |
| 10679 | // Cut off the implicit 'this'. |
| 10680 | if (isa<CXXMethodDecl>(FnDecl)) |
| 10681 | ArgsArray = ArgsArray.slice(1); |
| 10682 | checkCall(FnDecl, ArgsArray, 0, isa<CXXMethodDecl>(FnDecl), OpLoc, |
| 10683 | TheCall->getSourceRange(), VariadicDoesNotApply); |
| 10684 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10685 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10686 | } else { |
| 10687 | // We matched a built-in operator. Convert the arguments, then |
| 10688 | // break out so that we will build the appropriate built-in |
| 10689 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10690 | ExprResult ArgsRes0 = |
| 10691 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 10692 | Best->Conversions[0], AA_Passing); |
| 10693 | if (ArgsRes0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10694 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10695 | Args[0] = ArgsRes0.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10696 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10697 | ExprResult ArgsRes1 = |
| 10698 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 10699 | Best->Conversions[1], AA_Passing); |
| 10700 | if (ArgsRes1.isInvalid()) |
| 10701 | return ExprError(); |
| 10702 | Args[1] = ArgsRes1.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10703 | break; |
| 10704 | } |
| 10705 | } |
| 10706 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10707 | case OR_No_Viable_Function: { |
| 10708 | // C++ [over.match.oper]p9: |
| 10709 | // If the operator is the operator , [...] and there are no |
| 10710 | // viable functions, then the operator is assumed to be the |
| 10711 | // built-in operator and interpreted according to clause 5. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10712 | if (Opc == BO_Comma) |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10713 | break; |
| 10714 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10715 | // For class as left operand for assignment or compound assigment |
| 10716 | // operator do not fall through to handling in built-in, but report that |
| 10717 | // no overloaded assignment operator found |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10718 | ExprResult Result = ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10719 | if (Args[0]->getType()->isRecordType() && |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10720 | Opc >= BO_Assign && Opc <= BO_OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 10721 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 10722 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10723 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Eli Friedman | 3f93d4c | 2013-08-28 20:35:35 +0000 | [diff] [blame] | 10724 | if (Args[0]->getType()->isIncompleteType()) { |
| 10725 | Diag(OpLoc, diag::note_assign_lhs_incomplete) |
| 10726 | << Args[0]->getType() |
| 10727 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10728 | } |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10729 | } else { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10730 | // This is an erroneous use of an operator which can be overloaded by |
| 10731 | // a non-member function. Check for non-member operators which were |
| 10732 | // defined too late to be candidates. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10733 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, Args)) |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10734 | // FIXME: Recover by calling the found function. |
| 10735 | return ExprError(); |
| 10736 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10737 | // No viable function; try to create a built-in operation, which will |
| 10738 | // produce an error. Then, show the non-viable candidates. |
| 10739 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 10740 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10741 | assert(Result.isInvalid() && |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10742 | "C++ binary operator overloading is missing candidates!"); |
| 10743 | if (Result.isInvalid()) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10744 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10745 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10746 | return Result; |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10747 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10748 | |
| 10749 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10750 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_binary) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10751 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10752 | << Args[0]->getType() << Args[1]->getType() |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10753 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10754 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10755 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10756 | return ExprError(); |
| 10757 | |
| 10758 | case OR_Deleted: |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10759 | if (isImplicitlyDeleted(Best->Function)) { |
| 10760 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
| 10761 | Diag(OpLoc, diag::err_ovl_deleted_special_oper) |
Richard Smith | 0f46e64 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 10762 | << Context.getRecordType(Method->getParent()) |
| 10763 | << getSpecialMember(Method); |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 10764 | |
Richard Smith | 0f46e64 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 10765 | // The user probably meant to call this special member. Just |
| 10766 | // explain why it's deleted. |
| 10767 | NoteDeletedFunction(Method); |
| 10768 | return ExprError(); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10769 | } else { |
| 10770 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 10771 | << Best->Function->isDeleted() |
| 10772 | << BinaryOperator::getOpcodeStr(Opc) |
| 10773 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 10774 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10775 | } |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10776 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 10777 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10778 | return ExprError(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 10779 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10780 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10781 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10782 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10783 | } |
| 10784 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10785 | ExprResult |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10786 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 10787 | SourceLocation RLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10788 | Expr *Base, Expr *Idx) { |
| 10789 | Expr *Args[2] = { Base, Idx }; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10790 | DeclarationName OpName = |
| 10791 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 10792 | |
| 10793 | // If either side is type-dependent, create an appropriate dependent |
| 10794 | // expression. |
| 10795 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 10796 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10797 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10798 | // CHECKME: no 'operator' keyword? |
| 10799 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
| 10800 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10801 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 10802 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10803 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10804 | /*ADL*/ true, /*Overloaded*/ false, |
| 10805 | UnresolvedSetIterator(), |
| 10806 | UnresolvedSetIterator()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 10807 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10808 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10809 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10810 | Args, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10811 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10812 | VK_RValue, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10813 | RLoc, false)); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10814 | } |
| 10815 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10816 | // Handle placeholders on both operands. |
| 10817 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 10818 | return ExprError(); |
| 10819 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 10820 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10821 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10822 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10823 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10824 | |
| 10825 | // Subscript can only be overloaded as a member function. |
| 10826 | |
| 10827 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10828 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10829 | |
| 10830 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10831 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10832 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10833 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10834 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10835 | // Perform overload resolution. |
| 10836 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10837 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10838 | case OR_Success: { |
| 10839 | // We found a built-in operator or an overloaded operator. |
| 10840 | FunctionDecl *FnDecl = Best->Function; |
| 10841 | |
| 10842 | if (FnDecl) { |
| 10843 | // We matched an overloaded operator. Build a call to that |
| 10844 | // operator. |
| 10845 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10846 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10847 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10848 | // Convert the arguments. |
| 10849 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10850 | ExprResult Arg0 = |
| 10851 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 10852 | Best->FoundDecl, Method); |
| 10853 | if (Arg0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10854 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10855 | Args[0] = Arg0.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10856 | |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10857 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10858 | ExprResult InputInit |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10859 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10860 | Context, |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10861 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10862 | SourceLocation(), |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10863 | Owned(Args[1])); |
| 10864 | if (InputInit.isInvalid()) |
| 10865 | return ExprError(); |
| 10866 | |
| 10867 | Args[1] = InputInit.takeAs<Expr>(); |
| 10868 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10869 | // Determine the result type |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10870 | QualType ResultTy = FnDecl->getResultType(); |
| 10871 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10872 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10873 | |
| 10874 | // Build the actual expression node. |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10875 | DeclarationNameInfo OpLocInfo(OpName, LLoc); |
| 10876 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10877 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10878 | Best->FoundDecl, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10879 | HadMultipleCandidates, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10880 | OpLocInfo.getLoc(), |
| 10881 | OpLocInfo.getInfo()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10882 | if (FnExpr.isInvalid()) |
| 10883 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10884 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10885 | CXXOperatorCallExpr *TheCall = |
| 10886 | new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10887 | FnExpr.take(), Args, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10888 | ResultTy, VK, RLoc, |
| 10889 | false); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10890 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10891 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10892 | FnDecl)) |
| 10893 | return ExprError(); |
| 10894 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10895 | return MaybeBindToTemporary(TheCall); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10896 | } else { |
| 10897 | // We matched a built-in operator. Convert the arguments, then |
| 10898 | // break out so that we will build the appropriate built-in |
| 10899 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10900 | ExprResult ArgsRes0 = |
| 10901 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 10902 | Best->Conversions[0], AA_Passing); |
| 10903 | if (ArgsRes0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10904 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10905 | Args[0] = ArgsRes0.take(); |
| 10906 | |
| 10907 | ExprResult ArgsRes1 = |
| 10908 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 10909 | Best->Conversions[1], AA_Passing); |
| 10910 | if (ArgsRes1.isInvalid()) |
| 10911 | return ExprError(); |
| 10912 | Args[1] = ArgsRes1.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10913 | |
| 10914 | break; |
| 10915 | } |
| 10916 | } |
| 10917 | |
| 10918 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10919 | if (CandidateSet.empty()) |
| 10920 | Diag(LLoc, diag::err_ovl_no_oper) |
| 10921 | << Args[0]->getType() << /*subscript*/ 0 |
| 10922 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10923 | else |
| 10924 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 10925 | << Args[0]->getType() |
| 10926 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10927 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10928 | "[]", LLoc); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10929 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10930 | } |
| 10931 | |
| 10932 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10933 | Diag(LLoc, diag::err_ovl_ambiguous_oper_binary) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10934 | << "[]" |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10935 | << Args[0]->getType() << Args[1]->getType() |
| 10936 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10937 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10938 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10939 | return ExprError(); |
| 10940 | |
| 10941 | case OR_Deleted: |
| 10942 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 10943 | << Best->Function->isDeleted() << "[]" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 10944 | << getDeletedOrUnavailableSuffix(Best->Function) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10945 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10946 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10947 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10948 | return ExprError(); |
| 10949 | } |
| 10950 | |
| 10951 | // We matched a built-in operator; build it. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10952 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10953 | } |
| 10954 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10955 | /// BuildCallToMemberFunction - Build a call to a member |
| 10956 | /// function. MemExpr is the expression that refers to the member |
| 10957 | /// function (and includes the object parameter), Args/NumArgs are the |
| 10958 | /// arguments to the function call (not including the object |
| 10959 | /// parameter). The caller needs to validate that the member |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10960 | /// expression refers to a non-static member function or an overloaded |
| 10961 | /// member function. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10962 | ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10963 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10964 | SourceLocation LParenLoc, |
| 10965 | MultiExprArg Args, |
| 10966 | SourceLocation RParenLoc) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10967 | assert(MemExprE->getType() == Context.BoundMemberTy || |
| 10968 | MemExprE->getType() == Context.OverloadTy); |
| 10969 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10970 | // Dig out the member expression. This holds both the object |
| 10971 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10972 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10973 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10974 | // Determine whether this is a call to a pointer-to-member function. |
| 10975 | if (BinaryOperator *op = dyn_cast<BinaryOperator>(NakedMemExpr)) { |
| 10976 | assert(op->getType() == Context.BoundMemberTy); |
| 10977 | assert(op->getOpcode() == BO_PtrMemD || op->getOpcode() == BO_PtrMemI); |
| 10978 | |
| 10979 | QualType fnType = |
| 10980 | op->getRHS()->getType()->castAs<MemberPointerType>()->getPointeeType(); |
| 10981 | |
| 10982 | const FunctionProtoType *proto = fnType->castAs<FunctionProtoType>(); |
| 10983 | QualType resultType = proto->getCallResultType(Context); |
| 10984 | ExprValueKind valueKind = Expr::getValueKindForType(proto->getResultType()); |
| 10985 | |
| 10986 | // Check that the object type isn't more qualified than the |
| 10987 | // member function we're calling. |
| 10988 | Qualifiers funcQuals = Qualifiers::fromCVRMask(proto->getTypeQuals()); |
| 10989 | |
| 10990 | QualType objectType = op->getLHS()->getType(); |
| 10991 | if (op->getOpcode() == BO_PtrMemI) |
| 10992 | objectType = objectType->castAs<PointerType>()->getPointeeType(); |
| 10993 | Qualifiers objectQuals = objectType.getQualifiers(); |
| 10994 | |
| 10995 | Qualifiers difference = objectQuals - funcQuals; |
| 10996 | difference.removeObjCGCAttr(); |
| 10997 | difference.removeAddressSpace(); |
| 10998 | if (difference) { |
| 10999 | std::string qualsString = difference.getAsString(); |
| 11000 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
| 11001 | << fnType.getUnqualifiedType() |
| 11002 | << qualsString |
| 11003 | << (qualsString.find(' ') == std::string::npos ? 1 : 2); |
| 11004 | } |
| 11005 | |
| 11006 | CXXMemberCallExpr *call |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11007 | = new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11008 | resultType, valueKind, RParenLoc); |
| 11009 | |
| 11010 | if (CheckCallReturnType(proto->getResultType(), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11011 | op->getRHS()->getLocStart(), |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11012 | call, 0)) |
| 11013 | return ExprError(); |
| 11014 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11015 | if (ConvertArgumentsForCall(call, op, 0, proto, Args, RParenLoc)) |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11016 | return ExprError(); |
| 11017 | |
Richard Trieu | e2a90b8 | 2013-06-22 02:30:38 +0000 | [diff] [blame] | 11018 | if (CheckOtherCall(call, proto)) |
| 11019 | return ExprError(); |
| 11020 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11021 | return MaybeBindToTemporary(call); |
| 11022 | } |
| 11023 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11024 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11025 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11026 | return ExprError(); |
| 11027 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11028 | MemberExpr *MemExpr; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11029 | CXXMethodDecl *Method = 0; |
John McCall | bb6fb46 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 11030 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11031 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11032 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 11033 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11034 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11035 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11036 | Qualifier = MemExpr->getQualifier(); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11037 | UnbridgedCasts.restore(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11038 | } else { |
| 11039 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11040 | Qualifier = UnresExpr->getQualifier(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11041 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11042 | QualType ObjectType = UnresExpr->getBaseType(); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11043 | Expr::Classification ObjectClassification |
| 11044 | = UnresExpr->isArrow()? Expr::Classification::makeSimpleLValue() |
| 11045 | : UnresExpr->getBase()->Classify(Context); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11046 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11047 | // Add overload candidates |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11048 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11049 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11050 | // FIXME: avoid copy. |
| 11051 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 11052 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 11053 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11054 | TemplateArgs = &TemplateArgsBuffer; |
| 11055 | } |
| 11056 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11057 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 11058 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 11059 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11060 | NamedDecl *Func = *I; |
| 11061 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 11062 | if (isa<UsingShadowDecl>(Func)) |
| 11063 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 11064 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11065 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 11066 | // Microsoft supports direct constructor calls. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11067 | if (getLangOpts().MicrosoftExt && isa<CXXConstructorDecl>(Func)) { |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11068 | AddOverloadCandidate(cast<CXXConstructorDecl>(Func), I.getPair(), |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11069 | Args, CandidateSet); |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 11070 | } else if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 11071 | // If explicit template arguments were provided, we can't call a |
| 11072 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11073 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 11074 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11075 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11076 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11077 | ObjectClassification, Args, CandidateSet, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11078 | /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11079 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11080 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11081 | I.getPair(), ActingDC, TemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11082 | ObjectType, ObjectClassification, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11083 | Args, CandidateSet, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 11084 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11085 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 11086 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11087 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11088 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 11089 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11090 | UnbridgedCasts.restore(); |
| 11091 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11092 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11093 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getLocStart(), |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 11094 | Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11095 | case OR_Success: |
| 11096 | Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11097 | FoundDecl = Best->FoundDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11098 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 11099 | if (DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc())) |
| 11100 | return ExprError(); |
Faisal Vali | d570a92 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 11101 | // If FoundDecl is different from Method (such as if one is a template |
| 11102 | // and the other a specialization), make sure DiagnoseUseOfDecl is |
| 11103 | // called on both. |
| 11104 | // FIXME: This would be more comprehensively addressed by modifying |
| 11105 | // DiagnoseUseOfDecl to accept both the FoundDecl and the decl |
| 11106 | // being used. |
| 11107 | if (Method != FoundDecl.getDecl() && |
| 11108 | DiagnoseUseOfDecl(Method, UnresExpr->getNameLoc())) |
| 11109 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11110 | break; |
| 11111 | |
| 11112 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11113 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11114 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 11115 | << DeclName << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11116 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11117 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11118 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11119 | |
| 11120 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11121 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 11122 | << DeclName << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11123 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11124 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11125 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11126 | |
| 11127 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11128 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11129 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11130 | << DeclName |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11131 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11132 | << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11133 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11134 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11135 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11136 | } |
| 11137 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11138 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11139 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11140 | // If overload resolution picked a static member, build a |
| 11141 | // non-member call based on that function. |
| 11142 | if (Method->isStatic()) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11143 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, Args, |
| 11144 | RParenLoc); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11145 | } |
| 11146 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11147 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11148 | } |
| 11149 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11150 | QualType ResultType = Method->getResultType(); |
| 11151 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 11152 | ResultType = ResultType.getNonLValueExprType(Context); |
| 11153 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11154 | assert(Method && "Member call to something that isn't a method?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11155 | CXXMemberCallExpr *TheCall = |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11156 | new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11157 | ResultType, VK, RParenLoc); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11158 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 11159 | // Check for a valid return type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11160 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11161 | TheCall, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11162 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11163 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11164 | // Convert the object argument (for a non-static member function call). |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11165 | // We only need to do this if there was actually an overload; otherwise |
| 11166 | // it was done at lookup. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11167 | if (!Method->isStatic()) { |
| 11168 | ExprResult ObjectArg = |
| 11169 | PerformObjectArgumentInitialization(MemExpr->getBase(), Qualifier, |
| 11170 | FoundDecl, Method); |
| 11171 | if (ObjectArg.isInvalid()) |
| 11172 | return ExprError(); |
| 11173 | MemExpr->setBase(ObjectArg.take()); |
| 11174 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11175 | |
| 11176 | // Convert the rest of the arguments |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11177 | const FunctionProtoType *Proto = |
| 11178 | Method->getType()->getAs<FunctionProtoType>(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11179 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11180 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11181 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11182 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11183 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11184 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11185 | if (CheckFunctionCall(Method, TheCall, Proto)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11186 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 11187 | |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11188 | if ((isa<CXXConstructorDecl>(CurContext) || |
| 11189 | isa<CXXDestructorDecl>(CurContext)) && |
| 11190 | TheCall->getMethodDecl()->isPure()) { |
| 11191 | const CXXMethodDecl *MD = TheCall->getMethodDecl(); |
| 11192 | |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 11193 | if (isa<CXXThisExpr>(MemExpr->getBase()->IgnoreParenCasts())) { |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11194 | Diag(MemExpr->getLocStart(), |
| 11195 | diag::warn_call_to_pure_virtual_member_function_from_ctor_dtor) |
| 11196 | << MD->getDeclName() << isa<CXXDestructorDecl>(CurContext) |
| 11197 | << MD->getParent()->getDeclName(); |
| 11198 | |
| 11199 | Diag(MD->getLocStart(), diag::note_previous_decl) << MD->getDeclName(); |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 11200 | } |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11201 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11202 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11203 | } |
| 11204 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11205 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 11206 | /// type (C++ [over.call.object]), which can end up invoking an |
| 11207 | /// overloaded function call operator (@c operator()) or performing a |
| 11208 | /// user-defined conversion on the object argument. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 11209 | ExprResult |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11210 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 11211 | SourceLocation LParenLoc, |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11212 | MultiExprArg Args, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11213 | SourceLocation RParenLoc) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11214 | if (checkPlaceholderForOverload(*this, Obj)) |
| 11215 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11216 | ExprResult Object = Owned(Obj); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11217 | |
| 11218 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11219 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11220 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11221 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11222 | assert(Object.get()->getType()->isRecordType() && "Requires object type argument"); |
| 11223 | const RecordType *Record = Object.get()->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11224 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11225 | // C++ [over.call.object]p1: |
| 11226 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 11227 | // 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] | 11228 | // candidate functions includes at least the function call |
| 11229 | // operators of T. The function call operators of T are obtained by |
| 11230 | // ordinary lookup of the name operator() in the context of |
| 11231 | // (E).operator(). |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11232 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 11233 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11234 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11235 | if (RequireCompleteType(LParenLoc, Object.get()->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11236 | diag::err_incomplete_object_call, Object.get())) |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11237 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11238 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 11239 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 11240 | LookupQualifiedName(R, Record->getDecl()); |
| 11241 | R.suppressDiagnostics(); |
| 11242 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11243 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 11244 | Oper != OperEnd; ++Oper) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11245 | AddMethodCandidate(Oper.getPair(), Object.get()->getType(), |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11246 | Object.get()->Classify(Context), |
| 11247 | Args, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 11248 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 11249 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11250 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11251 | // C++ [over.call.object]p2: |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11252 | // In addition, for each (non-explicit in C++0x) conversion function |
| 11253 | // declared in T of the form |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11254 | // |
| 11255 | // operator conversion-type-id () cv-qualifier; |
| 11256 | // |
| 11257 | // where cv-qualifier is the same cv-qualification as, or a |
| 11258 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 11259 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 11260 | // R", or the type "reference to pointer to function of |
| 11261 | // (P1,...,Pn) returning R", or the type "reference to function |
| 11262 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11263 | // is also considered as a candidate function. Similarly, |
| 11264 | // surrogate call functions are added to the set of candidate |
| 11265 | // functions for each conversion function declared in an |
| 11266 | // accessible base class provided the function is not hidden |
| 11267 | // within T by another intervening declaration. |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 11268 | std::pair<CXXRecordDecl::conversion_iterator, |
| 11269 | CXXRecordDecl::conversion_iterator> Conversions |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 11270 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 11271 | for (CXXRecordDecl::conversion_iterator |
| 11272 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11273 | NamedDecl *D = *I; |
| 11274 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 11275 | if (isa<UsingShadowDecl>(D)) |
| 11276 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11277 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 11278 | // Skip over templated conversion functions; they aren't |
| 11279 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11280 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 11281 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 11282 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11283 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11284 | if (!Conv->isExplicit()) { |
| 11285 | // Strip the reference type (if any) and then the pointer type (if |
| 11286 | // any) to get down to what might be a function type. |
| 11287 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 11288 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 11289 | ConvType = ConvPtrType->getPointeeType(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11290 | |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11291 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
| 11292 | { |
| 11293 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11294 | Object.get(), Args, CandidateSet); |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11295 | } |
| 11296 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11297 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11298 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11299 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11300 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11301 | // Perform overload resolution. |
| 11302 | OverloadCandidateSet::iterator Best; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11303 | switch (CandidateSet.BestViableFunction(*this, Object.get()->getLocStart(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11304 | Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11305 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11306 | // Overload resolution succeeded; we'll build the appropriate call |
| 11307 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11308 | break; |
| 11309 | |
| 11310 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11311 | if (CandidateSet.empty()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11312 | Diag(Object.get()->getLocStart(), diag::err_ovl_no_oper) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11313 | << Object.get()->getType() << /*call*/ 1 |
| 11314 | << Object.get()->getSourceRange(); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11315 | else |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11316 | Diag(Object.get()->getLocStart(), |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11317 | diag::err_ovl_no_viable_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11318 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11319 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11320 | break; |
| 11321 | |
| 11322 | case OR_Ambiguous: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11323 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11324 | diag::err_ovl_ambiguous_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11325 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11326 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11327 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11328 | |
| 11329 | case OR_Deleted: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11330 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11331 | diag::err_ovl_deleted_object_call) |
| 11332 | << Best->Function->isDeleted() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11333 | << Object.get()->getType() |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11334 | << getDeletedOrUnavailableSuffix(Best->Function) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11335 | << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11336 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11337 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11338 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11339 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 11340 | if (Best == CandidateSet.end()) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11341 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11342 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11343 | UnbridgedCasts.restore(); |
| 11344 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11345 | if (Best->Function == 0) { |
| 11346 | // Since there is no function declaration, this is one of the |
| 11347 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11348 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11349 | = cast<CXXConversionDecl>( |
| 11350 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 11351 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11352 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 11353 | if (DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc)) |
| 11354 | return ExprError(); |
Faisal Vali | d570a92 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 11355 | assert(Conv == Best->FoundDecl.getDecl() && |
| 11356 | "Found Decl & conversion-to-functionptr should be same, right?!"); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11357 | // We selected one of the surrogate functions that converts the |
| 11358 | // object parameter to a function pointer. Perform the conversion |
| 11359 | // on the object argument, then let ActOnCallExpr finish the job. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11360 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 11361 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 11362 | // and then call it. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11363 | ExprResult Call = BuildCXXMemberCallExpr(Object.get(), Best->FoundDecl, |
| 11364 | Conv, HadMultipleCandidates); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 11365 | if (Call.isInvalid()) |
| 11366 | return ExprError(); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 11367 | // Record usage of conversion in an implicit cast. |
| 11368 | Call = Owned(ImplicitCastExpr::Create(Context, Call.get()->getType(), |
| 11369 | CK_UserDefinedConversion, |
| 11370 | Call.get(), 0, VK_RValue)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11371 | |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11372 | return ActOnCallExpr(S, Call.get(), LParenLoc, Args, RParenLoc); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11373 | } |
| 11374 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11375 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 11376 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11377 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 11378 | // that calls this method, using Object for the implicit object |
| 11379 | // parameter and passing along the remaining arguments. |
| 11380 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Nico Weber | e0ff690 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 11381 | |
| 11382 | // An error diagnostic has already been printed when parsing the declaration. |
Nico Weber | 1a52a4d | 2012-11-09 08:38:04 +0000 | [diff] [blame] | 11383 | if (Method->isInvalidDecl()) |
Nico Weber | e0ff690 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 11384 | return ExprError(); |
| 11385 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11386 | const FunctionProtoType *Proto = |
| 11387 | Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11388 | |
| 11389 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11390 | unsigned NumArgsToCheck = Args.size(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11391 | |
| 11392 | // Build the full argument list for the method call (the |
| 11393 | // implicit object parameter is placed at the beginning of the |
| 11394 | // list). |
| 11395 | Expr **MethodArgs; |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11396 | if (Args.size() < NumArgsInProto) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11397 | NumArgsToCheck = NumArgsInProto; |
| 11398 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 11399 | } else { |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11400 | MethodArgs = new Expr*[Args.size() + 1]; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11401 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11402 | MethodArgs[0] = Object.get(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11403 | for (unsigned ArgIdx = 0, e = Args.size(); ArgIdx != e; ++ArgIdx) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11404 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11405 | |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11406 | DeclarationNameInfo OpLocInfo( |
| 11407 | Context.DeclarationNames.getCXXOperatorName(OO_Call), LParenLoc); |
| 11408 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LParenLoc, RParenLoc)); |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11409 | ExprResult NewFn = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11410 | HadMultipleCandidates, |
| 11411 | OpLocInfo.getLoc(), |
| 11412 | OpLocInfo.getInfo()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11413 | if (NewFn.isInvalid()) |
| 11414 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11415 | |
| 11416 | // Once we've built TheCall, all of the expressions are properly |
| 11417 | // owned. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11418 | QualType ResultTy = Method->getResultType(); |
| 11419 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11420 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11421 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11422 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11423 | new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn.take(), |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11424 | llvm::makeArrayRef(MethodArgs, Args.size()+1), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 11425 | ResultTy, VK, RParenLoc, false); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11426 | delete [] MethodArgs; |
| 11427 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11428 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall, |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 11429 | Method)) |
| 11430 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11431 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11432 | // We may have default arguments. If so, we need to allocate more |
| 11433 | // slots in the call for them. |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11434 | if (Args.size() < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 11435 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11436 | else if (Args.size() > NumArgsInProto) |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11437 | NumArgsToCheck = NumArgsInProto; |
| 11438 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11439 | bool IsError = false; |
| 11440 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11441 | // Initialize the implicit object parameter. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11442 | ExprResult ObjRes = |
| 11443 | PerformObjectArgumentInitialization(Object.get(), /*Qualifier=*/0, |
| 11444 | Best->FoundDecl, Method); |
| 11445 | if (ObjRes.isInvalid()) |
| 11446 | IsError = true; |
| 11447 | else |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11448 | Object = ObjRes; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11449 | TheCall->setArg(0, Object.take()); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11450 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11451 | // Check the argument types. |
| 11452 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11453 | Expr *Arg; |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11454 | if (i < Args.size()) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11455 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11456 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11457 | // Pass the argument. |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11458 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11459 | ExprResult InputInit |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11460 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 11461 | Context, |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11462 | Method->getParamDecl(i)), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11463 | SourceLocation(), Arg); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11464 | |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11465 | IsError |= InputInit.isInvalid(); |
| 11466 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11467 | } else { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11468 | ExprResult DefArg |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 11469 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 11470 | if (DefArg.isInvalid()) { |
| 11471 | IsError = true; |
| 11472 | break; |
| 11473 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11474 | |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 11475 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11476 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11477 | |
| 11478 | TheCall->setArg(i + 1, Arg); |
| 11479 | } |
| 11480 | |
| 11481 | // If this is a variadic call, handle args passed through "...". |
| 11482 | if (Proto->isVariadic()) { |
| 11483 | // Promote the arguments (C99 6.5.2.2p7). |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11484 | for (unsigned i = NumArgsInProto, e = Args.size(); i < e; i++) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11485 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0); |
| 11486 | IsError |= Arg.isInvalid(); |
| 11487 | TheCall->setArg(i + 1, Arg.take()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11488 | } |
| 11489 | } |
| 11490 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11491 | if (IsError) return true; |
| 11492 | |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11493 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11494 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11495 | if (CheckFunctionCall(Method, TheCall, Proto)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 11496 | return true; |
| 11497 | |
John McCall | 182f709 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 11498 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11499 | } |
| 11500 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11501 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11502 | /// (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] | 11503 | /// @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] | 11504 | ExprResult |
Kaelyn Uhrain | baaeb85 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 11505 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc, |
| 11506 | bool *NoArrowOperatorFound) { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11507 | assert(Base->getType()->isRecordType() && |
| 11508 | "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11509 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11510 | if (checkPlaceholderForOverload(*this, Base)) |
| 11511 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11512 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11513 | SourceLocation Loc = Base->getExprLoc(); |
| 11514 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11515 | // C++ [over.ref]p1: |
| 11516 | // |
| 11517 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 11518 | // for a class object x of type T if T::operator->() exists and if |
| 11519 | // the operator is selected as the best match function by the |
| 11520 | // overload resolution mechanism (13.3). |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11521 | DeclarationName OpName = |
| 11522 | Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11523 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 11524 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11525 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11526 | if (RequireCompleteType(Loc, Base->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11527 | diag::err_typecheck_incomplete_tag, Base)) |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 11528 | return ExprError(); |
| 11529 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 11530 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 11531 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 11532 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 11533 | |
| 11534 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11535 | Oper != OperEnd; ++Oper) { |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11536 | AddMethodCandidate(Oper.getPair(), Base->getType(), Base->Classify(Context), |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 11537 | None, CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11538 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11539 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11540 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11541 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11542 | // Perform overload resolution. |
| 11543 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11544 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11545 | case OR_Success: |
| 11546 | // Overload resolution succeeded; we'll build the call below. |
| 11547 | break; |
| 11548 | |
| 11549 | case OR_No_Viable_Function: |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11550 | if (CandidateSet.empty()) { |
| 11551 | QualType BaseType = Base->getType(); |
Kaelyn Uhrain | baaeb85 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 11552 | if (NoArrowOperatorFound) { |
| 11553 | // Report this specific error to the caller instead of emitting a |
| 11554 | // diagnostic, as requested. |
| 11555 | *NoArrowOperatorFound = true; |
| 11556 | return ExprError(); |
| 11557 | } |
Kaelyn Uhrain | d422434 | 2013-07-15 19:54:54 +0000 | [diff] [blame] | 11558 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
| 11559 | << BaseType << Base->getSourceRange(); |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11560 | if (BaseType->isRecordType() && !BaseType->isPointerType()) { |
Kaelyn Uhrain | d422434 | 2013-07-15 19:54:54 +0000 | [diff] [blame] | 11561 | Diag(OpLoc, diag::note_typecheck_member_reference_suggestion) |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11562 | << FixItHint::CreateReplacement(OpLoc, "."); |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11563 | } |
| 11564 | } else |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11565 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11566 | << "operator->" << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11567 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11568 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11569 | |
| 11570 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 11571 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 11572 | << "->" << Base->getType() << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11573 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11574 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11575 | |
| 11576 | case OR_Deleted: |
| 11577 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 11578 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11579 | << "->" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11580 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11581 | << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11582 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11583 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11584 | } |
| 11585 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11586 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
| 11587 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11588 | // Convert the object parameter. |
| 11589 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11590 | ExprResult BaseResult = |
| 11591 | PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 11592 | Best->FoundDecl, Method); |
| 11593 | if (BaseResult.isInvalid()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11594 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11595 | Base = BaseResult.take(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 11596 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11597 | // Build the operator call. |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11598 | ExprResult FnExpr = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11599 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11600 | if (FnExpr.isInvalid()) |
| 11601 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11602 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11603 | QualType ResultTy = Method->getResultType(); |
| 11604 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11605 | ResultTy = ResultTy.getNonLValueExprType(Context); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11606 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11607 | new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr.take(), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 11608 | Base, ResultTy, VK, OpLoc, false); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 11609 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11610 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 11611 | Method)) |
| 11612 | return ExprError(); |
Eli Friedman | d593190 | 2011-04-04 01:18:25 +0000 | [diff] [blame] | 11613 | |
| 11614 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11615 | } |
| 11616 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11617 | /// BuildLiteralOperatorCall - Build a UserDefinedLiteral by creating a call to |
| 11618 | /// a literal operator described by the provided lookup results. |
| 11619 | ExprResult Sema::BuildLiteralOperatorCall(LookupResult &R, |
| 11620 | DeclarationNameInfo &SuffixInfo, |
| 11621 | ArrayRef<Expr*> Args, |
| 11622 | SourceLocation LitEndLoc, |
| 11623 | TemplateArgumentListInfo *TemplateArgs) { |
| 11624 | SourceLocation UDSuffixLoc = SuffixInfo.getCXXLiteralOperatorNameLoc(); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11625 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11626 | OverloadCandidateSet CandidateSet(UDSuffixLoc); |
| 11627 | AddFunctionCandidates(R.asUnresolvedSet(), Args, CandidateSet, true, |
| 11628 | TemplateArgs); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11629 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11630 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11631 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11632 | // Perform overload resolution. This will usually be trivial, but might need |
| 11633 | // to perform substitutions for a literal operator template. |
| 11634 | OverloadCandidateSet::iterator Best; |
| 11635 | switch (CandidateSet.BestViableFunction(*this, UDSuffixLoc, Best)) { |
| 11636 | case OR_Success: |
| 11637 | case OR_Deleted: |
| 11638 | break; |
| 11639 | |
| 11640 | case OR_No_Viable_Function: |
| 11641 | Diag(UDSuffixLoc, diag::err_ovl_no_viable_function_in_call) |
| 11642 | << R.getLookupName(); |
| 11643 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
| 11644 | return ExprError(); |
| 11645 | |
| 11646 | case OR_Ambiguous: |
| 11647 | Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) << R.getLookupName(); |
| 11648 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
| 11649 | return ExprError(); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11650 | } |
| 11651 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11652 | FunctionDecl *FD = Best->Function; |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11653 | ExprResult Fn = CreateFunctionRefExpr(*this, FD, Best->FoundDecl, |
| 11654 | HadMultipleCandidates, |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11655 | SuffixInfo.getLoc(), |
| 11656 | SuffixInfo.getInfo()); |
| 11657 | if (Fn.isInvalid()) |
| 11658 | return true; |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11659 | |
| 11660 | // Check the argument types. This should almost always be a no-op, except |
| 11661 | // that array-to-pointer decay is applied to string literals. |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11662 | Expr *ConvArgs[2]; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 11663 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11664 | ExprResult InputInit = PerformCopyInitialization( |
| 11665 | InitializedEntity::InitializeParameter(Context, FD->getParamDecl(ArgIdx)), |
| 11666 | SourceLocation(), Args[ArgIdx]); |
| 11667 | if (InputInit.isInvalid()) |
| 11668 | return true; |
| 11669 | ConvArgs[ArgIdx] = InputInit.take(); |
| 11670 | } |
| 11671 | |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11672 | QualType ResultTy = FD->getResultType(); |
| 11673 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11674 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11675 | |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11676 | UserDefinedLiteral *UDL = |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 11677 | new (Context) UserDefinedLiteral(Context, Fn.take(), |
| 11678 | llvm::makeArrayRef(ConvArgs, Args.size()), |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11679 | ResultTy, VK, LitEndLoc, UDSuffixLoc); |
| 11680 | |
| 11681 | if (CheckCallReturnType(FD->getResultType(), UDSuffixLoc, UDL, FD)) |
| 11682 | return ExprError(); |
| 11683 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11684 | if (CheckFunctionCall(FD, UDL, NULL)) |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11685 | return ExprError(); |
| 11686 | |
| 11687 | return MaybeBindToTemporary(UDL); |
| 11688 | } |
| 11689 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11690 | /// Build a call to 'begin' or 'end' for a C++11 for-range statement. If the |
| 11691 | /// given LookupResult is non-empty, it is assumed to describe a member which |
| 11692 | /// will be invoked. Otherwise, the function will be found via argument |
| 11693 | /// dependent lookup. |
| 11694 | /// CallExpr is set to a valid expression and FRS_Success returned on success, |
| 11695 | /// otherwise CallExpr is set to ExprError() and some non-success value |
| 11696 | /// is returned. |
| 11697 | Sema::ForRangeStatus |
| 11698 | Sema::BuildForRangeBeginEndCall(Scope *S, SourceLocation Loc, |
| 11699 | SourceLocation RangeLoc, VarDecl *Decl, |
| 11700 | BeginEndFunction BEF, |
| 11701 | const DeclarationNameInfo &NameInfo, |
| 11702 | LookupResult &MemberLookup, |
| 11703 | OverloadCandidateSet *CandidateSet, |
| 11704 | Expr *Range, ExprResult *CallExpr) { |
| 11705 | CandidateSet->clear(); |
| 11706 | if (!MemberLookup.empty()) { |
| 11707 | ExprResult MemberRef = |
| 11708 | BuildMemberReferenceExpr(Range, Range->getType(), Loc, |
| 11709 | /*IsPtr=*/false, CXXScopeSpec(), |
| 11710 | /*TemplateKWLoc=*/SourceLocation(), |
| 11711 | /*FirstQualifierInScope=*/0, |
| 11712 | MemberLookup, |
| 11713 | /*TemplateArgs=*/0); |
| 11714 | if (MemberRef.isInvalid()) { |
| 11715 | *CallExpr = ExprError(); |
| 11716 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11717 | << RangeLoc << BEF << Range->getType(); |
| 11718 | return FRS_DiagnosticIssued; |
| 11719 | } |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 11720 | *CallExpr = ActOnCallExpr(S, MemberRef.get(), Loc, None, Loc, 0); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11721 | if (CallExpr->isInvalid()) { |
| 11722 | *CallExpr = ExprError(); |
| 11723 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11724 | << RangeLoc << BEF << Range->getType(); |
| 11725 | return FRS_DiagnosticIssued; |
| 11726 | } |
| 11727 | } else { |
| 11728 | UnresolvedSet<0> FoundNames; |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11729 | UnresolvedLookupExpr *Fn = |
| 11730 | UnresolvedLookupExpr::Create(Context, /*NamingClass=*/0, |
| 11731 | NestedNameSpecifierLoc(), NameInfo, |
| 11732 | /*NeedsADL=*/true, /*Overloaded=*/false, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 11733 | FoundNames.begin(), FoundNames.end()); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11734 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11735 | bool CandidateSetError = buildOverloadedCallSet(S, Fn, Fn, Range, Loc, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11736 | CandidateSet, CallExpr); |
| 11737 | if (CandidateSet->empty() || CandidateSetError) { |
| 11738 | *CallExpr = ExprError(); |
| 11739 | return FRS_NoViableFunction; |
| 11740 | } |
| 11741 | OverloadCandidateSet::iterator Best; |
| 11742 | OverloadingResult OverloadResult = |
| 11743 | CandidateSet->BestViableFunction(*this, Fn->getLocStart(), Best); |
| 11744 | |
| 11745 | if (OverloadResult == OR_No_Viable_Function) { |
| 11746 | *CallExpr = ExprError(); |
| 11747 | return FRS_NoViableFunction; |
| 11748 | } |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11749 | *CallExpr = FinishOverloadedCallExpr(*this, S, Fn, Fn, Loc, Range, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11750 | Loc, 0, CandidateSet, &Best, |
| 11751 | OverloadResult, |
| 11752 | /*AllowTypoCorrection=*/false); |
| 11753 | if (CallExpr->isInvalid() || OverloadResult != OR_Success) { |
| 11754 | *CallExpr = ExprError(); |
| 11755 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11756 | << RangeLoc << BEF << Range->getType(); |
| 11757 | return FRS_DiagnosticIssued; |
| 11758 | } |
| 11759 | } |
| 11760 | return FRS_Success; |
| 11761 | } |
| 11762 | |
| 11763 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11764 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 11765 | /// a C++ overloaded function (possibly with some parentheses and |
| 11766 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 11767 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 11768 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 11769 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11770 | FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11771 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11772 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 11773 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11774 | if (SubExpr == PE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11775 | return PE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11776 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11777 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11778 | } |
| 11779 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11780 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11781 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 11782 | Found, Fn); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11783 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11784 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 11785 | "Implicit cast type cannot be determined from overload"); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 11786 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11787 | if (SubExpr == ICE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11788 | return ICE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11789 | |
| 11790 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 11791 | ICE->getCastKind(), |
| 11792 | SubExpr, 0, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 11793 | ICE->getValueKind()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11794 | } |
| 11795 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11796 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11797 | assert(UnOp->getOpcode() == UO_AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11798 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 11799 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 11800 | if (Method->isStatic()) { |
| 11801 | // Do nothing: static member functions aren't any different |
| 11802 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11803 | } else { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11804 | // Fix the sub expression, which really has to be an |
| 11805 | // UnresolvedLookupExpr holding an overloaded member function |
| 11806 | // or template. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11807 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 11808 | Found, Fn); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11809 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11810 | return UnOp; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11811 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11812 | assert(isa<DeclRefExpr>(SubExpr) |
| 11813 | && "fixed to something other than a decl ref"); |
| 11814 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 11815 | && "fixed to a member ref with no nested name qualifier"); |
| 11816 | |
| 11817 | // We have taken the address of a pointer to member |
| 11818 | // function. Perform the computation here so that we get the |
| 11819 | // appropriate pointer to member type. |
| 11820 | QualType ClassType |
| 11821 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 11822 | QualType MemPtrType |
| 11823 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 11824 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11825 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, MemPtrType, |
| 11826 | VK_RValue, OK_Ordinary, |
| 11827 | UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 11828 | } |
| 11829 | } |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11830 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 11831 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11832 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11833 | return UnOp; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11834 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11835 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11836 | Context.getPointerType(SubExpr->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11837 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11838 | UnOp->getOperatorLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11839 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11840 | |
| 11841 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11842 | // FIXME: avoid copy. |
| 11843 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11844 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11845 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11846 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11847 | } |
| 11848 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11849 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 11850 | ULE->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11851 | ULE->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11852 | Fn, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 11853 | /*enclosing*/ false, // FIXME? |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11854 | ULE->getNameLoc(), |
| 11855 | Fn->getType(), |
| 11856 | VK_LValue, |
| 11857 | Found.getDecl(), |
| 11858 | TemplateArgs); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 11859 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11860 | DRE->setHadMultipleCandidates(ULE->getNumDecls() > 1); |
| 11861 | return DRE; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11862 | } |
| 11863 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11864 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11865 | // FIXME: avoid copy. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11866 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 11867 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 11868 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11869 | TemplateArgs = &TemplateArgsBuffer; |
| 11870 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11871 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11872 | Expr *Base; |
| 11873 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11874 | // If we're filling in a static method where we used to have an |
| 11875 | // implicit member access, rewrite to a simple decl ref. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11876 | if (MemExpr->isImplicitAccess()) { |
| 11877 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11878 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 11879 | MemExpr->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11880 | MemExpr->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11881 | Fn, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 11882 | /*enclosing*/ false, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11883 | MemExpr->getMemberLoc(), |
| 11884 | Fn->getType(), |
| 11885 | VK_LValue, |
| 11886 | Found.getDecl(), |
| 11887 | TemplateArgs); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 11888 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11889 | DRE->setHadMultipleCandidates(MemExpr->getNumDecls() > 1); |
| 11890 | return DRE; |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 11891 | } else { |
| 11892 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 11893 | if (MemExpr->getQualifier()) |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 11894 | Loc = MemExpr->getQualifierLoc().getBeginLoc(); |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 11895 | CheckCXXThisCapture(Loc); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 11896 | Base = new (Context) CXXThisExpr(Loc, |
| 11897 | MemExpr->getBaseType(), |
| 11898 | /*isImplicit=*/true); |
| 11899 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11900 | } else |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11901 | Base = MemExpr->getBase(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11902 | |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11903 | ExprValueKind valueKind; |
| 11904 | QualType type; |
| 11905 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 11906 | valueKind = VK_LValue; |
| 11907 | type = Fn->getType(); |
| 11908 | } else { |
| 11909 | valueKind = VK_RValue; |
| 11910 | type = Context.BoundMemberTy; |
| 11911 | } |
| 11912 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11913 | MemberExpr *ME = MemberExpr::Create(Context, Base, |
| 11914 | MemExpr->isArrow(), |
| 11915 | MemExpr->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11916 | MemExpr->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11917 | Fn, |
| 11918 | Found, |
| 11919 | MemExpr->getMemberNameInfo(), |
| 11920 | TemplateArgs, |
| 11921 | type, valueKind, OK_Ordinary); |
| 11922 | ME->setHadMultipleCandidates(true); |
Richard Smith | 4a03022 | 2012-11-14 07:06:31 +0000 | [diff] [blame] | 11923 | MarkMemberReferenced(ME); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11924 | return ME; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11925 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11926 | |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11927 | llvm_unreachable("Invalid reference to overloaded function"); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11928 | } |
| 11929 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11930 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11931 | DeclAccessPair Found, |
| 11932 | FunctionDecl *Fn) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11933 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 11934 | } |
| 11935 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 11936 | } // end namespace clang |