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(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 512 | switch (ConversionKind) { |
| 513 | case StandardConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 514 | OS << "Standard conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 515 | Standard.DebugPrint(); |
| 516 | break; |
| 517 | case UserDefinedConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 518 | OS << "User-defined conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 519 | UserDefined.DebugPrint(); |
| 520 | break; |
| 521 | case EllipsisConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 522 | OS << "Ellipsis conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 523 | break; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 524 | case AmbiguousConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 525 | OS << "Ambiguous conversion"; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 526 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 527 | case BadConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 528 | OS << "Bad conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 529 | break; |
| 530 | } |
| 531 | |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 532 | OS << "\n"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 533 | } |
| 534 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 535 | void AmbiguousConversionSequence::construct() { |
| 536 | new (&conversions()) ConversionSet(); |
| 537 | } |
| 538 | |
| 539 | void AmbiguousConversionSequence::destruct() { |
| 540 | conversions().~ConversionSet(); |
| 541 | } |
| 542 | |
| 543 | void |
| 544 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 545 | FromTypePtr = O.FromTypePtr; |
| 546 | ToTypePtr = O.ToTypePtr; |
| 547 | new (&conversions()) ConversionSet(O.conversions()); |
| 548 | } |
| 549 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 550 | namespace { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 551 | // Structure used by DeductionFailureInfo to store |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 552 | // template argument information. |
| 553 | struct DFIArguments { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 554 | TemplateArgument FirstArg; |
| 555 | TemplateArgument SecondArg; |
| 556 | }; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 557 | // Structure used by DeductionFailureInfo to store |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 558 | // template parameter and template argument information. |
| 559 | struct DFIParamWithArguments : DFIArguments { |
| 560 | TemplateParameter Param; |
| 561 | }; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 562 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 563 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 564 | /// \brief Convert from Sema's representation of template deduction information |
| 565 | /// to the form used in overload-candidate information. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 566 | DeductionFailureInfo MakeDeductionFailureInfo(ASTContext &Context, |
| 567 | Sema::TemplateDeductionResult TDK, |
| 568 | TemplateDeductionInfo &Info) { |
| 569 | DeductionFailureInfo Result; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 570 | Result.Result = static_cast<unsigned>(TDK); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 571 | Result.HasDiagnostic = false; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 572 | Result.Data = 0; |
| 573 | switch (TDK) { |
| 574 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 575 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 576 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 577 | case Sema::TDK_TooManyArguments: |
| 578 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 579 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 580 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 581 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 582 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 583 | Result.Data = Info.Param.getOpaqueValue(); |
| 584 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 585 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 586 | case Sema::TDK_NonDeducedMismatch: { |
| 587 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 588 | DFIArguments *Saved = new (Context) DFIArguments; |
| 589 | Saved->FirstArg = Info.FirstArg; |
| 590 | Saved->SecondArg = Info.SecondArg; |
| 591 | Result.Data = Saved; |
| 592 | break; |
| 593 | } |
| 594 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 595 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 596 | case Sema::TDK_Underqualified: { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 597 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 598 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 599 | Saved->Param = Info.Param; |
| 600 | Saved->FirstArg = Info.FirstArg; |
| 601 | Saved->SecondArg = Info.SecondArg; |
| 602 | Result.Data = Saved; |
| 603 | break; |
| 604 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 605 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 606 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 607 | Result.Data = Info.take(); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 608 | if (Info.hasSFINAEDiagnostic()) { |
| 609 | PartialDiagnosticAt *Diag = new (Result.Diagnostic) PartialDiagnosticAt( |
| 610 | SourceLocation(), PartialDiagnostic::NullDiagnostic()); |
| 611 | Info.takeSFINAEDiagnostic(*Diag); |
| 612 | Result.HasDiagnostic = true; |
| 613 | } |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 614 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 615 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 616 | case Sema::TDK_FailedOverloadResolution: |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 617 | Result.Data = Info.Expression; |
| 618 | break; |
| 619 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 620 | case Sema::TDK_MiscellaneousDeductionFailure: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 621 | break; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 622 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 623 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 624 | return Result; |
| 625 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 626 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 627 | void DeductionFailureInfo::Destroy() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 628 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 629 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 630 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 631 | case Sema::TDK_InstantiationDepth: |
| 632 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 633 | case Sema::TDK_TooManyArguments: |
| 634 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 635 | case Sema::TDK_InvalidExplicitArguments: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 636 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 637 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 638 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 639 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 640 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 641 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | aaa045d | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 642 | // FIXME: Destroy the data? |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 643 | Data = 0; |
| 644 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 645 | |
| 646 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 647 | // FIXME: Destroy the template argument list? |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 648 | Data = 0; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 649 | if (PartialDiagnosticAt *Diag = getSFINAEDiagnostic()) { |
| 650 | Diag->~PartialDiagnosticAt(); |
| 651 | HasDiagnostic = false; |
| 652 | } |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 653 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 654 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 655 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 656 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 657 | break; |
| 658 | } |
| 659 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 660 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 661 | PartialDiagnosticAt *DeductionFailureInfo::getSFINAEDiagnostic() { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 662 | if (HasDiagnostic) |
| 663 | return static_cast<PartialDiagnosticAt*>(static_cast<void*>(Diagnostic)); |
| 664 | return 0; |
| 665 | } |
| 666 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 667 | TemplateParameter DeductionFailureInfo::getTemplateParameter() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 668 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 669 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 670 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 671 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 672 | case Sema::TDK_TooManyArguments: |
| 673 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 674 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 675 | case Sema::TDK_NonDeducedMismatch: |
| 676 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 677 | return TemplateParameter(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 678 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 679 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 680 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 681 | return TemplateParameter::getFromOpaqueValue(Data); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 682 | |
| 683 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 684 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 685 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 686 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 687 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 688 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 689 | break; |
| 690 | } |
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 | return TemplateParameter(); |
| 693 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 694 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 695 | TemplateArgumentList *DeductionFailureInfo::getTemplateArgumentList() { |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 696 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 697 | case Sema::TDK_Success: |
| 698 | case Sema::TDK_Invalid: |
| 699 | case Sema::TDK_InstantiationDepth: |
| 700 | case Sema::TDK_TooManyArguments: |
| 701 | case Sema::TDK_TooFewArguments: |
| 702 | case Sema::TDK_Incomplete: |
| 703 | case Sema::TDK_InvalidExplicitArguments: |
| 704 | case Sema::TDK_Inconsistent: |
| 705 | case Sema::TDK_Underqualified: |
| 706 | case Sema::TDK_NonDeducedMismatch: |
| 707 | case Sema::TDK_FailedOverloadResolution: |
| 708 | return 0; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 709 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 710 | case Sema::TDK_SubstitutionFailure: |
| 711 | return static_cast<TemplateArgumentList*>(Data); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 712 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 713 | // Unhandled |
| 714 | case Sema::TDK_MiscellaneousDeductionFailure: |
| 715 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | return 0; |
| 719 | } |
| 720 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 721 | const TemplateArgument *DeductionFailureInfo::getFirstArg() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 722 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 723 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 724 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 725 | case Sema::TDK_InstantiationDepth: |
| 726 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 727 | case Sema::TDK_TooManyArguments: |
| 728 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 729 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 730 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 731 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 732 | return 0; |
| 733 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 734 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 735 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 736 | case Sema::TDK_NonDeducedMismatch: |
| 737 | return &static_cast<DFIArguments*>(Data)->FirstArg; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 738 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 739 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 740 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 741 | break; |
| 742 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 743 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 744 | return 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 745 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 746 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 747 | const TemplateArgument *DeductionFailureInfo::getSecondArg() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 748 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 749 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 750 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 751 | case Sema::TDK_InstantiationDepth: |
| 752 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 753 | case Sema::TDK_TooManyArguments: |
| 754 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 755 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 756 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 757 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 758 | return 0; |
| 759 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 760 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 761 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 762 | case Sema::TDK_NonDeducedMismatch: |
| 763 | return &static_cast<DFIArguments*>(Data)->SecondArg; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 764 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 765 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 766 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 767 | break; |
| 768 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 769 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 770 | return 0; |
| 771 | } |
| 772 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 773 | Expr *DeductionFailureInfo::getExpr() { |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 774 | if (static_cast<Sema::TemplateDeductionResult>(Result) == |
| 775 | Sema::TDK_FailedOverloadResolution) |
| 776 | return static_cast<Expr*>(Data); |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
Benjamin Kramer | f5b132f | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 781 | void OverloadCandidateSet::destroyCandidates() { |
Richard Smith | e3898ac | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 782 | for (iterator i = begin(), e = end(); i != e; ++i) { |
Benjamin Kramer | 9e2822b | 2012-01-14 20:16:52 +0000 | [diff] [blame] | 783 | for (unsigned ii = 0, ie = i->NumConversions; ii != ie; ++ii) |
| 784 | i->Conversions[ii].~ImplicitConversionSequence(); |
Richard Smith | e3898ac | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 785 | if (!i->Viable && i->FailureKind == ovl_fail_bad_deduction) |
| 786 | i->DeductionFailure.Destroy(); |
| 787 | } |
Benjamin Kramer | f5b132f | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | void OverloadCandidateSet::clear() { |
| 791 | destroyCandidates(); |
Benjamin Kramer | 314f554 | 2012-01-14 19:31:39 +0000 | [diff] [blame] | 792 | NumInlineSequences = 0; |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 793 | Candidates.clear(); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 794 | Functions.clear(); |
| 795 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 796 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 797 | namespace { |
| 798 | class UnbridgedCastsSet { |
| 799 | struct Entry { |
| 800 | Expr **Addr; |
| 801 | Expr *Saved; |
| 802 | }; |
| 803 | SmallVector<Entry, 2> Entries; |
| 804 | |
| 805 | public: |
| 806 | void save(Sema &S, Expr *&E) { |
| 807 | assert(E->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 808 | Entry entry = { &E, E }; |
| 809 | Entries.push_back(entry); |
| 810 | E = S.stripARCUnbridgedCast(E); |
| 811 | } |
| 812 | |
| 813 | void restore() { |
| 814 | for (SmallVectorImpl<Entry>::iterator |
| 815 | i = Entries.begin(), e = Entries.end(); i != e; ++i) |
| 816 | *i->Addr = i->Saved; |
| 817 | } |
| 818 | }; |
| 819 | } |
| 820 | |
| 821 | /// checkPlaceholderForOverload - Do any interesting placeholder-like |
| 822 | /// preprocessing on the given expression. |
| 823 | /// |
| 824 | /// \param unbridgedCasts a collection to which to add unbridged casts; |
| 825 | /// without this, they will be immediately diagnosed as errors |
| 826 | /// |
| 827 | /// Return true on unrecoverable error. |
| 828 | static bool checkPlaceholderForOverload(Sema &S, Expr *&E, |
| 829 | UnbridgedCastsSet *unbridgedCasts = 0) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 830 | if (const BuiltinType *placeholder = E->getType()->getAsPlaceholderType()) { |
| 831 | // We can't handle overloaded expressions here because overload |
| 832 | // resolution might reasonably tweak them. |
| 833 | if (placeholder->getKind() == BuiltinType::Overload) return false; |
| 834 | |
| 835 | // If the context potentially accepts unbridged ARC casts, strip |
| 836 | // the unbridged cast and add it to the collection for later restoration. |
| 837 | if (placeholder->getKind() == BuiltinType::ARCUnbridgedCast && |
| 838 | unbridgedCasts) { |
| 839 | unbridgedCasts->save(S, E); |
| 840 | return false; |
| 841 | } |
| 842 | |
| 843 | // Go ahead and check everything else. |
| 844 | ExprResult result = S.CheckPlaceholderExpr(E); |
| 845 | if (result.isInvalid()) |
| 846 | return true; |
| 847 | |
| 848 | E = result.take(); |
| 849 | return false; |
| 850 | } |
| 851 | |
| 852 | // Nothing to do. |
| 853 | return false; |
| 854 | } |
| 855 | |
| 856 | /// checkArgPlaceholdersForOverload - Check a set of call operands for |
| 857 | /// placeholders. |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 858 | static bool checkArgPlaceholdersForOverload(Sema &S, |
| 859 | MultiExprArg Args, |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 860 | UnbridgedCastsSet &unbridged) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 861 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 862 | if (checkPlaceholderForOverload(S, Args[i], &unbridged)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 863 | return true; |
| 864 | |
| 865 | return false; |
| 866 | } |
| 867 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 868 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 869 | // overload of the declarations in Old. This routine returns false if |
| 870 | // New and Old cannot be overloaded, e.g., if New has the same |
| 871 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 872 | // declarations aren't functions (or function templates) at all. When |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 873 | // it does return false, MatchedDecl will point to the decl that New |
| 874 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 875 | // top of the underlying declaration. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 876 | // |
| 877 | // Example: Given the following input: |
| 878 | // |
| 879 | // void f(int, float); // #1 |
| 880 | // void f(int, int); // #2 |
| 881 | // int f(int, int); // #3 |
| 882 | // |
| 883 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 884 | // so IsOverload will not be used. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 885 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 886 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 887 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 888 | // (since they have different signatures), so this routine returns |
| 889 | // false; MatchedDecl is unchanged. |
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 #3, Old is an overload set containing #1 and #2. We |
| 892 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 893 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 894 | // identical (return types of functions are not part of the |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 895 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 896 | // point to the FunctionDecl for #2. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 897 | // |
| 898 | // 'NewIsUsingShadowDecl' indicates that 'New' is being introduced |
| 899 | // into a class by a using declaration. The rules for whether to hide |
| 900 | // shadow declarations ignore some properties which otherwise figure |
| 901 | // into a function template's signature. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 902 | Sema::OverloadKind |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 903 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
| 904 | NamedDecl *&Match, bool NewIsUsingDecl) { |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 905 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 906 | I != E; ++I) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 907 | NamedDecl *OldD = *I; |
| 908 | |
| 909 | bool OldIsUsingDecl = false; |
| 910 | if (isa<UsingShadowDecl>(OldD)) { |
| 911 | OldIsUsingDecl = true; |
| 912 | |
| 913 | // We can always introduce two using declarations into the same |
| 914 | // context, even if they have identical signatures. |
| 915 | if (NewIsUsingDecl) continue; |
| 916 | |
| 917 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 918 | } |
| 919 | |
| 920 | // If either declaration was introduced by a using declaration, |
| 921 | // we'll need to use slightly different rules for matching. |
| 922 | // Essentially, these rules are the normal rules, except that |
| 923 | // function templates hide function templates with different |
| 924 | // return types or template parameter lists. |
| 925 | bool UseMemberUsingDeclRules = |
John McCall | 78037ac | 2013-04-03 21:19:47 +0000 | [diff] [blame] | 926 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord() && |
| 927 | !New->getFriendObjectKind(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 928 | |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 929 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 930 | if (!IsOverload(New, OldT->getTemplatedDecl(), UseMemberUsingDeclRules)) { |
| 931 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 932 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 933 | continue; |
| 934 | } |
| 935 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 936 | Match = *I; |
| 937 | return Ovl_Match; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 938 | } |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 939 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 940 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
| 941 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 942 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 943 | continue; |
| 944 | } |
| 945 | |
Rafael Espindola | 90cc390 | 2013-04-15 12:49:13 +0000 | [diff] [blame] | 946 | if (!shouldLinkPossiblyHiddenDecl(*I, New)) |
| 947 | continue; |
| 948 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 949 | Match = *I; |
| 950 | return Ovl_Match; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 951 | } |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 952 | } else if (isa<UsingDecl>(OldD)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 953 | // We can overload with these, which can show up when doing |
| 954 | // redeclaration checks for UsingDecls. |
| 955 | assert(Old.getLookupKind() == LookupUsingDeclName); |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 956 | } else if (isa<TagDecl>(OldD)) { |
| 957 | // We can always overload with tags by hiding them. |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 958 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 959 | // Optimistically assume that an unresolved using decl will |
| 960 | // overload; if it doesn't, we'll have to diagnose during |
| 961 | // template instantiation. |
| 962 | } else { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 963 | // (C++ 13p1): |
| 964 | // Only function declarations can be overloaded; object and type |
| 965 | // declarations cannot be overloaded. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 966 | Match = *I; |
| 967 | return Ovl_NonFunction; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 968 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 969 | } |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 970 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 971 | return Ovl_Overload; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 974 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| 975 | bool UseUsingDeclRules) { |
| 976 | // C++ [basic.start.main]p2: This function shall not be overloaded. |
| 977 | if (New->isMain()) |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 978 | return false; |
Rafael Espindola | 7a525ac | 2013-01-12 01:47:40 +0000 | [diff] [blame] | 979 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 980 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 981 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 982 | |
| 983 | // C++ [temp.fct]p2: |
| 984 | // A function template can be overloaded with other function templates |
| 985 | // and with normal (non-template) functions. |
| 986 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 987 | return true; |
| 988 | |
| 989 | // Is the function New an overload of the function Old? |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 990 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 991 | QualType NewQType = Context.getCanonicalType(New->getType()); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 992 | |
| 993 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 994 | // determine whether they are overloads. If we find any mismatch |
| 995 | // in the signature, they are overloads. |
| 996 | |
| 997 | // If either of these functions is a K&R-style function (no |
| 998 | // prototype), then we consider them to have matching signatures. |
| 999 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 1000 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 1001 | return false; |
| 1002 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1003 | const FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 1004 | const FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1005 | |
| 1006 | // The signature of a function includes the types of its |
| 1007 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 1008 | // of the ellipsis; see C++ DR 357). |
| 1009 | if (OldQType != NewQType && |
| 1010 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 1011 | OldType->isVariadic() != NewType->isVariadic() || |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1012 | !FunctionArgTypesAreEqual(OldType, NewType))) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1013 | return true; |
| 1014 | |
| 1015 | // C++ [temp.over.link]p4: |
| 1016 | // The signature of a function template consists of its function |
| 1017 | // signature, its return type and its template parameter list. The names |
| 1018 | // of the template parameters are significant only for establishing the |
| 1019 | // relationship between the template parameters and the rest of the |
| 1020 | // signature. |
| 1021 | // |
| 1022 | // We check the return type and template parameter lists for function |
| 1023 | // templates first; the remaining checks follow. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 1024 | // |
| 1025 | // However, we don't consider either of these when deciding whether |
| 1026 | // a member introduced by a shadow declaration is hidden. |
| 1027 | if (!UseUsingDeclRules && NewTemplate && |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1028 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 1029 | OldTemplate->getTemplateParameters(), |
| 1030 | false, TPL_TemplateMatch) || |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1031 | OldType->getResultType() != NewType->getResultType())) |
| 1032 | return true; |
| 1033 | |
| 1034 | // If the function is a class member, its signature includes the |
Douglas Gregor | 57c9f4f | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 1035 | // 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] | 1036 | // |
| 1037 | // As part of this, also check whether one of the member functions |
| 1038 | // is static, in which case they are not overloads (C++ |
| 1039 | // 13.1p2). While not part of the definition of the signature, |
| 1040 | // this check is important to determine whether these functions |
| 1041 | // can be overloaded. |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1042 | CXXMethodDecl *OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 1043 | CXXMethodDecl *NewMethod = dyn_cast<CXXMethodDecl>(New); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1044 | if (OldMethod && NewMethod && |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1045 | !OldMethod->isStatic() && !NewMethod->isStatic()) { |
| 1046 | if (OldMethod->getRefQualifier() != NewMethod->getRefQualifier()) { |
| 1047 | if (!UseUsingDeclRules && |
| 1048 | (OldMethod->getRefQualifier() == RQ_None || |
| 1049 | NewMethod->getRefQualifier() == RQ_None)) { |
| 1050 | // C++0x [over.load]p2: |
| 1051 | // - Member function declarations with the same name and the same |
| 1052 | // parameter-type-list as well as member function template |
| 1053 | // declarations with the same name, the same parameter-type-list, and |
| 1054 | // the same template parameter lists cannot be overloaded if any of |
| 1055 | // them, but not all, have a ref-qualifier (8.3.5). |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1056 | Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1057 | << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1058 | Diag(OldMethod->getLocation(), diag::note_previous_declaration); |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1059 | } |
| 1060 | return true; |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1061 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1062 | |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1063 | // We may not have applied the implicit const for a constexpr member |
| 1064 | // function yet (because we haven't yet resolved whether this is a static |
| 1065 | // or non-static member function). Add it now, on the assumption that this |
| 1066 | // is a redeclaration of OldMethod. |
| 1067 | unsigned NewQuals = NewMethod->getTypeQualifiers(); |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1068 | if (!getLangOpts().CPlusPlus1y && NewMethod->isConstexpr() && |
Richard Smith | db2fe73 | 2013-06-25 18:46:26 +0000 | [diff] [blame] | 1069 | !isa<CXXConstructorDecl>(NewMethod)) |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1070 | NewQuals |= Qualifiers::Const; |
| 1071 | if (OldMethod->getTypeQualifiers() != NewQuals) |
| 1072 | return true; |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1073 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1074 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1075 | // The signatures match; this is not an overload. |
| 1076 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 1079 | /// \brief Checks availability of the function depending on the current |
| 1080 | /// function context. Inside an unavailable function, unavailability is ignored. |
| 1081 | /// |
| 1082 | /// \returns true if \arg FD is unavailable and current context is inside |
| 1083 | /// an available function, false otherwise. |
| 1084 | bool Sema::isFunctionConsideredUnavailable(FunctionDecl *FD) { |
| 1085 | return FD->isUnavailable() && !cast<Decl>(CurContext)->isUnavailable(); |
| 1086 | } |
| 1087 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1088 | /// \brief Tries a user-defined conversion from From to ToType. |
| 1089 | /// |
| 1090 | /// Produces an implicit conversion sequence for when a standard conversion |
| 1091 | /// is not an option. See TryImplicitConversion for more information. |
| 1092 | static ImplicitConversionSequence |
| 1093 | TryUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 1094 | bool SuppressUserConversions, |
| 1095 | bool AllowExplicit, |
| 1096 | bool InOverloadResolution, |
| 1097 | bool CStyle, |
| 1098 | bool AllowObjCWritebackConversion) { |
| 1099 | ImplicitConversionSequence ICS; |
| 1100 | |
| 1101 | if (SuppressUserConversions) { |
| 1102 | // We're not in the case above, so there is no conversion that |
| 1103 | // we can perform. |
| 1104 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1105 | return ICS; |
| 1106 | } |
| 1107 | |
| 1108 | // Attempt user-defined conversion. |
| 1109 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 1110 | OverloadingResult UserDefResult |
| 1111 | = IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, Conversions, |
| 1112 | AllowExplicit); |
| 1113 | |
| 1114 | if (UserDefResult == OR_Success) { |
| 1115 | ICS.setUserDefined(); |
| 1116 | // C++ [over.ics.user]p4: |
| 1117 | // A conversion of an expression of class type to the same class |
| 1118 | // type is given Exact Match rank, and a conversion of an |
| 1119 | // expression of class type to a base class of that type is |
| 1120 | // given Conversion rank, in spite of the fact that a copy |
| 1121 | // constructor (i.e., a user-defined conversion function) is |
| 1122 | // called for those cases. |
| 1123 | if (CXXConstructorDecl *Constructor |
| 1124 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
| 1125 | QualType FromCanon |
| 1126 | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 1127 | QualType ToCanon |
| 1128 | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 1129 | if (Constructor->isCopyConstructor() && |
| 1130 | (FromCanon == ToCanon || S.IsDerivedFrom(FromCanon, ToCanon))) { |
| 1131 | // Turn this into a "standard" conversion sequence, so that it |
| 1132 | // gets ranked with standard conversion sequences. |
| 1133 | ICS.setStandard(); |
| 1134 | ICS.Standard.setAsIdentityConversion(); |
| 1135 | ICS.Standard.setFromType(From->getType()); |
| 1136 | ICS.Standard.setAllToTypes(ToType); |
| 1137 | ICS.Standard.CopyConstructor = Constructor; |
| 1138 | if (ToCanon != FromCanon) |
| 1139 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | // C++ [over.best.ics]p4: |
| 1144 | // However, when considering the argument of a user-defined |
| 1145 | // conversion function that is a candidate by 13.3.1.3 when |
| 1146 | // invoked for the copying of the temporary in the second step |
| 1147 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 1148 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 1149 | // ellipsis conversion sequences are allowed. |
| 1150 | if (SuppressUserConversions && ICS.isUserDefined()) { |
| 1151 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
| 1152 | } |
| 1153 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
| 1154 | ICS.setAmbiguous(); |
| 1155 | ICS.Ambiguous.setFromType(From->getType()); |
| 1156 | ICS.Ambiguous.setToType(ToType); |
| 1157 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 1158 | Cand != Conversions.end(); ++Cand) |
| 1159 | if (Cand->Viable) |
| 1160 | ICS.Ambiguous.addConversion(Cand->Function); |
| 1161 | } else { |
| 1162 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1163 | } |
| 1164 | |
| 1165 | return ICS; |
| 1166 | } |
| 1167 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1168 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 1169 | /// from the given expression (Expr) to the given type (ToType). This |
| 1170 | /// function returns an implicit conversion sequence that can be used |
| 1171 | /// to perform the initialization. Given |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1172 | /// |
| 1173 | /// void f(float f); |
| 1174 | /// void g(int i) { f(i); } |
| 1175 | /// |
| 1176 | /// this routine would produce an implicit conversion sequence to |
| 1177 | /// describe the initialization of f from i, which will be a standard |
| 1178 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 1179 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 1180 | // |
| 1181 | /// Note that this routine only determines how the conversion can be |
| 1182 | /// performed; it does not actually perform the conversion. As such, |
| 1183 | /// it will not produce any diagnostics if no conversion is available, |
| 1184 | /// but will instead return an implicit conversion sequence of kind |
| 1185 | /// "BadConversion". |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1186 | /// |
| 1187 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 1188 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1189 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 1190 | /// permitted. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1191 | /// |
| 1192 | /// \param AllowObjCWritebackConversion Whether we allow the Objective-C |
| 1193 | /// writeback conversion, which allows __autoreleasing id* parameters to |
| 1194 | /// be initialized with __strong id* or __weak id* arguments. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1195 | static ImplicitConversionSequence |
| 1196 | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
| 1197 | bool SuppressUserConversions, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1198 | bool AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1199 | bool InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1200 | bool CStyle, |
| 1201 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1202 | ImplicitConversionSequence ICS; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1203 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1204 | ICS.Standard, CStyle, AllowObjCWritebackConversion)){ |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1205 | ICS.setStandard(); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1206 | return ICS; |
| 1207 | } |
| 1208 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1209 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 1210 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1211 | return ICS; |
| 1212 | } |
| 1213 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1214 | // C++ [over.ics.user]p4: |
| 1215 | // A conversion of an expression of class type to the same class |
| 1216 | // type is given Exact Match rank, and a conversion of an |
| 1217 | // expression of class type to a base class of that type is |
| 1218 | // given Conversion rank, in spite of the fact that a copy/move |
| 1219 | // constructor (i.e., a user-defined conversion function) is |
| 1220 | // called for those cases. |
| 1221 | QualType FromType = From->getType(); |
| 1222 | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1223 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
| 1224 | S.IsDerivedFrom(FromType, ToType))) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1225 | ICS.setStandard(); |
| 1226 | ICS.Standard.setAsIdentityConversion(); |
| 1227 | ICS.Standard.setFromType(FromType); |
| 1228 | ICS.Standard.setAllToTypes(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1229 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1230 | // We don't actually check at this point whether there is a valid |
| 1231 | // copy/move constructor, since overloading just assumes that it |
| 1232 | // exists. When we actually perform initialization, we'll find the |
| 1233 | // appropriate constructor to copy the returned object, if needed. |
| 1234 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1235 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1236 | // Determine whether this is considered a derived-to-base conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1237 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1238 | ICS.Standard.Second = ICK_Derived_To_Base; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1239 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1240 | return ICS; |
| 1241 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1242 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1243 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 1244 | AllowExplicit, InOverloadResolution, CStyle, |
| 1245 | AllowObjCWritebackConversion); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1248 | ImplicitConversionSequence |
| 1249 | Sema::TryImplicitConversion(Expr *From, QualType ToType, |
| 1250 | bool SuppressUserConversions, |
| 1251 | bool AllowExplicit, |
| 1252 | bool InOverloadResolution, |
| 1253 | bool CStyle, |
| 1254 | bool AllowObjCWritebackConversion) { |
| 1255 | return clang::TryImplicitConversion(*this, From, ToType, |
| 1256 | SuppressUserConversions, AllowExplicit, |
| 1257 | InOverloadResolution, CStyle, |
| 1258 | AllowObjCWritebackConversion); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1261 | /// PerformImplicitConversion - Perform an implicit conversion of the |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1262 | /// expression From to the type ToType. Returns the |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1263 | /// converted expression. Flavor is the kind of conversion we're |
| 1264 | /// performing, used in the error message. If @p AllowExplicit, |
| 1265 | /// explicit user-defined conversions are permitted. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1266 | ExprResult |
| 1267 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1268 | AssignmentAction Action, bool AllowExplicit) { |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1269 | ImplicitConversionSequence ICS; |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1270 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1273 | ExprResult |
| 1274 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1275 | AssignmentAction Action, bool AllowExplicit, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1276 | ImplicitConversionSequence& ICS) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1277 | if (checkPlaceholderForOverload(*this, From)) |
| 1278 | return ExprError(); |
| 1279 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1280 | // Objective-C ARC: Determine whether we will allow the writeback conversion. |
| 1281 | bool AllowObjCWritebackConversion |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1282 | = getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1283 | (Action == AA_Passing || Action == AA_Sending); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1284 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1285 | ICS = clang::TryImplicitConversion(*this, From, ToType, |
| 1286 | /*SuppressUserConversions=*/false, |
| 1287 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1288 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1289 | /*CStyle=*/false, |
| 1290 | AllowObjCWritebackConversion); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1291 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 1292 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1293 | |
| 1294 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1295 | /// conversion that strips "noreturn" off the nested function type. |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1296 | bool Sema::IsNoReturnConversion(QualType FromType, QualType ToType, |
| 1297 | QualType &ResultTy) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1298 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1299 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1300 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1301 | // Permit the conversion F(t __attribute__((noreturn))) -> F(t) |
| 1302 | // where F adds one of the following at most once: |
| 1303 | // - a pointer |
| 1304 | // - a member pointer |
| 1305 | // - a block pointer |
| 1306 | CanQualType CanTo = Context.getCanonicalType(ToType); |
| 1307 | CanQualType CanFrom = Context.getCanonicalType(FromType); |
| 1308 | Type::TypeClass TyClass = CanTo->getTypeClass(); |
| 1309 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1310 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) { |
| 1311 | if (TyClass == Type::Pointer) { |
| 1312 | CanTo = CanTo.getAs<PointerType>()->getPointeeType(); |
| 1313 | CanFrom = CanFrom.getAs<PointerType>()->getPointeeType(); |
| 1314 | } else if (TyClass == Type::BlockPointer) { |
| 1315 | CanTo = CanTo.getAs<BlockPointerType>()->getPointeeType(); |
| 1316 | CanFrom = CanFrom.getAs<BlockPointerType>()->getPointeeType(); |
| 1317 | } else if (TyClass == Type::MemberPointer) { |
| 1318 | CanTo = CanTo.getAs<MemberPointerType>()->getPointeeType(); |
| 1319 | CanFrom = CanFrom.getAs<MemberPointerType>()->getPointeeType(); |
| 1320 | } else { |
| 1321 | return false; |
| 1322 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1323 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1324 | TyClass = CanTo->getTypeClass(); |
| 1325 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1326 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) |
| 1327 | return false; |
| 1328 | } |
| 1329 | |
| 1330 | const FunctionType *FromFn = cast<FunctionType>(CanFrom); |
| 1331 | FunctionType::ExtInfo EInfo = FromFn->getExtInfo(); |
| 1332 | if (!EInfo.getNoReturn()) return false; |
| 1333 | |
| 1334 | FromFn = Context.adjustFunctionType(FromFn, EInfo.withNoReturn(false)); |
| 1335 | assert(QualType(FromFn, 0).isCanonical()); |
| 1336 | if (QualType(FromFn, 0) != CanTo) return false; |
| 1337 | |
| 1338 | ResultTy = ToType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1339 | return true; |
| 1340 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1341 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1342 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 1343 | /// vector conversion. |
| 1344 | /// |
| 1345 | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
| 1346 | /// conversion. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1347 | static bool IsVectorConversion(ASTContext &Context, QualType FromType, |
| 1348 | QualType ToType, ImplicitConversionKind &ICK) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1349 | // We need at least one of these types to be a vector type to have a vector |
| 1350 | // conversion. |
| 1351 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
| 1352 | return false; |
| 1353 | |
| 1354 | // Identical types require no conversions. |
| 1355 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1356 | return false; |
| 1357 | |
| 1358 | // There are no conversions between extended vector types, only identity. |
| 1359 | if (ToType->isExtVectorType()) { |
| 1360 | // There are no conversions between extended vector types other than the |
| 1361 | // identity conversion. |
| 1362 | if (FromType->isExtVectorType()) |
| 1363 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1364 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1365 | // Vector splat from any arithmetic type to a vector. |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 1366 | if (FromType->isArithmeticType()) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1367 | ICK = ICK_Vector_Splat; |
| 1368 | return true; |
| 1369 | } |
| 1370 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1371 | |
| 1372 | // We can perform the conversion between vector types in the following cases: |
| 1373 | // 1)vector types are equivalent AltiVec and GCC vector types |
| 1374 | // 2)lax vector conversions are permitted and the vector types are of the |
| 1375 | // same size |
| 1376 | if (ToType->isVectorType() && FromType->isVectorType()) { |
| 1377 | if (Context.areCompatibleVectorTypes(FromType, ToType) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1378 | (Context.getLangOpts().LaxVectorConversions && |
Chandler Carruth | c45eb9c | 2010-08-08 05:02:51 +0000 | [diff] [blame] | 1379 | (Context.getTypeSize(FromType) == Context.getTypeSize(ToType)))) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1380 | ICK = ICK_Vector_Conversion; |
| 1381 | return true; |
| 1382 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1383 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1384 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1385 | return false; |
| 1386 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1387 | |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1388 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 1389 | bool InOverloadResolution, |
| 1390 | StandardConversionSequence &SCS, |
| 1391 | bool CStyle); |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1392 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1393 | /// IsStandardConversion - Determines whether there is a standard |
| 1394 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 1395 | /// expression From to the type ToType. Standard conversion sequences |
| 1396 | /// only consider non-class types; for conversions that involve class |
| 1397 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 1398 | /// contain the standard conversion sequence required to perform this |
| 1399 | /// conversion and this routine will return true. Otherwise, this |
| 1400 | /// routine will return false and the value of SCS is unspecified. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1401 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 1402 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1403 | StandardConversionSequence &SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1404 | bool CStyle, |
| 1405 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1406 | QualType FromType = From->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1407 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1408 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1409 | SCS.setAsIdentityConversion(); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1410 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1411 | SCS.IncompatibleObjC = false; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1412 | SCS.setFromType(FromType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1413 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1414 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1415 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1416 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1417 | if (FromType->isRecordType() || ToType->isRecordType()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1418 | if (S.getLangOpts().CPlusPlus) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1419 | return false; |
| 1420 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1421 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1424 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 1425 | // array-to-pointer conversion, or function-to-pointer conversion |
| 1426 | // (C++ 4p1). |
| 1427 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1428 | if (FromType == S.Context.OverloadTy) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1429 | DeclAccessPair AccessPair; |
| 1430 | if (FunctionDecl *Fn |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1431 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1432 | AccessPair)) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1433 | // We were able to resolve the address of the overloaded function, |
| 1434 | // so we can convert to the type of that function. |
| 1435 | FromType = Fn->getType(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1436 | |
| 1437 | // we can sometimes resolve &foo<int> regardless of ToType, so check |
| 1438 | // if the type matches (identity) or we are converting to bool |
| 1439 | if (!S.Context.hasSameUnqualifiedType( |
| 1440 | S.ExtractUnqualifiedFunctionType(ToType), FromType)) { |
| 1441 | QualType resultTy; |
| 1442 | // if the function type matches except for [[noreturn]], it's ok |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1443 | if (!S.IsNoReturnConversion(FromType, |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1444 | S.ExtractUnqualifiedFunctionType(ToType), resultTy)) |
| 1445 | // otherwise, only a boolean conversion is standard |
| 1446 | if (!ToType->isBooleanType()) |
| 1447 | return false; |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1448 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1449 | |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1450 | // Check if the "from" expression is taking the address of an overloaded |
| 1451 | // function and recompute the FromType accordingly. Take advantage of the |
| 1452 | // fact that non-static member functions *must* have such an address-of |
| 1453 | // expression. |
| 1454 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn); |
| 1455 | if (Method && !Method->isStatic()) { |
| 1456 | assert(isa<UnaryOperator>(From->IgnoreParens()) && |
| 1457 | "Non-unary operator on non-static member address"); |
| 1458 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() |
| 1459 | == UO_AddrOf && |
| 1460 | "Non-address-of operator on non-static member address"); |
| 1461 | const Type *ClassType |
| 1462 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 1463 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
Chandler Carruth | fc5c8fc | 2011-03-29 18:38:10 +0000 | [diff] [blame] | 1464 | } else if (isa<UnaryOperator>(From->IgnoreParens())) { |
| 1465 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() == |
| 1466 | UO_AddrOf && |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1467 | "Non-address-of operator for overloaded function expression"); |
| 1468 | FromType = S.Context.getPointerType(FromType); |
| 1469 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1470 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1471 | // Check that we've computed the proper type after overload resolution. |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1472 | assert(S.Context.hasSameType( |
| 1473 | FromType, |
| 1474 | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1475 | } else { |
| 1476 | return false; |
| 1477 | } |
Anders Carlsson | 2bd6250 | 2010-11-04 05:28:09 +0000 | [diff] [blame] | 1478 | } |
John McCall | 2148011 | 2011-08-30 00:57:29 +0000 | [diff] [blame] | 1479 | // Lvalue-to-rvalue conversion (C++11 4.1): |
| 1480 | // A glvalue (3.10) of a non-function, non-array type T can |
| 1481 | // be converted to a prvalue. |
| 1482 | bool argIsLValue = From->isGLValue(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1483 | if (argIsLValue && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1484 | !FromType->isFunctionType() && !FromType->isArrayType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1485 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1486 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1487 | |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1488 | // C11 6.3.2.1p2: |
| 1489 | // ... if the lvalue has atomic type, the value has the non-atomic version |
| 1490 | // of the type of the lvalue ... |
| 1491 | if (const AtomicType *Atomic = FromType->getAs<AtomicType>()) |
| 1492 | FromType = Atomic->getValueType(); |
| 1493 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1494 | // If T is a non-class type, the type of the rvalue is the |
| 1495 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1496 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 1497 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1498 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1499 | } else if (FromType->isArrayType()) { |
| 1500 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1501 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1502 | |
| 1503 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 1504 | // bound of T" can be converted to an rvalue of type "pointer to |
| 1505 | // T" (C++ 4.2p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1506 | FromType = S.Context.getArrayDecayedType(FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1507 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1508 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1509 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1510 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1511 | |
| 1512 | // For the purpose of ranking in overload resolution |
| 1513 | // (13.3.3.1.1), this conversion is considered an |
| 1514 | // array-to-pointer conversion followed by a qualification |
| 1515 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1516 | SCS.Second = ICK_Identity; |
| 1517 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1518 | SCS.QualificationIncludesObjCLifetime = false; |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1519 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1520 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1521 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1522 | } else if (FromType->isFunctionType() && argIsLValue) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1523 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1524 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1525 | |
| 1526 | // An lvalue of function type T can be converted to an rvalue of |
| 1527 | // type "pointer to T." The result is a pointer to the |
| 1528 | // function. (C++ 4.3p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1529 | FromType = S.Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1530 | } else { |
| 1531 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1532 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1533 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1534 | SCS.setToType(0, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1535 | |
| 1536 | // The second conversion can be an integral promotion, floating |
| 1537 | // point promotion, integral conversion, floating point conversion, |
| 1538 | // floating-integral conversion, pointer conversion, |
| 1539 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1540 | // For overloading in C, this can also be a "compatible-type" |
| 1541 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1542 | bool IncompatibleObjC = false; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1543 | ImplicitConversionKind SecondICK = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1544 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1545 | // The unqualified versions of the types are the same: there's no |
| 1546 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1547 | SCS.Second = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1548 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1549 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1550 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1551 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1552 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1553 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1554 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1555 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1556 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1557 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1558 | SCS.Second = ICK_Complex_Promotion; |
| 1559 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1560 | } else if (ToType->isBooleanType() && |
| 1561 | (FromType->isArithmeticType() || |
| 1562 | FromType->isAnyPointerType() || |
| 1563 | FromType->isBlockPointerType() || |
| 1564 | FromType->isMemberPointerType() || |
| 1565 | FromType->isNullPtrType())) { |
| 1566 | // Boolean conversions (C++ 4.12). |
| 1567 | SCS.Second = ICK_Boolean_Conversion; |
| 1568 | FromType = S.Context.BoolTy; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1569 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1570 | ToType->isIntegralType(S.Context)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1571 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1572 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1573 | FromType = ToType.getUnqualifiedType(); |
Richard Smith | 42860f1 | 2013-05-10 20:29:50 +0000 | [diff] [blame] | 1574 | } else if (FromType->isAnyComplexType() && ToType->isAnyComplexType()) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1575 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1576 | SCS.Second = ICK_Complex_Conversion; |
| 1577 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1578 | } else if ((FromType->isAnyComplexType() && ToType->isArithmeticType()) || |
| 1579 | (ToType->isAnyComplexType() && FromType->isArithmeticType())) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1580 | // Complex-real conversions (C99 6.3.1.7) |
| 1581 | SCS.Second = ICK_Complex_Real; |
| 1582 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1583 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1584 | // Floating point conversions (C++ 4.8). |
| 1585 | SCS.Second = ICK_Floating_Conversion; |
| 1586 | FromType = ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1587 | } else if ((FromType->isRealFloatingType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1588 | ToType->isIntegralType(S.Context)) || |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1589 | (FromType->isIntegralOrUnscopedEnumerationType() && |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1590 | ToType->isRealFloatingType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1591 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1592 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1593 | FromType = ToType.getUnqualifiedType(); |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1594 | } else if (S.IsBlockPointerConversion(FromType, ToType, FromType)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1595 | SCS.Second = ICK_Block_Pointer_Conversion; |
| 1596 | } else if (AllowObjCWritebackConversion && |
| 1597 | S.isObjCWritebackConversion(FromType, ToType, FromType)) { |
| 1598 | SCS.Second = ICK_Writeback_Conversion; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1599 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1600 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1601 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1602 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1603 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1604 | FromType = FromType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1605 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1606 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1607 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1608 | SCS.Second = ICK_Pointer_Member; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1609 | } else if (IsVectorConversion(S.Context, FromType, ToType, SecondICK)) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1610 | SCS.Second = SecondICK; |
| 1611 | FromType = ToType.getUnqualifiedType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1612 | } else if (!S.getLangOpts().CPlusPlus && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1613 | S.Context.typesAreCompatible(ToType, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1614 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1615 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1616 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1617 | } else if (S.IsNoReturnConversion(FromType, ToType, FromType)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1618 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1619 | SCS.Second = ICK_NoReturn_Adjustment; |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1620 | } else if (IsTransparentUnionStandardConversion(S, From, ToType, |
| 1621 | InOverloadResolution, |
| 1622 | SCS, CStyle)) { |
| 1623 | SCS.Second = ICK_TransparentUnionConversion; |
| 1624 | FromType = ToType; |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1625 | } else if (tryAtomicConversion(S, From, ToType, InOverloadResolution, SCS, |
| 1626 | CStyle)) { |
| 1627 | // tryAtomicConversion has updated the standard conversion sequence |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1628 | // appropriately. |
| 1629 | return true; |
Guy Benyei | 6959acd | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 1630 | } else if (ToType->isEventT() && |
| 1631 | From->isIntegerConstantExpr(S.getASTContext()) && |
| 1632 | (From->EvaluateKnownConstInt(S.getASTContext()) == 0)) { |
| 1633 | SCS.Second = ICK_Zero_Event_Conversion; |
| 1634 | FromType = ToType; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1635 | } else { |
| 1636 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1637 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1638 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1639 | SCS.setToType(1, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1640 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1641 | QualType CanonFrom; |
| 1642 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1643 | // The third conversion can be a qualification conversion (C++ 4p1). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1644 | bool ObjCLifetimeConversion; |
| 1645 | if (S.IsQualificationConversion(FromType, ToType, CStyle, |
| 1646 | ObjCLifetimeConversion)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1647 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1648 | SCS.QualificationIncludesObjCLifetime = ObjCLifetimeConversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1649 | FromType = ToType; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1650 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1651 | CanonTo = S.Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1652 | } else { |
| 1653 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1654 | SCS.Third = ICK_Identity; |
| 1655 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1656 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1657 | // [...] Any difference in top-level cv-qualification is |
| 1658 | // subsumed by the initialization itself and does not constitute |
| 1659 | // a conversion. [...] |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1660 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1661 | CanonTo = S.Context.getCanonicalType(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1662 | if (CanonFrom.getLocalUnqualifiedType() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1663 | == CanonTo.getLocalUnqualifiedType() && |
Matt Arsenault | 5509f37 | 2013-02-26 21:15:54 +0000 | [diff] [blame] | 1664 | CanonFrom.getLocalQualifiers() != CanonTo.getLocalQualifiers()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1665 | FromType = ToType; |
| 1666 | CanonFrom = CanonTo; |
| 1667 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1668 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1669 | SCS.setToType(2, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1670 | |
| 1671 | // If we have not converted the argument type to the parameter type, |
| 1672 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1673 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1674 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1675 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1676 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1677 | } |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1678 | |
| 1679 | static bool |
| 1680 | IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 1681 | QualType &ToType, |
| 1682 | bool InOverloadResolution, |
| 1683 | StandardConversionSequence &SCS, |
| 1684 | bool CStyle) { |
| 1685 | |
| 1686 | const RecordType *UT = ToType->getAsUnionType(); |
| 1687 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 1688 | return false; |
| 1689 | // The field to initialize within the transparent union. |
| 1690 | RecordDecl *UD = UT->getDecl(); |
| 1691 | // It's compatible if the expression matches any of the fields. |
| 1692 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1693 | itend = UD->field_end(); |
| 1694 | it != itend; ++it) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1695 | if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS, |
| 1696 | CStyle, /*ObjCWritebackConversion=*/false)) { |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1697 | ToType = it->getType(); |
| 1698 | return true; |
| 1699 | } |
| 1700 | } |
| 1701 | return false; |
| 1702 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1703 | |
| 1704 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1705 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1706 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1707 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1708 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1709 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1710 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1711 | if (!To) { |
| 1712 | return false; |
| 1713 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1714 | |
| 1715 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1716 | // unsigned short int can be converted to an rvalue of type int if |
| 1717 | // int can represent all the values of the source type; otherwise, |
| 1718 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1719 | // int (C++ 4.5p1). |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1720 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1721 | !FromType->isEnumeralType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1722 | if (// We can promote any signed, promotable integer type to an int |
| 1723 | (FromType->isSignedIntegerType() || |
| 1724 | // We can promote any unsigned integer type whose size is |
| 1725 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1726 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1727 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1728 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1731 | return To->getKind() == BuiltinType::UInt; |
| 1732 | } |
| 1733 | |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1734 | // C++11 [conv.prom]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1735 | // A prvalue of an unscoped enumeration type whose underlying type is not |
| 1736 | // fixed (7.2) can be converted to an rvalue a prvalue of the first of the |
| 1737 | // following types that can represent all the values of the enumeration |
| 1738 | // (i.e., the values in the range bmin to bmax as described in 7.2): int, |
| 1739 | // unsigned int, long int, unsigned long int, long long int, or unsigned |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1740 | // 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] | 1741 | // values of the enumeration, an rvalue a prvalue of an unscoped enumeration |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1742 | // 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] | 1743 | // with lowest integer conversion rank (4.13) greater than the rank of long |
| 1744 | // 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] | 1745 | // there are two such extended types, the signed one is chosen. |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1746 | // C++11 [conv.prom]p4: |
| 1747 | // A prvalue of an unscoped enumeration type whose underlying type is fixed |
| 1748 | // can be converted to a prvalue of its underlying type. Moreover, if |
| 1749 | // integral promotion can be applied to its underlying type, a prvalue of an |
| 1750 | // unscoped enumeration type whose underlying type is fixed can also be |
| 1751 | // converted to a prvalue of the promoted underlying type. |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1752 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
| 1753 | // C++0x 7.2p9: Note that this implicit enum to int conversion is not |
| 1754 | // provided for a scoped enumeration. |
| 1755 | if (FromEnumType->getDecl()->isScoped()) |
| 1756 | return false; |
| 1757 | |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1758 | // We can perform an integral promotion to the underlying type of the enum, |
| 1759 | // even if that's not the promoted type. |
| 1760 | if (FromEnumType->getDecl()->isFixed()) { |
| 1761 | QualType Underlying = FromEnumType->getDecl()->getIntegerType(); |
| 1762 | return Context.hasSameUnqualifiedType(Underlying, ToType) || |
| 1763 | IsIntegralPromotion(From, Underlying, ToType); |
| 1764 | } |
| 1765 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1766 | // We have already pre-calculated the promotion type, so this is trivial. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1767 | if (ToType->isIntegerType() && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1768 | !RequireCompleteType(From->getLocStart(), FromType, 0)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1769 | return Context.hasSameUnqualifiedType(ToType, |
| 1770 | FromEnumType->getDecl()->getPromotionType()); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1771 | } |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1772 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1773 | // C++0x [conv.prom]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1774 | // A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted |
| 1775 | // to an rvalue a prvalue of the first of the following types that can |
| 1776 | // represent all the values of its underlying type: int, unsigned int, |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1777 | // 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] | 1778 | // 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] | 1779 | // underlying type, an rvalue a prvalue of type char16_t, char32_t, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1780 | // 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] | 1781 | // type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1782 | if (FromType->isAnyCharacterType() && !FromType->isCharType() && |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1783 | ToType->isIntegerType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1784 | // Determine whether the type we're converting from is signed or |
| 1785 | // unsigned. |
David Majnemer | 0ad9231 | 2011-07-22 21:09:04 +0000 | [diff] [blame] | 1786 | bool FromIsSigned = FromType->isSignedIntegerType(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1787 | uint64_t FromSize = Context.getTypeSize(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1788 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1789 | // The types we'll try to promote to, in the appropriate |
| 1790 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1791 | QualType PromoteTypes[6] = { |
| 1792 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1793 | Context.LongTy, Context.UnsignedLongTy , |
| 1794 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1795 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1796 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1797 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1798 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1799 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1800 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1801 | // We found the type that we can promote to. If this is the |
| 1802 | // type we wanted, we have a promotion. Otherwise, no |
| 1803 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1804 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1805 | } |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1810 | // rvalue of type int if int can represent all the values of the |
| 1811 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1812 | // unsigned int can represent all the values of the bit-field. If |
| 1813 | // the bit-field is larger yet, no integral promotion applies to |
| 1814 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1815 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1816 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1817 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1818 | using llvm::APSInt; |
| 1819 | if (From) |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 1820 | if (FieldDecl *MemberDecl = From->getSourceBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1821 | APSInt BitWidth; |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1822 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1823 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1824 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1825 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1826 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1827 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1828 | if (BitWidth < ToSize || |
| 1829 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1830 | return To->getKind() == BuiltinType::Int; |
| 1831 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1832 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1833 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1834 | // that fits into an unsigned int? |
| 1835 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1836 | return To->getKind() == BuiltinType::UInt; |
| 1837 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1839 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1840 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1841 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1842 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1843 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1844 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1845 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1846 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1847 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1848 | |
| 1849 | return false; |
| 1850 | } |
| 1851 | |
| 1852 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1853 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1854 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1855 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1856 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1857 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1858 | /// An rvalue of type float can be converted to an rvalue of type |
| 1859 | /// double. (C++ 4.6p1). |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1860 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1861 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1862 | return true; |
| 1863 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1864 | // C99 6.3.1.5p1: |
| 1865 | // When a float is promoted to double or long double, or a |
| 1866 | // double is promoted to long double [...]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1867 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1868 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1869 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1870 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1871 | return true; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1872 | |
| 1873 | // Half can be promoted to float. |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 1874 | if (!getLangOpts().NativeHalfType && |
| 1875 | FromBuiltin->getKind() == BuiltinType::Half && |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1876 | ToBuiltin->getKind() == BuiltinType::Float) |
| 1877 | return true; |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1880 | return false; |
| 1881 | } |
| 1882 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1883 | /// \brief Determine if a conversion is a complex promotion. |
| 1884 | /// |
| 1885 | /// A complex promotion is defined as a complex -> complex conversion |
| 1886 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1887 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1888 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1889 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1890 | if (!FromComplex) |
| 1891 | return false; |
| 1892 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1893 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1894 | if (!ToComplex) |
| 1895 | return false; |
| 1896 | |
| 1897 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1898 | ToComplex->getElementType()) || |
| 1899 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1900 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1903 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1904 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1905 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1906 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1907 | /// the right set of qualifiers on its pointee. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1908 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1909 | static QualType |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1910 | BuildSimilarlyQualifiedPointerType(const Type *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1911 | QualType ToPointee, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1912 | ASTContext &Context, |
| 1913 | bool StripObjCLifetime = false) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1914 | assert((FromPtr->getTypeClass() == Type::Pointer || |
| 1915 | FromPtr->getTypeClass() == Type::ObjCObjectPointer) && |
| 1916 | "Invalid similarly-qualified pointer type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1917 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1918 | /// Conversions to 'id' subsume cv-qualifier conversions. |
| 1919 | if (ToType->isObjCIdType() || ToType->isObjCQualifiedIdType()) |
Douglas Gregor | 143c7ac | 2010-12-06 22:09:19 +0000 | [diff] [blame] | 1920 | return ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1921 | |
| 1922 | QualType CanonFromPointee |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1923 | = Context.getCanonicalType(FromPtr->getPointeeType()); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1924 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1925 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1926 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1927 | if (StripObjCLifetime) |
| 1928 | Quals.removeObjCLifetime(); |
| 1929 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1930 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1931 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1932 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1933 | if (!ToType.isNull()) |
Douglas Gregor | af7bea5 | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1934 | return ToType.getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1935 | |
| 1936 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1937 | // already. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1938 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1939 | return Context.getObjCObjectPointerType(ToPointee); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1940 | return Context.getPointerType(ToPointee); |
| 1941 | } |
| 1942 | |
| 1943 | // Just build a canonical type that has the right qualifiers. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1944 | QualType QualifiedCanonToPointee |
| 1945 | = Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1946 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1947 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1948 | return Context.getObjCObjectPointerType(QualifiedCanonToPointee); |
| 1949 | return Context.getPointerType(QualifiedCanonToPointee); |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1950 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1951 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1952 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1953 | bool InOverloadResolution, |
| 1954 | ASTContext &Context) { |
| 1955 | // Handle value-dependent integral null pointer constants correctly. |
| 1956 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1957 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1958 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1959 | return !InOverloadResolution; |
| 1960 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1961 | return Expr->isNullPointerConstant(Context, |
| 1962 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1963 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1964 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1965 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1966 | /// IsPointerConversion - Determines whether the conversion of the |
| 1967 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1968 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1969 | /// 4.10). If so, returns true and places the converted type (that |
| 1970 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1971 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1972 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1973 | /// This routine also supports conversions to and from block pointers |
| 1974 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1975 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1976 | /// appropriate overloading rules for Objective-C, we may want to |
| 1977 | /// split the Objective-C checks into a different routine; however, |
| 1978 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1979 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1980 | /// set if the conversion is an allowed Objective-C conversion that |
| 1981 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1982 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1983 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1984 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1985 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1986 | IncompatibleObjC = false; |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1987 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, |
| 1988 | IncompatibleObjC)) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1989 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1990 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1991 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1992 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1993 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1994 | ConvertedType = ToType; |
| 1995 | return true; |
| 1996 | } |
| 1997 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1998 | // Blocks: Block pointers can be converted to void*. |
| 1999 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2000 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2001 | ConvertedType = ToType; |
| 2002 | return true; |
| 2003 | } |
| 2004 | // Blocks: A null pointer constant can be converted to a block |
| 2005 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2006 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2007 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2008 | ConvertedType = ToType; |
| 2009 | return true; |
| 2010 | } |
| 2011 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2012 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 2013 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2014 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2015 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2016 | ConvertedType = ToType; |
| 2017 | return true; |
| 2018 | } |
| 2019 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2020 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2021 | if (!ToTypePtr) |
| 2022 | return false; |
| 2023 | |
| 2024 | // 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] | 2025 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2026 | ConvertedType = ToType; |
| 2027 | return true; |
| 2028 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2029 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2030 | // Beyond this point, both types need to be pointers |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2031 | // , including objective-c pointers. |
| 2032 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2033 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2034 | !getLangOpts().ObjCAutoRefCount) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2035 | ConvertedType = BuildSimilarlyQualifiedPointerType( |
| 2036 | FromType->getAs<ObjCObjectPointerType>(), |
| 2037 | ToPointeeType, |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2038 | ToType, Context); |
| 2039 | return true; |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2040 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2041 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2042 | if (!FromTypePtr) |
| 2043 | return false; |
| 2044 | |
| 2045 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2046 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2047 | // 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] | 2048 | // pointer conversion, so don't do all of the work below. |
| 2049 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
| 2050 | return false; |
| 2051 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2052 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 2053 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 2054 | // 4.10p2). |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 2055 | if (FromPointeeType->isIncompleteOrObjectType() && |
| 2056 | ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2057 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2058 | ToPointeeType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2059 | ToType, Context, |
| 2060 | /*StripObjCLifetime=*/true); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2061 | return true; |
| 2062 | } |
| 2063 | |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2064 | // MSVC allows implicit function to void* type conversion. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2065 | if (getLangOpts().MicrosoftExt && FromPointeeType->isFunctionType() && |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2066 | ToPointeeType->isVoidType()) { |
| 2067 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2068 | ToPointeeType, |
| 2069 | ToType, Context); |
| 2070 | return true; |
| 2071 | } |
| 2072 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2073 | // When we're overloading in C, we allow a special kind of pointer |
| 2074 | // conversion for compatible-but-not-identical pointee types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2075 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2076 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2077 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2078 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2079 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2080 | return true; |
| 2081 | } |
| 2082 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2083 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2084 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2085 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 2086 | // can be converted to an rvalue of type "pointer to cv B," where |
| 2087 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 2088 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 2089 | // necessitates this conversion is ill-formed. The result of the |
| 2090 | // conversion is a pointer to the base class sub-object of the |
| 2091 | // derived class object. The null pointer value is converted to |
| 2092 | // the null pointer value of the destination type. |
| 2093 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2094 | // Note that we do not check for ambiguity or inaccessibility |
| 2095 | // here. That is handled by CheckPointerConversion. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2096 | if (getLangOpts().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2097 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | bf1764c | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 2098 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2099 | !RequireCompleteType(From->getLocStart(), FromPointeeType, 0) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2100 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2101 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2102 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2103 | ToType, Context); |
| 2104 | return true; |
| 2105 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2106 | |
Fariborz Jahanian | 5da3c08 | 2011-04-14 20:33:36 +0000 | [diff] [blame] | 2107 | if (FromPointeeType->isVectorType() && ToPointeeType->isVectorType() && |
| 2108 | Context.areCompatibleVectorTypes(FromPointeeType, ToPointeeType)) { |
| 2109 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2110 | ToPointeeType, |
| 2111 | ToType, Context); |
| 2112 | return true; |
| 2113 | } |
| 2114 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2115 | return false; |
| 2116 | } |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2117 | |
| 2118 | /// \brief Adopt the given qualifiers for the given type. |
| 2119 | static QualType AdoptQualifiers(ASTContext &Context, QualType T, Qualifiers Qs){ |
| 2120 | Qualifiers TQs = T.getQualifiers(); |
| 2121 | |
| 2122 | // Check whether qualifiers already match. |
| 2123 | if (TQs == Qs) |
| 2124 | return T; |
| 2125 | |
| 2126 | if (Qs.compatiblyIncludes(TQs)) |
| 2127 | return Context.getQualifiedType(T, Qs); |
| 2128 | |
| 2129 | return Context.getQualifiedType(T.getUnqualifiedType(), Qs); |
| 2130 | } |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2131 | |
| 2132 | /// isObjCPointerConversion - Determines whether this is an |
| 2133 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 2134 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2135 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2136 | QualType& ConvertedType, |
| 2137 | bool &IncompatibleObjC) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2138 | if (!getLangOpts().ObjC1) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2139 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2140 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2141 | // The set of qualifiers on the type we're converting from. |
| 2142 | Qualifiers FromQualifiers = FromType.getQualifiers(); |
| 2143 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2144 | // First, we handle all conversions on ObjC object pointer types. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2145 | const ObjCObjectPointerType* ToObjCPtr = |
| 2146 | ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2147 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2148 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2149 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2150 | if (ToObjCPtr && FromObjCPtr) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2151 | // If the pointee types are the same (ignoring qualifications), |
| 2152 | // then this is not a pointer conversion. |
| 2153 | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
| 2154 | FromObjCPtr->getPointeeType())) |
| 2155 | return false; |
| 2156 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2157 | // Check for compatible |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2158 | // 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] | 2159 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2160 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2161 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2162 | return true; |
| 2163 | } |
| 2164 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2165 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2166 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2167 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 2168 | /*compare=*/false)) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2169 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2170 | return true; |
| 2171 | } |
| 2172 | // Objective C++: We're able to convert from a pointer to an |
| 2173 | // interface to a pointer to a different interface. |
| 2174 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2175 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 2176 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2177 | if (getLangOpts().CPlusPlus && LHS && RHS && |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2178 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 2179 | FromObjCPtr->getPointeeType())) |
| 2180 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2181 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2182 | ToObjCPtr->getPointeeType(), |
| 2183 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2184 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2185 | return true; |
| 2186 | } |
| 2187 | |
| 2188 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 2189 | // Okay: this is some kind of implicit downcast of Objective-C |
| 2190 | // interfaces, which is permitted. However, we're going to |
| 2191 | // complain about it. |
| 2192 | IncompatibleObjC = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2193 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2194 | ToObjCPtr->getPointeeType(), |
| 2195 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2196 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2197 | return true; |
| 2198 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2199 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2200 | // 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] | 2201 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2202 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2203 | ToPointeeType = ToCPtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2204 | else if (const BlockPointerType *ToBlockPtr = |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2205 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 2206 | // 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] | 2207 | // to a block pointer type. |
| 2208 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2209 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2210 | return true; |
| 2211 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2212 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2213 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2214 | else if (FromType->getAs<BlockPointerType>() && |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2215 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2216 | // 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] | 2217 | // pointer to any object. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2218 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2219 | return true; |
| 2220 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2221 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2222 | return false; |
| 2223 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2224 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2225 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2226 | FromPointeeType = FromCPtr->getPointeeType(); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2227 | else if (const BlockPointerType *FromBlockPtr = |
| 2228 | FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2229 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2230 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2231 | return false; |
| 2232 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2233 | // If we have pointers to pointers, recursively check whether this |
| 2234 | // is an Objective-C conversion. |
| 2235 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 2236 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2237 | IncompatibleObjC)) { |
| 2238 | // We always complain about this conversion. |
| 2239 | IncompatibleObjC = true; |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2240 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2241 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2242 | return true; |
| 2243 | } |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2244 | // Allow conversion of pointee being objective-c pointer to another one; |
| 2245 | // as in I* to id. |
| 2246 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 2247 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 2248 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2249 | IncompatibleObjC)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2250 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2251 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2252 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2253 | return true; |
| 2254 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2255 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2256 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2257 | // differences in the argument and result types are in Objective-C |
| 2258 | // pointer conversions. If so, we permit the conversion (but |
| 2259 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2260 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2261 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2262 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2263 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2264 | if (FromFunctionType && ToFunctionType) { |
| 2265 | // If the function types are exactly the same, this isn't an |
| 2266 | // Objective-C pointer conversion. |
| 2267 | if (Context.getCanonicalType(FromPointeeType) |
| 2268 | == Context.getCanonicalType(ToPointeeType)) |
| 2269 | return false; |
| 2270 | |
| 2271 | // Perform the quick checks that will tell us whether these |
| 2272 | // function types are obviously different. |
| 2273 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2274 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 2275 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 2276 | return false; |
| 2277 | |
| 2278 | bool HasObjCConversion = false; |
| 2279 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 2280 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 2281 | // Okay, the types match exactly. Nothing to do. |
| 2282 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 2283 | ToFunctionType->getResultType(), |
| 2284 | ConvertedType, IncompatibleObjC)) { |
| 2285 | // Okay, we have an Objective-C pointer conversion. |
| 2286 | HasObjCConversion = true; |
| 2287 | } else { |
| 2288 | // Function types are too different. Abort. |
| 2289 | return false; |
| 2290 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2291 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2292 | // Check argument types. |
| 2293 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2294 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2295 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2296 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2297 | if (Context.getCanonicalType(FromArgType) |
| 2298 | == Context.getCanonicalType(ToArgType)) { |
| 2299 | // Okay, the types match exactly. Nothing to do. |
| 2300 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 2301 | ConvertedType, IncompatibleObjC)) { |
| 2302 | // Okay, we have an Objective-C pointer conversion. |
| 2303 | HasObjCConversion = true; |
| 2304 | } else { |
| 2305 | // Argument types are too different. Abort. |
| 2306 | return false; |
| 2307 | } |
| 2308 | } |
| 2309 | |
| 2310 | if (HasObjCConversion) { |
| 2311 | // We had an Objective-C conversion. Allow this pointer |
| 2312 | // conversion, but complain about it. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2313 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2314 | IncompatibleObjC = true; |
| 2315 | return true; |
| 2316 | } |
| 2317 | } |
| 2318 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2319 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2320 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2321 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2322 | /// \brief Determine whether this is an Objective-C writeback conversion, |
| 2323 | /// used for parameter passing when performing automatic reference counting. |
| 2324 | /// |
| 2325 | /// \param FromType The type we're converting form. |
| 2326 | /// |
| 2327 | /// \param ToType The type we're converting to. |
| 2328 | /// |
| 2329 | /// \param ConvertedType The type that will be produced after applying |
| 2330 | /// this conversion. |
| 2331 | bool Sema::isObjCWritebackConversion(QualType FromType, QualType ToType, |
| 2332 | QualType &ConvertedType) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2333 | if (!getLangOpts().ObjCAutoRefCount || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2334 | Context.hasSameUnqualifiedType(FromType, ToType)) |
| 2335 | return false; |
| 2336 | |
| 2337 | // Parameter must be a pointer to __autoreleasing (with no other qualifiers). |
| 2338 | QualType ToPointee; |
| 2339 | if (const PointerType *ToPointer = ToType->getAs<PointerType>()) |
| 2340 | ToPointee = ToPointer->getPointeeType(); |
| 2341 | else |
| 2342 | return false; |
| 2343 | |
| 2344 | Qualifiers ToQuals = ToPointee.getQualifiers(); |
| 2345 | if (!ToPointee->isObjCLifetimeType() || |
| 2346 | ToQuals.getObjCLifetime() != Qualifiers::OCL_Autoreleasing || |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2347 | !ToQuals.withoutObjCLifetime().empty()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2348 | return false; |
| 2349 | |
| 2350 | // Argument must be a pointer to __strong to __weak. |
| 2351 | QualType FromPointee; |
| 2352 | if (const PointerType *FromPointer = FromType->getAs<PointerType>()) |
| 2353 | FromPointee = FromPointer->getPointeeType(); |
| 2354 | else |
| 2355 | return false; |
| 2356 | |
| 2357 | Qualifiers FromQuals = FromPointee.getQualifiers(); |
| 2358 | if (!FromPointee->isObjCLifetimeType() || |
| 2359 | (FromQuals.getObjCLifetime() != Qualifiers::OCL_Strong && |
| 2360 | FromQuals.getObjCLifetime() != Qualifiers::OCL_Weak)) |
| 2361 | return false; |
| 2362 | |
| 2363 | // Make sure that we have compatible qualifiers. |
| 2364 | FromQuals.setObjCLifetime(Qualifiers::OCL_Autoreleasing); |
| 2365 | if (!ToQuals.compatiblyIncludes(FromQuals)) |
| 2366 | return false; |
| 2367 | |
| 2368 | // Remove qualifiers from the pointee type we're converting from; they |
| 2369 | // aren't used in the compatibility check belong, and we'll be adding back |
| 2370 | // qualifiers (with __autoreleasing) if the compatibility check succeeds. |
| 2371 | FromPointee = FromPointee.getUnqualifiedType(); |
| 2372 | |
| 2373 | // The unqualified form of the pointee types must be compatible. |
| 2374 | ToPointee = ToPointee.getUnqualifiedType(); |
| 2375 | bool IncompatibleObjC; |
| 2376 | if (Context.typesAreCompatible(FromPointee, ToPointee)) |
| 2377 | FromPointee = ToPointee; |
| 2378 | else if (!isObjCPointerConversion(FromPointee, ToPointee, FromPointee, |
| 2379 | IncompatibleObjC)) |
| 2380 | return false; |
| 2381 | |
| 2382 | /// \brief Construct the type we're converting to, which is a pointer to |
| 2383 | /// __autoreleasing pointee. |
| 2384 | FromPointee = Context.getQualifiedType(FromPointee, FromQuals); |
| 2385 | ConvertedType = Context.getPointerType(FromPointee); |
| 2386 | return true; |
| 2387 | } |
| 2388 | |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2389 | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
| 2390 | QualType& ConvertedType) { |
| 2391 | QualType ToPointeeType; |
| 2392 | if (const BlockPointerType *ToBlockPtr = |
| 2393 | ToType->getAs<BlockPointerType>()) |
| 2394 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 2395 | else |
| 2396 | return false; |
| 2397 | |
| 2398 | QualType FromPointeeType; |
| 2399 | if (const BlockPointerType *FromBlockPtr = |
| 2400 | FromType->getAs<BlockPointerType>()) |
| 2401 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2402 | else |
| 2403 | return false; |
| 2404 | // We have pointer to blocks, check whether the only |
| 2405 | // differences in the argument and result types are in Objective-C |
| 2406 | // pointer conversions. If so, we permit the conversion. |
| 2407 | |
| 2408 | const FunctionProtoType *FromFunctionType |
| 2409 | = FromPointeeType->getAs<FunctionProtoType>(); |
| 2410 | const FunctionProtoType *ToFunctionType |
| 2411 | = ToPointeeType->getAs<FunctionProtoType>(); |
| 2412 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2413 | if (!FromFunctionType || !ToFunctionType) |
| 2414 | return false; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2415 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2416 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2417 | return true; |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2418 | |
| 2419 | // Perform the quick checks that will tell us whether these |
| 2420 | // function types are obviously different. |
| 2421 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2422 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
| 2423 | return false; |
| 2424 | |
| 2425 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
| 2426 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
| 2427 | if (FromEInfo != ToEInfo) |
| 2428 | return false; |
| 2429 | |
| 2430 | bool IncompatibleObjC = false; |
Fariborz Jahanian | 462dae5 | 2011-02-13 20:11:42 +0000 | [diff] [blame] | 2431 | if (Context.hasSameType(FromFunctionType->getResultType(), |
| 2432 | ToFunctionType->getResultType())) { |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2433 | // Okay, the types match exactly. Nothing to do. |
| 2434 | } else { |
| 2435 | QualType RHS = FromFunctionType->getResultType(); |
| 2436 | QualType LHS = ToFunctionType->getResultType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2437 | if ((!getLangOpts().CPlusPlus || !RHS->isRecordType()) && |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2438 | !RHS.hasQualifiers() && LHS.hasQualifiers()) |
| 2439 | LHS = LHS.getUnqualifiedType(); |
| 2440 | |
| 2441 | if (Context.hasSameType(RHS,LHS)) { |
| 2442 | // OK exact match. |
| 2443 | } else if (isObjCPointerConversion(RHS, LHS, |
| 2444 | ConvertedType, IncompatibleObjC)) { |
| 2445 | if (IncompatibleObjC) |
| 2446 | return false; |
| 2447 | // Okay, we have an Objective-C pointer conversion. |
| 2448 | } |
| 2449 | else |
| 2450 | return false; |
| 2451 | } |
| 2452 | |
| 2453 | // Check argument types. |
| 2454 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2455 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2456 | IncompatibleObjC = false; |
| 2457 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2458 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2459 | if (Context.hasSameType(FromArgType, ToArgType)) { |
| 2460 | // Okay, the types match exactly. Nothing to do. |
| 2461 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
| 2462 | ConvertedType, IncompatibleObjC)) { |
| 2463 | if (IncompatibleObjC) |
| 2464 | return false; |
| 2465 | // Okay, we have an Objective-C pointer conversion. |
| 2466 | } else |
| 2467 | // Argument types are too different. Abort. |
| 2468 | return false; |
| 2469 | } |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 2470 | if (LangOpts.ObjCAutoRefCount && |
| 2471 | !Context.FunctionTypesMatchOnNSConsumedAttrs(FromFunctionType, |
| 2472 | ToFunctionType)) |
| 2473 | return false; |
Fariborz Jahanian | f9d9527 | 2011-09-28 20:22:05 +0000 | [diff] [blame] | 2474 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2475 | ConvertedType = ToType; |
| 2476 | return true; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2477 | } |
| 2478 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2479 | enum { |
| 2480 | ft_default, |
| 2481 | ft_different_class, |
| 2482 | ft_parameter_arity, |
| 2483 | ft_parameter_mismatch, |
| 2484 | ft_return_type, |
| 2485 | ft_qualifer_mismatch |
| 2486 | }; |
| 2487 | |
| 2488 | /// HandleFunctionTypeMismatch - Gives diagnostic information for differeing |
| 2489 | /// function types. Catches different number of parameter, mismatch in |
| 2490 | /// parameter types, and different return types. |
| 2491 | void Sema::HandleFunctionTypeMismatch(PartialDiagnostic &PDiag, |
| 2492 | QualType FromType, QualType ToType) { |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2493 | // If either type is not valid, include no extra info. |
| 2494 | if (FromType.isNull() || ToType.isNull()) { |
| 2495 | PDiag << ft_default; |
| 2496 | return; |
| 2497 | } |
| 2498 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2499 | // Get the function type from the pointers. |
| 2500 | if (FromType->isMemberPointerType() && ToType->isMemberPointerType()) { |
| 2501 | const MemberPointerType *FromMember = FromType->getAs<MemberPointerType>(), |
| 2502 | *ToMember = ToType->getAs<MemberPointerType>(); |
| 2503 | if (FromMember->getClass() != ToMember->getClass()) { |
| 2504 | PDiag << ft_different_class << QualType(ToMember->getClass(), 0) |
| 2505 | << QualType(FromMember->getClass(), 0); |
| 2506 | return; |
| 2507 | } |
| 2508 | FromType = FromMember->getPointeeType(); |
| 2509 | ToType = ToMember->getPointeeType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2510 | } |
| 2511 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2512 | if (FromType->isPointerType()) |
| 2513 | FromType = FromType->getPointeeType(); |
| 2514 | if (ToType->isPointerType()) |
| 2515 | ToType = ToType->getPointeeType(); |
| 2516 | |
| 2517 | // Remove references. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2518 | FromType = FromType.getNonReferenceType(); |
| 2519 | ToType = ToType.getNonReferenceType(); |
| 2520 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2521 | // Don't print extra info for non-specialized template functions. |
| 2522 | if (FromType->isInstantiationDependentType() && |
| 2523 | !FromType->getAs<TemplateSpecializationType>()) { |
| 2524 | PDiag << ft_default; |
| 2525 | return; |
| 2526 | } |
| 2527 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2528 | // No extra info for same types. |
| 2529 | if (Context.hasSameType(FromType, ToType)) { |
| 2530 | PDiag << ft_default; |
| 2531 | return; |
| 2532 | } |
| 2533 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2534 | const FunctionProtoType *FromFunction = FromType->getAs<FunctionProtoType>(), |
| 2535 | *ToFunction = ToType->getAs<FunctionProtoType>(); |
| 2536 | |
| 2537 | // Both types need to be function types. |
| 2538 | if (!FromFunction || !ToFunction) { |
| 2539 | PDiag << ft_default; |
| 2540 | return; |
| 2541 | } |
| 2542 | |
| 2543 | if (FromFunction->getNumArgs() != ToFunction->getNumArgs()) { |
| 2544 | PDiag << ft_parameter_arity << ToFunction->getNumArgs() |
| 2545 | << FromFunction->getNumArgs(); |
| 2546 | return; |
| 2547 | } |
| 2548 | |
| 2549 | // Handle different parameter types. |
| 2550 | unsigned ArgPos; |
| 2551 | if (!FunctionArgTypesAreEqual(FromFunction, ToFunction, &ArgPos)) { |
| 2552 | PDiag << ft_parameter_mismatch << ArgPos + 1 |
| 2553 | << ToFunction->getArgType(ArgPos) |
| 2554 | << FromFunction->getArgType(ArgPos); |
| 2555 | return; |
| 2556 | } |
| 2557 | |
| 2558 | // Handle different return type. |
| 2559 | if (!Context.hasSameType(FromFunction->getResultType(), |
| 2560 | ToFunction->getResultType())) { |
| 2561 | PDiag << ft_return_type << ToFunction->getResultType() |
| 2562 | << FromFunction->getResultType(); |
| 2563 | return; |
| 2564 | } |
| 2565 | |
| 2566 | unsigned FromQuals = FromFunction->getTypeQuals(), |
| 2567 | ToQuals = ToFunction->getTypeQuals(); |
| 2568 | if (FromQuals != ToQuals) { |
| 2569 | PDiag << ft_qualifer_mismatch << ToQuals << FromQuals; |
| 2570 | return; |
| 2571 | } |
| 2572 | |
| 2573 | // Unable to find a difference, so add no extra info. |
| 2574 | PDiag << ft_default; |
| 2575 | } |
| 2576 | |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2577 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
Douglas Gregor | dec1cc4 | 2011-12-15 17:15:07 +0000 | [diff] [blame] | 2578 | /// for equality of their argument types. Caller has already checked that |
Eli Friedman | 0601700 | 2013-06-18 22:41:37 +0000 | [diff] [blame] | 2579 | /// they have same number of arguments. If the parameters are different, |
| 2580 | /// ArgPos will have the parameter index of the first different parameter. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2581 | bool Sema::FunctionArgTypesAreEqual(const FunctionProtoType *OldType, |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2582 | const FunctionProtoType *NewType, |
| 2583 | unsigned *ArgPos) { |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2584 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 2585 | N = NewType->arg_type_begin(), |
| 2586 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
Richard Trieu | 7ea491c | 2013-08-09 21:42:32 +0000 | [diff] [blame^] | 2587 | if (!Context.hasSameType(O->getUnqualifiedType(), |
| 2588 | N->getUnqualifiedType())) { |
Larisse Voufo | 3151b7c | 2013-08-06 03:57:41 +0000 | [diff] [blame] | 2589 | if (ArgPos) *ArgPos = O - OldType->arg_type_begin(); |
| 2590 | return false; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2591 | } |
| 2592 | } |
| 2593 | return true; |
| 2594 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2595 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2596 | /// CheckPointerConversion - Check the pointer conversion from the |
| 2597 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2598 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2599 | /// conversions for which IsPointerConversion has already returned |
| 2600 | /// true. It returns true and produces a diagnostic if there was an |
| 2601 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2602 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2603 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2604 | CXXCastPath& BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2605 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2606 | QualType FromType = From->getType(); |
Argyrios Kyrtzidis | b335872 | 2010-09-28 14:54:11 +0000 | [diff] [blame] | 2607 | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2608 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2609 | Kind = CK_BitCast; |
| 2610 | |
David Blaikie | 50800fc | 2012-08-08 17:33:31 +0000 | [diff] [blame] | 2611 | if (!IsCStyleOrFunctionalCast && !FromType->isAnyPointerType() && |
| 2612 | From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull) == |
| 2613 | Expr::NPCK_ZeroExpression) { |
| 2614 | if (Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy)) |
| 2615 | DiagRuntimeBehavior(From->getExprLoc(), From, |
| 2616 | PDiag(diag::warn_impcast_bool_to_null_pointer) |
| 2617 | << ToType << From->getSourceRange()); |
| 2618 | else if (!isUnevaluatedContext()) |
| 2619 | Diag(From->getExprLoc(), diag::warn_non_literal_null_pointer) |
| 2620 | << ToType << From->getSourceRange(); |
| 2621 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2622 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
| 2623 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2624 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 2625 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 2626 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2627 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 2628 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2629 | // We must have a derived-to-base conversion. Check an |
| 2630 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2631 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 2632 | From->getExprLoc(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2633 | From->getSourceRange(), &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2634 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2635 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2636 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2637 | // The conversion was successful. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2638 | Kind = CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2639 | } |
| 2640 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2641 | } else if (const ObjCObjectPointerType *ToPtrType = |
| 2642 | ToType->getAs<ObjCObjectPointerType>()) { |
| 2643 | if (const ObjCObjectPointerType *FromPtrType = |
| 2644 | FromType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2645 | // Objective-C++ conversions are always okay. |
| 2646 | // FIXME: We should have a different class of conversions for the |
| 2647 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2648 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2649 | return false; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2650 | } else if (FromType->isBlockPointerType()) { |
| 2651 | Kind = CK_BlockPointerToObjCPointerCast; |
| 2652 | } else { |
| 2653 | Kind = CK_CPointerToObjCPointerCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2654 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2655 | } else if (ToType->isBlockPointerType()) { |
| 2656 | if (!FromType->isBlockPointerType()) |
| 2657 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2658 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2659 | |
| 2660 | // We shouldn't fall into this case unless it's valid for other |
| 2661 | // reasons. |
| 2662 | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 2663 | Kind = CK_NullToPointer; |
| 2664 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2665 | return false; |
| 2666 | } |
| 2667 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2668 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 2669 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 2670 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 2671 | /// If so, returns true and places the converted type (that might differ from |
| 2672 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 2673 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2674 | QualType ToType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2675 | bool InOverloadResolution, |
| 2676 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2677 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2678 | if (!ToTypePtr) |
| 2679 | return false; |
| 2680 | |
| 2681 | // 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] | 2682 | if (From->isNullPointerConstant(Context, |
| 2683 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 2684 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2685 | ConvertedType = ToType; |
| 2686 | return true; |
| 2687 | } |
| 2688 | |
| 2689 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2690 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2691 | if (!FromTypePtr) |
| 2692 | return false; |
| 2693 | |
| 2694 | // A pointer to member of B can be converted to a pointer to member of D, |
| 2695 | // where D is derived from B (C++ 4.11p2). |
| 2696 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 2697 | QualType ToClass(ToTypePtr->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2698 | |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2699 | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2700 | !RequireCompleteType(From->getLocStart(), ToClass, 0) && |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2701 | IsDerivedFrom(ToClass, FromClass)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2702 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 2703 | ToClass.getTypePtr()); |
| 2704 | return true; |
| 2705 | } |
| 2706 | |
| 2707 | return false; |
| 2708 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2709 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2710 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 2711 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2712 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2713 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 2714 | /// true and produces a diagnostic if there was an error, or returns false |
| 2715 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2716 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2717 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2718 | CXXCastPath &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2719 | bool IgnoreBaseAccess) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2720 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2721 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2722 | if (!FromPtrType) { |
| 2723 | // This must be a null pointer to member pointer conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2724 | assert(From->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2725 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2726 | "Expr must be null pointer constant!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2727 | Kind = CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2728 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2729 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2730 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2731 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2732 | assert(ToPtrType && "No member pointer cast has a target type " |
| 2733 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2734 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2735 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 2736 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2737 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2738 | // FIXME: What about dependent types? |
| 2739 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 2740 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2741 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2742 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 2743 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2744 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 2745 | assert(DerivationOkay && |
| 2746 | "Should not have been called if derivation isn't OK."); |
| 2747 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2748 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2749 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 2750 | getUnqualifiedType())) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2751 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 2752 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 2753 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 2754 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2755 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2756 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2757 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2758 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 2759 | << FromClass << ToClass << QualType(VBase, 0) |
| 2760 | << From->getSourceRange(); |
| 2761 | return true; |
| 2762 | } |
| 2763 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2764 | if (!IgnoreBaseAccess) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2765 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 2766 | Paths.front(), |
| 2767 | diag::err_downcast_from_inaccessible_base); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2768 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2769 | // Must be a base to derived member conversion. |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2770 | BuildBasePathArray(Paths, BasePath); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2771 | Kind = CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2772 | return false; |
| 2773 | } |
| 2774 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2775 | /// IsQualificationConversion - Determines whether the conversion from |
| 2776 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 2777 | /// (C++ 4.4). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2778 | /// |
| 2779 | /// \param ObjCLifetimeConversion Output parameter that will be set to indicate |
| 2780 | /// when the qualification conversion involves a change in the Objective-C |
| 2781 | /// object lifetime. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2782 | bool |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2783 | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2784 | bool CStyle, bool &ObjCLifetimeConversion) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2785 | FromType = Context.getCanonicalType(FromType); |
| 2786 | ToType = Context.getCanonicalType(ToType); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2787 | ObjCLifetimeConversion = false; |
| 2788 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2789 | // If FromType and ToType are the same type, this is not a |
| 2790 | // qualification conversion. |
Sebastian Redl | 22c9240 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 2791 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2792 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2793 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2794 | // (C++ 4.4p4): |
| 2795 | // A conversion can add cv-qualifiers at levels other than the first |
| 2796 | // in multi-level pointers, subject to the following rules: [...] |
| 2797 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2798 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2799 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2800 | // Within each iteration of the loop, we check the qualifiers to |
| 2801 | // determine if this still looks like a qualification |
| 2802 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2803 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2804 | // until there are no more pointers or pointers-to-members left to |
| 2805 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2806 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2807 | |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2808 | Qualifiers FromQuals = FromType.getQualifiers(); |
| 2809 | Qualifiers ToQuals = ToType.getQualifiers(); |
| 2810 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2811 | // Objective-C ARC: |
| 2812 | // Check Objective-C lifetime conversions. |
| 2813 | if (FromQuals.getObjCLifetime() != ToQuals.getObjCLifetime() && |
| 2814 | UnwrappedAnyPointer) { |
| 2815 | if (ToQuals.compatiblyIncludesObjCLifetime(FromQuals)) { |
| 2816 | ObjCLifetimeConversion = true; |
| 2817 | FromQuals.removeObjCLifetime(); |
| 2818 | ToQuals.removeObjCLifetime(); |
| 2819 | } else { |
| 2820 | // Qualification conversions cannot cast between different |
| 2821 | // Objective-C lifetime qualifiers. |
| 2822 | return false; |
| 2823 | } |
| 2824 | } |
| 2825 | |
Douglas Gregor | 377e1bd | 2011-05-08 06:09:53 +0000 | [diff] [blame] | 2826 | // Allow addition/removal of GC attributes but not changing GC attributes. |
| 2827 | if (FromQuals.getObjCGCAttr() != ToQuals.getObjCGCAttr() && |
| 2828 | (!FromQuals.hasObjCGCAttr() || !ToQuals.hasObjCGCAttr())) { |
| 2829 | FromQuals.removeObjCGCAttr(); |
| 2830 | ToQuals.removeObjCGCAttr(); |
| 2831 | } |
| 2832 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2833 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 2834 | // 2,j, and similarly for volatile. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2835 | if (!CStyle && !ToQuals.compatiblyIncludes(FromQuals)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2836 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2837 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2838 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 2839 | // every cv for 0 < k < j. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2840 | if (!CStyle && FromQuals.getCVRQualifiers() != ToQuals.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2841 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2842 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2843 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2844 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 2845 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2846 | PreviousToQualsIncludeConst |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2847 | = PreviousToQualsIncludeConst && ToQuals.hasConst(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2848 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2849 | |
| 2850 | // We are left with FromType and ToType being the pointee types |
| 2851 | // after unwrapping the original FromType and ToType the same number |
| 2852 | // of types. If we unwrapped any pointers, and if FromType and |
| 2853 | // ToType have the same unqualified type (since we checked |
| 2854 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2855 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2858 | /// \brief - Determine whether this is a conversion from a scalar type to an |
| 2859 | /// atomic type. |
| 2860 | /// |
| 2861 | /// If successful, updates \c SCS's second and third steps in the conversion |
| 2862 | /// sequence to finish the conversion. |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 2863 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 2864 | bool InOverloadResolution, |
| 2865 | StandardConversionSequence &SCS, |
| 2866 | bool CStyle) { |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2867 | const AtomicType *ToAtomic = ToType->getAs<AtomicType>(); |
| 2868 | if (!ToAtomic) |
| 2869 | return false; |
| 2870 | |
| 2871 | StandardConversionSequence InnerSCS; |
| 2872 | if (!IsStandardConversion(S, From, ToAtomic->getValueType(), |
| 2873 | InOverloadResolution, InnerSCS, |
| 2874 | CStyle, /*AllowObjCWritebackConversion=*/false)) |
| 2875 | return false; |
| 2876 | |
| 2877 | SCS.Second = InnerSCS.Second; |
| 2878 | SCS.setToType(1, InnerSCS.getToType(1)); |
| 2879 | SCS.Third = InnerSCS.Third; |
| 2880 | SCS.QualificationIncludesObjCLifetime |
| 2881 | = InnerSCS.QualificationIncludesObjCLifetime; |
| 2882 | SCS.setToType(2, InnerSCS.getToType(2)); |
| 2883 | return true; |
| 2884 | } |
| 2885 | |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2886 | static bool isFirstArgumentCompatibleWithType(ASTContext &Context, |
| 2887 | CXXConstructorDecl *Constructor, |
| 2888 | QualType Type) { |
| 2889 | const FunctionProtoType *CtorType = |
| 2890 | Constructor->getType()->getAs<FunctionProtoType>(); |
| 2891 | if (CtorType->getNumArgs() > 0) { |
| 2892 | QualType FirstArg = CtorType->getArgType(0); |
| 2893 | if (Context.hasSameUnqualifiedType(Type, FirstArg.getNonReferenceType())) |
| 2894 | return true; |
| 2895 | } |
| 2896 | return false; |
| 2897 | } |
| 2898 | |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2899 | static OverloadingResult |
| 2900 | IsInitializerListConstructorConversion(Sema &S, Expr *From, QualType ToType, |
| 2901 | CXXRecordDecl *To, |
| 2902 | UserDefinedConversionSequence &User, |
| 2903 | OverloadCandidateSet &CandidateSet, |
| 2904 | bool AllowExplicit) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2905 | DeclContext::lookup_result R = S.LookupConstructors(To); |
| 2906 | for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2907 | Con != ConEnd; ++Con) { |
| 2908 | NamedDecl *D = *Con; |
| 2909 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2910 | |
| 2911 | // Find the constructor (which may be a template). |
| 2912 | CXXConstructorDecl *Constructor = 0; |
| 2913 | FunctionTemplateDecl *ConstructorTmpl |
| 2914 | = dyn_cast<FunctionTemplateDecl>(D); |
| 2915 | if (ConstructorTmpl) |
| 2916 | Constructor |
| 2917 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 2918 | else |
| 2919 | Constructor = cast<CXXConstructorDecl>(D); |
| 2920 | |
| 2921 | bool Usable = !Constructor->isInvalidDecl() && |
| 2922 | S.isInitListConstructor(Constructor) && |
| 2923 | (AllowExplicit || !Constructor->isExplicit()); |
| 2924 | if (Usable) { |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2925 | // If the first argument is (a reference to) the target type, |
| 2926 | // suppress conversions. |
| 2927 | bool SuppressUserConversions = |
| 2928 | isFirstArgumentCompatibleWithType(S.Context, Constructor, ToType); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2929 | if (ConstructorTmpl) |
| 2930 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2931 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2932 | From, CandidateSet, |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2933 | SuppressUserConversions); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2934 | else |
| 2935 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2936 | From, CandidateSet, |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2937 | SuppressUserConversions); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2938 | } |
| 2939 | } |
| 2940 | |
| 2941 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 2942 | |
| 2943 | OverloadCandidateSet::iterator Best; |
| 2944 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
| 2945 | case OR_Success: { |
| 2946 | // Record the standard conversion we used and the conversion function. |
| 2947 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2948 | QualType ThisType = Constructor->getThisType(S.Context); |
| 2949 | // Initializer lists don't have conversions as such. |
| 2950 | User.Before.setAsIdentityConversion(); |
| 2951 | User.HadMultipleCandidates = HadMultipleCandidates; |
| 2952 | User.ConversionFunction = Constructor; |
| 2953 | User.FoundConversionFunction = Best->FoundDecl; |
| 2954 | User.After.setAsIdentityConversion(); |
| 2955 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 2956 | User.After.setAllToTypes(ToType); |
| 2957 | return OR_Success; |
| 2958 | } |
| 2959 | |
| 2960 | case OR_No_Viable_Function: |
| 2961 | return OR_No_Viable_Function; |
| 2962 | case OR_Deleted: |
| 2963 | return OR_Deleted; |
| 2964 | case OR_Ambiguous: |
| 2965 | return OR_Ambiguous; |
| 2966 | } |
| 2967 | |
| 2968 | llvm_unreachable("Invalid OverloadResult!"); |
| 2969 | } |
| 2970 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2971 | /// Determines whether there is a user-defined conversion sequence |
| 2972 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 2973 | /// ToType. If such a conversion exists, User will contain the |
| 2974 | /// user-defined conversion sequence that performs such a conversion |
| 2975 | /// and this routine will return true. Otherwise, this routine returns |
| 2976 | /// false and User is unspecified. |
| 2977 | /// |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2978 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 2979 | /// "explicit" conversion functions as well as non-explicit conversion |
| 2980 | /// functions (C++0x [class.conv.fct]p2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2981 | static OverloadingResult |
| 2982 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2983 | UserDefinedConversionSequence &User, |
| 2984 | OverloadCandidateSet &CandidateSet, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2985 | bool AllowExplicit) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2986 | // Whether we will only visit constructors. |
| 2987 | bool ConstructorsOnly = false; |
| 2988 | |
| 2989 | // If the type we are conversion to is a class type, enumerate its |
| 2990 | // constructors. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2991 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2992 | // C++ [over.match.ctor]p1: |
| 2993 | // When objects of class type are direct-initialized (8.5), or |
| 2994 | // copy-initialized from an expression of the same or a |
| 2995 | // derived class type (8.5), overload resolution selects the |
| 2996 | // constructor. [...] For copy-initialization, the candidate |
| 2997 | // functions are all the converting constructors (12.3.1) of |
| 2998 | // that class. The argument list is the expression-list within |
| 2999 | // the parentheses of the initializer. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3000 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3001 | (From->getType()->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3002 | S.IsDerivedFrom(From->getType(), ToType))) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3003 | ConstructorsOnly = true; |
| 3004 | |
Benjamin Kramer | 63b6ebe | 2012-11-23 17:04:52 +0000 | [diff] [blame] | 3005 | S.RequireCompleteType(From->getExprLoc(), ToType, 0); |
Argyrios Kyrtzidis | e36bca6 | 2011-04-22 17:45:37 +0000 | [diff] [blame] | 3006 | // RequireCompleteType may have returned true due to some invalid decl |
| 3007 | // during template instantiation, but ToType may be complete enough now |
| 3008 | // to try to recover. |
| 3009 | if (ToType->isIncompleteType()) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 3010 | // We're not going to find any constructors. |
| 3011 | } else if (CXXRecordDecl *ToRecordDecl |
| 3012 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3013 | |
| 3014 | Expr **Args = &From; |
| 3015 | unsigned NumArgs = 1; |
| 3016 | bool ListInitializing = false; |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3017 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(From)) { |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3018 | // But first, see if there is an init-list-contructor that will work. |
| 3019 | OverloadingResult Result = IsInitializerListConstructorConversion( |
| 3020 | S, From, ToType, ToRecordDecl, User, CandidateSet, AllowExplicit); |
| 3021 | if (Result != OR_No_Viable_Function) |
| 3022 | return Result; |
| 3023 | // Never mind. |
| 3024 | CandidateSet.clear(); |
| 3025 | |
| 3026 | // If we're list-initializing, we pass the individual elements as |
| 3027 | // arguments, not the entire list. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3028 | Args = InitList->getInits(); |
| 3029 | NumArgs = InitList->getNumInits(); |
| 3030 | ListInitializing = true; |
| 3031 | } |
| 3032 | |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3033 | DeclContext::lookup_result R = S.LookupConstructors(ToRecordDecl); |
| 3034 | for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3035 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3036 | NamedDecl *D = *Con; |
| 3037 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3038 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3039 | // Find the constructor (which may be a template). |
| 3040 | CXXConstructorDecl *Constructor = 0; |
| 3041 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3042 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3043 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3044 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3045 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 3046 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3047 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3048 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3049 | bool Usable = !Constructor->isInvalidDecl(); |
| 3050 | if (ListInitializing) |
| 3051 | Usable = Usable && (AllowExplicit || !Constructor->isExplicit()); |
| 3052 | else |
| 3053 | Usable = Usable &&Constructor->isConvertingConstructor(AllowExplicit); |
| 3054 | if (Usable) { |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3055 | bool SuppressUserConversions = !ConstructorsOnly; |
| 3056 | if (SuppressUserConversions && ListInitializing) { |
| 3057 | SuppressUserConversions = false; |
| 3058 | if (NumArgs == 1) { |
| 3059 | // If the first argument is (a reference to) the target type, |
| 3060 | // suppress conversions. |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 3061 | SuppressUserConversions = isFirstArgumentCompatibleWithType( |
| 3062 | S.Context, Constructor, ToType); |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3063 | } |
| 3064 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3065 | if (ConstructorTmpl) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3066 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3067 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3068 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3069 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3070 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 3071 | // Allow one user-defined conversion when user specifies a |
| 3072 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3073 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3074 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3075 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3076 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3077 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3078 | } |
| 3079 | } |
| 3080 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3081 | // Enumerate conversion functions, if we're allowed to. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3082 | if (ConstructorsOnly || isa<InitListExpr>(From)) { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3083 | } else if (S.RequireCompleteType(From->getLocStart(), From->getType(), 0)) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3084 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3085 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3086 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3087 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3088 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 3089 | // Add all of the conversion functions as candidates. |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3090 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3091 | CXXRecordDecl::conversion_iterator> |
| 3092 | Conversions = FromRecordDecl->getVisibleConversionFunctions(); |
| 3093 | for (CXXRecordDecl::conversion_iterator |
| 3094 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3095 | DeclAccessPair FoundDecl = I.getPair(); |
| 3096 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3097 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3098 | if (isa<UsingShadowDecl>(D)) |
| 3099 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3100 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3101 | CXXConversionDecl *Conv; |
| 3102 | FunctionTemplateDecl *ConvTemplate; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3103 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 3104 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3105 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3106 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3107 | |
| 3108 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3109 | if (ConvTemplate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3110 | S.AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
| 3111 | ActingContext, From, ToType, |
| 3112 | CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3113 | else |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3114 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, |
| 3115 | From, ToType, CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3116 | } |
| 3117 | } |
| 3118 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3119 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3120 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3121 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3122 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3123 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3124 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3125 | case OR_Success: |
| 3126 | // Record the standard conversion we used and the conversion function. |
| 3127 | if (CXXConstructorDecl *Constructor |
| 3128 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 3129 | // C++ [over.ics.user]p1: |
| 3130 | // If the user-defined conversion is specified by a |
| 3131 | // constructor (12.3.1), the initial standard conversion |
| 3132 | // sequence converts the source type to the type required by |
| 3133 | // the argument of the constructor. |
| 3134 | // |
| 3135 | QualType ThisType = Constructor->getThisType(S.Context); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3136 | if (isa<InitListExpr>(From)) { |
| 3137 | // Initializer lists don't have conversions as such. |
| 3138 | User.Before.setAsIdentityConversion(); |
| 3139 | } else { |
| 3140 | if (Best->Conversions[0].isEllipsis()) |
| 3141 | User.EllipsisConversion = true; |
| 3142 | else { |
| 3143 | User.Before = Best->Conversions[0].Standard; |
| 3144 | User.EllipsisConversion = false; |
| 3145 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3146 | } |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3147 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3148 | User.ConversionFunction = Constructor; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3149 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3150 | User.After.setAsIdentityConversion(); |
| 3151 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 3152 | User.After.setAllToTypes(ToType); |
| 3153 | return OR_Success; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3154 | } |
| 3155 | if (CXXConversionDecl *Conversion |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3156 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 3157 | // C++ [over.ics.user]p1: |
| 3158 | // |
| 3159 | // [...] If the user-defined conversion is specified by a |
| 3160 | // conversion function (12.3.2), the initial standard |
| 3161 | // conversion sequence converts the source type to the |
| 3162 | // implicit object parameter of the conversion function. |
| 3163 | User.Before = Best->Conversions[0].Standard; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3164 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3165 | User.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3166 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3167 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3168 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3169 | // C++ [over.ics.user]p2: |
| 3170 | // The second standard conversion sequence converts the |
| 3171 | // result of the user-defined conversion to the target type |
| 3172 | // for the sequence. Since an implicit conversion sequence |
| 3173 | // is an initialization, the special rules for |
| 3174 | // initialization by user-defined conversion apply when |
| 3175 | // selecting the best user-defined conversion for a |
| 3176 | // user-defined conversion sequence (see 13.3.3 and |
| 3177 | // 13.3.3.1). |
| 3178 | User.After = Best->FinalConversion; |
| 3179 | return OR_Success; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3180 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3181 | llvm_unreachable("Not a constructor or conversion function?"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3182 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3183 | case OR_No_Viable_Function: |
| 3184 | return OR_No_Viable_Function; |
| 3185 | case OR_Deleted: |
| 3186 | // No conversion here! We're done. |
| 3187 | return OR_Deleted; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3188 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3189 | case OR_Ambiguous: |
| 3190 | return OR_Ambiguous; |
| 3191 | } |
| 3192 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3193 | llvm_unreachable("Invalid OverloadResult!"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3194 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3195 | |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3196 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3197 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3198 | ImplicitConversionSequence ICS; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3199 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3200 | OverloadingResult OvResult = |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3201 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3202 | CandidateSet, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3203 | if (OvResult == OR_Ambiguous) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3204 | Diag(From->getLocStart(), |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3205 | diag::err_typecheck_ambiguous_condition) |
| 3206 | << From->getType() << ToType << From->getSourceRange(); |
Larisse Voufo | 7419d01 | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 3207 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) { |
Larisse Voufo | 288f76a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 3208 | if (!RequireCompleteType(From->getLocStart(), ToType, |
Larisse Voufo | 7419d01 | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 3209 | diag::err_typecheck_nonviable_condition_incomplete, |
| 3210 | From->getType(), From->getSourceRange())) |
| 3211 | Diag(From->getLocStart(), |
| 3212 | diag::err_typecheck_nonviable_condition) |
| 3213 | << From->getType() << From->getSourceRange() << ToType; |
| 3214 | } |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3215 | else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3216 | return false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3217 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3218 | return true; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3219 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3220 | |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3221 | /// \brief Compare the user-defined conversion functions or constructors |
| 3222 | /// of two user-defined conversion sequences to determine whether any ordering |
| 3223 | /// is possible. |
| 3224 | static ImplicitConversionSequence::CompareKind |
| 3225 | compareConversionFunctions(Sema &S, |
| 3226 | FunctionDecl *Function1, |
| 3227 | FunctionDecl *Function2) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3228 | if (!S.getLangOpts().ObjC1 || !S.getLangOpts().CPlusPlus11) |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3229 | return ImplicitConversionSequence::Indistinguishable; |
| 3230 | |
| 3231 | // Objective-C++: |
| 3232 | // If both conversion functions are implicitly-declared conversions from |
| 3233 | // a lambda closure type to a function pointer and a block pointer, |
| 3234 | // respectively, always prefer the conversion to a function pointer, |
| 3235 | // because the function pointer is more lightweight and is more likely |
| 3236 | // to keep code working. |
| 3237 | CXXConversionDecl *Conv1 = dyn_cast<CXXConversionDecl>(Function1); |
| 3238 | if (!Conv1) |
| 3239 | return ImplicitConversionSequence::Indistinguishable; |
| 3240 | |
| 3241 | CXXConversionDecl *Conv2 = dyn_cast<CXXConversionDecl>(Function2); |
| 3242 | if (!Conv2) |
| 3243 | return ImplicitConversionSequence::Indistinguishable; |
| 3244 | |
| 3245 | if (Conv1->getParent()->isLambda() && Conv2->getParent()->isLambda()) { |
| 3246 | bool Block1 = Conv1->getConversionType()->isBlockPointerType(); |
| 3247 | bool Block2 = Conv2->getConversionType()->isBlockPointerType(); |
| 3248 | if (Block1 != Block2) |
| 3249 | return Block1? ImplicitConversionSequence::Worse |
| 3250 | : ImplicitConversionSequence::Better; |
| 3251 | } |
| 3252 | |
| 3253 | return ImplicitConversionSequence::Indistinguishable; |
| 3254 | } |
| 3255 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3256 | /// CompareImplicitConversionSequences - Compare two implicit |
| 3257 | /// conversion sequences to determine whether one is better than the |
| 3258 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3259 | static ImplicitConversionSequence::CompareKind |
| 3260 | CompareImplicitConversionSequences(Sema &S, |
| 3261 | const ImplicitConversionSequence& ICS1, |
| 3262 | const ImplicitConversionSequence& ICS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3263 | { |
| 3264 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 3265 | // conversion sequences (as defined in 13.3.3.1) |
| 3266 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 3267 | // conversion sequence than a user-defined conversion sequence or |
| 3268 | // an ellipsis conversion sequence, and |
| 3269 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 3270 | // conversion sequence than an ellipsis conversion sequence |
| 3271 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3272 | // |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3273 | // C++0x [over.best.ics]p10: |
| 3274 | // For the purpose of ranking implicit conversion sequences as |
| 3275 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 3276 | // treated as a user-defined sequence that is indistinguishable |
| 3277 | // from any other user-defined conversion sequence. |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3278 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 3279 | return ImplicitConversionSequence::Better; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3280 | if (ICS2.getKindRank() < ICS1.getKindRank()) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3281 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3282 | |
Benjamin Kramer | b6eee07 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 3283 | // The following checks require both conversion sequences to be of |
| 3284 | // the same kind. |
| 3285 | if (ICS1.getKind() != ICS2.getKind()) |
| 3286 | return ImplicitConversionSequence::Indistinguishable; |
| 3287 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3288 | ImplicitConversionSequence::CompareKind Result = |
| 3289 | ImplicitConversionSequence::Indistinguishable; |
| 3290 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3291 | // Two implicit conversion sequences of the same form are |
| 3292 | // indistinguishable conversion sequences unless one of the |
| 3293 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3294 | if (ICS1.isStandard()) |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3295 | Result = CompareStandardConversionSequences(S, |
| 3296 | ICS1.Standard, ICS2.Standard); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3297 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3298 | // User-defined conversion sequence U1 is a better conversion |
| 3299 | // sequence than another user-defined conversion sequence U2 if |
| 3300 | // they contain the same user-defined conversion function or |
| 3301 | // constructor and if the second standard conversion sequence of |
| 3302 | // U1 is better than the second standard conversion sequence of |
| 3303 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3304 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3305 | ICS2.UserDefined.ConversionFunction) |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3306 | Result = CompareStandardConversionSequences(S, |
| 3307 | ICS1.UserDefined.After, |
| 3308 | ICS2.UserDefined.After); |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3309 | else |
| 3310 | Result = compareConversionFunctions(S, |
| 3311 | ICS1.UserDefined.ConversionFunction, |
| 3312 | ICS2.UserDefined.ConversionFunction); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3313 | } |
| 3314 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3315 | // List-initialization sequence L1 is a better conversion sequence than |
| 3316 | // list-initialization sequence L2 if L1 converts to std::initializer_list<X> |
| 3317 | // for some X and L2 does not. |
| 3318 | if (Result == ImplicitConversionSequence::Indistinguishable && |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 3319 | !ICS1.isBad() && |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3320 | ICS1.isListInitializationSequence() && |
| 3321 | ICS2.isListInitializationSequence()) { |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 3322 | if (ICS1.isStdInitializerListElement() && |
| 3323 | !ICS2.isStdInitializerListElement()) |
| 3324 | return ImplicitConversionSequence::Better; |
| 3325 | if (!ICS1.isStdInitializerListElement() && |
| 3326 | ICS2.isStdInitializerListElement()) |
| 3327 | return ImplicitConversionSequence::Worse; |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3328 | } |
| 3329 | |
| 3330 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3331 | } |
| 3332 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3333 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 3334 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 3335 | Qualifiers Quals; |
| 3336 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3337 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3338 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3339 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3340 | return Context.hasSameUnqualifiedType(T1, T2); |
| 3341 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3342 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3343 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 3344 | // determine if one is a proper subset of the other. |
| 3345 | static ImplicitConversionSequence::CompareKind |
| 3346 | compareStandardConversionSubsets(ASTContext &Context, |
| 3347 | const StandardConversionSequence& SCS1, |
| 3348 | const StandardConversionSequence& SCS2) { |
| 3349 | ImplicitConversionSequence::CompareKind Result |
| 3350 | = ImplicitConversionSequence::Indistinguishable; |
| 3351 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3352 | // the identity conversion sequence is considered to be a subsequence of |
Douglas Gregor | ae65f4b | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 3353 | // any non-identity conversion sequence |
Douglas Gregor | 4ae5b72 | 2011-06-05 06:15:20 +0000 | [diff] [blame] | 3354 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 3355 | return ImplicitConversionSequence::Better; |
| 3356 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 3357 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3358 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3359 | if (SCS1.Second != SCS2.Second) { |
| 3360 | if (SCS1.Second == ICK_Identity) |
| 3361 | Result = ImplicitConversionSequence::Better; |
| 3362 | else if (SCS2.Second == ICK_Identity) |
| 3363 | Result = ImplicitConversionSequence::Worse; |
| 3364 | else |
| 3365 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3366 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3367 | return ImplicitConversionSequence::Indistinguishable; |
| 3368 | |
| 3369 | if (SCS1.Third == SCS2.Third) { |
| 3370 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 3371 | : ImplicitConversionSequence::Indistinguishable; |
| 3372 | } |
| 3373 | |
| 3374 | if (SCS1.Third == ICK_Identity) |
| 3375 | return Result == ImplicitConversionSequence::Worse |
| 3376 | ? ImplicitConversionSequence::Indistinguishable |
| 3377 | : ImplicitConversionSequence::Better; |
| 3378 | |
| 3379 | if (SCS2.Third == ICK_Identity) |
| 3380 | return Result == ImplicitConversionSequence::Better |
| 3381 | ? ImplicitConversionSequence::Indistinguishable |
| 3382 | : ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3383 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3384 | return ImplicitConversionSequence::Indistinguishable; |
| 3385 | } |
| 3386 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3387 | /// \brief Determine whether one of the given reference bindings is better |
| 3388 | /// than the other based on what kind of bindings they are. |
| 3389 | static bool isBetterReferenceBindingKind(const StandardConversionSequence &SCS1, |
| 3390 | const StandardConversionSequence &SCS2) { |
| 3391 | // C++0x [over.ics.rank]p3b4: |
| 3392 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 3393 | // implicit object parameter of a non-static member function declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3394 | // without a ref-qualifier, and *either* S1 binds an rvalue reference |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3395 | // 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] | 3396 | // lvalue reference to a function lvalue and S2 binds an rvalue |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3397 | // reference*. |
| 3398 | // |
| 3399 | // FIXME: Rvalue references. We're going rogue with the above edits, |
| 3400 | // because the semantics in the current C++0x working paper (N3225 at the |
| 3401 | // time of this writing) break the standard definition of std::forward |
| 3402 | // and std::reference_wrapper when dealing with references to functions. |
| 3403 | // Proposed wording changes submitted to CWG for consideration. |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3404 | if (SCS1.BindsImplicitObjectArgumentWithoutRefQualifier || |
| 3405 | SCS2.BindsImplicitObjectArgumentWithoutRefQualifier) |
| 3406 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3407 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3408 | return (!SCS1.IsLvalueReference && SCS1.BindsToRvalue && |
| 3409 | SCS2.IsLvalueReference) || |
| 3410 | (SCS1.IsLvalueReference && SCS1.BindsToFunctionLvalue && |
| 3411 | !SCS2.IsLvalueReference); |
| 3412 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3413 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3414 | /// CompareStandardConversionSequences - Compare two standard |
| 3415 | /// conversion sequences to determine whether one is better than the |
| 3416 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3417 | static ImplicitConversionSequence::CompareKind |
| 3418 | CompareStandardConversionSequences(Sema &S, |
| 3419 | const StandardConversionSequence& SCS1, |
| 3420 | const StandardConversionSequence& SCS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3421 | { |
| 3422 | // Standard conversion sequence S1 is a better conversion sequence |
| 3423 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 3424 | |
| 3425 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 3426 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 3427 | // excluding any Lvalue Transformation; the identity conversion |
| 3428 | // sequence is considered to be a subsequence of any |
| 3429 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3430 | if (ImplicitConversionSequence::CompareKind CK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3431 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3432 | return CK; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3433 | |
| 3434 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 3435 | // defined below), or, if not that, |
| 3436 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 3437 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 3438 | if (Rank1 < Rank2) |
| 3439 | return ImplicitConversionSequence::Better; |
| 3440 | else if (Rank2 < Rank1) |
| 3441 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3442 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3443 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 3444 | // are indistinguishable unless one of the following rules |
| 3445 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3446 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3447 | // A conversion that is not a conversion of a pointer, or |
| 3448 | // pointer to member, to bool is better than another conversion |
| 3449 | // that is such a conversion. |
| 3450 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 3451 | return SCS2.isPointerConversionToBool() |
| 3452 | ? ImplicitConversionSequence::Better |
| 3453 | : ImplicitConversionSequence::Worse; |
| 3454 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3455 | // C++ [over.ics.rank]p4b2: |
| 3456 | // |
| 3457 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3458 | // conversion of B* to A* is better than conversion of B* to |
| 3459 | // void*, and conversion of A* to void* is better than conversion |
| 3460 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3461 | bool SCS1ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3462 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3463 | bool SCS2ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3464 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3465 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 3466 | // Exactly one of the conversion sequences is a conversion to |
| 3467 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3468 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 3469 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3470 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 3471 | // Neither conversion sequence converts to a void pointer; compare |
| 3472 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3473 | if (ImplicitConversionSequence::CompareKind DerivedCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3474 | = CompareDerivedToBaseConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3475 | return DerivedCK; |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3476 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid && |
| 3477 | !S.Context.hasSameType(SCS1.getFromType(), SCS2.getFromType())) { |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3478 | // Both conversion sequences are conversions to void |
| 3479 | // pointers. Compare the source types to determine if there's an |
| 3480 | // inheritance relationship in their sources. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3481 | QualType FromType1 = SCS1.getFromType(); |
| 3482 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3483 | |
| 3484 | // Adjust the types we're converting from via the array-to-pointer |
| 3485 | // conversion, if we need to. |
| 3486 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3487 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3488 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3489 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3490 | |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3491 | QualType FromPointee1 = FromType1->getPointeeType().getUnqualifiedType(); |
| 3492 | QualType FromPointee2 = FromType2->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3493 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3494 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3495 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3496 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3497 | return ImplicitConversionSequence::Worse; |
| 3498 | |
| 3499 | // Objective-C++: If one interface is more specific than the |
| 3500 | // other, it is the better one. |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3501 | const ObjCObjectPointerType* FromObjCPtr1 |
| 3502 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3503 | const ObjCObjectPointerType* FromObjCPtr2 |
| 3504 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3505 | if (FromObjCPtr1 && FromObjCPtr2) { |
| 3506 | bool AssignLeft = S.Context.canAssignObjCInterfaces(FromObjCPtr1, |
| 3507 | FromObjCPtr2); |
| 3508 | bool AssignRight = S.Context.canAssignObjCInterfaces(FromObjCPtr2, |
| 3509 | FromObjCPtr1); |
| 3510 | if (AssignLeft != AssignRight) { |
| 3511 | return AssignLeft? ImplicitConversionSequence::Better |
| 3512 | : ImplicitConversionSequence::Worse; |
| 3513 | } |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3514 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3515 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3516 | |
| 3517 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 3518 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3519 | if (ImplicitConversionSequence::CompareKind QualCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3520 | = CompareQualificationConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3521 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3522 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3523 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3524 | // Check for a better reference binding based on the kind of bindings. |
| 3525 | if (isBetterReferenceBindingKind(SCS1, SCS2)) |
| 3526 | return ImplicitConversionSequence::Better; |
| 3527 | else if (isBetterReferenceBindingKind(SCS2, SCS1)) |
| 3528 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3529 | |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 3530 | // C++ [over.ics.rank]p3b4: |
| 3531 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 3532 | // which the references refer are the same type except for |
| 3533 | // top-level cv-qualifiers, and the type to which the reference |
| 3534 | // initialized by S2 refers is more cv-qualified than the type |
| 3535 | // to which the reference initialized by S1 refers. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3536 | QualType T1 = SCS1.getToType(2); |
| 3537 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3538 | T1 = S.Context.getCanonicalType(T1); |
| 3539 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3540 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3541 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3542 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3543 | if (UnqualT1 == UnqualT2) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3544 | // Objective-C++ ARC: If the references refer to objects with different |
| 3545 | // lifetimes, prefer bindings that don't change lifetime. |
| 3546 | if (SCS1.ObjCLifetimeConversionBinding != |
| 3547 | SCS2.ObjCLifetimeConversionBinding) { |
| 3548 | return SCS1.ObjCLifetimeConversionBinding |
| 3549 | ? ImplicitConversionSequence::Worse |
| 3550 | : ImplicitConversionSequence::Better; |
| 3551 | } |
| 3552 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3553 | // If the type is an array type, promote the element qualifiers to the |
| 3554 | // type for comparison. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3555 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3556 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3557 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3558 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3559 | if (T2.isMoreQualifiedThan(T1)) |
| 3560 | return ImplicitConversionSequence::Better; |
| 3561 | else if (T1.isMoreQualifiedThan(T2)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3562 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3563 | } |
| 3564 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3565 | |
Francois Pichet | 1c98d62 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3566 | // In Microsoft mode, prefer an integral conversion to a |
| 3567 | // floating-to-integral conversion if the integral conversion |
| 3568 | // is between types of the same size. |
| 3569 | // For example: |
| 3570 | // void f(float); |
| 3571 | // void f(int); |
| 3572 | // int main { |
| 3573 | // long a; |
| 3574 | // f(a); |
| 3575 | // } |
| 3576 | // Here, MSVC will call f(int) instead of generating a compile error |
| 3577 | // as clang will do in standard mode. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3578 | if (S.getLangOpts().MicrosoftMode && |
Francois Pichet | 1c98d62 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3579 | SCS1.Second == ICK_Integral_Conversion && |
| 3580 | SCS2.Second == ICK_Floating_Integral && |
| 3581 | S.Context.getTypeSize(SCS1.getFromType()) == |
| 3582 | S.Context.getTypeSize(SCS1.getToType(2))) |
| 3583 | return ImplicitConversionSequence::Better; |
| 3584 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3585 | return ImplicitConversionSequence::Indistinguishable; |
| 3586 | } |
| 3587 | |
| 3588 | /// CompareQualificationConversions - Compares two standard conversion |
| 3589 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3590 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 3591 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3592 | CompareQualificationConversions(Sema &S, |
| 3593 | const StandardConversionSequence& SCS1, |
| 3594 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 3595 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3596 | // -- S1 and S2 differ only in their qualification conversion and |
| 3597 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 3598 | // cv-qualification signature of type T1 is a proper subset of |
| 3599 | // the cv-qualification signature of type T2, and S1 is not the |
| 3600 | // deprecated string literal array-to-pointer conversion (4.2). |
| 3601 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 3602 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 3603 | return ImplicitConversionSequence::Indistinguishable; |
| 3604 | |
| 3605 | // FIXME: the example in the standard doesn't use a qualification |
| 3606 | // conversion (!) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3607 | QualType T1 = SCS1.getToType(2); |
| 3608 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3609 | T1 = S.Context.getCanonicalType(T1); |
| 3610 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3611 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3612 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3613 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3614 | |
| 3615 | // If the types are the same, we won't learn anything by unwrapped |
| 3616 | // them. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3617 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3618 | return ImplicitConversionSequence::Indistinguishable; |
| 3619 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3620 | // If the type is an array type, promote the element qualifiers to the type |
| 3621 | // for comparison. |
| 3622 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3623 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3624 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3625 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3626 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3627 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3628 | = ImplicitConversionSequence::Indistinguishable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3629 | |
| 3630 | // Objective-C++ ARC: |
| 3631 | // Prefer qualification conversions not involving a change in lifetime |
| 3632 | // to qualification conversions that do not change lifetime. |
| 3633 | if (SCS1.QualificationIncludesObjCLifetime != |
| 3634 | SCS2.QualificationIncludesObjCLifetime) { |
| 3635 | Result = SCS1.QualificationIncludesObjCLifetime |
| 3636 | ? ImplicitConversionSequence::Worse |
| 3637 | : ImplicitConversionSequence::Better; |
| 3638 | } |
| 3639 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3640 | while (S.Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3641 | // Within each iteration of the loop, we check the qualifiers to |
| 3642 | // determine if this still looks like a qualification |
| 3643 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 3644 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3645 | // until there are no more pointers or pointers-to-members left |
| 3646 | // to unwrap. This essentially mimics what |
| 3647 | // IsQualificationConversion does, but here we're checking for a |
| 3648 | // strict subset of qualifiers. |
| 3649 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 3650 | // The qualifiers are the same, so this doesn't tell us anything |
| 3651 | // about how the sequences rank. |
| 3652 | ; |
| 3653 | else if (T2.isMoreQualifiedThan(T1)) { |
| 3654 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 3655 | if (Result == ImplicitConversionSequence::Worse) |
| 3656 | // Neither has qualifiers that are a subset of the other's |
| 3657 | // qualifiers. |
| 3658 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3659 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3660 | Result = ImplicitConversionSequence::Better; |
| 3661 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 3662 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 3663 | if (Result == ImplicitConversionSequence::Better) |
| 3664 | // Neither has qualifiers that are a subset of the other's |
| 3665 | // qualifiers. |
| 3666 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3667 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3668 | Result = ImplicitConversionSequence::Worse; |
| 3669 | } else { |
| 3670 | // Qualifiers are disjoint. |
| 3671 | return ImplicitConversionSequence::Indistinguishable; |
| 3672 | } |
| 3673 | |
| 3674 | // If the types after this point are equivalent, we're done. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3675 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3676 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3677 | } |
| 3678 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3679 | // Check that the winning standard conversion sequence isn't using |
| 3680 | // the deprecated string literal array to pointer conversion. |
| 3681 | switch (Result) { |
| 3682 | case ImplicitConversionSequence::Better: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3683 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3684 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3685 | break; |
| 3686 | |
| 3687 | case ImplicitConversionSequence::Indistinguishable: |
| 3688 | break; |
| 3689 | |
| 3690 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3691 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3692 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3693 | break; |
| 3694 | } |
| 3695 | |
| 3696 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3697 | } |
| 3698 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3699 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 3700 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3701 | /// various kinds of derived-to-base conversions (C++ |
| 3702 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 3703 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3704 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3705 | CompareDerivedToBaseConversions(Sema &S, |
| 3706 | const StandardConversionSequence& SCS1, |
| 3707 | const StandardConversionSequence& SCS2) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3708 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3709 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3710 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3711 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3712 | |
| 3713 | // Adjust the types we're converting from via the array-to-pointer |
| 3714 | // conversion, if we need to. |
| 3715 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3716 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3717 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3718 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3719 | |
| 3720 | // Canonicalize all of the types. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3721 | FromType1 = S.Context.getCanonicalType(FromType1); |
| 3722 | ToType1 = S.Context.getCanonicalType(ToType1); |
| 3723 | FromType2 = S.Context.getCanonicalType(FromType2); |
| 3724 | ToType2 = S.Context.getCanonicalType(ToType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3725 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3726 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3727 | // |
| 3728 | // If class B is derived directly or indirectly from class A and |
| 3729 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3730 | // |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3731 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3732 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 3733 | SCS2.Second == ICK_Pointer_Conversion && |
| 3734 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 3735 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 3736 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3737 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3738 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3739 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3740 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3741 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3742 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3743 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3744 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3745 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3746 | // -- 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] | 3747 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3748 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3749 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3750 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3751 | return ImplicitConversionSequence::Worse; |
| 3752 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3753 | |
| 3754 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 3755 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3756 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3757 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3758 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3759 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3760 | } |
| 3761 | } else if (SCS1.Second == ICK_Pointer_Conversion && |
| 3762 | SCS2.Second == ICK_Pointer_Conversion) { |
| 3763 | const ObjCObjectPointerType *FromPtr1 |
| 3764 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3765 | const ObjCObjectPointerType *FromPtr2 |
| 3766 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3767 | const ObjCObjectPointerType *ToPtr1 |
| 3768 | = ToType1->getAs<ObjCObjectPointerType>(); |
| 3769 | const ObjCObjectPointerType *ToPtr2 |
| 3770 | = ToType2->getAs<ObjCObjectPointerType>(); |
| 3771 | |
| 3772 | if (FromPtr1 && FromPtr2 && ToPtr1 && ToPtr2) { |
| 3773 | // Apply the same conversion ranking rules for Objective-C pointer types |
| 3774 | // that we do for C++ pointers to class types. However, we employ the |
| 3775 | // Objective-C pseudo-subtyping relationship used for assignment of |
| 3776 | // Objective-C pointer types. |
| 3777 | bool FromAssignLeft |
| 3778 | = S.Context.canAssignObjCInterfaces(FromPtr1, FromPtr2); |
| 3779 | bool FromAssignRight |
| 3780 | = S.Context.canAssignObjCInterfaces(FromPtr2, FromPtr1); |
| 3781 | bool ToAssignLeft |
| 3782 | = S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2); |
| 3783 | bool ToAssignRight |
| 3784 | = S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1); |
| 3785 | |
| 3786 | // A conversion to an a non-id object pointer type or qualified 'id' |
| 3787 | // type is better than a conversion to 'id'. |
| 3788 | if (ToPtr1->isObjCIdType() && |
| 3789 | (ToPtr2->isObjCQualifiedIdType() || ToPtr2->getInterfaceDecl())) |
| 3790 | return ImplicitConversionSequence::Worse; |
| 3791 | if (ToPtr2->isObjCIdType() && |
| 3792 | (ToPtr1->isObjCQualifiedIdType() || ToPtr1->getInterfaceDecl())) |
| 3793 | return ImplicitConversionSequence::Better; |
| 3794 | |
| 3795 | // A conversion to a non-id object pointer type is better than a |
| 3796 | // conversion to a qualified 'id' type |
| 3797 | if (ToPtr1->isObjCQualifiedIdType() && ToPtr2->getInterfaceDecl()) |
| 3798 | return ImplicitConversionSequence::Worse; |
| 3799 | if (ToPtr2->isObjCQualifiedIdType() && ToPtr1->getInterfaceDecl()) |
| 3800 | return ImplicitConversionSequence::Better; |
| 3801 | |
| 3802 | // A conversion to an a non-Class object pointer type or qualified 'Class' |
| 3803 | // type is better than a conversion to 'Class'. |
| 3804 | if (ToPtr1->isObjCClassType() && |
| 3805 | (ToPtr2->isObjCQualifiedClassType() || ToPtr2->getInterfaceDecl())) |
| 3806 | return ImplicitConversionSequence::Worse; |
| 3807 | if (ToPtr2->isObjCClassType() && |
| 3808 | (ToPtr1->isObjCQualifiedClassType() || ToPtr1->getInterfaceDecl())) |
| 3809 | return ImplicitConversionSequence::Better; |
| 3810 | |
| 3811 | // A conversion to a non-Class object pointer type is better than a |
| 3812 | // conversion to a qualified 'Class' type. |
| 3813 | if (ToPtr1->isObjCQualifiedClassType() && ToPtr2->getInterfaceDecl()) |
| 3814 | return ImplicitConversionSequence::Worse; |
| 3815 | if (ToPtr2->isObjCQualifiedClassType() && ToPtr1->getInterfaceDecl()) |
| 3816 | return ImplicitConversionSequence::Better; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3817 | |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3818 | // -- "conversion of C* to B* is better than conversion of C* to A*," |
| 3819 | if (S.Context.hasSameType(FromType1, FromType2) && |
| 3820 | !FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType() && |
| 3821 | (ToAssignLeft != ToAssignRight)) |
| 3822 | return ToAssignLeft? ImplicitConversionSequence::Worse |
| 3823 | : ImplicitConversionSequence::Better; |
| 3824 | |
| 3825 | // -- "conversion of B* to A* is better than conversion of C* to A*," |
| 3826 | if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) && |
| 3827 | (FromAssignLeft != FromAssignRight)) |
| 3828 | return FromAssignLeft? ImplicitConversionSequence::Better |
| 3829 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3830 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3831 | } |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3832 | |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3833 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3834 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 3835 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 3836 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3837 | const MemberPointerType * FromMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3838 | FromType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3839 | const MemberPointerType * ToMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3840 | ToType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3841 | const MemberPointerType * FromMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3842 | FromType2->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3843 | const MemberPointerType * ToMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3844 | ToType2->getAs<MemberPointerType>(); |
| 3845 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 3846 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 3847 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 3848 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 3849 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 3850 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 3851 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 3852 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3853 | // 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] | 3854 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3855 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3856 | return ImplicitConversionSequence::Worse; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3857 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3858 | return ImplicitConversionSequence::Better; |
| 3859 | } |
| 3860 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 3861 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3862 | if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3863 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3864 | else if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3865 | return ImplicitConversionSequence::Worse; |
| 3866 | } |
| 3867 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3868 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3869 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3870 | // -- 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] | 3871 | // -- binding of an expression of type C to a reference of type |
| 3872 | // B& is better than binding an expression of type C to a |
| 3873 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3874 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3875 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3876 | if (S.IsDerivedFrom(ToType1, ToType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3877 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3878 | else if (S.IsDerivedFrom(ToType2, ToType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3879 | return ImplicitConversionSequence::Worse; |
| 3880 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3881 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3882 | // -- 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] | 3883 | // -- binding of an expression of type B to a reference of type |
| 3884 | // A& is better than binding an expression of type C to a |
| 3885 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3886 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3887 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3888 | if (S.IsDerivedFrom(FromType2, FromType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3889 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3890 | else if (S.IsDerivedFrom(FromType1, FromType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3891 | return ImplicitConversionSequence::Worse; |
| 3892 | } |
| 3893 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3894 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3895 | return ImplicitConversionSequence::Indistinguishable; |
| 3896 | } |
| 3897 | |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 3898 | /// \brief Determine whether the given type is valid, e.g., it is not an invalid |
| 3899 | /// C++ class. |
| 3900 | static bool isTypeValid(QualType T) { |
| 3901 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) |
| 3902 | return !Record->isInvalidDecl(); |
| 3903 | |
| 3904 | return true; |
| 3905 | } |
| 3906 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3907 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 3908 | /// determine whether they are reference-related, |
| 3909 | /// reference-compatible, reference-compatible with added |
| 3910 | /// qualification, or incompatible, for use in C++ initialization by |
| 3911 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 3912 | /// type, and the first type (T1) is the pointee type of the reference |
| 3913 | /// type being initialized. |
| 3914 | Sema::ReferenceCompareResult |
| 3915 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 3916 | QualType OrigT1, QualType OrigT2, |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3917 | bool &DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3918 | bool &ObjCConversion, |
| 3919 | bool &ObjCLifetimeConversion) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3920 | assert(!OrigT1->isReferenceType() && |
| 3921 | "T1 must be the pointee type of the reference type"); |
| 3922 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 3923 | |
| 3924 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 3925 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 3926 | Qualifiers T1Quals, T2Quals; |
| 3927 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3928 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 3929 | |
| 3930 | // C++ [dcl.init.ref]p4: |
| 3931 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 3932 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 3933 | // T1 is a base class of T2. |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3934 | DerivedToBase = false; |
| 3935 | ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3936 | ObjCLifetimeConversion = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3937 | if (UnqualT1 == UnqualT2) { |
| 3938 | // Nothing to do. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3939 | } else if (!RequireCompleteType(Loc, OrigT2, 0) && |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 3940 | isTypeValid(UnqualT1) && isTypeValid(UnqualT2) && |
| 3941 | IsDerivedFrom(UnqualT2, UnqualT1)) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3942 | DerivedToBase = true; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3943 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
| 3944 | UnqualT2->isObjCObjectOrInterfaceType() && |
| 3945 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
| 3946 | ObjCConversion = true; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3947 | else |
| 3948 | return Ref_Incompatible; |
| 3949 | |
| 3950 | // At this point, we know that T1 and T2 are reference-related (at |
| 3951 | // least). |
| 3952 | |
| 3953 | // If the type is an array type, promote the element qualifiers to the type |
| 3954 | // for comparison. |
| 3955 | if (isa<ArrayType>(T1) && T1Quals) |
| 3956 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 3957 | if (isa<ArrayType>(T2) && T2Quals) |
| 3958 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 3959 | |
| 3960 | // C++ [dcl.init.ref]p4: |
| 3961 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 3962 | // reference-related to T2 and cv1 is the same cv-qualification |
| 3963 | // as, or greater cv-qualification than, cv2. For purposes of |
| 3964 | // overload resolution, cases for which cv1 is greater |
| 3965 | // cv-qualification than cv2 are identified as |
| 3966 | // reference-compatible with added qualification (see 13.3.3.2). |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 3967 | // |
| 3968 | // Note that we also require equivalence of Objective-C GC and address-space |
| 3969 | // qualifiers when performing these computations, so that e.g., an int in |
| 3970 | // address space 1 is not reference-compatible with an int in address |
| 3971 | // space 2. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3972 | if (T1Quals.getObjCLifetime() != T2Quals.getObjCLifetime() && |
| 3973 | T1Quals.compatiblyIncludesObjCLifetime(T2Quals)) { |
| 3974 | T1Quals.removeObjCLifetime(); |
| 3975 | T2Quals.removeObjCLifetime(); |
| 3976 | ObjCLifetimeConversion = true; |
| 3977 | } |
| 3978 | |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 3979 | if (T1Quals == T2Quals) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3980 | return Ref_Compatible; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3981 | else if (T1Quals.compatiblyIncludes(T2Quals)) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3982 | return Ref_Compatible_With_Added_Qualification; |
| 3983 | else |
| 3984 | return Ref_Related; |
| 3985 | } |
| 3986 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3987 | /// \brief Look for a user-defined conversion to an value reference-compatible |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3988 | /// with DeclType. Return true if something definite is found. |
| 3989 | static bool |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3990 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
| 3991 | QualType DeclType, SourceLocation DeclLoc, |
| 3992 | Expr *Init, QualType T2, bool AllowRvalues, |
| 3993 | bool AllowExplicit) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3994 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 3995 | CXXRecordDecl *T2RecordDecl |
| 3996 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 3997 | |
| 3998 | OverloadCandidateSet CandidateSet(DeclLoc); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3999 | std::pair<CXXRecordDecl::conversion_iterator, |
| 4000 | CXXRecordDecl::conversion_iterator> |
| 4001 | Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 4002 | for (CXXRecordDecl::conversion_iterator |
| 4003 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4004 | NamedDecl *D = *I; |
| 4005 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4006 | if (isa<UsingShadowDecl>(D)) |
| 4007 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 4008 | |
| 4009 | FunctionTemplateDecl *ConvTemplate |
| 4010 | = dyn_cast<FunctionTemplateDecl>(D); |
| 4011 | CXXConversionDecl *Conv; |
| 4012 | if (ConvTemplate) |
| 4013 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 4014 | else |
| 4015 | Conv = cast<CXXConversionDecl>(D); |
| 4016 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4017 | // 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] | 4018 | // explicit conversions, skip it. |
| 4019 | if (!AllowExplicit && Conv->isExplicit()) |
| 4020 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4021 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4022 | if (AllowRvalues) { |
| 4023 | bool DerivedToBase = false; |
| 4024 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4025 | bool ObjCLifetimeConversion = false; |
Douglas Gregor | 203050c | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4026 | |
| 4027 | // If we are initializing an rvalue reference, don't permit conversion |
| 4028 | // functions that return lvalues. |
| 4029 | if (!ConvTemplate && DeclType->isRValueReferenceType()) { |
| 4030 | const ReferenceType *RefType |
| 4031 | = Conv->getConversionType()->getAs<LValueReferenceType>(); |
| 4032 | if (RefType && !RefType->getPointeeType()->isFunctionType()) |
| 4033 | continue; |
| 4034 | } |
| 4035 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4036 | if (!ConvTemplate && |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4037 | S.CompareReferenceRelationship( |
| 4038 | DeclLoc, |
| 4039 | Conv->getConversionType().getNonReferenceType() |
| 4040 | .getUnqualifiedType(), |
| 4041 | DeclType.getNonReferenceType().getUnqualifiedType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4042 | DerivedToBase, ObjCConversion, ObjCLifetimeConversion) == |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4043 | Sema::Ref_Incompatible) |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4044 | continue; |
| 4045 | } else { |
| 4046 | // If the conversion function doesn't return a reference type, |
| 4047 | // it can't be considered for this conversion. An rvalue reference |
| 4048 | // is only acceptable if its referencee is a function type. |
| 4049 | |
| 4050 | const ReferenceType *RefType = |
| 4051 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 4052 | if (!RefType || |
| 4053 | (!RefType->isLValueReferenceType() && |
| 4054 | !RefType->getPointeeType()->isFunctionType())) |
| 4055 | continue; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4056 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4057 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4058 | if (ConvTemplate) |
| 4059 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4060 | Init, DeclType, CandidateSet); |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4061 | else |
| 4062 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4063 | DeclType, CandidateSet); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4064 | } |
| 4065 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4066 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4067 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4068 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4069 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4070 | case OR_Success: |
| 4071 | // C++ [over.ics.ref]p1: |
| 4072 | // |
| 4073 | // [...] If the parameter binds directly to the result of |
| 4074 | // applying a conversion function to the argument |
| 4075 | // expression, the implicit conversion sequence is a |
| 4076 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 4077 | // second standard conversion sequence either an identity |
| 4078 | // conversion or, if the conversion function returns an |
| 4079 | // entity of a type that is a derived class of the parameter |
| 4080 | // type, a derived-to-base Conversion. |
| 4081 | if (!Best->FinalConversion.DirectBinding) |
| 4082 | return false; |
| 4083 | |
| 4084 | ICS.setUserDefined(); |
| 4085 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 4086 | ICS.UserDefined.After = Best->FinalConversion; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4087 | ICS.UserDefined.HadMultipleCandidates = HadMultipleCandidates; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4088 | ICS.UserDefined.ConversionFunction = Best->Function; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 4089 | ICS.UserDefined.FoundConversionFunction = Best->FoundDecl; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4090 | ICS.UserDefined.EllipsisConversion = false; |
| 4091 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 4092 | ICS.UserDefined.After.DirectBinding && |
| 4093 | "Expected a direct reference binding!"); |
| 4094 | return true; |
| 4095 | |
| 4096 | case OR_Ambiguous: |
| 4097 | ICS.setAmbiguous(); |
| 4098 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4099 | Cand != CandidateSet.end(); ++Cand) |
| 4100 | if (Cand->Viable) |
| 4101 | ICS.Ambiguous.addConversion(Cand->Function); |
| 4102 | return true; |
| 4103 | |
| 4104 | case OR_No_Viable_Function: |
| 4105 | case OR_Deleted: |
| 4106 | // There was no suitable conversion, or we found a deleted |
| 4107 | // conversion; continue with other checks. |
| 4108 | return false; |
| 4109 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4110 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4111 | llvm_unreachable("Invalid OverloadResult!"); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4114 | /// \brief Compute an implicit conversion sequence for reference |
| 4115 | /// initialization. |
| 4116 | static ImplicitConversionSequence |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4117 | TryReferenceInit(Sema &S, Expr *Init, QualType DeclType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4118 | SourceLocation DeclLoc, |
| 4119 | bool SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 4120 | bool AllowExplicit) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4121 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 4122 | |
| 4123 | // Most paths end in a failed conversion. |
| 4124 | ImplicitConversionSequence ICS; |
| 4125 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4126 | |
| 4127 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 4128 | QualType T2 = Init->getType(); |
| 4129 | |
| 4130 | // If the initializer is the address of an overloaded function, try |
| 4131 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4132 | // type of the resulting function. |
| 4133 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4134 | DeclAccessPair Found; |
| 4135 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 4136 | false, Found)) |
| 4137 | T2 = Fn->getType(); |
| 4138 | } |
| 4139 | |
| 4140 | // Compute some basic properties of the types and the initializer. |
| 4141 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 4142 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4143 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4144 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4145 | Expr::Classification InitCategory = Init->Classify(S.Context); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4146 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4147 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4148 | ObjCConversion, ObjCLifetimeConversion); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4149 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4150 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4151 | // C++0x [dcl.init.ref]p5: |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 4152 | // A reference to type "cv1 T1" is initialized by an expression |
| 4153 | // of type "cv2 T2" as follows: |
| 4154 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4155 | // -- If reference is an lvalue reference and the initializer expression |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4156 | if (!isRValRef) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4157 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 4158 | // reference-compatible with "cv2 T2," or |
| 4159 | // |
| 4160 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 4161 | if (InitCategory.isLValue() && |
| 4162 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4163 | // C++ [over.ics.ref]p1: |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4164 | // When a parameter of reference type binds directly (8.5.3) |
| 4165 | // to an argument expression, the implicit conversion sequence |
| 4166 | // is the identity conversion, unless the argument expression |
| 4167 | // has a type that is a derived class of the parameter type, |
| 4168 | // in which case the implicit conversion sequence is a |
| 4169 | // derived-to-base Conversion (13.3.3.1). |
| 4170 | ICS.setStandard(); |
| 4171 | ICS.Standard.First = ICK_Identity; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4172 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 4173 | : ObjCConversion? ICK_Compatible_Conversion |
| 4174 | : ICK_Identity; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4175 | ICS.Standard.Third = ICK_Identity; |
| 4176 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4177 | ICS.Standard.setToType(0, T2); |
| 4178 | ICS.Standard.setToType(1, T1); |
| 4179 | ICS.Standard.setToType(2, T1); |
| 4180 | ICS.Standard.ReferenceBinding = true; |
| 4181 | ICS.Standard.DirectBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4182 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4183 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4184 | ICS.Standard.BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4185 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4186 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4187 | ICS.Standard.CopyConstructor = 0; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4188 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4189 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 4190 | // derived-to-base conversions is suppressed when we're |
| 4191 | // computing the implicit conversion sequence (C++ |
| 4192 | // [over.best.ics]p2). |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4193 | return ICS; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4194 | } |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4195 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4196 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 4197 | // not reference-related to T2, and can be implicitly |
| 4198 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 4199 | // is reference-compatible with "cv3 T3" 92) (this |
| 4200 | // conversion is selected by enumerating the applicable |
| 4201 | // conversion functions (13.3.1.6) and choosing the best |
| 4202 | // one through overload resolution (13.3)), |
| 4203 | if (!SuppressUserConversions && T2->isRecordType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4204 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4205 | RefRelationship == Sema::Ref_Incompatible) { |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4206 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4207 | Init, T2, /*AllowRvalues=*/false, |
| 4208 | AllowExplicit)) |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4209 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4210 | } |
| 4211 | } |
| 4212 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4213 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 4214 | // 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] | 4215 | // shall be an rvalue reference. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4216 | // |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 4217 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 4218 | // point, which is that, due to p2 (which short-circuits reference |
| 4219 | // binding by only attempting a simple conversion for non-direct |
| 4220 | // bindings) and p3's strange wording, we allow a const volatile |
| 4221 | // reference to bind to an rvalue. Hence the check for the presence |
| 4222 | // of "const" rather than checking for "const" being the only |
| 4223 | // qualifier. |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4224 | // This is also the point where rvalue references and lvalue inits no longer |
| 4225 | // go together. |
Richard Smith | 8ab10aa | 2012-05-24 04:29:20 +0000 | [diff] [blame] | 4226 | if (!isRValRef && (!T1.isConstQualified() || T1.isVolatileQualified())) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4227 | return ICS; |
| 4228 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4229 | // -- If the initializer expression |
| 4230 | // |
| 4231 | // -- is an xvalue, class prvalue, array prvalue or function |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4232 | // lvalue and "cv1 T1" is reference-compatible with "cv2 T2", or |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4233 | if (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification && |
| 4234 | (InitCategory.isXValue() || |
| 4235 | (InitCategory.isPRValue() && (T2->isRecordType() || T2->isArrayType())) || |
| 4236 | (InitCategory.isLValue() && T2->isFunctionType()))) { |
| 4237 | ICS.setStandard(); |
| 4238 | ICS.Standard.First = ICK_Identity; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4239 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4240 | : ObjCConversion? ICK_Compatible_Conversion |
| 4241 | : ICK_Identity; |
| 4242 | ICS.Standard.Third = ICK_Identity; |
| 4243 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4244 | ICS.Standard.setToType(0, T2); |
| 4245 | ICS.Standard.setToType(1, T1); |
| 4246 | ICS.Standard.setToType(2, T1); |
| 4247 | ICS.Standard.ReferenceBinding = true; |
| 4248 | // In C++0x, this is always a direct binding. In C++98/03, it's a direct |
| 4249 | // binding unless we're binding to a class prvalue. |
| 4250 | // Note: Although xvalues wouldn't normally show up in C++98/03 code, we |
| 4251 | // allow the use of rvalue references in C++98/03 for the benefit of |
| 4252 | // standard library implementors; therefore, we need the xvalue check here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4253 | ICS.Standard.DirectBinding = |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4254 | S.getLangOpts().CPlusPlus11 || |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4255 | (InitCategory.isPRValue() && !T2->isRecordType()); |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4256 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4257 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4258 | ICS.Standard.BindsToRvalue = InitCategory.isRValue(); |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4259 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4260 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4261 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4262 | return ICS; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4263 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4264 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4265 | // -- has a class type (i.e., T2 is a class type), where T1 is not |
| 4266 | // reference-related to T2, and can be implicitly converted to |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4267 | // an xvalue, class prvalue, or function lvalue of type |
| 4268 | // "cv3 T3", where "cv1 T1" is reference-compatible with |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4269 | // "cv3 T3", |
| 4270 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4271 | // then the reference is bound to the value of the initializer |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4272 | // expression in the first case and to the result of the conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4273 | // in the second case (or, in either case, to an appropriate base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4274 | // class subobject). |
| 4275 | if (!SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4276 | T2->isRecordType() && !S.RequireCompleteType(DeclLoc, T2, 0) && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4277 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4278 | Init, T2, /*AllowRvalues=*/true, |
| 4279 | AllowExplicit)) { |
| 4280 | // In the second case, if the reference is an rvalue reference |
| 4281 | // and the second standard conversion sequence of the |
| 4282 | // user-defined conversion sequence includes an lvalue-to-rvalue |
| 4283 | // conversion, the program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4284 | if (ICS.isUserDefined() && isRValRef && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4285 | ICS.UserDefined.After.First == ICK_Lvalue_To_Rvalue) |
| 4286 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4287 | |
Douglas Gregor | 68ed68b | 2011-01-21 16:36:05 +0000 | [diff] [blame] | 4288 | return ICS; |
Rafael Espindola | aa5952c | 2011-01-22 15:32:35 +0000 | [diff] [blame] | 4289 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4290 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4291 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 4292 | // initialized from the initializer expression using the |
| 4293 | // rules for a non-reference copy initialization (8.5). The |
| 4294 | // reference is then bound to the temporary. If T1 is |
| 4295 | // reference-related to T2, cv1 must be the same |
| 4296 | // cv-qualification as, or greater cv-qualification than, |
| 4297 | // cv2; otherwise, the program is ill-formed. |
| 4298 | if (RefRelationship == Sema::Ref_Related) { |
| 4299 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 4300 | // we would be reference-compatible or reference-compatible with |
| 4301 | // added qualification. But that wasn't the case, so the reference |
| 4302 | // initialization fails. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4303 | // |
| 4304 | // Note that we only want to check address spaces and cvr-qualifiers here. |
| 4305 | // ObjC GC and lifetime qualifiers aren't important. |
| 4306 | Qualifiers T1Quals = T1.getQualifiers(); |
| 4307 | Qualifiers T2Quals = T2.getQualifiers(); |
| 4308 | T1Quals.removeObjCGCAttr(); |
| 4309 | T1Quals.removeObjCLifetime(); |
| 4310 | T2Quals.removeObjCGCAttr(); |
| 4311 | T2Quals.removeObjCLifetime(); |
| 4312 | if (!T1Quals.compatiblyIncludes(T2Quals)) |
| 4313 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4314 | } |
| 4315 | |
| 4316 | // If at least one of the types is a class type, the types are not |
| 4317 | // related, and we aren't allowed any user conversions, the |
| 4318 | // reference binding fails. This case is important for breaking |
| 4319 | // recursion, since TryImplicitConversion below will attempt to |
| 4320 | // create a temporary through the use of a copy constructor. |
| 4321 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 4322 | (T1->isRecordType() || T2->isRecordType())) |
| 4323 | return ICS; |
| 4324 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4325 | // If T1 is reference-related to T2 and the reference is an rvalue |
| 4326 | // reference, the initializer expression shall not be an lvalue. |
| 4327 | if (RefRelationship >= Sema::Ref_Related && |
| 4328 | isRValRef && Init->Classify(S.Context).isLValue()) |
| 4329 | return ICS; |
| 4330 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4331 | // C++ [over.ics.ref]p2: |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4332 | // When a parameter of reference type is not bound directly to |
| 4333 | // an argument expression, the conversion sequence is the one |
| 4334 | // required to convert the argument expression to the |
| 4335 | // underlying type of the reference according to |
| 4336 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 4337 | // to copy-initializing a temporary of the underlying type with |
| 4338 | // the argument expression. Any difference in top-level |
| 4339 | // cv-qualification is subsumed by the initialization itself |
| 4340 | // and does not constitute a conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4341 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
| 4342 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4343 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4344 | /*CStyle=*/false, |
| 4345 | /*AllowObjCWritebackConversion=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4346 | |
| 4347 | // Of course, that's still a reference binding. |
| 4348 | if (ICS.isStandard()) { |
| 4349 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4350 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4351 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4352 | ICS.Standard.BindsToRvalue = true; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4353 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4354 | ICS.Standard.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4355 | } else if (ICS.isUserDefined()) { |
Douglas Gregor | 203050c | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4356 | // Don't allow rvalue references to bind to lvalues. |
| 4357 | if (DeclType->isRValueReferenceType()) { |
| 4358 | if (const ReferenceType *RefType |
| 4359 | = ICS.UserDefined.ConversionFunction->getResultType() |
| 4360 | ->getAs<LValueReferenceType>()) { |
| 4361 | if (!RefType->getPointeeType()->isFunctionType()) { |
| 4362 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, Init, |
| 4363 | DeclType); |
| 4364 | return ICS; |
| 4365 | } |
| 4366 | } |
| 4367 | } |
| 4368 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4369 | ICS.UserDefined.After.ReferenceBinding = true; |
Douglas Gregor | f20d272 | 2011-08-15 13:59:46 +0000 | [diff] [blame] | 4370 | ICS.UserDefined.After.IsLvalueReference = !isRValRef; |
| 4371 | ICS.UserDefined.After.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4372 | ICS.UserDefined.After.BindsToRvalue = true; |
| 4373 | ICS.UserDefined.After.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4374 | ICS.UserDefined.After.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4375 | } |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4376 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4377 | return ICS; |
| 4378 | } |
| 4379 | |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4380 | static ImplicitConversionSequence |
| 4381 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
| 4382 | bool SuppressUserConversions, |
| 4383 | bool InOverloadResolution, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4384 | bool AllowObjCWritebackConversion, |
| 4385 | bool AllowExplicit = false); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4386 | |
| 4387 | /// TryListConversion - Try to copy-initialize a value of type ToType from the |
| 4388 | /// initializer list From. |
| 4389 | static ImplicitConversionSequence |
| 4390 | TryListConversion(Sema &S, InitListExpr *From, QualType ToType, |
| 4391 | bool SuppressUserConversions, |
| 4392 | bool InOverloadResolution, |
| 4393 | bool AllowObjCWritebackConversion) { |
| 4394 | // C++11 [over.ics.list]p1: |
| 4395 | // When an argument is an initializer list, it is not an expression and |
| 4396 | // special rules apply for converting it to a parameter type. |
| 4397 | |
| 4398 | ImplicitConversionSequence Result; |
| 4399 | Result.setBad(BadConversionSequence::no_conversion, From, ToType); |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4400 | Result.setListInitializationSequence(); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4401 | |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4402 | // 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] | 4403 | // initialized from init lists. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4404 | if (S.RequireCompleteType(From->getLocStart(), ToType, 0)) |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4405 | return Result; |
| 4406 | |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4407 | // C++11 [over.ics.list]p2: |
| 4408 | // If the parameter type is std::initializer_list<X> or "array of X" and |
| 4409 | // all the elements can be implicitly converted to X, the implicit |
| 4410 | // conversion sequence is the worst conversion necessary to convert an |
| 4411 | // element of the list to X. |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4412 | bool toStdInitializerList = false; |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4413 | QualType X; |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4414 | if (ToType->isArrayType()) |
Richard Smith | 2801d9a | 2012-12-09 06:48:56 +0000 | [diff] [blame] | 4415 | X = S.Context.getAsArrayType(ToType)->getElementType(); |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4416 | else |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4417 | toStdInitializerList = S.isStdInitializerList(ToType, &X); |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4418 | if (!X.isNull()) { |
| 4419 | for (unsigned i = 0, e = From->getNumInits(); i < e; ++i) { |
| 4420 | Expr *Init = From->getInit(i); |
| 4421 | ImplicitConversionSequence ICS = |
| 4422 | TryCopyInitialization(S, Init, X, SuppressUserConversions, |
| 4423 | InOverloadResolution, |
| 4424 | AllowObjCWritebackConversion); |
| 4425 | // If a single element isn't convertible, fail. |
| 4426 | if (ICS.isBad()) { |
| 4427 | Result = ICS; |
| 4428 | break; |
| 4429 | } |
| 4430 | // Otherwise, look for the worst conversion. |
| 4431 | if (Result.isBad() || |
| 4432 | CompareImplicitConversionSequences(S, ICS, Result) == |
| 4433 | ImplicitConversionSequence::Worse) |
| 4434 | Result = ICS; |
| 4435 | } |
Douglas Gregor | 5b4bf13 | 2012-04-04 23:09:20 +0000 | [diff] [blame] | 4436 | |
| 4437 | // For an empty list, we won't have computed any conversion sequence. |
| 4438 | // Introduce the identity conversion sequence. |
| 4439 | if (From->getNumInits() == 0) { |
| 4440 | Result.setStandard(); |
| 4441 | Result.Standard.setAsIdentityConversion(); |
| 4442 | Result.Standard.setFromType(ToType); |
| 4443 | Result.Standard.setAllToTypes(ToType); |
| 4444 | } |
| 4445 | |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4446 | Result.setListInitializationSequence(); |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4447 | Result.setStdInitializerListElement(toStdInitializerList); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4448 | return Result; |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4449 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4450 | |
| 4451 | // C++11 [over.ics.list]p3: |
| 4452 | // Otherwise, if the parameter is a non-aggregate class X and overload |
| 4453 | // resolution chooses a single best constructor [...] the implicit |
| 4454 | // conversion sequence is a user-defined conversion sequence. If multiple |
| 4455 | // constructors are viable but none is better than the others, the |
| 4456 | // implicit conversion sequence is a user-defined conversion sequence. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4457 | if (ToType->isRecordType() && !ToType->isAggregateType()) { |
| 4458 | // This function can deal with initializer lists. |
| 4459 | Result = TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 4460 | /*AllowExplicit=*/false, |
| 4461 | InOverloadResolution, /*CStyle=*/false, |
| 4462 | AllowObjCWritebackConversion); |
| 4463 | Result.setListInitializationSequence(); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4464 | return Result; |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4465 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4466 | |
| 4467 | // C++11 [over.ics.list]p4: |
| 4468 | // Otherwise, if the parameter has an aggregate type which can be |
| 4469 | // initialized from the initializer list [...] the implicit conversion |
| 4470 | // sequence is a user-defined conversion sequence. |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4471 | if (ToType->isAggregateType()) { |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4472 | // Type is an aggregate, argument is an init list. At this point it comes |
| 4473 | // down to checking whether the initialization works. |
| 4474 | // FIXME: Find out whether this parameter is consumed or not. |
| 4475 | InitializedEntity Entity = |
| 4476 | InitializedEntity::InitializeParameter(S.Context, ToType, |
| 4477 | /*Consumed=*/false); |
| 4478 | if (S.CanPerformCopyInitialization(Entity, S.Owned(From))) { |
| 4479 | Result.setUserDefined(); |
| 4480 | Result.UserDefined.Before.setAsIdentityConversion(); |
| 4481 | // Initializer lists don't have a type. |
| 4482 | Result.UserDefined.Before.setFromType(QualType()); |
| 4483 | Result.UserDefined.Before.setAllToTypes(QualType()); |
| 4484 | |
| 4485 | Result.UserDefined.After.setAsIdentityConversion(); |
| 4486 | Result.UserDefined.After.setFromType(ToType); |
| 4487 | Result.UserDefined.After.setAllToTypes(ToType); |
Benjamin Kramer | 83db10e | 2012-02-02 19:35:29 +0000 | [diff] [blame] | 4488 | Result.UserDefined.ConversionFunction = 0; |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4489 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4490 | return Result; |
| 4491 | } |
| 4492 | |
| 4493 | // C++11 [over.ics.list]p5: |
| 4494 | // 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] | 4495 | if (ToType->isReferenceType()) { |
| 4496 | // The standard is notoriously unclear here, since 13.3.3.1.4 doesn't |
| 4497 | // mention initializer lists in any way. So we go by what list- |
| 4498 | // initialization would do and try to extrapolate from that. |
| 4499 | |
| 4500 | QualType T1 = ToType->getAs<ReferenceType>()->getPointeeType(); |
| 4501 | |
| 4502 | // If the initializer list has a single element that is reference-related |
| 4503 | // to the parameter type, we initialize the reference from that. |
| 4504 | if (From->getNumInits() == 1) { |
| 4505 | Expr *Init = From->getInit(0); |
| 4506 | |
| 4507 | QualType T2 = Init->getType(); |
| 4508 | |
| 4509 | // If the initializer is the address of an overloaded function, try |
| 4510 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4511 | // type of the resulting function. |
| 4512 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4513 | DeclAccessPair Found; |
| 4514 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction( |
| 4515 | Init, ToType, false, Found)) |
| 4516 | T2 = Fn->getType(); |
| 4517 | } |
| 4518 | |
| 4519 | // Compute some basic properties of the types and the initializer. |
| 4520 | bool dummy1 = false; |
| 4521 | bool dummy2 = false; |
| 4522 | bool dummy3 = false; |
| 4523 | Sema::ReferenceCompareResult RefRelationship |
| 4524 | = S.CompareReferenceRelationship(From->getLocStart(), T1, T2, dummy1, |
| 4525 | dummy2, dummy3); |
| 4526 | |
| 4527 | if (RefRelationship >= Sema::Ref_Related) |
| 4528 | return TryReferenceInit(S, Init, ToType, |
| 4529 | /*FIXME:*/From->getLocStart(), |
| 4530 | SuppressUserConversions, |
| 4531 | /*AllowExplicit=*/false); |
| 4532 | } |
| 4533 | |
| 4534 | // Otherwise, we bind the reference to a temporary created from the |
| 4535 | // initializer list. |
| 4536 | Result = TryListConversion(S, From, T1, SuppressUserConversions, |
| 4537 | InOverloadResolution, |
| 4538 | AllowObjCWritebackConversion); |
| 4539 | if (Result.isFailure()) |
| 4540 | return Result; |
| 4541 | assert(!Result.isEllipsis() && |
| 4542 | "Sub-initialization cannot result in ellipsis conversion."); |
| 4543 | |
| 4544 | // Can we even bind to a temporary? |
| 4545 | if (ToType->isRValueReferenceType() || |
| 4546 | (T1.isConstQualified() && !T1.isVolatileQualified())) { |
| 4547 | StandardConversionSequence &SCS = Result.isStandard() ? Result.Standard : |
| 4548 | Result.UserDefined.After; |
| 4549 | SCS.ReferenceBinding = true; |
| 4550 | SCS.IsLvalueReference = ToType->isLValueReferenceType(); |
| 4551 | SCS.BindsToRvalue = true; |
| 4552 | SCS.BindsToFunctionLvalue = false; |
| 4553 | SCS.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4554 | SCS.ObjCLifetimeConversionBinding = false; |
| 4555 | } else |
| 4556 | Result.setBad(BadConversionSequence::lvalue_ref_to_rvalue, |
| 4557 | From, ToType); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4558 | return Result; |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4559 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4560 | |
| 4561 | // C++11 [over.ics.list]p6: |
| 4562 | // Otherwise, if the parameter type is not a class: |
| 4563 | if (!ToType->isRecordType()) { |
| 4564 | // - if the initializer list has one element, the implicit conversion |
| 4565 | // sequence is the one required to convert the element to the |
| 4566 | // parameter type. |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4567 | unsigned NumInits = From->getNumInits(); |
| 4568 | if (NumInits == 1) |
| 4569 | Result = TryCopyInitialization(S, From->getInit(0), ToType, |
| 4570 | SuppressUserConversions, |
| 4571 | InOverloadResolution, |
| 4572 | AllowObjCWritebackConversion); |
| 4573 | // - if the initializer list has no elements, the implicit conversion |
| 4574 | // sequence is the identity conversion. |
| 4575 | else if (NumInits == 0) { |
| 4576 | Result.setStandard(); |
| 4577 | Result.Standard.setAsIdentityConversion(); |
John McCall | e14ba2c | 2012-04-04 02:40:27 +0000 | [diff] [blame] | 4578 | Result.Standard.setFromType(ToType); |
| 4579 | Result.Standard.setAllToTypes(ToType); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4580 | } |
Sebastian Redl | 2422e82 | 2012-02-28 23:36:38 +0000 | [diff] [blame] | 4581 | Result.setListInitializationSequence(); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4582 | return Result; |
| 4583 | } |
| 4584 | |
| 4585 | // C++11 [over.ics.list]p7: |
| 4586 | // In all cases other than those enumerated above, no conversion is possible |
| 4587 | return Result; |
| 4588 | } |
| 4589 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4590 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 4591 | /// ToType from the expression From. Return the implicit conversion |
| 4592 | /// sequence required to pass this argument, which may be a bad |
| 4593 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4594 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | 74e386e | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 4595 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4596 | static ImplicitConversionSequence |
| 4597 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4598 | bool SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4599 | bool InOverloadResolution, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4600 | bool AllowObjCWritebackConversion, |
| 4601 | bool AllowExplicit) { |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4602 | if (InitListExpr *FromInitList = dyn_cast<InitListExpr>(From)) |
| 4603 | return TryListConversion(S, FromInitList, ToType, SuppressUserConversions, |
| 4604 | InOverloadResolution,AllowObjCWritebackConversion); |
| 4605 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4606 | if (ToType->isReferenceType()) |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4607 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4608 | /*FIXME:*/From->getLocStart(), |
| 4609 | SuppressUserConversions, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4610 | AllowExplicit); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4611 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4612 | return TryImplicitConversion(S, From, ToType, |
| 4613 | SuppressUserConversions, |
| 4614 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4615 | InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4616 | /*CStyle=*/false, |
| 4617 | AllowObjCWritebackConversion); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4618 | } |
| 4619 | |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 4620 | static bool TryCopyInitialization(const CanQualType FromQTy, |
| 4621 | const CanQualType ToQTy, |
| 4622 | Sema &S, |
| 4623 | SourceLocation Loc, |
| 4624 | ExprValueKind FromVK) { |
| 4625 | OpaqueValueExpr TmpExpr(Loc, FromQTy, FromVK); |
| 4626 | ImplicitConversionSequence ICS = |
| 4627 | TryCopyInitialization(S, &TmpExpr, ToQTy, true, true, false); |
| 4628 | |
| 4629 | return !ICS.isBad(); |
| 4630 | } |
| 4631 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4632 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 4633 | /// parameter of the given member function (@c Method) from the |
| 4634 | /// expression @p From. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4635 | static ImplicitConversionSequence |
Richard Smith | 98bfbf5 | 2013-01-26 02:07:32 +0000 | [diff] [blame] | 4636 | TryObjectArgumentInitialization(Sema &S, QualType FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4637 | Expr::Classification FromClassification, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4638 | CXXMethodDecl *Method, |
| 4639 | CXXRecordDecl *ActingContext) { |
| 4640 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 4641 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 4642 | // const volatile object. |
| 4643 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 4644 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4645 | QualType ImplicitParamType = S.Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4646 | |
| 4647 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 4648 | // to exit early. |
| 4649 | ImplicitConversionSequence ICS; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4650 | |
| 4651 | // We need to have an object of class type. |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4652 | if (const PointerType *PT = FromType->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4653 | FromType = PT->getPointeeType(); |
| 4654 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4655 | // When we had a pointer, it's implicitly dereferenced, so we |
| 4656 | // better have an lvalue. |
| 4657 | assert(FromClassification.isLValue()); |
| 4658 | } |
| 4659 | |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4660 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4661 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4662 | // C++0x [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4663 | // For non-static member functions, the type of the implicit object |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4664 | // parameter is |
| 4665 | // |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4666 | // - "lvalue reference to cv X" for functions declared without a |
| 4667 | // ref-qualifier or with the & ref-qualifier |
| 4668 | // - "rvalue reference to cv X" for functions declared with the && |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4669 | // ref-qualifier |
| 4670 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4671 | // where X is the class of which the function is a member and cv is the |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4672 | // cv-qualification on the member function declaration. |
| 4673 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4674 | // However, when finding an implicit conversion sequence for the argument, we |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4675 | // are not allowed to create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4676 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 4677 | // reference binding here, that allows class rvalues to bind to |
| 4678 | // non-constant references. |
| 4679 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4680 | // First check the qualifiers. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4681 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4682 | if (ImplicitParamType.getCVRQualifiers() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4683 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4684 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4685 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
Richard Smith | 98bfbf5 | 2013-01-26 02:07:32 +0000 | [diff] [blame] | 4686 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4687 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4688 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4689 | |
| 4690 | // Check that we have either the same type or a derived type. It |
| 4691 | // affects the conversion rank. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4692 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4693 | ImplicitConversionKind SecondKind; |
| 4694 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 4695 | SecondKind = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4696 | } else if (S.IsDerivedFrom(FromType, ClassType)) |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4697 | SecondKind = ICK_Derived_To_Base; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4698 | else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4699 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 4700 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4701 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4702 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4703 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4704 | // Check the ref-qualifier. |
| 4705 | switch (Method->getRefQualifier()) { |
| 4706 | case RQ_None: |
| 4707 | // Do nothing; we don't care about lvalueness or rvalueness. |
| 4708 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4709 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4710 | case RQ_LValue: |
| 4711 | if (!FromClassification.isLValue() && Quals != Qualifiers::Const) { |
| 4712 | // non-const lvalue reference cannot bind to an rvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4713 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4714 | ImplicitParamType); |
| 4715 | return ICS; |
| 4716 | } |
| 4717 | break; |
| 4718 | |
| 4719 | case RQ_RValue: |
| 4720 | if (!FromClassification.isRValue()) { |
| 4721 | // rvalue reference cannot bind to an lvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4722 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4723 | ImplicitParamType); |
| 4724 | return ICS; |
| 4725 | } |
| 4726 | break; |
| 4727 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4728 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4729 | // Success. Mark this as a reference binding. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4730 | ICS.setStandard(); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4731 | ICS.Standard.setAsIdentityConversion(); |
| 4732 | ICS.Standard.Second = SecondKind; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4733 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 4734 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4735 | ICS.Standard.ReferenceBinding = true; |
| 4736 | ICS.Standard.DirectBinding = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4737 | ICS.Standard.IsLvalueReference = Method->getRefQualifier() != RQ_RValue; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4738 | ICS.Standard.BindsToFunctionLvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4739 | ICS.Standard.BindsToRvalue = FromClassification.isRValue(); |
| 4740 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier |
| 4741 | = (Method->getRefQualifier() == RQ_None); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4742 | return ICS; |
| 4743 | } |
| 4744 | |
| 4745 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 4746 | /// the implicit object parameter for the given Method with the given |
| 4747 | /// expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4748 | ExprResult |
| 4749 | Sema::PerformObjectArgumentInitialization(Expr *From, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4750 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4751 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4752 | CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4753 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4754 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4755 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4756 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4757 | Expr::Classification FromClassification; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4758 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4759 | FromRecordType = PT->getPointeeType(); |
| 4760 | DestType = Method->getThisType(Context); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4761 | FromClassification = Expr::Classification::makeSimpleLValue(); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4762 | } else { |
| 4763 | FromRecordType = From->getType(); |
| 4764 | DestType = ImplicitParamRecordType; |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4765 | FromClassification = From->Classify(Context); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4766 | } |
| 4767 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4768 | // Note that we always use the true parent context when performing |
| 4769 | // the actual argument initialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4770 | ImplicitConversionSequence ICS |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4771 | = TryObjectArgumentInitialization(*this, From->getType(), FromClassification, |
| 4772 | Method, Method->getParent()); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4773 | if (ICS.isBad()) { |
| 4774 | if (ICS.Bad.Kind == BadConversionSequence::bad_qualifiers) { |
| 4775 | Qualifiers FromQs = FromRecordType.getQualifiers(); |
| 4776 | Qualifiers ToQs = DestType.getQualifiers(); |
| 4777 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 4778 | if (CVR) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4779 | Diag(From->getLocStart(), |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4780 | diag::err_member_function_call_bad_cvr) |
| 4781 | << Method->getDeclName() << FromRecordType << (CVR - 1) |
| 4782 | << From->getSourceRange(); |
| 4783 | Diag(Method->getLocation(), diag::note_previous_decl) |
| 4784 | << Method->getDeclName(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4785 | return ExprError(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4786 | } |
| 4787 | } |
| 4788 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4789 | return Diag(From->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4790 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4791 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4792 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4793 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4794 | if (ICS.Standard.Second == ICK_Derived_To_Base) { |
| 4795 | ExprResult FromRes = |
| 4796 | PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
| 4797 | if (FromRes.isInvalid()) |
| 4798 | return ExprError(); |
| 4799 | From = FromRes.take(); |
| 4800 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4801 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4802 | if (!Context.hasSameType(From->getType(), DestType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4803 | From = ImpCastExprToType(From, DestType, CK_NoOp, |
Richard Smith | acdfa4d | 2011-11-10 23:32:36 +0000 | [diff] [blame] | 4804 | From->getValueKind()).take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4805 | return Owned(From); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4806 | } |
| 4807 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4808 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 4809 | /// expression From to bool (C++0x [conv]p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4810 | static ImplicitConversionSequence |
| 4811 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
Douglas Gregor | c6dfe19 | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 4812 | // FIXME: This is pretty broken. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4813 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 4814 | // FIXME: Are these flags correct? |
| 4815 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4816 | /*AllowExplicit=*/true, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4817 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4818 | /*CStyle=*/false, |
| 4819 | /*AllowObjCWritebackConversion=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4820 | } |
| 4821 | |
| 4822 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 4823 | /// of the expression From to bool (C++0x [conv]p3). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4824 | ExprResult Sema::PerformContextuallyConvertToBool(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4825 | if (checkPlaceholderForOverload(*this, From)) |
| 4826 | return ExprError(); |
| 4827 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4828 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4829 | if (!ICS.isBad()) |
| 4830 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4831 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 4832 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4833 | return Diag(From->getLocStart(), |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4834 | diag::err_typecheck_bool_condition) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 4835 | << From->getType() << From->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4836 | return ExprError(); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4837 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4838 | |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4839 | /// Check that the specified conversion is permitted in a converted constant |
| 4840 | /// expression, according to C++11 [expr.const]p3. Return true if the conversion |
| 4841 | /// is acceptable. |
| 4842 | static bool CheckConvertedConstantConversions(Sema &S, |
| 4843 | StandardConversionSequence &SCS) { |
| 4844 | // Since we know that the target type is an integral or unscoped enumeration |
| 4845 | // type, most conversion kinds are impossible. All possible First and Third |
| 4846 | // conversions are fine. |
| 4847 | switch (SCS.Second) { |
| 4848 | case ICK_Identity: |
| 4849 | case ICK_Integral_Promotion: |
| 4850 | case ICK_Integral_Conversion: |
Guy Benyei | 6959acd | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 4851 | case ICK_Zero_Event_Conversion: |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4852 | return true; |
| 4853 | |
| 4854 | case ICK_Boolean_Conversion: |
Richard Smith | 2bcb984 | 2012-09-13 22:00:12 +0000 | [diff] [blame] | 4855 | // Conversion from an integral or unscoped enumeration type to bool is |
| 4856 | // classified as ICK_Boolean_Conversion, but it's also an integral |
| 4857 | // conversion, so it's permitted in a converted constant expression. |
| 4858 | return SCS.getFromType()->isIntegralOrUnscopedEnumerationType() && |
| 4859 | SCS.getToType(2)->isBooleanType(); |
| 4860 | |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4861 | case ICK_Floating_Integral: |
| 4862 | case ICK_Complex_Real: |
| 4863 | return false; |
| 4864 | |
| 4865 | case ICK_Lvalue_To_Rvalue: |
| 4866 | case ICK_Array_To_Pointer: |
| 4867 | case ICK_Function_To_Pointer: |
| 4868 | case ICK_NoReturn_Adjustment: |
| 4869 | case ICK_Qualification: |
| 4870 | case ICK_Compatible_Conversion: |
| 4871 | case ICK_Vector_Conversion: |
| 4872 | case ICK_Vector_Splat: |
| 4873 | case ICK_Derived_To_Base: |
| 4874 | case ICK_Pointer_Conversion: |
| 4875 | case ICK_Pointer_Member: |
| 4876 | case ICK_Block_Pointer_Conversion: |
| 4877 | case ICK_Writeback_Conversion: |
| 4878 | case ICK_Floating_Promotion: |
| 4879 | case ICK_Complex_Promotion: |
| 4880 | case ICK_Complex_Conversion: |
| 4881 | case ICK_Floating_Conversion: |
| 4882 | case ICK_TransparentUnionConversion: |
| 4883 | llvm_unreachable("unexpected second conversion kind"); |
| 4884 | |
| 4885 | case ICK_Num_Conversion_Kinds: |
| 4886 | break; |
| 4887 | } |
| 4888 | |
| 4889 | llvm_unreachable("unknown conversion kind"); |
| 4890 | } |
| 4891 | |
| 4892 | /// CheckConvertedConstantExpression - Check that the expression From is a |
| 4893 | /// converted constant expression of type T, perform the conversion and produce |
| 4894 | /// the converted expression, per C++11 [expr.const]p3. |
| 4895 | ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, |
| 4896 | llvm::APSInt &Value, |
| 4897 | CCEKind CCE) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4898 | assert(LangOpts.CPlusPlus11 && "converted constant expression outside C++11"); |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4899 | assert(T->isIntegralOrEnumerationType() && "unexpected converted const type"); |
| 4900 | |
| 4901 | if (checkPlaceholderForOverload(*this, From)) |
| 4902 | return ExprError(); |
| 4903 | |
| 4904 | // C++11 [expr.const]p3 with proposed wording fixes: |
| 4905 | // A converted constant expression of type T is a core constant expression, |
| 4906 | // implicitly converted to a prvalue of type T, where the converted |
| 4907 | // expression is a literal constant expression and the implicit conversion |
| 4908 | // sequence contains only user-defined conversions, lvalue-to-rvalue |
| 4909 | // conversions, integral promotions, and integral conversions other than |
| 4910 | // narrowing conversions. |
| 4911 | ImplicitConversionSequence ICS = |
| 4912 | TryImplicitConversion(From, T, |
| 4913 | /*SuppressUserConversions=*/false, |
| 4914 | /*AllowExplicit=*/false, |
| 4915 | /*InOverloadResolution=*/false, |
| 4916 | /*CStyle=*/false, |
| 4917 | /*AllowObjcWritebackConversion=*/false); |
| 4918 | StandardConversionSequence *SCS = 0; |
| 4919 | switch (ICS.getKind()) { |
| 4920 | case ImplicitConversionSequence::StandardConversion: |
| 4921 | if (!CheckConvertedConstantConversions(*this, ICS.Standard)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4922 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4923 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 4924 | << From->getType() << From->getSourceRange() << T; |
| 4925 | SCS = &ICS.Standard; |
| 4926 | break; |
| 4927 | case ImplicitConversionSequence::UserDefinedConversion: |
| 4928 | // We are converting from class type to an integral or enumeration type, so |
| 4929 | // the Before sequence must be trivial. |
| 4930 | if (!CheckConvertedConstantConversions(*this, ICS.UserDefined.After)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4931 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4932 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 4933 | << From->getType() << From->getSourceRange() << T; |
| 4934 | SCS = &ICS.UserDefined.After; |
| 4935 | break; |
| 4936 | case ImplicitConversionSequence::AmbiguousConversion: |
| 4937 | case ImplicitConversionSequence::BadConversion: |
| 4938 | if (!DiagnoseMultipleUserDefinedConversion(From, T)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4939 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4940 | diag::err_typecheck_converted_constant_expression) |
| 4941 | << From->getType() << From->getSourceRange() << T; |
| 4942 | return ExprError(); |
| 4943 | |
| 4944 | case ImplicitConversionSequence::EllipsisConversion: |
| 4945 | llvm_unreachable("ellipsis conversion in converted constant expression"); |
| 4946 | } |
| 4947 | |
| 4948 | ExprResult Result = PerformImplicitConversion(From, T, ICS, AA_Converting); |
| 4949 | if (Result.isInvalid()) |
| 4950 | return Result; |
| 4951 | |
| 4952 | // Check for a narrowing implicit conversion. |
| 4953 | APValue PreNarrowingValue; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4954 | QualType PreNarrowingType; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4955 | switch (SCS->getNarrowingKind(Context, Result.get(), PreNarrowingValue, |
| 4956 | PreNarrowingType)) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4957 | case NK_Variable_Narrowing: |
| 4958 | // Implicit conversion to a narrower type, and the value is not a constant |
| 4959 | // expression. We'll diagnose this in a moment. |
| 4960 | case NK_Not_Narrowing: |
| 4961 | break; |
| 4962 | |
| 4963 | case NK_Constant_Narrowing: |
Eli Friedman | 1ef28db | 2012-03-29 23:39:39 +0000 | [diff] [blame] | 4964 | Diag(From->getLocStart(), |
| 4965 | isSFINAEContext() ? diag::err_cce_narrowing_sfinae : |
| 4966 | diag::err_cce_narrowing) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4967 | << CCE << /*Constant*/1 |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4968 | << PreNarrowingValue.getAsString(Context, PreNarrowingType) << T; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4969 | break; |
| 4970 | |
| 4971 | case NK_Type_Narrowing: |
Eli Friedman | 1ef28db | 2012-03-29 23:39:39 +0000 | [diff] [blame] | 4972 | Diag(From->getLocStart(), |
| 4973 | isSFINAEContext() ? diag::err_cce_narrowing_sfinae : |
| 4974 | diag::err_cce_narrowing) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4975 | << CCE << /*Constant*/0 << From->getType() << T; |
| 4976 | break; |
| 4977 | } |
| 4978 | |
| 4979 | // Check the expression is a constant expression. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4980 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4981 | Expr::EvalResult Eval; |
| 4982 | Eval.Diag = &Notes; |
| 4983 | |
Douglas Gregor | 484f6fa | 2013-04-08 23:24:07 +0000 | [diff] [blame] | 4984 | if (!Result.get()->EvaluateAsRValue(Eval, Context) || !Eval.Val.isInt()) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4985 | // The expression can't be folded, so we can't keep it at this position in |
| 4986 | // the AST. |
| 4987 | Result = ExprError(); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 4988 | } else { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4989 | Value = Eval.Val.getInt(); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 4990 | |
| 4991 | if (Notes.empty()) { |
| 4992 | // It's a constant expression. |
| 4993 | return Result; |
| 4994 | } |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4995 | } |
| 4996 | |
| 4997 | // It's not a constant expression. Produce an appropriate diagnostic. |
| 4998 | if (Notes.size() == 1 && |
| 4999 | Notes[0].second.getDiagID() == diag::note_invalid_subexpr_in_const_expr) |
| 5000 | Diag(Notes[0].first, diag::err_expr_not_cce) << CCE; |
| 5001 | else { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5002 | Diag(From->getLocStart(), diag::err_expr_not_cce) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5003 | << CCE << From->getSourceRange(); |
| 5004 | for (unsigned I = 0; I < Notes.size(); ++I) |
| 5005 | Diag(Notes[I].first, Notes[I].second); |
| 5006 | } |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5007 | return Result; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5008 | } |
| 5009 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5010 | /// dropPointerConversions - If the given standard conversion sequence |
| 5011 | /// involves any pointer conversions, remove them. This may change |
| 5012 | /// the result type of the conversion sequence. |
| 5013 | static void dropPointerConversion(StandardConversionSequence &SCS) { |
| 5014 | if (SCS.Second == ICK_Pointer_Conversion) { |
| 5015 | SCS.Second = ICK_Identity; |
| 5016 | SCS.Third = ICK_Identity; |
| 5017 | SCS.ToTypePtrs[2] = SCS.ToTypePtrs[1] = SCS.ToTypePtrs[0]; |
| 5018 | } |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5019 | } |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5020 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5021 | /// TryContextuallyConvertToObjCPointer - Attempt to contextually |
| 5022 | /// convert the expression From to an Objective-C pointer type. |
| 5023 | static ImplicitConversionSequence |
| 5024 | TryContextuallyConvertToObjCPointer(Sema &S, Expr *From) { |
| 5025 | // Do an implicit conversion to 'id'. |
| 5026 | QualType Ty = S.Context.getObjCIdType(); |
| 5027 | ImplicitConversionSequence ICS |
| 5028 | = TryImplicitConversion(S, From, Ty, |
| 5029 | // FIXME: Are these flags correct? |
| 5030 | /*SuppressUserConversions=*/false, |
| 5031 | /*AllowExplicit=*/true, |
| 5032 | /*InOverloadResolution=*/false, |
| 5033 | /*CStyle=*/false, |
| 5034 | /*AllowObjCWritebackConversion=*/false); |
| 5035 | |
| 5036 | // Strip off any final conversions to 'id'. |
| 5037 | switch (ICS.getKind()) { |
| 5038 | case ImplicitConversionSequence::BadConversion: |
| 5039 | case ImplicitConversionSequence::AmbiguousConversion: |
| 5040 | case ImplicitConversionSequence::EllipsisConversion: |
| 5041 | break; |
| 5042 | |
| 5043 | case ImplicitConversionSequence::UserDefinedConversion: |
| 5044 | dropPointerConversion(ICS.UserDefined.After); |
| 5045 | break; |
| 5046 | |
| 5047 | case ImplicitConversionSequence::StandardConversion: |
| 5048 | dropPointerConversion(ICS.Standard); |
| 5049 | break; |
| 5050 | } |
| 5051 | |
| 5052 | return ICS; |
| 5053 | } |
| 5054 | |
| 5055 | /// PerformContextuallyConvertToObjCPointer - Perform a contextual |
| 5056 | /// conversion of the expression From to an Objective-C pointer type. |
| 5057 | ExprResult Sema::PerformContextuallyConvertToObjCPointer(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 5058 | if (checkPlaceholderForOverload(*this, From)) |
| 5059 | return ExprError(); |
| 5060 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5061 | QualType Ty = Context.getObjCIdType(); |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5062 | ImplicitConversionSequence ICS = |
| 5063 | TryContextuallyConvertToObjCPointer(*this, From); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5064 | if (!ICS.isBad()) |
| 5065 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5066 | return ExprError(); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5067 | } |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5068 | |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5069 | /// Determine whether the provided type is an integral type, or an enumeration |
| 5070 | /// type of a permitted flavor. |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5071 | bool Sema::ICEConvertDiagnoser::match(QualType T) { |
| 5072 | return AllowScopedEnumerations ? T->isIntegralOrEnumerationType() |
| 5073 | : T->isIntegralOrUnscopedEnumerationType(); |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5074 | } |
| 5075 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5076 | static ExprResult |
| 5077 | diagnoseAmbiguousConversion(Sema &SemaRef, SourceLocation Loc, Expr *From, |
| 5078 | Sema::ContextualImplicitConverter &Converter, |
| 5079 | QualType T, UnresolvedSetImpl &ViableConversions) { |
| 5080 | |
| 5081 | if (Converter.Suppress) |
| 5082 | return ExprError(); |
| 5083 | |
| 5084 | Converter.diagnoseAmbiguous(SemaRef, Loc, T) << From->getSourceRange(); |
| 5085 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5086 | CXXConversionDecl *Conv = |
| 5087 | cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 5088 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 5089 | Converter.noteAmbiguous(SemaRef, Conv, ConvTy); |
| 5090 | } |
| 5091 | return SemaRef.Owned(From); |
| 5092 | } |
| 5093 | |
| 5094 | static bool |
| 5095 | diagnoseNoViableConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
| 5096 | Sema::ContextualImplicitConverter &Converter, |
| 5097 | QualType T, bool HadMultipleCandidates, |
| 5098 | UnresolvedSetImpl &ExplicitConversions) { |
| 5099 | if (ExplicitConversions.size() == 1 && !Converter.Suppress) { |
| 5100 | DeclAccessPair Found = ExplicitConversions[0]; |
| 5101 | CXXConversionDecl *Conversion = |
| 5102 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 5103 | |
| 5104 | // The user probably meant to invoke the given explicit |
| 5105 | // conversion; use it. |
| 5106 | QualType ConvTy = Conversion->getConversionType().getNonReferenceType(); |
| 5107 | std::string TypeStr; |
| 5108 | ConvTy.getAsStringInternal(TypeStr, SemaRef.getPrintingPolicy()); |
| 5109 | |
| 5110 | Converter.diagnoseExplicitConv(SemaRef, Loc, T, ConvTy) |
| 5111 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 5112 | "static_cast<" + TypeStr + ">(") |
| 5113 | << FixItHint::CreateInsertion( |
| 5114 | SemaRef.PP.getLocForEndOfToken(From->getLocEnd()), ")"); |
| 5115 | Converter.noteExplicitConv(SemaRef, Conversion, ConvTy); |
| 5116 | |
| 5117 | // If we aren't in a SFINAE context, build a call to the |
| 5118 | // explicit conversion function. |
| 5119 | if (SemaRef.isSFINAEContext()) |
| 5120 | return true; |
| 5121 | |
| 5122 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
| 5123 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5124 | HadMultipleCandidates); |
| 5125 | if (Result.isInvalid()) |
| 5126 | return true; |
| 5127 | // Record usage of conversion in an implicit cast. |
| 5128 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
| 5129 | CK_UserDefinedConversion, Result.get(), 0, |
| 5130 | Result.get()->getValueKind()); |
| 5131 | } |
| 5132 | return false; |
| 5133 | } |
| 5134 | |
| 5135 | static bool recordConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
| 5136 | Sema::ContextualImplicitConverter &Converter, |
| 5137 | QualType T, bool HadMultipleCandidates, |
| 5138 | DeclAccessPair &Found) { |
| 5139 | CXXConversionDecl *Conversion = |
| 5140 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 5141 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
| 5142 | |
| 5143 | QualType ToType = Conversion->getConversionType().getNonReferenceType(); |
| 5144 | if (!Converter.SuppressConversion) { |
| 5145 | if (SemaRef.isSFINAEContext()) |
| 5146 | return true; |
| 5147 | |
| 5148 | Converter.diagnoseConversion(SemaRef, Loc, T, ToType) |
| 5149 | << From->getSourceRange(); |
| 5150 | } |
| 5151 | |
| 5152 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5153 | HadMultipleCandidates); |
| 5154 | if (Result.isInvalid()) |
| 5155 | return true; |
| 5156 | // Record usage of conversion in an implicit cast. |
| 5157 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
| 5158 | CK_UserDefinedConversion, Result.get(), 0, |
| 5159 | Result.get()->getValueKind()); |
| 5160 | return false; |
| 5161 | } |
| 5162 | |
| 5163 | static ExprResult finishContextualImplicitConversion( |
| 5164 | Sema &SemaRef, SourceLocation Loc, Expr *From, |
| 5165 | Sema::ContextualImplicitConverter &Converter) { |
| 5166 | if (!Converter.match(From->getType()) && !Converter.Suppress) |
| 5167 | Converter.diagnoseNoMatch(SemaRef, Loc, From->getType()) |
| 5168 | << From->getSourceRange(); |
| 5169 | |
| 5170 | return SemaRef.DefaultLvalueConversion(From); |
| 5171 | } |
| 5172 | |
| 5173 | static void |
| 5174 | collectViableConversionCandidates(Sema &SemaRef, Expr *From, QualType ToType, |
| 5175 | UnresolvedSetImpl &ViableConversions, |
| 5176 | OverloadCandidateSet &CandidateSet) { |
| 5177 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5178 | DeclAccessPair FoundDecl = ViableConversions[I]; |
| 5179 | NamedDecl *D = FoundDecl.getDecl(); |
| 5180 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 5181 | if (isa<UsingShadowDecl>(D)) |
| 5182 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 5183 | |
| 5184 | CXXConversionDecl *Conv; |
| 5185 | FunctionTemplateDecl *ConvTemplate; |
| 5186 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 5187 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 5188 | else |
| 5189 | Conv = cast<CXXConversionDecl>(D); |
| 5190 | |
| 5191 | if (ConvTemplate) |
| 5192 | SemaRef.AddTemplateConversionCandidate( |
| 5193 | ConvTemplate, FoundDecl, ActingContext, From, ToType, CandidateSet); |
| 5194 | else |
| 5195 | SemaRef.AddConversionCandidate(Conv, FoundDecl, ActingContext, From, |
| 5196 | ToType, CandidateSet); |
| 5197 | } |
| 5198 | } |
| 5199 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5200 | /// \brief Attempt to convert the given expression to a type which is accepted |
| 5201 | /// by the given converter. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5202 | /// |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5203 | /// This routine will attempt to convert an expression of class type to a |
| 5204 | /// type accepted by the specified converter. In C++11 and before, the class |
| 5205 | /// must have a single non-explicit conversion function converting to a matching |
| 5206 | /// type. In C++1y, there can be multiple such conversion functions, but only |
| 5207 | /// one target type. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5208 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5209 | /// \param Loc The source location of the construct that requires the |
| 5210 | /// conversion. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5211 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 5212 | /// \param From The expression we're converting from. |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5213 | /// |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5214 | /// \param Converter Used to control and diagnose the conversion process. |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5215 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5216 | /// \returns The expression, converted to an integral or enumeration type if |
| 5217 | /// successful. |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5218 | ExprResult Sema::PerformContextualImplicitConversion( |
| 5219 | SourceLocation Loc, Expr *From, ContextualImplicitConverter &Converter) { |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5220 | // We can't perform any more checking for type-dependent expressions. |
| 5221 | if (From->isTypeDependent()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5222 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5223 | |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5224 | // Process placeholders immediately. |
| 5225 | if (From->hasPlaceholderType()) { |
| 5226 | ExprResult result = CheckPlaceholderExpr(From); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5227 | if (result.isInvalid()) |
| 5228 | return result; |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5229 | From = result.take(); |
| 5230 | } |
| 5231 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5232 | // If the expression already has a matching type, we're golden. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5233 | QualType T = From->getType(); |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5234 | if (Converter.match(T)) |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5235 | return DefaultLvalueConversion(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5236 | |
| 5237 | // FIXME: Check for missing '()' if T is a function type? |
| 5238 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5239 | // We can only perform contextual implicit conversions on objects of class |
| 5240 | // type. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5241 | const RecordType *RecordTy = T->getAs<RecordType>(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5242 | if (!RecordTy || !getLangOpts().CPlusPlus) { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5243 | if (!Converter.Suppress) |
| 5244 | Converter.diagnoseNoMatch(*this, Loc, T) << From->getSourceRange(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5245 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5246 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5247 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5248 | // We must have a complete class type. |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 5249 | struct TypeDiagnoserPartialDiag : TypeDiagnoser { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5250 | ContextualImplicitConverter &Converter; |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5251 | Expr *From; |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5252 | |
| 5253 | TypeDiagnoserPartialDiag(ContextualImplicitConverter &Converter, Expr *From) |
| 5254 | : TypeDiagnoser(Converter.Suppress), Converter(Converter), From(From) {} |
| 5255 | |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5256 | virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5257 | Converter.diagnoseIncomplete(S, Loc, T) << From->getSourceRange(); |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5258 | } |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5259 | } IncompleteDiagnoser(Converter, From); |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5260 | |
| 5261 | if (RequireCompleteType(Loc, T, IncompleteDiagnoser)) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5262 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5263 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5264 | // Look for a conversion to an integral or enumeration type. |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5265 | UnresolvedSet<4> |
| 5266 | ViableConversions; // These are *potentially* viable in C++1y. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5267 | UnresolvedSet<4> ExplicitConversions; |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 5268 | std::pair<CXXRecordDecl::conversion_iterator, |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5269 | CXXRecordDecl::conversion_iterator> Conversions = |
| 5270 | cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5271 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5272 | bool HadMultipleCandidates = |
| 5273 | (std::distance(Conversions.first, Conversions.second) > 1); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5274 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5275 | // To check that there is only one target type, in C++1y: |
| 5276 | QualType ToType; |
| 5277 | bool HasUniqueTargetType = true; |
| 5278 | |
| 5279 | // Collect explicit or viable (potentially in C++1y) conversions. |
| 5280 | for (CXXRecordDecl::conversion_iterator I = Conversions.first, |
| 5281 | E = Conversions.second; |
| 5282 | I != E; ++I) { |
| 5283 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5284 | CXXConversionDecl *Conversion; |
| 5285 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 5286 | if (ConvTemplate) { |
| 5287 | if (getLangOpts().CPlusPlus1y) |
| 5288 | Conversion = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 5289 | else |
| 5290 | continue; // C++11 does not consider conversion operator templates(?). |
| 5291 | } else |
| 5292 | Conversion = cast<CXXConversionDecl>(D); |
| 5293 | |
| 5294 | assert((!ConvTemplate || getLangOpts().CPlusPlus1y) && |
| 5295 | "Conversion operator templates are considered potentially " |
| 5296 | "viable in C++1y"); |
| 5297 | |
| 5298 | QualType CurToType = Conversion->getConversionType().getNonReferenceType(); |
| 5299 | if (Converter.match(CurToType) || ConvTemplate) { |
| 5300 | |
| 5301 | if (Conversion->isExplicit()) { |
| 5302 | // FIXME: For C++1y, do we need this restriction? |
| 5303 | // cf. diagnoseNoViableConversion() |
| 5304 | if (!ConvTemplate) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5305 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5306 | } else { |
| 5307 | if (!ConvTemplate && getLangOpts().CPlusPlus1y) { |
| 5308 | if (ToType.isNull()) |
| 5309 | ToType = CurToType.getUnqualifiedType(); |
| 5310 | else if (HasUniqueTargetType && |
| 5311 | (CurToType.getUnqualifiedType() != ToType)) |
| 5312 | HasUniqueTargetType = false; |
| 5313 | } |
| 5314 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5315 | } |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5316 | } |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5317 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5318 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5319 | if (getLangOpts().CPlusPlus1y) { |
| 5320 | // C++1y [conv]p6: |
| 5321 | // ... An expression e of class type E appearing in such a context |
| 5322 | // is said to be contextually implicitly converted to a specified |
| 5323 | // type T and is well-formed if and only if e can be implicitly |
| 5324 | // converted to a type T that is determined as follows: E is searched |
Larisse Voufo | 7acc5a6 | 2013-06-10 08:25:58 +0000 | [diff] [blame] | 5325 | // for conversion functions whose return type is cv T or reference to |
| 5326 | // cv T such that T is allowed by the context. There shall be |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5327 | // exactly one such T. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5328 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5329 | // If no unique T is found: |
| 5330 | if (ToType.isNull()) { |
| 5331 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5332 | HadMultipleCandidates, |
| 5333 | ExplicitConversions)) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5334 | return ExprError(); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5335 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5336 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5337 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5338 | // If more than one unique Ts are found: |
| 5339 | if (!HasUniqueTargetType) |
| 5340 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5341 | ViableConversions); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5342 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5343 | // If one unique T is found: |
| 5344 | // First, build a candidate set from the previously recorded |
| 5345 | // potentially viable conversions. |
| 5346 | OverloadCandidateSet CandidateSet(Loc); |
| 5347 | collectViableConversionCandidates(*this, From, ToType, ViableConversions, |
| 5348 | CandidateSet); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5349 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5350 | // Then, perform overload resolution over the candidate set. |
| 5351 | OverloadCandidateSet::iterator Best; |
| 5352 | switch (CandidateSet.BestViableFunction(*this, Loc, Best)) { |
| 5353 | case OR_Success: { |
| 5354 | // Apply this conversion. |
| 5355 | DeclAccessPair Found = |
| 5356 | DeclAccessPair::make(Best->Function, Best->FoundDecl.getAccess()); |
| 5357 | if (recordConversion(*this, Loc, From, Converter, T, |
| 5358 | HadMultipleCandidates, Found)) |
| 5359 | return ExprError(); |
| 5360 | break; |
| 5361 | } |
| 5362 | case OR_Ambiguous: |
| 5363 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5364 | ViableConversions); |
| 5365 | case OR_No_Viable_Function: |
| 5366 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5367 | HadMultipleCandidates, |
| 5368 | ExplicitConversions)) |
| 5369 | return ExprError(); |
| 5370 | // fall through 'OR_Deleted' case. |
| 5371 | case OR_Deleted: |
| 5372 | // We'll complain below about a non-integral condition type. |
| 5373 | break; |
| 5374 | } |
| 5375 | } else { |
| 5376 | switch (ViableConversions.size()) { |
| 5377 | case 0: { |
| 5378 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5379 | HadMultipleCandidates, |
| 5380 | ExplicitConversions)) |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5381 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5382 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5383 | // We'll complain below about a non-integral condition type. |
| 5384 | break; |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5385 | } |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5386 | case 1: { |
| 5387 | // Apply this conversion. |
| 5388 | DeclAccessPair Found = ViableConversions[0]; |
| 5389 | if (recordConversion(*this, Loc, From, Converter, T, |
| 5390 | HadMultipleCandidates, Found)) |
| 5391 | return ExprError(); |
| 5392 | break; |
| 5393 | } |
| 5394 | default: |
| 5395 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5396 | ViableConversions); |
| 5397 | } |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5398 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5399 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5400 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5401 | } |
| 5402 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5403 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 5404 | /// candidate functions, using the given function call arguments. If |
| 5405 | /// @p SuppressUserConversions, then don't allow user-defined |
| 5406 | /// conversions via constructors or conversion operators. |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5407 | /// |
James Dennett | 699c904 | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 5408 | /// \param PartialOverloading true if we are performing "partial" overloading |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5409 | /// based on an incomplete set of function arguments. This feature is used by |
| 5410 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5411 | void |
| 5412 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5413 | DeclAccessPair FoundDecl, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5414 | ArrayRef<Expr *> Args, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 5415 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 5416 | bool SuppressUserConversions, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5417 | bool PartialOverloading, |
| 5418 | bool AllowExplicit) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5419 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5420 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5421 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5422 | assert(!Function->getDescribedFunctionTemplate() && |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5423 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5424 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5425 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5426 | if (!isa<CXXConstructorDecl>(Method)) { |
| 5427 | // If we get here, it's because we're calling a member function |
| 5428 | // that is named without a member access expression (e.g., |
| 5429 | // "this->f") that was either written explicitly or created |
| 5430 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5431 | // function, e.g., X::f(). We use an empty type for the implied |
| 5432 | // object argument (C++ [over.call.func]p3), and the acting context |
| 5433 | // is irrelevant. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5434 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5435 | QualType(), Expr::Classification::makeSimpleLValue(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5436 | Args, CandidateSet, SuppressUserConversions); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5437 | return; |
| 5438 | } |
| 5439 | // We treat a constructor like a non-member function, since its object |
| 5440 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5441 | } |
| 5442 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 5443 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5444 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5445 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5446 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5447 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5448 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5449 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 5450 | // C++ [class.copy]p3: |
| 5451 | // A member function template is never instantiated to perform the copy |
| 5452 | // of a class object to an object of its class type. |
| 5453 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5454 | if (Args.size() == 1 && |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 5455 | Constructor->isSpecializationCopyingObject() && |
Douglas Gregor | 1211606 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 5456 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 5457 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5458 | return; |
| 5459 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5460 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5461 | // Add this candidate |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5462 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5463 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5464 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5465 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5466 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5467 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5468 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5469 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5470 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5471 | |
| 5472 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5473 | // parameters is viable only if it has an ellipsis in its parameter |
| 5474 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5475 | if ((Args.size() + (PartialOverloading && Args.size())) > NumArgsInProto && |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 5476 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5477 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5478 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5479 | return; |
| 5480 | } |
| 5481 | |
| 5482 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5483 | // is viable only if the (m+1)st parameter has a default argument |
| 5484 | // (8.3.6). For the purposes of overload resolution, the |
| 5485 | // parameter list is truncated on the right, so that there are |
| 5486 | // exactly m parameters. |
| 5487 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5488 | if (Args.size() < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5489 | // Not enough arguments. |
| 5490 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5491 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5492 | return; |
| 5493 | } |
| 5494 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5495 | // (CUDA B.1): Check for invalid calls between targets. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5496 | if (getLangOpts().CUDA) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5497 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
| 5498 | if (CheckCUDATarget(Caller, Function)) { |
| 5499 | Candidate.Viable = false; |
| 5500 | Candidate.FailureKind = ovl_fail_bad_target; |
| 5501 | return; |
| 5502 | } |
| 5503 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5504 | // Determine the implicit conversion sequences for each of the |
| 5505 | // arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5506 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5507 | if (ArgIdx < NumArgsInProto) { |
| 5508 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5509 | // exist for each argument an implicit conversion sequence |
| 5510 | // (13.3.3.1) that converts that argument to the corresponding |
| 5511 | // parameter of F. |
| 5512 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5513 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5514 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5515 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5516 | /*InOverloadResolution=*/true, |
| 5517 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5518 | getLangOpts().ObjCAutoRefCount, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5519 | AllowExplicit); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5520 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 5521 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5522 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5523 | break; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5524 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5525 | } else { |
| 5526 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5527 | // argument for which there is no corresponding parameter is |
| 5528 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5529 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5530 | } |
| 5531 | } |
| 5532 | } |
| 5533 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5534 | /// \brief Add all of the function declarations in the given function set to |
| 5535 | /// the overload canddiate set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5536 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5537 | ArrayRef<Expr *> Args, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5538 | OverloadCandidateSet& CandidateSet, |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5539 | bool SuppressUserConversions, |
| 5540 | TemplateArgumentListInfo *ExplicitTemplateArgs) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5541 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5542 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 5543 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5544 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5545 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5546 | cast<CXXMethodDecl>(FD)->getParent(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5547 | Args[0]->getType(), Args[0]->Classify(Context), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5548 | Args.slice(1), CandidateSet, |
| 5549 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5550 | else |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5551 | AddOverloadCandidate(FD, F.getPair(), Args, CandidateSet, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5552 | SuppressUserConversions); |
| 5553 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5554 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5555 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 5556 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5557 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5558 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5559 | ExplicitTemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5560 | Args[0]->getType(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5561 | Args[0]->Classify(Context), Args.slice(1), |
| 5562 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5563 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5564 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5565 | ExplicitTemplateArgs, Args, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5566 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5567 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5568 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5569 | } |
| 5570 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5571 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 5572 | /// method) as a method candidate to the given overload set. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5573 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5574 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5575 | Expr::Classification ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5576 | ArrayRef<Expr *> Args, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5577 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5578 | bool SuppressUserConversions) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5579 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5580 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5581 | |
| 5582 | if (isa<UsingShadowDecl>(Decl)) |
| 5583 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5584 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5585 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 5586 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 5587 | "Expected a member function template"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5588 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 5589 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5590 | ObjectType, ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5591 | Args, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5592 | SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5593 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5594 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5595 | ObjectType, ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5596 | Args, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5597 | CandidateSet, SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5598 | } |
| 5599 | } |
| 5600 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5601 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 5602 | /// of candidate functions, using the given function call arguments |
| 5603 | /// and the object argument (@c Object). For example, in a call |
| 5604 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 5605 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 5606 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5607 | /// operators. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5608 | void |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5609 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5610 | CXXRecordDecl *ActingContext, QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5611 | Expr::Classification ObjectClassification, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5612 | ArrayRef<Expr *> Args, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5613 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5614 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5615 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5616 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5617 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5618 | assert(!isa<CXXConstructorDecl>(Method) && |
| 5619 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5620 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5621 | if (!CandidateSet.isNewCandidate(Method)) |
| 5622 | return; |
| 5623 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5624 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5625 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5626 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5627 | // Add this candidate |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5628 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5629 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5630 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5631 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5632 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5633 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5634 | |
| 5635 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5636 | |
| 5637 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5638 | // parameters is viable only if it has an ellipsis in its parameter |
| 5639 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5640 | if (Args.size() > NumArgsInProto && !Proto->isVariadic()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5641 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5642 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5643 | return; |
| 5644 | } |
| 5645 | |
| 5646 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5647 | // is viable only if the (m+1)st parameter has a default argument |
| 5648 | // (8.3.6). For the purposes of overload resolution, the |
| 5649 | // parameter list is truncated on the right, so that there are |
| 5650 | // exactly m parameters. |
| 5651 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5652 | if (Args.size() < MinRequiredArgs) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5653 | // Not enough arguments. |
| 5654 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5655 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5656 | return; |
| 5657 | } |
| 5658 | |
| 5659 | Candidate.Viable = true; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5660 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5661 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5662 | // The implicit object argument is ignored. |
| 5663 | Candidate.IgnoreObjectArgument = true; |
| 5664 | else { |
| 5665 | // Determine the implicit conversion sequence for the object |
| 5666 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5667 | Candidate.Conversions[0] |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5668 | = TryObjectArgumentInitialization(*this, ObjectType, ObjectClassification, |
| 5669 | Method, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5670 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5671 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5672 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5673 | return; |
| 5674 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5675 | } |
| 5676 | |
| 5677 | // Determine the implicit conversion sequences for each of the |
| 5678 | // arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5679 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5680 | if (ArgIdx < NumArgsInProto) { |
| 5681 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5682 | // exist for each argument an implicit conversion sequence |
| 5683 | // (13.3.3.1) that converts that argument to the corresponding |
| 5684 | // parameter of F. |
| 5685 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5686 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5687 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5688 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5689 | /*InOverloadResolution=*/true, |
| 5690 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5691 | getLangOpts().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5692 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5693 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5694 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5695 | break; |
| 5696 | } |
| 5697 | } else { |
| 5698 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5699 | // argument for which there is no corresponding parameter is |
| 5700 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5701 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5702 | } |
| 5703 | } |
| 5704 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5705 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5706 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 5707 | /// set, using template argument deduction to produce an appropriate member |
| 5708 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5709 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5710 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5711 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5712 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5713 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5714 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5715 | Expr::Classification ObjectClassification, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5716 | ArrayRef<Expr *> Args, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5717 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5718 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5719 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 5720 | return; |
| 5721 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5722 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5723 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5724 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5725 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5726 | // candidate functions in the usual way.113) A given name can refer to one |
| 5727 | // or more function templates and also to a set of overloaded non-template |
| 5728 | // functions. In such a case, the candidate functions generated from each |
| 5729 | // function template are combined with the set of non-template candidate |
| 5730 | // functions. |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5731 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5732 | FunctionDecl *Specialization = 0; |
| 5733 | if (TemplateDeductionResult Result |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5734 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, Args, |
| 5735 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5736 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5737 | Candidate.FoundDecl = FoundDecl; |
| 5738 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 5739 | Candidate.Viable = false; |
| 5740 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 5741 | Candidate.IsSurrogate = false; |
| 5742 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5743 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5744 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5745 | Info); |
| 5746 | return; |
| 5747 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5748 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5749 | // Add the function template specialization produced by template argument |
| 5750 | // deduction as a candidate. |
| 5751 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5752 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5753 | "Specialization is not a member function?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5754 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5755 | ActingContext, ObjectType, ObjectClassification, Args, |
| 5756 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5757 | } |
| 5758 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5759 | /// \brief Add a C++ function template specialization as a candidate |
| 5760 | /// in the candidate set, using template argument deduction to produce |
| 5761 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5762 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5763 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5764 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5765 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5766 | ArrayRef<Expr *> Args, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5767 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5768 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5769 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 5770 | return; |
| 5771 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5772 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5773 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5774 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5775 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5776 | // candidate functions in the usual way.113) A given name can refer to one |
| 5777 | // or more function templates and also to a set of overloaded non-template |
| 5778 | // functions. In such a case, the candidate functions generated from each |
| 5779 | // function template are combined with the set of non-template candidate |
| 5780 | // functions. |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5781 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5782 | FunctionDecl *Specialization = 0; |
| 5783 | if (TemplateDeductionResult Result |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5784 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, Args, |
| 5785 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5786 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5787 | Candidate.FoundDecl = FoundDecl; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5788 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 5789 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5790 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5791 | Candidate.IsSurrogate = false; |
| 5792 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5793 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5794 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5795 | Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5796 | return; |
| 5797 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5798 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5799 | // Add the function template specialization produced by template argument |
| 5800 | // deduction as a candidate. |
| 5801 | assert(Specialization && "Missing function template specialization?"); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5802 | AddOverloadCandidate(Specialization, FoundDecl, Args, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5803 | SuppressUserConversions); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5804 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5805 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5806 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5807 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5808 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5809 | /// and ToType is the type that we're eventually trying to convert to |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5810 | /// (which may or may not be the same type as the type that the |
| 5811 | /// conversion function produces). |
| 5812 | void |
| 5813 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5814 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5815 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5816 | Expr *From, QualType ToType, |
| 5817 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5818 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 5819 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5820 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5821 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 5822 | return; |
| 5823 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 5824 | // If the conversion function has an undeduced return type, trigger its |
| 5825 | // deduction now. |
| 5826 | if (getLangOpts().CPlusPlus1y && ConvType->isUndeducedType()) { |
| 5827 | if (DeduceReturnType(Conversion, From->getExprLoc())) |
| 5828 | return; |
| 5829 | ConvType = Conversion->getConversionType().getNonReferenceType(); |
| 5830 | } |
| 5831 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5832 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5833 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5834 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5835 | // Add this candidate |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5836 | OverloadCandidate &Candidate = CandidateSet.addCandidate(1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5837 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5838 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5839 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5840 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5841 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5842 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 5843 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5844 | Candidate.Viable = true; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5845 | Candidate.ExplicitCallArguments = 1; |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5846 | |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 5847 | // C++ [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5848 | // For conversion functions, the function is considered to be a member of |
| 5849 | // the class of the implicit implied object argument for the purpose of |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 5850 | // defining the type of the implicit object parameter. |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5851 | // |
| 5852 | // Determine the implicit conversion sequence for the implicit |
| 5853 | // object parameter. |
| 5854 | QualType ImplicitParamType = From->getType(); |
| 5855 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
| 5856 | ImplicitParamType = FromPtrType->getPointeeType(); |
| 5857 | CXXRecordDecl *ConversionContext |
| 5858 | = cast<CXXRecordDecl>(ImplicitParamType->getAs<RecordType>()->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5859 | |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5860 | Candidate.Conversions[0] |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5861 | = TryObjectArgumentInitialization(*this, From->getType(), |
| 5862 | From->Classify(Context), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5863 | Conversion, ConversionContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5864 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5865 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5866 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5867 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5868 | return; |
| 5869 | } |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5870 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5871 | // 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] | 5872 | // derived to base as such conversions are given Conversion Rank. They only |
| 5873 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 5874 | QualType FromCanon |
| 5875 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 5876 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 5877 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 5878 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5879 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 5880 | return; |
| 5881 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5882 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5883 | // To determine what the conversion from the result of calling the |
| 5884 | // conversion function to the type we're eventually trying to |
| 5885 | // convert to (ToType), we need to synthesize a call to the |
| 5886 | // conversion function and attempt copy initialization from it. This |
| 5887 | // makes sure that we get the right semantics with respect to |
| 5888 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 5889 | // call on the stack and we don't need its arguments to be |
| 5890 | // well-formed. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5891 | DeclRefExpr ConversionRef(Conversion, false, Conversion->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5892 | VK_LValue, From->getLocStart()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5893 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 5894 | Context.getPointerType(Conversion->getType()), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5895 | CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5896 | &ConversionRef, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5897 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5898 | QualType ConversionType = Conversion->getConversionType(); |
| 5899 | if (RequireCompleteType(From->getLocStart(), ConversionType, 0)) { |
Douglas Gregor | 7d14d38 | 2010-11-13 19:36:57 +0000 | [diff] [blame] | 5900 | Candidate.Viable = false; |
| 5901 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 5902 | return; |
| 5903 | } |
| 5904 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5905 | ExprValueKind VK = Expr::getValueKindForType(ConversionType); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5906 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5907 | // 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] | 5908 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 5909 | // allocator). |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5910 | QualType CallResultType = ConversionType.getNonLValueExprType(Context); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 5911 | CallExpr Call(Context, &ConversionFn, None, CallResultType, VK, |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 5912 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5913 | ImplicitConversionSequence ICS = |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5914 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 5915 | /*SuppressUserConversions=*/true, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5916 | /*InOverloadResolution=*/false, |
| 5917 | /*AllowObjCWritebackConversion=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5918 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5919 | switch (ICS.getKind()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5920 | case ImplicitConversionSequence::StandardConversion: |
| 5921 | Candidate.FinalConversion = ICS.Standard; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5922 | |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5923 | // C++ [over.ics.user]p3: |
| 5924 | // If the user-defined conversion is specified by a specialization of a |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5925 | // conversion function template, the second standard conversion sequence |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5926 | // shall have exact match rank. |
| 5927 | if (Conversion->getPrimaryTemplate() && |
| 5928 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 5929 | Candidate.Viable = false; |
| 5930 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 5931 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5932 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 5933 | // C++0x [dcl.init.ref]p5: |
| 5934 | // In the second case, if the reference is an rvalue reference and |
| 5935 | // the second standard conversion sequence of the user-defined |
| 5936 | // conversion sequence includes an lvalue-to-rvalue conversion, the |
| 5937 | // program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5938 | if (ToType->isRValueReferenceType() && |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 5939 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 5940 | Candidate.Viable = false; |
| 5941 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 5942 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5943 | break; |
| 5944 | |
| 5945 | case ImplicitConversionSequence::BadConversion: |
| 5946 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5947 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5948 | break; |
| 5949 | |
| 5950 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5951 | llvm_unreachable( |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5952 | "Can only end up with a standard conversion sequence or failure"); |
| 5953 | } |
| 5954 | } |
| 5955 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5956 | /// \brief Adds a conversion function template specialization |
| 5957 | /// candidate to the overload set, using template argument deduction |
| 5958 | /// to deduce the template arguments of the conversion function |
| 5959 | /// template from the type that we are converting to (C++ |
| 5960 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5961 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5962 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5963 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5964 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5965 | Expr *From, QualType ToType, |
| 5966 | OverloadCandidateSet &CandidateSet) { |
| 5967 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 5968 | "Only conversion function templates permitted here"); |
| 5969 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5970 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 5971 | return; |
| 5972 | |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5973 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5974 | CXXConversionDecl *Specialization = 0; |
| 5975 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5976 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5977 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5978 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5979 | Candidate.FoundDecl = FoundDecl; |
| 5980 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 5981 | Candidate.Viable = false; |
| 5982 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 5983 | Candidate.IsSurrogate = false; |
| 5984 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5985 | Candidate.ExplicitCallArguments = 1; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5986 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5987 | Info); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5988 | return; |
| 5989 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5990 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5991 | // Add the conversion function template specialization produced by |
| 5992 | // template argument deduction as a candidate. |
| 5993 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5994 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5995 | CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5996 | } |
| 5997 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5998 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 5999 | /// converts the given @c Object to a function pointer via the |
| 6000 | /// conversion function @c Conversion, and then attempts to call it |
| 6001 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 6002 | /// the type of function that we'll eventually be calling. |
| 6003 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6004 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6005 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6006 | const FunctionProtoType *Proto, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6007 | Expr *Object, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6008 | ArrayRef<Expr *> Args, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6009 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6010 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 6011 | return; |
| 6012 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6013 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6014 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6015 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6016 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6017 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6018 | Candidate.Function = 0; |
| 6019 | Candidate.Surrogate = Conversion; |
| 6020 | Candidate.Viable = true; |
| 6021 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6022 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6023 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6024 | |
| 6025 | // Determine the implicit conversion sequence for the implicit |
| 6026 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6027 | ImplicitConversionSequence ObjectInit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6028 | = TryObjectArgumentInitialization(*this, Object->getType(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6029 | Object->Classify(Context), |
| 6030 | Conversion, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6031 | if (ObjectInit.isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6032 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6033 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6034 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6035 | return; |
| 6036 | } |
| 6037 | |
| 6038 | // The first conversion is actually a user-defined conversion whose |
| 6039 | // first conversion is ObjectInit's standard conversion (which is |
| 6040 | // effectively a reference binding). Record it as such. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6041 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6042 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 6043 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6044 | Candidate.Conversions[0].UserDefined.HadMultipleCandidates = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6045 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 6046 | Candidate.Conversions[0].UserDefined.FoundConversionFunction = FoundDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6047 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6048 | = Candidate.Conversions[0].UserDefined.Before; |
| 6049 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 6050 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6051 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6052 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 6053 | |
| 6054 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 6055 | // parameters is viable only if it has an ellipsis in its parameter |
| 6056 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6057 | if (Args.size() > NumArgsInProto && !Proto->isVariadic()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6058 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6059 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6060 | return; |
| 6061 | } |
| 6062 | |
| 6063 | // Function types don't have any default arguments, so just check if |
| 6064 | // we have enough arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6065 | if (Args.size() < NumArgsInProto) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6066 | // Not enough arguments. |
| 6067 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6068 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6069 | return; |
| 6070 | } |
| 6071 | |
| 6072 | // Determine the implicit conversion sequences for each of the |
| 6073 | // arguments. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6074 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6075 | if (ArgIdx < NumArgsInProto) { |
| 6076 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 6077 | // exist for each argument an implicit conversion sequence |
| 6078 | // (13.3.3.1) that converts that argument to the corresponding |
| 6079 | // parameter of F. |
| 6080 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6081 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6082 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6083 | /*SuppressUserConversions=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6084 | /*InOverloadResolution=*/false, |
| 6085 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6086 | getLangOpts().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6087 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6088 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6089 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6090 | break; |
| 6091 | } |
| 6092 | } else { |
| 6093 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 6094 | // argument for which there is no corresponding parameter is |
| 6095 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6096 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6097 | } |
| 6098 | } |
| 6099 | } |
| 6100 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6101 | /// \brief Add overload candidates for overloaded operators that are |
| 6102 | /// member functions. |
| 6103 | /// |
| 6104 | /// Add the overloaded operator candidates that are member functions |
| 6105 | /// for the operator Op that was used in an operator expression such |
| 6106 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 6107 | /// CandidateSet will store the added overload candidates. (C++ |
| 6108 | /// [over.match.oper]). |
| 6109 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 6110 | SourceLocation OpLoc, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6111 | ArrayRef<Expr *> Args, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6112 | OverloadCandidateSet& CandidateSet, |
| 6113 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6114 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 6115 | |
| 6116 | // C++ [over.match.oper]p3: |
| 6117 | // For a unary operator @ with an operand of a type whose |
| 6118 | // cv-unqualified version is T1, and for a binary operator @ with |
| 6119 | // a left operand of a type whose cv-unqualified version is T1 and |
| 6120 | // a right operand of a type whose cv-unqualified version is T2, |
| 6121 | // three sets of candidate functions, designated member |
| 6122 | // candidates, non-member candidates and built-in candidates, are |
| 6123 | // constructed as follows: |
| 6124 | QualType T1 = Args[0]->getType(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6125 | |
Richard Smith | e410be9 | 2013-04-20 12:41:22 +0000 | [diff] [blame] | 6126 | // -- If T1 is a complete class type or a class currently being |
| 6127 | // defined, the set of member candidates is the result of the |
| 6128 | // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, |
| 6129 | // the set of member candidates is empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6130 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Richard Smith | e410be9 | 2013-04-20 12:41:22 +0000 | [diff] [blame] | 6131 | // Complete the type if it can be completed. |
| 6132 | RequireCompleteType(OpLoc, T1, 0); |
| 6133 | // If the type is neither complete nor being defined, bail out now. |
| 6134 | if (!T1Rec->getDecl()->getDefinition()) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 6135 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6136 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6137 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 6138 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 6139 | Operators.suppressDiagnostics(); |
| 6140 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6141 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 6142 | OperEnd = Operators.end(); |
| 6143 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6144 | ++Oper) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6145 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 6146 | Args[0]->Classify(Context), |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6147 | Args.slice(1), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6148 | CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6149 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6150 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6151 | } |
| 6152 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6153 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 6154 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 6155 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6156 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 6157 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6158 | /// operator. NumContextualBoolArguments is the number of arguments |
| 6159 | /// (at the beginning of the argument list) that will be contextually |
| 6160 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6161 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6162 | ArrayRef<Expr *> Args, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6163 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6164 | bool IsAssignmentOperator, |
| 6165 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6166 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6167 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6168 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6169 | // Add this candidate |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6170 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6171 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6172 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 6173 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6174 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6175 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6176 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6177 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 6178 | |
| 6179 | // Determine the implicit conversion sequences for each of the |
| 6180 | // arguments. |
| 6181 | Candidate.Viable = true; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6182 | Candidate.ExplicitCallArguments = Args.size(); |
| 6183 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6184 | // C++ [over.match.oper]p4: |
| 6185 | // For the built-in assignment operators, conversions of the |
| 6186 | // left operand are restricted as follows: |
| 6187 | // -- no temporaries are introduced to hold the left operand, and |
| 6188 | // -- no user-defined conversions are applied to the left |
| 6189 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6190 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6191 | // |
| 6192 | // We block these conversions by turning off user-defined |
| 6193 | // conversions, since that is the only way that initialization of |
| 6194 | // a reference to a non-class type can occur from something that |
| 6195 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6196 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6197 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6198 | "Contextual conversion to bool requires bool type"); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6199 | Candidate.Conversions[ArgIdx] |
| 6200 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6201 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6202 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6203 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6204 | ArgIdx == 0 && IsAssignmentOperator, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6205 | /*InOverloadResolution=*/false, |
| 6206 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6207 | getLangOpts().ObjCAutoRefCount); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6208 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6209 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6210 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6211 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6212 | break; |
| 6213 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6214 | } |
| 6215 | } |
| 6216 | |
Craig Topper | fe09f3f | 2013-07-01 06:29:40 +0000 | [diff] [blame] | 6217 | namespace { |
| 6218 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6219 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 6220 | /// candidate operator functions for built-in operators (C++ |
| 6221 | /// [over.built]). The types are separated into pointer types and |
| 6222 | /// enumeration types. |
| 6223 | class BuiltinCandidateTypeSet { |
| 6224 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6225 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6226 | |
| 6227 | /// PointerTypes - The set of pointer types that will be used in the |
| 6228 | /// built-in candidates. |
| 6229 | TypeSet PointerTypes; |
| 6230 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6231 | /// MemberPointerTypes - The set of member pointer types that will be |
| 6232 | /// used in the built-in candidates. |
| 6233 | TypeSet MemberPointerTypes; |
| 6234 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6235 | /// EnumerationTypes - The set of enumeration types that will be |
| 6236 | /// used in the built-in candidates. |
| 6237 | TypeSet EnumerationTypes; |
| 6238 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6239 | /// \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] | 6240 | /// candidates. |
| 6241 | TypeSet VectorTypes; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6242 | |
| 6243 | /// \brief A flag indicating non-record types are viable candidates |
| 6244 | bool HasNonRecordTypes; |
| 6245 | |
| 6246 | /// \brief A flag indicating whether either arithmetic or enumeration types |
| 6247 | /// were present in the candidate set. |
| 6248 | bool HasArithmeticOrEnumeralTypes; |
| 6249 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6250 | /// \brief A flag indicating whether the nullptr type was present in the |
| 6251 | /// candidate set. |
| 6252 | bool HasNullPtrType; |
| 6253 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 6254 | /// Sema - The semantic analysis instance where we are building the |
| 6255 | /// candidate type set. |
| 6256 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6257 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6258 | /// Context - The AST context in which we will build the type sets. |
| 6259 | ASTContext &Context; |
| 6260 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6261 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6262 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6263 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6264 | |
| 6265 | public: |
| 6266 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6267 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6268 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6269 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6270 | : HasNonRecordTypes(false), |
| 6271 | HasArithmeticOrEnumeralTypes(false), |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6272 | HasNullPtrType(false), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6273 | SemaRef(SemaRef), |
| 6274 | Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6275 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6276 | void AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6277 | SourceLocation Loc, |
| 6278 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6279 | bool AllowExplicitConversions, |
| 6280 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6281 | |
| 6282 | /// pointer_begin - First pointer type found; |
| 6283 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 6284 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6285 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6286 | iterator pointer_end() { return PointerTypes.end(); } |
| 6287 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6288 | /// member_pointer_begin - First member pointer type found; |
| 6289 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 6290 | |
| 6291 | /// member_pointer_end - Past the last member pointer type found; |
| 6292 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 6293 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6294 | /// enumeration_begin - First enumeration type found; |
| 6295 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 6296 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6297 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6298 | iterator enumeration_end() { return EnumerationTypes.end(); } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6299 | |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6300 | iterator vector_begin() { return VectorTypes.begin(); } |
| 6301 | iterator vector_end() { return VectorTypes.end(); } |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6302 | |
| 6303 | bool hasNonRecordTypes() { return HasNonRecordTypes; } |
| 6304 | bool hasArithmeticOrEnumeralTypes() { return HasArithmeticOrEnumeralTypes; } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6305 | bool hasNullPtrType() const { return HasNullPtrType; } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6306 | }; |
| 6307 | |
Craig Topper | fe09f3f | 2013-07-01 06:29:40 +0000 | [diff] [blame] | 6308 | } // end anonymous namespace |
| 6309 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6310 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6311 | /// the set of pointer types along with any more-qualified variants of |
| 6312 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6313 | /// will add "int const *", "int const volatile *", "int const |
| 6314 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6315 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6316 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6317 | /// |
| 6318 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6319 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6320 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6321 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6322 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6323 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6324 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6325 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6326 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6327 | QualType PointeeTy; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6328 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6329 | bool buildObjCPtr = false; |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6330 | if (!PointerTy) { |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6331 | const ObjCObjectPointerType *PTy = Ty->castAs<ObjCObjectPointerType>(); |
| 6332 | PointeeTy = PTy->getPointeeType(); |
| 6333 | buildObjCPtr = true; |
| 6334 | } else { |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6335 | PointeeTy = PointerTy->getPointeeType(); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6336 | } |
| 6337 | |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6338 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6339 | // (the qualifier would sink to the element type), and for another, the |
| 6340 | // only overload situation where it matters is subscript or pointer +- int, |
| 6341 | // and those shouldn't have qualifier variants anyway. |
| 6342 | if (PointeeTy->isArrayType()) |
| 6343 | return true; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6344 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6345 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6346 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 6347 | bool hasRestrict = VisibleQuals.hasRestrict(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6348 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6349 | // Iterate through all strict supersets of BaseCVR. |
| 6350 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6351 | if ((CVR | BaseCVR) != CVR) continue; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6352 | // Skip over volatile if no volatile found anywhere in the types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6353 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6354 | |
| 6355 | // Skip over restrict if no restrict found anywhere in the types, or if |
| 6356 | // the type cannot be restrict-qualified. |
| 6357 | if ((CVR & Qualifiers::Restrict) && |
| 6358 | (!hasRestrict || |
| 6359 | (!(PointeeTy->isAnyPointerType() || PointeeTy->isReferenceType())))) |
| 6360 | continue; |
| 6361 | |
| 6362 | // Build qualified pointee type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6363 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6364 | |
| 6365 | // Build qualified pointer type. |
| 6366 | QualType QPointerTy; |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6367 | if (!buildObjCPtr) |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6368 | QPointerTy = Context.getPointerType(QPointeeTy); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6369 | else |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6370 | QPointerTy = Context.getObjCObjectPointerType(QPointeeTy); |
| 6371 | |
| 6372 | // Insert qualified pointer type. |
| 6373 | PointerTypes.insert(QPointerTy); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6374 | } |
| 6375 | |
| 6376 | return true; |
| 6377 | } |
| 6378 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6379 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 6380 | /// to the set of pointer types along with any more-qualified variants of |
| 6381 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6382 | /// will add "int const *", "int const volatile *", "int const |
| 6383 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6384 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6385 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6386 | /// |
| 6387 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6388 | bool |
| 6389 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 6390 | QualType Ty) { |
| 6391 | // Insert this type. |
| 6392 | if (!MemberPointerTypes.insert(Ty)) |
| 6393 | return false; |
| 6394 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6395 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 6396 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6397 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6398 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6399 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6400 | // (the qualifier would sink to the element type), and for another, the |
| 6401 | // only overload situation where it matters is subscript or pointer +- int, |
| 6402 | // and those shouldn't have qualifier variants anyway. |
| 6403 | if (PointeeTy->isArrayType()) |
| 6404 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6405 | const Type *ClassTy = PointerTy->getClass(); |
| 6406 | |
| 6407 | // Iterate through all strict supersets of the pointee type's CVR |
| 6408 | // qualifiers. |
| 6409 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 6410 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6411 | if ((CVR | BaseCVR) != CVR) continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6412 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6413 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6414 | MemberPointerTypes.insert( |
| 6415 | Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6416 | } |
| 6417 | |
| 6418 | return true; |
| 6419 | } |
| 6420 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6421 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 6422 | /// 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] | 6423 | /// primarily interested in pointer types and enumeration types. We also |
| 6424 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6425 | /// AllowUserConversions is true if we should look at the conversion |
| 6426 | /// functions of a class type, and AllowExplicitConversions if we |
| 6427 | /// should also include the explicit conversion functions of a class |
| 6428 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6429 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6430 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6431 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6432 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6433 | bool AllowExplicitConversions, |
| 6434 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6435 | // Only deal with canonical types. |
| 6436 | Ty = Context.getCanonicalType(Ty); |
| 6437 | |
| 6438 | // Look through reference types; they aren't part of the type of an |
| 6439 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6440 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6441 | Ty = RefTy->getPointeeType(); |
| 6442 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 6443 | // If we're dealing with an array type, decay to the pointer. |
| 6444 | if (Ty->isArrayType()) |
| 6445 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 6446 | |
| 6447 | // Otherwise, we don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 6448 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6449 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6450 | // Flag if we ever add a non-record type. |
| 6451 | const RecordType *TyRec = Ty->getAs<RecordType>(); |
| 6452 | HasNonRecordTypes = HasNonRecordTypes || !TyRec; |
| 6453 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6454 | // Flag if we encounter an arithmetic type. |
| 6455 | HasArithmeticOrEnumeralTypes = |
| 6456 | HasArithmeticOrEnumeralTypes || Ty->isArithmeticType(); |
| 6457 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6458 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
| 6459 | PointerTypes.insert(Ty); |
| 6460 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6461 | // Insert our type, and its more-qualified variants, into the set |
| 6462 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6463 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6464 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6465 | } else if (Ty->isMemberPointerType()) { |
| 6466 | // Member pointers are far easier, since the pointee can't be converted. |
| 6467 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 6468 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6469 | } else if (Ty->isEnumeralType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6470 | HasArithmeticOrEnumeralTypes = true; |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6471 | EnumerationTypes.insert(Ty); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6472 | } else if (Ty->isVectorType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6473 | // We treat vector types as arithmetic types in many contexts as an |
| 6474 | // extension. |
| 6475 | HasArithmeticOrEnumeralTypes = true; |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6476 | VectorTypes.insert(Ty); |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6477 | } else if (Ty->isNullPtrType()) { |
| 6478 | HasNullPtrType = true; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6479 | } else if (AllowUserConversions && TyRec) { |
| 6480 | // No conversion functions in incomplete types. |
| 6481 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) |
| 6482 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6483 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6484 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6485 | std::pair<CXXRecordDecl::conversion_iterator, |
| 6486 | CXXRecordDecl::conversion_iterator> |
| 6487 | Conversions = ClassDecl->getVisibleConversionFunctions(); |
| 6488 | for (CXXRecordDecl::conversion_iterator |
| 6489 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6490 | NamedDecl *D = I.getDecl(); |
| 6491 | if (isa<UsingShadowDecl>(D)) |
| 6492 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6493 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6494 | // Skip conversion function templates; they don't tell us anything |
| 6495 | // about which builtin types we can convert to. |
| 6496 | if (isa<FunctionTemplateDecl>(D)) |
| 6497 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6498 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6499 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
| 6500 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
| 6501 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
| 6502 | VisibleQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6503 | } |
| 6504 | } |
| 6505 | } |
| 6506 | } |
| 6507 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6508 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 6509 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 6510 | /// given type to the candidate set. |
| 6511 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 6512 | QualType T, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6513 | ArrayRef<Expr *> Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6514 | OverloadCandidateSet &CandidateSet) { |
| 6515 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6516 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6517 | // T& operator=(T&, T) |
| 6518 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 6519 | ParamTypes[1] = T; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6520 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6521 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6522 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6523 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 6524 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6525 | ParamTypes[0] |
| 6526 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6527 | ParamTypes[1] = T; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6528 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6529 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6530 | } |
| 6531 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6532 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 6533 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 6534 | /// 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] | 6535 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 6536 | Qualifiers VRQuals; |
| 6537 | const RecordType *TyRec; |
| 6538 | if (const MemberPointerType *RHSMPType = |
| 6539 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6540 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6541 | else |
| 6542 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 6543 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6544 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6545 | VRQuals.addVolatile(); |
| 6546 | VRQuals.addRestrict(); |
| 6547 | return VRQuals; |
| 6548 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6549 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6550 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 6551 | if (!ClassDecl->hasDefinition()) |
| 6552 | return VRQuals; |
| 6553 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6554 | std::pair<CXXRecordDecl::conversion_iterator, |
| 6555 | CXXRecordDecl::conversion_iterator> |
| 6556 | Conversions = ClassDecl->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6557 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6558 | for (CXXRecordDecl::conversion_iterator |
| 6559 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 6560 | NamedDecl *D = I.getDecl(); |
| 6561 | if (isa<UsingShadowDecl>(D)) |
| 6562 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6563 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6564 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 6565 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 6566 | CanTy = ResTypeRef->getPointeeType(); |
| 6567 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 6568 | // as see them. |
| 6569 | bool done = false; |
| 6570 | while (!done) { |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6571 | if (CanTy.isRestrictQualified()) |
| 6572 | VRQuals.addRestrict(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6573 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 6574 | CanTy = ResTypePtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6575 | else if (const MemberPointerType *ResTypeMPtr = |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6576 | CanTy->getAs<MemberPointerType>()) |
| 6577 | CanTy = ResTypeMPtr->getPointeeType(); |
| 6578 | else |
| 6579 | done = true; |
| 6580 | if (CanTy.isVolatileQualified()) |
| 6581 | VRQuals.addVolatile(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6582 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 6583 | return VRQuals; |
| 6584 | } |
| 6585 | } |
| 6586 | } |
| 6587 | return VRQuals; |
| 6588 | } |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6589 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6590 | namespace { |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6591 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6592 | /// \brief Helper class to manage the addition of builtin operator overload |
| 6593 | /// candidates. It provides shared state and utility methods used throughout |
| 6594 | /// the process, as well as a helper method to add each group of builtin |
| 6595 | /// operator overloads from the standard to a candidate set. |
| 6596 | class BuiltinOperatorOverloadBuilder { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6597 | // Common instance state available to all overload candidate addition methods. |
| 6598 | Sema &S; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6599 | ArrayRef<Expr *> Args; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6600 | Qualifiers VisibleTypeConversionsQuals; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6601 | bool HasArithmeticOrEnumeralCandidateType; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6602 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6603 | OverloadCandidateSet &CandidateSet; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6604 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6605 | // Define some constants used to index and iterate over the arithemetic types |
| 6606 | // provided via the getArithmeticType() method below. |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6607 | // The "promoted arithmetic types" are the arithmetic |
| 6608 | // types are that preserved by promotion (C++ [over.built]p2). |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6609 | static const unsigned FirstIntegralType = 3; |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6610 | static const unsigned LastIntegralType = 20; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6611 | static const unsigned FirstPromotedIntegralType = 3, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6612 | LastPromotedIntegralType = 11; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6613 | static const unsigned FirstPromotedArithmeticType = 0, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6614 | LastPromotedArithmeticType = 11; |
| 6615 | static const unsigned NumArithmeticTypes = 20; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6616 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6617 | /// \brief Get the canonical type for a given arithmetic type index. |
| 6618 | CanQualType getArithmeticType(unsigned index) { |
| 6619 | assert(index < NumArithmeticTypes); |
| 6620 | static CanQualType ASTContext::* const |
| 6621 | ArithmeticTypes[NumArithmeticTypes] = { |
| 6622 | // Start of promoted types. |
| 6623 | &ASTContext::FloatTy, |
| 6624 | &ASTContext::DoubleTy, |
| 6625 | &ASTContext::LongDoubleTy, |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6626 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6627 | // Start of integral types. |
| 6628 | &ASTContext::IntTy, |
| 6629 | &ASTContext::LongTy, |
| 6630 | &ASTContext::LongLongTy, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6631 | &ASTContext::Int128Ty, |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6632 | &ASTContext::UnsignedIntTy, |
| 6633 | &ASTContext::UnsignedLongTy, |
| 6634 | &ASTContext::UnsignedLongLongTy, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6635 | &ASTContext::UnsignedInt128Ty, |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6636 | // End of promoted types. |
| 6637 | |
| 6638 | &ASTContext::BoolTy, |
| 6639 | &ASTContext::CharTy, |
| 6640 | &ASTContext::WCharTy, |
| 6641 | &ASTContext::Char16Ty, |
| 6642 | &ASTContext::Char32Ty, |
| 6643 | &ASTContext::SignedCharTy, |
| 6644 | &ASTContext::ShortTy, |
| 6645 | &ASTContext::UnsignedCharTy, |
| 6646 | &ASTContext::UnsignedShortTy, |
| 6647 | // End of integral types. |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6648 | // FIXME: What about complex? What about half? |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6649 | }; |
| 6650 | return S.Context.*ArithmeticTypes[index]; |
| 6651 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6652 | |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6653 | /// \brief Gets the canonical type resulting from the usual arithemetic |
| 6654 | /// converions for the given arithmetic types. |
| 6655 | CanQualType getUsualArithmeticConversions(unsigned L, unsigned R) { |
| 6656 | // Accelerator table for performing the usual arithmetic conversions. |
| 6657 | // The rules are basically: |
| 6658 | // - if either is floating-point, use the wider floating-point |
| 6659 | // - if same signedness, use the higher rank |
| 6660 | // - if same size, use unsigned of the higher rank |
| 6661 | // - use the larger type |
| 6662 | // These rules, together with the axiom that higher ranks are |
| 6663 | // never smaller, are sufficient to precompute all of these results |
| 6664 | // *except* when dealing with signed types of higher rank. |
| 6665 | // (we could precompute SLL x UI for all known platforms, but it's |
| 6666 | // better not to make any assumptions). |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6667 | // 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] | 6668 | enum PromotedType { |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6669 | Dep=-1, |
| 6670 | Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6671 | }; |
Nuno Lopes | 79e244f | 2012-04-21 14:45:25 +0000 | [diff] [blame] | 6672 | static const PromotedType ConversionsTable[LastPromotedArithmeticType] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6673 | [LastPromotedArithmeticType] = { |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6674 | /* Flt*/ { Flt, Dbl, LDbl, Flt, Flt, Flt, Flt, Flt, Flt, Flt, Flt }, |
| 6675 | /* Dbl*/ { Dbl, Dbl, LDbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl }, |
| 6676 | /*LDbl*/ { LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl }, |
| 6677 | /* SI*/ { Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 }, |
| 6678 | /* SL*/ { Flt, Dbl, LDbl, SL, SL, SLL, S128, Dep, UL, ULL, U128 }, |
| 6679 | /* SLL*/ { Flt, Dbl, LDbl, SLL, SLL, SLL, S128, Dep, Dep, ULL, U128 }, |
| 6680 | /*S128*/ { Flt, Dbl, LDbl, S128, S128, S128, S128, S128, S128, S128, U128 }, |
| 6681 | /* UI*/ { Flt, Dbl, LDbl, UI, Dep, Dep, S128, UI, UL, ULL, U128 }, |
| 6682 | /* UL*/ { Flt, Dbl, LDbl, UL, UL, Dep, S128, UL, UL, ULL, U128 }, |
| 6683 | /* ULL*/ { Flt, Dbl, LDbl, ULL, ULL, ULL, S128, ULL, ULL, ULL, U128 }, |
| 6684 | /*U128*/ { Flt, Dbl, LDbl, U128, U128, U128, U128, U128, U128, U128, U128 }, |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6685 | }; |
| 6686 | |
| 6687 | assert(L < LastPromotedArithmeticType); |
| 6688 | assert(R < LastPromotedArithmeticType); |
| 6689 | int Idx = ConversionsTable[L][R]; |
| 6690 | |
| 6691 | // Fast path: the table gives us a concrete answer. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6692 | if (Idx != Dep) return getArithmeticType(Idx); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6693 | |
| 6694 | // Slow path: we need to compare widths. |
| 6695 | // An invariant is that the signed type has higher rank. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6696 | CanQualType LT = getArithmeticType(L), |
| 6697 | RT = getArithmeticType(R); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6698 | unsigned LW = S.Context.getIntWidth(LT), |
| 6699 | RW = S.Context.getIntWidth(RT); |
| 6700 | |
| 6701 | // If they're different widths, use the signed type. |
| 6702 | if (LW > RW) return LT; |
| 6703 | else if (LW < RW) return RT; |
| 6704 | |
| 6705 | // Otherwise, use the unsigned type of the signed type's rank. |
| 6706 | if (L == SL || R == SL) return S.Context.UnsignedLongTy; |
| 6707 | assert(L == SLL || R == SLL); |
| 6708 | return S.Context.UnsignedLongLongTy; |
| 6709 | } |
| 6710 | |
Chandler Carruth | 3c69dc4 | 2010-12-12 09:22:45 +0000 | [diff] [blame] | 6711 | /// \brief Helper method to factor out the common pattern of adding overloads |
| 6712 | /// for '++' and '--' builtin operators. |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6713 | void addPlusPlusMinusMinusStyleOverloads(QualType CandidateTy, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6714 | bool HasVolatile, |
| 6715 | bool HasRestrict) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6716 | QualType ParamTypes[2] = { |
| 6717 | S.Context.getLValueReferenceType(CandidateTy), |
| 6718 | S.Context.IntTy |
| 6719 | }; |
| 6720 | |
| 6721 | // Non-volatile version. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6722 | if (Args.size() == 1) |
| 6723 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6724 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6725 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6726 | |
| 6727 | // Use a heuristic to reduce number of builtin candidates in the set: |
| 6728 | // add volatile version only if there are conversions to a volatile type. |
| 6729 | if (HasVolatile) { |
| 6730 | ParamTypes[0] = |
| 6731 | S.Context.getLValueReferenceType( |
| 6732 | S.Context.getVolatileType(CandidateTy)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6733 | if (Args.size() == 1) |
| 6734 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6735 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6736 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6737 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6738 | |
| 6739 | // Add restrict version only if there are conversions to a restrict type |
| 6740 | // and our candidate type is a non-restrict-qualified pointer. |
| 6741 | if (HasRestrict && CandidateTy->isAnyPointerType() && |
| 6742 | !CandidateTy.isRestrictQualified()) { |
| 6743 | ParamTypes[0] |
| 6744 | = S.Context.getLValueReferenceType( |
| 6745 | S.Context.getCVRQualifiedType(CandidateTy, Qualifiers::Restrict)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6746 | if (Args.size() == 1) |
| 6747 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6748 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6749 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6750 | |
| 6751 | if (HasVolatile) { |
| 6752 | ParamTypes[0] |
| 6753 | = S.Context.getLValueReferenceType( |
| 6754 | S.Context.getCVRQualifiedType(CandidateTy, |
| 6755 | (Qualifiers::Volatile | |
| 6756 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6757 | if (Args.size() == 1) |
| 6758 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6759 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6760 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6761 | } |
| 6762 | } |
| 6763 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6764 | } |
| 6765 | |
| 6766 | public: |
| 6767 | BuiltinOperatorOverloadBuilder( |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6768 | Sema &S, ArrayRef<Expr *> Args, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6769 | Qualifiers VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6770 | bool HasArithmeticOrEnumeralCandidateType, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6771 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6772 | OverloadCandidateSet &CandidateSet) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6773 | : S(S), Args(Args), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6774 | VisibleTypeConversionsQuals(VisibleTypeConversionsQuals), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6775 | HasArithmeticOrEnumeralCandidateType( |
| 6776 | HasArithmeticOrEnumeralCandidateType), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6777 | CandidateTypes(CandidateTypes), |
| 6778 | CandidateSet(CandidateSet) { |
| 6779 | // Validate some of our static helper constants in debug builds. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6780 | assert(getArithmeticType(FirstPromotedIntegralType) == S.Context.IntTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6781 | "Invalid first promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6782 | assert(getArithmeticType(LastPromotedIntegralType - 1) |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6783 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6784 | "Invalid last promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6785 | assert(getArithmeticType(FirstPromotedArithmeticType) |
| 6786 | == S.Context.FloatTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6787 | "Invalid first promoted arithmetic type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6788 | assert(getArithmeticType(LastPromotedArithmeticType - 1) |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6789 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6790 | "Invalid last promoted arithmetic type"); |
| 6791 | } |
| 6792 | |
| 6793 | // C++ [over.built]p3: |
| 6794 | // |
| 6795 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 6796 | // is either volatile or empty, there exist candidate operator |
| 6797 | // functions of the form |
| 6798 | // |
| 6799 | // VQ T& operator++(VQ T&); |
| 6800 | // T operator++(VQ T&, int); |
| 6801 | // |
| 6802 | // C++ [over.built]p4: |
| 6803 | // |
| 6804 | // For every pair (T, VQ), where T is an arithmetic type other |
| 6805 | // than bool, and VQ is either volatile or empty, there exist |
| 6806 | // candidate operator functions of the form |
| 6807 | // |
| 6808 | // VQ T& operator--(VQ T&); |
| 6809 | // T operator--(VQ T&, int); |
| 6810 | void addPlusPlusMinusMinusArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6811 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6812 | return; |
| 6813 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6814 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 6815 | Arith < NumArithmeticTypes; ++Arith) { |
| 6816 | addPlusPlusMinusMinusStyleOverloads( |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6817 | getArithmeticType(Arith), |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6818 | VisibleTypeConversionsQuals.hasVolatile(), |
| 6819 | VisibleTypeConversionsQuals.hasRestrict()); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6820 | } |
| 6821 | } |
| 6822 | |
| 6823 | // C++ [over.built]p5: |
| 6824 | // |
| 6825 | // For every pair (T, VQ), where T is a cv-qualified or |
| 6826 | // cv-unqualified object type, and VQ is either volatile or |
| 6827 | // empty, there exist candidate operator functions of the form |
| 6828 | // |
| 6829 | // T*VQ& operator++(T*VQ&); |
| 6830 | // T*VQ& operator--(T*VQ&); |
| 6831 | // T* operator++(T*VQ&, int); |
| 6832 | // T* operator--(T*VQ&, int); |
| 6833 | void addPlusPlusMinusMinusPointerOverloads() { |
| 6834 | for (BuiltinCandidateTypeSet::iterator |
| 6835 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6836 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6837 | Ptr != PtrEnd; ++Ptr) { |
| 6838 | // Skip pointer types that aren't pointers to object types. |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6839 | if (!(*Ptr)->getPointeeType()->isObjectType()) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6840 | continue; |
| 6841 | |
| 6842 | addPlusPlusMinusMinusStyleOverloads(*Ptr, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6843 | (!(*Ptr).isVolatileQualified() && |
| 6844 | VisibleTypeConversionsQuals.hasVolatile()), |
| 6845 | (!(*Ptr).isRestrictQualified() && |
| 6846 | VisibleTypeConversionsQuals.hasRestrict())); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6847 | } |
| 6848 | } |
| 6849 | |
| 6850 | // C++ [over.built]p6: |
| 6851 | // For every cv-qualified or cv-unqualified object type T, there |
| 6852 | // exist candidate operator functions of the form |
| 6853 | // |
| 6854 | // T& operator*(T*); |
| 6855 | // |
| 6856 | // C++ [over.built]p7: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6857 | // 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] | 6858 | // ref-qualifier, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6859 | // T& operator*(T*); |
| 6860 | void addUnaryStarPointerOverloads() { |
| 6861 | for (BuiltinCandidateTypeSet::iterator |
| 6862 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6863 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6864 | Ptr != PtrEnd; ++Ptr) { |
| 6865 | QualType ParamTy = *Ptr; |
| 6866 | QualType PointeeTy = ParamTy->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6867 | if (!PointeeTy->isObjectType() && !PointeeTy->isFunctionType()) |
| 6868 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6869 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6870 | if (const FunctionProtoType *Proto =PointeeTy->getAs<FunctionProtoType>()) |
| 6871 | if (Proto->getTypeQuals() || Proto->getRefQualifier()) |
| 6872 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6873 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6874 | S.AddBuiltinCandidate(S.Context.getLValueReferenceType(PointeeTy), |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6875 | &ParamTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6876 | } |
| 6877 | } |
| 6878 | |
| 6879 | // C++ [over.built]p9: |
| 6880 | // For every promoted arithmetic type T, there exist candidate |
| 6881 | // operator functions of the form |
| 6882 | // |
| 6883 | // T operator+(T); |
| 6884 | // T operator-(T); |
| 6885 | void addUnaryPlusOrMinusArithmeticOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6886 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6887 | return; |
| 6888 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6889 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 6890 | Arith < LastPromotedArithmeticType; ++Arith) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6891 | QualType ArithTy = getArithmeticType(Arith); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6892 | S.AddBuiltinCandidate(ArithTy, &ArithTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6893 | } |
| 6894 | |
| 6895 | // Extension: We also add these operators for vector types. |
| 6896 | for (BuiltinCandidateTypeSet::iterator |
| 6897 | Vec = CandidateTypes[0].vector_begin(), |
| 6898 | VecEnd = CandidateTypes[0].vector_end(); |
| 6899 | Vec != VecEnd; ++Vec) { |
| 6900 | QualType VecTy = *Vec; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6901 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6902 | } |
| 6903 | } |
| 6904 | |
| 6905 | // C++ [over.built]p8: |
| 6906 | // For every type T, there exist candidate operator functions of |
| 6907 | // the form |
| 6908 | // |
| 6909 | // T* operator+(T*); |
| 6910 | void addUnaryPlusPointerOverloads() { |
| 6911 | for (BuiltinCandidateTypeSet::iterator |
| 6912 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6913 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6914 | Ptr != PtrEnd; ++Ptr) { |
| 6915 | QualType ParamTy = *Ptr; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6916 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6917 | } |
| 6918 | } |
| 6919 | |
| 6920 | // C++ [over.built]p10: |
| 6921 | // For every promoted integral type T, there exist candidate |
| 6922 | // operator functions of the form |
| 6923 | // |
| 6924 | // T operator~(T); |
| 6925 | void addUnaryTildePromotedIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6926 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6927 | return; |
| 6928 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6929 | for (unsigned Int = FirstPromotedIntegralType; |
| 6930 | Int < LastPromotedIntegralType; ++Int) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6931 | QualType IntTy = getArithmeticType(Int); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6932 | S.AddBuiltinCandidate(IntTy, &IntTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6933 | } |
| 6934 | |
| 6935 | // Extension: We also add this operator for vector types. |
| 6936 | for (BuiltinCandidateTypeSet::iterator |
| 6937 | Vec = CandidateTypes[0].vector_begin(), |
| 6938 | VecEnd = CandidateTypes[0].vector_end(); |
| 6939 | Vec != VecEnd; ++Vec) { |
| 6940 | QualType VecTy = *Vec; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6941 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6942 | } |
| 6943 | } |
| 6944 | |
| 6945 | // C++ [over.match.oper]p16: |
| 6946 | // For every pointer to member type T, there exist candidate operator |
| 6947 | // functions of the form |
| 6948 | // |
| 6949 | // bool operator==(T,T); |
| 6950 | // bool operator!=(T,T); |
| 6951 | void addEqualEqualOrNotEqualMemberPointerOverloads() { |
| 6952 | /// Set of (canonical) types that we've already handled. |
| 6953 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6954 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6955 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6956 | for (BuiltinCandidateTypeSet::iterator |
| 6957 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 6958 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 6959 | MemPtr != MemPtrEnd; |
| 6960 | ++MemPtr) { |
| 6961 | // Don't add the same builtin candidate twice. |
| 6962 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 6963 | continue; |
| 6964 | |
| 6965 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6966 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6967 | } |
| 6968 | } |
| 6969 | } |
| 6970 | |
| 6971 | // C++ [over.built]p15: |
| 6972 | // |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6973 | // For every T, where T is an enumeration type, a pointer type, or |
| 6974 | // std::nullptr_t, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6975 | // |
| 6976 | // bool operator<(T, T); |
| 6977 | // bool operator>(T, T); |
| 6978 | // bool operator<=(T, T); |
| 6979 | // bool operator>=(T, T); |
| 6980 | // bool operator==(T, T); |
| 6981 | // bool operator!=(T, T); |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6982 | void addRelationalPointerOrEnumeralOverloads() { |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 6983 | // C++ [over.match.oper]p3: |
| 6984 | // [...]the built-in candidates include all of the candidate operator |
| 6985 | // functions defined in 13.6 that, compared to the given operator, [...] |
| 6986 | // do not have the same parameter-type-list as any non-template non-member |
| 6987 | // candidate. |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6988 | // |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 6989 | // Note that in practice, this only affects enumeration types because there |
| 6990 | // aren't any built-in candidates of record type, and a user-defined operator |
| 6991 | // must have an operand of record or enumeration type. Also, the only other |
| 6992 | // overloaded operator with enumeration arguments, operator=, |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6993 | // cannot be overloaded for enumeration types, so this is the only place |
| 6994 | // where we must suppress candidates like this. |
| 6995 | llvm::DenseSet<std::pair<CanQualType, CanQualType> > |
| 6996 | UserDefinedBinaryOperators; |
| 6997 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6998 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6999 | if (CandidateTypes[ArgIdx].enumeration_begin() != |
| 7000 | CandidateTypes[ArgIdx].enumeration_end()) { |
| 7001 | for (OverloadCandidateSet::iterator C = CandidateSet.begin(), |
| 7002 | CEnd = CandidateSet.end(); |
| 7003 | C != CEnd; ++C) { |
| 7004 | if (!C->Viable || !C->Function || C->Function->getNumParams() != 2) |
| 7005 | continue; |
| 7006 | |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 7007 | if (C->Function->isFunctionTemplateSpecialization()) |
| 7008 | continue; |
| 7009 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7010 | QualType FirstParamType = |
| 7011 | C->Function->getParamDecl(0)->getType().getUnqualifiedType(); |
| 7012 | QualType SecondParamType = |
| 7013 | C->Function->getParamDecl(1)->getType().getUnqualifiedType(); |
| 7014 | |
| 7015 | // Skip if either parameter isn't of enumeral type. |
| 7016 | if (!FirstParamType->isEnumeralType() || |
| 7017 | !SecondParamType->isEnumeralType()) |
| 7018 | continue; |
| 7019 | |
| 7020 | // Add this operator to the set of known user-defined operators. |
| 7021 | UserDefinedBinaryOperators.insert( |
| 7022 | std::make_pair(S.Context.getCanonicalType(FirstParamType), |
| 7023 | S.Context.getCanonicalType(SecondParamType))); |
| 7024 | } |
| 7025 | } |
| 7026 | } |
| 7027 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7028 | /// Set of (canonical) types that we've already handled. |
| 7029 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7030 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7031 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7032 | for (BuiltinCandidateTypeSet::iterator |
| 7033 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 7034 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 7035 | Ptr != PtrEnd; ++Ptr) { |
| 7036 | // Don't add the same builtin candidate twice. |
| 7037 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7038 | continue; |
| 7039 | |
| 7040 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7041 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7042 | } |
| 7043 | for (BuiltinCandidateTypeSet::iterator |
| 7044 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7045 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7046 | Enum != EnumEnd; ++Enum) { |
| 7047 | CanQualType CanonType = S.Context.getCanonicalType(*Enum); |
| 7048 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7049 | // Don't add the same builtin candidate twice, or if a user defined |
| 7050 | // candidate exists. |
| 7051 | if (!AddedTypes.insert(CanonType) || |
| 7052 | UserDefinedBinaryOperators.count(std::make_pair(CanonType, |
| 7053 | CanonType))) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7054 | continue; |
| 7055 | |
| 7056 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7057 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7058 | } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7059 | |
| 7060 | if (CandidateTypes[ArgIdx].hasNullPtrType()) { |
| 7061 | CanQualType NullPtrTy = S.Context.getCanonicalType(S.Context.NullPtrTy); |
| 7062 | if (AddedTypes.insert(NullPtrTy) && |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7063 | !UserDefinedBinaryOperators.count(std::make_pair(NullPtrTy, |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7064 | NullPtrTy))) { |
| 7065 | QualType ParamTypes[2] = { NullPtrTy, NullPtrTy }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7066 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7067 | CandidateSet); |
| 7068 | } |
| 7069 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7070 | } |
| 7071 | } |
| 7072 | |
| 7073 | // C++ [over.built]p13: |
| 7074 | // |
| 7075 | // For every cv-qualified or cv-unqualified object type T |
| 7076 | // there exist candidate operator functions of the form |
| 7077 | // |
| 7078 | // T* operator+(T*, ptrdiff_t); |
| 7079 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 7080 | // T* operator-(T*, ptrdiff_t); |
| 7081 | // T* operator+(ptrdiff_t, T*); |
| 7082 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 7083 | // |
| 7084 | // C++ [over.built]p14: |
| 7085 | // |
| 7086 | // For every T, where T is a pointer to object type, there |
| 7087 | // exist candidate operator functions of the form |
| 7088 | // |
| 7089 | // ptrdiff_t operator-(T, T); |
| 7090 | void addBinaryPlusOrMinusPointerOverloads(OverloadedOperatorKind Op) { |
| 7091 | /// Set of (canonical) types that we've already handled. |
| 7092 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7093 | |
| 7094 | for (int Arg = 0; Arg < 2; ++Arg) { |
| 7095 | QualType AsymetricParamTypes[2] = { |
| 7096 | S.Context.getPointerDiffType(), |
| 7097 | S.Context.getPointerDiffType(), |
| 7098 | }; |
| 7099 | for (BuiltinCandidateTypeSet::iterator |
| 7100 | Ptr = CandidateTypes[Arg].pointer_begin(), |
| 7101 | PtrEnd = CandidateTypes[Arg].pointer_end(); |
| 7102 | Ptr != PtrEnd; ++Ptr) { |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7103 | QualType PointeeTy = (*Ptr)->getPointeeType(); |
| 7104 | if (!PointeeTy->isObjectType()) |
| 7105 | continue; |
| 7106 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7107 | AsymetricParamTypes[Arg] = *Ptr; |
| 7108 | if (Arg == 0 || Op == OO_Plus) { |
| 7109 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 7110 | // T* operator+(ptrdiff_t, T*); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7111 | S.AddBuiltinCandidate(*Ptr, AsymetricParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7112 | } |
| 7113 | if (Op == OO_Minus) { |
| 7114 | // ptrdiff_t operator-(T, T); |
| 7115 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7116 | continue; |
| 7117 | |
| 7118 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 7119 | S.AddBuiltinCandidate(S.Context.getPointerDiffType(), ParamTypes, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7120 | Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7121 | } |
| 7122 | } |
| 7123 | } |
| 7124 | } |
| 7125 | |
| 7126 | // C++ [over.built]p12: |
| 7127 | // |
| 7128 | // For every pair of promoted arithmetic types L and R, there |
| 7129 | // exist candidate operator functions of the form |
| 7130 | // |
| 7131 | // LR operator*(L, R); |
| 7132 | // LR operator/(L, R); |
| 7133 | // LR operator+(L, R); |
| 7134 | // LR operator-(L, R); |
| 7135 | // bool operator<(L, R); |
| 7136 | // bool operator>(L, R); |
| 7137 | // bool operator<=(L, R); |
| 7138 | // bool operator>=(L, R); |
| 7139 | // bool operator==(L, R); |
| 7140 | // bool operator!=(L, R); |
| 7141 | // |
| 7142 | // where LR is the result of the usual arithmetic conversions |
| 7143 | // between types L and R. |
| 7144 | // |
| 7145 | // C++ [over.built]p24: |
| 7146 | // |
| 7147 | // For every pair of promoted arithmetic types L and R, there exist |
| 7148 | // candidate operator functions of the form |
| 7149 | // |
| 7150 | // LR operator?(bool, L, R); |
| 7151 | // |
| 7152 | // where LR is the result of the usual arithmetic conversions |
| 7153 | // between types L and R. |
| 7154 | // Our candidates ignore the first parameter. |
| 7155 | void addGenericBinaryArithmeticOverloads(bool isComparison) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7156 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7157 | return; |
| 7158 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7159 | for (unsigned Left = FirstPromotedArithmeticType; |
| 7160 | Left < LastPromotedArithmeticType; ++Left) { |
| 7161 | for (unsigned Right = FirstPromotedArithmeticType; |
| 7162 | Right < LastPromotedArithmeticType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7163 | QualType LandR[2] = { getArithmeticType(Left), |
| 7164 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7165 | QualType Result = |
| 7166 | isComparison ? S.Context.BoolTy |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7167 | : getUsualArithmeticConversions(Left, Right); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7168 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7169 | } |
| 7170 | } |
| 7171 | |
| 7172 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 7173 | // conditional operator for vector types. |
| 7174 | for (BuiltinCandidateTypeSet::iterator |
| 7175 | Vec1 = CandidateTypes[0].vector_begin(), |
| 7176 | Vec1End = CandidateTypes[0].vector_end(); |
| 7177 | Vec1 != Vec1End; ++Vec1) { |
| 7178 | for (BuiltinCandidateTypeSet::iterator |
| 7179 | Vec2 = CandidateTypes[1].vector_begin(), |
| 7180 | Vec2End = CandidateTypes[1].vector_end(); |
| 7181 | Vec2 != Vec2End; ++Vec2) { |
| 7182 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 7183 | QualType Result = S.Context.BoolTy; |
| 7184 | if (!isComparison) { |
| 7185 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 7186 | Result = *Vec1; |
| 7187 | else |
| 7188 | Result = *Vec2; |
| 7189 | } |
| 7190 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7191 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7192 | } |
| 7193 | } |
| 7194 | } |
| 7195 | |
| 7196 | // C++ [over.built]p17: |
| 7197 | // |
| 7198 | // For every pair of promoted integral types L and R, there |
| 7199 | // exist candidate operator functions of the form |
| 7200 | // |
| 7201 | // LR operator%(L, R); |
| 7202 | // LR operator&(L, R); |
| 7203 | // LR operator^(L, R); |
| 7204 | // LR operator|(L, R); |
| 7205 | // L operator<<(L, R); |
| 7206 | // L operator>>(L, R); |
| 7207 | // |
| 7208 | // where LR is the result of the usual arithmetic conversions |
| 7209 | // between types L and R. |
| 7210 | void addBinaryBitwiseArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7211 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7212 | return; |
| 7213 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7214 | for (unsigned Left = FirstPromotedIntegralType; |
| 7215 | Left < LastPromotedIntegralType; ++Left) { |
| 7216 | for (unsigned Right = FirstPromotedIntegralType; |
| 7217 | Right < LastPromotedIntegralType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7218 | QualType LandR[2] = { getArithmeticType(Left), |
| 7219 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7220 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 7221 | ? LandR[0] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7222 | : getUsualArithmeticConversions(Left, Right); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7223 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7224 | } |
| 7225 | } |
| 7226 | } |
| 7227 | |
| 7228 | // C++ [over.built]p20: |
| 7229 | // |
| 7230 | // For every pair (T, VQ), where T is an enumeration or |
| 7231 | // pointer to member type and VQ is either volatile or |
| 7232 | // empty, there exist candidate operator functions of the form |
| 7233 | // |
| 7234 | // VQ T& operator=(VQ T&, T); |
| 7235 | void addAssignmentMemberPointerOrEnumeralOverloads() { |
| 7236 | /// Set of (canonical) types that we've already handled. |
| 7237 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7238 | |
| 7239 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7240 | for (BuiltinCandidateTypeSet::iterator |
| 7241 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7242 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7243 | Enum != EnumEnd; ++Enum) { |
| 7244 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 7245 | continue; |
| 7246 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7247 | AddBuiltinAssignmentOperatorCandidates(S, *Enum, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7248 | } |
| 7249 | |
| 7250 | for (BuiltinCandidateTypeSet::iterator |
| 7251 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7252 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7253 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7254 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7255 | continue; |
| 7256 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7257 | AddBuiltinAssignmentOperatorCandidates(S, *MemPtr, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7258 | } |
| 7259 | } |
| 7260 | } |
| 7261 | |
| 7262 | // C++ [over.built]p19: |
| 7263 | // |
| 7264 | // For every pair (T, VQ), where T is any type and VQ is either |
| 7265 | // volatile or empty, there exist candidate operator functions |
| 7266 | // of the form |
| 7267 | // |
| 7268 | // T*VQ& operator=(T*VQ&, T*); |
| 7269 | // |
| 7270 | // C++ [over.built]p21: |
| 7271 | // |
| 7272 | // For every pair (T, VQ), where T is a cv-qualified or |
| 7273 | // cv-unqualified object type and VQ is either volatile or |
| 7274 | // empty, there exist candidate operator functions of the form |
| 7275 | // |
| 7276 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 7277 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 7278 | void addAssignmentPointerOverloads(bool isEqualOp) { |
| 7279 | /// Set of (canonical) types that we've already handled. |
| 7280 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7281 | |
| 7282 | for (BuiltinCandidateTypeSet::iterator |
| 7283 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7284 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7285 | Ptr != PtrEnd; ++Ptr) { |
| 7286 | // If this is operator=, keep track of the builtin candidates we added. |
| 7287 | if (isEqualOp) |
| 7288 | AddedTypes.insert(S.Context.getCanonicalType(*Ptr)); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7289 | else if (!(*Ptr)->getPointeeType()->isObjectType()) |
| 7290 | continue; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7291 | |
| 7292 | // non-volatile version |
| 7293 | QualType ParamTypes[2] = { |
| 7294 | S.Context.getLValueReferenceType(*Ptr), |
| 7295 | isEqualOp ? *Ptr : S.Context.getPointerDiffType(), |
| 7296 | }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7297 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7298 | /*IsAssigmentOperator=*/ isEqualOp); |
| 7299 | |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7300 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7301 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7302 | if (NeedVolatile) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7303 | // volatile version |
| 7304 | ParamTypes[0] = |
| 7305 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7306 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7307 | /*IsAssigmentOperator=*/isEqualOp); |
| 7308 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7309 | |
| 7310 | if (!(*Ptr).isRestrictQualified() && |
| 7311 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7312 | // restrict version |
| 7313 | ParamTypes[0] |
| 7314 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7315 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7316 | /*IsAssigmentOperator=*/isEqualOp); |
| 7317 | |
| 7318 | if (NeedVolatile) { |
| 7319 | // volatile restrict version |
| 7320 | ParamTypes[0] |
| 7321 | = S.Context.getLValueReferenceType( |
| 7322 | S.Context.getCVRQualifiedType(*Ptr, |
| 7323 | (Qualifiers::Volatile | |
| 7324 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7325 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7326 | /*IsAssigmentOperator=*/isEqualOp); |
| 7327 | } |
| 7328 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7329 | } |
| 7330 | |
| 7331 | if (isEqualOp) { |
| 7332 | for (BuiltinCandidateTypeSet::iterator |
| 7333 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7334 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7335 | Ptr != PtrEnd; ++Ptr) { |
| 7336 | // Make sure we don't add the same candidate twice. |
| 7337 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7338 | continue; |
| 7339 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7340 | QualType ParamTypes[2] = { |
| 7341 | S.Context.getLValueReferenceType(*Ptr), |
| 7342 | *Ptr, |
| 7343 | }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7344 | |
| 7345 | // non-volatile version |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7346 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7347 | /*IsAssigmentOperator=*/true); |
| 7348 | |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7349 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7350 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7351 | if (NeedVolatile) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7352 | // volatile version |
| 7353 | ParamTypes[0] = |
| 7354 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7355 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7356 | /*IsAssigmentOperator=*/true); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7357 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7358 | |
| 7359 | if (!(*Ptr).isRestrictQualified() && |
| 7360 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7361 | // restrict version |
| 7362 | ParamTypes[0] |
| 7363 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7364 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7365 | /*IsAssigmentOperator=*/true); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7366 | |
| 7367 | if (NeedVolatile) { |
| 7368 | // volatile restrict version |
| 7369 | ParamTypes[0] |
| 7370 | = S.Context.getLValueReferenceType( |
| 7371 | S.Context.getCVRQualifiedType(*Ptr, |
| 7372 | (Qualifiers::Volatile | |
| 7373 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7374 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7375 | /*IsAssigmentOperator=*/true); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7376 | } |
| 7377 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7378 | } |
| 7379 | } |
| 7380 | } |
| 7381 | |
| 7382 | // C++ [over.built]p18: |
| 7383 | // |
| 7384 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 7385 | // VQ is either volatile or empty, and R is a promoted |
| 7386 | // arithmetic type, there exist candidate operator functions of |
| 7387 | // the form |
| 7388 | // |
| 7389 | // VQ L& operator=(VQ L&, R); |
| 7390 | // VQ L& operator*=(VQ L&, R); |
| 7391 | // VQ L& operator/=(VQ L&, R); |
| 7392 | // VQ L& operator+=(VQ L&, R); |
| 7393 | // VQ L& operator-=(VQ L&, R); |
| 7394 | void addAssignmentArithmeticOverloads(bool isEqualOp) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7395 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7396 | return; |
| 7397 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7398 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 7399 | for (unsigned Right = FirstPromotedArithmeticType; |
| 7400 | Right < LastPromotedArithmeticType; ++Right) { |
| 7401 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7402 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7403 | |
| 7404 | // Add this built-in operator as a candidate (VQ is empty). |
| 7405 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7406 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7407 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7408 | /*IsAssigmentOperator=*/isEqualOp); |
| 7409 | |
| 7410 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7411 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7412 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7413 | S.Context.getVolatileType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7414 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7415 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7416 | /*IsAssigmentOperator=*/isEqualOp); |
| 7417 | } |
| 7418 | } |
| 7419 | } |
| 7420 | |
| 7421 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 7422 | for (BuiltinCandidateTypeSet::iterator |
| 7423 | Vec1 = CandidateTypes[0].vector_begin(), |
| 7424 | Vec1End = CandidateTypes[0].vector_end(); |
| 7425 | Vec1 != Vec1End; ++Vec1) { |
| 7426 | for (BuiltinCandidateTypeSet::iterator |
| 7427 | Vec2 = CandidateTypes[1].vector_begin(), |
| 7428 | Vec2End = CandidateTypes[1].vector_end(); |
| 7429 | Vec2 != Vec2End; ++Vec2) { |
| 7430 | QualType ParamTypes[2]; |
| 7431 | ParamTypes[1] = *Vec2; |
| 7432 | // Add this built-in operator as a candidate (VQ is empty). |
| 7433 | ParamTypes[0] = S.Context.getLValueReferenceType(*Vec1); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7434 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7435 | /*IsAssigmentOperator=*/isEqualOp); |
| 7436 | |
| 7437 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7438 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7439 | ParamTypes[0] = S.Context.getVolatileType(*Vec1); |
| 7440 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
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 | } |
| 7445 | } |
| 7446 | } |
| 7447 | |
| 7448 | // C++ [over.built]p22: |
| 7449 | // |
| 7450 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 7451 | // is either volatile or empty, and R is a promoted integral |
| 7452 | // type, there exist candidate operator functions of the form |
| 7453 | // |
| 7454 | // VQ L& operator%=(VQ L&, R); |
| 7455 | // VQ L& operator<<=(VQ L&, R); |
| 7456 | // VQ L& operator>>=(VQ L&, R); |
| 7457 | // VQ L& operator&=(VQ L&, R); |
| 7458 | // VQ L& operator^=(VQ L&, R); |
| 7459 | // VQ L& operator|=(VQ L&, R); |
| 7460 | void addAssignmentIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7461 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7462 | return; |
| 7463 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7464 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 7465 | for (unsigned Right = FirstPromotedIntegralType; |
| 7466 | Right < LastPromotedIntegralType; ++Right) { |
| 7467 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7468 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7469 | |
| 7470 | // Add this built-in operator as a candidate (VQ is empty). |
| 7471 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7472 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7473 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7474 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7475 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7476 | ParamTypes[0] = getArithmeticType(Left); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7477 | ParamTypes[0] = S.Context.getVolatileType(ParamTypes[0]); |
| 7478 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7479 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7480 | } |
| 7481 | } |
| 7482 | } |
| 7483 | } |
| 7484 | |
| 7485 | // C++ [over.operator]p23: |
| 7486 | // |
| 7487 | // There also exist candidate operator functions of the form |
| 7488 | // |
| 7489 | // bool operator!(bool); |
| 7490 | // bool operator&&(bool, bool); |
| 7491 | // bool operator||(bool, bool); |
| 7492 | void addExclaimOverload() { |
| 7493 | QualType ParamTy = S.Context.BoolTy; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7494 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7495 | /*IsAssignmentOperator=*/false, |
| 7496 | /*NumContextualBoolArguments=*/1); |
| 7497 | } |
| 7498 | void addAmpAmpOrPipePipeOverload() { |
| 7499 | QualType ParamTypes[2] = { S.Context.BoolTy, S.Context.BoolTy }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7500 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7501 | /*IsAssignmentOperator=*/false, |
| 7502 | /*NumContextualBoolArguments=*/2); |
| 7503 | } |
| 7504 | |
| 7505 | // C++ [over.built]p13: |
| 7506 | // |
| 7507 | // For every cv-qualified or cv-unqualified object type T there |
| 7508 | // exist candidate operator functions of the form |
| 7509 | // |
| 7510 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 7511 | // T& operator[](T*, ptrdiff_t); |
| 7512 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 7513 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 7514 | // T& operator[](ptrdiff_t, T*); |
| 7515 | void addSubscriptOverloads() { |
| 7516 | for (BuiltinCandidateTypeSet::iterator |
| 7517 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7518 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7519 | Ptr != PtrEnd; ++Ptr) { |
| 7520 | QualType ParamTypes[2] = { *Ptr, S.Context.getPointerDiffType() }; |
| 7521 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7522 | if (!PointeeType->isObjectType()) |
| 7523 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7524 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7525 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7526 | |
| 7527 | // T& operator[](T*, ptrdiff_t) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7528 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7529 | } |
| 7530 | |
| 7531 | for (BuiltinCandidateTypeSet::iterator |
| 7532 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7533 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7534 | Ptr != PtrEnd; ++Ptr) { |
| 7535 | QualType ParamTypes[2] = { S.Context.getPointerDiffType(), *Ptr }; |
| 7536 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7537 | if (!PointeeType->isObjectType()) |
| 7538 | continue; |
| 7539 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7540 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7541 | |
| 7542 | // T& operator[](ptrdiff_t, T*) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7543 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7544 | } |
| 7545 | } |
| 7546 | |
| 7547 | // C++ [over.built]p11: |
| 7548 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 7549 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 7550 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 7551 | // there exist candidate operator functions of the form |
| 7552 | // |
| 7553 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 7554 | // |
| 7555 | // where CV12 is the union of CV1 and CV2. |
| 7556 | void addArrowStarOverloads() { |
| 7557 | for (BuiltinCandidateTypeSet::iterator |
| 7558 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7559 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7560 | Ptr != PtrEnd; ++Ptr) { |
| 7561 | QualType C1Ty = (*Ptr); |
| 7562 | QualType C1; |
| 7563 | QualifierCollector Q1; |
| 7564 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
| 7565 | if (!isa<RecordType>(C1)) |
| 7566 | continue; |
| 7567 | // heuristic to reduce number of builtin candidates in the set. |
| 7568 | // Add volatile/restrict version only if there are conversions to a |
| 7569 | // volatile/restrict type. |
| 7570 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 7571 | continue; |
| 7572 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 7573 | continue; |
| 7574 | for (BuiltinCandidateTypeSet::iterator |
| 7575 | MemPtr = CandidateTypes[1].member_pointer_begin(), |
| 7576 | MemPtrEnd = CandidateTypes[1].member_pointer_end(); |
| 7577 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7578 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 7579 | QualType C2 = QualType(mptr->getClass(), 0); |
| 7580 | C2 = C2.getUnqualifiedType(); |
| 7581 | if (C1 != C2 && !S.IsDerivedFrom(C1, C2)) |
| 7582 | break; |
| 7583 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 7584 | // build CV12 T& |
| 7585 | QualType T = mptr->getPointeeType(); |
| 7586 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 7587 | T.isVolatileQualified()) |
| 7588 | continue; |
| 7589 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 7590 | T.isRestrictQualified()) |
| 7591 | continue; |
| 7592 | T = Q1.apply(S.Context, T); |
| 7593 | QualType ResultTy = S.Context.getLValueReferenceType(T); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7594 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7595 | } |
| 7596 | } |
| 7597 | } |
| 7598 | |
| 7599 | // Note that we don't consider the first argument, since it has been |
| 7600 | // contextually converted to bool long ago. The candidates below are |
| 7601 | // therefore added as binary. |
| 7602 | // |
| 7603 | // C++ [over.built]p25: |
| 7604 | // For every type T, where T is a pointer, pointer-to-member, or scoped |
| 7605 | // enumeration type, there exist candidate operator functions of the form |
| 7606 | // |
| 7607 | // T operator?(bool, T, T); |
| 7608 | // |
| 7609 | void addConditionalOperatorOverloads() { |
| 7610 | /// Set of (canonical) types that we've already handled. |
| 7611 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7612 | |
| 7613 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7614 | for (BuiltinCandidateTypeSet::iterator |
| 7615 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 7616 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 7617 | Ptr != PtrEnd; ++Ptr) { |
| 7618 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7619 | continue; |
| 7620 | |
| 7621 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7622 | S.AddBuiltinCandidate(*Ptr, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7623 | } |
| 7624 | |
| 7625 | for (BuiltinCandidateTypeSet::iterator |
| 7626 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7627 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7628 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7629 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7630 | continue; |
| 7631 | |
| 7632 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7633 | S.AddBuiltinCandidate(*MemPtr, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7634 | } |
| 7635 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7636 | if (S.getLangOpts().CPlusPlus11) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7637 | for (BuiltinCandidateTypeSet::iterator |
| 7638 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7639 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7640 | Enum != EnumEnd; ++Enum) { |
| 7641 | if (!(*Enum)->getAs<EnumType>()->getDecl()->isScoped()) |
| 7642 | continue; |
| 7643 | |
| 7644 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 7645 | continue; |
| 7646 | |
| 7647 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7648 | S.AddBuiltinCandidate(*Enum, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7649 | } |
| 7650 | } |
| 7651 | } |
| 7652 | } |
| 7653 | }; |
| 7654 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7655 | } // end anonymous namespace |
| 7656 | |
| 7657 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 7658 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 7659 | /// on the operator @p Op and the arguments given. For example, if the |
| 7660 | /// operator is a binary '+', this routine might add "int |
| 7661 | /// operator+(int, int)" to cover integer addition. |
Robert Wilhelm | 834c058 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 7662 | void Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 7663 | SourceLocation OpLoc, |
| 7664 | ArrayRef<Expr *> Args, |
| 7665 | OverloadCandidateSet &CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7666 | // Find all of the types that the arguments can convert to, but only |
| 7667 | // if the operator we're looking at has built-in operator candidates |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7668 | // that make use of these types. Also record whether we encounter non-record |
| 7669 | // candidate types or either arithmetic or enumeral candidate types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7670 | Qualifiers VisibleTypeConversionsQuals; |
| 7671 | VisibleTypeConversionsQuals.addConst(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7672 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 7673 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7674 | |
| 7675 | bool HasNonRecordCandidateType = false; |
| 7676 | bool HasArithmeticOrEnumeralCandidateType = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7677 | SmallVector<BuiltinCandidateTypeSet, 2> CandidateTypes; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7678 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7679 | CandidateTypes.push_back(BuiltinCandidateTypeSet(*this)); |
| 7680 | CandidateTypes[ArgIdx].AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 7681 | OpLoc, |
| 7682 | true, |
| 7683 | (Op == OO_Exclaim || |
| 7684 | Op == OO_AmpAmp || |
| 7685 | Op == OO_PipePipe), |
| 7686 | VisibleTypeConversionsQuals); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7687 | HasNonRecordCandidateType = HasNonRecordCandidateType || |
| 7688 | CandidateTypes[ArgIdx].hasNonRecordTypes(); |
| 7689 | HasArithmeticOrEnumeralCandidateType = |
| 7690 | HasArithmeticOrEnumeralCandidateType || |
| 7691 | CandidateTypes[ArgIdx].hasArithmeticOrEnumeralTypes(); |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7692 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7693 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7694 | // Exit early when no non-record types have been added to the candidate set |
| 7695 | // for any of the arguments to the operator. |
Douglas Gregor | 25aaff9 | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 7696 | // |
| 7697 | // We can't exit early for !, ||, or &&, since there we have always have |
| 7698 | // 'bool' overloads. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7699 | if (!HasNonRecordCandidateType && |
Douglas Gregor | 25aaff9 | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 7700 | !(Op == OO_Exclaim || Op == OO_AmpAmp || Op == OO_PipePipe)) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7701 | return; |
| 7702 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7703 | // Setup an object to manage the common state for building overloads. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7704 | BuiltinOperatorOverloadBuilder OpBuilder(*this, Args, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7705 | VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7706 | HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7707 | CandidateTypes, CandidateSet); |
| 7708 | |
| 7709 | // Dispatch over the operation to add in only those overloads which apply. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7710 | switch (Op) { |
| 7711 | case OO_None: |
| 7712 | case NUM_OVERLOADED_OPERATORS: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7713 | llvm_unreachable("Expected an overloaded operator"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7714 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7715 | case OO_New: |
| 7716 | case OO_Delete: |
| 7717 | case OO_Array_New: |
| 7718 | case OO_Array_Delete: |
| 7719 | case OO_Call: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7720 | llvm_unreachable( |
| 7721 | "Special operators don't use AddBuiltinOperatorCandidates"); |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7722 | |
| 7723 | case OO_Comma: |
| 7724 | case OO_Arrow: |
| 7725 | // C++ [over.match.oper]p3: |
| 7726 | // -- For the operator ',', the unary operator '&', or the |
| 7727 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7728 | break; |
| 7729 | |
| 7730 | case OO_Plus: // '+' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7731 | if (Args.size() == 1) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7732 | OpBuilder.addUnaryPlusPointerOverloads(); |
Chandler Carruth | 32fe0d0 | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 7733 | // Fall through. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7734 | |
| 7735 | case OO_Minus: // '-' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7736 | if (Args.size() == 1) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7737 | OpBuilder.addUnaryPlusOrMinusArithmeticOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7738 | } else { |
| 7739 | OpBuilder.addBinaryPlusOrMinusPointerOverloads(Op); |
| 7740 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7741 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7742 | break; |
| 7743 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7744 | case OO_Star: // '*' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7745 | if (Args.size() == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7746 | OpBuilder.addUnaryStarPointerOverloads(); |
| 7747 | else |
| 7748 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7749 | break; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7750 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7751 | case OO_Slash: |
| 7752 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 7753 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7754 | |
| 7755 | case OO_PlusPlus: |
| 7756 | case OO_MinusMinus: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7757 | OpBuilder.addPlusPlusMinusMinusArithmeticOverloads(Op); |
| 7758 | OpBuilder.addPlusPlusMinusMinusPointerOverloads(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7759 | break; |
| 7760 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 7761 | case OO_EqualEqual: |
| 7762 | case OO_ExclaimEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7763 | OpBuilder.addEqualEqualOrNotEqualMemberPointerOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 7764 | // Fall through. |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 7765 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7766 | case OO_Less: |
| 7767 | case OO_Greater: |
| 7768 | case OO_LessEqual: |
| 7769 | case OO_GreaterEqual: |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7770 | OpBuilder.addRelationalPointerOrEnumeralOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 7771 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/true); |
| 7772 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7773 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7774 | case OO_Percent: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7775 | case OO_Caret: |
| 7776 | case OO_Pipe: |
| 7777 | case OO_LessLess: |
| 7778 | case OO_GreaterGreater: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7779 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7780 | break; |
| 7781 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7782 | case OO_Amp: // '&' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7783 | if (Args.size() == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7784 | // C++ [over.match.oper]p3: |
| 7785 | // -- For the operator ',', the unary operator '&', or the |
| 7786 | // operator '->', the built-in candidates set is empty. |
| 7787 | break; |
| 7788 | |
| 7789 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
| 7790 | break; |
| 7791 | |
| 7792 | case OO_Tilde: |
| 7793 | OpBuilder.addUnaryTildePromotedIntegralOverloads(); |
| 7794 | break; |
| 7795 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7796 | case OO_Equal: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7797 | OpBuilder.addAssignmentMemberPointerOrEnumeralOverloads(); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7798 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7799 | |
| 7800 | case OO_PlusEqual: |
| 7801 | case OO_MinusEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7802 | OpBuilder.addAssignmentPointerOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7803 | // Fall through. |
| 7804 | |
| 7805 | case OO_StarEqual: |
| 7806 | case OO_SlashEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7807 | OpBuilder.addAssignmentArithmeticOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7808 | break; |
| 7809 | |
| 7810 | case OO_PercentEqual: |
| 7811 | case OO_LessLessEqual: |
| 7812 | case OO_GreaterGreaterEqual: |
| 7813 | case OO_AmpEqual: |
| 7814 | case OO_CaretEqual: |
| 7815 | case OO_PipeEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7816 | OpBuilder.addAssignmentIntegralOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7817 | break; |
| 7818 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7819 | case OO_Exclaim: |
| 7820 | OpBuilder.addExclaimOverload(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7821 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7822 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7823 | case OO_AmpAmp: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7824 | case OO_PipePipe: |
| 7825 | OpBuilder.addAmpAmpOrPipePipeOverload(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7826 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7827 | |
| 7828 | case OO_Subscript: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7829 | OpBuilder.addSubscriptOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7830 | break; |
| 7831 | |
| 7832 | case OO_ArrowStar: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7833 | OpBuilder.addArrowStarOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7834 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 7835 | |
| 7836 | case OO_Conditional: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7837 | OpBuilder.addConditionalOperatorOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7838 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7839 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7840 | } |
| 7841 | } |
| 7842 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7843 | /// \brief Add function candidates found via argument-dependent lookup |
| 7844 | /// to the set of overloading candidates. |
| 7845 | /// |
| 7846 | /// This routine performs argument-dependent name lookup based on the |
| 7847 | /// given function name (which may also be an operator name) and adds |
| 7848 | /// all of the overload candidates found by ADL to the overload |
| 7849 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7850 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7851 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
Richard Smith | f5cd5cc | 2012-02-25 06:24:24 +0000 | [diff] [blame] | 7852 | bool Operator, SourceLocation Loc, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 7853 | ArrayRef<Expr *> Args, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 7854 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7855 | OverloadCandidateSet& CandidateSet, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 7856 | bool PartialOverloading) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7857 | ADLResult Fns; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7858 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 7859 | // FIXME: This approach for uniquing ADL results (and removing |
| 7860 | // redundant candidates from the set) relies on pointer-equality, |
| 7861 | // which means we need to key off the canonical decl. However, |
| 7862 | // always going back to the canonical decl might not get us the |
| 7863 | // right set of default arguments. What default arguments are |
| 7864 | // we supposed to consider on ADL candidates, anyway? |
| 7865 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7866 | // FIXME: Pass in the explicit template arguments? |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 7867 | ArgumentDependentLookup(Name, Operator, Loc, Args, Fns); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7868 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7869 | // Erase all of the candidates we already knew about. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7870 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 7871 | CandEnd = CandidateSet.end(); |
| 7872 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7873 | if (Cand->Function) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7874 | Fns.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7875 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7876 | Fns.erase(FunTmpl); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7877 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7878 | |
| 7879 | // For each of the ADL candidates we found, add it to the overload |
| 7880 | // set. |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7881 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7882 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7883 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7884 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7885 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7886 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7887 | AddOverloadCandidate(FD, FoundDecl, Args, CandidateSet, false, |
| 7888 | PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7889 | } else |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7890 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7891 | FoundDecl, ExplicitTemplateArgs, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7892 | Args, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7893 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7894 | } |
| 7895 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7896 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 7897 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7898 | bool |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7899 | isBetterOverloadCandidate(Sema &S, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 7900 | const OverloadCandidate &Cand1, |
| 7901 | const OverloadCandidate &Cand2, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7902 | SourceLocation Loc, |
| 7903 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7904 | // Define viable functions to be better candidates than non-viable |
| 7905 | // functions. |
| 7906 | if (!Cand2.Viable) |
| 7907 | return Cand1.Viable; |
| 7908 | else if (!Cand1.Viable) |
| 7909 | return false; |
| 7910 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7911 | // C++ [over.match.best]p1: |
| 7912 | // |
| 7913 | // -- if F is a static member function, ICS1(F) is defined such |
| 7914 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 7915 | // any function G, and, symmetrically, ICS1(G) is neither |
| 7916 | // better nor worse than ICS1(F). |
| 7917 | unsigned StartArg = 0; |
| 7918 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 7919 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7920 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7921 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7922 | // A viable function F1 is defined to be a better function than another |
| 7923 | // 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] | 7924 | // conversion sequence than ICSi(F2), and then... |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 7925 | unsigned NumArgs = Cand1.NumConversions; |
| 7926 | assert(Cand2.NumConversions == NumArgs && "Overload candidate mismatch"); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7927 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7928 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7929 | switch (CompareImplicitConversionSequences(S, |
| 7930 | Cand1.Conversions[ArgIdx], |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7931 | Cand2.Conversions[ArgIdx])) { |
| 7932 | case ImplicitConversionSequence::Better: |
| 7933 | // Cand1 has a better conversion sequence. |
| 7934 | HasBetterConversion = true; |
| 7935 | break; |
| 7936 | |
| 7937 | case ImplicitConversionSequence::Worse: |
| 7938 | // Cand1 can't be better than Cand2. |
| 7939 | return false; |
| 7940 | |
| 7941 | case ImplicitConversionSequence::Indistinguishable: |
| 7942 | // Do nothing. |
| 7943 | break; |
| 7944 | } |
| 7945 | } |
| 7946 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7947 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7948 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7949 | if (HasBetterConversion) |
| 7950 | return true; |
| 7951 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7952 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7953 | // specialization, or, if not that, |
Douglas Gregor | ccd4713 | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 7954 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7955 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 7956 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7957 | |
| 7958 | // -- F1 and F2 are function template specializations, and the function |
| 7959 | // template for F1 is more specialized than the template for F2 |
| 7960 | // 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] | 7961 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 7962 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7963 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7964 | if (FunctionTemplateDecl *BetterTemplate |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7965 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 7966 | Cand2.Function->getPrimaryTemplate(), |
| 7967 | Loc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7968 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 7969 | : TPOC_Call, |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7970 | Cand1.ExplicitCallArguments)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7971 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7972 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7973 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7974 | // -- the context is an initialization by user-defined conversion |
| 7975 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 7976 | // from the return type of F1 to the destination type (i.e., |
| 7977 | // the type of the entity being initialized) is a better |
| 7978 | // conversion sequence than the standard conversion sequence |
| 7979 | // from the return type of F2 to the destination type. |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7980 | if (UserDefinedConversion && Cand1.Function && Cand2.Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7981 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7982 | isa<CXXConversionDecl>(Cand2.Function)) { |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 7983 | // First check whether we prefer one of the conversion functions over the |
| 7984 | // other. This only distinguishes the results in non-standard, extension |
| 7985 | // cases such as the conversion from a lambda closure type to a function |
| 7986 | // pointer or block. |
| 7987 | ImplicitConversionSequence::CompareKind FuncResult |
| 7988 | = compareConversionFunctions(S, Cand1.Function, Cand2.Function); |
| 7989 | if (FuncResult != ImplicitConversionSequence::Indistinguishable) |
| 7990 | return FuncResult; |
| 7991 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7992 | switch (CompareStandardConversionSequences(S, |
| 7993 | Cand1.FinalConversion, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7994 | Cand2.FinalConversion)) { |
| 7995 | case ImplicitConversionSequence::Better: |
| 7996 | // Cand1 has a better conversion sequence. |
| 7997 | return true; |
| 7998 | |
| 7999 | case ImplicitConversionSequence::Worse: |
| 8000 | // Cand1 can't be better than Cand2. |
| 8001 | return false; |
| 8002 | |
| 8003 | case ImplicitConversionSequence::Indistinguishable: |
| 8004 | // Do nothing |
| 8005 | break; |
| 8006 | } |
| 8007 | } |
| 8008 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8009 | return false; |
| 8010 | } |
| 8011 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8012 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8013 | /// within an overload candidate set. |
| 8014 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 8015 | /// \param Loc The location of the function name (or operator symbol) for |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8016 | /// which overload resolution occurs. |
| 8017 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 8018 | /// \param Best If overload resolution was successful or found a deleted |
| 8019 | /// function, \p Best points to the candidate function found. |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8020 | /// |
| 8021 | /// \returns The result of overload resolution. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8022 | OverloadingResult |
| 8023 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 8024 | iterator &Best, |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8025 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8026 | // Find the best viable function. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8027 | Best = end(); |
| 8028 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
| 8029 | if (Cand->Viable) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8030 | if (Best == end() || isBetterOverloadCandidate(S, *Cand, *Best, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8031 | UserDefinedConversion)) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8032 | Best = Cand; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8033 | } |
| 8034 | |
| 8035 | // If we didn't find any viable functions, abort. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8036 | if (Best == end()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8037 | return OR_No_Viable_Function; |
| 8038 | |
| 8039 | // Make sure that this function is better than every other viable |
| 8040 | // function. If not, we have an ambiguity. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8041 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8042 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8043 | Cand != Best && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8044 | !isBetterOverloadCandidate(S, *Best, *Cand, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8045 | UserDefinedConversion)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8046 | Best = end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8047 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8048 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8049 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8050 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8051 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8052 | if (Best->Function && |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 8053 | (Best->Function->isDeleted() || |
| 8054 | S.isFunctionConsideredUnavailable(Best->Function))) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8055 | return OR_Deleted; |
| 8056 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8057 | return OR_Success; |
| 8058 | } |
| 8059 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8060 | namespace { |
| 8061 | |
| 8062 | enum OverloadCandidateKind { |
| 8063 | oc_function, |
| 8064 | oc_method, |
| 8065 | oc_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8066 | oc_function_template, |
| 8067 | oc_method_template, |
| 8068 | oc_constructor_template, |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8069 | oc_implicit_default_constructor, |
| 8070 | oc_implicit_copy_constructor, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8071 | oc_implicit_move_constructor, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8072 | oc_implicit_copy_assignment, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8073 | oc_implicit_move_assignment, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8074 | oc_implicit_inherited_constructor |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8075 | }; |
| 8076 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8077 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 8078 | FunctionDecl *Fn, |
| 8079 | std::string &Description) { |
| 8080 | bool isTemplate = false; |
| 8081 | |
| 8082 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 8083 | isTemplate = true; |
| 8084 | Description = S.getTemplateArgumentBindingsText( |
| 8085 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 8086 | } |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8087 | |
| 8088 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8089 | if (!Ctor->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8090 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8091 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8092 | if (Ctor->getInheritedConstructor()) |
| 8093 | return oc_implicit_inherited_constructor; |
| 8094 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8095 | if (Ctor->isDefaultConstructor()) |
| 8096 | return oc_implicit_default_constructor; |
| 8097 | |
| 8098 | if (Ctor->isMoveConstructor()) |
| 8099 | return oc_implicit_move_constructor; |
| 8100 | |
| 8101 | assert(Ctor->isCopyConstructor() && |
| 8102 | "unexpected sort of implicit constructor"); |
| 8103 | return oc_implicit_copy_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8104 | } |
| 8105 | |
| 8106 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 8107 | // This actually gets spelled 'candidate function' for now, but |
| 8108 | // it doesn't hurt to split it out. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8109 | if (!Meth->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8110 | return isTemplate ? oc_method_template : oc_method; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8111 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8112 | if (Meth->isMoveAssignmentOperator()) |
| 8113 | return oc_implicit_move_assignment; |
| 8114 | |
Douglas Gregor | ef7d78b | 2012-02-10 08:36:38 +0000 | [diff] [blame] | 8115 | if (Meth->isCopyAssignmentOperator()) |
| 8116 | return oc_implicit_copy_assignment; |
| 8117 | |
| 8118 | assert(isa<CXXConversionDecl>(Meth) && "expected conversion"); |
| 8119 | return oc_method; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8120 | } |
| 8121 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8122 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8123 | } |
| 8124 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8125 | void MaybeEmitInheritedConstructorNote(Sema &S, Decl *Fn) { |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8126 | const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn); |
| 8127 | if (!Ctor) return; |
| 8128 | |
| 8129 | Ctor = Ctor->getInheritedConstructor(); |
| 8130 | if (!Ctor) return; |
| 8131 | |
| 8132 | S.Diag(Ctor->getLocation(), diag::note_ovl_candidate_inherited_constructor); |
| 8133 | } |
| 8134 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8135 | } // end anonymous namespace |
| 8136 | |
| 8137 | // Notes the location of an overload candidate. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8138 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn, QualType DestType) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8139 | std::string FnDesc; |
| 8140 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8141 | PartialDiagnostic PD = PDiag(diag::note_ovl_candidate) |
| 8142 | << (unsigned) K << FnDesc; |
| 8143 | HandleFunctionTypeMismatch(PD, Fn->getType(), DestType); |
| 8144 | Diag(Fn->getLocation(), PD); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8145 | MaybeEmitInheritedConstructorNote(*this, Fn); |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8146 | } |
| 8147 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8148 | //Notes the location of all overload candidates designated through |
| 8149 | // OverloadedExpr |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8150 | void Sema::NoteAllOverloadCandidates(Expr* OverloadedExpr, QualType DestType) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8151 | assert(OverloadedExpr->getType() == Context.OverloadTy); |
| 8152 | |
| 8153 | OverloadExpr::FindResult Ovl = OverloadExpr::find(OverloadedExpr); |
| 8154 | OverloadExpr *OvlExpr = Ovl.Expression; |
| 8155 | |
| 8156 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 8157 | IEnd = OvlExpr->decls_end(); |
| 8158 | I != IEnd; ++I) { |
| 8159 | if (FunctionTemplateDecl *FunTmpl = |
| 8160 | dyn_cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8161 | NoteOverloadCandidate(FunTmpl->getTemplatedDecl(), DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8162 | } else if (FunctionDecl *Fun |
| 8163 | = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8164 | NoteOverloadCandidate(Fun, DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8165 | } |
| 8166 | } |
| 8167 | } |
| 8168 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8169 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 8170 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 8171 | /// target types of the conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8172 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
| 8173 | Sema &S, |
| 8174 | SourceLocation CaretLoc, |
| 8175 | const PartialDiagnostic &PDiag) const { |
| 8176 | S.Diag(CaretLoc, PDiag) |
| 8177 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
Matt Beaumont-Gay | 45a37da | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 8178 | // FIXME: The note limiting machinery is borrowed from |
| 8179 | // OverloadCandidateSet::NoteCandidates; there's an opportunity for |
| 8180 | // refactoring here. |
| 8181 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
| 8182 | unsigned CandsShown = 0; |
| 8183 | AmbiguousConversionSequence::const_iterator I, E; |
| 8184 | for (I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
| 8185 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
| 8186 | break; |
| 8187 | ++CandsShown; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8188 | S.NoteOverloadCandidate(*I); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8189 | } |
Matt Beaumont-Gay | 45a37da | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 8190 | if (I != E) |
| 8191 | S.Diag(SourceLocation(), diag::note_ovl_too_many_candidates) << int(E - I); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8192 | } |
| 8193 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8194 | namespace { |
| 8195 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8196 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 8197 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 8198 | assert(Conv.isBad()); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8199 | assert(Cand->Function && "for now, candidate must be a function"); |
| 8200 | FunctionDecl *Fn = Cand->Function; |
| 8201 | |
| 8202 | // There's a conversion slot for the object argument if this is a |
| 8203 | // non-constructor method. Note that 'I' corresponds the |
| 8204 | // conversion-slot index. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8205 | bool isObjectArgument = false; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8206 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8207 | if (I == 0) |
| 8208 | isObjectArgument = true; |
| 8209 | else |
| 8210 | I--; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8211 | } |
| 8212 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8213 | std::string FnDesc; |
| 8214 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 8215 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8216 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 8217 | QualType FromTy = Conv.Bad.getFromType(); |
| 8218 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8219 | |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8220 | if (FromTy == S.Context.OverloadTy) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8221 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8222 | Expr *E = FromExpr->IgnoreParens(); |
| 8223 | if (isa<UnaryOperator>(E)) |
| 8224 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 8225 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8226 | |
| 8227 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 8228 | << (unsigned) FnKind << FnDesc |
| 8229 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8230 | << ToTy << Name << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8231 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8232 | return; |
| 8233 | } |
| 8234 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8235 | // Do some hand-waving analysis to see if the non-viability is due |
| 8236 | // to a qualifier mismatch. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8237 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 8238 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 8239 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 8240 | CToTy = RT->getPointeeType(); |
| 8241 | else { |
| 8242 | // TODO: detect and diagnose the full richness of const mismatches. |
| 8243 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 8244 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 8245 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 8246 | } |
| 8247 | |
| 8248 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 8249 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8250 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8251 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8252 | |
| 8253 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 8254 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 8255 | << (unsigned) FnKind << FnDesc |
| 8256 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8257 | << FromTy |
| 8258 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 8259 | << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8260 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8261 | return; |
| 8262 | } |
| 8263 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8264 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 8265 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8266 | << (unsigned) FnKind << FnDesc |
| 8267 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8268 | << FromTy |
| 8269 | << FromQs.getObjCLifetime() << ToQs.getObjCLifetime() |
| 8270 | << (unsigned) isObjectArgument << I+1; |
| 8271 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8272 | return; |
| 8273 | } |
| 8274 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 8275 | if (FromQs.getObjCGCAttr() != ToQs.getObjCGCAttr()) { |
| 8276 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_gc) |
| 8277 | << (unsigned) FnKind << FnDesc |
| 8278 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8279 | << FromTy |
| 8280 | << FromQs.getObjCGCAttr() << ToQs.getObjCGCAttr() |
| 8281 | << (unsigned) isObjectArgument << I+1; |
| 8282 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8283 | return; |
| 8284 | } |
| 8285 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8286 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 8287 | assert(CVR && "unexpected qualifiers mismatch"); |
| 8288 | |
| 8289 | if (isObjectArgument) { |
| 8290 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 8291 | << (unsigned) FnKind << FnDesc |
| 8292 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8293 | << FromTy << (CVR - 1); |
| 8294 | } else { |
| 8295 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 8296 | << (unsigned) FnKind << FnDesc |
| 8297 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8298 | << FromTy << (CVR - 1) << I+1; |
| 8299 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8300 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8301 | return; |
| 8302 | } |
| 8303 | |
Sebastian Redl | fd2a00a | 2011-09-24 17:48:32 +0000 | [diff] [blame] | 8304 | // Special diagnostic for failure to convert an initializer list, since |
| 8305 | // telling the user that it has type void is not useful. |
| 8306 | if (FromExpr && isa<InitListExpr>(FromExpr)) { |
| 8307 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_list_argument) |
| 8308 | << (unsigned) FnKind << FnDesc |
| 8309 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8310 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8311 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8312 | return; |
| 8313 | } |
| 8314 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8315 | // Diagnose references or pointers to incomplete types differently, |
| 8316 | // since it's far from impossible that the incompleteness triggered |
| 8317 | // the failure. |
| 8318 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 8319 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 8320 | TempFromTy = PTy->getPointeeType(); |
| 8321 | if (TempFromTy->isIncompleteType()) { |
| 8322 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 8323 | << (unsigned) FnKind << FnDesc |
| 8324 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8325 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8326 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8327 | return; |
| 8328 | } |
| 8329 | |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8330 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8331 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8332 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 8333 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 8334 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8335 | FromPtrTy->getPointeeType()) && |
| 8336 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 8337 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8338 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8339 | FromPtrTy->getPointeeType())) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8340 | BaseToDerivedConversion = 1; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8341 | } |
| 8342 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 8343 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 8344 | if (const ObjCObjectPointerType *ToPtrTy |
| 8345 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 8346 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 8347 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 8348 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8349 | FromPtrTy->getPointeeType()) && |
| 8350 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8351 | BaseToDerivedConversion = 2; |
| 8352 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8353 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 8354 | !FromTy->isIncompleteType() && |
| 8355 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 8356 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) { |
| 8357 | BaseToDerivedConversion = 3; |
| 8358 | } else if (ToTy->isLValueReferenceType() && !FromExpr->isLValue() && |
| 8359 | ToTy.getNonReferenceType().getCanonicalType() == |
| 8360 | FromTy.getNonReferenceType().getCanonicalType()) { |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8361 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_lvalue) |
| 8362 | << (unsigned) FnKind << FnDesc |
| 8363 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8364 | << (unsigned) isObjectArgument << I + 1; |
| 8365 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8366 | return; |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8367 | } |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8368 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8369 | |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8370 | if (BaseToDerivedConversion) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8371 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8372 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8373 | << (unsigned) FnKind << FnDesc |
| 8374 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8375 | << (BaseToDerivedConversion - 1) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8376 | << FromTy << ToTy << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8377 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8378 | return; |
| 8379 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8380 | |
Fariborz Jahanian | 909bcb3 | 2011-07-20 17:14:09 +0000 | [diff] [blame] | 8381 | if (isa<ObjCObjectPointerType>(CFromTy) && |
| 8382 | isa<PointerType>(CToTy)) { |
| 8383 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8384 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8385 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
| 8386 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_arc_conv) |
| 8387 | << (unsigned) FnKind << FnDesc |
| 8388 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8389 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8390 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8391 | return; |
| 8392 | } |
| 8393 | } |
| 8394 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8395 | // Emit the generic diagnostic and, optionally, add the hints to it. |
| 8396 | PartialDiagnostic FDiag = S.PDiag(diag::note_ovl_candidate_bad_conv); |
| 8397 | FDiag << (unsigned) FnKind << FnDesc |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8398 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8399 | << FromTy << ToTy << (unsigned) isObjectArgument << I + 1 |
| 8400 | << (unsigned) (Cand->Fix.Kind); |
| 8401 | |
| 8402 | // If we can fix the conversion, suggest the FixIts. |
Benjamin Kramer | 1136ef0 | 2012-01-14 21:05:10 +0000 | [diff] [blame] | 8403 | for (std::vector<FixItHint>::iterator HI = Cand->Fix.Hints.begin(), |
| 8404 | HE = Cand->Fix.Hints.end(); HI != HE; ++HI) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8405 | FDiag << *HI; |
| 8406 | S.Diag(Fn->getLocation(), FDiag); |
| 8407 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8408 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8409 | } |
| 8410 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8411 | /// Additional arity mismatch diagnosis specific to a function overload |
| 8412 | /// candidates. This is not covered by the more general DiagnoseArityMismatch() |
| 8413 | /// over a candidate in any candidate set. |
| 8414 | bool CheckArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 8415 | unsigned NumArgs) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8416 | FunctionDecl *Fn = Cand->Function; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8417 | unsigned MinParams = Fn->getMinRequiredArguments(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8418 | |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8419 | // With invalid overloaded operators, it's possible that we think we |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8420 | // 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] | 8421 | // right number of arguments, because only overloaded operators have |
| 8422 | // the weird behavior of overloading member and non-member functions. |
| 8423 | // Just don't report anything. |
| 8424 | if (Fn->isInvalidDecl() && |
| 8425 | Fn->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8426 | return true; |
| 8427 | |
| 8428 | if (NumArgs < MinParams) { |
| 8429 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 8430 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8431 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
| 8432 | } else { |
| 8433 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 8434 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8435 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
| 8436 | } |
| 8437 | |
| 8438 | return false; |
| 8439 | } |
| 8440 | |
| 8441 | /// General arity mismatch diagnosis over a candidate in a candidate set. |
| 8442 | void DiagnoseArityMismatch(Sema &S, Decl *D, unsigned NumFormalArgs) { |
| 8443 | assert(isa<FunctionDecl>(D) && |
| 8444 | "The templated declaration should at least be a function" |
| 8445 | " when diagnosing bad template argument deduction due to too many" |
| 8446 | " or too few arguments"); |
| 8447 | |
| 8448 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| 8449 | |
| 8450 | // TODO: treat calls to a missing default constructor as a special case |
| 8451 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 8452 | unsigned MinParams = Fn->getMinRequiredArguments(); |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8453 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8454 | // at least / at most / exactly |
| 8455 | unsigned mode, modeCount; |
| 8456 | if (NumFormalArgs < MinParams) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8457 | if (MinParams != FnTy->getNumArgs() || |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 8458 | FnTy->isVariadic() || FnTy->isTemplateVariadic()) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8459 | mode = 0; // "at least" |
| 8460 | else |
| 8461 | mode = 2; // "exactly" |
| 8462 | modeCount = MinParams; |
| 8463 | } else { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8464 | if (MinParams != FnTy->getNumArgs()) |
| 8465 | mode = 1; // "at most" |
| 8466 | else |
| 8467 | mode = 2; // "exactly" |
| 8468 | modeCount = FnTy->getNumArgs(); |
| 8469 | } |
| 8470 | |
| 8471 | std::string Description; |
| 8472 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 8473 | |
Richard Smith | f7b8056 | 2012-05-11 05:16:41 +0000 | [diff] [blame] | 8474 | if (modeCount == 1 && Fn->getParamDecl(0)->getDeclName()) |
| 8475 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity_one) |
| 8476 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 8477 | << Fn->getParamDecl(0) << NumFormalArgs; |
| 8478 | else |
| 8479 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
| 8480 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 8481 | << modeCount << NumFormalArgs; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8482 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8483 | } |
| 8484 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8485 | /// Arity mismatch diagnosis specific to a function overload candidate. |
| 8486 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 8487 | unsigned NumFormalArgs) { |
| 8488 | if (!CheckArityMismatch(S, Cand, NumFormalArgs)) |
| 8489 | DiagnoseArityMismatch(S, Cand->Function, NumFormalArgs); |
| 8490 | } |
Larisse Voufo | 8c5d407 | 2013-07-19 22:53:23 +0000 | [diff] [blame] | 8491 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8492 | TemplateDecl *getDescribedTemplate(Decl *Templated) { |
| 8493 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Templated)) |
| 8494 | return FD->getDescribedFunctionTemplate(); |
| 8495 | else if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Templated)) |
| 8496 | return RD->getDescribedClassTemplate(); |
| 8497 | |
| 8498 | llvm_unreachable("Unsupported: Getting the described template declaration" |
| 8499 | " for bad deduction diagnosis"); |
| 8500 | } |
| 8501 | |
| 8502 | /// Diagnose a failed template-argument deduction. |
| 8503 | void DiagnoseBadDeduction(Sema &S, Decl *Templated, |
| 8504 | DeductionFailureInfo &DeductionFailure, |
| 8505 | unsigned NumArgs) { |
| 8506 | TemplateParameter Param = DeductionFailure.getTemplateParameter(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8507 | NamedDecl *ParamD; |
| 8508 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 8509 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 8510 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8511 | switch (DeductionFailure.Result) { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8512 | case Sema::TDK_Success: |
| 8513 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 8514 | |
| 8515 | case Sema::TDK_Incomplete: { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8516 | assert(ParamD && "no parameter found for incomplete deduction result"); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8517 | S.Diag(Templated->getLocation(), |
| 8518 | diag::note_ovl_candidate_incomplete_deduction) |
| 8519 | << ParamD->getDeclName(); |
| 8520 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8521 | return; |
| 8522 | } |
| 8523 | |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8524 | case Sema::TDK_Underqualified: { |
| 8525 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 8526 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 8527 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8528 | QualType Param = DeductionFailure.getFirstArg()->getAsType(); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8529 | |
| 8530 | // Param will have been canonicalized, but it should just be a |
| 8531 | // qualified version of ParamD, so move the qualifiers to that. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 8532 | QualifierCollector Qs; |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8533 | Qs.strip(Param); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 8534 | QualType NonCanonParam = Qs.apply(S.Context, TParam->getTypeForDecl()); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8535 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 8536 | |
| 8537 | // Arg has also been canonicalized, but there's nothing we can do |
| 8538 | // about that. It also doesn't matter as much, because it won't |
| 8539 | // have any template parameters in it (because deduction isn't |
| 8540 | // done on dependent types). |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8541 | QualType Arg = DeductionFailure.getSecondArg()->getAsType(); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8542 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8543 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_underqualified) |
| 8544 | << ParamD->getDeclName() << Arg << NonCanonParam; |
| 8545 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8546 | return; |
| 8547 | } |
| 8548 | |
| 8549 | case Sema::TDK_Inconsistent: { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8550 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8551 | int which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8552 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8553 | which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8554 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8555 | which = 1; |
| 8556 | else { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8557 | which = 2; |
| 8558 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8559 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8560 | S.Diag(Templated->getLocation(), |
| 8561 | diag::note_ovl_candidate_inconsistent_deduction) |
| 8562 | << which << ParamD->getDeclName() << *DeductionFailure.getFirstArg() |
| 8563 | << *DeductionFailure.getSecondArg(); |
| 8564 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8565 | return; |
| 8566 | } |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8567 | |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8568 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8569 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8570 | if (ParamD->getDeclName()) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8571 | S.Diag(Templated->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8572 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8573 | << ParamD->getDeclName(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8574 | else { |
| 8575 | int index = 0; |
| 8576 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 8577 | index = TTP->getIndex(); |
| 8578 | else if (NonTypeTemplateParmDecl *NTTP |
| 8579 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 8580 | index = NTTP->getIndex(); |
| 8581 | else |
| 8582 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8583 | S.Diag(Templated->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8584 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8585 | << (index + 1); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8586 | } |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8587 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8588 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8589 | |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8590 | case Sema::TDK_TooManyArguments: |
| 8591 | case Sema::TDK_TooFewArguments: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8592 | DiagnoseArityMismatch(S, Templated, NumArgs); |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8593 | return; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8594 | |
| 8595 | case Sema::TDK_InstantiationDepth: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8596 | S.Diag(Templated->getLocation(), |
| 8597 | diag::note_ovl_candidate_instantiation_depth); |
| 8598 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8599 | return; |
| 8600 | |
| 8601 | case Sema::TDK_SubstitutionFailure: { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8602 | // Format the template argument list into the argument string. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8603 | SmallString<128> TemplateArgString; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8604 | if (TemplateArgumentList *Args = |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8605 | DeductionFailure.getTemplateArgumentList()) { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8606 | TemplateArgString = " "; |
| 8607 | TemplateArgString += S.getTemplateArgumentBindingsText( |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8608 | getDescribedTemplate(Templated)->getTemplateParameters(), *Args); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8609 | } |
| 8610 | |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8611 | // If this candidate was disabled by enable_if, say so. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8612 | PartialDiagnosticAt *PDiag = DeductionFailure.getSFINAEDiagnostic(); |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8613 | if (PDiag && PDiag->second.getDiagID() == |
| 8614 | diag::err_typename_nested_not_found_enable_if) { |
| 8615 | // FIXME: Use the source range of the condition, and the fully-qualified |
| 8616 | // name of the enable_if template. These are both present in PDiag. |
| 8617 | S.Diag(PDiag->first, diag::note_ovl_candidate_disabled_by_enable_if) |
| 8618 | << "'enable_if'" << TemplateArgString; |
| 8619 | return; |
| 8620 | } |
| 8621 | |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8622 | // Format the SFINAE diagnostic into the argument string. |
| 8623 | // FIXME: Add a general mechanism to include a PartialDiagnostic *'s |
| 8624 | // formatted message in another diagnostic. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8625 | SmallString<128> SFINAEArgString; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8626 | SourceRange R; |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8627 | if (PDiag) { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8628 | SFINAEArgString = ": "; |
| 8629 | R = SourceRange(PDiag->first, PDiag->first); |
| 8630 | PDiag->second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 8631 | } |
| 8632 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8633 | S.Diag(Templated->getLocation(), |
| 8634 | diag::note_ovl_candidate_substitution_failure) |
| 8635 | << TemplateArgString << SFINAEArgString << R; |
| 8636 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8637 | return; |
| 8638 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8639 | |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8640 | case Sema::TDK_FailedOverloadResolution: { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8641 | OverloadExpr::FindResult R = OverloadExpr::find(DeductionFailure.getExpr()); |
| 8642 | S.Diag(Templated->getLocation(), |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8643 | diag::note_ovl_candidate_failed_overload_resolution) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8644 | << R.Expression->getName(); |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8645 | return; |
| 8646 | } |
| 8647 | |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8648 | case Sema::TDK_NonDeducedMismatch: { |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8649 | // FIXME: Provide a source location to indicate what we couldn't match. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8650 | TemplateArgument FirstTA = *DeductionFailure.getFirstArg(); |
| 8651 | TemplateArgument SecondTA = *DeductionFailure.getSecondArg(); |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8652 | if (FirstTA.getKind() == TemplateArgument::Template && |
| 8653 | SecondTA.getKind() == TemplateArgument::Template) { |
| 8654 | TemplateName FirstTN = FirstTA.getAsTemplate(); |
| 8655 | TemplateName SecondTN = SecondTA.getAsTemplate(); |
| 8656 | if (FirstTN.getKind() == TemplateName::Template && |
| 8657 | SecondTN.getKind() == TemplateName::Template) { |
| 8658 | if (FirstTN.getAsTemplateDecl()->getName() == |
| 8659 | SecondTN.getAsTemplateDecl()->getName()) { |
| 8660 | // FIXME: This fixes a bad diagnostic where both templates are named |
| 8661 | // the same. This particular case is a bit difficult since: |
| 8662 | // 1) It is passed as a string to the diagnostic printer. |
| 8663 | // 2) The diagnostic printer only attempts to find a better |
| 8664 | // name for types, not decls. |
| 8665 | // Ideally, this should folded into the diagnostic printer. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8666 | S.Diag(Templated->getLocation(), |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8667 | diag::note_ovl_candidate_non_deduced_mismatch_qualified) |
| 8668 | << FirstTN.getAsTemplateDecl() << SecondTN.getAsTemplateDecl(); |
| 8669 | return; |
| 8670 | } |
| 8671 | } |
| 8672 | } |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8673 | S.Diag(Templated->getLocation(), |
| 8674 | diag::note_ovl_candidate_non_deduced_mismatch) |
| 8675 | << FirstTA << SecondTA; |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8676 | return; |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8677 | } |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8678 | // TODO: diagnose these individually, then kill off |
| 8679 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8680 | case Sema::TDK_MiscellaneousDeductionFailure: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8681 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_bad_deduction); |
| 8682 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8683 | return; |
| 8684 | } |
| 8685 | } |
| 8686 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8687 | /// Diagnose a failed template-argument deduction, for function calls. |
| 8688 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, unsigned NumArgs) { |
| 8689 | unsigned TDK = Cand->DeductionFailure.Result; |
| 8690 | if (TDK == Sema::TDK_TooFewArguments || TDK == Sema::TDK_TooManyArguments) { |
| 8691 | if (CheckArityMismatch(S, Cand, NumArgs)) |
| 8692 | return; |
| 8693 | } |
| 8694 | DiagnoseBadDeduction(S, Cand->Function, // pattern |
| 8695 | Cand->DeductionFailure, NumArgs); |
| 8696 | } |
| 8697 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8698 | /// CUDA: diagnose an invalid call across targets. |
| 8699 | void DiagnoseBadTarget(Sema &S, OverloadCandidate *Cand) { |
| 8700 | FunctionDecl *Caller = cast<FunctionDecl>(S.CurContext); |
| 8701 | FunctionDecl *Callee = Cand->Function; |
| 8702 | |
| 8703 | Sema::CUDAFunctionTarget CallerTarget = S.IdentifyCUDATarget(Caller), |
| 8704 | CalleeTarget = S.IdentifyCUDATarget(Callee); |
| 8705 | |
| 8706 | std::string FnDesc; |
| 8707 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Callee, FnDesc); |
| 8708 | |
| 8709 | S.Diag(Callee->getLocation(), diag::note_ovl_candidate_bad_target) |
| 8710 | << (unsigned) FnKind << CalleeTarget << CallerTarget; |
| 8711 | } |
| 8712 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8713 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 8714 | /// already generated a primary error at the call site. |
| 8715 | /// |
| 8716 | /// It really does need to be a single diagnostic with its caret |
| 8717 | /// pointed at the candidate declaration. Yes, this creates some |
| 8718 | /// major challenges of technical writing. Yes, this makes pointing |
| 8719 | /// out problems with specific arguments quite awkward. It's still |
| 8720 | /// better than generating twenty screens of text for every failed |
| 8721 | /// overload. |
| 8722 | /// |
| 8723 | /// It would be great to be able to express per-candidate problems |
| 8724 | /// more richly for those diagnostic clients that cared, but we'd |
| 8725 | /// still have to be just as careful with the default diagnostics. |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8726 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8727 | unsigned NumArgs) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8728 | FunctionDecl *Fn = Cand->Function; |
| 8729 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8730 | // Note deleted candidates, but only if they're viable. |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 8731 | if (Cand->Viable && (Fn->isDeleted() || |
| 8732 | S.isFunctionConsideredUnavailable(Fn))) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8733 | std::string FnDesc; |
| 8734 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8735 | |
| 8736 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 8737 | << FnKind << FnDesc |
| 8738 | << (Fn->isDeleted() ? (Fn->isDeletedAsWritten() ? 1 : 2) : 0); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8739 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8740 | return; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8741 | } |
| 8742 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8743 | // We don't really have anything else to say about viable candidates. |
| 8744 | if (Cand->Viable) { |
| 8745 | S.NoteOverloadCandidate(Fn); |
| 8746 | return; |
| 8747 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8748 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8749 | switch (Cand->FailureKind) { |
| 8750 | case ovl_fail_too_many_arguments: |
| 8751 | case ovl_fail_too_few_arguments: |
| 8752 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8753 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8754 | case ovl_fail_bad_deduction: |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8755 | return DiagnoseBadDeduction(S, Cand, NumArgs); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8756 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8757 | case ovl_fail_trivial_conversion: |
| 8758 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 8759 | case ovl_fail_final_conversion_not_exact: |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8760 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8761 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8762 | case ovl_fail_bad_conversion: { |
| 8763 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8764 | for (unsigned N = Cand->NumConversions; I != N; ++I) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8765 | if (Cand->Conversions[I].isBad()) |
| 8766 | return DiagnoseBadConversion(S, Cand, I); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8767 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8768 | // FIXME: this currently happens when we're called from SemaInit |
| 8769 | // when user-conversion overload fails. Figure out how to handle |
| 8770 | // those conditions and diagnose them well. |
| 8771 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8772 | } |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8773 | |
| 8774 | case ovl_fail_bad_target: |
| 8775 | return DiagnoseBadTarget(S, Cand); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8776 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8777 | } |
| 8778 | |
| 8779 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 8780 | // Desugar the type of the surrogate down to a function type, |
| 8781 | // retaining as many typedefs as possible while still showing |
| 8782 | // the function type (and, therefore, its parameter types). |
| 8783 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 8784 | bool isLValueReference = false; |
| 8785 | bool isRValueReference = false; |
| 8786 | bool isPointer = false; |
| 8787 | if (const LValueReferenceType *FnTypeRef = |
| 8788 | FnType->getAs<LValueReferenceType>()) { |
| 8789 | FnType = FnTypeRef->getPointeeType(); |
| 8790 | isLValueReference = true; |
| 8791 | } else if (const RValueReferenceType *FnTypeRef = |
| 8792 | FnType->getAs<RValueReferenceType>()) { |
| 8793 | FnType = FnTypeRef->getPointeeType(); |
| 8794 | isRValueReference = true; |
| 8795 | } |
| 8796 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 8797 | FnType = FnTypePtr->getPointeeType(); |
| 8798 | isPointer = true; |
| 8799 | } |
| 8800 | // Desugar down to a function type. |
| 8801 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 8802 | // Reconstruct the pointer/reference as appropriate. |
| 8803 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 8804 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 8805 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 8806 | |
| 8807 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 8808 | << FnType; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8809 | MaybeEmitInheritedConstructorNote(S, Cand->Surrogate); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8810 | } |
| 8811 | |
| 8812 | void NoteBuiltinOperatorCandidate(Sema &S, |
David Blaikie | 0bea863 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 8813 | StringRef Opc, |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8814 | SourceLocation OpLoc, |
| 8815 | OverloadCandidate *Cand) { |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8816 | assert(Cand->NumConversions <= 2 && "builtin operator is not binary"); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8817 | std::string TypeStr("operator"); |
| 8818 | TypeStr += Opc; |
| 8819 | TypeStr += "("; |
| 8820 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8821 | if (Cand->NumConversions == 1) { |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8822 | TypeStr += ")"; |
| 8823 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 8824 | } else { |
| 8825 | TypeStr += ", "; |
| 8826 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 8827 | TypeStr += ")"; |
| 8828 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 8829 | } |
| 8830 | } |
| 8831 | |
| 8832 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 8833 | OverloadCandidate *Cand) { |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8834 | unsigned NoOperands = Cand->NumConversions; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8835 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 8836 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8837 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 8838 | if (!ICS.isAmbiguous()) continue; |
| 8839 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8840 | ICS.DiagnoseAmbiguousConversion(S, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8841 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8842 | } |
| 8843 | } |
| 8844 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8845 | static SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8846 | if (Cand->Function) |
| 8847 | return Cand->Function->getLocation(); |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 8848 | if (Cand->IsSurrogate) |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8849 | return Cand->Surrogate->getLocation(); |
| 8850 | return SourceLocation(); |
| 8851 | } |
| 8852 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8853 | static unsigned RankDeductionFailure(const DeductionFailureInfo &DFI) { |
Chandler Carruth | 78bf680 | 2011-09-10 00:51:24 +0000 | [diff] [blame] | 8854 | switch ((Sema::TemplateDeductionResult)DFI.Result) { |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8855 | case Sema::TDK_Success: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8856 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8857 | |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 8858 | case Sema::TDK_Invalid: |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8859 | case Sema::TDK_Incomplete: |
| 8860 | return 1; |
| 8861 | |
| 8862 | case Sema::TDK_Underqualified: |
| 8863 | case Sema::TDK_Inconsistent: |
| 8864 | return 2; |
| 8865 | |
| 8866 | case Sema::TDK_SubstitutionFailure: |
| 8867 | case Sema::TDK_NonDeducedMismatch: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8868 | case Sema::TDK_MiscellaneousDeductionFailure: |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8869 | return 3; |
| 8870 | |
| 8871 | case Sema::TDK_InstantiationDepth: |
| 8872 | case Sema::TDK_FailedOverloadResolution: |
| 8873 | return 4; |
| 8874 | |
| 8875 | case Sema::TDK_InvalidExplicitArguments: |
| 8876 | return 5; |
| 8877 | |
| 8878 | case Sema::TDK_TooManyArguments: |
| 8879 | case Sema::TDK_TooFewArguments: |
| 8880 | return 6; |
| 8881 | } |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8882 | llvm_unreachable("Unhandled deduction result"); |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8883 | } |
| 8884 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8885 | struct CompareOverloadCandidatesForDisplay { |
| 8886 | Sema &S; |
| 8887 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8888 | |
| 8889 | bool operator()(const OverloadCandidate *L, |
| 8890 | const OverloadCandidate *R) { |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 8891 | // Fast-path this check. |
| 8892 | if (L == R) return false; |
| 8893 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8894 | // Order first by viability. |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8895 | if (L->Viable) { |
| 8896 | if (!R->Viable) return true; |
| 8897 | |
| 8898 | // TODO: introduce a tri-valued comparison for overload |
| 8899 | // candidates. Would be more worthwhile if we had a sort |
| 8900 | // that could exploit it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8901 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation())) return true; |
| 8902 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation())) return false; |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8903 | } else if (R->Viable) |
| 8904 | return false; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8905 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8906 | assert(L->Viable == R->Viable); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8907 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8908 | // Criteria by which we can sort non-viable candidates: |
| 8909 | if (!L->Viable) { |
| 8910 | // 1. Arity mismatches come after other candidates. |
| 8911 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 8912 | L->FailureKind == ovl_fail_too_few_arguments) |
| 8913 | return false; |
| 8914 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 8915 | R->FailureKind == ovl_fail_too_few_arguments) |
| 8916 | return true; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8917 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8918 | // 2. Bad conversions come first and are ordered by the number |
| 8919 | // of bad conversions and quality of good conversions. |
| 8920 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 8921 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 8922 | return true; |
| 8923 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8924 | // The conversion that can be fixed with a smaller number of changes, |
| 8925 | // comes first. |
| 8926 | unsigned numLFixes = L->Fix.NumConversionsFixed; |
| 8927 | unsigned numRFixes = R->Fix.NumConversionsFixed; |
| 8928 | numLFixes = (numLFixes == 0) ? UINT_MAX : numLFixes; |
| 8929 | numRFixes = (numRFixes == 0) ? UINT_MAX : numRFixes; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 8930 | if (numLFixes != numRFixes) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8931 | if (numLFixes < numRFixes) |
| 8932 | return true; |
| 8933 | else |
| 8934 | return false; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 8935 | } |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8936 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8937 | // If there's any ordering between the defined conversions... |
| 8938 | // FIXME: this might not be transitive. |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8939 | assert(L->NumConversions == R->NumConversions); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8940 | |
| 8941 | int leftBetter = 0; |
John McCall | 3a81337 | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 8942 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8943 | for (unsigned E = L->NumConversions; I != E; ++I) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8944 | switch (CompareImplicitConversionSequences(S, |
| 8945 | L->Conversions[I], |
| 8946 | R->Conversions[I])) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8947 | case ImplicitConversionSequence::Better: |
| 8948 | leftBetter++; |
| 8949 | break; |
| 8950 | |
| 8951 | case ImplicitConversionSequence::Worse: |
| 8952 | leftBetter--; |
| 8953 | break; |
| 8954 | |
| 8955 | case ImplicitConversionSequence::Indistinguishable: |
| 8956 | break; |
| 8957 | } |
| 8958 | } |
| 8959 | if (leftBetter > 0) return true; |
| 8960 | if (leftBetter < 0) return false; |
| 8961 | |
| 8962 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 8963 | return false; |
| 8964 | |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8965 | if (L->FailureKind == ovl_fail_bad_deduction) { |
| 8966 | if (R->FailureKind != ovl_fail_bad_deduction) |
| 8967 | return true; |
| 8968 | |
| 8969 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 8970 | return RankDeductionFailure(L->DeductionFailure) |
Eli Friedman | ce1846e | 2011-10-14 23:10:30 +0000 | [diff] [blame] | 8971 | < RankDeductionFailure(R->DeductionFailure); |
Eli Friedman | 1c522f7 | 2011-10-14 21:52:24 +0000 | [diff] [blame] | 8972 | } else if (R->FailureKind == ovl_fail_bad_deduction) |
| 8973 | return false; |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8974 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8975 | // TODO: others? |
| 8976 | } |
| 8977 | |
| 8978 | // Sort everything else by location. |
| 8979 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 8980 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 8981 | |
| 8982 | // Put candidates without locations (e.g. builtins) at the end. |
| 8983 | if (LLoc.isInvalid()) return false; |
| 8984 | if (RLoc.isInvalid()) return true; |
| 8985 | |
| 8986 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8987 | } |
| 8988 | }; |
| 8989 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8990 | /// CompleteNonViableCandidate - Normally, overload resolution only |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8991 | /// computes up to the first. Produces the FixIt set if possible. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8992 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8993 | ArrayRef<Expr *> Args) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8994 | assert(!Cand->Viable); |
| 8995 | |
| 8996 | // Don't do anything on failures other than bad conversion. |
| 8997 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 8998 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8999 | // We only want the FixIts if all the arguments can be corrected. |
| 9000 | bool Unfixable = false; |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9001 | // Use a implicit copy initialization to check conversion fixes. |
| 9002 | Cand->Fix.setConversionChecker(TryCopyInitialization); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9003 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9004 | // Skip forward to the first bad conversion. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9005 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9006 | unsigned ConvCount = Cand->NumConversions; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9007 | while (true) { |
| 9008 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 9009 | ConvIdx++; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9010 | if (Cand->Conversions[ConvIdx - 1].isBad()) { |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9011 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx - 1, S); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9012 | break; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9013 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9014 | } |
| 9015 | |
| 9016 | if (ConvIdx == ConvCount) |
| 9017 | return; |
| 9018 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9019 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 9020 | "remaining conversion is initialized?"); |
| 9021 | |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 9022 | // FIXME: this should probably be preserved from the overload |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9023 | // operation somehow. |
| 9024 | bool SuppressUserConversions = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9025 | |
| 9026 | const FunctionProtoType* Proto; |
| 9027 | unsigned ArgIdx = ConvIdx; |
| 9028 | |
| 9029 | if (Cand->IsSurrogate) { |
| 9030 | QualType ConvType |
| 9031 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 9032 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 9033 | ConvType = ConvPtrType->getPointeeType(); |
| 9034 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 9035 | ArgIdx--; |
| 9036 | } else if (Cand->Function) { |
| 9037 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 9038 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 9039 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 9040 | ArgIdx--; |
| 9041 | } else { |
| 9042 | // Builtin binary operator with a bad first conversion. |
| 9043 | assert(ConvCount <= 3); |
| 9044 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 9045 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 9046 | = TryCopyInitialization(S, Args[ConvIdx], |
| 9047 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9048 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9049 | /*InOverloadResolution*/ true, |
| 9050 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9051 | S.getLangOpts().ObjCAutoRefCount); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9052 | return; |
| 9053 | } |
| 9054 | |
| 9055 | // Fill in the rest of the conversions. |
| 9056 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 9057 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9058 | if (ArgIdx < NumArgsInProto) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9059 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 9060 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9061 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9062 | /*InOverloadResolution=*/true, |
| 9063 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9064 | S.getLangOpts().ObjCAutoRefCount); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9065 | // Store the FixIt in the candidate if it exists. |
| 9066 | if (!Unfixable && Cand->Conversions[ConvIdx].isBad()) |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9067 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx, S); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9068 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9069 | else |
| 9070 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 9071 | } |
| 9072 | } |
| 9073 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9074 | } // end anonymous namespace |
| 9075 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9076 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 9077 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9078 | /// set. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9079 | void OverloadCandidateSet::NoteCandidates(Sema &S, |
| 9080 | OverloadCandidateDisplayKind OCD, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9081 | ArrayRef<Expr *> Args, |
David Blaikie | 0bea863 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 9082 | StringRef Opc, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9083 | SourceLocation OpLoc) { |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9084 | // Sort the candidates by viability and position. Sorting directly would |
| 9085 | // be prohibitive, so we make a set of pointers and sort those. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9086 | SmallVector<OverloadCandidate*, 32> Cands; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9087 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
| 9088 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9089 | if (Cand->Viable) |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9090 | Cands.push_back(Cand); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9091 | else if (OCD == OCD_AllCandidates) { |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9092 | CompleteNonViableCandidate(S, Cand, Args); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9093 | if (Cand->Function || Cand->IsSurrogate) |
| 9094 | Cands.push_back(Cand); |
| 9095 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 9096 | // want to list every possible builtin candidate. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9097 | } |
| 9098 | } |
| 9099 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 9100 | std::sort(Cands.begin(), Cands.end(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9101 | CompareOverloadCandidatesForDisplay(S)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9102 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9103 | bool ReportedAmbiguousConversions = false; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9104 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9105 | SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 9106 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9107 | unsigned CandsShown = 0; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9108 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 9109 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 9110 | |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9111 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 9112 | // the user with. FIXME: This limit should depend on details of the |
| 9113 | // candidate list. |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 9114 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) { |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9115 | break; |
| 9116 | } |
| 9117 | ++CandsShown; |
| 9118 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9119 | if (Cand->Function) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9120 | NoteFunctionCandidate(S, Cand, Args.size()); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9121 | else if (Cand->IsSurrogate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9122 | NoteSurrogateCandidate(S, Cand); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9123 | else { |
| 9124 | assert(Cand->Viable && |
| 9125 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9126 | // Generally we only see ambiguities including viable builtin |
| 9127 | // operators if overload resolution got screwed up by an |
| 9128 | // ambiguous user-defined conversion. |
| 9129 | // |
| 9130 | // FIXME: It's quite possible for different conversions to see |
| 9131 | // different ambiguities, though. |
| 9132 | if (!ReportedAmbiguousConversions) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9133 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9134 | ReportedAmbiguousConversions = true; |
| 9135 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9136 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9137 | // If this is a viable builtin, print it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9138 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 9139 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9140 | } |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9141 | |
| 9142 | if (I != E) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9143 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9144 | } |
| 9145 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9146 | static SourceLocation |
| 9147 | GetLocationForCandidate(const TemplateSpecCandidate *Cand) { |
| 9148 | return Cand->Specialization ? Cand->Specialization->getLocation() |
| 9149 | : SourceLocation(); |
| 9150 | } |
| 9151 | |
| 9152 | struct CompareTemplateSpecCandidatesForDisplay { |
| 9153 | Sema &S; |
| 9154 | CompareTemplateSpecCandidatesForDisplay(Sema &S) : S(S) {} |
| 9155 | |
| 9156 | bool operator()(const TemplateSpecCandidate *L, |
| 9157 | const TemplateSpecCandidate *R) { |
| 9158 | // Fast-path this check. |
| 9159 | if (L == R) |
| 9160 | return false; |
| 9161 | |
| 9162 | // Assuming that both candidates are not matches... |
| 9163 | |
| 9164 | // Sort by the ranking of deduction failures. |
| 9165 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 9166 | return RankDeductionFailure(L->DeductionFailure) < |
| 9167 | RankDeductionFailure(R->DeductionFailure); |
| 9168 | |
| 9169 | // Sort everything else by location. |
| 9170 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 9171 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 9172 | |
| 9173 | // Put candidates without locations (e.g. builtins) at the end. |
| 9174 | if (LLoc.isInvalid()) |
| 9175 | return false; |
| 9176 | if (RLoc.isInvalid()) |
| 9177 | return true; |
| 9178 | |
| 9179 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
| 9180 | } |
| 9181 | }; |
| 9182 | |
| 9183 | /// Diagnose a template argument deduction failure. |
| 9184 | /// We are treating these failures as overload failures due to bad |
| 9185 | /// deductions. |
| 9186 | void TemplateSpecCandidate::NoteDeductionFailure(Sema &S) { |
| 9187 | DiagnoseBadDeduction(S, Specialization, // pattern |
| 9188 | DeductionFailure, /*NumArgs=*/0); |
| 9189 | } |
| 9190 | |
| 9191 | void TemplateSpecCandidateSet::destroyCandidates() { |
| 9192 | for (iterator i = begin(), e = end(); i != e; ++i) { |
| 9193 | i->DeductionFailure.Destroy(); |
| 9194 | } |
| 9195 | } |
| 9196 | |
| 9197 | void TemplateSpecCandidateSet::clear() { |
| 9198 | destroyCandidates(); |
| 9199 | Candidates.clear(); |
| 9200 | } |
| 9201 | |
| 9202 | /// NoteCandidates - When no template specialization match is found, prints |
| 9203 | /// diagnostic messages containing the non-matching specializations that form |
| 9204 | /// the candidate set. |
| 9205 | /// This is analoguous to OverloadCandidateSet::NoteCandidates() with |
| 9206 | /// OCD == OCD_AllCandidates and Cand->Viable == false. |
| 9207 | void TemplateSpecCandidateSet::NoteCandidates(Sema &S, SourceLocation Loc) { |
| 9208 | // Sort the candidates by position (assuming no candidate is a match). |
| 9209 | // Sorting directly would be prohibitive, so we make a set of pointers |
| 9210 | // and sort those. |
| 9211 | SmallVector<TemplateSpecCandidate *, 32> Cands; |
| 9212 | Cands.reserve(size()); |
| 9213 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
| 9214 | if (Cand->Specialization) |
| 9215 | Cands.push_back(Cand); |
| 9216 | // Otherwise, this is a non matching builtin candidate. We do not, |
| 9217 | // in general, want to list every possible builtin candidate. |
| 9218 | } |
| 9219 | |
| 9220 | std::sort(Cands.begin(), Cands.end(), |
| 9221 | CompareTemplateSpecCandidatesForDisplay(S)); |
| 9222 | |
| 9223 | // FIXME: Perhaps rename OverloadsShown and getShowOverloads() |
| 9224 | // for generalization purposes (?). |
| 9225 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
| 9226 | |
| 9227 | SmallVectorImpl<TemplateSpecCandidate *>::iterator I, E; |
| 9228 | unsigned CandsShown = 0; |
| 9229 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 9230 | TemplateSpecCandidate *Cand = *I; |
| 9231 | |
| 9232 | // Set an arbitrary limit on the number of candidates we'll spam |
| 9233 | // the user with. FIXME: This limit should depend on details of the |
| 9234 | // candidate list. |
| 9235 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
| 9236 | break; |
| 9237 | ++CandsShown; |
| 9238 | |
| 9239 | assert(Cand->Specialization && |
| 9240 | "Non-matching built-in candidates are not added to Cands."); |
| 9241 | Cand->NoteDeductionFailure(S); |
| 9242 | } |
| 9243 | |
| 9244 | if (I != E) |
| 9245 | S.Diag(Loc, diag::note_ovl_too_many_candidates) << int(E - I); |
| 9246 | } |
| 9247 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9248 | // [PossiblyAFunctionType] --> [Return] |
| 9249 | // NonFunctionType --> NonFunctionType |
| 9250 | // R (A) --> R(A) |
| 9251 | // R (*)(A) --> R (A) |
| 9252 | // R (&)(A) --> R (A) |
| 9253 | // R (S::*)(A) --> R (A) |
| 9254 | QualType Sema::ExtractUnqualifiedFunctionType(QualType PossiblyAFunctionType) { |
| 9255 | QualType Ret = PossiblyAFunctionType; |
| 9256 | if (const PointerType *ToTypePtr = |
| 9257 | PossiblyAFunctionType->getAs<PointerType>()) |
| 9258 | Ret = ToTypePtr->getPointeeType(); |
| 9259 | else if (const ReferenceType *ToTypeRef = |
| 9260 | PossiblyAFunctionType->getAs<ReferenceType>()) |
| 9261 | Ret = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 9262 | else if (const MemberPointerType *MemTypePtr = |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9263 | PossiblyAFunctionType->getAs<MemberPointerType>()) |
| 9264 | Ret = MemTypePtr->getPointeeType(); |
| 9265 | Ret = |
| 9266 | Context.getCanonicalType(Ret).getUnqualifiedType(); |
| 9267 | return Ret; |
| 9268 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9269 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9270 | // A helper class to help with address of function resolution |
| 9271 | // - allows us to avoid passing around all those ugly parameters |
| 9272 | class AddressOfFunctionResolver |
| 9273 | { |
| 9274 | Sema& S; |
| 9275 | Expr* SourceExpr; |
| 9276 | const QualType& TargetType; |
| 9277 | QualType TargetFunctionType; // Extracted function type from target type |
| 9278 | |
| 9279 | bool Complain; |
| 9280 | //DeclAccessPair& ResultFunctionAccessPair; |
| 9281 | ASTContext& Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9282 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9283 | bool TargetTypeIsNonStaticMemberFunction; |
| 9284 | bool FoundNonTemplateFunction; |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9285 | bool StaticMemberFunctionFromBoundPointer; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9286 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9287 | OverloadExpr::FindResult OvlExprInfo; |
| 9288 | OverloadExpr *OvlExpr; |
| 9289 | TemplateArgumentListInfo OvlExplicitTemplateArgs; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9290 | SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9291 | TemplateSpecCandidateSet FailedCandidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9292 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9293 | public: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9294 | AddressOfFunctionResolver(Sema &S, Expr *SourceExpr, |
| 9295 | const QualType &TargetType, bool Complain) |
| 9296 | : S(S), SourceExpr(SourceExpr), TargetType(TargetType), |
| 9297 | Complain(Complain), Context(S.getASTContext()), |
| 9298 | TargetTypeIsNonStaticMemberFunction( |
| 9299 | !!TargetType->getAs<MemberPointerType>()), |
| 9300 | FoundNonTemplateFunction(false), |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9301 | StaticMemberFunctionFromBoundPointer(false), |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9302 | OvlExprInfo(OverloadExpr::find(SourceExpr)), |
| 9303 | OvlExpr(OvlExprInfo.Expression), |
| 9304 | FailedCandidates(OvlExpr->getNameLoc()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9305 | ExtractUnqualifiedFunctionTypeFromTargetType(); |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9306 | |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9307 | if (TargetFunctionType->isFunctionType()) { |
| 9308 | if (UnresolvedMemberExpr *UME = dyn_cast<UnresolvedMemberExpr>(OvlExpr)) |
| 9309 | if (!UME->isImplicitAccess() && |
| 9310 | !S.ResolveSingleFunctionTemplateSpecialization(UME)) |
| 9311 | StaticMemberFunctionFromBoundPointer = true; |
| 9312 | } else if (OvlExpr->hasExplicitTemplateArgs()) { |
| 9313 | DeclAccessPair dap; |
| 9314 | if (FunctionDecl *Fn = S.ResolveSingleFunctionTemplateSpecialization( |
| 9315 | OvlExpr, false, &dap)) { |
| 9316 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
| 9317 | if (!Method->isStatic()) { |
| 9318 | // If the target type is a non-function type and the function found |
| 9319 | // is a non-static member function, pretend as if that was the |
| 9320 | // target, it's the only possible type to end up with. |
| 9321 | TargetTypeIsNonStaticMemberFunction = true; |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9322 | |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9323 | // And skip adding the function if its not in the proper form. |
| 9324 | // We'll diagnose this due to an empty set of functions. |
| 9325 | if (!OvlExprInfo.HasFormOfMemberPointer) |
| 9326 | return; |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9327 | } |
| 9328 | |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9329 | Matches.push_back(std::make_pair(dap, Fn)); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 9330 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9331 | return; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 9332 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9333 | |
| 9334 | if (OvlExpr->hasExplicitTemplateArgs()) |
| 9335 | OvlExpr->getExplicitTemplateArgs().copyInto(OvlExplicitTemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9336 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9337 | if (FindAllFunctionsThatMatchTargetTypeExactly()) { |
| 9338 | // C++ [over.over]p4: |
| 9339 | // If more than one function is selected, [...] |
| 9340 | if (Matches.size() > 1) { |
| 9341 | if (FoundNonTemplateFunction) |
| 9342 | EliminateAllTemplateMatches(); |
| 9343 | else |
| 9344 | EliminateAllExceptMostSpecializedTemplate(); |
| 9345 | } |
| 9346 | } |
| 9347 | } |
| 9348 | |
| 9349 | private: |
| 9350 | bool isTargetTypeAFunction() const { |
| 9351 | return TargetFunctionType->isFunctionType(); |
| 9352 | } |
| 9353 | |
| 9354 | // [ToType] [Return] |
| 9355 | |
| 9356 | // R (*)(A) --> R (A), IsNonStaticMemberFunction = false |
| 9357 | // R (&)(A) --> R (A), IsNonStaticMemberFunction = false |
| 9358 | // R (S::*)(A) --> R (A), IsNonStaticMemberFunction = true |
| 9359 | void inline ExtractUnqualifiedFunctionTypeFromTargetType() { |
| 9360 | TargetFunctionType = S.ExtractUnqualifiedFunctionType(TargetType); |
| 9361 | } |
| 9362 | |
| 9363 | // return true if any matching specializations were found |
| 9364 | bool AddMatchingTemplateFunction(FunctionTemplateDecl* FunctionTemplate, |
| 9365 | const DeclAccessPair& CurAccessFunPair) { |
| 9366 | if (CXXMethodDecl *Method |
| 9367 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
| 9368 | // Skip non-static function templates when converting to pointer, and |
| 9369 | // static when converting to member pointer. |
| 9370 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 9371 | return false; |
| 9372 | } |
| 9373 | else if (TargetTypeIsNonStaticMemberFunction) |
| 9374 | return false; |
| 9375 | |
| 9376 | // C++ [over.over]p2: |
| 9377 | // If the name is a function template, template argument deduction is |
| 9378 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 9379 | // resulting template argument list is used to generate a single |
| 9380 | // function template specialization, which is added to the set of |
| 9381 | // overloaded functions considered. |
| 9382 | FunctionDecl *Specialization = 0; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9383 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9384 | if (Sema::TemplateDeductionResult Result |
| 9385 | = S.DeduceTemplateArguments(FunctionTemplate, |
| 9386 | &OvlExplicitTemplateArgs, |
| 9387 | TargetFunctionType, Specialization, |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9388 | Info, /*InOverloadResolution=*/true)) { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9389 | // Make a note of the failed deduction for diagnostics. |
| 9390 | FailedCandidates.addCandidate() |
| 9391 | .set(FunctionTemplate->getTemplatedDecl(), |
| 9392 | MakeDeductionFailureInfo(Context, Result, Info)); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9393 | (void)Result; |
| 9394 | return false; |
| 9395 | } |
| 9396 | |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9397 | // Template argument deduction ensures that we have an exact match or |
| 9398 | // compatible pointer-to-function arguments that would be adjusted by ICS. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9399 | // This function template specicalization works. |
| 9400 | Specialization = cast<FunctionDecl>(Specialization->getCanonicalDecl()); |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9401 | assert(S.isSameOrCompatibleFunctionType( |
| 9402 | Context.getCanonicalType(Specialization->getType()), |
| 9403 | Context.getCanonicalType(TargetFunctionType))); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9404 | Matches.push_back(std::make_pair(CurAccessFunPair, Specialization)); |
| 9405 | return true; |
| 9406 | } |
| 9407 | |
| 9408 | bool AddMatchingNonTemplateFunction(NamedDecl* Fn, |
| 9409 | const DeclAccessPair& CurAccessFunPair) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9410 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 9411 | // Skip non-static functions when converting to pointer, and static |
| 9412 | // when converting to member pointer. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9413 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 9414 | return false; |
| 9415 | } |
| 9416 | else if (TargetTypeIsNonStaticMemberFunction) |
| 9417 | return false; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9418 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9419 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9420 | if (S.getLangOpts().CUDA) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 9421 | if (FunctionDecl *Caller = dyn_cast<FunctionDecl>(S.CurContext)) |
| 9422 | if (S.CheckCUDATarget(Caller, FunDecl)) |
| 9423 | return false; |
| 9424 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9425 | // If any candidate has a placeholder return type, trigger its deduction |
| 9426 | // now. |
| 9427 | if (S.getLangOpts().CPlusPlus1y && |
| 9428 | FunDecl->getResultType()->isUndeducedType() && |
| 9429 | S.DeduceReturnType(FunDecl, SourceExpr->getLocStart(), Complain)) |
| 9430 | return false; |
| 9431 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 9432 | QualType ResultTy; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9433 | if (Context.hasSameUnqualifiedType(TargetFunctionType, |
| 9434 | FunDecl->getType()) || |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 9435 | S.IsNoReturnConversion(FunDecl->getType(), TargetFunctionType, |
| 9436 | ResultTy)) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9437 | Matches.push_back(std::make_pair(CurAccessFunPair, |
| 9438 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 9439 | FoundNonTemplateFunction = true; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9440 | return true; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 9441 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9442 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9443 | |
| 9444 | return false; |
| 9445 | } |
| 9446 | |
| 9447 | bool FindAllFunctionsThatMatchTargetTypeExactly() { |
| 9448 | bool Ret = false; |
| 9449 | |
| 9450 | // If the overload expression doesn't have the form of a pointer to |
| 9451 | // member, don't try to convert it to a pointer-to-member type. |
| 9452 | if (IsInvalidFormOfPointerToMemberFunction()) |
| 9453 | return false; |
| 9454 | |
| 9455 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 9456 | E = OvlExpr->decls_end(); |
| 9457 | I != E; ++I) { |
| 9458 | // Look through any using declarations to find the underlying function. |
| 9459 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 9460 | |
| 9461 | // C++ [over.over]p3: |
| 9462 | // Non-member functions and static member functions match |
| 9463 | // targets of type "pointer-to-function" or "reference-to-function." |
| 9464 | // Nonstatic member functions match targets of |
| 9465 | // type "pointer-to-member-function." |
| 9466 | // Note that according to DR 247, the containing class does not matter. |
| 9467 | if (FunctionTemplateDecl *FunctionTemplate |
| 9468 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
| 9469 | if (AddMatchingTemplateFunction(FunctionTemplate, I.getPair())) |
| 9470 | Ret = true; |
| 9471 | } |
| 9472 | // If we have explicit template arguments supplied, skip non-templates. |
| 9473 | else if (!OvlExpr->hasExplicitTemplateArgs() && |
| 9474 | AddMatchingNonTemplateFunction(Fn, I.getPair())) |
| 9475 | Ret = true; |
| 9476 | } |
| 9477 | assert(Ret || Matches.empty()); |
| 9478 | return Ret; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9479 | } |
| 9480 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9481 | void EliminateAllExceptMostSpecializedTemplate() { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 9482 | // [...] and any given function template specialization F1 is |
| 9483 | // eliminated if the set contains a second function template |
| 9484 | // specialization whose function template is more specialized |
| 9485 | // than the function template of F1 according to the partial |
| 9486 | // ordering rules of 14.5.5.2. |
| 9487 | |
| 9488 | // The algorithm specified above is quadratic. We instead use a |
| 9489 | // two-pass algorithm (similar to the one used to identify the |
| 9490 | // best viable function in an overload set) that identifies the |
| 9491 | // best function template (if it exists). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9492 | |
| 9493 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 9494 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 9495 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9496 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9497 | // TODO: It looks like FailedCandidates does not serve much purpose |
| 9498 | // here, since the no_viable diagnostic has index 0. |
| 9499 | UnresolvedSetIterator Result = S.getMostSpecialized( |
| 9500 | MatchesCopy.begin(), MatchesCopy.end(), FailedCandidates, TPOC_Other, 0, |
| 9501 | SourceExpr->getLocStart(), S.PDiag(), |
| 9502 | S.PDiag(diag::err_addr_ovl_ambiguous) << Matches[0] |
| 9503 | .second->getDeclName(), |
| 9504 | S.PDiag(diag::note_ovl_candidate) << (unsigned)oc_function_template, |
| 9505 | Complain, TargetFunctionType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9506 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9507 | if (Result != MatchesCopy.end()) { |
| 9508 | // Make it the first and only element |
| 9509 | Matches[0].first = Matches[Result - MatchesCopy.begin()].first; |
| 9510 | Matches[0].second = cast<FunctionDecl>(*Result); |
| 9511 | Matches.resize(1); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9512 | } |
| 9513 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9514 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9515 | void EliminateAllTemplateMatches() { |
| 9516 | // [...] any function template specializations in the set are |
| 9517 | // eliminated if the set also contains a non-template function, [...] |
| 9518 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
| 9519 | if (Matches[I].second->getPrimaryTemplate() == 0) |
| 9520 | ++I; |
| 9521 | else { |
| 9522 | Matches[I] = Matches[--N]; |
| 9523 | Matches.set_size(N); |
| 9524 | } |
| 9525 | } |
| 9526 | } |
| 9527 | |
| 9528 | public: |
| 9529 | void ComplainNoMatchesFound() const { |
| 9530 | assert(Matches.empty()); |
| 9531 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_no_viable) |
| 9532 | << OvlExpr->getName() << TargetFunctionType |
| 9533 | << OvlExpr->getSourceRange(); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9534 | FailedCandidates.NoteCandidates(S, OvlExpr->getLocStart()); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9535 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9536 | } |
| 9537 | |
| 9538 | bool IsInvalidFormOfPointerToMemberFunction() const { |
| 9539 | return TargetTypeIsNonStaticMemberFunction && |
| 9540 | !OvlExprInfo.HasFormOfMemberPointer; |
| 9541 | } |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9542 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9543 | void ComplainIsInvalidFormOfPointerToMemberFunction() const { |
| 9544 | // TODO: Should we condition this on whether any functions might |
| 9545 | // have matched, or is it more appropriate to do that in callers? |
| 9546 | // TODO: a fixit wouldn't hurt. |
| 9547 | S.Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
| 9548 | << TargetType << OvlExpr->getSourceRange(); |
| 9549 | } |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9550 | |
| 9551 | bool IsStaticMemberFunctionFromBoundPointer() const { |
| 9552 | return StaticMemberFunctionFromBoundPointer; |
| 9553 | } |
| 9554 | |
| 9555 | void ComplainIsStaticMemberFunctionFromBoundPointer() const { |
| 9556 | S.Diag(OvlExpr->getLocStart(), |
| 9557 | diag::err_invalid_form_pointer_member_function) |
| 9558 | << OvlExpr->getSourceRange(); |
| 9559 | } |
| 9560 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9561 | void ComplainOfInvalidConversion() const { |
| 9562 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 9563 | << OvlExpr->getName() << TargetType; |
| 9564 | } |
| 9565 | |
| 9566 | void ComplainMultipleMatchesFound() const { |
| 9567 | assert(Matches.size() > 1); |
| 9568 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 9569 | << OvlExpr->getName() |
| 9570 | << OvlExpr->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9571 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9572 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9573 | |
| 9574 | bool hadMultipleCandidates() const { return (OvlExpr->getNumDecls() > 1); } |
| 9575 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9576 | int getNumMatches() const { return Matches.size(); } |
| 9577 | |
| 9578 | FunctionDecl* getMatchingFunctionDecl() const { |
| 9579 | if (Matches.size() != 1) return 0; |
| 9580 | return Matches[0].second; |
| 9581 | } |
| 9582 | |
| 9583 | const DeclAccessPair* getMatchingFunctionAccessPair() const { |
| 9584 | if (Matches.size() != 1) return 0; |
| 9585 | return &Matches[0].first; |
| 9586 | } |
| 9587 | }; |
| 9588 | |
| 9589 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 9590 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 9591 | /// expression with overloaded function type and @p ToType is the type |
| 9592 | /// we're trying to resolve to. For example: |
| 9593 | /// |
| 9594 | /// @code |
| 9595 | /// int f(double); |
| 9596 | /// int f(int); |
| 9597 | /// |
| 9598 | /// int (*pfd)(double) = f; // selects f(double) |
| 9599 | /// @endcode |
| 9600 | /// |
| 9601 | /// This routine returns the resulting FunctionDecl if it could be |
| 9602 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 9603 | /// routine will emit diagnostics if there is an error. |
| 9604 | FunctionDecl * |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9605 | Sema::ResolveAddressOfOverloadedFunction(Expr *AddressOfExpr, |
| 9606 | QualType TargetType, |
| 9607 | bool Complain, |
| 9608 | DeclAccessPair &FoundResult, |
| 9609 | bool *pHadMultipleCandidates) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9610 | assert(AddressOfExpr->getType() == Context.OverloadTy); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9611 | |
| 9612 | AddressOfFunctionResolver Resolver(*this, AddressOfExpr, TargetType, |
| 9613 | Complain); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9614 | int NumMatches = Resolver.getNumMatches(); |
| 9615 | FunctionDecl* Fn = 0; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9616 | if (NumMatches == 0 && Complain) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9617 | if (Resolver.IsInvalidFormOfPointerToMemberFunction()) |
| 9618 | Resolver.ComplainIsInvalidFormOfPointerToMemberFunction(); |
| 9619 | else |
| 9620 | Resolver.ComplainNoMatchesFound(); |
| 9621 | } |
| 9622 | else if (NumMatches > 1 && Complain) |
| 9623 | Resolver.ComplainMultipleMatchesFound(); |
| 9624 | else if (NumMatches == 1) { |
| 9625 | Fn = Resolver.getMatchingFunctionDecl(); |
| 9626 | assert(Fn); |
| 9627 | FoundResult = *Resolver.getMatchingFunctionAccessPair(); |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9628 | if (Complain) { |
| 9629 | if (Resolver.IsStaticMemberFunctionFromBoundPointer()) |
| 9630 | Resolver.ComplainIsStaticMemberFunctionFromBoundPointer(); |
| 9631 | else |
| 9632 | CheckAddressOfMemberAccess(AddressOfExpr, FoundResult); |
| 9633 | } |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 9634 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9635 | |
| 9636 | if (pHadMultipleCandidates) |
| 9637 | *pHadMultipleCandidates = Resolver.hadMultipleCandidates(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9638 | return Fn; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9639 | } |
| 9640 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9641 | /// \brief Given an expression that refers to an overloaded function, try to |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9642 | /// resolve that overloaded function expression down to a single function. |
| 9643 | /// |
| 9644 | /// This routine can only resolve template-ids that refer to a single function |
| 9645 | /// template, where that template-id refers to a single template whose template |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9646 | /// arguments are either provided by the template-id or have defaults, |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9647 | /// as described in C++0x [temp.arg.explicit]p3. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9648 | FunctionDecl * |
| 9649 | Sema::ResolveSingleFunctionTemplateSpecialization(OverloadExpr *ovl, |
| 9650 | bool Complain, |
| 9651 | DeclAccessPair *FoundResult) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9652 | // C++ [over.over]p1: |
| 9653 | // [...] [Note: any redundant set of parentheses surrounding the |
| 9654 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9655 | // C++ [over.over]p1: |
| 9656 | // [...] The overloaded function name can be preceded by the & |
| 9657 | // operator. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9658 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9659 | // If we didn't actually find any template-ids, we're done. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9660 | if (!ovl->hasExplicitTemplateArgs()) |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9661 | return 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 9662 | |
| 9663 | TemplateArgumentListInfo ExplicitTemplateArgs; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9664 | ovl->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9665 | TemplateSpecCandidateSet FailedCandidates(ovl->getNameLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9666 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9667 | // Look through all of the overloaded functions, searching for one |
| 9668 | // whose type matches exactly. |
| 9669 | FunctionDecl *Matched = 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9670 | for (UnresolvedSetIterator I = ovl->decls_begin(), |
| 9671 | E = ovl->decls_end(); I != E; ++I) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9672 | // C++0x [temp.arg.explicit]p3: |
| 9673 | // [...] In contexts where deduction is done and fails, or in contexts |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9674 | // where deduction is not done, if a template argument list is |
| 9675 | // specified and it, along with any default template arguments, |
| 9676 | // identifies a single function template specialization, then the |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9677 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | 66a8c9a | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 9678 | FunctionTemplateDecl *FunctionTemplate |
| 9679 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9680 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9681 | // C++ [over.over]p2: |
| 9682 | // If the name is a function template, template argument deduction is |
| 9683 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 9684 | // resulting template argument list is used to generate a single |
| 9685 | // function template specialization, which is added to the set of |
| 9686 | // overloaded functions considered. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9687 | FunctionDecl *Specialization = 0; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9688 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9689 | if (TemplateDeductionResult Result |
| 9690 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9691 | Specialization, Info, |
| 9692 | /*InOverloadResolution=*/true)) { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9693 | // Make a note of the failed deduction for diagnostics. |
| 9694 | // TODO: Actually use the failed-deduction info? |
| 9695 | FailedCandidates.addCandidate() |
| 9696 | .set(FunctionTemplate->getTemplatedDecl(), |
| 9697 | MakeDeductionFailureInfo(Context, Result, Info)); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9698 | (void)Result; |
| 9699 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9700 | } |
| 9701 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9702 | assert(Specialization && "no specialization and no error?"); |
| 9703 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9704 | // Multiple matches; we can't resolve to a single declaration. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9705 | if (Matched) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9706 | if (Complain) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9707 | Diag(ovl->getExprLoc(), diag::err_addr_ovl_ambiguous) |
| 9708 | << ovl->getName(); |
| 9709 | NoteAllOverloadCandidates(ovl); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9710 | } |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9711 | return 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9712 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9713 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9714 | Matched = Specialization; |
| 9715 | if (FoundResult) *FoundResult = I.getPair(); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9716 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9717 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9718 | if (Matched && getLangOpts().CPlusPlus1y && |
| 9719 | Matched->getResultType()->isUndeducedType() && |
| 9720 | DeduceReturnType(Matched, ovl->getExprLoc(), Complain)) |
| 9721 | return 0; |
| 9722 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9723 | return Matched; |
| 9724 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9725 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9726 | |
| 9727 | |
| 9728 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9729 | // Resolve and fix an overloaded expression that can be resolved |
| 9730 | // because it identifies a single function template specialization. |
| 9731 | // |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9732 | // Last three arguments should only be supplied if Complain = true |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9733 | // |
| 9734 | // Return true if it was logically possible to so resolve the |
| 9735 | // expression, regardless of whether or not it succeeded. Always |
| 9736 | // returns true if 'complain' is set. |
| 9737 | bool Sema::ResolveAndFixSingleFunctionTemplateSpecialization( |
| 9738 | ExprResult &SrcExpr, bool doFunctionPointerConverion, |
| 9739 | bool complain, const SourceRange& OpRangeForComplaining, |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9740 | QualType DestTypeForComplaining, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9741 | unsigned DiagIDForComplaining) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9742 | assert(SrcExpr.get()->getType() == Context.OverloadTy); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9743 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9744 | OverloadExpr::FindResult ovl = OverloadExpr::find(SrcExpr.get()); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9745 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9746 | DeclAccessPair found; |
| 9747 | ExprResult SingleFunctionExpression; |
| 9748 | if (FunctionDecl *fn = ResolveSingleFunctionTemplateSpecialization( |
| 9749 | ovl.Expression, /*complain*/ false, &found)) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9750 | if (DiagnoseUseOfDecl(fn, SrcExpr.get()->getLocStart())) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9751 | SrcExpr = ExprError(); |
| 9752 | return true; |
| 9753 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9754 | |
| 9755 | // It is only correct to resolve to an instance method if we're |
| 9756 | // resolving a form that's permitted to be a pointer to member. |
| 9757 | // Otherwise we'll end up making a bound member expression, which |
| 9758 | // is illegal in all the contexts we resolve like this. |
| 9759 | if (!ovl.HasFormOfMemberPointer && |
| 9760 | isa<CXXMethodDecl>(fn) && |
| 9761 | cast<CXXMethodDecl>(fn)->isInstance()) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9762 | if (!complain) return false; |
| 9763 | |
| 9764 | Diag(ovl.Expression->getExprLoc(), |
| 9765 | diag::err_bound_member_function) |
| 9766 | << 0 << ovl.Expression->getSourceRange(); |
| 9767 | |
| 9768 | // TODO: I believe we only end up here if there's a mix of |
| 9769 | // static and non-static candidates (otherwise the expression |
| 9770 | // would have 'bound member' type, not 'overload' type). |
| 9771 | // Ideally we would note which candidate was chosen and why |
| 9772 | // the static candidates were rejected. |
| 9773 | SrcExpr = ExprError(); |
| 9774 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9775 | } |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 9776 | |
Sylvestre Ledru | 43e3dee | 2012-07-31 06:56:50 +0000 | [diff] [blame] | 9777 | // Fix the expression to refer to 'fn'. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9778 | SingleFunctionExpression = |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9779 | Owned(FixOverloadedFunctionReference(SrcExpr.take(), found, fn)); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9780 | |
| 9781 | // If desired, do function-to-pointer decay. |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9782 | if (doFunctionPointerConverion) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9783 | SingleFunctionExpression = |
| 9784 | DefaultFunctionArrayLvalueConversion(SingleFunctionExpression.take()); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9785 | if (SingleFunctionExpression.isInvalid()) { |
| 9786 | SrcExpr = ExprError(); |
| 9787 | return true; |
| 9788 | } |
| 9789 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9790 | } |
| 9791 | |
| 9792 | if (!SingleFunctionExpression.isUsable()) { |
| 9793 | if (complain) { |
| 9794 | Diag(OpRangeForComplaining.getBegin(), DiagIDForComplaining) |
| 9795 | << ovl.Expression->getName() |
| 9796 | << DestTypeForComplaining |
| 9797 | << OpRangeForComplaining |
| 9798 | << ovl.Expression->getQualifierLoc().getSourceRange(); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9799 | NoteAllOverloadCandidates(SrcExpr.get()); |
| 9800 | |
| 9801 | SrcExpr = ExprError(); |
| 9802 | return true; |
| 9803 | } |
| 9804 | |
| 9805 | return false; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9806 | } |
| 9807 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9808 | SrcExpr = SingleFunctionExpression; |
| 9809 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9810 | } |
| 9811 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9812 | /// \brief Add a single candidate to the overload set. |
| 9813 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9814 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 9815 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9816 | ArrayRef<Expr *> Args, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9817 | OverloadCandidateSet &CandidateSet, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9818 | bool PartialOverloading, |
| 9819 | bool KnownValid) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9820 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9821 | if (isa<UsingShadowDecl>(Callee)) |
| 9822 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 9823 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9824 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9825 | if (ExplicitTemplateArgs) { |
| 9826 | assert(!KnownValid && "Explicit template arguments?"); |
| 9827 | return; |
| 9828 | } |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9829 | S.AddOverloadCandidate(Func, FoundDecl, Args, CandidateSet, false, |
| 9830 | PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9831 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9832 | } |
| 9833 | |
| 9834 | if (FunctionTemplateDecl *FuncTemplate |
| 9835 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9836 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9837 | ExplicitTemplateArgs, Args, CandidateSet); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9838 | return; |
| 9839 | } |
| 9840 | |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9841 | assert(!KnownValid && "unhandled case in overloaded call candidate"); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9842 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9843 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9844 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 9845 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9846 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9847 | ArrayRef<Expr *> Args, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9848 | OverloadCandidateSet &CandidateSet, |
| 9849 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9850 | |
| 9851 | #ifndef NDEBUG |
| 9852 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 9853 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9854 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9855 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 9856 | // and let Y be the lookup set produced by argument dependent |
| 9857 | // lookup (defined as follows). If X contains |
| 9858 | // |
| 9859 | // -- a declaration of a class member, or |
| 9860 | // |
| 9861 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9862 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9863 | // |
| 9864 | // -- a declaration that is neither a function or a function |
| 9865 | // template |
| 9866 | // |
| 9867 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9868 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9869 | if (ULE->requiresADL()) { |
| 9870 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 9871 | E = ULE->decls_end(); I != E; ++I) { |
| 9872 | assert(!(*I)->getDeclContext()->isRecord()); |
| 9873 | assert(isa<UsingShadowDecl>(*I) || |
| 9874 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 9875 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9876 | } |
| 9877 | } |
| 9878 | #endif |
| 9879 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9880 | // It would be nice to avoid this copy. |
| 9881 | TemplateArgumentListInfo TABuffer; |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 9882 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9883 | if (ULE->hasExplicitTemplateArgs()) { |
| 9884 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 9885 | ExplicitTemplateArgs = &TABuffer; |
| 9886 | } |
| 9887 | |
| 9888 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 9889 | E = ULE->decls_end(); I != E; ++I) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9890 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, Args, |
| 9891 | CandidateSet, PartialOverloading, |
| 9892 | /*KnownValid*/ true); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9893 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9894 | if (ULE->requiresADL()) |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9895 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
Richard Smith | f5cd5cc | 2012-02-25 06:24:24 +0000 | [diff] [blame] | 9896 | ULE->getExprLoc(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9897 | Args, ExplicitTemplateArgs, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 9898 | CandidateSet, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9899 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9900 | |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 9901 | /// Determine whether a declaration with the specified name could be moved into |
| 9902 | /// a different namespace. |
| 9903 | static bool canBeDeclaredInNamespace(const DeclarationName &Name) { |
| 9904 | switch (Name.getCXXOverloadedOperator()) { |
| 9905 | case OO_New: case OO_Array_New: |
| 9906 | case OO_Delete: case OO_Array_Delete: |
| 9907 | return false; |
| 9908 | |
| 9909 | default: |
| 9910 | return true; |
| 9911 | } |
| 9912 | } |
| 9913 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9914 | /// Attempt to recover from an ill-formed use of a non-dependent name in a |
| 9915 | /// template, where the non-dependent name was declared after the template |
| 9916 | /// was defined. This is common in code written for a compilers which do not |
| 9917 | /// correctly implement two-stage name lookup. |
| 9918 | /// |
| 9919 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 9920 | static bool |
| 9921 | DiagnoseTwoPhaseLookup(Sema &SemaRef, SourceLocation FnLoc, |
| 9922 | const CXXScopeSpec &SS, LookupResult &R, |
| 9923 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9924 | ArrayRef<Expr *> Args) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9925 | if (SemaRef.ActiveTemplateInstantiations.empty() || !SS.isEmpty()) |
| 9926 | return false; |
| 9927 | |
| 9928 | for (DeclContext *DC = SemaRef.CurContext; DC; DC = DC->getParent()) { |
Nick Lewycky | 5a7120c | 2012-03-14 20:41:00 +0000 | [diff] [blame] | 9929 | if (DC->isTransparentContext()) |
| 9930 | continue; |
| 9931 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9932 | SemaRef.LookupQualifiedName(R, DC); |
| 9933 | |
| 9934 | if (!R.empty()) { |
| 9935 | R.suppressDiagnostics(); |
| 9936 | |
| 9937 | if (isa<CXXRecordDecl>(DC)) { |
| 9938 | // Don't diagnose names we find in classes; we get much better |
| 9939 | // diagnostics for these from DiagnoseEmptyLookup. |
| 9940 | R.clear(); |
| 9941 | return false; |
| 9942 | } |
| 9943 | |
| 9944 | OverloadCandidateSet Candidates(FnLoc); |
| 9945 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 9946 | AddOverloadedCallCandidate(SemaRef, I.getPair(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9947 | ExplicitTemplateArgs, Args, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9948 | Candidates, false, /*KnownValid*/ false); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9949 | |
| 9950 | OverloadCandidateSet::iterator Best; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9951 | if (Candidates.BestViableFunction(SemaRef, FnLoc, Best) != OR_Success) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9952 | // No viable functions. Don't bother the user with notes for functions |
| 9953 | // which don't work and shouldn't be found anyway. |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9954 | R.clear(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9955 | return false; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9956 | } |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9957 | |
| 9958 | // Find the namespaces where ADL would have looked, and suggest |
| 9959 | // declaring the function there instead. |
| 9960 | Sema::AssociatedNamespaceSet AssociatedNamespaces; |
| 9961 | Sema::AssociatedClassSet AssociatedClasses; |
John McCall | 42f48fb | 2012-08-24 20:38:34 +0000 | [diff] [blame] | 9962 | SemaRef.FindAssociatedClassesAndNamespaces(FnLoc, Args, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9963 | AssociatedNamespaces, |
| 9964 | AssociatedClasses); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9965 | Sema::AssociatedNamespaceSet SuggestedNamespaces; |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 9966 | if (canBeDeclaredInNamespace(R.getLookupName())) { |
| 9967 | DeclContext *Std = SemaRef.getStdNamespace(); |
| 9968 | for (Sema::AssociatedNamespaceSet::iterator |
| 9969 | it = AssociatedNamespaces.begin(), |
| 9970 | end = AssociatedNamespaces.end(); it != end; ++it) { |
| 9971 | // Never suggest declaring a function within namespace 'std'. |
| 9972 | if (Std && Std->Encloses(*it)) |
| 9973 | continue; |
Richard Smith | 19e0d95 | 2012-12-22 02:46:14 +0000 | [diff] [blame] | 9974 | |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 9975 | // Never suggest declaring a function within a namespace with a |
| 9976 | // reserved name, like __gnu_cxx. |
| 9977 | NamespaceDecl *NS = dyn_cast<NamespaceDecl>(*it); |
| 9978 | if (NS && |
| 9979 | NS->getQualifiedNameAsString().find("__") != std::string::npos) |
| 9980 | continue; |
| 9981 | |
| 9982 | SuggestedNamespaces.insert(*it); |
| 9983 | } |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9984 | } |
| 9985 | |
| 9986 | SemaRef.Diag(R.getNameLoc(), diag::err_not_found_by_two_phase_lookup) |
| 9987 | << R.getLookupName(); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9988 | if (SuggestedNamespaces.empty()) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9989 | SemaRef.Diag(Best->Function->getLocation(), |
| 9990 | diag::note_not_found_by_two_phase_lookup) |
| 9991 | << R.getLookupName() << 0; |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9992 | } else if (SuggestedNamespaces.size() == 1) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9993 | SemaRef.Diag(Best->Function->getLocation(), |
| 9994 | diag::note_not_found_by_two_phase_lookup) |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9995 | << R.getLookupName() << 1 << *SuggestedNamespaces.begin(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9996 | } else { |
| 9997 | // FIXME: It would be useful to list the associated namespaces here, |
| 9998 | // but the diagnostics infrastructure doesn't provide a way to produce |
| 9999 | // a localized representation of a list of items. |
| 10000 | SemaRef.Diag(Best->Function->getLocation(), |
| 10001 | diag::note_not_found_by_two_phase_lookup) |
| 10002 | << R.getLookupName() << 2; |
| 10003 | } |
| 10004 | |
| 10005 | // Try to recover by calling this function. |
| 10006 | return true; |
| 10007 | } |
| 10008 | |
| 10009 | R.clear(); |
| 10010 | } |
| 10011 | |
| 10012 | return false; |
| 10013 | } |
| 10014 | |
| 10015 | /// Attempt to recover from ill-formed use of a non-dependent operator in a |
| 10016 | /// template, where the non-dependent operator was declared after the template |
| 10017 | /// was defined. |
| 10018 | /// |
| 10019 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 10020 | static bool |
| 10021 | DiagnoseTwoPhaseOperatorLookup(Sema &SemaRef, OverloadedOperatorKind Op, |
| 10022 | SourceLocation OpLoc, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10023 | ArrayRef<Expr *> Args) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10024 | DeclarationName OpName = |
| 10025 | SemaRef.Context.DeclarationNames.getCXXOperatorName(Op); |
| 10026 | LookupResult R(SemaRef, OpName, OpLoc, Sema::LookupOperatorName); |
| 10027 | return DiagnoseTwoPhaseLookup(SemaRef, OpLoc, CXXScopeSpec(), R, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10028 | /*ExplicitTemplateArgs=*/0, Args); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10029 | } |
| 10030 | |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 10031 | namespace { |
Richard Smith | e49ff3e | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 10032 | class BuildRecoveryCallExprRAII { |
| 10033 | Sema &SemaRef; |
| 10034 | public: |
| 10035 | BuildRecoveryCallExprRAII(Sema &S) : SemaRef(S) { |
| 10036 | assert(SemaRef.IsBuildingRecoveryCallExpr == false); |
| 10037 | SemaRef.IsBuildingRecoveryCallExpr = true; |
| 10038 | } |
| 10039 | |
| 10040 | ~BuildRecoveryCallExprRAII() { |
| 10041 | SemaRef.IsBuildingRecoveryCallExpr = false; |
| 10042 | } |
| 10043 | }; |
| 10044 | |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 10045 | } |
| 10046 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10047 | /// Attempts to recover from a call where no functions were found. |
| 10048 | /// |
| 10049 | /// Returns true if new candidates were found. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10050 | static ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 10051 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10052 | UnresolvedLookupExpr *ULE, |
| 10053 | SourceLocation LParenLoc, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10054 | llvm::MutableArrayRef<Expr *> Args, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10055 | SourceLocation RParenLoc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10056 | bool EmptyLookup, bool AllowTypoCorrection) { |
Richard Smith | e49ff3e | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 10057 | // Do not try to recover if it is already building a recovery call. |
| 10058 | // This stops infinite loops for template instantiations like |
| 10059 | // |
| 10060 | // template <typename T> auto foo(T t) -> decltype(foo(t)) {} |
| 10061 | // template <typename T> auto foo(T t) -> decltype(foo(&t)) {} |
| 10062 | // |
| 10063 | if (SemaRef.IsBuildingRecoveryCallExpr) |
| 10064 | return ExprError(); |
| 10065 | BuildRecoveryCallExprRAII RCE(SemaRef); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10066 | |
| 10067 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10068 | SS.Adopt(ULE->getQualifierLoc()); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10069 | SourceLocation TemplateKWLoc = ULE->getTemplateKeywordLoc(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10070 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10071 | TemplateArgumentListInfo TABuffer; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10072 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10073 | if (ULE->hasExplicitTemplateArgs()) { |
| 10074 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 10075 | ExplicitTemplateArgs = &TABuffer; |
| 10076 | } |
| 10077 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10078 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 10079 | Sema::LookupOrdinaryName); |
Kaelyn Uhrain | 761695f | 2013-07-08 23:13:39 +0000 | [diff] [blame] | 10080 | FunctionCallFilterCCC Validator(SemaRef, Args.size(), |
| 10081 | ExplicitTemplateArgs != 0); |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10082 | NoTypoCorrectionCCC RejectAll; |
| 10083 | CorrectionCandidateCallback *CCC = AllowTypoCorrection ? |
| 10084 | (CorrectionCandidateCallback*)&Validator : |
| 10085 | (CorrectionCandidateCallback*)&RejectAll; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10086 | if (!DiagnoseTwoPhaseLookup(SemaRef, Fn->getExprLoc(), SS, R, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10087 | ExplicitTemplateArgs, Args) && |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10088 | (!EmptyLookup || |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10089 | SemaRef.DiagnoseEmptyLookup(S, SS, R, *CCC, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10090 | ExplicitTemplateArgs, Args))) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10091 | return ExprError(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10092 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10093 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 10094 | |
| 10095 | // Build an implicit member call if appropriate. Just drop the |
| 10096 | // casts and such from the call, we don't really care. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10097 | ExprResult NewFn = ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10098 | if ((*R.begin())->isCXXClassMember()) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10099 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, |
| 10100 | R, ExplicitTemplateArgs); |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 10101 | else if (ExplicitTemplateArgs || TemplateKWLoc.isValid()) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10102 | NewFn = SemaRef.BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 10103 | ExplicitTemplateArgs); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10104 | else |
| 10105 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 10106 | |
| 10107 | if (NewFn.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10108 | return ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10109 | |
| 10110 | // This shouldn't cause an infinite loop because we're giving it |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10111 | // an expression with viable lookup results, which should never |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10112 | // end up here. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10113 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, NewFn.take(), LParenLoc, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10114 | MultiExprArg(Args.data(), Args.size()), |
| 10115 | RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10116 | } |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 10117 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10118 | /// \brief Constructs and populates an OverloadedCandidateSet from |
| 10119 | /// the given function. |
| 10120 | /// \returns true when an the ExprResult output parameter has been set. |
| 10121 | bool Sema::buildOverloadedCallSet(Scope *S, Expr *Fn, |
| 10122 | UnresolvedLookupExpr *ULE, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10123 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10124 | SourceLocation RParenLoc, |
| 10125 | OverloadCandidateSet *CandidateSet, |
| 10126 | ExprResult *Result) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10127 | #ifndef NDEBUG |
| 10128 | if (ULE->requiresADL()) { |
| 10129 | // To do ADL, we must have found an unqualified name. |
| 10130 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 10131 | |
| 10132 | // We don't perform ADL for implicit declarations of builtins. |
| 10133 | // Verify that this was correctly set up. |
| 10134 | FunctionDecl *F; |
| 10135 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 10136 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 10137 | F->getBuiltinID() && F->isImplicit()) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 10138 | llvm_unreachable("performing ADL for builtin"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10139 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10140 | // We don't perform ADL in C. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10141 | assert(getLangOpts().CPlusPlus && "ADL enabled in C"); |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 10142 | } |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10143 | #endif |
| 10144 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10145 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10146 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) { |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10147 | *Result = ExprError(); |
| 10148 | return true; |
| 10149 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 10150 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10151 | // Add the functions denoted by the callee to the set of candidate |
| 10152 | // functions, including those from argument-dependent lookup. |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10153 | AddOverloadedCallCandidates(ULE, Args, *CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10154 | |
| 10155 | // If we found nothing, try to recover. |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10156 | // BuildRecoveryCallExpr diagnoses the error itself, so we just bail |
| 10157 | // out if it fails. |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10158 | if (CandidateSet->empty()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10159 | // In Microsoft mode, if we are inside a template class member function then |
| 10160 | // create a type dependent CallExpr. The goal is to postpone name lookup |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 10161 | // to instantiation time to be able to search into type dependent base |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10162 | // classes. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10163 | if (getLangOpts().MicrosoftMode && CurContext->isDependentContext() && |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 10164 | (isa<FunctionDecl>(CurContext) || isa<CXXRecordDecl>(CurContext))) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10165 | CallExpr *CE = new (Context) CallExpr(Context, Fn, Args, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10166 | Context.DependentTy, VK_RValue, |
| 10167 | RParenLoc); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10168 | CE->setTypeDependent(true); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10169 | *Result = Owned(CE); |
| 10170 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10171 | } |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10172 | return false; |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 10173 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10174 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10175 | UnbridgedCasts.restore(); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10176 | return false; |
| 10177 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10178 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10179 | /// FinishOverloadedCallExpr - given an OverloadCandidateSet, builds and returns |
| 10180 | /// the completed call expression. If overload resolution fails, emits |
| 10181 | /// diagnostics and returns ExprError() |
| 10182 | static ExprResult FinishOverloadedCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
| 10183 | UnresolvedLookupExpr *ULE, |
| 10184 | SourceLocation LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10185 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10186 | SourceLocation RParenLoc, |
| 10187 | Expr *ExecConfig, |
| 10188 | OverloadCandidateSet *CandidateSet, |
| 10189 | OverloadCandidateSet::iterator *Best, |
| 10190 | OverloadingResult OverloadResult, |
| 10191 | bool AllowTypoCorrection) { |
| 10192 | if (CandidateSet->empty()) |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10193 | return BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10194 | RParenLoc, /*EmptyLookup=*/true, |
| 10195 | AllowTypoCorrection); |
| 10196 | |
| 10197 | switch (OverloadResult) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10198 | case OR_Success: { |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10199 | FunctionDecl *FDecl = (*Best)->Function; |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10200 | SemaRef.CheckUnresolvedLookupAccess(ULE, (*Best)->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 10201 | if (SemaRef.DiagnoseUseOfDecl(FDecl, ULE->getNameLoc())) |
| 10202 | return ExprError(); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10203 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10204 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
| 10205 | ExecConfig); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10206 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10207 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10208 | case OR_No_Viable_Function: { |
| 10209 | // Try to recover by looking for viable functions which the user might |
| 10210 | // have meant to call. |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10211 | ExprResult Recovery = BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10212 | Args, RParenLoc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10213 | /*EmptyLookup=*/false, |
| 10214 | AllowTypoCorrection); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10215 | if (!Recovery.isInvalid()) |
| 10216 | return Recovery; |
| 10217 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10218 | SemaRef.Diag(Fn->getLocStart(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10219 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10220 | << ULE->getName() << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10221 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, Args); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10222 | break; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10223 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10224 | |
| 10225 | case OR_Ambiguous: |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10226 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10227 | << ULE->getName() << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10228 | CandidateSet->NoteCandidates(SemaRef, OCD_ViableCandidates, Args); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10229 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10230 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10231 | case OR_Deleted: { |
| 10232 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_deleted_call) |
| 10233 | << (*Best)->Function->isDeleted() |
| 10234 | << ULE->getName() |
| 10235 | << SemaRef.getDeletedOrUnavailableSuffix((*Best)->Function) |
| 10236 | << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10237 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, Args); |
Argyrios Kyrtzidis | 0d579b6 | 2011-11-04 15:58:13 +0000 | [diff] [blame] | 10238 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10239 | // We emitted an error for the unvailable/deleted function call but keep |
| 10240 | // the call in the AST. |
| 10241 | FunctionDecl *FDecl = (*Best)->Function; |
| 10242 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10243 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
| 10244 | ExecConfig); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10245 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10246 | } |
| 10247 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 10248 | // Overload resolution failed. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10249 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10250 | } |
| 10251 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10252 | /// BuildOverloadedCallExpr - Given the call expression that calls Fn |
| 10253 | /// (which eventually refers to the declaration Func) and the call |
| 10254 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 10255 | /// to a specific function. If overload resolution succeeds, returns |
| 10256 | /// the call expression produced by overload resolution. |
| 10257 | /// Otherwise, emits diagnostics and returns ExprError. |
| 10258 | ExprResult Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, |
| 10259 | UnresolvedLookupExpr *ULE, |
| 10260 | SourceLocation LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10261 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10262 | SourceLocation RParenLoc, |
| 10263 | Expr *ExecConfig, |
| 10264 | bool AllowTypoCorrection) { |
| 10265 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
| 10266 | ExprResult result; |
| 10267 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10268 | if (buildOverloadedCallSet(S, Fn, ULE, Args, LParenLoc, &CandidateSet, |
| 10269 | &result)) |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10270 | return result; |
| 10271 | |
| 10272 | OverloadCandidateSet::iterator Best; |
| 10273 | OverloadingResult OverloadResult = |
| 10274 | CandidateSet.BestViableFunction(*this, Fn->getLocStart(), Best); |
| 10275 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10276 | return FinishOverloadedCallExpr(*this, S, Fn, ULE, LParenLoc, Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10277 | RParenLoc, ExecConfig, &CandidateSet, |
| 10278 | &Best, OverloadResult, |
| 10279 | AllowTypoCorrection); |
| 10280 | } |
| 10281 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10282 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 10283 | return Functions.size() > 1 || |
| 10284 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 10285 | } |
| 10286 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10287 | /// \brief Create a unary operation that may resolve to an overloaded |
| 10288 | /// operator. |
| 10289 | /// |
| 10290 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 10291 | /// |
| 10292 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 10293 | /// operator. |
| 10294 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 10295 | /// \param Fns The set of non-member functions that will be |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10296 | /// considered by overload resolution. The caller needs to build this |
| 10297 | /// set based on the context using, e.g., |
| 10298 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 10299 | /// set should not contain any member functions; those will be added |
| 10300 | /// by CreateOverloadedUnaryOp(). |
| 10301 | /// |
James Dennett | 8da1687 | 2012-06-22 10:32:46 +0000 | [diff] [blame] | 10302 | /// \param Input The input argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10303 | ExprResult |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10304 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 10305 | const UnresolvedSetImpl &Fns, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10306 | Expr *Input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10307 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10308 | |
| 10309 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 10310 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 10311 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10312 | // TODO: provide better source location info. |
| 10313 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10314 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10315 | if (checkPlaceholderForOverload(*this, Input)) |
| 10316 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10317 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10318 | Expr *Args[2] = { Input, 0 }; |
| 10319 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10320 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10321 | // For post-increment and post-decrement, add the implicit '0' as |
| 10322 | // the second argument, so that we know this is a post-increment or |
| 10323 | // post-decrement. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10324 | if (Opc == UO_PostInc || Opc == UO_PostDec) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10325 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 10326 | Args[1] = IntegerLiteral::Create(Context, Zero, Context.IntTy, |
| 10327 | SourceLocation()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10328 | NumArgs = 2; |
| 10329 | } |
| 10330 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10331 | ArrayRef<Expr *> ArgsArray(Args, NumArgs); |
| 10332 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10333 | if (Input->isTypeDependent()) { |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10334 | if (Fns.empty()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10335 | return Owned(new (Context) UnaryOperator(Input, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10336 | Opc, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10337 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10338 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10339 | OpLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10340 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10341 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10342 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 10343 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10344 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10345 | /*ADL*/ true, IsOverloaded(Fns), |
| 10346 | Fns.begin(), Fns.end()); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10347 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, ArgsArray, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10348 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10349 | VK_RValue, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10350 | OpLoc, false)); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10351 | } |
| 10352 | |
| 10353 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10354 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10355 | |
| 10356 | // Add the candidates from the given function set. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10357 | AddFunctionCandidates(Fns, ArgsArray, CandidateSet, false); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10358 | |
| 10359 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10360 | AddMemberOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10361 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10362 | // Add candidates from ADL. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10363 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, OpLoc, |
| 10364 | ArgsArray, /*ExplicitTemplateArgs*/ 0, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10365 | CandidateSet); |
| 10366 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10367 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10368 | AddBuiltinOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10369 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10370 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10371 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10372 | // Perform overload resolution. |
| 10373 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10374 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10375 | case OR_Success: { |
| 10376 | // We found a built-in operator or an overloaded operator. |
| 10377 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10378 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10379 | if (FnDecl) { |
| 10380 | // We matched an overloaded operator. Build a call to that |
| 10381 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10382 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10383 | // Convert the arguments. |
| 10384 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10385 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10386 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10387 | ExprResult InputRes = |
| 10388 | PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 10389 | Best->FoundDecl, Method); |
| 10390 | if (InputRes.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10391 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10392 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10393 | } else { |
| 10394 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10395 | ExprResult InputInit |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 10396 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10397 | Context, |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 10398 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10399 | SourceLocation(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10400 | Input); |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 10401 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10402 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10403 | Input = InputInit.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10404 | } |
| 10405 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10406 | // Determine the result type. |
| 10407 | QualType ResultTy = FnDecl->getResultType(); |
| 10408 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10409 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10410 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10411 | // Build the actual expression node. |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10412 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10413 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10414 | if (FnExpr.isInvalid()) |
| 10415 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10416 | |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 10417 | Args[0] = Input; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10418 | CallExpr *TheCall = |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10419 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), ArgsArray, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10420 | ResultTy, VK, OpLoc, false); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10421 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10422 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 10423 | FnDecl)) |
| 10424 | return ExprError(); |
| 10425 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10426 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10427 | } else { |
| 10428 | // We matched a built-in operator. Convert the arguments, then |
| 10429 | // break out so that we will build the appropriate built-in |
| 10430 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10431 | ExprResult InputRes = |
| 10432 | PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 10433 | Best->Conversions[0], AA_Passing); |
| 10434 | if (InputRes.isInvalid()) |
| 10435 | return ExprError(); |
| 10436 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10437 | break; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10438 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10439 | } |
| 10440 | |
| 10441 | case OR_No_Viable_Function: |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10442 | // This is an erroneous use of an operator which can be overloaded by |
| 10443 | // a non-member function. Check for non-member operators which were |
| 10444 | // defined too late to be candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10445 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, ArgsArray)) |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10446 | // FIXME: Recover by calling the found function. |
| 10447 | return ExprError(); |
| 10448 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10449 | // No viable function; fall through to handling this as a |
| 10450 | // built-in operator, which will produce an error message for us. |
| 10451 | break; |
| 10452 | |
| 10453 | case OR_Ambiguous: |
| 10454 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 10455 | << UnaryOperator::getOpcodeStr(Opc) |
| 10456 | << Input->getType() |
| 10457 | << Input->getSourceRange(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10458 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, ArgsArray, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10459 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
| 10460 | return ExprError(); |
| 10461 | |
| 10462 | case OR_Deleted: |
| 10463 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 10464 | << Best->Function->isDeleted() |
| 10465 | << UnaryOperator::getOpcodeStr(Opc) |
| 10466 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 10467 | << Input->getSourceRange(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10468 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, ArgsArray, |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 10469 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10470 | return ExprError(); |
| 10471 | } |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10472 | |
| 10473 | // Either we found no viable overloaded operator or we matched a |
| 10474 | // built-in operator. In either case, fall through to trying to |
| 10475 | // build a built-in operation. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10476 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10477 | } |
| 10478 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10479 | /// \brief Create a binary operation that may resolve to an overloaded |
| 10480 | /// operator. |
| 10481 | /// |
| 10482 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 10483 | /// |
| 10484 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 10485 | /// operator. |
| 10486 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 10487 | /// \param Fns The set of non-member functions that will be |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10488 | /// considered by overload resolution. The caller needs to build this |
| 10489 | /// set based on the context using, e.g., |
| 10490 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 10491 | /// set should not contain any member functions; those will be added |
| 10492 | /// by CreateOverloadedBinOp(). |
| 10493 | /// |
| 10494 | /// \param LHS Left-hand argument. |
| 10495 | /// \param RHS Right-hand argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10496 | ExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10497 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10498 | unsigned OpcIn, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10499 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10500 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10501 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10502 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10503 | |
| 10504 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 10505 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 10506 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 10507 | |
| 10508 | // If either side is type-dependent, create an appropriate dependent |
| 10509 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10510 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10511 | if (Fns.empty()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10512 | // If there are no functions to store, just build a dependent |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10513 | // BinaryOperator or CompoundAssignment. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10514 | if (Opc <= BO_Assign || Opc > BO_OrAssign) |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10515 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10516 | Context.DependentTy, |
| 10517 | VK_RValue, OK_Ordinary, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10518 | OpLoc, |
| 10519 | FPFeatures.fp_contract)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10520 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10521 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 10522 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10523 | VK_LValue, |
| 10524 | OK_Ordinary, |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10525 | Context.DependentTy, |
| 10526 | Context.DependentTy, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10527 | OpLoc, |
| 10528 | FPFeatures.fp_contract)); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10529 | } |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10530 | |
| 10531 | // FIXME: save results of ADL from here? |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10532 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10533 | // TODO: provide better source location info in DNLoc component. |
| 10534 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10535 | UnresolvedLookupExpr *Fn |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10536 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
| 10537 | NestedNameSpecifierLoc(), OpNameInfo, |
| 10538 | /*ADL*/ true, IsOverloaded(Fns), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10539 | Fns.begin(), Fns.end()); |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10540 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, Args, |
| 10541 | Context.DependentTy, VK_RValue, |
| 10542 | OpLoc, FPFeatures.fp_contract)); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10543 | } |
| 10544 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10545 | // Always do placeholder-like conversions on the RHS. |
| 10546 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 10547 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10548 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 10549 | // Do placeholder-like conversion on the LHS; note that we should |
| 10550 | // not get here with a PseudoObject LHS. |
| 10551 | assert(Args[0]->getObjectKind() != OK_ObjCProperty); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10552 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 10553 | return ExprError(); |
| 10554 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 10555 | // If this is the assignment operator, we only perform overload resolution |
| 10556 | // if the left-hand side is a class or enumeration type. This is actually |
| 10557 | // a hack. The standard requires that we do overload resolution between the |
| 10558 | // various built-in candidates, but as DR507 points out, this can lead to |
| 10559 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 10560 | // Note that we go the traditional code path for compound assignment forms. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10561 | if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10562 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10563 | |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10564 | // If this is the .* operator, which is not overloadable, just |
| 10565 | // create a built-in binary operator. |
| 10566 | if (Opc == BO_PtrMemD) |
| 10567 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 10568 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10569 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10570 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10571 | |
| 10572 | // Add the candidates from the given function set. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10573 | AddFunctionCandidates(Fns, Args, CandidateSet, false); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10574 | |
| 10575 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10576 | AddMemberOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10577 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10578 | // Add candidates from ADL. |
| 10579 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10580 | OpLoc, Args, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10581 | /*ExplicitTemplateArgs*/ 0, |
| 10582 | CandidateSet); |
| 10583 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10584 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10585 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10586 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10587 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10588 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10589 | // Perform overload resolution. |
| 10590 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10591 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 10592 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10593 | // We found a built-in operator or an overloaded operator. |
| 10594 | FunctionDecl *FnDecl = Best->Function; |
| 10595 | |
| 10596 | if (FnDecl) { |
| 10597 | // We matched an overloaded operator. Build a call to that |
| 10598 | // operator. |
| 10599 | |
| 10600 | // Convert the arguments. |
| 10601 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10602 | // Best->Access is only meaningful for class members. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10603 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10604 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10605 | ExprResult Arg1 = |
| 10606 | PerformCopyInitialization( |
| 10607 | InitializedEntity::InitializeParameter(Context, |
| 10608 | FnDecl->getParamDecl(0)), |
| 10609 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10610 | if (Arg1.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10611 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10612 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10613 | ExprResult Arg0 = |
| 10614 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 10615 | Best->FoundDecl, Method); |
| 10616 | if (Arg0.isInvalid()) |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10617 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10618 | Args[0] = Arg0.takeAs<Expr>(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10619 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10620 | } else { |
| 10621 | // Convert the arguments. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10622 | ExprResult Arg0 = PerformCopyInitialization( |
| 10623 | InitializedEntity::InitializeParameter(Context, |
| 10624 | FnDecl->getParamDecl(0)), |
| 10625 | SourceLocation(), Owned(Args[0])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10626 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10627 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10628 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10629 | ExprResult Arg1 = |
| 10630 | PerformCopyInitialization( |
| 10631 | InitializedEntity::InitializeParameter(Context, |
| 10632 | FnDecl->getParamDecl(1)), |
| 10633 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10634 | if (Arg1.isInvalid()) |
| 10635 | return ExprError(); |
| 10636 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 10637 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10638 | } |
| 10639 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10640 | // Determine the result type. |
| 10641 | QualType ResultTy = FnDecl->getResultType(); |
| 10642 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10643 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10644 | |
| 10645 | // Build the actual expression node. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10646 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10647 | Best->FoundDecl, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10648 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10649 | if (FnExpr.isInvalid()) |
| 10650 | return ExprError(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10651 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10652 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10653 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10654 | Args, ResultTy, VK, OpLoc, |
| 10655 | FPFeatures.fp_contract); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10656 | |
| 10657 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 10658 | FnDecl)) |
| 10659 | return ExprError(); |
| 10660 | |
Nick Lewycky | 9b7ea0d | 2013-01-24 02:03:08 +0000 | [diff] [blame] | 10661 | ArrayRef<const Expr *> ArgsArray(Args, 2); |
| 10662 | // Cut off the implicit 'this'. |
| 10663 | if (isa<CXXMethodDecl>(FnDecl)) |
| 10664 | ArgsArray = ArgsArray.slice(1); |
| 10665 | checkCall(FnDecl, ArgsArray, 0, isa<CXXMethodDecl>(FnDecl), OpLoc, |
| 10666 | TheCall->getSourceRange(), VariadicDoesNotApply); |
| 10667 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10668 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10669 | } else { |
| 10670 | // We matched a built-in operator. Convert the arguments, then |
| 10671 | // break out so that we will build the appropriate built-in |
| 10672 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10673 | ExprResult ArgsRes0 = |
| 10674 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 10675 | Best->Conversions[0], AA_Passing); |
| 10676 | if (ArgsRes0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10677 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10678 | Args[0] = ArgsRes0.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10679 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10680 | ExprResult ArgsRes1 = |
| 10681 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 10682 | Best->Conversions[1], AA_Passing); |
| 10683 | if (ArgsRes1.isInvalid()) |
| 10684 | return ExprError(); |
| 10685 | Args[1] = ArgsRes1.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10686 | break; |
| 10687 | } |
| 10688 | } |
| 10689 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10690 | case OR_No_Viable_Function: { |
| 10691 | // C++ [over.match.oper]p9: |
| 10692 | // If the operator is the operator , [...] and there are no |
| 10693 | // viable functions, then the operator is assumed to be the |
| 10694 | // built-in operator and interpreted according to clause 5. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10695 | if (Opc == BO_Comma) |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10696 | break; |
| 10697 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10698 | // For class as left operand for assignment or compound assigment |
| 10699 | // operator do not fall through to handling in built-in, but report that |
| 10700 | // no overloaded assignment operator found |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10701 | ExprResult Result = ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10702 | if (Args[0]->getType()->isRecordType() && |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10703 | Opc >= BO_Assign && Opc <= BO_OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 10704 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 10705 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10706 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10707 | } else { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10708 | // This is an erroneous use of an operator which can be overloaded by |
| 10709 | // a non-member function. Check for non-member operators which were |
| 10710 | // defined too late to be candidates. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10711 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, Args)) |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10712 | // FIXME: Recover by calling the found function. |
| 10713 | return ExprError(); |
| 10714 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10715 | // No viable function; try to create a built-in operation, which will |
| 10716 | // produce an error. Then, show the non-viable candidates. |
| 10717 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 10718 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10719 | assert(Result.isInvalid() && |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10720 | "C++ binary operator overloading is missing candidates!"); |
| 10721 | if (Result.isInvalid()) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10722 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10723 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10724 | return Result; |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10725 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10726 | |
| 10727 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10728 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_binary) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10729 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10730 | << Args[0]->getType() << Args[1]->getType() |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10731 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10732 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10733 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10734 | return ExprError(); |
| 10735 | |
| 10736 | case OR_Deleted: |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10737 | if (isImplicitlyDeleted(Best->Function)) { |
| 10738 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
| 10739 | Diag(OpLoc, diag::err_ovl_deleted_special_oper) |
Richard Smith | 0f46e64 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 10740 | << Context.getRecordType(Method->getParent()) |
| 10741 | << getSpecialMember(Method); |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 10742 | |
Richard Smith | 0f46e64 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 10743 | // The user probably meant to call this special member. Just |
| 10744 | // explain why it's deleted. |
| 10745 | NoteDeletedFunction(Method); |
| 10746 | return ExprError(); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10747 | } else { |
| 10748 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 10749 | << Best->Function->isDeleted() |
| 10750 | << BinaryOperator::getOpcodeStr(Opc) |
| 10751 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 10752 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10753 | } |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10754 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 10755 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10756 | return ExprError(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 10757 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10758 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10759 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10760 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10761 | } |
| 10762 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10763 | ExprResult |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10764 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 10765 | SourceLocation RLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10766 | Expr *Base, Expr *Idx) { |
| 10767 | Expr *Args[2] = { Base, Idx }; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10768 | DeclarationName OpName = |
| 10769 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 10770 | |
| 10771 | // If either side is type-dependent, create an appropriate dependent |
| 10772 | // expression. |
| 10773 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 10774 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10775 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10776 | // CHECKME: no 'operator' keyword? |
| 10777 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
| 10778 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10779 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 10780 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10781 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10782 | /*ADL*/ true, /*Overloaded*/ false, |
| 10783 | UnresolvedSetIterator(), |
| 10784 | UnresolvedSetIterator()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 10785 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10786 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10787 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10788 | Args, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10789 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10790 | VK_RValue, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10791 | RLoc, false)); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10792 | } |
| 10793 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10794 | // Handle placeholders on both operands. |
| 10795 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 10796 | return ExprError(); |
| 10797 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 10798 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10799 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10800 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10801 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10802 | |
| 10803 | // Subscript can only be overloaded as a member function. |
| 10804 | |
| 10805 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10806 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10807 | |
| 10808 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10809 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10810 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10811 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10812 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10813 | // Perform overload resolution. |
| 10814 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10815 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10816 | case OR_Success: { |
| 10817 | // We found a built-in operator or an overloaded operator. |
| 10818 | FunctionDecl *FnDecl = Best->Function; |
| 10819 | |
| 10820 | if (FnDecl) { |
| 10821 | // We matched an overloaded operator. Build a call to that |
| 10822 | // operator. |
| 10823 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10824 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10825 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10826 | // Convert the arguments. |
| 10827 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10828 | ExprResult Arg0 = |
| 10829 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 10830 | Best->FoundDecl, Method); |
| 10831 | if (Arg0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10832 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10833 | Args[0] = Arg0.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10834 | |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10835 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10836 | ExprResult InputInit |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10837 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10838 | Context, |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10839 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10840 | SourceLocation(), |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10841 | Owned(Args[1])); |
| 10842 | if (InputInit.isInvalid()) |
| 10843 | return ExprError(); |
| 10844 | |
| 10845 | Args[1] = InputInit.takeAs<Expr>(); |
| 10846 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10847 | // Determine the result type |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10848 | QualType ResultTy = FnDecl->getResultType(); |
| 10849 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10850 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10851 | |
| 10852 | // Build the actual expression node. |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10853 | DeclarationNameInfo OpLocInfo(OpName, LLoc); |
| 10854 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10855 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10856 | Best->FoundDecl, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10857 | HadMultipleCandidates, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10858 | OpLocInfo.getLoc(), |
| 10859 | OpLocInfo.getInfo()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10860 | if (FnExpr.isInvalid()) |
| 10861 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10862 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10863 | CXXOperatorCallExpr *TheCall = |
| 10864 | new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10865 | FnExpr.take(), Args, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10866 | ResultTy, VK, RLoc, |
| 10867 | false); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10868 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10869 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10870 | FnDecl)) |
| 10871 | return ExprError(); |
| 10872 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10873 | return MaybeBindToTemporary(TheCall); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10874 | } else { |
| 10875 | // We matched a built-in operator. Convert the arguments, then |
| 10876 | // break out so that we will build the appropriate built-in |
| 10877 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10878 | ExprResult ArgsRes0 = |
| 10879 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 10880 | Best->Conversions[0], AA_Passing); |
| 10881 | if (ArgsRes0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10882 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10883 | Args[0] = ArgsRes0.take(); |
| 10884 | |
| 10885 | ExprResult ArgsRes1 = |
| 10886 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 10887 | Best->Conversions[1], AA_Passing); |
| 10888 | if (ArgsRes1.isInvalid()) |
| 10889 | return ExprError(); |
| 10890 | Args[1] = ArgsRes1.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10891 | |
| 10892 | break; |
| 10893 | } |
| 10894 | } |
| 10895 | |
| 10896 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10897 | if (CandidateSet.empty()) |
| 10898 | Diag(LLoc, diag::err_ovl_no_oper) |
| 10899 | << Args[0]->getType() << /*subscript*/ 0 |
| 10900 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10901 | else |
| 10902 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 10903 | << Args[0]->getType() |
| 10904 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10905 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10906 | "[]", LLoc); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10907 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10908 | } |
| 10909 | |
| 10910 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10911 | Diag(LLoc, diag::err_ovl_ambiguous_oper_binary) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10912 | << "[]" |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10913 | << Args[0]->getType() << Args[1]->getType() |
| 10914 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10915 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10916 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10917 | return ExprError(); |
| 10918 | |
| 10919 | case OR_Deleted: |
| 10920 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 10921 | << Best->Function->isDeleted() << "[]" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 10922 | << getDeletedOrUnavailableSuffix(Best->Function) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10923 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10924 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10925 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10926 | return ExprError(); |
| 10927 | } |
| 10928 | |
| 10929 | // We matched a built-in operator; build it. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10930 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10931 | } |
| 10932 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10933 | /// BuildCallToMemberFunction - Build a call to a member |
| 10934 | /// function. MemExpr is the expression that refers to the member |
| 10935 | /// function (and includes the object parameter), Args/NumArgs are the |
| 10936 | /// arguments to the function call (not including the object |
| 10937 | /// parameter). The caller needs to validate that the member |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10938 | /// expression refers to a non-static member function or an overloaded |
| 10939 | /// member function. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10940 | ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10941 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10942 | SourceLocation LParenLoc, |
| 10943 | MultiExprArg Args, |
| 10944 | SourceLocation RParenLoc) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10945 | assert(MemExprE->getType() == Context.BoundMemberTy || |
| 10946 | MemExprE->getType() == Context.OverloadTy); |
| 10947 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10948 | // Dig out the member expression. This holds both the object |
| 10949 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10950 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10951 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10952 | // Determine whether this is a call to a pointer-to-member function. |
| 10953 | if (BinaryOperator *op = dyn_cast<BinaryOperator>(NakedMemExpr)) { |
| 10954 | assert(op->getType() == Context.BoundMemberTy); |
| 10955 | assert(op->getOpcode() == BO_PtrMemD || op->getOpcode() == BO_PtrMemI); |
| 10956 | |
| 10957 | QualType fnType = |
| 10958 | op->getRHS()->getType()->castAs<MemberPointerType>()->getPointeeType(); |
| 10959 | |
| 10960 | const FunctionProtoType *proto = fnType->castAs<FunctionProtoType>(); |
| 10961 | QualType resultType = proto->getCallResultType(Context); |
| 10962 | ExprValueKind valueKind = Expr::getValueKindForType(proto->getResultType()); |
| 10963 | |
| 10964 | // Check that the object type isn't more qualified than the |
| 10965 | // member function we're calling. |
| 10966 | Qualifiers funcQuals = Qualifiers::fromCVRMask(proto->getTypeQuals()); |
| 10967 | |
| 10968 | QualType objectType = op->getLHS()->getType(); |
| 10969 | if (op->getOpcode() == BO_PtrMemI) |
| 10970 | objectType = objectType->castAs<PointerType>()->getPointeeType(); |
| 10971 | Qualifiers objectQuals = objectType.getQualifiers(); |
| 10972 | |
| 10973 | Qualifiers difference = objectQuals - funcQuals; |
| 10974 | difference.removeObjCGCAttr(); |
| 10975 | difference.removeAddressSpace(); |
| 10976 | if (difference) { |
| 10977 | std::string qualsString = difference.getAsString(); |
| 10978 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
| 10979 | << fnType.getUnqualifiedType() |
| 10980 | << qualsString |
| 10981 | << (qualsString.find(' ') == std::string::npos ? 1 : 2); |
| 10982 | } |
| 10983 | |
| 10984 | CXXMemberCallExpr *call |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10985 | = new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10986 | resultType, valueKind, RParenLoc); |
| 10987 | |
| 10988 | if (CheckCallReturnType(proto->getResultType(), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10989 | op->getRHS()->getLocStart(), |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10990 | call, 0)) |
| 10991 | return ExprError(); |
| 10992 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10993 | if (ConvertArgumentsForCall(call, op, 0, proto, Args, RParenLoc)) |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10994 | return ExprError(); |
| 10995 | |
Richard Trieu | e2a90b8 | 2013-06-22 02:30:38 +0000 | [diff] [blame] | 10996 | if (CheckOtherCall(call, proto)) |
| 10997 | return ExprError(); |
| 10998 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10999 | return MaybeBindToTemporary(call); |
| 11000 | } |
| 11001 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11002 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11003 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11004 | return ExprError(); |
| 11005 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11006 | MemberExpr *MemExpr; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11007 | CXXMethodDecl *Method = 0; |
John McCall | bb6fb46 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 11008 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11009 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11010 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 11011 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11012 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11013 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11014 | Qualifier = MemExpr->getQualifier(); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11015 | UnbridgedCasts.restore(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11016 | } else { |
| 11017 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11018 | Qualifier = UnresExpr->getQualifier(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11019 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11020 | QualType ObjectType = UnresExpr->getBaseType(); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11021 | Expr::Classification ObjectClassification |
| 11022 | = UnresExpr->isArrow()? Expr::Classification::makeSimpleLValue() |
| 11023 | : UnresExpr->getBase()->Classify(Context); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11024 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11025 | // Add overload candidates |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11026 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11027 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11028 | // FIXME: avoid copy. |
| 11029 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 11030 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 11031 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11032 | TemplateArgs = &TemplateArgsBuffer; |
| 11033 | } |
| 11034 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11035 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 11036 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 11037 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11038 | NamedDecl *Func = *I; |
| 11039 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 11040 | if (isa<UsingShadowDecl>(Func)) |
| 11041 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 11042 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11043 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 11044 | // Microsoft supports direct constructor calls. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11045 | if (getLangOpts().MicrosoftExt && isa<CXXConstructorDecl>(Func)) { |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11046 | AddOverloadCandidate(cast<CXXConstructorDecl>(Func), I.getPair(), |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11047 | Args, CandidateSet); |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 11048 | } else if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 11049 | // If explicit template arguments were provided, we can't call a |
| 11050 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11051 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 11052 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11053 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11054 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11055 | ObjectClassification, Args, CandidateSet, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11056 | /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11057 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11058 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11059 | I.getPair(), ActingDC, TemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11060 | ObjectType, ObjectClassification, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11061 | Args, CandidateSet, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 11062 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11063 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 11064 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11065 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11066 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 11067 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11068 | UnbridgedCasts.restore(); |
| 11069 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11070 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11071 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getLocStart(), |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 11072 | Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11073 | case OR_Success: |
| 11074 | Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11075 | FoundDecl = Best->FoundDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11076 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 11077 | if (DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc())) |
| 11078 | return ExprError(); |
Faisal Vali | d570a92 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 11079 | // If FoundDecl is different from Method (such as if one is a template |
| 11080 | // and the other a specialization), make sure DiagnoseUseOfDecl is |
| 11081 | // called on both. |
| 11082 | // FIXME: This would be more comprehensively addressed by modifying |
| 11083 | // DiagnoseUseOfDecl to accept both the FoundDecl and the decl |
| 11084 | // being used. |
| 11085 | if (Method != FoundDecl.getDecl() && |
| 11086 | DiagnoseUseOfDecl(Method, UnresExpr->getNameLoc())) |
| 11087 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11088 | break; |
| 11089 | |
| 11090 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11091 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11092 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 11093 | << DeclName << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11094 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11095 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11096 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11097 | |
| 11098 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11099 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 11100 | << DeclName << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11101 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11102 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11103 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11104 | |
| 11105 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11106 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11107 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11108 | << DeclName |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11109 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11110 | << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11111 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11112 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11113 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11114 | } |
| 11115 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11116 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11117 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11118 | // If overload resolution picked a static member, build a |
| 11119 | // non-member call based on that function. |
| 11120 | if (Method->isStatic()) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11121 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, Args, |
| 11122 | RParenLoc); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11123 | } |
| 11124 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11125 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11126 | } |
| 11127 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11128 | QualType ResultType = Method->getResultType(); |
| 11129 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 11130 | ResultType = ResultType.getNonLValueExprType(Context); |
| 11131 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11132 | assert(Method && "Member call to something that isn't a method?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11133 | CXXMemberCallExpr *TheCall = |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11134 | new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11135 | ResultType, VK, RParenLoc); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11136 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 11137 | // Check for a valid return type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11138 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11139 | TheCall, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11140 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11141 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11142 | // Convert the object argument (for a non-static member function call). |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11143 | // We only need to do this if there was actually an overload; otherwise |
| 11144 | // it was done at lookup. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11145 | if (!Method->isStatic()) { |
| 11146 | ExprResult ObjectArg = |
| 11147 | PerformObjectArgumentInitialization(MemExpr->getBase(), Qualifier, |
| 11148 | FoundDecl, Method); |
| 11149 | if (ObjectArg.isInvalid()) |
| 11150 | return ExprError(); |
| 11151 | MemExpr->setBase(ObjectArg.take()); |
| 11152 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11153 | |
| 11154 | // Convert the rest of the arguments |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11155 | const FunctionProtoType *Proto = |
| 11156 | Method->getType()->getAs<FunctionProtoType>(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11157 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11158 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11159 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11160 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11161 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11162 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11163 | if (CheckFunctionCall(Method, TheCall, Proto)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11164 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 11165 | |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11166 | if ((isa<CXXConstructorDecl>(CurContext) || |
| 11167 | isa<CXXDestructorDecl>(CurContext)) && |
| 11168 | TheCall->getMethodDecl()->isPure()) { |
| 11169 | const CXXMethodDecl *MD = TheCall->getMethodDecl(); |
| 11170 | |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 11171 | if (isa<CXXThisExpr>(MemExpr->getBase()->IgnoreParenCasts())) { |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11172 | Diag(MemExpr->getLocStart(), |
| 11173 | diag::warn_call_to_pure_virtual_member_function_from_ctor_dtor) |
| 11174 | << MD->getDeclName() << isa<CXXDestructorDecl>(CurContext) |
| 11175 | << MD->getParent()->getDeclName(); |
| 11176 | |
| 11177 | Diag(MD->getLocStart(), diag::note_previous_decl) << MD->getDeclName(); |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 11178 | } |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11179 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11180 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11181 | } |
| 11182 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11183 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 11184 | /// type (C++ [over.call.object]), which can end up invoking an |
| 11185 | /// overloaded function call operator (@c operator()) or performing a |
| 11186 | /// user-defined conversion on the object argument. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 11187 | ExprResult |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11188 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 11189 | SourceLocation LParenLoc, |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11190 | MultiExprArg Args, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11191 | SourceLocation RParenLoc) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11192 | if (checkPlaceholderForOverload(*this, Obj)) |
| 11193 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11194 | ExprResult Object = Owned(Obj); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11195 | |
| 11196 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11197 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11198 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11199 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11200 | assert(Object.get()->getType()->isRecordType() && "Requires object type argument"); |
| 11201 | const RecordType *Record = Object.get()->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11202 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11203 | // C++ [over.call.object]p1: |
| 11204 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 11205 | // 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] | 11206 | // candidate functions includes at least the function call |
| 11207 | // operators of T. The function call operators of T are obtained by |
| 11208 | // ordinary lookup of the name operator() in the context of |
| 11209 | // (E).operator(). |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11210 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 11211 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11212 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11213 | if (RequireCompleteType(LParenLoc, Object.get()->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11214 | diag::err_incomplete_object_call, Object.get())) |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11215 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11216 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 11217 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 11218 | LookupQualifiedName(R, Record->getDecl()); |
| 11219 | R.suppressDiagnostics(); |
| 11220 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11221 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 11222 | Oper != OperEnd; ++Oper) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11223 | AddMethodCandidate(Oper.getPair(), Object.get()->getType(), |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11224 | Object.get()->Classify(Context), |
| 11225 | Args, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 11226 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 11227 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11228 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11229 | // C++ [over.call.object]p2: |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11230 | // In addition, for each (non-explicit in C++0x) conversion function |
| 11231 | // declared in T of the form |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11232 | // |
| 11233 | // operator conversion-type-id () cv-qualifier; |
| 11234 | // |
| 11235 | // where cv-qualifier is the same cv-qualification as, or a |
| 11236 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 11237 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 11238 | // R", or the type "reference to pointer to function of |
| 11239 | // (P1,...,Pn) returning R", or the type "reference to function |
| 11240 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11241 | // is also considered as a candidate function. Similarly, |
| 11242 | // surrogate call functions are added to the set of candidate |
| 11243 | // functions for each conversion function declared in an |
| 11244 | // accessible base class provided the function is not hidden |
| 11245 | // within T by another intervening declaration. |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 11246 | std::pair<CXXRecordDecl::conversion_iterator, |
| 11247 | CXXRecordDecl::conversion_iterator> Conversions |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 11248 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 11249 | for (CXXRecordDecl::conversion_iterator |
| 11250 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11251 | NamedDecl *D = *I; |
| 11252 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 11253 | if (isa<UsingShadowDecl>(D)) |
| 11254 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11255 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 11256 | // Skip over templated conversion functions; they aren't |
| 11257 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11258 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 11259 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 11260 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11261 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11262 | if (!Conv->isExplicit()) { |
| 11263 | // Strip the reference type (if any) and then the pointer type (if |
| 11264 | // any) to get down to what might be a function type. |
| 11265 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 11266 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 11267 | ConvType = ConvPtrType->getPointeeType(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11268 | |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11269 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
| 11270 | { |
| 11271 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11272 | Object.get(), Args, CandidateSet); |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11273 | } |
| 11274 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11275 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11276 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11277 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11278 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11279 | // Perform overload resolution. |
| 11280 | OverloadCandidateSet::iterator Best; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11281 | switch (CandidateSet.BestViableFunction(*this, Object.get()->getLocStart(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11282 | Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11283 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11284 | // Overload resolution succeeded; we'll build the appropriate call |
| 11285 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11286 | break; |
| 11287 | |
| 11288 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11289 | if (CandidateSet.empty()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11290 | Diag(Object.get()->getLocStart(), diag::err_ovl_no_oper) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11291 | << Object.get()->getType() << /*call*/ 1 |
| 11292 | << Object.get()->getSourceRange(); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11293 | else |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11294 | Diag(Object.get()->getLocStart(), |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11295 | diag::err_ovl_no_viable_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11296 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11297 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11298 | break; |
| 11299 | |
| 11300 | case OR_Ambiguous: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11301 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11302 | diag::err_ovl_ambiguous_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11303 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11304 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11305 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11306 | |
| 11307 | case OR_Deleted: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11308 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11309 | diag::err_ovl_deleted_object_call) |
| 11310 | << Best->Function->isDeleted() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11311 | << Object.get()->getType() |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11312 | << getDeletedOrUnavailableSuffix(Best->Function) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11313 | << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11314 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11315 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11316 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11317 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 11318 | if (Best == CandidateSet.end()) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11319 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11320 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11321 | UnbridgedCasts.restore(); |
| 11322 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11323 | if (Best->Function == 0) { |
| 11324 | // Since there is no function declaration, this is one of the |
| 11325 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11326 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11327 | = cast<CXXConversionDecl>( |
| 11328 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 11329 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11330 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 11331 | if (DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc)) |
| 11332 | return ExprError(); |
Faisal Vali | d570a92 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 11333 | assert(Conv == Best->FoundDecl.getDecl() && |
| 11334 | "Found Decl & conversion-to-functionptr should be same, right?!"); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11335 | // We selected one of the surrogate functions that converts the |
| 11336 | // object parameter to a function pointer. Perform the conversion |
| 11337 | // on the object argument, then let ActOnCallExpr finish the job. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11338 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 11339 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 11340 | // and then call it. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11341 | ExprResult Call = BuildCXXMemberCallExpr(Object.get(), Best->FoundDecl, |
| 11342 | Conv, HadMultipleCandidates); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 11343 | if (Call.isInvalid()) |
| 11344 | return ExprError(); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 11345 | // Record usage of conversion in an implicit cast. |
| 11346 | Call = Owned(ImplicitCastExpr::Create(Context, Call.get()->getType(), |
| 11347 | CK_UserDefinedConversion, |
| 11348 | Call.get(), 0, VK_RValue)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11349 | |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11350 | return ActOnCallExpr(S, Call.get(), LParenLoc, Args, RParenLoc); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11351 | } |
| 11352 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11353 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 11354 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11355 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 11356 | // that calls this method, using Object for the implicit object |
| 11357 | // parameter and passing along the remaining arguments. |
| 11358 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Nico Weber | e0ff690 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 11359 | |
| 11360 | // An error diagnostic has already been printed when parsing the declaration. |
Nico Weber | 1a52a4d | 2012-11-09 08:38:04 +0000 | [diff] [blame] | 11361 | if (Method->isInvalidDecl()) |
Nico Weber | e0ff690 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 11362 | return ExprError(); |
| 11363 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11364 | const FunctionProtoType *Proto = |
| 11365 | Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11366 | |
| 11367 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11368 | unsigned NumArgsToCheck = Args.size(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11369 | |
| 11370 | // Build the full argument list for the method call (the |
| 11371 | // implicit object parameter is placed at the beginning of the |
| 11372 | // list). |
| 11373 | Expr **MethodArgs; |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11374 | if (Args.size() < NumArgsInProto) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11375 | NumArgsToCheck = NumArgsInProto; |
| 11376 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 11377 | } else { |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11378 | MethodArgs = new Expr*[Args.size() + 1]; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11379 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11380 | MethodArgs[0] = Object.get(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11381 | for (unsigned ArgIdx = 0, e = Args.size(); ArgIdx != e; ++ArgIdx) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11382 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11383 | |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11384 | DeclarationNameInfo OpLocInfo( |
| 11385 | Context.DeclarationNames.getCXXOperatorName(OO_Call), LParenLoc); |
| 11386 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LParenLoc, RParenLoc)); |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11387 | ExprResult NewFn = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11388 | HadMultipleCandidates, |
| 11389 | OpLocInfo.getLoc(), |
| 11390 | OpLocInfo.getInfo()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11391 | if (NewFn.isInvalid()) |
| 11392 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11393 | |
| 11394 | // Once we've built TheCall, all of the expressions are properly |
| 11395 | // owned. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11396 | QualType ResultTy = Method->getResultType(); |
| 11397 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11398 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11399 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11400 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11401 | new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn.take(), |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11402 | llvm::makeArrayRef(MethodArgs, Args.size()+1), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 11403 | ResultTy, VK, RParenLoc, false); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11404 | delete [] MethodArgs; |
| 11405 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11406 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall, |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 11407 | Method)) |
| 11408 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11409 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11410 | // We may have default arguments. If so, we need to allocate more |
| 11411 | // slots in the call for them. |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11412 | if (Args.size() < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 11413 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11414 | else if (Args.size() > NumArgsInProto) |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11415 | NumArgsToCheck = NumArgsInProto; |
| 11416 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11417 | bool IsError = false; |
| 11418 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11419 | // Initialize the implicit object parameter. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11420 | ExprResult ObjRes = |
| 11421 | PerformObjectArgumentInitialization(Object.get(), /*Qualifier=*/0, |
| 11422 | Best->FoundDecl, Method); |
| 11423 | if (ObjRes.isInvalid()) |
| 11424 | IsError = true; |
| 11425 | else |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11426 | Object = ObjRes; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11427 | TheCall->setArg(0, Object.take()); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11428 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11429 | // Check the argument types. |
| 11430 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11431 | Expr *Arg; |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11432 | if (i < Args.size()) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11433 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11434 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11435 | // Pass the argument. |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11436 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11437 | ExprResult InputInit |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11438 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 11439 | Context, |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11440 | Method->getParamDecl(i)), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11441 | SourceLocation(), Arg); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11442 | |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11443 | IsError |= InputInit.isInvalid(); |
| 11444 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11445 | } else { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11446 | ExprResult DefArg |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 11447 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 11448 | if (DefArg.isInvalid()) { |
| 11449 | IsError = true; |
| 11450 | break; |
| 11451 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11452 | |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 11453 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11454 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11455 | |
| 11456 | TheCall->setArg(i + 1, Arg); |
| 11457 | } |
| 11458 | |
| 11459 | // If this is a variadic call, handle args passed through "...". |
| 11460 | if (Proto->isVariadic()) { |
| 11461 | // Promote the arguments (C99 6.5.2.2p7). |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11462 | for (unsigned i = NumArgsInProto, e = Args.size(); i < e; i++) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11463 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0); |
| 11464 | IsError |= Arg.isInvalid(); |
| 11465 | TheCall->setArg(i + 1, Arg.take()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11466 | } |
| 11467 | } |
| 11468 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11469 | if (IsError) return true; |
| 11470 | |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11471 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11472 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11473 | if (CheckFunctionCall(Method, TheCall, Proto)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 11474 | return true; |
| 11475 | |
John McCall | 182f709 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 11476 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11477 | } |
| 11478 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11479 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11480 | /// (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] | 11481 | /// @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] | 11482 | ExprResult |
Kaelyn Uhrain | baaeb85 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 11483 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc, |
| 11484 | bool *NoArrowOperatorFound) { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11485 | assert(Base->getType()->isRecordType() && |
| 11486 | "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11487 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11488 | if (checkPlaceholderForOverload(*this, Base)) |
| 11489 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11490 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11491 | SourceLocation Loc = Base->getExprLoc(); |
| 11492 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11493 | // C++ [over.ref]p1: |
| 11494 | // |
| 11495 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 11496 | // for a class object x of type T if T::operator->() exists and if |
| 11497 | // the operator is selected as the best match function by the |
| 11498 | // overload resolution mechanism (13.3). |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11499 | DeclarationName OpName = |
| 11500 | Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11501 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 11502 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11503 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11504 | if (RequireCompleteType(Loc, Base->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11505 | diag::err_typecheck_incomplete_tag, Base)) |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 11506 | return ExprError(); |
| 11507 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 11508 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 11509 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 11510 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 11511 | |
| 11512 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11513 | Oper != OperEnd; ++Oper) { |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11514 | AddMethodCandidate(Oper.getPair(), Base->getType(), Base->Classify(Context), |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 11515 | None, CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11516 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11517 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11518 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11519 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11520 | // Perform overload resolution. |
| 11521 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11522 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11523 | case OR_Success: |
| 11524 | // Overload resolution succeeded; we'll build the call below. |
| 11525 | break; |
| 11526 | |
| 11527 | case OR_No_Viable_Function: |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11528 | if (CandidateSet.empty()) { |
| 11529 | QualType BaseType = Base->getType(); |
Kaelyn Uhrain | baaeb85 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 11530 | if (NoArrowOperatorFound) { |
| 11531 | // Report this specific error to the caller instead of emitting a |
| 11532 | // diagnostic, as requested. |
| 11533 | *NoArrowOperatorFound = true; |
| 11534 | return ExprError(); |
| 11535 | } |
Kaelyn Uhrain | d422434 | 2013-07-15 19:54:54 +0000 | [diff] [blame] | 11536 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
| 11537 | << BaseType << Base->getSourceRange(); |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11538 | if (BaseType->isRecordType() && !BaseType->isPointerType()) { |
Kaelyn Uhrain | d422434 | 2013-07-15 19:54:54 +0000 | [diff] [blame] | 11539 | Diag(OpLoc, diag::note_typecheck_member_reference_suggestion) |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11540 | << FixItHint::CreateReplacement(OpLoc, "."); |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11541 | } |
| 11542 | } else |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11543 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11544 | << "operator->" << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11545 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11546 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11547 | |
| 11548 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 11549 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 11550 | << "->" << Base->getType() << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11551 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11552 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11553 | |
| 11554 | case OR_Deleted: |
| 11555 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 11556 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11557 | << "->" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11558 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11559 | << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11560 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11561 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11562 | } |
| 11563 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11564 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
| 11565 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11566 | // Convert the object parameter. |
| 11567 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11568 | ExprResult BaseResult = |
| 11569 | PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 11570 | Best->FoundDecl, Method); |
| 11571 | if (BaseResult.isInvalid()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11572 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11573 | Base = BaseResult.take(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 11574 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11575 | // Build the operator call. |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11576 | ExprResult FnExpr = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11577 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11578 | if (FnExpr.isInvalid()) |
| 11579 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11580 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11581 | QualType ResultTy = Method->getResultType(); |
| 11582 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11583 | ResultTy = ResultTy.getNonLValueExprType(Context); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11584 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11585 | new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr.take(), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 11586 | Base, ResultTy, VK, OpLoc, false); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 11587 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11588 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 11589 | Method)) |
| 11590 | return ExprError(); |
Eli Friedman | d593190 | 2011-04-04 01:18:25 +0000 | [diff] [blame] | 11591 | |
| 11592 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11593 | } |
| 11594 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11595 | /// BuildLiteralOperatorCall - Build a UserDefinedLiteral by creating a call to |
| 11596 | /// a literal operator described by the provided lookup results. |
| 11597 | ExprResult Sema::BuildLiteralOperatorCall(LookupResult &R, |
| 11598 | DeclarationNameInfo &SuffixInfo, |
| 11599 | ArrayRef<Expr*> Args, |
| 11600 | SourceLocation LitEndLoc, |
| 11601 | TemplateArgumentListInfo *TemplateArgs) { |
| 11602 | SourceLocation UDSuffixLoc = SuffixInfo.getCXXLiteralOperatorNameLoc(); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11603 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11604 | OverloadCandidateSet CandidateSet(UDSuffixLoc); |
| 11605 | AddFunctionCandidates(R.asUnresolvedSet(), Args, CandidateSet, true, |
| 11606 | TemplateArgs); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11607 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11608 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11609 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11610 | // Perform overload resolution. This will usually be trivial, but might need |
| 11611 | // to perform substitutions for a literal operator template. |
| 11612 | OverloadCandidateSet::iterator Best; |
| 11613 | switch (CandidateSet.BestViableFunction(*this, UDSuffixLoc, Best)) { |
| 11614 | case OR_Success: |
| 11615 | case OR_Deleted: |
| 11616 | break; |
| 11617 | |
| 11618 | case OR_No_Viable_Function: |
| 11619 | Diag(UDSuffixLoc, diag::err_ovl_no_viable_function_in_call) |
| 11620 | << R.getLookupName(); |
| 11621 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
| 11622 | return ExprError(); |
| 11623 | |
| 11624 | case OR_Ambiguous: |
| 11625 | Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) << R.getLookupName(); |
| 11626 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
| 11627 | return ExprError(); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11628 | } |
| 11629 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11630 | FunctionDecl *FD = Best->Function; |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11631 | ExprResult Fn = CreateFunctionRefExpr(*this, FD, Best->FoundDecl, |
| 11632 | HadMultipleCandidates, |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11633 | SuffixInfo.getLoc(), |
| 11634 | SuffixInfo.getInfo()); |
| 11635 | if (Fn.isInvalid()) |
| 11636 | return true; |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11637 | |
| 11638 | // Check the argument types. This should almost always be a no-op, except |
| 11639 | // that array-to-pointer decay is applied to string literals. |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11640 | Expr *ConvArgs[2]; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 11641 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11642 | ExprResult InputInit = PerformCopyInitialization( |
| 11643 | InitializedEntity::InitializeParameter(Context, FD->getParamDecl(ArgIdx)), |
| 11644 | SourceLocation(), Args[ArgIdx]); |
| 11645 | if (InputInit.isInvalid()) |
| 11646 | return true; |
| 11647 | ConvArgs[ArgIdx] = InputInit.take(); |
| 11648 | } |
| 11649 | |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11650 | QualType ResultTy = FD->getResultType(); |
| 11651 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11652 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11653 | |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11654 | UserDefinedLiteral *UDL = |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 11655 | new (Context) UserDefinedLiteral(Context, Fn.take(), |
| 11656 | llvm::makeArrayRef(ConvArgs, Args.size()), |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11657 | ResultTy, VK, LitEndLoc, UDSuffixLoc); |
| 11658 | |
| 11659 | if (CheckCallReturnType(FD->getResultType(), UDSuffixLoc, UDL, FD)) |
| 11660 | return ExprError(); |
| 11661 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11662 | if (CheckFunctionCall(FD, UDL, NULL)) |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11663 | return ExprError(); |
| 11664 | |
| 11665 | return MaybeBindToTemporary(UDL); |
| 11666 | } |
| 11667 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11668 | /// Build a call to 'begin' or 'end' for a C++11 for-range statement. If the |
| 11669 | /// given LookupResult is non-empty, it is assumed to describe a member which |
| 11670 | /// will be invoked. Otherwise, the function will be found via argument |
| 11671 | /// dependent lookup. |
| 11672 | /// CallExpr is set to a valid expression and FRS_Success returned on success, |
| 11673 | /// otherwise CallExpr is set to ExprError() and some non-success value |
| 11674 | /// is returned. |
| 11675 | Sema::ForRangeStatus |
| 11676 | Sema::BuildForRangeBeginEndCall(Scope *S, SourceLocation Loc, |
| 11677 | SourceLocation RangeLoc, VarDecl *Decl, |
| 11678 | BeginEndFunction BEF, |
| 11679 | const DeclarationNameInfo &NameInfo, |
| 11680 | LookupResult &MemberLookup, |
| 11681 | OverloadCandidateSet *CandidateSet, |
| 11682 | Expr *Range, ExprResult *CallExpr) { |
| 11683 | CandidateSet->clear(); |
| 11684 | if (!MemberLookup.empty()) { |
| 11685 | ExprResult MemberRef = |
| 11686 | BuildMemberReferenceExpr(Range, Range->getType(), Loc, |
| 11687 | /*IsPtr=*/false, CXXScopeSpec(), |
| 11688 | /*TemplateKWLoc=*/SourceLocation(), |
| 11689 | /*FirstQualifierInScope=*/0, |
| 11690 | MemberLookup, |
| 11691 | /*TemplateArgs=*/0); |
| 11692 | if (MemberRef.isInvalid()) { |
| 11693 | *CallExpr = ExprError(); |
| 11694 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11695 | << RangeLoc << BEF << Range->getType(); |
| 11696 | return FRS_DiagnosticIssued; |
| 11697 | } |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 11698 | *CallExpr = ActOnCallExpr(S, MemberRef.get(), Loc, None, Loc, 0); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11699 | if (CallExpr->isInvalid()) { |
| 11700 | *CallExpr = ExprError(); |
| 11701 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11702 | << RangeLoc << BEF << Range->getType(); |
| 11703 | return FRS_DiagnosticIssued; |
| 11704 | } |
| 11705 | } else { |
| 11706 | UnresolvedSet<0> FoundNames; |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11707 | UnresolvedLookupExpr *Fn = |
| 11708 | UnresolvedLookupExpr::Create(Context, /*NamingClass=*/0, |
| 11709 | NestedNameSpecifierLoc(), NameInfo, |
| 11710 | /*NeedsADL=*/true, /*Overloaded=*/false, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 11711 | FoundNames.begin(), FoundNames.end()); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11712 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11713 | bool CandidateSetError = buildOverloadedCallSet(S, Fn, Fn, Range, Loc, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11714 | CandidateSet, CallExpr); |
| 11715 | if (CandidateSet->empty() || CandidateSetError) { |
| 11716 | *CallExpr = ExprError(); |
| 11717 | return FRS_NoViableFunction; |
| 11718 | } |
| 11719 | OverloadCandidateSet::iterator Best; |
| 11720 | OverloadingResult OverloadResult = |
| 11721 | CandidateSet->BestViableFunction(*this, Fn->getLocStart(), Best); |
| 11722 | |
| 11723 | if (OverloadResult == OR_No_Viable_Function) { |
| 11724 | *CallExpr = ExprError(); |
| 11725 | return FRS_NoViableFunction; |
| 11726 | } |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11727 | *CallExpr = FinishOverloadedCallExpr(*this, S, Fn, Fn, Loc, Range, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11728 | Loc, 0, CandidateSet, &Best, |
| 11729 | OverloadResult, |
| 11730 | /*AllowTypoCorrection=*/false); |
| 11731 | if (CallExpr->isInvalid() || OverloadResult != OR_Success) { |
| 11732 | *CallExpr = ExprError(); |
| 11733 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11734 | << RangeLoc << BEF << Range->getType(); |
| 11735 | return FRS_DiagnosticIssued; |
| 11736 | } |
| 11737 | } |
| 11738 | return FRS_Success; |
| 11739 | } |
| 11740 | |
| 11741 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11742 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 11743 | /// a C++ overloaded function (possibly with some parentheses and |
| 11744 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 11745 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 11746 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 11747 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11748 | FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11749 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11750 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 11751 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11752 | if (SubExpr == PE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11753 | return PE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11754 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11755 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11756 | } |
| 11757 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11758 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11759 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 11760 | Found, Fn); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11761 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11762 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 11763 | "Implicit cast type cannot be determined from overload"); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 11764 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11765 | if (SubExpr == ICE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11766 | return ICE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11767 | |
| 11768 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 11769 | ICE->getCastKind(), |
| 11770 | SubExpr, 0, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 11771 | ICE->getValueKind()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11772 | } |
| 11773 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11774 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11775 | assert(UnOp->getOpcode() == UO_AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11776 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 11777 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 11778 | if (Method->isStatic()) { |
| 11779 | // Do nothing: static member functions aren't any different |
| 11780 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11781 | } else { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11782 | // Fix the sub expression, which really has to be an |
| 11783 | // UnresolvedLookupExpr holding an overloaded member function |
| 11784 | // or template. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11785 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 11786 | Found, Fn); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11787 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11788 | return UnOp; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11789 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11790 | assert(isa<DeclRefExpr>(SubExpr) |
| 11791 | && "fixed to something other than a decl ref"); |
| 11792 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 11793 | && "fixed to a member ref with no nested name qualifier"); |
| 11794 | |
| 11795 | // We have taken the address of a pointer to member |
| 11796 | // function. Perform the computation here so that we get the |
| 11797 | // appropriate pointer to member type. |
| 11798 | QualType ClassType |
| 11799 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 11800 | QualType MemPtrType |
| 11801 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 11802 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11803 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, MemPtrType, |
| 11804 | VK_RValue, OK_Ordinary, |
| 11805 | UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 11806 | } |
| 11807 | } |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11808 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 11809 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11810 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11811 | return UnOp; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11812 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11813 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11814 | Context.getPointerType(SubExpr->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11815 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11816 | UnOp->getOperatorLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11817 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11818 | |
| 11819 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11820 | // FIXME: avoid copy. |
| 11821 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11822 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11823 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11824 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11825 | } |
| 11826 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11827 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 11828 | ULE->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11829 | ULE->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11830 | Fn, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 11831 | /*enclosing*/ false, // FIXME? |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11832 | ULE->getNameLoc(), |
| 11833 | Fn->getType(), |
| 11834 | VK_LValue, |
| 11835 | Found.getDecl(), |
| 11836 | TemplateArgs); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 11837 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11838 | DRE->setHadMultipleCandidates(ULE->getNumDecls() > 1); |
| 11839 | return DRE; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11840 | } |
| 11841 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11842 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11843 | // FIXME: avoid copy. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11844 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 11845 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 11846 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11847 | TemplateArgs = &TemplateArgsBuffer; |
| 11848 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11849 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11850 | Expr *Base; |
| 11851 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11852 | // If we're filling in a static method where we used to have an |
| 11853 | // implicit member access, rewrite to a simple decl ref. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11854 | if (MemExpr->isImplicitAccess()) { |
| 11855 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11856 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 11857 | MemExpr->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11858 | MemExpr->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11859 | Fn, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 11860 | /*enclosing*/ false, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11861 | MemExpr->getMemberLoc(), |
| 11862 | Fn->getType(), |
| 11863 | VK_LValue, |
| 11864 | Found.getDecl(), |
| 11865 | TemplateArgs); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 11866 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11867 | DRE->setHadMultipleCandidates(MemExpr->getNumDecls() > 1); |
| 11868 | return DRE; |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 11869 | } else { |
| 11870 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 11871 | if (MemExpr->getQualifier()) |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 11872 | Loc = MemExpr->getQualifierLoc().getBeginLoc(); |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 11873 | CheckCXXThisCapture(Loc); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 11874 | Base = new (Context) CXXThisExpr(Loc, |
| 11875 | MemExpr->getBaseType(), |
| 11876 | /*isImplicit=*/true); |
| 11877 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11878 | } else |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11879 | Base = MemExpr->getBase(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11880 | |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11881 | ExprValueKind valueKind; |
| 11882 | QualType type; |
| 11883 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 11884 | valueKind = VK_LValue; |
| 11885 | type = Fn->getType(); |
| 11886 | } else { |
| 11887 | valueKind = VK_RValue; |
| 11888 | type = Context.BoundMemberTy; |
| 11889 | } |
| 11890 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11891 | MemberExpr *ME = MemberExpr::Create(Context, Base, |
| 11892 | MemExpr->isArrow(), |
| 11893 | MemExpr->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11894 | MemExpr->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11895 | Fn, |
| 11896 | Found, |
| 11897 | MemExpr->getMemberNameInfo(), |
| 11898 | TemplateArgs, |
| 11899 | type, valueKind, OK_Ordinary); |
| 11900 | ME->setHadMultipleCandidates(true); |
Richard Smith | 4a03022 | 2012-11-14 07:06:31 +0000 | [diff] [blame] | 11901 | MarkMemberReferenced(ME); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11902 | return ME; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11903 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11904 | |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11905 | llvm_unreachable("Invalid reference to overloaded function"); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11906 | } |
| 11907 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11908 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11909 | DeclAccessPair Found, |
| 11910 | FunctionDecl *Fn) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11911 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 11912 | } |
| 11913 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 11914 | } // end namespace clang |