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 { |
| 551 | // Structure used by OverloadCandidate::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 | }; |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 557 | // Structure used by OverloadCandidate::DeductionFailureInfo to store |
| 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. |
| 566 | OverloadCandidate::DeductionFailureInfo |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 567 | static MakeDeductionFailureInfo(ASTContext &Context, |
| 568 | Sema::TemplateDeductionResult TDK, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 569 | TemplateDeductionInfo &Info) { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 570 | OverloadCandidate::DeductionFailureInfo Result; |
| 571 | Result.Result = static_cast<unsigned>(TDK); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 572 | Result.HasDiagnostic = false; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 573 | Result.Data = 0; |
| 574 | switch (TDK) { |
| 575 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 576 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 577 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 578 | case Sema::TDK_TooManyArguments: |
| 579 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 580 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 581 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 582 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 583 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 584 | Result.Data = Info.Param.getOpaqueValue(); |
| 585 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 586 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 587 | case Sema::TDK_NonDeducedMismatch: { |
| 588 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 589 | DFIArguments *Saved = new (Context) DFIArguments; |
| 590 | Saved->FirstArg = Info.FirstArg; |
| 591 | Saved->SecondArg = Info.SecondArg; |
| 592 | Result.Data = Saved; |
| 593 | break; |
| 594 | } |
| 595 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 596 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 597 | case Sema::TDK_Underqualified: { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 598 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 599 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 600 | Saved->Param = Info.Param; |
| 601 | Saved->FirstArg = Info.FirstArg; |
| 602 | Saved->SecondArg = Info.SecondArg; |
| 603 | Result.Data = Saved; |
| 604 | break; |
| 605 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 606 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 607 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 608 | Result.Data = Info.take(); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 609 | if (Info.hasSFINAEDiagnostic()) { |
| 610 | PartialDiagnosticAt *Diag = new (Result.Diagnostic) PartialDiagnosticAt( |
| 611 | SourceLocation(), PartialDiagnostic::NullDiagnostic()); |
| 612 | Info.takeSFINAEDiagnostic(*Diag); |
| 613 | Result.HasDiagnostic = true; |
| 614 | } |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 615 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 616 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 617 | case Sema::TDK_FailedOverloadResolution: |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 618 | Result.Data = Info.Expression; |
| 619 | break; |
| 620 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 621 | case Sema::TDK_MiscellaneousDeductionFailure: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 622 | break; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 623 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 624 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 625 | return Result; |
| 626 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 627 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 628 | void OverloadCandidate::DeductionFailureInfo::Destroy() { |
| 629 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 630 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 631 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 632 | case Sema::TDK_InstantiationDepth: |
| 633 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 634 | case Sema::TDK_TooManyArguments: |
| 635 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 636 | case Sema::TDK_InvalidExplicitArguments: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 637 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 638 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 639 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 640 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 641 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 642 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | aaa045d | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 643 | // FIXME: Destroy the data? |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 644 | Data = 0; |
| 645 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 646 | |
| 647 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 648 | // FIXME: Destroy the template argument list? |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 649 | Data = 0; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 650 | if (PartialDiagnosticAt *Diag = getSFINAEDiagnostic()) { |
| 651 | Diag->~PartialDiagnosticAt(); |
| 652 | HasDiagnostic = false; |
| 653 | } |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 654 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 655 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 656 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 657 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 658 | break; |
| 659 | } |
| 660 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 661 | |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 662 | PartialDiagnosticAt * |
| 663 | OverloadCandidate::DeductionFailureInfo::getSFINAEDiagnostic() { |
| 664 | if (HasDiagnostic) |
| 665 | return static_cast<PartialDiagnosticAt*>(static_cast<void*>(Diagnostic)); |
| 666 | return 0; |
| 667 | } |
| 668 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 669 | TemplateParameter |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 670 | OverloadCandidate::DeductionFailureInfo::getTemplateParameter() { |
| 671 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 672 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 673 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 674 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 675 | case Sema::TDK_TooManyArguments: |
| 676 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 677 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 678 | case Sema::TDK_NonDeducedMismatch: |
| 679 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 680 | return TemplateParameter(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 681 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 682 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 683 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 684 | return TemplateParameter::getFromOpaqueValue(Data); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 685 | |
| 686 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 687 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 688 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 689 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 690 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 691 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 692 | break; |
| 693 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 694 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 695 | return TemplateParameter(); |
| 696 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 697 | |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 698 | TemplateArgumentList * |
| 699 | OverloadCandidate::DeductionFailureInfo::getTemplateArgumentList() { |
| 700 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 701 | case Sema::TDK_Success: |
| 702 | case Sema::TDK_Invalid: |
| 703 | case Sema::TDK_InstantiationDepth: |
| 704 | case Sema::TDK_TooManyArguments: |
| 705 | case Sema::TDK_TooFewArguments: |
| 706 | case Sema::TDK_Incomplete: |
| 707 | case Sema::TDK_InvalidExplicitArguments: |
| 708 | case Sema::TDK_Inconsistent: |
| 709 | case Sema::TDK_Underqualified: |
| 710 | case Sema::TDK_NonDeducedMismatch: |
| 711 | case Sema::TDK_FailedOverloadResolution: |
| 712 | return 0; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 713 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 714 | case Sema::TDK_SubstitutionFailure: |
| 715 | return static_cast<TemplateArgumentList*>(Data); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 716 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 717 | // Unhandled |
| 718 | case Sema::TDK_MiscellaneousDeductionFailure: |
| 719 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | return 0; |
| 723 | } |
| 724 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 725 | const TemplateArgument *OverloadCandidate::DeductionFailureInfo::getFirstArg() { |
| 726 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 727 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 728 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 729 | case Sema::TDK_InstantiationDepth: |
| 730 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 731 | case Sema::TDK_TooManyArguments: |
| 732 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 733 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 734 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 735 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 736 | return 0; |
| 737 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 738 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 739 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 740 | case Sema::TDK_NonDeducedMismatch: |
| 741 | return &static_cast<DFIArguments*>(Data)->FirstArg; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 742 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 743 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 744 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 745 | break; |
| 746 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 747 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 748 | return 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 749 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 750 | |
| 751 | const TemplateArgument * |
| 752 | OverloadCandidate::DeductionFailureInfo::getSecondArg() { |
| 753 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 754 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 755 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 756 | case Sema::TDK_InstantiationDepth: |
| 757 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 758 | case Sema::TDK_TooManyArguments: |
| 759 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 760 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 761 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 762 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 763 | return 0; |
| 764 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 765 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 766 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 767 | case Sema::TDK_NonDeducedMismatch: |
| 768 | return &static_cast<DFIArguments*>(Data)->SecondArg; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 769 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 770 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 771 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 772 | break; |
| 773 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 774 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 775 | return 0; |
| 776 | } |
| 777 | |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 778 | Expr * |
| 779 | OverloadCandidate::DeductionFailureInfo::getExpr() { |
| 780 | if (static_cast<Sema::TemplateDeductionResult>(Result) == |
| 781 | Sema::TDK_FailedOverloadResolution) |
| 782 | return static_cast<Expr*>(Data); |
| 783 | |
| 784 | return 0; |
| 785 | } |
| 786 | |
Benjamin Kramer | f5b132f | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 787 | void OverloadCandidateSet::destroyCandidates() { |
Richard Smith | e3898ac | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 788 | for (iterator i = begin(), e = end(); i != e; ++i) { |
Benjamin Kramer | 9e2822b | 2012-01-14 20:16:52 +0000 | [diff] [blame] | 789 | for (unsigned ii = 0, ie = i->NumConversions; ii != ie; ++ii) |
| 790 | i->Conversions[ii].~ImplicitConversionSequence(); |
Richard Smith | e3898ac | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 791 | if (!i->Viable && i->FailureKind == ovl_fail_bad_deduction) |
| 792 | i->DeductionFailure.Destroy(); |
| 793 | } |
Benjamin Kramer | f5b132f | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | void OverloadCandidateSet::clear() { |
| 797 | destroyCandidates(); |
Benjamin Kramer | 314f554 | 2012-01-14 19:31:39 +0000 | [diff] [blame] | 798 | NumInlineSequences = 0; |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 799 | Candidates.clear(); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 800 | Functions.clear(); |
| 801 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 802 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 803 | namespace { |
| 804 | class UnbridgedCastsSet { |
| 805 | struct Entry { |
| 806 | Expr **Addr; |
| 807 | Expr *Saved; |
| 808 | }; |
| 809 | SmallVector<Entry, 2> Entries; |
| 810 | |
| 811 | public: |
| 812 | void save(Sema &S, Expr *&E) { |
| 813 | assert(E->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 814 | Entry entry = { &E, E }; |
| 815 | Entries.push_back(entry); |
| 816 | E = S.stripARCUnbridgedCast(E); |
| 817 | } |
| 818 | |
| 819 | void restore() { |
| 820 | for (SmallVectorImpl<Entry>::iterator |
| 821 | i = Entries.begin(), e = Entries.end(); i != e; ++i) |
| 822 | *i->Addr = i->Saved; |
| 823 | } |
| 824 | }; |
| 825 | } |
| 826 | |
| 827 | /// checkPlaceholderForOverload - Do any interesting placeholder-like |
| 828 | /// preprocessing on the given expression. |
| 829 | /// |
| 830 | /// \param unbridgedCasts a collection to which to add unbridged casts; |
| 831 | /// without this, they will be immediately diagnosed as errors |
| 832 | /// |
| 833 | /// Return true on unrecoverable error. |
| 834 | static bool checkPlaceholderForOverload(Sema &S, Expr *&E, |
| 835 | UnbridgedCastsSet *unbridgedCasts = 0) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 836 | if (const BuiltinType *placeholder = E->getType()->getAsPlaceholderType()) { |
| 837 | // We can't handle overloaded expressions here because overload |
| 838 | // resolution might reasonably tweak them. |
| 839 | if (placeholder->getKind() == BuiltinType::Overload) return false; |
| 840 | |
| 841 | // If the context potentially accepts unbridged ARC casts, strip |
| 842 | // the unbridged cast and add it to the collection for later restoration. |
| 843 | if (placeholder->getKind() == BuiltinType::ARCUnbridgedCast && |
| 844 | unbridgedCasts) { |
| 845 | unbridgedCasts->save(S, E); |
| 846 | return false; |
| 847 | } |
| 848 | |
| 849 | // Go ahead and check everything else. |
| 850 | ExprResult result = S.CheckPlaceholderExpr(E); |
| 851 | if (result.isInvalid()) |
| 852 | return true; |
| 853 | |
| 854 | E = result.take(); |
| 855 | return false; |
| 856 | } |
| 857 | |
| 858 | // Nothing to do. |
| 859 | return false; |
| 860 | } |
| 861 | |
| 862 | /// checkArgPlaceholdersForOverload - Check a set of call operands for |
| 863 | /// placeholders. |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 864 | static bool checkArgPlaceholdersForOverload(Sema &S, |
| 865 | MultiExprArg Args, |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 866 | UnbridgedCastsSet &unbridged) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 867 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 868 | if (checkPlaceholderForOverload(S, Args[i], &unbridged)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 869 | return true; |
| 870 | |
| 871 | return false; |
| 872 | } |
| 873 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 874 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 875 | // overload of the declarations in Old. This routine returns false if |
| 876 | // New and Old cannot be overloaded, e.g., if New has the same |
| 877 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 878 | // declarations aren't functions (or function templates) at all. When |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 879 | // it does return false, MatchedDecl will point to the decl that New |
| 880 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 881 | // top of the underlying declaration. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 882 | // |
| 883 | // Example: Given the following input: |
| 884 | // |
| 885 | // void f(int, float); // #1 |
| 886 | // void f(int, int); // #2 |
| 887 | // int f(int, int); // #3 |
| 888 | // |
| 889 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 890 | // so IsOverload will not be used. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 891 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 892 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 893 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 894 | // (since they have different signatures), so this routine returns |
| 895 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 896 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 897 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 898 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 899 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 900 | // identical (return types of functions are not part of the |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 901 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 902 | // point to the FunctionDecl for #2. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 903 | // |
| 904 | // 'NewIsUsingShadowDecl' indicates that 'New' is being introduced |
| 905 | // into a class by a using declaration. The rules for whether to hide |
| 906 | // shadow declarations ignore some properties which otherwise figure |
| 907 | // into a function template's signature. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 908 | Sema::OverloadKind |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 909 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
| 910 | NamedDecl *&Match, bool NewIsUsingDecl) { |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 911 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 912 | I != E; ++I) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 913 | NamedDecl *OldD = *I; |
| 914 | |
| 915 | bool OldIsUsingDecl = false; |
| 916 | if (isa<UsingShadowDecl>(OldD)) { |
| 917 | OldIsUsingDecl = true; |
| 918 | |
| 919 | // We can always introduce two using declarations into the same |
| 920 | // context, even if they have identical signatures. |
| 921 | if (NewIsUsingDecl) continue; |
| 922 | |
| 923 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 924 | } |
| 925 | |
| 926 | // If either declaration was introduced by a using declaration, |
| 927 | // we'll need to use slightly different rules for matching. |
| 928 | // Essentially, these rules are the normal rules, except that |
| 929 | // function templates hide function templates with different |
| 930 | // return types or template parameter lists. |
| 931 | bool UseMemberUsingDeclRules = |
John McCall | 78037ac | 2013-04-03 21:19:47 +0000 | [diff] [blame] | 932 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord() && |
| 933 | !New->getFriendObjectKind(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 934 | |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 935 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 936 | if (!IsOverload(New, OldT->getTemplatedDecl(), UseMemberUsingDeclRules)) { |
| 937 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 938 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 939 | continue; |
| 940 | } |
| 941 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 942 | Match = *I; |
| 943 | return Ovl_Match; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 944 | } |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 945 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 946 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
| 947 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 948 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 949 | continue; |
| 950 | } |
| 951 | |
Rafael Espindola | 90cc390 | 2013-04-15 12:49:13 +0000 | [diff] [blame] | 952 | if (!shouldLinkPossiblyHiddenDecl(*I, New)) |
| 953 | continue; |
| 954 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 955 | Match = *I; |
| 956 | return Ovl_Match; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 957 | } |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 958 | } else if (isa<UsingDecl>(OldD)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 959 | // We can overload with these, which can show up when doing |
| 960 | // redeclaration checks for UsingDecls. |
| 961 | assert(Old.getLookupKind() == LookupUsingDeclName); |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 962 | } else if (isa<TagDecl>(OldD)) { |
| 963 | // We can always overload with tags by hiding them. |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 964 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 965 | // Optimistically assume that an unresolved using decl will |
| 966 | // overload; if it doesn't, we'll have to diagnose during |
| 967 | // template instantiation. |
| 968 | } else { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 969 | // (C++ 13p1): |
| 970 | // Only function declarations can be overloaded; object and type |
| 971 | // declarations cannot be overloaded. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 972 | Match = *I; |
| 973 | return Ovl_NonFunction; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 974 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 975 | } |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 976 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 977 | return Ovl_Overload; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 980 | static bool canBeOverloaded(const FunctionDecl &D) { |
| 981 | if (D.getAttr<OverloadableAttr>()) |
| 982 | return true; |
Rafael Espindola | d2fdd42 | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 983 | if (D.isExternC()) |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 984 | return false; |
Rafael Espindola | 7a525ac | 2013-01-12 01:47:40 +0000 | [diff] [blame] | 985 | |
| 986 | // Main cannot be overloaded (basic.start.main). |
| 987 | if (D.isMain()) |
| 988 | return false; |
| 989 | |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 990 | return true; |
| 991 | } |
| 992 | |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 993 | static bool shouldTryToOverload(Sema &S, FunctionDecl *New, FunctionDecl *Old, |
| 994 | bool UseUsingDeclRules) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 995 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 996 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 997 | |
| 998 | // C++ [temp.fct]p2: |
| 999 | // A function template can be overloaded with other function templates |
| 1000 | // and with normal (non-template) functions. |
| 1001 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 1002 | return true; |
| 1003 | |
| 1004 | // Is the function New an overload of the function Old? |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1005 | QualType OldQType = S.Context.getCanonicalType(Old->getType()); |
| 1006 | QualType NewQType = S.Context.getCanonicalType(New->getType()); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1007 | |
| 1008 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 1009 | // determine whether they are overloads. If we find any mismatch |
| 1010 | // in the signature, they are overloads. |
| 1011 | |
| 1012 | // If either of these functions is a K&R-style function (no |
| 1013 | // prototype), then we consider them to have matching signatures. |
| 1014 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 1015 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 1016 | return false; |
| 1017 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1018 | const FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 1019 | const FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1020 | |
| 1021 | // The signature of a function includes the types of its |
| 1022 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 1023 | // of the ellipsis; see C++ DR 357). |
| 1024 | if (OldQType != NewQType && |
| 1025 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 1026 | OldType->isVariadic() != NewType->isVariadic() || |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1027 | !S.FunctionArgTypesAreEqual(OldType, NewType))) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1028 | return true; |
| 1029 | |
| 1030 | // C++ [temp.over.link]p4: |
| 1031 | // The signature of a function template consists of its function |
| 1032 | // signature, its return type and its template parameter list. The names |
| 1033 | // of the template parameters are significant only for establishing the |
| 1034 | // relationship between the template parameters and the rest of the |
| 1035 | // signature. |
| 1036 | // |
| 1037 | // We check the return type and template parameter lists for function |
| 1038 | // templates first; the remaining checks follow. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 1039 | // |
| 1040 | // However, we don't consider either of these when deciding whether |
| 1041 | // a member introduced by a shadow declaration is hidden. |
| 1042 | if (!UseUsingDeclRules && NewTemplate && |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1043 | (!S.TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 1044 | OldTemplate->getTemplateParameters(), |
| 1045 | false, S.TPL_TemplateMatch) || |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1046 | OldType->getResultType() != NewType->getResultType())) |
| 1047 | return true; |
| 1048 | |
| 1049 | // If the function is a class member, its signature includes the |
Douglas Gregor | 57c9f4f | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 1050 | // 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] | 1051 | // |
| 1052 | // As part of this, also check whether one of the member functions |
| 1053 | // is static, in which case they are not overloads (C++ |
| 1054 | // 13.1p2). While not part of the definition of the signature, |
| 1055 | // this check is important to determine whether these functions |
| 1056 | // can be overloaded. |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1057 | CXXMethodDecl *OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 1058 | CXXMethodDecl *NewMethod = dyn_cast<CXXMethodDecl>(New); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1059 | if (OldMethod && NewMethod && |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1060 | !OldMethod->isStatic() && !NewMethod->isStatic()) { |
| 1061 | if (OldMethod->getRefQualifier() != NewMethod->getRefQualifier()) { |
| 1062 | if (!UseUsingDeclRules && |
| 1063 | (OldMethod->getRefQualifier() == RQ_None || |
| 1064 | NewMethod->getRefQualifier() == RQ_None)) { |
| 1065 | // C++0x [over.load]p2: |
| 1066 | // - Member function declarations with the same name and the same |
| 1067 | // parameter-type-list as well as member function template |
| 1068 | // declarations with the same name, the same parameter-type-list, and |
| 1069 | // the same template parameter lists cannot be overloaded if any of |
| 1070 | // them, but not all, have a ref-qualifier (8.3.5). |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1071 | S.Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1072 | << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1073 | S.Diag(OldMethod->getLocation(), diag::note_previous_declaration); |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1074 | } |
| 1075 | return true; |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1076 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1077 | |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1078 | // We may not have applied the implicit const for a constexpr member |
| 1079 | // function yet (because we haven't yet resolved whether this is a static |
| 1080 | // or non-static member function). Add it now, on the assumption that this |
| 1081 | // is a redeclaration of OldMethod. |
| 1082 | unsigned NewQuals = NewMethod->getTypeQualifiers(); |
Richard Smith | 714fcc1 | 2013-01-14 08:00:39 +0000 | [diff] [blame] | 1083 | if (NewMethod->isConstexpr() && !isa<CXXConstructorDecl>(NewMethod)) |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1084 | NewQuals |= Qualifiers::Const; |
| 1085 | if (OldMethod->getTypeQualifiers() != NewQuals) |
| 1086 | return true; |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1087 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1088 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1089 | // The signatures match; this is not an overload. |
| 1090 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1093 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| 1094 | bool UseUsingDeclRules) { |
| 1095 | if (!shouldTryToOverload(*this, New, Old, UseUsingDeclRules)) |
| 1096 | return false; |
| 1097 | |
| 1098 | // If both of the functions are extern "C", then they are not |
| 1099 | // overloads. |
| 1100 | if (!canBeOverloaded(*Old) && !canBeOverloaded(*New)) |
| 1101 | return false; |
| 1102 | |
| 1103 | return true; |
| 1104 | } |
| 1105 | |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 1106 | /// \brief Checks availability of the function depending on the current |
| 1107 | /// function context. Inside an unavailable function, unavailability is ignored. |
| 1108 | /// |
| 1109 | /// \returns true if \arg FD is unavailable and current context is inside |
| 1110 | /// an available function, false otherwise. |
| 1111 | bool Sema::isFunctionConsideredUnavailable(FunctionDecl *FD) { |
| 1112 | return FD->isUnavailable() && !cast<Decl>(CurContext)->isUnavailable(); |
| 1113 | } |
| 1114 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1115 | /// \brief Tries a user-defined conversion from From to ToType. |
| 1116 | /// |
| 1117 | /// Produces an implicit conversion sequence for when a standard conversion |
| 1118 | /// is not an option. See TryImplicitConversion for more information. |
| 1119 | static ImplicitConversionSequence |
| 1120 | TryUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 1121 | bool SuppressUserConversions, |
| 1122 | bool AllowExplicit, |
| 1123 | bool InOverloadResolution, |
| 1124 | bool CStyle, |
| 1125 | bool AllowObjCWritebackConversion) { |
| 1126 | ImplicitConversionSequence ICS; |
| 1127 | |
| 1128 | if (SuppressUserConversions) { |
| 1129 | // We're not in the case above, so there is no conversion that |
| 1130 | // we can perform. |
| 1131 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1132 | return ICS; |
| 1133 | } |
| 1134 | |
| 1135 | // Attempt user-defined conversion. |
| 1136 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 1137 | OverloadingResult UserDefResult |
| 1138 | = IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, Conversions, |
| 1139 | AllowExplicit); |
| 1140 | |
| 1141 | if (UserDefResult == OR_Success) { |
| 1142 | ICS.setUserDefined(); |
| 1143 | // C++ [over.ics.user]p4: |
| 1144 | // A conversion of an expression of class type to the same class |
| 1145 | // type is given Exact Match rank, and a conversion of an |
| 1146 | // expression of class type to a base class of that type is |
| 1147 | // given Conversion rank, in spite of the fact that a copy |
| 1148 | // constructor (i.e., a user-defined conversion function) is |
| 1149 | // called for those cases. |
| 1150 | if (CXXConstructorDecl *Constructor |
| 1151 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
| 1152 | QualType FromCanon |
| 1153 | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 1154 | QualType ToCanon |
| 1155 | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 1156 | if (Constructor->isCopyConstructor() && |
| 1157 | (FromCanon == ToCanon || S.IsDerivedFrom(FromCanon, ToCanon))) { |
| 1158 | // Turn this into a "standard" conversion sequence, so that it |
| 1159 | // gets ranked with standard conversion sequences. |
| 1160 | ICS.setStandard(); |
| 1161 | ICS.Standard.setAsIdentityConversion(); |
| 1162 | ICS.Standard.setFromType(From->getType()); |
| 1163 | ICS.Standard.setAllToTypes(ToType); |
| 1164 | ICS.Standard.CopyConstructor = Constructor; |
| 1165 | if (ToCanon != FromCanon) |
| 1166 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | // C++ [over.best.ics]p4: |
| 1171 | // However, when considering the argument of a user-defined |
| 1172 | // conversion function that is a candidate by 13.3.1.3 when |
| 1173 | // invoked for the copying of the temporary in the second step |
| 1174 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 1175 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 1176 | // ellipsis conversion sequences are allowed. |
| 1177 | if (SuppressUserConversions && ICS.isUserDefined()) { |
| 1178 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
| 1179 | } |
| 1180 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
| 1181 | ICS.setAmbiguous(); |
| 1182 | ICS.Ambiguous.setFromType(From->getType()); |
| 1183 | ICS.Ambiguous.setToType(ToType); |
| 1184 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 1185 | Cand != Conversions.end(); ++Cand) |
| 1186 | if (Cand->Viable) |
| 1187 | ICS.Ambiguous.addConversion(Cand->Function); |
| 1188 | } else { |
| 1189 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1190 | } |
| 1191 | |
| 1192 | return ICS; |
| 1193 | } |
| 1194 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1195 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 1196 | /// from the given expression (Expr) to the given type (ToType). This |
| 1197 | /// function returns an implicit conversion sequence that can be used |
| 1198 | /// to perform the initialization. Given |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1199 | /// |
| 1200 | /// void f(float f); |
| 1201 | /// void g(int i) { f(i); } |
| 1202 | /// |
| 1203 | /// this routine would produce an implicit conversion sequence to |
| 1204 | /// describe the initialization of f from i, which will be a standard |
| 1205 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 1206 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 1207 | // |
| 1208 | /// Note that this routine only determines how the conversion can be |
| 1209 | /// performed; it does not actually perform the conversion. As such, |
| 1210 | /// it will not produce any diagnostics if no conversion is available, |
| 1211 | /// but will instead return an implicit conversion sequence of kind |
| 1212 | /// "BadConversion". |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1213 | /// |
| 1214 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 1215 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1216 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 1217 | /// permitted. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1218 | /// |
| 1219 | /// \param AllowObjCWritebackConversion Whether we allow the Objective-C |
| 1220 | /// writeback conversion, which allows __autoreleasing id* parameters to |
| 1221 | /// be initialized with __strong id* or __weak id* arguments. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1222 | static ImplicitConversionSequence |
| 1223 | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
| 1224 | bool SuppressUserConversions, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1225 | bool AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1226 | bool InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1227 | bool CStyle, |
| 1228 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1229 | ImplicitConversionSequence ICS; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1230 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1231 | ICS.Standard, CStyle, AllowObjCWritebackConversion)){ |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1232 | ICS.setStandard(); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1233 | return ICS; |
| 1234 | } |
| 1235 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1236 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 1237 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1238 | return ICS; |
| 1239 | } |
| 1240 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1241 | // C++ [over.ics.user]p4: |
| 1242 | // A conversion of an expression of class type to the same class |
| 1243 | // type is given Exact Match rank, and a conversion of an |
| 1244 | // expression of class type to a base class of that type is |
| 1245 | // given Conversion rank, in spite of the fact that a copy/move |
| 1246 | // constructor (i.e., a user-defined conversion function) is |
| 1247 | // called for those cases. |
| 1248 | QualType FromType = From->getType(); |
| 1249 | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1250 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
| 1251 | S.IsDerivedFrom(FromType, ToType))) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1252 | ICS.setStandard(); |
| 1253 | ICS.Standard.setAsIdentityConversion(); |
| 1254 | ICS.Standard.setFromType(FromType); |
| 1255 | ICS.Standard.setAllToTypes(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1256 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1257 | // We don't actually check at this point whether there is a valid |
| 1258 | // copy/move constructor, since overloading just assumes that it |
| 1259 | // exists. When we actually perform initialization, we'll find the |
| 1260 | // appropriate constructor to copy the returned object, if needed. |
| 1261 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1262 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1263 | // Determine whether this is considered a derived-to-base conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1264 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1265 | ICS.Standard.Second = ICK_Derived_To_Base; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1266 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1267 | return ICS; |
| 1268 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1269 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1270 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 1271 | AllowExplicit, InOverloadResolution, CStyle, |
| 1272 | AllowObjCWritebackConversion); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1275 | ImplicitConversionSequence |
| 1276 | Sema::TryImplicitConversion(Expr *From, QualType ToType, |
| 1277 | bool SuppressUserConversions, |
| 1278 | bool AllowExplicit, |
| 1279 | bool InOverloadResolution, |
| 1280 | bool CStyle, |
| 1281 | bool AllowObjCWritebackConversion) { |
| 1282 | return clang::TryImplicitConversion(*this, From, ToType, |
| 1283 | SuppressUserConversions, AllowExplicit, |
| 1284 | InOverloadResolution, CStyle, |
| 1285 | AllowObjCWritebackConversion); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1288 | /// PerformImplicitConversion - Perform an implicit conversion of the |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1289 | /// expression From to the type ToType. Returns the |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1290 | /// converted expression. Flavor is the kind of conversion we're |
| 1291 | /// performing, used in the error message. If @p AllowExplicit, |
| 1292 | /// explicit user-defined conversions are permitted. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1293 | ExprResult |
| 1294 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1295 | AssignmentAction Action, bool AllowExplicit) { |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1296 | ImplicitConversionSequence ICS; |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1297 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1300 | ExprResult |
| 1301 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1302 | AssignmentAction Action, bool AllowExplicit, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1303 | ImplicitConversionSequence& ICS) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1304 | if (checkPlaceholderForOverload(*this, From)) |
| 1305 | return ExprError(); |
| 1306 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1307 | // Objective-C ARC: Determine whether we will allow the writeback conversion. |
| 1308 | bool AllowObjCWritebackConversion |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1309 | = getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1310 | (Action == AA_Passing || Action == AA_Sending); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1311 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1312 | ICS = clang::TryImplicitConversion(*this, From, ToType, |
| 1313 | /*SuppressUserConversions=*/false, |
| 1314 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1315 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1316 | /*CStyle=*/false, |
| 1317 | AllowObjCWritebackConversion); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1318 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 1319 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1320 | |
| 1321 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1322 | /// conversion that strips "noreturn" off the nested function type. |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1323 | bool Sema::IsNoReturnConversion(QualType FromType, QualType ToType, |
| 1324 | QualType &ResultTy) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1325 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1326 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1327 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1328 | // Permit the conversion F(t __attribute__((noreturn))) -> F(t) |
| 1329 | // where F adds one of the following at most once: |
| 1330 | // - a pointer |
| 1331 | // - a member pointer |
| 1332 | // - a block pointer |
| 1333 | CanQualType CanTo = Context.getCanonicalType(ToType); |
| 1334 | CanQualType CanFrom = Context.getCanonicalType(FromType); |
| 1335 | Type::TypeClass TyClass = CanTo->getTypeClass(); |
| 1336 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1337 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) { |
| 1338 | if (TyClass == Type::Pointer) { |
| 1339 | CanTo = CanTo.getAs<PointerType>()->getPointeeType(); |
| 1340 | CanFrom = CanFrom.getAs<PointerType>()->getPointeeType(); |
| 1341 | } else if (TyClass == Type::BlockPointer) { |
| 1342 | CanTo = CanTo.getAs<BlockPointerType>()->getPointeeType(); |
| 1343 | CanFrom = CanFrom.getAs<BlockPointerType>()->getPointeeType(); |
| 1344 | } else if (TyClass == Type::MemberPointer) { |
| 1345 | CanTo = CanTo.getAs<MemberPointerType>()->getPointeeType(); |
| 1346 | CanFrom = CanFrom.getAs<MemberPointerType>()->getPointeeType(); |
| 1347 | } else { |
| 1348 | return false; |
| 1349 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1350 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1351 | TyClass = CanTo->getTypeClass(); |
| 1352 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1353 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) |
| 1354 | return false; |
| 1355 | } |
| 1356 | |
| 1357 | const FunctionType *FromFn = cast<FunctionType>(CanFrom); |
| 1358 | FunctionType::ExtInfo EInfo = FromFn->getExtInfo(); |
| 1359 | if (!EInfo.getNoReturn()) return false; |
| 1360 | |
| 1361 | FromFn = Context.adjustFunctionType(FromFn, EInfo.withNoReturn(false)); |
| 1362 | assert(QualType(FromFn, 0).isCanonical()); |
| 1363 | if (QualType(FromFn, 0) != CanTo) return false; |
| 1364 | |
| 1365 | ResultTy = ToType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1366 | return true; |
| 1367 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1368 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1369 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 1370 | /// vector conversion. |
| 1371 | /// |
| 1372 | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
| 1373 | /// conversion. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1374 | static bool IsVectorConversion(ASTContext &Context, QualType FromType, |
| 1375 | QualType ToType, ImplicitConversionKind &ICK) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1376 | // We need at least one of these types to be a vector type to have a vector |
| 1377 | // conversion. |
| 1378 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
| 1379 | return false; |
| 1380 | |
| 1381 | // Identical types require no conversions. |
| 1382 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1383 | return false; |
| 1384 | |
| 1385 | // There are no conversions between extended vector types, only identity. |
| 1386 | if (ToType->isExtVectorType()) { |
| 1387 | // There are no conversions between extended vector types other than the |
| 1388 | // identity conversion. |
| 1389 | if (FromType->isExtVectorType()) |
| 1390 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1391 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1392 | // Vector splat from any arithmetic type to a vector. |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 1393 | if (FromType->isArithmeticType()) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1394 | ICK = ICK_Vector_Splat; |
| 1395 | return true; |
| 1396 | } |
| 1397 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1398 | |
| 1399 | // We can perform the conversion between vector types in the following cases: |
| 1400 | // 1)vector types are equivalent AltiVec and GCC vector types |
| 1401 | // 2)lax vector conversions are permitted and the vector types are of the |
| 1402 | // same size |
| 1403 | if (ToType->isVectorType() && FromType->isVectorType()) { |
| 1404 | if (Context.areCompatibleVectorTypes(FromType, ToType) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1405 | (Context.getLangOpts().LaxVectorConversions && |
Chandler Carruth | c45eb9c | 2010-08-08 05:02:51 +0000 | [diff] [blame] | 1406 | (Context.getTypeSize(FromType) == Context.getTypeSize(ToType)))) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1407 | ICK = ICK_Vector_Conversion; |
| 1408 | return true; |
| 1409 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1410 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1411 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1412 | return false; |
| 1413 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1414 | |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1415 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 1416 | bool InOverloadResolution, |
| 1417 | StandardConversionSequence &SCS, |
| 1418 | bool CStyle); |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1419 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1420 | /// IsStandardConversion - Determines whether there is a standard |
| 1421 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 1422 | /// expression From to the type ToType. Standard conversion sequences |
| 1423 | /// only consider non-class types; for conversions that involve class |
| 1424 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 1425 | /// contain the standard conversion sequence required to perform this |
| 1426 | /// conversion and this routine will return true. Otherwise, this |
| 1427 | /// routine will return false and the value of SCS is unspecified. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1428 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 1429 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1430 | StandardConversionSequence &SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1431 | bool CStyle, |
| 1432 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1433 | QualType FromType = From->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1434 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1435 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1436 | SCS.setAsIdentityConversion(); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1437 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1438 | SCS.IncompatibleObjC = false; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1439 | SCS.setFromType(FromType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1440 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1441 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1442 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1443 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1444 | if (FromType->isRecordType() || ToType->isRecordType()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1445 | if (S.getLangOpts().CPlusPlus) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1446 | return false; |
| 1447 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1448 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1451 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 1452 | // array-to-pointer conversion, or function-to-pointer conversion |
| 1453 | // (C++ 4p1). |
| 1454 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1455 | if (FromType == S.Context.OverloadTy) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1456 | DeclAccessPair AccessPair; |
| 1457 | if (FunctionDecl *Fn |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1458 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1459 | AccessPair)) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1460 | // We were able to resolve the address of the overloaded function, |
| 1461 | // so we can convert to the type of that function. |
| 1462 | FromType = Fn->getType(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1463 | |
| 1464 | // we can sometimes resolve &foo<int> regardless of ToType, so check |
| 1465 | // if the type matches (identity) or we are converting to bool |
| 1466 | if (!S.Context.hasSameUnqualifiedType( |
| 1467 | S.ExtractUnqualifiedFunctionType(ToType), FromType)) { |
| 1468 | QualType resultTy; |
| 1469 | // if the function type matches except for [[noreturn]], it's ok |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1470 | if (!S.IsNoReturnConversion(FromType, |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1471 | S.ExtractUnqualifiedFunctionType(ToType), resultTy)) |
| 1472 | // otherwise, only a boolean conversion is standard |
| 1473 | if (!ToType->isBooleanType()) |
| 1474 | return false; |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1475 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1476 | |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1477 | // Check if the "from" expression is taking the address of an overloaded |
| 1478 | // function and recompute the FromType accordingly. Take advantage of the |
| 1479 | // fact that non-static member functions *must* have such an address-of |
| 1480 | // expression. |
| 1481 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn); |
| 1482 | if (Method && !Method->isStatic()) { |
| 1483 | assert(isa<UnaryOperator>(From->IgnoreParens()) && |
| 1484 | "Non-unary operator on non-static member address"); |
| 1485 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() |
| 1486 | == UO_AddrOf && |
| 1487 | "Non-address-of operator on non-static member address"); |
| 1488 | const Type *ClassType |
| 1489 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 1490 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
Chandler Carruth | fc5c8fc | 2011-03-29 18:38:10 +0000 | [diff] [blame] | 1491 | } else if (isa<UnaryOperator>(From->IgnoreParens())) { |
| 1492 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() == |
| 1493 | UO_AddrOf && |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1494 | "Non-address-of operator for overloaded function expression"); |
| 1495 | FromType = S.Context.getPointerType(FromType); |
| 1496 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1497 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1498 | // Check that we've computed the proper type after overload resolution. |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1499 | assert(S.Context.hasSameType( |
| 1500 | FromType, |
| 1501 | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1502 | } else { |
| 1503 | return false; |
| 1504 | } |
Anders Carlsson | 2bd6250 | 2010-11-04 05:28:09 +0000 | [diff] [blame] | 1505 | } |
John McCall | 2148011 | 2011-08-30 00:57:29 +0000 | [diff] [blame] | 1506 | // Lvalue-to-rvalue conversion (C++11 4.1): |
| 1507 | // A glvalue (3.10) of a non-function, non-array type T can |
| 1508 | // be converted to a prvalue. |
| 1509 | bool argIsLValue = From->isGLValue(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1510 | if (argIsLValue && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1511 | !FromType->isFunctionType() && !FromType->isArrayType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1512 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1513 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1514 | |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1515 | // C11 6.3.2.1p2: |
| 1516 | // ... if the lvalue has atomic type, the value has the non-atomic version |
| 1517 | // of the type of the lvalue ... |
| 1518 | if (const AtomicType *Atomic = FromType->getAs<AtomicType>()) |
| 1519 | FromType = Atomic->getValueType(); |
| 1520 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1521 | // If T is a non-class type, the type of the rvalue is the |
| 1522 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1523 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 1524 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1525 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1526 | } else if (FromType->isArrayType()) { |
| 1527 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1528 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1529 | |
| 1530 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 1531 | // bound of T" can be converted to an rvalue of type "pointer to |
| 1532 | // T" (C++ 4.2p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1533 | FromType = S.Context.getArrayDecayedType(FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1534 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1535 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1536 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1537 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1538 | |
| 1539 | // For the purpose of ranking in overload resolution |
| 1540 | // (13.3.3.1.1), this conversion is considered an |
| 1541 | // array-to-pointer conversion followed by a qualification |
| 1542 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1543 | SCS.Second = ICK_Identity; |
| 1544 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1545 | SCS.QualificationIncludesObjCLifetime = false; |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1546 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1547 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1548 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1549 | } else if (FromType->isFunctionType() && argIsLValue) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1550 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1551 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1552 | |
| 1553 | // An lvalue of function type T can be converted to an rvalue of |
| 1554 | // type "pointer to T." The result is a pointer to the |
| 1555 | // function. (C++ 4.3p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1556 | FromType = S.Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1557 | } else { |
| 1558 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1559 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1560 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1561 | SCS.setToType(0, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1562 | |
| 1563 | // The second conversion can be an integral promotion, floating |
| 1564 | // point promotion, integral conversion, floating point conversion, |
| 1565 | // floating-integral conversion, pointer conversion, |
| 1566 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1567 | // For overloading in C, this can also be a "compatible-type" |
| 1568 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1569 | bool IncompatibleObjC = false; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1570 | ImplicitConversionKind SecondICK = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1571 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1572 | // The unqualified versions of the types are the same: there's no |
| 1573 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1574 | SCS.Second = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1575 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1576 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1577 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1578 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1579 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1580 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1581 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1582 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1583 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1584 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1585 | SCS.Second = ICK_Complex_Promotion; |
| 1586 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1587 | } else if (ToType->isBooleanType() && |
| 1588 | (FromType->isArithmeticType() || |
| 1589 | FromType->isAnyPointerType() || |
| 1590 | FromType->isBlockPointerType() || |
| 1591 | FromType->isMemberPointerType() || |
| 1592 | FromType->isNullPtrType())) { |
| 1593 | // Boolean conversions (C++ 4.12). |
| 1594 | SCS.Second = ICK_Boolean_Conversion; |
| 1595 | FromType = S.Context.BoolTy; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1596 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1597 | ToType->isIntegralType(S.Context)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1598 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1599 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1600 | FromType = ToType.getUnqualifiedType(); |
Richard Smith | 42860f1 | 2013-05-10 20:29:50 +0000 | [diff] [blame] | 1601 | } else if (FromType->isAnyComplexType() && ToType->isAnyComplexType()) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1602 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1603 | SCS.Second = ICK_Complex_Conversion; |
| 1604 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1605 | } else if ((FromType->isAnyComplexType() && ToType->isArithmeticType()) || |
| 1606 | (ToType->isAnyComplexType() && FromType->isArithmeticType())) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1607 | // Complex-real conversions (C99 6.3.1.7) |
| 1608 | SCS.Second = ICK_Complex_Real; |
| 1609 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1610 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1611 | // Floating point conversions (C++ 4.8). |
| 1612 | SCS.Second = ICK_Floating_Conversion; |
| 1613 | FromType = ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1614 | } else if ((FromType->isRealFloatingType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1615 | ToType->isIntegralType(S.Context)) || |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1616 | (FromType->isIntegralOrUnscopedEnumerationType() && |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1617 | ToType->isRealFloatingType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1618 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1619 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1620 | FromType = ToType.getUnqualifiedType(); |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1621 | } else if (S.IsBlockPointerConversion(FromType, ToType, FromType)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1622 | SCS.Second = ICK_Block_Pointer_Conversion; |
| 1623 | } else if (AllowObjCWritebackConversion && |
| 1624 | S.isObjCWritebackConversion(FromType, ToType, FromType)) { |
| 1625 | SCS.Second = ICK_Writeback_Conversion; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1626 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1627 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1628 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1629 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1630 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1631 | FromType = FromType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1632 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1633 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1634 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1635 | SCS.Second = ICK_Pointer_Member; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1636 | } else if (IsVectorConversion(S.Context, FromType, ToType, SecondICK)) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1637 | SCS.Second = SecondICK; |
| 1638 | FromType = ToType.getUnqualifiedType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1639 | } else if (!S.getLangOpts().CPlusPlus && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1640 | S.Context.typesAreCompatible(ToType, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1641 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1642 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1643 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1644 | } else if (S.IsNoReturnConversion(FromType, ToType, FromType)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1645 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1646 | SCS.Second = ICK_NoReturn_Adjustment; |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1647 | } else if (IsTransparentUnionStandardConversion(S, From, ToType, |
| 1648 | InOverloadResolution, |
| 1649 | SCS, CStyle)) { |
| 1650 | SCS.Second = ICK_TransparentUnionConversion; |
| 1651 | FromType = ToType; |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1652 | } else if (tryAtomicConversion(S, From, ToType, InOverloadResolution, SCS, |
| 1653 | CStyle)) { |
| 1654 | // tryAtomicConversion has updated the standard conversion sequence |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1655 | // appropriately. |
| 1656 | return true; |
Guy Benyei | 6959acd | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 1657 | } else if (ToType->isEventT() && |
| 1658 | From->isIntegerConstantExpr(S.getASTContext()) && |
| 1659 | (From->EvaluateKnownConstInt(S.getASTContext()) == 0)) { |
| 1660 | SCS.Second = ICK_Zero_Event_Conversion; |
| 1661 | FromType = ToType; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1662 | } else { |
| 1663 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1664 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1665 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1666 | SCS.setToType(1, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1667 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1668 | QualType CanonFrom; |
| 1669 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1670 | // The third conversion can be a qualification conversion (C++ 4p1). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1671 | bool ObjCLifetimeConversion; |
| 1672 | if (S.IsQualificationConversion(FromType, ToType, CStyle, |
| 1673 | ObjCLifetimeConversion)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1674 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1675 | SCS.QualificationIncludesObjCLifetime = ObjCLifetimeConversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1676 | FromType = ToType; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1677 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1678 | CanonTo = S.Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1679 | } else { |
| 1680 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1681 | SCS.Third = ICK_Identity; |
| 1682 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1683 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1684 | // [...] Any difference in top-level cv-qualification is |
| 1685 | // subsumed by the initialization itself and does not constitute |
| 1686 | // a conversion. [...] |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1687 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1688 | CanonTo = S.Context.getCanonicalType(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1689 | if (CanonFrom.getLocalUnqualifiedType() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1690 | == CanonTo.getLocalUnqualifiedType() && |
Matt Arsenault | 5509f37 | 2013-02-26 21:15:54 +0000 | [diff] [blame] | 1691 | CanonFrom.getLocalQualifiers() != CanonTo.getLocalQualifiers()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1692 | FromType = ToType; |
| 1693 | CanonFrom = CanonTo; |
| 1694 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1695 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1696 | SCS.setToType(2, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1697 | |
| 1698 | // If we have not converted the argument type to the parameter type, |
| 1699 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1700 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1701 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1702 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1703 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1704 | } |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1705 | |
| 1706 | static bool |
| 1707 | IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 1708 | QualType &ToType, |
| 1709 | bool InOverloadResolution, |
| 1710 | StandardConversionSequence &SCS, |
| 1711 | bool CStyle) { |
| 1712 | |
| 1713 | const RecordType *UT = ToType->getAsUnionType(); |
| 1714 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 1715 | return false; |
| 1716 | // The field to initialize within the transparent union. |
| 1717 | RecordDecl *UD = UT->getDecl(); |
| 1718 | // It's compatible if the expression matches any of the fields. |
| 1719 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1720 | itend = UD->field_end(); |
| 1721 | it != itend; ++it) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1722 | if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS, |
| 1723 | CStyle, /*ObjCWritebackConversion=*/false)) { |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1724 | ToType = it->getType(); |
| 1725 | return true; |
| 1726 | } |
| 1727 | } |
| 1728 | return false; |
| 1729 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1730 | |
| 1731 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1732 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1733 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1734 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1735 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1736 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1737 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1738 | if (!To) { |
| 1739 | return false; |
| 1740 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1741 | |
| 1742 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1743 | // unsigned short int can be converted to an rvalue of type int if |
| 1744 | // int can represent all the values of the source type; otherwise, |
| 1745 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1746 | // int (C++ 4.5p1). |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1747 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1748 | !FromType->isEnumeralType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1749 | if (// We can promote any signed, promotable integer type to an int |
| 1750 | (FromType->isSignedIntegerType() || |
| 1751 | // We can promote any unsigned integer type whose size is |
| 1752 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1753 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1754 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1755 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1758 | return To->getKind() == BuiltinType::UInt; |
| 1759 | } |
| 1760 | |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1761 | // C++11 [conv.prom]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1762 | // A prvalue of an unscoped enumeration type whose underlying type is not |
| 1763 | // fixed (7.2) can be converted to an rvalue a prvalue of the first of the |
| 1764 | // following types that can represent all the values of the enumeration |
| 1765 | // (i.e., the values in the range bmin to bmax as described in 7.2): int, |
| 1766 | // unsigned int, long int, unsigned long int, long long int, or unsigned |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1767 | // 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] | 1768 | // values of the enumeration, an rvalue a prvalue of an unscoped enumeration |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1769 | // 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] | 1770 | // with lowest integer conversion rank (4.13) greater than the rank of long |
| 1771 | // 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] | 1772 | // there are two such extended types, the signed one is chosen. |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1773 | // C++11 [conv.prom]p4: |
| 1774 | // A prvalue of an unscoped enumeration type whose underlying type is fixed |
| 1775 | // can be converted to a prvalue of its underlying type. Moreover, if |
| 1776 | // integral promotion can be applied to its underlying type, a prvalue of an |
| 1777 | // unscoped enumeration type whose underlying type is fixed can also be |
| 1778 | // converted to a prvalue of the promoted underlying type. |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1779 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
| 1780 | // C++0x 7.2p9: Note that this implicit enum to int conversion is not |
| 1781 | // provided for a scoped enumeration. |
| 1782 | if (FromEnumType->getDecl()->isScoped()) |
| 1783 | return false; |
| 1784 | |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1785 | // We can perform an integral promotion to the underlying type of the enum, |
| 1786 | // even if that's not the promoted type. |
| 1787 | if (FromEnumType->getDecl()->isFixed()) { |
| 1788 | QualType Underlying = FromEnumType->getDecl()->getIntegerType(); |
| 1789 | return Context.hasSameUnqualifiedType(Underlying, ToType) || |
| 1790 | IsIntegralPromotion(From, Underlying, ToType); |
| 1791 | } |
| 1792 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1793 | // We have already pre-calculated the promotion type, so this is trivial. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1794 | if (ToType->isIntegerType() && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1795 | !RequireCompleteType(From->getLocStart(), FromType, 0)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1796 | return Context.hasSameUnqualifiedType(ToType, |
| 1797 | FromEnumType->getDecl()->getPromotionType()); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1798 | } |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1799 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1800 | // C++0x [conv.prom]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1801 | // A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted |
| 1802 | // to an rvalue a prvalue of the first of the following types that can |
| 1803 | // represent all the values of its underlying type: int, unsigned int, |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1804 | // 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] | 1805 | // 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] | 1806 | // underlying type, an rvalue a prvalue of type char16_t, char32_t, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1807 | // 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] | 1808 | // type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1809 | if (FromType->isAnyCharacterType() && !FromType->isCharType() && |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1810 | ToType->isIntegerType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1811 | // Determine whether the type we're converting from is signed or |
| 1812 | // unsigned. |
David Majnemer | 0ad9231 | 2011-07-22 21:09:04 +0000 | [diff] [blame] | 1813 | bool FromIsSigned = FromType->isSignedIntegerType(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1814 | uint64_t FromSize = Context.getTypeSize(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1815 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1816 | // The types we'll try to promote to, in the appropriate |
| 1817 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | QualType PromoteTypes[6] = { |
| 1819 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1820 | Context.LongTy, Context.UnsignedLongTy , |
| 1821 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1822 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1823 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1824 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1825 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1826 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1827 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1828 | // We found the type that we can promote to. If this is the |
| 1829 | // type we wanted, we have a promotion. Otherwise, no |
| 1830 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1831 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1832 | } |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1837 | // rvalue of type int if int can represent all the values of the |
| 1838 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1839 | // unsigned int can represent all the values of the bit-field. If |
| 1840 | // the bit-field is larger yet, no integral promotion applies to |
| 1841 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1842 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1843 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1844 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1845 | using llvm::APSInt; |
| 1846 | if (From) |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 1847 | if (FieldDecl *MemberDecl = From->getSourceBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1848 | APSInt BitWidth; |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1849 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1850 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1851 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1852 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1853 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1854 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1855 | if (BitWidth < ToSize || |
| 1856 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1857 | return To->getKind() == BuiltinType::Int; |
| 1858 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1859 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1860 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1861 | // that fits into an unsigned int? |
| 1862 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1863 | return To->getKind() == BuiltinType::UInt; |
| 1864 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1865 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1866 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1867 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1868 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1869 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1870 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1871 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1872 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1873 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1874 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1875 | |
| 1876 | return false; |
| 1877 | } |
| 1878 | |
| 1879 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1880 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1881 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1882 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1883 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1884 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1885 | /// An rvalue of type float can be converted to an rvalue of type |
| 1886 | /// double. (C++ 4.6p1). |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1887 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1888 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1889 | return true; |
| 1890 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1891 | // C99 6.3.1.5p1: |
| 1892 | // When a float is promoted to double or long double, or a |
| 1893 | // double is promoted to long double [...]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1894 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1895 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1896 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1897 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1898 | return true; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1899 | |
| 1900 | // Half can be promoted to float. |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 1901 | if (!getLangOpts().NativeHalfType && |
| 1902 | FromBuiltin->getKind() == BuiltinType::Half && |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1903 | ToBuiltin->getKind() == BuiltinType::Float) |
| 1904 | return true; |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1905 | } |
| 1906 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1907 | return false; |
| 1908 | } |
| 1909 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1910 | /// \brief Determine if a conversion is a complex promotion. |
| 1911 | /// |
| 1912 | /// A complex promotion is defined as a complex -> complex conversion |
| 1913 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1914 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1915 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1916 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1917 | if (!FromComplex) |
| 1918 | return false; |
| 1919 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1920 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1921 | if (!ToComplex) |
| 1922 | return false; |
| 1923 | |
| 1924 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1925 | ToComplex->getElementType()) || |
| 1926 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1927 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1930 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1931 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1932 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1933 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1934 | /// the right set of qualifiers on its pointee. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1935 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1936 | static QualType |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1937 | BuildSimilarlyQualifiedPointerType(const Type *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1938 | QualType ToPointee, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1939 | ASTContext &Context, |
| 1940 | bool StripObjCLifetime = false) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1941 | assert((FromPtr->getTypeClass() == Type::Pointer || |
| 1942 | FromPtr->getTypeClass() == Type::ObjCObjectPointer) && |
| 1943 | "Invalid similarly-qualified pointer type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1944 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1945 | /// Conversions to 'id' subsume cv-qualifier conversions. |
| 1946 | if (ToType->isObjCIdType() || ToType->isObjCQualifiedIdType()) |
Douglas Gregor | 143c7ac | 2010-12-06 22:09:19 +0000 | [diff] [blame] | 1947 | return ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1948 | |
| 1949 | QualType CanonFromPointee |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1950 | = Context.getCanonicalType(FromPtr->getPointeeType()); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1951 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1952 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1953 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1954 | if (StripObjCLifetime) |
| 1955 | Quals.removeObjCLifetime(); |
| 1956 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1957 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1958 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1959 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1960 | if (!ToType.isNull()) |
Douglas Gregor | af7bea5 | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1961 | return ToType.getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1962 | |
| 1963 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1964 | // already. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1965 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1966 | return Context.getObjCObjectPointerType(ToPointee); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1967 | return Context.getPointerType(ToPointee); |
| 1968 | } |
| 1969 | |
| 1970 | // Just build a canonical type that has the right qualifiers. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1971 | QualType QualifiedCanonToPointee |
| 1972 | = Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1973 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1974 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1975 | return Context.getObjCObjectPointerType(QualifiedCanonToPointee); |
| 1976 | return Context.getPointerType(QualifiedCanonToPointee); |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1977 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1978 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1979 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1980 | bool InOverloadResolution, |
| 1981 | ASTContext &Context) { |
| 1982 | // Handle value-dependent integral null pointer constants correctly. |
| 1983 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1984 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1985 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1986 | return !InOverloadResolution; |
| 1987 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1988 | return Expr->isNullPointerConstant(Context, |
| 1989 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1990 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1991 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1992 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1993 | /// IsPointerConversion - Determines whether the conversion of the |
| 1994 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1995 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1996 | /// 4.10). If so, returns true and places the converted type (that |
| 1997 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1998 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1999 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 2000 | /// This routine also supports conversions to and from block pointers |
| 2001 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 2002 | /// pointers to interfaces. FIXME: Once we've determined the |
| 2003 | /// appropriate overloading rules for Objective-C, we may want to |
| 2004 | /// split the Objective-C checks into a different routine; however, |
| 2005 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2006 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 2007 | /// set if the conversion is an allowed Objective-C conversion that |
| 2008 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2009 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2010 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2011 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2012 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2013 | IncompatibleObjC = false; |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2014 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, |
| 2015 | IncompatibleObjC)) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2016 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2017 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2018 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 2019 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2020 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 2021 | ConvertedType = ToType; |
| 2022 | return true; |
| 2023 | } |
| 2024 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2025 | // Blocks: Block pointers can be converted to void*. |
| 2026 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2027 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2028 | ConvertedType = ToType; |
| 2029 | return true; |
| 2030 | } |
| 2031 | // Blocks: A null pointer constant can be converted to a block |
| 2032 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2033 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2034 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2035 | ConvertedType = ToType; |
| 2036 | return true; |
| 2037 | } |
| 2038 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2039 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 2040 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2041 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2042 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2043 | ConvertedType = ToType; |
| 2044 | return true; |
| 2045 | } |
| 2046 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2047 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2048 | if (!ToTypePtr) |
| 2049 | return false; |
| 2050 | |
| 2051 | // 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] | 2052 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2053 | ConvertedType = ToType; |
| 2054 | return true; |
| 2055 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2056 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2057 | // Beyond this point, both types need to be pointers |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2058 | // , including objective-c pointers. |
| 2059 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2060 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2061 | !getLangOpts().ObjCAutoRefCount) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2062 | ConvertedType = BuildSimilarlyQualifiedPointerType( |
| 2063 | FromType->getAs<ObjCObjectPointerType>(), |
| 2064 | ToPointeeType, |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2065 | ToType, Context); |
| 2066 | return true; |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2067 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2068 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2069 | if (!FromTypePtr) |
| 2070 | return false; |
| 2071 | |
| 2072 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2073 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2074 | // 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] | 2075 | // pointer conversion, so don't do all of the work below. |
| 2076 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
| 2077 | return false; |
| 2078 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2079 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 2080 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 2081 | // 4.10p2). |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 2082 | if (FromPointeeType->isIncompleteOrObjectType() && |
| 2083 | ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2084 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2085 | ToPointeeType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2086 | ToType, Context, |
| 2087 | /*StripObjCLifetime=*/true); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2088 | return true; |
| 2089 | } |
| 2090 | |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2091 | // MSVC allows implicit function to void* type conversion. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2092 | if (getLangOpts().MicrosoftExt && FromPointeeType->isFunctionType() && |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2093 | ToPointeeType->isVoidType()) { |
| 2094 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2095 | ToPointeeType, |
| 2096 | ToType, Context); |
| 2097 | return true; |
| 2098 | } |
| 2099 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2100 | // When we're overloading in C, we allow a special kind of pointer |
| 2101 | // conversion for compatible-but-not-identical pointee types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2102 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2103 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2104 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2105 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2106 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2107 | return true; |
| 2108 | } |
| 2109 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2110 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2111 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2112 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 2113 | // can be converted to an rvalue of type "pointer to cv B," where |
| 2114 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 2115 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 2116 | // necessitates this conversion is ill-formed. The result of the |
| 2117 | // conversion is a pointer to the base class sub-object of the |
| 2118 | // derived class object. The null pointer value is converted to |
| 2119 | // the null pointer value of the destination type. |
| 2120 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2121 | // Note that we do not check for ambiguity or inaccessibility |
| 2122 | // here. That is handled by CheckPointerConversion. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2123 | if (getLangOpts().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2124 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | bf1764c | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 2125 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2126 | !RequireCompleteType(From->getLocStart(), FromPointeeType, 0) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2127 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2128 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2129 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2130 | ToType, Context); |
| 2131 | return true; |
| 2132 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2133 | |
Fariborz Jahanian | 5da3c08 | 2011-04-14 20:33:36 +0000 | [diff] [blame] | 2134 | if (FromPointeeType->isVectorType() && ToPointeeType->isVectorType() && |
| 2135 | Context.areCompatibleVectorTypes(FromPointeeType, ToPointeeType)) { |
| 2136 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2137 | ToPointeeType, |
| 2138 | ToType, Context); |
| 2139 | return true; |
| 2140 | } |
| 2141 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2142 | return false; |
| 2143 | } |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2144 | |
| 2145 | /// \brief Adopt the given qualifiers for the given type. |
| 2146 | static QualType AdoptQualifiers(ASTContext &Context, QualType T, Qualifiers Qs){ |
| 2147 | Qualifiers TQs = T.getQualifiers(); |
| 2148 | |
| 2149 | // Check whether qualifiers already match. |
| 2150 | if (TQs == Qs) |
| 2151 | return T; |
| 2152 | |
| 2153 | if (Qs.compatiblyIncludes(TQs)) |
| 2154 | return Context.getQualifiedType(T, Qs); |
| 2155 | |
| 2156 | return Context.getQualifiedType(T.getUnqualifiedType(), Qs); |
| 2157 | } |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2158 | |
| 2159 | /// isObjCPointerConversion - Determines whether this is an |
| 2160 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 2161 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2162 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2163 | QualType& ConvertedType, |
| 2164 | bool &IncompatibleObjC) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2165 | if (!getLangOpts().ObjC1) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2166 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2167 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2168 | // The set of qualifiers on the type we're converting from. |
| 2169 | Qualifiers FromQualifiers = FromType.getQualifiers(); |
| 2170 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2171 | // First, we handle all conversions on ObjC object pointer types. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2172 | const ObjCObjectPointerType* ToObjCPtr = |
| 2173 | ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2174 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2175 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2176 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2177 | if (ToObjCPtr && FromObjCPtr) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2178 | // If the pointee types are the same (ignoring qualifications), |
| 2179 | // then this is not a pointer conversion. |
| 2180 | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
| 2181 | FromObjCPtr->getPointeeType())) |
| 2182 | return false; |
| 2183 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2184 | // Check for compatible |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2185 | // 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] | 2186 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2187 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2188 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2189 | return true; |
| 2190 | } |
| 2191 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2192 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2193 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2194 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 2195 | /*compare=*/false)) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2196 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2197 | return true; |
| 2198 | } |
| 2199 | // Objective C++: We're able to convert from a pointer to an |
| 2200 | // interface to a pointer to a different interface. |
| 2201 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2202 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 2203 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2204 | if (getLangOpts().CPlusPlus && LHS && RHS && |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2205 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 2206 | FromObjCPtr->getPointeeType())) |
| 2207 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2208 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2209 | ToObjCPtr->getPointeeType(), |
| 2210 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2211 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2212 | return true; |
| 2213 | } |
| 2214 | |
| 2215 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 2216 | // Okay: this is some kind of implicit downcast of Objective-C |
| 2217 | // interfaces, which is permitted. However, we're going to |
| 2218 | // complain about it. |
| 2219 | IncompatibleObjC = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2220 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2221 | ToObjCPtr->getPointeeType(), |
| 2222 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2223 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2224 | return true; |
| 2225 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2226 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2227 | // 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] | 2228 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2229 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2230 | ToPointeeType = ToCPtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2231 | else if (const BlockPointerType *ToBlockPtr = |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2232 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 2233 | // 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] | 2234 | // to a block pointer type. |
| 2235 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2236 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2237 | return true; |
| 2238 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2239 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2240 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2241 | else if (FromType->getAs<BlockPointerType>() && |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2242 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2243 | // 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] | 2244 | // pointer to any object. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2245 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2246 | return true; |
| 2247 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2248 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2249 | return false; |
| 2250 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2251 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2252 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2253 | FromPointeeType = FromCPtr->getPointeeType(); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2254 | else if (const BlockPointerType *FromBlockPtr = |
| 2255 | FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2256 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2257 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2258 | return false; |
| 2259 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2260 | // If we have pointers to pointers, recursively check whether this |
| 2261 | // is an Objective-C conversion. |
| 2262 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 2263 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2264 | IncompatibleObjC)) { |
| 2265 | // We always complain about this conversion. |
| 2266 | IncompatibleObjC = true; |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2267 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2268 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2269 | return true; |
| 2270 | } |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2271 | // Allow conversion of pointee being objective-c pointer to another one; |
| 2272 | // as in I* to id. |
| 2273 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 2274 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 2275 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2276 | IncompatibleObjC)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2277 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2278 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2279 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2280 | return true; |
| 2281 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2282 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2283 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2284 | // differences in the argument and result types are in Objective-C |
| 2285 | // pointer conversions. If so, we permit the conversion (but |
| 2286 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2287 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2288 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2289 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2290 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2291 | if (FromFunctionType && ToFunctionType) { |
| 2292 | // If the function types are exactly the same, this isn't an |
| 2293 | // Objective-C pointer conversion. |
| 2294 | if (Context.getCanonicalType(FromPointeeType) |
| 2295 | == Context.getCanonicalType(ToPointeeType)) |
| 2296 | return false; |
| 2297 | |
| 2298 | // Perform the quick checks that will tell us whether these |
| 2299 | // function types are obviously different. |
| 2300 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2301 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 2302 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 2303 | return false; |
| 2304 | |
| 2305 | bool HasObjCConversion = false; |
| 2306 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 2307 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 2308 | // Okay, the types match exactly. Nothing to do. |
| 2309 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 2310 | ToFunctionType->getResultType(), |
| 2311 | ConvertedType, IncompatibleObjC)) { |
| 2312 | // Okay, we have an Objective-C pointer conversion. |
| 2313 | HasObjCConversion = true; |
| 2314 | } else { |
| 2315 | // Function types are too different. Abort. |
| 2316 | return false; |
| 2317 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2318 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2319 | // Check argument types. |
| 2320 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2321 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2322 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2323 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2324 | if (Context.getCanonicalType(FromArgType) |
| 2325 | == Context.getCanonicalType(ToArgType)) { |
| 2326 | // Okay, the types match exactly. Nothing to do. |
| 2327 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 2328 | ConvertedType, IncompatibleObjC)) { |
| 2329 | // Okay, we have an Objective-C pointer conversion. |
| 2330 | HasObjCConversion = true; |
| 2331 | } else { |
| 2332 | // Argument types are too different. Abort. |
| 2333 | return false; |
| 2334 | } |
| 2335 | } |
| 2336 | |
| 2337 | if (HasObjCConversion) { |
| 2338 | // We had an Objective-C conversion. Allow this pointer |
| 2339 | // conversion, but complain about it. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2340 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2341 | IncompatibleObjC = true; |
| 2342 | return true; |
| 2343 | } |
| 2344 | } |
| 2345 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2346 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2347 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2348 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2349 | /// \brief Determine whether this is an Objective-C writeback conversion, |
| 2350 | /// used for parameter passing when performing automatic reference counting. |
| 2351 | /// |
| 2352 | /// \param FromType The type we're converting form. |
| 2353 | /// |
| 2354 | /// \param ToType The type we're converting to. |
| 2355 | /// |
| 2356 | /// \param ConvertedType The type that will be produced after applying |
| 2357 | /// this conversion. |
| 2358 | bool Sema::isObjCWritebackConversion(QualType FromType, QualType ToType, |
| 2359 | QualType &ConvertedType) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2360 | if (!getLangOpts().ObjCAutoRefCount || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2361 | Context.hasSameUnqualifiedType(FromType, ToType)) |
| 2362 | return false; |
| 2363 | |
| 2364 | // Parameter must be a pointer to __autoreleasing (with no other qualifiers). |
| 2365 | QualType ToPointee; |
| 2366 | if (const PointerType *ToPointer = ToType->getAs<PointerType>()) |
| 2367 | ToPointee = ToPointer->getPointeeType(); |
| 2368 | else |
| 2369 | return false; |
| 2370 | |
| 2371 | Qualifiers ToQuals = ToPointee.getQualifiers(); |
| 2372 | if (!ToPointee->isObjCLifetimeType() || |
| 2373 | ToQuals.getObjCLifetime() != Qualifiers::OCL_Autoreleasing || |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2374 | !ToQuals.withoutObjCLifetime().empty()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2375 | return false; |
| 2376 | |
| 2377 | // Argument must be a pointer to __strong to __weak. |
| 2378 | QualType FromPointee; |
| 2379 | if (const PointerType *FromPointer = FromType->getAs<PointerType>()) |
| 2380 | FromPointee = FromPointer->getPointeeType(); |
| 2381 | else |
| 2382 | return false; |
| 2383 | |
| 2384 | Qualifiers FromQuals = FromPointee.getQualifiers(); |
| 2385 | if (!FromPointee->isObjCLifetimeType() || |
| 2386 | (FromQuals.getObjCLifetime() != Qualifiers::OCL_Strong && |
| 2387 | FromQuals.getObjCLifetime() != Qualifiers::OCL_Weak)) |
| 2388 | return false; |
| 2389 | |
| 2390 | // Make sure that we have compatible qualifiers. |
| 2391 | FromQuals.setObjCLifetime(Qualifiers::OCL_Autoreleasing); |
| 2392 | if (!ToQuals.compatiblyIncludes(FromQuals)) |
| 2393 | return false; |
| 2394 | |
| 2395 | // Remove qualifiers from the pointee type we're converting from; they |
| 2396 | // aren't used in the compatibility check belong, and we'll be adding back |
| 2397 | // qualifiers (with __autoreleasing) if the compatibility check succeeds. |
| 2398 | FromPointee = FromPointee.getUnqualifiedType(); |
| 2399 | |
| 2400 | // The unqualified form of the pointee types must be compatible. |
| 2401 | ToPointee = ToPointee.getUnqualifiedType(); |
| 2402 | bool IncompatibleObjC; |
| 2403 | if (Context.typesAreCompatible(FromPointee, ToPointee)) |
| 2404 | FromPointee = ToPointee; |
| 2405 | else if (!isObjCPointerConversion(FromPointee, ToPointee, FromPointee, |
| 2406 | IncompatibleObjC)) |
| 2407 | return false; |
| 2408 | |
| 2409 | /// \brief Construct the type we're converting to, which is a pointer to |
| 2410 | /// __autoreleasing pointee. |
| 2411 | FromPointee = Context.getQualifiedType(FromPointee, FromQuals); |
| 2412 | ConvertedType = Context.getPointerType(FromPointee); |
| 2413 | return true; |
| 2414 | } |
| 2415 | |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2416 | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
| 2417 | QualType& ConvertedType) { |
| 2418 | QualType ToPointeeType; |
| 2419 | if (const BlockPointerType *ToBlockPtr = |
| 2420 | ToType->getAs<BlockPointerType>()) |
| 2421 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 2422 | else |
| 2423 | return false; |
| 2424 | |
| 2425 | QualType FromPointeeType; |
| 2426 | if (const BlockPointerType *FromBlockPtr = |
| 2427 | FromType->getAs<BlockPointerType>()) |
| 2428 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2429 | else |
| 2430 | return false; |
| 2431 | // We have pointer to blocks, check whether the only |
| 2432 | // differences in the argument and result types are in Objective-C |
| 2433 | // pointer conversions. If so, we permit the conversion. |
| 2434 | |
| 2435 | const FunctionProtoType *FromFunctionType |
| 2436 | = FromPointeeType->getAs<FunctionProtoType>(); |
| 2437 | const FunctionProtoType *ToFunctionType |
| 2438 | = ToPointeeType->getAs<FunctionProtoType>(); |
| 2439 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2440 | if (!FromFunctionType || !ToFunctionType) |
| 2441 | return false; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2442 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2443 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2444 | return true; |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2445 | |
| 2446 | // Perform the quick checks that will tell us whether these |
| 2447 | // function types are obviously different. |
| 2448 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2449 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
| 2450 | return false; |
| 2451 | |
| 2452 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
| 2453 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
| 2454 | if (FromEInfo != ToEInfo) |
| 2455 | return false; |
| 2456 | |
| 2457 | bool IncompatibleObjC = false; |
Fariborz Jahanian | 462dae5 | 2011-02-13 20:11:42 +0000 | [diff] [blame] | 2458 | if (Context.hasSameType(FromFunctionType->getResultType(), |
| 2459 | ToFunctionType->getResultType())) { |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2460 | // Okay, the types match exactly. Nothing to do. |
| 2461 | } else { |
| 2462 | QualType RHS = FromFunctionType->getResultType(); |
| 2463 | QualType LHS = ToFunctionType->getResultType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2464 | if ((!getLangOpts().CPlusPlus || !RHS->isRecordType()) && |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2465 | !RHS.hasQualifiers() && LHS.hasQualifiers()) |
| 2466 | LHS = LHS.getUnqualifiedType(); |
| 2467 | |
| 2468 | if (Context.hasSameType(RHS,LHS)) { |
| 2469 | // OK exact match. |
| 2470 | } else if (isObjCPointerConversion(RHS, LHS, |
| 2471 | ConvertedType, IncompatibleObjC)) { |
| 2472 | if (IncompatibleObjC) |
| 2473 | return false; |
| 2474 | // Okay, we have an Objective-C pointer conversion. |
| 2475 | } |
| 2476 | else |
| 2477 | return false; |
| 2478 | } |
| 2479 | |
| 2480 | // Check argument types. |
| 2481 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2482 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2483 | IncompatibleObjC = false; |
| 2484 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2485 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2486 | if (Context.hasSameType(FromArgType, ToArgType)) { |
| 2487 | // Okay, the types match exactly. Nothing to do. |
| 2488 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
| 2489 | ConvertedType, IncompatibleObjC)) { |
| 2490 | if (IncompatibleObjC) |
| 2491 | return false; |
| 2492 | // Okay, we have an Objective-C pointer conversion. |
| 2493 | } else |
| 2494 | // Argument types are too different. Abort. |
| 2495 | return false; |
| 2496 | } |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 2497 | if (LangOpts.ObjCAutoRefCount && |
| 2498 | !Context.FunctionTypesMatchOnNSConsumedAttrs(FromFunctionType, |
| 2499 | ToFunctionType)) |
| 2500 | return false; |
Fariborz Jahanian | f9d9527 | 2011-09-28 20:22:05 +0000 | [diff] [blame] | 2501 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2502 | ConvertedType = ToType; |
| 2503 | return true; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2506 | enum { |
| 2507 | ft_default, |
| 2508 | ft_different_class, |
| 2509 | ft_parameter_arity, |
| 2510 | ft_parameter_mismatch, |
| 2511 | ft_return_type, |
| 2512 | ft_qualifer_mismatch |
| 2513 | }; |
| 2514 | |
| 2515 | /// HandleFunctionTypeMismatch - Gives diagnostic information for differeing |
| 2516 | /// function types. Catches different number of parameter, mismatch in |
| 2517 | /// parameter types, and different return types. |
| 2518 | void Sema::HandleFunctionTypeMismatch(PartialDiagnostic &PDiag, |
| 2519 | QualType FromType, QualType ToType) { |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2520 | // If either type is not valid, include no extra info. |
| 2521 | if (FromType.isNull() || ToType.isNull()) { |
| 2522 | PDiag << ft_default; |
| 2523 | return; |
| 2524 | } |
| 2525 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2526 | // Get the function type from the pointers. |
| 2527 | if (FromType->isMemberPointerType() && ToType->isMemberPointerType()) { |
| 2528 | const MemberPointerType *FromMember = FromType->getAs<MemberPointerType>(), |
| 2529 | *ToMember = ToType->getAs<MemberPointerType>(); |
| 2530 | if (FromMember->getClass() != ToMember->getClass()) { |
| 2531 | PDiag << ft_different_class << QualType(ToMember->getClass(), 0) |
| 2532 | << QualType(FromMember->getClass(), 0); |
| 2533 | return; |
| 2534 | } |
| 2535 | FromType = FromMember->getPointeeType(); |
| 2536 | ToType = ToMember->getPointeeType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2539 | if (FromType->isPointerType()) |
| 2540 | FromType = FromType->getPointeeType(); |
| 2541 | if (ToType->isPointerType()) |
| 2542 | ToType = ToType->getPointeeType(); |
| 2543 | |
| 2544 | // Remove references. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2545 | FromType = FromType.getNonReferenceType(); |
| 2546 | ToType = ToType.getNonReferenceType(); |
| 2547 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2548 | // Don't print extra info for non-specialized template functions. |
| 2549 | if (FromType->isInstantiationDependentType() && |
| 2550 | !FromType->getAs<TemplateSpecializationType>()) { |
| 2551 | PDiag << ft_default; |
| 2552 | return; |
| 2553 | } |
| 2554 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2555 | // No extra info for same types. |
| 2556 | if (Context.hasSameType(FromType, ToType)) { |
| 2557 | PDiag << ft_default; |
| 2558 | return; |
| 2559 | } |
| 2560 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2561 | const FunctionProtoType *FromFunction = FromType->getAs<FunctionProtoType>(), |
| 2562 | *ToFunction = ToType->getAs<FunctionProtoType>(); |
| 2563 | |
| 2564 | // Both types need to be function types. |
| 2565 | if (!FromFunction || !ToFunction) { |
| 2566 | PDiag << ft_default; |
| 2567 | return; |
| 2568 | } |
| 2569 | |
| 2570 | if (FromFunction->getNumArgs() != ToFunction->getNumArgs()) { |
| 2571 | PDiag << ft_parameter_arity << ToFunction->getNumArgs() |
| 2572 | << FromFunction->getNumArgs(); |
| 2573 | return; |
| 2574 | } |
| 2575 | |
| 2576 | // Handle different parameter types. |
| 2577 | unsigned ArgPos; |
| 2578 | if (!FunctionArgTypesAreEqual(FromFunction, ToFunction, &ArgPos)) { |
| 2579 | PDiag << ft_parameter_mismatch << ArgPos + 1 |
| 2580 | << ToFunction->getArgType(ArgPos) |
| 2581 | << FromFunction->getArgType(ArgPos); |
| 2582 | return; |
| 2583 | } |
| 2584 | |
| 2585 | // Handle different return type. |
| 2586 | if (!Context.hasSameType(FromFunction->getResultType(), |
| 2587 | ToFunction->getResultType())) { |
| 2588 | PDiag << ft_return_type << ToFunction->getResultType() |
| 2589 | << FromFunction->getResultType(); |
| 2590 | return; |
| 2591 | } |
| 2592 | |
| 2593 | unsigned FromQuals = FromFunction->getTypeQuals(), |
| 2594 | ToQuals = ToFunction->getTypeQuals(); |
| 2595 | if (FromQuals != ToQuals) { |
| 2596 | PDiag << ft_qualifer_mismatch << ToQuals << FromQuals; |
| 2597 | return; |
| 2598 | } |
| 2599 | |
| 2600 | // Unable to find a difference, so add no extra info. |
| 2601 | PDiag << ft_default; |
| 2602 | } |
| 2603 | |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2604 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
Douglas Gregor | dec1cc4 | 2011-12-15 17:15:07 +0000 | [diff] [blame] | 2605 | /// for equality of their argument types. Caller has already checked that |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2606 | /// they have same number of arguments. This routine assumes that Objective-C |
| 2607 | /// pointer types which only differ in their protocol qualifiers are equal. |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 2608 | /// If the parameters are different, ArgPos will have the parameter index |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2609 | /// of the first different parameter. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2610 | bool Sema::FunctionArgTypesAreEqual(const FunctionProtoType *OldType, |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2611 | const FunctionProtoType *NewType, |
| 2612 | unsigned *ArgPos) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2613 | if (!getLangOpts().ObjC1) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2614 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 2615 | N = NewType->arg_type_begin(), |
| 2616 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
| 2617 | if (!Context.hasSameType(*O, *N)) { |
| 2618 | if (ArgPos) *ArgPos = O - OldType->arg_type_begin(); |
| 2619 | return false; |
| 2620 | } |
| 2621 | } |
| 2622 | return true; |
| 2623 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2624 | |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2625 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 2626 | N = NewType->arg_type_begin(), |
| 2627 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
| 2628 | QualType ToType = (*O); |
| 2629 | QualType FromType = (*N); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2630 | if (!Context.hasSameType(ToType, FromType)) { |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2631 | if (const PointerType *PTTo = ToType->getAs<PointerType>()) { |
| 2632 | if (const PointerType *PTFr = FromType->getAs<PointerType>()) |
Chandler Carruth | 0ee93de | 2010-05-06 00:15:06 +0000 | [diff] [blame] | 2633 | if ((PTTo->getPointeeType()->isObjCQualifiedIdType() && |
| 2634 | PTFr->getPointeeType()->isObjCQualifiedIdType()) || |
| 2635 | (PTTo->getPointeeType()->isObjCQualifiedClassType() && |
| 2636 | PTFr->getPointeeType()->isObjCQualifiedClassType())) |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2637 | continue; |
| 2638 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2639 | else if (const ObjCObjectPointerType *PTTo = |
| 2640 | ToType->getAs<ObjCObjectPointerType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2641 | if (const ObjCObjectPointerType *PTFr = |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2642 | FromType->getAs<ObjCObjectPointerType>()) |
Douglas Gregor | dec1cc4 | 2011-12-15 17:15:07 +0000 | [diff] [blame] | 2643 | if (Context.hasSameUnqualifiedType( |
| 2644 | PTTo->getObjectType()->getBaseType(), |
| 2645 | PTFr->getObjectType()->getBaseType())) |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2646 | continue; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2647 | } |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2648 | if (ArgPos) *ArgPos = O - OldType->arg_type_begin(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2649 | return false; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2650 | } |
| 2651 | } |
| 2652 | return true; |
| 2653 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2654 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2655 | /// CheckPointerConversion - Check the pointer conversion from the |
| 2656 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2657 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2658 | /// conversions for which IsPointerConversion has already returned |
| 2659 | /// true. It returns true and produces a diagnostic if there was an |
| 2660 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2661 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2662 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2663 | CXXCastPath& BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2664 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2665 | QualType FromType = From->getType(); |
Argyrios Kyrtzidis | b335872 | 2010-09-28 14:54:11 +0000 | [diff] [blame] | 2666 | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2667 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2668 | Kind = CK_BitCast; |
| 2669 | |
David Blaikie | 50800fc | 2012-08-08 17:33:31 +0000 | [diff] [blame] | 2670 | if (!IsCStyleOrFunctionalCast && !FromType->isAnyPointerType() && |
| 2671 | From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull) == |
| 2672 | Expr::NPCK_ZeroExpression) { |
| 2673 | if (Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy)) |
| 2674 | DiagRuntimeBehavior(From->getExprLoc(), From, |
| 2675 | PDiag(diag::warn_impcast_bool_to_null_pointer) |
| 2676 | << ToType << From->getSourceRange()); |
| 2677 | else if (!isUnevaluatedContext()) |
| 2678 | Diag(From->getExprLoc(), diag::warn_non_literal_null_pointer) |
| 2679 | << ToType << From->getSourceRange(); |
| 2680 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2681 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
| 2682 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2683 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 2684 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 2685 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2686 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 2687 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2688 | // We must have a derived-to-base conversion. Check an |
| 2689 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2690 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 2691 | From->getExprLoc(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2692 | From->getSourceRange(), &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2693 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2694 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2695 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2696 | // The conversion was successful. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2697 | Kind = CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2698 | } |
| 2699 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2700 | } else if (const ObjCObjectPointerType *ToPtrType = |
| 2701 | ToType->getAs<ObjCObjectPointerType>()) { |
| 2702 | if (const ObjCObjectPointerType *FromPtrType = |
| 2703 | FromType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2704 | // Objective-C++ conversions are always okay. |
| 2705 | // FIXME: We should have a different class of conversions for the |
| 2706 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2707 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2708 | return false; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2709 | } else if (FromType->isBlockPointerType()) { |
| 2710 | Kind = CK_BlockPointerToObjCPointerCast; |
| 2711 | } else { |
| 2712 | Kind = CK_CPointerToObjCPointerCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2713 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2714 | } else if (ToType->isBlockPointerType()) { |
| 2715 | if (!FromType->isBlockPointerType()) |
| 2716 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2717 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2718 | |
| 2719 | // We shouldn't fall into this case unless it's valid for other |
| 2720 | // reasons. |
| 2721 | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 2722 | Kind = CK_NullToPointer; |
| 2723 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2724 | return false; |
| 2725 | } |
| 2726 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2727 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 2728 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 2729 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 2730 | /// If so, returns true and places the converted type (that might differ from |
| 2731 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 2732 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2733 | QualType ToType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2734 | bool InOverloadResolution, |
| 2735 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2736 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2737 | if (!ToTypePtr) |
| 2738 | return false; |
| 2739 | |
| 2740 | // 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] | 2741 | if (From->isNullPointerConstant(Context, |
| 2742 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 2743 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2744 | ConvertedType = ToType; |
| 2745 | return true; |
| 2746 | } |
| 2747 | |
| 2748 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2749 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2750 | if (!FromTypePtr) |
| 2751 | return false; |
| 2752 | |
| 2753 | // A pointer to member of B can be converted to a pointer to member of D, |
| 2754 | // where D is derived from B (C++ 4.11p2). |
| 2755 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 2756 | QualType ToClass(ToTypePtr->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2757 | |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2758 | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2759 | !RequireCompleteType(From->getLocStart(), ToClass, 0) && |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2760 | IsDerivedFrom(ToClass, FromClass)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2761 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 2762 | ToClass.getTypePtr()); |
| 2763 | return true; |
| 2764 | } |
| 2765 | |
| 2766 | return false; |
| 2767 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2768 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2769 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 2770 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2771 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2772 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 2773 | /// true and produces a diagnostic if there was an error, or returns false |
| 2774 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2775 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2776 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2777 | CXXCastPath &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2778 | bool IgnoreBaseAccess) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2779 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2780 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2781 | if (!FromPtrType) { |
| 2782 | // This must be a null pointer to member pointer conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2783 | assert(From->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2784 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2785 | "Expr must be null pointer constant!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2786 | Kind = CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2787 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2788 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2789 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2790 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2791 | assert(ToPtrType && "No member pointer cast has a target type " |
| 2792 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2793 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2794 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 2795 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2796 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2797 | // FIXME: What about dependent types? |
| 2798 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 2799 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2800 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2801 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 2802 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2803 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 2804 | assert(DerivationOkay && |
| 2805 | "Should not have been called if derivation isn't OK."); |
| 2806 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2807 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2808 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 2809 | getUnqualifiedType())) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2810 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 2811 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 2812 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 2813 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2814 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2815 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2816 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2817 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 2818 | << FromClass << ToClass << QualType(VBase, 0) |
| 2819 | << From->getSourceRange(); |
| 2820 | return true; |
| 2821 | } |
| 2822 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2823 | if (!IgnoreBaseAccess) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2824 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 2825 | Paths.front(), |
| 2826 | diag::err_downcast_from_inaccessible_base); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2827 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2828 | // Must be a base to derived member conversion. |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2829 | BuildBasePathArray(Paths, BasePath); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2830 | Kind = CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2831 | return false; |
| 2832 | } |
| 2833 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2834 | /// IsQualificationConversion - Determines whether the conversion from |
| 2835 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 2836 | /// (C++ 4.4). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2837 | /// |
| 2838 | /// \param ObjCLifetimeConversion Output parameter that will be set to indicate |
| 2839 | /// when the qualification conversion involves a change in the Objective-C |
| 2840 | /// object lifetime. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2841 | bool |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2842 | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2843 | bool CStyle, bool &ObjCLifetimeConversion) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2844 | FromType = Context.getCanonicalType(FromType); |
| 2845 | ToType = Context.getCanonicalType(ToType); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2846 | ObjCLifetimeConversion = false; |
| 2847 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2848 | // If FromType and ToType are the same type, this is not a |
| 2849 | // qualification conversion. |
Sebastian Redl | 22c9240 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 2850 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2851 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2852 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2853 | // (C++ 4.4p4): |
| 2854 | // A conversion can add cv-qualifiers at levels other than the first |
| 2855 | // in multi-level pointers, subject to the following rules: [...] |
| 2856 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2857 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2858 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2859 | // Within each iteration of the loop, we check the qualifiers to |
| 2860 | // determine if this still looks like a qualification |
| 2861 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2862 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2863 | // until there are no more pointers or pointers-to-members left to |
| 2864 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2865 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2866 | |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2867 | Qualifiers FromQuals = FromType.getQualifiers(); |
| 2868 | Qualifiers ToQuals = ToType.getQualifiers(); |
| 2869 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2870 | // Objective-C ARC: |
| 2871 | // Check Objective-C lifetime conversions. |
| 2872 | if (FromQuals.getObjCLifetime() != ToQuals.getObjCLifetime() && |
| 2873 | UnwrappedAnyPointer) { |
| 2874 | if (ToQuals.compatiblyIncludesObjCLifetime(FromQuals)) { |
| 2875 | ObjCLifetimeConversion = true; |
| 2876 | FromQuals.removeObjCLifetime(); |
| 2877 | ToQuals.removeObjCLifetime(); |
| 2878 | } else { |
| 2879 | // Qualification conversions cannot cast between different |
| 2880 | // Objective-C lifetime qualifiers. |
| 2881 | return false; |
| 2882 | } |
| 2883 | } |
| 2884 | |
Douglas Gregor | 377e1bd | 2011-05-08 06:09:53 +0000 | [diff] [blame] | 2885 | // Allow addition/removal of GC attributes but not changing GC attributes. |
| 2886 | if (FromQuals.getObjCGCAttr() != ToQuals.getObjCGCAttr() && |
| 2887 | (!FromQuals.hasObjCGCAttr() || !ToQuals.hasObjCGCAttr())) { |
| 2888 | FromQuals.removeObjCGCAttr(); |
| 2889 | ToQuals.removeObjCGCAttr(); |
| 2890 | } |
| 2891 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2892 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 2893 | // 2,j, and similarly for volatile. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2894 | if (!CStyle && !ToQuals.compatiblyIncludes(FromQuals)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2895 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2896 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2897 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 2898 | // every cv for 0 < k < j. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2899 | if (!CStyle && FromQuals.getCVRQualifiers() != ToQuals.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2900 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2901 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2902 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2903 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 2904 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2905 | PreviousToQualsIncludeConst |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2906 | = PreviousToQualsIncludeConst && ToQuals.hasConst(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2907 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2908 | |
| 2909 | // We are left with FromType and ToType being the pointee types |
| 2910 | // after unwrapping the original FromType and ToType the same number |
| 2911 | // of types. If we unwrapped any pointers, and if FromType and |
| 2912 | // ToType have the same unqualified type (since we checked |
| 2913 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2914 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2915 | } |
| 2916 | |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2917 | /// \brief - Determine whether this is a conversion from a scalar type to an |
| 2918 | /// atomic type. |
| 2919 | /// |
| 2920 | /// If successful, updates \c SCS's second and third steps in the conversion |
| 2921 | /// sequence to finish the conversion. |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 2922 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 2923 | bool InOverloadResolution, |
| 2924 | StandardConversionSequence &SCS, |
| 2925 | bool CStyle) { |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2926 | const AtomicType *ToAtomic = ToType->getAs<AtomicType>(); |
| 2927 | if (!ToAtomic) |
| 2928 | return false; |
| 2929 | |
| 2930 | StandardConversionSequence InnerSCS; |
| 2931 | if (!IsStandardConversion(S, From, ToAtomic->getValueType(), |
| 2932 | InOverloadResolution, InnerSCS, |
| 2933 | CStyle, /*AllowObjCWritebackConversion=*/false)) |
| 2934 | return false; |
| 2935 | |
| 2936 | SCS.Second = InnerSCS.Second; |
| 2937 | SCS.setToType(1, InnerSCS.getToType(1)); |
| 2938 | SCS.Third = InnerSCS.Third; |
| 2939 | SCS.QualificationIncludesObjCLifetime |
| 2940 | = InnerSCS.QualificationIncludesObjCLifetime; |
| 2941 | SCS.setToType(2, InnerSCS.getToType(2)); |
| 2942 | return true; |
| 2943 | } |
| 2944 | |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2945 | static bool isFirstArgumentCompatibleWithType(ASTContext &Context, |
| 2946 | CXXConstructorDecl *Constructor, |
| 2947 | QualType Type) { |
| 2948 | const FunctionProtoType *CtorType = |
| 2949 | Constructor->getType()->getAs<FunctionProtoType>(); |
| 2950 | if (CtorType->getNumArgs() > 0) { |
| 2951 | QualType FirstArg = CtorType->getArgType(0); |
| 2952 | if (Context.hasSameUnqualifiedType(Type, FirstArg.getNonReferenceType())) |
| 2953 | return true; |
| 2954 | } |
| 2955 | return false; |
| 2956 | } |
| 2957 | |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2958 | static OverloadingResult |
| 2959 | IsInitializerListConstructorConversion(Sema &S, Expr *From, QualType ToType, |
| 2960 | CXXRecordDecl *To, |
| 2961 | UserDefinedConversionSequence &User, |
| 2962 | OverloadCandidateSet &CandidateSet, |
| 2963 | bool AllowExplicit) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2964 | DeclContext::lookup_result R = S.LookupConstructors(To); |
| 2965 | for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2966 | Con != ConEnd; ++Con) { |
| 2967 | NamedDecl *D = *Con; |
| 2968 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2969 | |
| 2970 | // Find the constructor (which may be a template). |
| 2971 | CXXConstructorDecl *Constructor = 0; |
| 2972 | FunctionTemplateDecl *ConstructorTmpl |
| 2973 | = dyn_cast<FunctionTemplateDecl>(D); |
| 2974 | if (ConstructorTmpl) |
| 2975 | Constructor |
| 2976 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 2977 | else |
| 2978 | Constructor = cast<CXXConstructorDecl>(D); |
| 2979 | |
| 2980 | bool Usable = !Constructor->isInvalidDecl() && |
| 2981 | S.isInitListConstructor(Constructor) && |
| 2982 | (AllowExplicit || !Constructor->isExplicit()); |
| 2983 | if (Usable) { |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2984 | // If the first argument is (a reference to) the target type, |
| 2985 | // suppress conversions. |
| 2986 | bool SuppressUserConversions = |
| 2987 | isFirstArgumentCompatibleWithType(S.Context, Constructor, ToType); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2988 | if (ConstructorTmpl) |
| 2989 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2990 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2991 | From, CandidateSet, |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2992 | SuppressUserConversions); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2993 | else |
| 2994 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2995 | From, CandidateSet, |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2996 | SuppressUserConversions); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2997 | } |
| 2998 | } |
| 2999 | |
| 3000 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3001 | |
| 3002 | OverloadCandidateSet::iterator Best; |
| 3003 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
| 3004 | case OR_Success: { |
| 3005 | // Record the standard conversion we used and the conversion function. |
| 3006 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3007 | QualType ThisType = Constructor->getThisType(S.Context); |
| 3008 | // Initializer lists don't have conversions as such. |
| 3009 | User.Before.setAsIdentityConversion(); |
| 3010 | User.HadMultipleCandidates = HadMultipleCandidates; |
| 3011 | User.ConversionFunction = Constructor; |
| 3012 | User.FoundConversionFunction = Best->FoundDecl; |
| 3013 | User.After.setAsIdentityConversion(); |
| 3014 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 3015 | User.After.setAllToTypes(ToType); |
| 3016 | return OR_Success; |
| 3017 | } |
| 3018 | |
| 3019 | case OR_No_Viable_Function: |
| 3020 | return OR_No_Viable_Function; |
| 3021 | case OR_Deleted: |
| 3022 | return OR_Deleted; |
| 3023 | case OR_Ambiguous: |
| 3024 | return OR_Ambiguous; |
| 3025 | } |
| 3026 | |
| 3027 | llvm_unreachable("Invalid OverloadResult!"); |
| 3028 | } |
| 3029 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 3030 | /// Determines whether there is a user-defined conversion sequence |
| 3031 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 3032 | /// ToType. If such a conversion exists, User will contain the |
| 3033 | /// user-defined conversion sequence that performs such a conversion |
| 3034 | /// and this routine will return true. Otherwise, this routine returns |
| 3035 | /// false and User is unspecified. |
| 3036 | /// |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 3037 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 3038 | /// "explicit" conversion functions as well as non-explicit conversion |
| 3039 | /// functions (C++0x [class.conv.fct]p2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3040 | static OverloadingResult |
| 3041 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3042 | UserDefinedConversionSequence &User, |
| 3043 | OverloadCandidateSet &CandidateSet, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3044 | bool AllowExplicit) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3045 | // Whether we will only visit constructors. |
| 3046 | bool ConstructorsOnly = false; |
| 3047 | |
| 3048 | // If the type we are conversion to is a class type, enumerate its |
| 3049 | // constructors. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3050 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3051 | // C++ [over.match.ctor]p1: |
| 3052 | // When objects of class type are direct-initialized (8.5), or |
| 3053 | // copy-initialized from an expression of the same or a |
| 3054 | // derived class type (8.5), overload resolution selects the |
| 3055 | // constructor. [...] For copy-initialization, the candidate |
| 3056 | // functions are all the converting constructors (12.3.1) of |
| 3057 | // that class. The argument list is the expression-list within |
| 3058 | // the parentheses of the initializer. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3059 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3060 | (From->getType()->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3061 | S.IsDerivedFrom(From->getType(), ToType))) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3062 | ConstructorsOnly = true; |
| 3063 | |
Benjamin Kramer | 63b6ebe | 2012-11-23 17:04:52 +0000 | [diff] [blame] | 3064 | S.RequireCompleteType(From->getExprLoc(), ToType, 0); |
Argyrios Kyrtzidis | e36bca6 | 2011-04-22 17:45:37 +0000 | [diff] [blame] | 3065 | // RequireCompleteType may have returned true due to some invalid decl |
| 3066 | // during template instantiation, but ToType may be complete enough now |
| 3067 | // to try to recover. |
| 3068 | if (ToType->isIncompleteType()) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 3069 | // We're not going to find any constructors. |
| 3070 | } else if (CXXRecordDecl *ToRecordDecl |
| 3071 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3072 | |
| 3073 | Expr **Args = &From; |
| 3074 | unsigned NumArgs = 1; |
| 3075 | bool ListInitializing = false; |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3076 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(From)) { |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3077 | // But first, see if there is an init-list-contructor that will work. |
| 3078 | OverloadingResult Result = IsInitializerListConstructorConversion( |
| 3079 | S, From, ToType, ToRecordDecl, User, CandidateSet, AllowExplicit); |
| 3080 | if (Result != OR_No_Viable_Function) |
| 3081 | return Result; |
| 3082 | // Never mind. |
| 3083 | CandidateSet.clear(); |
| 3084 | |
| 3085 | // If we're list-initializing, we pass the individual elements as |
| 3086 | // arguments, not the entire list. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3087 | Args = InitList->getInits(); |
| 3088 | NumArgs = InitList->getNumInits(); |
| 3089 | ListInitializing = true; |
| 3090 | } |
| 3091 | |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3092 | DeclContext::lookup_result R = S.LookupConstructors(ToRecordDecl); |
| 3093 | for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3094 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3095 | NamedDecl *D = *Con; |
| 3096 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3097 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3098 | // Find the constructor (which may be a template). |
| 3099 | CXXConstructorDecl *Constructor = 0; |
| 3100 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3101 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3102 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3103 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3104 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 3105 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3106 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3107 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3108 | bool Usable = !Constructor->isInvalidDecl(); |
| 3109 | if (ListInitializing) |
| 3110 | Usable = Usable && (AllowExplicit || !Constructor->isExplicit()); |
| 3111 | else |
| 3112 | Usable = Usable &&Constructor->isConvertingConstructor(AllowExplicit); |
| 3113 | if (Usable) { |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3114 | bool SuppressUserConversions = !ConstructorsOnly; |
| 3115 | if (SuppressUserConversions && ListInitializing) { |
| 3116 | SuppressUserConversions = false; |
| 3117 | if (NumArgs == 1) { |
| 3118 | // If the first argument is (a reference to) the target type, |
| 3119 | // suppress conversions. |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 3120 | SuppressUserConversions = isFirstArgumentCompatibleWithType( |
| 3121 | S.Context, Constructor, ToType); |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3122 | } |
| 3123 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3124 | if (ConstructorTmpl) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3125 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3126 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3127 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3128 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3129 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 3130 | // Allow one user-defined conversion when user specifies a |
| 3131 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3132 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3133 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3134 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3135 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3136 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3137 | } |
| 3138 | } |
| 3139 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3140 | // Enumerate conversion functions, if we're allowed to. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3141 | if (ConstructorsOnly || isa<InitListExpr>(From)) { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3142 | } else if (S.RequireCompleteType(From->getLocStart(), From->getType(), 0)) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3143 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3144 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3145 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3146 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3147 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 3148 | // Add all of the conversion functions as candidates. |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3149 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3150 | CXXRecordDecl::conversion_iterator> |
| 3151 | Conversions = FromRecordDecl->getVisibleConversionFunctions(); |
| 3152 | for (CXXRecordDecl::conversion_iterator |
| 3153 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3154 | DeclAccessPair FoundDecl = I.getPair(); |
| 3155 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3156 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3157 | if (isa<UsingShadowDecl>(D)) |
| 3158 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3159 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3160 | CXXConversionDecl *Conv; |
| 3161 | FunctionTemplateDecl *ConvTemplate; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3162 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 3163 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3164 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3165 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3166 | |
| 3167 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3168 | if (ConvTemplate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3169 | S.AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
| 3170 | ActingContext, From, ToType, |
| 3171 | CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3172 | else |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3173 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, |
| 3174 | From, ToType, CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3175 | } |
| 3176 | } |
| 3177 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3178 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3179 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3180 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3181 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3182 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3183 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3184 | case OR_Success: |
| 3185 | // Record the standard conversion we used and the conversion function. |
| 3186 | if (CXXConstructorDecl *Constructor |
| 3187 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 3188 | // C++ [over.ics.user]p1: |
| 3189 | // If the user-defined conversion is specified by a |
| 3190 | // constructor (12.3.1), the initial standard conversion |
| 3191 | // sequence converts the source type to the type required by |
| 3192 | // the argument of the constructor. |
| 3193 | // |
| 3194 | QualType ThisType = Constructor->getThisType(S.Context); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3195 | if (isa<InitListExpr>(From)) { |
| 3196 | // Initializer lists don't have conversions as such. |
| 3197 | User.Before.setAsIdentityConversion(); |
| 3198 | } else { |
| 3199 | if (Best->Conversions[0].isEllipsis()) |
| 3200 | User.EllipsisConversion = true; |
| 3201 | else { |
| 3202 | User.Before = Best->Conversions[0].Standard; |
| 3203 | User.EllipsisConversion = false; |
| 3204 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3205 | } |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3206 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3207 | User.ConversionFunction = Constructor; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3208 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3209 | User.After.setAsIdentityConversion(); |
| 3210 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 3211 | User.After.setAllToTypes(ToType); |
| 3212 | return OR_Success; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3213 | } |
| 3214 | if (CXXConversionDecl *Conversion |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3215 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 3216 | // C++ [over.ics.user]p1: |
| 3217 | // |
| 3218 | // [...] If the user-defined conversion is specified by a |
| 3219 | // conversion function (12.3.2), the initial standard |
| 3220 | // conversion sequence converts the source type to the |
| 3221 | // implicit object parameter of the conversion function. |
| 3222 | User.Before = Best->Conversions[0].Standard; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3223 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3224 | User.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3225 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3226 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3227 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3228 | // C++ [over.ics.user]p2: |
| 3229 | // The second standard conversion sequence converts the |
| 3230 | // result of the user-defined conversion to the target type |
| 3231 | // for the sequence. Since an implicit conversion sequence |
| 3232 | // is an initialization, the special rules for |
| 3233 | // initialization by user-defined conversion apply when |
| 3234 | // selecting the best user-defined conversion for a |
| 3235 | // user-defined conversion sequence (see 13.3.3 and |
| 3236 | // 13.3.3.1). |
| 3237 | User.After = Best->FinalConversion; |
| 3238 | return OR_Success; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3239 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3240 | llvm_unreachable("Not a constructor or conversion function?"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3241 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3242 | case OR_No_Viable_Function: |
| 3243 | return OR_No_Viable_Function; |
| 3244 | case OR_Deleted: |
| 3245 | // No conversion here! We're done. |
| 3246 | return OR_Deleted; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3247 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3248 | case OR_Ambiguous: |
| 3249 | return OR_Ambiguous; |
| 3250 | } |
| 3251 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3252 | llvm_unreachable("Invalid OverloadResult!"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3253 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3254 | |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3255 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3256 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3257 | ImplicitConversionSequence ICS; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3258 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3259 | OverloadingResult OvResult = |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3260 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3261 | CandidateSet, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3262 | if (OvResult == OR_Ambiguous) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3263 | Diag(From->getLocStart(), |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3264 | diag::err_typecheck_ambiguous_condition) |
| 3265 | << From->getType() << ToType << From->getSourceRange(); |
| 3266 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3267 | Diag(From->getLocStart(), |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3268 | diag::err_typecheck_nonviable_condition) |
| 3269 | << From->getType() << ToType << From->getSourceRange(); |
| 3270 | else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3271 | return false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3272 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3273 | return true; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3274 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3275 | |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3276 | /// \brief Compare the user-defined conversion functions or constructors |
| 3277 | /// of two user-defined conversion sequences to determine whether any ordering |
| 3278 | /// is possible. |
| 3279 | static ImplicitConversionSequence::CompareKind |
| 3280 | compareConversionFunctions(Sema &S, |
| 3281 | FunctionDecl *Function1, |
| 3282 | FunctionDecl *Function2) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3283 | if (!S.getLangOpts().ObjC1 || !S.getLangOpts().CPlusPlus11) |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3284 | return ImplicitConversionSequence::Indistinguishable; |
| 3285 | |
| 3286 | // Objective-C++: |
| 3287 | // If both conversion functions are implicitly-declared conversions from |
| 3288 | // a lambda closure type to a function pointer and a block pointer, |
| 3289 | // respectively, always prefer the conversion to a function pointer, |
| 3290 | // because the function pointer is more lightweight and is more likely |
| 3291 | // to keep code working. |
| 3292 | CXXConversionDecl *Conv1 = dyn_cast<CXXConversionDecl>(Function1); |
| 3293 | if (!Conv1) |
| 3294 | return ImplicitConversionSequence::Indistinguishable; |
| 3295 | |
| 3296 | CXXConversionDecl *Conv2 = dyn_cast<CXXConversionDecl>(Function2); |
| 3297 | if (!Conv2) |
| 3298 | return ImplicitConversionSequence::Indistinguishable; |
| 3299 | |
| 3300 | if (Conv1->getParent()->isLambda() && Conv2->getParent()->isLambda()) { |
| 3301 | bool Block1 = Conv1->getConversionType()->isBlockPointerType(); |
| 3302 | bool Block2 = Conv2->getConversionType()->isBlockPointerType(); |
| 3303 | if (Block1 != Block2) |
| 3304 | return Block1? ImplicitConversionSequence::Worse |
| 3305 | : ImplicitConversionSequence::Better; |
| 3306 | } |
| 3307 | |
| 3308 | return ImplicitConversionSequence::Indistinguishable; |
| 3309 | } |
| 3310 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3311 | /// CompareImplicitConversionSequences - Compare two implicit |
| 3312 | /// conversion sequences to determine whether one is better than the |
| 3313 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3314 | static ImplicitConversionSequence::CompareKind |
| 3315 | CompareImplicitConversionSequences(Sema &S, |
| 3316 | const ImplicitConversionSequence& ICS1, |
| 3317 | const ImplicitConversionSequence& ICS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3318 | { |
| 3319 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 3320 | // conversion sequences (as defined in 13.3.3.1) |
| 3321 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 3322 | // conversion sequence than a user-defined conversion sequence or |
| 3323 | // an ellipsis conversion sequence, and |
| 3324 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 3325 | // conversion sequence than an ellipsis conversion sequence |
| 3326 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3327 | // |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3328 | // C++0x [over.best.ics]p10: |
| 3329 | // For the purpose of ranking implicit conversion sequences as |
| 3330 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 3331 | // treated as a user-defined sequence that is indistinguishable |
| 3332 | // from any other user-defined conversion sequence. |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3333 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 3334 | return ImplicitConversionSequence::Better; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3335 | if (ICS2.getKindRank() < ICS1.getKindRank()) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3336 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3337 | |
Benjamin Kramer | b6eee07 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 3338 | // The following checks require both conversion sequences to be of |
| 3339 | // the same kind. |
| 3340 | if (ICS1.getKind() != ICS2.getKind()) |
| 3341 | return ImplicitConversionSequence::Indistinguishable; |
| 3342 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3343 | ImplicitConversionSequence::CompareKind Result = |
| 3344 | ImplicitConversionSequence::Indistinguishable; |
| 3345 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3346 | // Two implicit conversion sequences of the same form are |
| 3347 | // indistinguishable conversion sequences unless one of the |
| 3348 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3349 | if (ICS1.isStandard()) |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3350 | Result = CompareStandardConversionSequences(S, |
| 3351 | ICS1.Standard, ICS2.Standard); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3352 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3353 | // User-defined conversion sequence U1 is a better conversion |
| 3354 | // sequence than another user-defined conversion sequence U2 if |
| 3355 | // they contain the same user-defined conversion function or |
| 3356 | // constructor and if the second standard conversion sequence of |
| 3357 | // U1 is better than the second standard conversion sequence of |
| 3358 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3359 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3360 | ICS2.UserDefined.ConversionFunction) |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3361 | Result = CompareStandardConversionSequences(S, |
| 3362 | ICS1.UserDefined.After, |
| 3363 | ICS2.UserDefined.After); |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3364 | else |
| 3365 | Result = compareConversionFunctions(S, |
| 3366 | ICS1.UserDefined.ConversionFunction, |
| 3367 | ICS2.UserDefined.ConversionFunction); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3368 | } |
| 3369 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3370 | // List-initialization sequence L1 is a better conversion sequence than |
| 3371 | // list-initialization sequence L2 if L1 converts to std::initializer_list<X> |
| 3372 | // for some X and L2 does not. |
| 3373 | if (Result == ImplicitConversionSequence::Indistinguishable && |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 3374 | !ICS1.isBad() && |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3375 | ICS1.isListInitializationSequence() && |
| 3376 | ICS2.isListInitializationSequence()) { |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 3377 | if (ICS1.isStdInitializerListElement() && |
| 3378 | !ICS2.isStdInitializerListElement()) |
| 3379 | return ImplicitConversionSequence::Better; |
| 3380 | if (!ICS1.isStdInitializerListElement() && |
| 3381 | ICS2.isStdInitializerListElement()) |
| 3382 | return ImplicitConversionSequence::Worse; |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3383 | } |
| 3384 | |
| 3385 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3386 | } |
| 3387 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3388 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 3389 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 3390 | Qualifiers Quals; |
| 3391 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3392 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3393 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3394 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3395 | return Context.hasSameUnqualifiedType(T1, T2); |
| 3396 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3397 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3398 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 3399 | // determine if one is a proper subset of the other. |
| 3400 | static ImplicitConversionSequence::CompareKind |
| 3401 | compareStandardConversionSubsets(ASTContext &Context, |
| 3402 | const StandardConversionSequence& SCS1, |
| 3403 | const StandardConversionSequence& SCS2) { |
| 3404 | ImplicitConversionSequence::CompareKind Result |
| 3405 | = ImplicitConversionSequence::Indistinguishable; |
| 3406 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3407 | // the identity conversion sequence is considered to be a subsequence of |
Douglas Gregor | ae65f4b | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 3408 | // any non-identity conversion sequence |
Douglas Gregor | 4ae5b72 | 2011-06-05 06:15:20 +0000 | [diff] [blame] | 3409 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 3410 | return ImplicitConversionSequence::Better; |
| 3411 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 3412 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3413 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3414 | if (SCS1.Second != SCS2.Second) { |
| 3415 | if (SCS1.Second == ICK_Identity) |
| 3416 | Result = ImplicitConversionSequence::Better; |
| 3417 | else if (SCS2.Second == ICK_Identity) |
| 3418 | Result = ImplicitConversionSequence::Worse; |
| 3419 | else |
| 3420 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3421 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3422 | return ImplicitConversionSequence::Indistinguishable; |
| 3423 | |
| 3424 | if (SCS1.Third == SCS2.Third) { |
| 3425 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 3426 | : ImplicitConversionSequence::Indistinguishable; |
| 3427 | } |
| 3428 | |
| 3429 | if (SCS1.Third == ICK_Identity) |
| 3430 | return Result == ImplicitConversionSequence::Worse |
| 3431 | ? ImplicitConversionSequence::Indistinguishable |
| 3432 | : ImplicitConversionSequence::Better; |
| 3433 | |
| 3434 | if (SCS2.Third == ICK_Identity) |
| 3435 | return Result == ImplicitConversionSequence::Better |
| 3436 | ? ImplicitConversionSequence::Indistinguishable |
| 3437 | : ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3438 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3439 | return ImplicitConversionSequence::Indistinguishable; |
| 3440 | } |
| 3441 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3442 | /// \brief Determine whether one of the given reference bindings is better |
| 3443 | /// than the other based on what kind of bindings they are. |
| 3444 | static bool isBetterReferenceBindingKind(const StandardConversionSequence &SCS1, |
| 3445 | const StandardConversionSequence &SCS2) { |
| 3446 | // C++0x [over.ics.rank]p3b4: |
| 3447 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 3448 | // implicit object parameter of a non-static member function declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3449 | // without a ref-qualifier, and *either* S1 binds an rvalue reference |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3450 | // 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] | 3451 | // lvalue reference to a function lvalue and S2 binds an rvalue |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3452 | // reference*. |
| 3453 | // |
| 3454 | // FIXME: Rvalue references. We're going rogue with the above edits, |
| 3455 | // because the semantics in the current C++0x working paper (N3225 at the |
| 3456 | // time of this writing) break the standard definition of std::forward |
| 3457 | // and std::reference_wrapper when dealing with references to functions. |
| 3458 | // Proposed wording changes submitted to CWG for consideration. |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3459 | if (SCS1.BindsImplicitObjectArgumentWithoutRefQualifier || |
| 3460 | SCS2.BindsImplicitObjectArgumentWithoutRefQualifier) |
| 3461 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3462 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3463 | return (!SCS1.IsLvalueReference && SCS1.BindsToRvalue && |
| 3464 | SCS2.IsLvalueReference) || |
| 3465 | (SCS1.IsLvalueReference && SCS1.BindsToFunctionLvalue && |
| 3466 | !SCS2.IsLvalueReference); |
| 3467 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3468 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3469 | /// CompareStandardConversionSequences - Compare two standard |
| 3470 | /// conversion sequences to determine whether one is better than the |
| 3471 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3472 | static ImplicitConversionSequence::CompareKind |
| 3473 | CompareStandardConversionSequences(Sema &S, |
| 3474 | const StandardConversionSequence& SCS1, |
| 3475 | const StandardConversionSequence& SCS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3476 | { |
| 3477 | // Standard conversion sequence S1 is a better conversion sequence |
| 3478 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 3479 | |
| 3480 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 3481 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 3482 | // excluding any Lvalue Transformation; the identity conversion |
| 3483 | // sequence is considered to be a subsequence of any |
| 3484 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3485 | if (ImplicitConversionSequence::CompareKind CK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3486 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3487 | return CK; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3488 | |
| 3489 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 3490 | // defined below), or, if not that, |
| 3491 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 3492 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 3493 | if (Rank1 < Rank2) |
| 3494 | return ImplicitConversionSequence::Better; |
| 3495 | else if (Rank2 < Rank1) |
| 3496 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3497 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3498 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 3499 | // are indistinguishable unless one of the following rules |
| 3500 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3501 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3502 | // A conversion that is not a conversion of a pointer, or |
| 3503 | // pointer to member, to bool is better than another conversion |
| 3504 | // that is such a conversion. |
| 3505 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 3506 | return SCS2.isPointerConversionToBool() |
| 3507 | ? ImplicitConversionSequence::Better |
| 3508 | : ImplicitConversionSequence::Worse; |
| 3509 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3510 | // C++ [over.ics.rank]p4b2: |
| 3511 | // |
| 3512 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3513 | // conversion of B* to A* is better than conversion of B* to |
| 3514 | // void*, and conversion of A* to void* is better than conversion |
| 3515 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3516 | bool SCS1ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3517 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3518 | bool SCS2ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3519 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3520 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 3521 | // Exactly one of the conversion sequences is a conversion to |
| 3522 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3523 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 3524 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3525 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 3526 | // Neither conversion sequence converts to a void pointer; compare |
| 3527 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3528 | if (ImplicitConversionSequence::CompareKind DerivedCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3529 | = CompareDerivedToBaseConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3530 | return DerivedCK; |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3531 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid && |
| 3532 | !S.Context.hasSameType(SCS1.getFromType(), SCS2.getFromType())) { |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3533 | // Both conversion sequences are conversions to void |
| 3534 | // pointers. Compare the source types to determine if there's an |
| 3535 | // inheritance relationship in their sources. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3536 | QualType FromType1 = SCS1.getFromType(); |
| 3537 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3538 | |
| 3539 | // Adjust the types we're converting from via the array-to-pointer |
| 3540 | // conversion, if we need to. |
| 3541 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3542 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3543 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3544 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3545 | |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3546 | QualType FromPointee1 = FromType1->getPointeeType().getUnqualifiedType(); |
| 3547 | QualType FromPointee2 = FromType2->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3548 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3549 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3550 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3551 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3552 | return ImplicitConversionSequence::Worse; |
| 3553 | |
| 3554 | // Objective-C++: If one interface is more specific than the |
| 3555 | // other, it is the better one. |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3556 | const ObjCObjectPointerType* FromObjCPtr1 |
| 3557 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3558 | const ObjCObjectPointerType* FromObjCPtr2 |
| 3559 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3560 | if (FromObjCPtr1 && FromObjCPtr2) { |
| 3561 | bool AssignLeft = S.Context.canAssignObjCInterfaces(FromObjCPtr1, |
| 3562 | FromObjCPtr2); |
| 3563 | bool AssignRight = S.Context.canAssignObjCInterfaces(FromObjCPtr2, |
| 3564 | FromObjCPtr1); |
| 3565 | if (AssignLeft != AssignRight) { |
| 3566 | return AssignLeft? ImplicitConversionSequence::Better |
| 3567 | : ImplicitConversionSequence::Worse; |
| 3568 | } |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3569 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3570 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3571 | |
| 3572 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 3573 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3574 | if (ImplicitConversionSequence::CompareKind QualCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3575 | = CompareQualificationConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3576 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3577 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3578 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3579 | // Check for a better reference binding based on the kind of bindings. |
| 3580 | if (isBetterReferenceBindingKind(SCS1, SCS2)) |
| 3581 | return ImplicitConversionSequence::Better; |
| 3582 | else if (isBetterReferenceBindingKind(SCS2, SCS1)) |
| 3583 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3584 | |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 3585 | // C++ [over.ics.rank]p3b4: |
| 3586 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 3587 | // which the references refer are the same type except for |
| 3588 | // top-level cv-qualifiers, and the type to which the reference |
| 3589 | // initialized by S2 refers is more cv-qualified than the type |
| 3590 | // to which the reference initialized by S1 refers. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3591 | QualType T1 = SCS1.getToType(2); |
| 3592 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3593 | T1 = S.Context.getCanonicalType(T1); |
| 3594 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3595 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3596 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3597 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3598 | if (UnqualT1 == UnqualT2) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3599 | // Objective-C++ ARC: If the references refer to objects with different |
| 3600 | // lifetimes, prefer bindings that don't change lifetime. |
| 3601 | if (SCS1.ObjCLifetimeConversionBinding != |
| 3602 | SCS2.ObjCLifetimeConversionBinding) { |
| 3603 | return SCS1.ObjCLifetimeConversionBinding |
| 3604 | ? ImplicitConversionSequence::Worse |
| 3605 | : ImplicitConversionSequence::Better; |
| 3606 | } |
| 3607 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3608 | // If the type is an array type, promote the element qualifiers to the |
| 3609 | // type for comparison. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3610 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3611 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3612 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3613 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3614 | if (T2.isMoreQualifiedThan(T1)) |
| 3615 | return ImplicitConversionSequence::Better; |
| 3616 | else if (T1.isMoreQualifiedThan(T2)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3617 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3618 | } |
| 3619 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3620 | |
Francois Pichet | 1c98d62 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3621 | // In Microsoft mode, prefer an integral conversion to a |
| 3622 | // floating-to-integral conversion if the integral conversion |
| 3623 | // is between types of the same size. |
| 3624 | // For example: |
| 3625 | // void f(float); |
| 3626 | // void f(int); |
| 3627 | // int main { |
| 3628 | // long a; |
| 3629 | // f(a); |
| 3630 | // } |
| 3631 | // Here, MSVC will call f(int) instead of generating a compile error |
| 3632 | // as clang will do in standard mode. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3633 | if (S.getLangOpts().MicrosoftMode && |
Francois Pichet | 1c98d62 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3634 | SCS1.Second == ICK_Integral_Conversion && |
| 3635 | SCS2.Second == ICK_Floating_Integral && |
| 3636 | S.Context.getTypeSize(SCS1.getFromType()) == |
| 3637 | S.Context.getTypeSize(SCS1.getToType(2))) |
| 3638 | return ImplicitConversionSequence::Better; |
| 3639 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3640 | return ImplicitConversionSequence::Indistinguishable; |
| 3641 | } |
| 3642 | |
| 3643 | /// CompareQualificationConversions - Compares two standard conversion |
| 3644 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3645 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 3646 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3647 | CompareQualificationConversions(Sema &S, |
| 3648 | const StandardConversionSequence& SCS1, |
| 3649 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 3650 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3651 | // -- S1 and S2 differ only in their qualification conversion and |
| 3652 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 3653 | // cv-qualification signature of type T1 is a proper subset of |
| 3654 | // the cv-qualification signature of type T2, and S1 is not the |
| 3655 | // deprecated string literal array-to-pointer conversion (4.2). |
| 3656 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 3657 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 3658 | return ImplicitConversionSequence::Indistinguishable; |
| 3659 | |
| 3660 | // FIXME: the example in the standard doesn't use a qualification |
| 3661 | // conversion (!) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3662 | QualType T1 = SCS1.getToType(2); |
| 3663 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3664 | T1 = S.Context.getCanonicalType(T1); |
| 3665 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3666 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3667 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3668 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3669 | |
| 3670 | // If the types are the same, we won't learn anything by unwrapped |
| 3671 | // them. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3672 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3673 | return ImplicitConversionSequence::Indistinguishable; |
| 3674 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3675 | // If the type is an array type, promote the element qualifiers to the type |
| 3676 | // for comparison. |
| 3677 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3678 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3679 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3680 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3681 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3682 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3683 | = ImplicitConversionSequence::Indistinguishable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3684 | |
| 3685 | // Objective-C++ ARC: |
| 3686 | // Prefer qualification conversions not involving a change in lifetime |
| 3687 | // to qualification conversions that do not change lifetime. |
| 3688 | if (SCS1.QualificationIncludesObjCLifetime != |
| 3689 | SCS2.QualificationIncludesObjCLifetime) { |
| 3690 | Result = SCS1.QualificationIncludesObjCLifetime |
| 3691 | ? ImplicitConversionSequence::Worse |
| 3692 | : ImplicitConversionSequence::Better; |
| 3693 | } |
| 3694 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3695 | while (S.Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3696 | // Within each iteration of the loop, we check the qualifiers to |
| 3697 | // determine if this still looks like a qualification |
| 3698 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 3699 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3700 | // until there are no more pointers or pointers-to-members left |
| 3701 | // to unwrap. This essentially mimics what |
| 3702 | // IsQualificationConversion does, but here we're checking for a |
| 3703 | // strict subset of qualifiers. |
| 3704 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 3705 | // The qualifiers are the same, so this doesn't tell us anything |
| 3706 | // about how the sequences rank. |
| 3707 | ; |
| 3708 | else if (T2.isMoreQualifiedThan(T1)) { |
| 3709 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 3710 | if (Result == ImplicitConversionSequence::Worse) |
| 3711 | // Neither has qualifiers that are a subset of the other's |
| 3712 | // qualifiers. |
| 3713 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3714 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3715 | Result = ImplicitConversionSequence::Better; |
| 3716 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 3717 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 3718 | if (Result == ImplicitConversionSequence::Better) |
| 3719 | // Neither has qualifiers that are a subset of the other's |
| 3720 | // qualifiers. |
| 3721 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3722 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3723 | Result = ImplicitConversionSequence::Worse; |
| 3724 | } else { |
| 3725 | // Qualifiers are disjoint. |
| 3726 | return ImplicitConversionSequence::Indistinguishable; |
| 3727 | } |
| 3728 | |
| 3729 | // If the types after this point are equivalent, we're done. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3730 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3731 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3732 | } |
| 3733 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3734 | // Check that the winning standard conversion sequence isn't using |
| 3735 | // the deprecated string literal array to pointer conversion. |
| 3736 | switch (Result) { |
| 3737 | case ImplicitConversionSequence::Better: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3738 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3739 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3740 | break; |
| 3741 | |
| 3742 | case ImplicitConversionSequence::Indistinguishable: |
| 3743 | break; |
| 3744 | |
| 3745 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3746 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3747 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3748 | break; |
| 3749 | } |
| 3750 | |
| 3751 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3752 | } |
| 3753 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3754 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 3755 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3756 | /// various kinds of derived-to-base conversions (C++ |
| 3757 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 3758 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3759 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3760 | CompareDerivedToBaseConversions(Sema &S, |
| 3761 | const StandardConversionSequence& SCS1, |
| 3762 | const StandardConversionSequence& SCS2) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3763 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3764 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3765 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3766 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3767 | |
| 3768 | // Adjust the types we're converting from via the array-to-pointer |
| 3769 | // conversion, if we need to. |
| 3770 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3771 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3772 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3773 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3774 | |
| 3775 | // Canonicalize all of the types. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3776 | FromType1 = S.Context.getCanonicalType(FromType1); |
| 3777 | ToType1 = S.Context.getCanonicalType(ToType1); |
| 3778 | FromType2 = S.Context.getCanonicalType(FromType2); |
| 3779 | ToType2 = S.Context.getCanonicalType(ToType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3780 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3781 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3782 | // |
| 3783 | // If class B is derived directly or indirectly from class A and |
| 3784 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3785 | // |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3786 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3787 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 3788 | SCS2.Second == ICK_Pointer_Conversion && |
| 3789 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 3790 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 3791 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3792 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3793 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3794 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3795 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3796 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3797 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3798 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3799 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3800 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3801 | // -- 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] | 3802 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3803 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3804 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3805 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3806 | return ImplicitConversionSequence::Worse; |
| 3807 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3808 | |
| 3809 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 3810 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3811 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3812 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3813 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3814 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3815 | } |
| 3816 | } else if (SCS1.Second == ICK_Pointer_Conversion && |
| 3817 | SCS2.Second == ICK_Pointer_Conversion) { |
| 3818 | const ObjCObjectPointerType *FromPtr1 |
| 3819 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3820 | const ObjCObjectPointerType *FromPtr2 |
| 3821 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3822 | const ObjCObjectPointerType *ToPtr1 |
| 3823 | = ToType1->getAs<ObjCObjectPointerType>(); |
| 3824 | const ObjCObjectPointerType *ToPtr2 |
| 3825 | = ToType2->getAs<ObjCObjectPointerType>(); |
| 3826 | |
| 3827 | if (FromPtr1 && FromPtr2 && ToPtr1 && ToPtr2) { |
| 3828 | // Apply the same conversion ranking rules for Objective-C pointer types |
| 3829 | // that we do for C++ pointers to class types. However, we employ the |
| 3830 | // Objective-C pseudo-subtyping relationship used for assignment of |
| 3831 | // Objective-C pointer types. |
| 3832 | bool FromAssignLeft |
| 3833 | = S.Context.canAssignObjCInterfaces(FromPtr1, FromPtr2); |
| 3834 | bool FromAssignRight |
| 3835 | = S.Context.canAssignObjCInterfaces(FromPtr2, FromPtr1); |
| 3836 | bool ToAssignLeft |
| 3837 | = S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2); |
| 3838 | bool ToAssignRight |
| 3839 | = S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1); |
| 3840 | |
| 3841 | // A conversion to an a non-id object pointer type or qualified 'id' |
| 3842 | // type is better than a conversion to 'id'. |
| 3843 | if (ToPtr1->isObjCIdType() && |
| 3844 | (ToPtr2->isObjCQualifiedIdType() || ToPtr2->getInterfaceDecl())) |
| 3845 | return ImplicitConversionSequence::Worse; |
| 3846 | if (ToPtr2->isObjCIdType() && |
| 3847 | (ToPtr1->isObjCQualifiedIdType() || ToPtr1->getInterfaceDecl())) |
| 3848 | return ImplicitConversionSequence::Better; |
| 3849 | |
| 3850 | // A conversion to a non-id object pointer type is better than a |
| 3851 | // conversion to a qualified 'id' type |
| 3852 | if (ToPtr1->isObjCQualifiedIdType() && ToPtr2->getInterfaceDecl()) |
| 3853 | return ImplicitConversionSequence::Worse; |
| 3854 | if (ToPtr2->isObjCQualifiedIdType() && ToPtr1->getInterfaceDecl()) |
| 3855 | return ImplicitConversionSequence::Better; |
| 3856 | |
| 3857 | // A conversion to an a non-Class object pointer type or qualified 'Class' |
| 3858 | // type is better than a conversion to 'Class'. |
| 3859 | if (ToPtr1->isObjCClassType() && |
| 3860 | (ToPtr2->isObjCQualifiedClassType() || ToPtr2->getInterfaceDecl())) |
| 3861 | return ImplicitConversionSequence::Worse; |
| 3862 | if (ToPtr2->isObjCClassType() && |
| 3863 | (ToPtr1->isObjCQualifiedClassType() || ToPtr1->getInterfaceDecl())) |
| 3864 | return ImplicitConversionSequence::Better; |
| 3865 | |
| 3866 | // A conversion to a non-Class object pointer type is better than a |
| 3867 | // conversion to a qualified 'Class' type. |
| 3868 | if (ToPtr1->isObjCQualifiedClassType() && ToPtr2->getInterfaceDecl()) |
| 3869 | return ImplicitConversionSequence::Worse; |
| 3870 | if (ToPtr2->isObjCQualifiedClassType() && ToPtr1->getInterfaceDecl()) |
| 3871 | return ImplicitConversionSequence::Better; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3872 | |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3873 | // -- "conversion of C* to B* is better than conversion of C* to A*," |
| 3874 | if (S.Context.hasSameType(FromType1, FromType2) && |
| 3875 | !FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType() && |
| 3876 | (ToAssignLeft != ToAssignRight)) |
| 3877 | return ToAssignLeft? ImplicitConversionSequence::Worse |
| 3878 | : ImplicitConversionSequence::Better; |
| 3879 | |
| 3880 | // -- "conversion of B* to A* is better than conversion of C* to A*," |
| 3881 | if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) && |
| 3882 | (FromAssignLeft != FromAssignRight)) |
| 3883 | return FromAssignLeft? ImplicitConversionSequence::Better |
| 3884 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3885 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3886 | } |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3887 | |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3888 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3889 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 3890 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 3891 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3892 | const MemberPointerType * FromMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3893 | FromType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3894 | const MemberPointerType * ToMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3895 | ToType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3896 | const MemberPointerType * FromMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3897 | FromType2->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3898 | const MemberPointerType * ToMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3899 | ToType2->getAs<MemberPointerType>(); |
| 3900 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 3901 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 3902 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 3903 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 3904 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 3905 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 3906 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 3907 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3908 | // 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] | 3909 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3910 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3911 | return ImplicitConversionSequence::Worse; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3912 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3913 | return ImplicitConversionSequence::Better; |
| 3914 | } |
| 3915 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 3916 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3917 | if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3918 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3919 | else if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3920 | return ImplicitConversionSequence::Worse; |
| 3921 | } |
| 3922 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3923 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3924 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3925 | // -- 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] | 3926 | // -- binding of an expression of type C to a reference of type |
| 3927 | // B& is better than binding an expression of type C to a |
| 3928 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3929 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3930 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3931 | if (S.IsDerivedFrom(ToType1, ToType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3932 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3933 | else if (S.IsDerivedFrom(ToType2, ToType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3934 | return ImplicitConversionSequence::Worse; |
| 3935 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3936 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3937 | // -- 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] | 3938 | // -- binding of an expression of type B to a reference of type |
| 3939 | // A& is better than binding an expression of type C to a |
| 3940 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3941 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3942 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3943 | if (S.IsDerivedFrom(FromType2, FromType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3944 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3945 | else if (S.IsDerivedFrom(FromType1, FromType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3946 | return ImplicitConversionSequence::Worse; |
| 3947 | } |
| 3948 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3949 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3950 | return ImplicitConversionSequence::Indistinguishable; |
| 3951 | } |
| 3952 | |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 3953 | /// \brief Determine whether the given type is valid, e.g., it is not an invalid |
| 3954 | /// C++ class. |
| 3955 | static bool isTypeValid(QualType T) { |
| 3956 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) |
| 3957 | return !Record->isInvalidDecl(); |
| 3958 | |
| 3959 | return true; |
| 3960 | } |
| 3961 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3962 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 3963 | /// determine whether they are reference-related, |
| 3964 | /// reference-compatible, reference-compatible with added |
| 3965 | /// qualification, or incompatible, for use in C++ initialization by |
| 3966 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 3967 | /// type, and the first type (T1) is the pointee type of the reference |
| 3968 | /// type being initialized. |
| 3969 | Sema::ReferenceCompareResult |
| 3970 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 3971 | QualType OrigT1, QualType OrigT2, |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3972 | bool &DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3973 | bool &ObjCConversion, |
| 3974 | bool &ObjCLifetimeConversion) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3975 | assert(!OrigT1->isReferenceType() && |
| 3976 | "T1 must be the pointee type of the reference type"); |
| 3977 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 3978 | |
| 3979 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 3980 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 3981 | Qualifiers T1Quals, T2Quals; |
| 3982 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3983 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 3984 | |
| 3985 | // C++ [dcl.init.ref]p4: |
| 3986 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 3987 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 3988 | // T1 is a base class of T2. |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3989 | DerivedToBase = false; |
| 3990 | ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3991 | ObjCLifetimeConversion = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3992 | if (UnqualT1 == UnqualT2) { |
| 3993 | // Nothing to do. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3994 | } else if (!RequireCompleteType(Loc, OrigT2, 0) && |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 3995 | isTypeValid(UnqualT1) && isTypeValid(UnqualT2) && |
| 3996 | IsDerivedFrom(UnqualT2, UnqualT1)) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3997 | DerivedToBase = true; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3998 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
| 3999 | UnqualT2->isObjCObjectOrInterfaceType() && |
| 4000 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
| 4001 | ObjCConversion = true; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4002 | else |
| 4003 | return Ref_Incompatible; |
| 4004 | |
| 4005 | // At this point, we know that T1 and T2 are reference-related (at |
| 4006 | // least). |
| 4007 | |
| 4008 | // If the type is an array type, promote the element qualifiers to the type |
| 4009 | // for comparison. |
| 4010 | if (isa<ArrayType>(T1) && T1Quals) |
| 4011 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 4012 | if (isa<ArrayType>(T2) && T2Quals) |
| 4013 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 4014 | |
| 4015 | // C++ [dcl.init.ref]p4: |
| 4016 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 4017 | // reference-related to T2 and cv1 is the same cv-qualification |
| 4018 | // as, or greater cv-qualification than, cv2. For purposes of |
| 4019 | // overload resolution, cases for which cv1 is greater |
| 4020 | // cv-qualification than cv2 are identified as |
| 4021 | // reference-compatible with added qualification (see 13.3.3.2). |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 4022 | // |
| 4023 | // Note that we also require equivalence of Objective-C GC and address-space |
| 4024 | // qualifiers when performing these computations, so that e.g., an int in |
| 4025 | // address space 1 is not reference-compatible with an int in address |
| 4026 | // space 2. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4027 | if (T1Quals.getObjCLifetime() != T2Quals.getObjCLifetime() && |
| 4028 | T1Quals.compatiblyIncludesObjCLifetime(T2Quals)) { |
| 4029 | T1Quals.removeObjCLifetime(); |
| 4030 | T2Quals.removeObjCLifetime(); |
| 4031 | ObjCLifetimeConversion = true; |
| 4032 | } |
| 4033 | |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 4034 | if (T1Quals == T2Quals) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4035 | return Ref_Compatible; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4036 | else if (T1Quals.compatiblyIncludes(T2Quals)) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4037 | return Ref_Compatible_With_Added_Qualification; |
| 4038 | else |
| 4039 | return Ref_Related; |
| 4040 | } |
| 4041 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4042 | /// \brief Look for a user-defined conversion to an value reference-compatible |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4043 | /// with DeclType. Return true if something definite is found. |
| 4044 | static bool |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4045 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
| 4046 | QualType DeclType, SourceLocation DeclLoc, |
| 4047 | Expr *Init, QualType T2, bool AllowRvalues, |
| 4048 | bool AllowExplicit) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4049 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 4050 | CXXRecordDecl *T2RecordDecl |
| 4051 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 4052 | |
| 4053 | OverloadCandidateSet CandidateSet(DeclLoc); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 4054 | std::pair<CXXRecordDecl::conversion_iterator, |
| 4055 | CXXRecordDecl::conversion_iterator> |
| 4056 | Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 4057 | for (CXXRecordDecl::conversion_iterator |
| 4058 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4059 | NamedDecl *D = *I; |
| 4060 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4061 | if (isa<UsingShadowDecl>(D)) |
| 4062 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 4063 | |
| 4064 | FunctionTemplateDecl *ConvTemplate |
| 4065 | = dyn_cast<FunctionTemplateDecl>(D); |
| 4066 | CXXConversionDecl *Conv; |
| 4067 | if (ConvTemplate) |
| 4068 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 4069 | else |
| 4070 | Conv = cast<CXXConversionDecl>(D); |
| 4071 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4072 | // 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] | 4073 | // explicit conversions, skip it. |
| 4074 | if (!AllowExplicit && Conv->isExplicit()) |
| 4075 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4076 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4077 | if (AllowRvalues) { |
| 4078 | bool DerivedToBase = false; |
| 4079 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4080 | bool ObjCLifetimeConversion = false; |
Douglas Gregor | 203050c | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4081 | |
| 4082 | // If we are initializing an rvalue reference, don't permit conversion |
| 4083 | // functions that return lvalues. |
| 4084 | if (!ConvTemplate && DeclType->isRValueReferenceType()) { |
| 4085 | const ReferenceType *RefType |
| 4086 | = Conv->getConversionType()->getAs<LValueReferenceType>(); |
| 4087 | if (RefType && !RefType->getPointeeType()->isFunctionType()) |
| 4088 | continue; |
| 4089 | } |
| 4090 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4091 | if (!ConvTemplate && |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4092 | S.CompareReferenceRelationship( |
| 4093 | DeclLoc, |
| 4094 | Conv->getConversionType().getNonReferenceType() |
| 4095 | .getUnqualifiedType(), |
| 4096 | DeclType.getNonReferenceType().getUnqualifiedType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4097 | DerivedToBase, ObjCConversion, ObjCLifetimeConversion) == |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4098 | Sema::Ref_Incompatible) |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4099 | continue; |
| 4100 | } else { |
| 4101 | // If the conversion function doesn't return a reference type, |
| 4102 | // it can't be considered for this conversion. An rvalue reference |
| 4103 | // is only acceptable if its referencee is a function type. |
| 4104 | |
| 4105 | const ReferenceType *RefType = |
| 4106 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 4107 | if (!RefType || |
| 4108 | (!RefType->isLValueReferenceType() && |
| 4109 | !RefType->getPointeeType()->isFunctionType())) |
| 4110 | continue; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4111 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4112 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4113 | if (ConvTemplate) |
| 4114 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4115 | Init, DeclType, CandidateSet); |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4116 | else |
| 4117 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4118 | DeclType, CandidateSet); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4119 | } |
| 4120 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4121 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4122 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4123 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4124 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4125 | case OR_Success: |
| 4126 | // C++ [over.ics.ref]p1: |
| 4127 | // |
| 4128 | // [...] If the parameter binds directly to the result of |
| 4129 | // applying a conversion function to the argument |
| 4130 | // expression, the implicit conversion sequence is a |
| 4131 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 4132 | // second standard conversion sequence either an identity |
| 4133 | // conversion or, if the conversion function returns an |
| 4134 | // entity of a type that is a derived class of the parameter |
| 4135 | // type, a derived-to-base Conversion. |
| 4136 | if (!Best->FinalConversion.DirectBinding) |
| 4137 | return false; |
| 4138 | |
| 4139 | ICS.setUserDefined(); |
| 4140 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 4141 | ICS.UserDefined.After = Best->FinalConversion; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4142 | ICS.UserDefined.HadMultipleCandidates = HadMultipleCandidates; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4143 | ICS.UserDefined.ConversionFunction = Best->Function; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 4144 | ICS.UserDefined.FoundConversionFunction = Best->FoundDecl; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4145 | ICS.UserDefined.EllipsisConversion = false; |
| 4146 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 4147 | ICS.UserDefined.After.DirectBinding && |
| 4148 | "Expected a direct reference binding!"); |
| 4149 | return true; |
| 4150 | |
| 4151 | case OR_Ambiguous: |
| 4152 | ICS.setAmbiguous(); |
| 4153 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4154 | Cand != CandidateSet.end(); ++Cand) |
| 4155 | if (Cand->Viable) |
| 4156 | ICS.Ambiguous.addConversion(Cand->Function); |
| 4157 | return true; |
| 4158 | |
| 4159 | case OR_No_Viable_Function: |
| 4160 | case OR_Deleted: |
| 4161 | // There was no suitable conversion, or we found a deleted |
| 4162 | // conversion; continue with other checks. |
| 4163 | return false; |
| 4164 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4165 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4166 | llvm_unreachable("Invalid OverloadResult!"); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4167 | } |
| 4168 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4169 | /// \brief Compute an implicit conversion sequence for reference |
| 4170 | /// initialization. |
| 4171 | static ImplicitConversionSequence |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4172 | TryReferenceInit(Sema &S, Expr *Init, QualType DeclType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4173 | SourceLocation DeclLoc, |
| 4174 | bool SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 4175 | bool AllowExplicit) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4176 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 4177 | |
| 4178 | // Most paths end in a failed conversion. |
| 4179 | ImplicitConversionSequence ICS; |
| 4180 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4181 | |
| 4182 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 4183 | QualType T2 = Init->getType(); |
| 4184 | |
| 4185 | // If the initializer is the address of an overloaded function, try |
| 4186 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4187 | // type of the resulting function. |
| 4188 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4189 | DeclAccessPair Found; |
| 4190 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 4191 | false, Found)) |
| 4192 | T2 = Fn->getType(); |
| 4193 | } |
| 4194 | |
| 4195 | // Compute some basic properties of the types and the initializer. |
| 4196 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 4197 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4198 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4199 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4200 | Expr::Classification InitCategory = Init->Classify(S.Context); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4201 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4202 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4203 | ObjCConversion, ObjCLifetimeConversion); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4204 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4205 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4206 | // C++0x [dcl.init.ref]p5: |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 4207 | // A reference to type "cv1 T1" is initialized by an expression |
| 4208 | // of type "cv2 T2" as follows: |
| 4209 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4210 | // -- If reference is an lvalue reference and the initializer expression |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4211 | if (!isRValRef) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4212 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 4213 | // reference-compatible with "cv2 T2," or |
| 4214 | // |
| 4215 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 4216 | if (InitCategory.isLValue() && |
| 4217 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4218 | // C++ [over.ics.ref]p1: |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4219 | // When a parameter of reference type binds directly (8.5.3) |
| 4220 | // to an argument expression, the implicit conversion sequence |
| 4221 | // is the identity conversion, unless the argument expression |
| 4222 | // has a type that is a derived class of the parameter type, |
| 4223 | // in which case the implicit conversion sequence is a |
| 4224 | // derived-to-base Conversion (13.3.3.1). |
| 4225 | ICS.setStandard(); |
| 4226 | ICS.Standard.First = ICK_Identity; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4227 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 4228 | : ObjCConversion? ICK_Compatible_Conversion |
| 4229 | : ICK_Identity; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4230 | ICS.Standard.Third = ICK_Identity; |
| 4231 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4232 | ICS.Standard.setToType(0, T2); |
| 4233 | ICS.Standard.setToType(1, T1); |
| 4234 | ICS.Standard.setToType(2, T1); |
| 4235 | ICS.Standard.ReferenceBinding = true; |
| 4236 | ICS.Standard.DirectBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4237 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4238 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4239 | ICS.Standard.BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4240 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4241 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4242 | ICS.Standard.CopyConstructor = 0; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4243 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4244 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 4245 | // derived-to-base conversions is suppressed when we're |
| 4246 | // computing the implicit conversion sequence (C++ |
| 4247 | // [over.best.ics]p2). |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4248 | return ICS; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4249 | } |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4250 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4251 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 4252 | // not reference-related to T2, and can be implicitly |
| 4253 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 4254 | // is reference-compatible with "cv3 T3" 92) (this |
| 4255 | // conversion is selected by enumerating the applicable |
| 4256 | // conversion functions (13.3.1.6) and choosing the best |
| 4257 | // one through overload resolution (13.3)), |
| 4258 | if (!SuppressUserConversions && T2->isRecordType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4259 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4260 | RefRelationship == Sema::Ref_Incompatible) { |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4261 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4262 | Init, T2, /*AllowRvalues=*/false, |
| 4263 | AllowExplicit)) |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4264 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4265 | } |
| 4266 | } |
| 4267 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4268 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 4269 | // 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] | 4270 | // shall be an rvalue reference. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4271 | // |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 4272 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 4273 | // point, which is that, due to p2 (which short-circuits reference |
| 4274 | // binding by only attempting a simple conversion for non-direct |
| 4275 | // bindings) and p3's strange wording, we allow a const volatile |
| 4276 | // reference to bind to an rvalue. Hence the check for the presence |
| 4277 | // of "const" rather than checking for "const" being the only |
| 4278 | // qualifier. |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4279 | // This is also the point where rvalue references and lvalue inits no longer |
| 4280 | // go together. |
Richard Smith | 8ab10aa | 2012-05-24 04:29:20 +0000 | [diff] [blame] | 4281 | if (!isRValRef && (!T1.isConstQualified() || T1.isVolatileQualified())) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4282 | return ICS; |
| 4283 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4284 | // -- If the initializer expression |
| 4285 | // |
| 4286 | // -- is an xvalue, class prvalue, array prvalue or function |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4287 | // lvalue and "cv1 T1" is reference-compatible with "cv2 T2", or |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4288 | if (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification && |
| 4289 | (InitCategory.isXValue() || |
| 4290 | (InitCategory.isPRValue() && (T2->isRecordType() || T2->isArrayType())) || |
| 4291 | (InitCategory.isLValue() && T2->isFunctionType()))) { |
| 4292 | ICS.setStandard(); |
| 4293 | ICS.Standard.First = ICK_Identity; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4294 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4295 | : ObjCConversion? ICK_Compatible_Conversion |
| 4296 | : ICK_Identity; |
| 4297 | ICS.Standard.Third = ICK_Identity; |
| 4298 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4299 | ICS.Standard.setToType(0, T2); |
| 4300 | ICS.Standard.setToType(1, T1); |
| 4301 | ICS.Standard.setToType(2, T1); |
| 4302 | ICS.Standard.ReferenceBinding = true; |
| 4303 | // In C++0x, this is always a direct binding. In C++98/03, it's a direct |
| 4304 | // binding unless we're binding to a class prvalue. |
| 4305 | // Note: Although xvalues wouldn't normally show up in C++98/03 code, we |
| 4306 | // allow the use of rvalue references in C++98/03 for the benefit of |
| 4307 | // standard library implementors; therefore, we need the xvalue check here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4308 | ICS.Standard.DirectBinding = |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4309 | S.getLangOpts().CPlusPlus11 || |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4310 | (InitCategory.isPRValue() && !T2->isRecordType()); |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4311 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4312 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4313 | ICS.Standard.BindsToRvalue = InitCategory.isRValue(); |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4314 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4315 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4316 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4317 | return ICS; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4318 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4319 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4320 | // -- has a class type (i.e., T2 is a class type), where T1 is not |
| 4321 | // reference-related to T2, and can be implicitly converted to |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4322 | // an xvalue, class prvalue, or function lvalue of type |
| 4323 | // "cv3 T3", where "cv1 T1" is reference-compatible with |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4324 | // "cv3 T3", |
| 4325 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4326 | // then the reference is bound to the value of the initializer |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4327 | // expression in the first case and to the result of the conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4328 | // in the second case (or, in either case, to an appropriate base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4329 | // class subobject). |
| 4330 | if (!SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4331 | T2->isRecordType() && !S.RequireCompleteType(DeclLoc, T2, 0) && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4332 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4333 | Init, T2, /*AllowRvalues=*/true, |
| 4334 | AllowExplicit)) { |
| 4335 | // In the second case, if the reference is an rvalue reference |
| 4336 | // and the second standard conversion sequence of the |
| 4337 | // user-defined conversion sequence includes an lvalue-to-rvalue |
| 4338 | // conversion, the program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4339 | if (ICS.isUserDefined() && isRValRef && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4340 | ICS.UserDefined.After.First == ICK_Lvalue_To_Rvalue) |
| 4341 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4342 | |
Douglas Gregor | 68ed68b | 2011-01-21 16:36:05 +0000 | [diff] [blame] | 4343 | return ICS; |
Rafael Espindola | aa5952c | 2011-01-22 15:32:35 +0000 | [diff] [blame] | 4344 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4345 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4346 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 4347 | // initialized from the initializer expression using the |
| 4348 | // rules for a non-reference copy initialization (8.5). The |
| 4349 | // reference is then bound to the temporary. If T1 is |
| 4350 | // reference-related to T2, cv1 must be the same |
| 4351 | // cv-qualification as, or greater cv-qualification than, |
| 4352 | // cv2; otherwise, the program is ill-formed. |
| 4353 | if (RefRelationship == Sema::Ref_Related) { |
| 4354 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 4355 | // we would be reference-compatible or reference-compatible with |
| 4356 | // added qualification. But that wasn't the case, so the reference |
| 4357 | // initialization fails. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4358 | // |
| 4359 | // Note that we only want to check address spaces and cvr-qualifiers here. |
| 4360 | // ObjC GC and lifetime qualifiers aren't important. |
| 4361 | Qualifiers T1Quals = T1.getQualifiers(); |
| 4362 | Qualifiers T2Quals = T2.getQualifiers(); |
| 4363 | T1Quals.removeObjCGCAttr(); |
| 4364 | T1Quals.removeObjCLifetime(); |
| 4365 | T2Quals.removeObjCGCAttr(); |
| 4366 | T2Quals.removeObjCLifetime(); |
| 4367 | if (!T1Quals.compatiblyIncludes(T2Quals)) |
| 4368 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4369 | } |
| 4370 | |
| 4371 | // If at least one of the types is a class type, the types are not |
| 4372 | // related, and we aren't allowed any user conversions, the |
| 4373 | // reference binding fails. This case is important for breaking |
| 4374 | // recursion, since TryImplicitConversion below will attempt to |
| 4375 | // create a temporary through the use of a copy constructor. |
| 4376 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 4377 | (T1->isRecordType() || T2->isRecordType())) |
| 4378 | return ICS; |
| 4379 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4380 | // If T1 is reference-related to T2 and the reference is an rvalue |
| 4381 | // reference, the initializer expression shall not be an lvalue. |
| 4382 | if (RefRelationship >= Sema::Ref_Related && |
| 4383 | isRValRef && Init->Classify(S.Context).isLValue()) |
| 4384 | return ICS; |
| 4385 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4386 | // C++ [over.ics.ref]p2: |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4387 | // When a parameter of reference type is not bound directly to |
| 4388 | // an argument expression, the conversion sequence is the one |
| 4389 | // required to convert the argument expression to the |
| 4390 | // underlying type of the reference according to |
| 4391 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 4392 | // to copy-initializing a temporary of the underlying type with |
| 4393 | // the argument expression. Any difference in top-level |
| 4394 | // cv-qualification is subsumed by the initialization itself |
| 4395 | // and does not constitute a conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4396 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
| 4397 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4398 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4399 | /*CStyle=*/false, |
| 4400 | /*AllowObjCWritebackConversion=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4401 | |
| 4402 | // Of course, that's still a reference binding. |
| 4403 | if (ICS.isStandard()) { |
| 4404 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4405 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4406 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4407 | ICS.Standard.BindsToRvalue = true; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4408 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4409 | ICS.Standard.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4410 | } else if (ICS.isUserDefined()) { |
Douglas Gregor | 203050c | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4411 | // Don't allow rvalue references to bind to lvalues. |
| 4412 | if (DeclType->isRValueReferenceType()) { |
| 4413 | if (const ReferenceType *RefType |
| 4414 | = ICS.UserDefined.ConversionFunction->getResultType() |
| 4415 | ->getAs<LValueReferenceType>()) { |
| 4416 | if (!RefType->getPointeeType()->isFunctionType()) { |
| 4417 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, Init, |
| 4418 | DeclType); |
| 4419 | return ICS; |
| 4420 | } |
| 4421 | } |
| 4422 | } |
| 4423 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4424 | ICS.UserDefined.After.ReferenceBinding = true; |
Douglas Gregor | f20d272 | 2011-08-15 13:59:46 +0000 | [diff] [blame] | 4425 | ICS.UserDefined.After.IsLvalueReference = !isRValRef; |
| 4426 | ICS.UserDefined.After.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4427 | ICS.UserDefined.After.BindsToRvalue = true; |
| 4428 | ICS.UserDefined.After.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4429 | ICS.UserDefined.After.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4430 | } |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4431 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4432 | return ICS; |
| 4433 | } |
| 4434 | |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4435 | static ImplicitConversionSequence |
| 4436 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
| 4437 | bool SuppressUserConversions, |
| 4438 | bool InOverloadResolution, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4439 | bool AllowObjCWritebackConversion, |
| 4440 | bool AllowExplicit = false); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4441 | |
| 4442 | /// TryListConversion - Try to copy-initialize a value of type ToType from the |
| 4443 | /// initializer list From. |
| 4444 | static ImplicitConversionSequence |
| 4445 | TryListConversion(Sema &S, InitListExpr *From, QualType ToType, |
| 4446 | bool SuppressUserConversions, |
| 4447 | bool InOverloadResolution, |
| 4448 | bool AllowObjCWritebackConversion) { |
| 4449 | // C++11 [over.ics.list]p1: |
| 4450 | // When an argument is an initializer list, it is not an expression and |
| 4451 | // special rules apply for converting it to a parameter type. |
| 4452 | |
| 4453 | ImplicitConversionSequence Result; |
| 4454 | Result.setBad(BadConversionSequence::no_conversion, From, ToType); |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4455 | Result.setListInitializationSequence(); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4456 | |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4457 | // 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] | 4458 | // initialized from init lists. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4459 | if (S.RequireCompleteType(From->getLocStart(), ToType, 0)) |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4460 | return Result; |
| 4461 | |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4462 | // C++11 [over.ics.list]p2: |
| 4463 | // If the parameter type is std::initializer_list<X> or "array of X" and |
| 4464 | // all the elements can be implicitly converted to X, the implicit |
| 4465 | // conversion sequence is the worst conversion necessary to convert an |
| 4466 | // element of the list to X. |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4467 | bool toStdInitializerList = false; |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4468 | QualType X; |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4469 | if (ToType->isArrayType()) |
Richard Smith | 2801d9a | 2012-12-09 06:48:56 +0000 | [diff] [blame] | 4470 | X = S.Context.getAsArrayType(ToType)->getElementType(); |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4471 | else |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4472 | toStdInitializerList = S.isStdInitializerList(ToType, &X); |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4473 | if (!X.isNull()) { |
| 4474 | for (unsigned i = 0, e = From->getNumInits(); i < e; ++i) { |
| 4475 | Expr *Init = From->getInit(i); |
| 4476 | ImplicitConversionSequence ICS = |
| 4477 | TryCopyInitialization(S, Init, X, SuppressUserConversions, |
| 4478 | InOverloadResolution, |
| 4479 | AllowObjCWritebackConversion); |
| 4480 | // If a single element isn't convertible, fail. |
| 4481 | if (ICS.isBad()) { |
| 4482 | Result = ICS; |
| 4483 | break; |
| 4484 | } |
| 4485 | // Otherwise, look for the worst conversion. |
| 4486 | if (Result.isBad() || |
| 4487 | CompareImplicitConversionSequences(S, ICS, Result) == |
| 4488 | ImplicitConversionSequence::Worse) |
| 4489 | Result = ICS; |
| 4490 | } |
Douglas Gregor | 5b4bf13 | 2012-04-04 23:09:20 +0000 | [diff] [blame] | 4491 | |
| 4492 | // For an empty list, we won't have computed any conversion sequence. |
| 4493 | // Introduce the identity conversion sequence. |
| 4494 | if (From->getNumInits() == 0) { |
| 4495 | Result.setStandard(); |
| 4496 | Result.Standard.setAsIdentityConversion(); |
| 4497 | Result.Standard.setFromType(ToType); |
| 4498 | Result.Standard.setAllToTypes(ToType); |
| 4499 | } |
| 4500 | |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4501 | Result.setListInitializationSequence(); |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4502 | Result.setStdInitializerListElement(toStdInitializerList); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4503 | return Result; |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4504 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4505 | |
| 4506 | // C++11 [over.ics.list]p3: |
| 4507 | // Otherwise, if the parameter is a non-aggregate class X and overload |
| 4508 | // resolution chooses a single best constructor [...] the implicit |
| 4509 | // conversion sequence is a user-defined conversion sequence. If multiple |
| 4510 | // constructors are viable but none is better than the others, the |
| 4511 | // implicit conversion sequence is a user-defined conversion sequence. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4512 | if (ToType->isRecordType() && !ToType->isAggregateType()) { |
| 4513 | // This function can deal with initializer lists. |
| 4514 | Result = TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 4515 | /*AllowExplicit=*/false, |
| 4516 | InOverloadResolution, /*CStyle=*/false, |
| 4517 | AllowObjCWritebackConversion); |
| 4518 | Result.setListInitializationSequence(); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4519 | return Result; |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4520 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4521 | |
| 4522 | // C++11 [over.ics.list]p4: |
| 4523 | // Otherwise, if the parameter has an aggregate type which can be |
| 4524 | // initialized from the initializer list [...] the implicit conversion |
| 4525 | // sequence is a user-defined conversion sequence. |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4526 | if (ToType->isAggregateType()) { |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4527 | // Type is an aggregate, argument is an init list. At this point it comes |
| 4528 | // down to checking whether the initialization works. |
| 4529 | // FIXME: Find out whether this parameter is consumed or not. |
| 4530 | InitializedEntity Entity = |
| 4531 | InitializedEntity::InitializeParameter(S.Context, ToType, |
| 4532 | /*Consumed=*/false); |
| 4533 | if (S.CanPerformCopyInitialization(Entity, S.Owned(From))) { |
| 4534 | Result.setUserDefined(); |
| 4535 | Result.UserDefined.Before.setAsIdentityConversion(); |
| 4536 | // Initializer lists don't have a type. |
| 4537 | Result.UserDefined.Before.setFromType(QualType()); |
| 4538 | Result.UserDefined.Before.setAllToTypes(QualType()); |
| 4539 | |
| 4540 | Result.UserDefined.After.setAsIdentityConversion(); |
| 4541 | Result.UserDefined.After.setFromType(ToType); |
| 4542 | Result.UserDefined.After.setAllToTypes(ToType); |
Benjamin Kramer | 83db10e | 2012-02-02 19:35:29 +0000 | [diff] [blame] | 4543 | Result.UserDefined.ConversionFunction = 0; |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4544 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4545 | return Result; |
| 4546 | } |
| 4547 | |
| 4548 | // C++11 [over.ics.list]p5: |
| 4549 | // 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] | 4550 | if (ToType->isReferenceType()) { |
| 4551 | // The standard is notoriously unclear here, since 13.3.3.1.4 doesn't |
| 4552 | // mention initializer lists in any way. So we go by what list- |
| 4553 | // initialization would do and try to extrapolate from that. |
| 4554 | |
| 4555 | QualType T1 = ToType->getAs<ReferenceType>()->getPointeeType(); |
| 4556 | |
| 4557 | // If the initializer list has a single element that is reference-related |
| 4558 | // to the parameter type, we initialize the reference from that. |
| 4559 | if (From->getNumInits() == 1) { |
| 4560 | Expr *Init = From->getInit(0); |
| 4561 | |
| 4562 | QualType T2 = Init->getType(); |
| 4563 | |
| 4564 | // If the initializer is the address of an overloaded function, try |
| 4565 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4566 | // type of the resulting function. |
| 4567 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4568 | DeclAccessPair Found; |
| 4569 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction( |
| 4570 | Init, ToType, false, Found)) |
| 4571 | T2 = Fn->getType(); |
| 4572 | } |
| 4573 | |
| 4574 | // Compute some basic properties of the types and the initializer. |
| 4575 | bool dummy1 = false; |
| 4576 | bool dummy2 = false; |
| 4577 | bool dummy3 = false; |
| 4578 | Sema::ReferenceCompareResult RefRelationship |
| 4579 | = S.CompareReferenceRelationship(From->getLocStart(), T1, T2, dummy1, |
| 4580 | dummy2, dummy3); |
| 4581 | |
| 4582 | if (RefRelationship >= Sema::Ref_Related) |
| 4583 | return TryReferenceInit(S, Init, ToType, |
| 4584 | /*FIXME:*/From->getLocStart(), |
| 4585 | SuppressUserConversions, |
| 4586 | /*AllowExplicit=*/false); |
| 4587 | } |
| 4588 | |
| 4589 | // Otherwise, we bind the reference to a temporary created from the |
| 4590 | // initializer list. |
| 4591 | Result = TryListConversion(S, From, T1, SuppressUserConversions, |
| 4592 | InOverloadResolution, |
| 4593 | AllowObjCWritebackConversion); |
| 4594 | if (Result.isFailure()) |
| 4595 | return Result; |
| 4596 | assert(!Result.isEllipsis() && |
| 4597 | "Sub-initialization cannot result in ellipsis conversion."); |
| 4598 | |
| 4599 | // Can we even bind to a temporary? |
| 4600 | if (ToType->isRValueReferenceType() || |
| 4601 | (T1.isConstQualified() && !T1.isVolatileQualified())) { |
| 4602 | StandardConversionSequence &SCS = Result.isStandard() ? Result.Standard : |
| 4603 | Result.UserDefined.After; |
| 4604 | SCS.ReferenceBinding = true; |
| 4605 | SCS.IsLvalueReference = ToType->isLValueReferenceType(); |
| 4606 | SCS.BindsToRvalue = true; |
| 4607 | SCS.BindsToFunctionLvalue = false; |
| 4608 | SCS.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4609 | SCS.ObjCLifetimeConversionBinding = false; |
| 4610 | } else |
| 4611 | Result.setBad(BadConversionSequence::lvalue_ref_to_rvalue, |
| 4612 | From, ToType); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4613 | return Result; |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4614 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4615 | |
| 4616 | // C++11 [over.ics.list]p6: |
| 4617 | // Otherwise, if the parameter type is not a class: |
| 4618 | if (!ToType->isRecordType()) { |
| 4619 | // - if the initializer list has one element, the implicit conversion |
| 4620 | // sequence is the one required to convert the element to the |
| 4621 | // parameter type. |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4622 | unsigned NumInits = From->getNumInits(); |
| 4623 | if (NumInits == 1) |
| 4624 | Result = TryCopyInitialization(S, From->getInit(0), ToType, |
| 4625 | SuppressUserConversions, |
| 4626 | InOverloadResolution, |
| 4627 | AllowObjCWritebackConversion); |
| 4628 | // - if the initializer list has no elements, the implicit conversion |
| 4629 | // sequence is the identity conversion. |
| 4630 | else if (NumInits == 0) { |
| 4631 | Result.setStandard(); |
| 4632 | Result.Standard.setAsIdentityConversion(); |
John McCall | e14ba2c | 2012-04-04 02:40:27 +0000 | [diff] [blame] | 4633 | Result.Standard.setFromType(ToType); |
| 4634 | Result.Standard.setAllToTypes(ToType); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4635 | } |
Sebastian Redl | 2422e82 | 2012-02-28 23:36:38 +0000 | [diff] [blame] | 4636 | Result.setListInitializationSequence(); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4637 | return Result; |
| 4638 | } |
| 4639 | |
| 4640 | // C++11 [over.ics.list]p7: |
| 4641 | // In all cases other than those enumerated above, no conversion is possible |
| 4642 | return Result; |
| 4643 | } |
| 4644 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4645 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 4646 | /// ToType from the expression From. Return the implicit conversion |
| 4647 | /// sequence required to pass this argument, which may be a bad |
| 4648 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4649 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | 74e386e | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 4650 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4651 | static ImplicitConversionSequence |
| 4652 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4653 | bool SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4654 | bool InOverloadResolution, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4655 | bool AllowObjCWritebackConversion, |
| 4656 | bool AllowExplicit) { |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4657 | if (InitListExpr *FromInitList = dyn_cast<InitListExpr>(From)) |
| 4658 | return TryListConversion(S, FromInitList, ToType, SuppressUserConversions, |
| 4659 | InOverloadResolution,AllowObjCWritebackConversion); |
| 4660 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4661 | if (ToType->isReferenceType()) |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4662 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4663 | /*FIXME:*/From->getLocStart(), |
| 4664 | SuppressUserConversions, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4665 | AllowExplicit); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4666 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4667 | return TryImplicitConversion(S, From, ToType, |
| 4668 | SuppressUserConversions, |
| 4669 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4670 | InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4671 | /*CStyle=*/false, |
| 4672 | AllowObjCWritebackConversion); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4673 | } |
| 4674 | |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 4675 | static bool TryCopyInitialization(const CanQualType FromQTy, |
| 4676 | const CanQualType ToQTy, |
| 4677 | Sema &S, |
| 4678 | SourceLocation Loc, |
| 4679 | ExprValueKind FromVK) { |
| 4680 | OpaqueValueExpr TmpExpr(Loc, FromQTy, FromVK); |
| 4681 | ImplicitConversionSequence ICS = |
| 4682 | TryCopyInitialization(S, &TmpExpr, ToQTy, true, true, false); |
| 4683 | |
| 4684 | return !ICS.isBad(); |
| 4685 | } |
| 4686 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4687 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 4688 | /// parameter of the given member function (@c Method) from the |
| 4689 | /// expression @p From. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4690 | static ImplicitConversionSequence |
Richard Smith | 98bfbf5 | 2013-01-26 02:07:32 +0000 | [diff] [blame] | 4691 | TryObjectArgumentInitialization(Sema &S, QualType FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4692 | Expr::Classification FromClassification, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4693 | CXXMethodDecl *Method, |
| 4694 | CXXRecordDecl *ActingContext) { |
| 4695 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 4696 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 4697 | // const volatile object. |
| 4698 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 4699 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4700 | QualType ImplicitParamType = S.Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4701 | |
| 4702 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 4703 | // to exit early. |
| 4704 | ImplicitConversionSequence ICS; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4705 | |
| 4706 | // We need to have an object of class type. |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4707 | if (const PointerType *PT = FromType->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4708 | FromType = PT->getPointeeType(); |
| 4709 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4710 | // When we had a pointer, it's implicitly dereferenced, so we |
| 4711 | // better have an lvalue. |
| 4712 | assert(FromClassification.isLValue()); |
| 4713 | } |
| 4714 | |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4715 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4716 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4717 | // C++0x [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4718 | // For non-static member functions, the type of the implicit object |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4719 | // parameter is |
| 4720 | // |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4721 | // - "lvalue reference to cv X" for functions declared without a |
| 4722 | // ref-qualifier or with the & ref-qualifier |
| 4723 | // - "rvalue reference to cv X" for functions declared with the && |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4724 | // ref-qualifier |
| 4725 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4726 | // 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] | 4727 | // cv-qualification on the member function declaration. |
| 4728 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4729 | // However, when finding an implicit conversion sequence for the argument, we |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4730 | // are not allowed to create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4731 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 4732 | // reference binding here, that allows class rvalues to bind to |
| 4733 | // non-constant references. |
| 4734 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4735 | // First check the qualifiers. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4736 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4737 | if (ImplicitParamType.getCVRQualifiers() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4738 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4739 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4740 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
Richard Smith | 98bfbf5 | 2013-01-26 02:07:32 +0000 | [diff] [blame] | 4741 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4742 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4743 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4744 | |
| 4745 | // Check that we have either the same type or a derived type. It |
| 4746 | // affects the conversion rank. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4747 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4748 | ImplicitConversionKind SecondKind; |
| 4749 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 4750 | SecondKind = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4751 | } else if (S.IsDerivedFrom(FromType, ClassType)) |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4752 | SecondKind = ICK_Derived_To_Base; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4753 | else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4754 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 4755 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4756 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4757 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4758 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4759 | // Check the ref-qualifier. |
| 4760 | switch (Method->getRefQualifier()) { |
| 4761 | case RQ_None: |
| 4762 | // Do nothing; we don't care about lvalueness or rvalueness. |
| 4763 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4764 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4765 | case RQ_LValue: |
| 4766 | if (!FromClassification.isLValue() && Quals != Qualifiers::Const) { |
| 4767 | // non-const lvalue reference cannot bind to an rvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4768 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4769 | ImplicitParamType); |
| 4770 | return ICS; |
| 4771 | } |
| 4772 | break; |
| 4773 | |
| 4774 | case RQ_RValue: |
| 4775 | if (!FromClassification.isRValue()) { |
| 4776 | // rvalue reference cannot bind to an lvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4777 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4778 | ImplicitParamType); |
| 4779 | return ICS; |
| 4780 | } |
| 4781 | break; |
| 4782 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4783 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4784 | // Success. Mark this as a reference binding. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4785 | ICS.setStandard(); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4786 | ICS.Standard.setAsIdentityConversion(); |
| 4787 | ICS.Standard.Second = SecondKind; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4788 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 4789 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4790 | ICS.Standard.ReferenceBinding = true; |
| 4791 | ICS.Standard.DirectBinding = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4792 | ICS.Standard.IsLvalueReference = Method->getRefQualifier() != RQ_RValue; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4793 | ICS.Standard.BindsToFunctionLvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4794 | ICS.Standard.BindsToRvalue = FromClassification.isRValue(); |
| 4795 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier |
| 4796 | = (Method->getRefQualifier() == RQ_None); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4797 | return ICS; |
| 4798 | } |
| 4799 | |
| 4800 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 4801 | /// the implicit object parameter for the given Method with the given |
| 4802 | /// expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4803 | ExprResult |
| 4804 | Sema::PerformObjectArgumentInitialization(Expr *From, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4805 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4806 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4807 | CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4808 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4809 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4810 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4811 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4812 | Expr::Classification FromClassification; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4813 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4814 | FromRecordType = PT->getPointeeType(); |
| 4815 | DestType = Method->getThisType(Context); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4816 | FromClassification = Expr::Classification::makeSimpleLValue(); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4817 | } else { |
| 4818 | FromRecordType = From->getType(); |
| 4819 | DestType = ImplicitParamRecordType; |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4820 | FromClassification = From->Classify(Context); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4821 | } |
| 4822 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4823 | // Note that we always use the true parent context when performing |
| 4824 | // the actual argument initialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4825 | ImplicitConversionSequence ICS |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4826 | = TryObjectArgumentInitialization(*this, From->getType(), FromClassification, |
| 4827 | Method, Method->getParent()); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4828 | if (ICS.isBad()) { |
| 4829 | if (ICS.Bad.Kind == BadConversionSequence::bad_qualifiers) { |
| 4830 | Qualifiers FromQs = FromRecordType.getQualifiers(); |
| 4831 | Qualifiers ToQs = DestType.getQualifiers(); |
| 4832 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 4833 | if (CVR) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4834 | Diag(From->getLocStart(), |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4835 | diag::err_member_function_call_bad_cvr) |
| 4836 | << Method->getDeclName() << FromRecordType << (CVR - 1) |
| 4837 | << From->getSourceRange(); |
| 4838 | Diag(Method->getLocation(), diag::note_previous_decl) |
| 4839 | << Method->getDeclName(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4840 | return ExprError(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4841 | } |
| 4842 | } |
| 4843 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4844 | return Diag(From->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4845 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4846 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4847 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4848 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4849 | if (ICS.Standard.Second == ICK_Derived_To_Base) { |
| 4850 | ExprResult FromRes = |
| 4851 | PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
| 4852 | if (FromRes.isInvalid()) |
| 4853 | return ExprError(); |
| 4854 | From = FromRes.take(); |
| 4855 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4856 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4857 | if (!Context.hasSameType(From->getType(), DestType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4858 | From = ImpCastExprToType(From, DestType, CK_NoOp, |
Richard Smith | acdfa4d | 2011-11-10 23:32:36 +0000 | [diff] [blame] | 4859 | From->getValueKind()).take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4860 | return Owned(From); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4861 | } |
| 4862 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4863 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 4864 | /// expression From to bool (C++0x [conv]p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4865 | static ImplicitConversionSequence |
| 4866 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
Douglas Gregor | c6dfe19 | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 4867 | // FIXME: This is pretty broken. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4868 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 4869 | // FIXME: Are these flags correct? |
| 4870 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4871 | /*AllowExplicit=*/true, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4872 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4873 | /*CStyle=*/false, |
| 4874 | /*AllowObjCWritebackConversion=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4875 | } |
| 4876 | |
| 4877 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 4878 | /// of the expression From to bool (C++0x [conv]p3). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4879 | ExprResult Sema::PerformContextuallyConvertToBool(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4880 | if (checkPlaceholderForOverload(*this, From)) |
| 4881 | return ExprError(); |
| 4882 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4883 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4884 | if (!ICS.isBad()) |
| 4885 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4886 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 4887 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4888 | return Diag(From->getLocStart(), |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4889 | diag::err_typecheck_bool_condition) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 4890 | << From->getType() << From->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4891 | return ExprError(); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4892 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4893 | |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4894 | /// Check that the specified conversion is permitted in a converted constant |
| 4895 | /// expression, according to C++11 [expr.const]p3. Return true if the conversion |
| 4896 | /// is acceptable. |
| 4897 | static bool CheckConvertedConstantConversions(Sema &S, |
| 4898 | StandardConversionSequence &SCS) { |
| 4899 | // Since we know that the target type is an integral or unscoped enumeration |
| 4900 | // type, most conversion kinds are impossible. All possible First and Third |
| 4901 | // conversions are fine. |
| 4902 | switch (SCS.Second) { |
| 4903 | case ICK_Identity: |
| 4904 | case ICK_Integral_Promotion: |
| 4905 | case ICK_Integral_Conversion: |
Guy Benyei | 6959acd | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 4906 | case ICK_Zero_Event_Conversion: |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4907 | return true; |
| 4908 | |
| 4909 | case ICK_Boolean_Conversion: |
Richard Smith | 2bcb984 | 2012-09-13 22:00:12 +0000 | [diff] [blame] | 4910 | // Conversion from an integral or unscoped enumeration type to bool is |
| 4911 | // classified as ICK_Boolean_Conversion, but it's also an integral |
| 4912 | // conversion, so it's permitted in a converted constant expression. |
| 4913 | return SCS.getFromType()->isIntegralOrUnscopedEnumerationType() && |
| 4914 | SCS.getToType(2)->isBooleanType(); |
| 4915 | |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4916 | case ICK_Floating_Integral: |
| 4917 | case ICK_Complex_Real: |
| 4918 | return false; |
| 4919 | |
| 4920 | case ICK_Lvalue_To_Rvalue: |
| 4921 | case ICK_Array_To_Pointer: |
| 4922 | case ICK_Function_To_Pointer: |
| 4923 | case ICK_NoReturn_Adjustment: |
| 4924 | case ICK_Qualification: |
| 4925 | case ICK_Compatible_Conversion: |
| 4926 | case ICK_Vector_Conversion: |
| 4927 | case ICK_Vector_Splat: |
| 4928 | case ICK_Derived_To_Base: |
| 4929 | case ICK_Pointer_Conversion: |
| 4930 | case ICK_Pointer_Member: |
| 4931 | case ICK_Block_Pointer_Conversion: |
| 4932 | case ICK_Writeback_Conversion: |
| 4933 | case ICK_Floating_Promotion: |
| 4934 | case ICK_Complex_Promotion: |
| 4935 | case ICK_Complex_Conversion: |
| 4936 | case ICK_Floating_Conversion: |
| 4937 | case ICK_TransparentUnionConversion: |
| 4938 | llvm_unreachable("unexpected second conversion kind"); |
| 4939 | |
| 4940 | case ICK_Num_Conversion_Kinds: |
| 4941 | break; |
| 4942 | } |
| 4943 | |
| 4944 | llvm_unreachable("unknown conversion kind"); |
| 4945 | } |
| 4946 | |
| 4947 | /// CheckConvertedConstantExpression - Check that the expression From is a |
| 4948 | /// converted constant expression of type T, perform the conversion and produce |
| 4949 | /// the converted expression, per C++11 [expr.const]p3. |
| 4950 | ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, |
| 4951 | llvm::APSInt &Value, |
| 4952 | CCEKind CCE) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4953 | assert(LangOpts.CPlusPlus11 && "converted constant expression outside C++11"); |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4954 | assert(T->isIntegralOrEnumerationType() && "unexpected converted const type"); |
| 4955 | |
| 4956 | if (checkPlaceholderForOverload(*this, From)) |
| 4957 | return ExprError(); |
| 4958 | |
| 4959 | // C++11 [expr.const]p3 with proposed wording fixes: |
| 4960 | // A converted constant expression of type T is a core constant expression, |
| 4961 | // implicitly converted to a prvalue of type T, where the converted |
| 4962 | // expression is a literal constant expression and the implicit conversion |
| 4963 | // sequence contains only user-defined conversions, lvalue-to-rvalue |
| 4964 | // conversions, integral promotions, and integral conversions other than |
| 4965 | // narrowing conversions. |
| 4966 | ImplicitConversionSequence ICS = |
| 4967 | TryImplicitConversion(From, T, |
| 4968 | /*SuppressUserConversions=*/false, |
| 4969 | /*AllowExplicit=*/false, |
| 4970 | /*InOverloadResolution=*/false, |
| 4971 | /*CStyle=*/false, |
| 4972 | /*AllowObjcWritebackConversion=*/false); |
| 4973 | StandardConversionSequence *SCS = 0; |
| 4974 | switch (ICS.getKind()) { |
| 4975 | case ImplicitConversionSequence::StandardConversion: |
| 4976 | if (!CheckConvertedConstantConversions(*this, ICS.Standard)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4977 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4978 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 4979 | << From->getType() << From->getSourceRange() << T; |
| 4980 | SCS = &ICS.Standard; |
| 4981 | break; |
| 4982 | case ImplicitConversionSequence::UserDefinedConversion: |
| 4983 | // We are converting from class type to an integral or enumeration type, so |
| 4984 | // the Before sequence must be trivial. |
| 4985 | if (!CheckConvertedConstantConversions(*this, ICS.UserDefined.After)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4986 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4987 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 4988 | << From->getType() << From->getSourceRange() << T; |
| 4989 | SCS = &ICS.UserDefined.After; |
| 4990 | break; |
| 4991 | case ImplicitConversionSequence::AmbiguousConversion: |
| 4992 | case ImplicitConversionSequence::BadConversion: |
| 4993 | if (!DiagnoseMultipleUserDefinedConversion(From, T)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4994 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4995 | diag::err_typecheck_converted_constant_expression) |
| 4996 | << From->getType() << From->getSourceRange() << T; |
| 4997 | return ExprError(); |
| 4998 | |
| 4999 | case ImplicitConversionSequence::EllipsisConversion: |
| 5000 | llvm_unreachable("ellipsis conversion in converted constant expression"); |
| 5001 | } |
| 5002 | |
| 5003 | ExprResult Result = PerformImplicitConversion(From, T, ICS, AA_Converting); |
| 5004 | if (Result.isInvalid()) |
| 5005 | return Result; |
| 5006 | |
| 5007 | // Check for a narrowing implicit conversion. |
| 5008 | APValue PreNarrowingValue; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 5009 | QualType PreNarrowingType; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 5010 | switch (SCS->getNarrowingKind(Context, Result.get(), PreNarrowingValue, |
| 5011 | PreNarrowingType)) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5012 | case NK_Variable_Narrowing: |
| 5013 | // Implicit conversion to a narrower type, and the value is not a constant |
| 5014 | // expression. We'll diagnose this in a moment. |
| 5015 | case NK_Not_Narrowing: |
| 5016 | break; |
| 5017 | |
| 5018 | case NK_Constant_Narrowing: |
Eli Friedman | 1ef28db | 2012-03-29 23:39:39 +0000 | [diff] [blame] | 5019 | Diag(From->getLocStart(), |
| 5020 | isSFINAEContext() ? diag::err_cce_narrowing_sfinae : |
| 5021 | diag::err_cce_narrowing) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5022 | << CCE << /*Constant*/1 |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 5023 | << PreNarrowingValue.getAsString(Context, PreNarrowingType) << T; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5024 | break; |
| 5025 | |
| 5026 | case NK_Type_Narrowing: |
Eli Friedman | 1ef28db | 2012-03-29 23:39:39 +0000 | [diff] [blame] | 5027 | Diag(From->getLocStart(), |
| 5028 | isSFINAEContext() ? diag::err_cce_narrowing_sfinae : |
| 5029 | diag::err_cce_narrowing) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5030 | << CCE << /*Constant*/0 << From->getType() << T; |
| 5031 | break; |
| 5032 | } |
| 5033 | |
| 5034 | // Check the expression is a constant expression. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5035 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5036 | Expr::EvalResult Eval; |
| 5037 | Eval.Diag = &Notes; |
| 5038 | |
Douglas Gregor | 484f6fa | 2013-04-08 23:24:07 +0000 | [diff] [blame] | 5039 | if (!Result.get()->EvaluateAsRValue(Eval, Context) || !Eval.Val.isInt()) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5040 | // The expression can't be folded, so we can't keep it at this position in |
| 5041 | // the AST. |
| 5042 | Result = ExprError(); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5043 | } else { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5044 | Value = Eval.Val.getInt(); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5045 | |
| 5046 | if (Notes.empty()) { |
| 5047 | // It's a constant expression. |
| 5048 | return Result; |
| 5049 | } |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5050 | } |
| 5051 | |
| 5052 | // It's not a constant expression. Produce an appropriate diagnostic. |
| 5053 | if (Notes.size() == 1 && |
| 5054 | Notes[0].second.getDiagID() == diag::note_invalid_subexpr_in_const_expr) |
| 5055 | Diag(Notes[0].first, diag::err_expr_not_cce) << CCE; |
| 5056 | else { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5057 | Diag(From->getLocStart(), diag::err_expr_not_cce) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5058 | << CCE << From->getSourceRange(); |
| 5059 | for (unsigned I = 0; I < Notes.size(); ++I) |
| 5060 | Diag(Notes[I].first, Notes[I].second); |
| 5061 | } |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5062 | return Result; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5063 | } |
| 5064 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5065 | /// dropPointerConversions - If the given standard conversion sequence |
| 5066 | /// involves any pointer conversions, remove them. This may change |
| 5067 | /// the result type of the conversion sequence. |
| 5068 | static void dropPointerConversion(StandardConversionSequence &SCS) { |
| 5069 | if (SCS.Second == ICK_Pointer_Conversion) { |
| 5070 | SCS.Second = ICK_Identity; |
| 5071 | SCS.Third = ICK_Identity; |
| 5072 | SCS.ToTypePtrs[2] = SCS.ToTypePtrs[1] = SCS.ToTypePtrs[0]; |
| 5073 | } |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5074 | } |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5075 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5076 | /// TryContextuallyConvertToObjCPointer - Attempt to contextually |
| 5077 | /// convert the expression From to an Objective-C pointer type. |
| 5078 | static ImplicitConversionSequence |
| 5079 | TryContextuallyConvertToObjCPointer(Sema &S, Expr *From) { |
| 5080 | // Do an implicit conversion to 'id'. |
| 5081 | QualType Ty = S.Context.getObjCIdType(); |
| 5082 | ImplicitConversionSequence ICS |
| 5083 | = TryImplicitConversion(S, From, Ty, |
| 5084 | // FIXME: Are these flags correct? |
| 5085 | /*SuppressUserConversions=*/false, |
| 5086 | /*AllowExplicit=*/true, |
| 5087 | /*InOverloadResolution=*/false, |
| 5088 | /*CStyle=*/false, |
| 5089 | /*AllowObjCWritebackConversion=*/false); |
| 5090 | |
| 5091 | // Strip off any final conversions to 'id'. |
| 5092 | switch (ICS.getKind()) { |
| 5093 | case ImplicitConversionSequence::BadConversion: |
| 5094 | case ImplicitConversionSequence::AmbiguousConversion: |
| 5095 | case ImplicitConversionSequence::EllipsisConversion: |
| 5096 | break; |
| 5097 | |
| 5098 | case ImplicitConversionSequence::UserDefinedConversion: |
| 5099 | dropPointerConversion(ICS.UserDefined.After); |
| 5100 | break; |
| 5101 | |
| 5102 | case ImplicitConversionSequence::StandardConversion: |
| 5103 | dropPointerConversion(ICS.Standard); |
| 5104 | break; |
| 5105 | } |
| 5106 | |
| 5107 | return ICS; |
| 5108 | } |
| 5109 | |
| 5110 | /// PerformContextuallyConvertToObjCPointer - Perform a contextual |
| 5111 | /// conversion of the expression From to an Objective-C pointer type. |
| 5112 | ExprResult Sema::PerformContextuallyConvertToObjCPointer(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 5113 | if (checkPlaceholderForOverload(*this, From)) |
| 5114 | return ExprError(); |
| 5115 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5116 | QualType Ty = Context.getObjCIdType(); |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5117 | ImplicitConversionSequence ICS = |
| 5118 | TryContextuallyConvertToObjCPointer(*this, From); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5119 | if (!ICS.isBad()) |
| 5120 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5121 | return ExprError(); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5122 | } |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5123 | |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5124 | /// Determine whether the provided type is an integral type, or an enumeration |
| 5125 | /// type of a permitted flavor. |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5126 | bool Sema::ICEConvertDiagnoser::match(QualType T) { |
| 5127 | return AllowScopedEnumerations ? T->isIntegralOrEnumerationType() |
| 5128 | : T->isIntegralOrUnscopedEnumerationType(); |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5129 | } |
| 5130 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5131 | static ExprResult |
| 5132 | diagnoseAmbiguousConversion(Sema &SemaRef, SourceLocation Loc, Expr *From, |
| 5133 | Sema::ContextualImplicitConverter &Converter, |
| 5134 | QualType T, UnresolvedSetImpl &ViableConversions) { |
| 5135 | |
| 5136 | if (Converter.Suppress) |
| 5137 | return ExprError(); |
| 5138 | |
| 5139 | Converter.diagnoseAmbiguous(SemaRef, Loc, T) << From->getSourceRange(); |
| 5140 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5141 | CXXConversionDecl *Conv = |
| 5142 | cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 5143 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 5144 | Converter.noteAmbiguous(SemaRef, Conv, ConvTy); |
| 5145 | } |
| 5146 | return SemaRef.Owned(From); |
| 5147 | } |
| 5148 | |
| 5149 | static bool |
| 5150 | diagnoseNoViableConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
| 5151 | Sema::ContextualImplicitConverter &Converter, |
| 5152 | QualType T, bool HadMultipleCandidates, |
| 5153 | UnresolvedSetImpl &ExplicitConversions) { |
| 5154 | if (ExplicitConversions.size() == 1 && !Converter.Suppress) { |
| 5155 | DeclAccessPair Found = ExplicitConversions[0]; |
| 5156 | CXXConversionDecl *Conversion = |
| 5157 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 5158 | |
| 5159 | // The user probably meant to invoke the given explicit |
| 5160 | // conversion; use it. |
| 5161 | QualType ConvTy = Conversion->getConversionType().getNonReferenceType(); |
| 5162 | std::string TypeStr; |
| 5163 | ConvTy.getAsStringInternal(TypeStr, SemaRef.getPrintingPolicy()); |
| 5164 | |
| 5165 | Converter.diagnoseExplicitConv(SemaRef, Loc, T, ConvTy) |
| 5166 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 5167 | "static_cast<" + TypeStr + ">(") |
| 5168 | << FixItHint::CreateInsertion( |
| 5169 | SemaRef.PP.getLocForEndOfToken(From->getLocEnd()), ")"); |
| 5170 | Converter.noteExplicitConv(SemaRef, Conversion, ConvTy); |
| 5171 | |
| 5172 | // If we aren't in a SFINAE context, build a call to the |
| 5173 | // explicit conversion function. |
| 5174 | if (SemaRef.isSFINAEContext()) |
| 5175 | return true; |
| 5176 | |
| 5177 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
| 5178 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5179 | HadMultipleCandidates); |
| 5180 | if (Result.isInvalid()) |
| 5181 | return true; |
| 5182 | // Record usage of conversion in an implicit cast. |
| 5183 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
| 5184 | CK_UserDefinedConversion, Result.get(), 0, |
| 5185 | Result.get()->getValueKind()); |
| 5186 | } |
| 5187 | return false; |
| 5188 | } |
| 5189 | |
| 5190 | static bool recordConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
| 5191 | Sema::ContextualImplicitConverter &Converter, |
| 5192 | QualType T, bool HadMultipleCandidates, |
| 5193 | DeclAccessPair &Found) { |
| 5194 | CXXConversionDecl *Conversion = |
| 5195 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 5196 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
| 5197 | |
| 5198 | QualType ToType = Conversion->getConversionType().getNonReferenceType(); |
| 5199 | if (!Converter.SuppressConversion) { |
| 5200 | if (SemaRef.isSFINAEContext()) |
| 5201 | return true; |
| 5202 | |
| 5203 | Converter.diagnoseConversion(SemaRef, Loc, T, ToType) |
| 5204 | << From->getSourceRange(); |
| 5205 | } |
| 5206 | |
| 5207 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5208 | HadMultipleCandidates); |
| 5209 | if (Result.isInvalid()) |
| 5210 | return true; |
| 5211 | // Record usage of conversion in an implicit cast. |
| 5212 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
| 5213 | CK_UserDefinedConversion, Result.get(), 0, |
| 5214 | Result.get()->getValueKind()); |
| 5215 | return false; |
| 5216 | } |
| 5217 | |
| 5218 | static ExprResult finishContextualImplicitConversion( |
| 5219 | Sema &SemaRef, SourceLocation Loc, Expr *From, |
| 5220 | Sema::ContextualImplicitConverter &Converter) { |
| 5221 | if (!Converter.match(From->getType()) && !Converter.Suppress) |
| 5222 | Converter.diagnoseNoMatch(SemaRef, Loc, From->getType()) |
| 5223 | << From->getSourceRange(); |
| 5224 | |
| 5225 | return SemaRef.DefaultLvalueConversion(From); |
| 5226 | } |
| 5227 | |
| 5228 | static void |
| 5229 | collectViableConversionCandidates(Sema &SemaRef, Expr *From, QualType ToType, |
| 5230 | UnresolvedSetImpl &ViableConversions, |
| 5231 | OverloadCandidateSet &CandidateSet) { |
| 5232 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5233 | DeclAccessPair FoundDecl = ViableConversions[I]; |
| 5234 | NamedDecl *D = FoundDecl.getDecl(); |
| 5235 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 5236 | if (isa<UsingShadowDecl>(D)) |
| 5237 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 5238 | |
| 5239 | CXXConversionDecl *Conv; |
| 5240 | FunctionTemplateDecl *ConvTemplate; |
| 5241 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 5242 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 5243 | else |
| 5244 | Conv = cast<CXXConversionDecl>(D); |
| 5245 | |
| 5246 | if (ConvTemplate) |
| 5247 | SemaRef.AddTemplateConversionCandidate( |
| 5248 | ConvTemplate, FoundDecl, ActingContext, From, ToType, CandidateSet); |
| 5249 | else |
| 5250 | SemaRef.AddConversionCandidate(Conv, FoundDecl, ActingContext, From, |
| 5251 | ToType, CandidateSet); |
| 5252 | } |
| 5253 | } |
| 5254 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5255 | /// \brief Attempt to convert the given expression to a type which is accepted |
| 5256 | /// by the given converter. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5257 | /// |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5258 | /// This routine will attempt to convert an expression of class type to a |
| 5259 | /// type accepted by the specified converter. In C++11 and before, the class |
| 5260 | /// must have a single non-explicit conversion function converting to a matching |
| 5261 | /// type. In C++1y, there can be multiple such conversion functions, but only |
| 5262 | /// one target type. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5263 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5264 | /// \param Loc The source location of the construct that requires the |
| 5265 | /// conversion. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5266 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 5267 | /// \param From The expression we're converting from. |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5268 | /// |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5269 | /// \param Converter Used to control and diagnose the conversion process. |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5270 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5271 | /// \returns The expression, converted to an integral or enumeration type if |
| 5272 | /// successful. |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5273 | ExprResult Sema::PerformContextualImplicitConversion( |
| 5274 | SourceLocation Loc, Expr *From, ContextualImplicitConverter &Converter) { |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5275 | // We can't perform any more checking for type-dependent expressions. |
| 5276 | if (From->isTypeDependent()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5277 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5278 | |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5279 | // Process placeholders immediately. |
| 5280 | if (From->hasPlaceholderType()) { |
| 5281 | ExprResult result = CheckPlaceholderExpr(From); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5282 | if (result.isInvalid()) |
| 5283 | return result; |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5284 | From = result.take(); |
| 5285 | } |
| 5286 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5287 | // If the expression already has a matching type, we're golden. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5288 | QualType T = From->getType(); |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5289 | if (Converter.match(T)) |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5290 | return DefaultLvalueConversion(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5291 | |
| 5292 | // FIXME: Check for missing '()' if T is a function type? |
| 5293 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5294 | // We can only perform contextual implicit conversions on objects of class |
| 5295 | // type. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5296 | const RecordType *RecordTy = T->getAs<RecordType>(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5297 | if (!RecordTy || !getLangOpts().CPlusPlus) { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5298 | if (!Converter.Suppress) |
| 5299 | Converter.diagnoseNoMatch(*this, Loc, T) << From->getSourceRange(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5300 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5301 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5302 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5303 | // We must have a complete class type. |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 5304 | struct TypeDiagnoserPartialDiag : TypeDiagnoser { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5305 | ContextualImplicitConverter &Converter; |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5306 | Expr *From; |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5307 | |
| 5308 | TypeDiagnoserPartialDiag(ContextualImplicitConverter &Converter, Expr *From) |
| 5309 | : TypeDiagnoser(Converter.Suppress), Converter(Converter), From(From) {} |
| 5310 | |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5311 | virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5312 | Converter.diagnoseIncomplete(S, Loc, T) << From->getSourceRange(); |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5313 | } |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5314 | } IncompleteDiagnoser(Converter, From); |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5315 | |
| 5316 | if (RequireCompleteType(Loc, T, IncompleteDiagnoser)) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5317 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5318 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5319 | // Look for a conversion to an integral or enumeration type. |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5320 | UnresolvedSet<4> |
| 5321 | ViableConversions; // These are *potentially* viable in C++1y. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5322 | UnresolvedSet<4> ExplicitConversions; |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 5323 | std::pair<CXXRecordDecl::conversion_iterator, |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5324 | CXXRecordDecl::conversion_iterator> Conversions = |
| 5325 | cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5326 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5327 | bool HadMultipleCandidates = |
| 5328 | (std::distance(Conversions.first, Conversions.second) > 1); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5329 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5330 | // To check that there is only one target type, in C++1y: |
| 5331 | QualType ToType; |
| 5332 | bool HasUniqueTargetType = true; |
| 5333 | |
| 5334 | // Collect explicit or viable (potentially in C++1y) conversions. |
| 5335 | for (CXXRecordDecl::conversion_iterator I = Conversions.first, |
| 5336 | E = Conversions.second; |
| 5337 | I != E; ++I) { |
| 5338 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5339 | CXXConversionDecl *Conversion; |
| 5340 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 5341 | if (ConvTemplate) { |
| 5342 | if (getLangOpts().CPlusPlus1y) |
| 5343 | Conversion = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 5344 | else |
| 5345 | continue; // C++11 does not consider conversion operator templates(?). |
| 5346 | } else |
| 5347 | Conversion = cast<CXXConversionDecl>(D); |
| 5348 | |
| 5349 | assert((!ConvTemplate || getLangOpts().CPlusPlus1y) && |
| 5350 | "Conversion operator templates are considered potentially " |
| 5351 | "viable in C++1y"); |
| 5352 | |
| 5353 | QualType CurToType = Conversion->getConversionType().getNonReferenceType(); |
| 5354 | if (Converter.match(CurToType) || ConvTemplate) { |
| 5355 | |
| 5356 | if (Conversion->isExplicit()) { |
| 5357 | // FIXME: For C++1y, do we need this restriction? |
| 5358 | // cf. diagnoseNoViableConversion() |
| 5359 | if (!ConvTemplate) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5360 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5361 | } else { |
| 5362 | if (!ConvTemplate && getLangOpts().CPlusPlus1y) { |
| 5363 | if (ToType.isNull()) |
| 5364 | ToType = CurToType.getUnqualifiedType(); |
| 5365 | else if (HasUniqueTargetType && |
| 5366 | (CurToType.getUnqualifiedType() != ToType)) |
| 5367 | HasUniqueTargetType = false; |
| 5368 | } |
| 5369 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5370 | } |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5371 | } |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5372 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5373 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5374 | if (getLangOpts().CPlusPlus1y) { |
| 5375 | // C++1y [conv]p6: |
| 5376 | // ... An expression e of class type E appearing in such a context |
| 5377 | // is said to be contextually implicitly converted to a specified |
| 5378 | // type T and is well-formed if and only if e can be implicitly |
| 5379 | // 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] | 5380 | // for conversion functions whose return type is cv T or reference to |
| 5381 | // 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] | 5382 | // exactly one such T. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5383 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5384 | // If no unique T is found: |
| 5385 | if (ToType.isNull()) { |
| 5386 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5387 | HadMultipleCandidates, |
| 5388 | ExplicitConversions)) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5389 | return ExprError(); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5390 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5391 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5392 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5393 | // If more than one unique Ts are found: |
| 5394 | if (!HasUniqueTargetType) |
| 5395 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5396 | ViableConversions); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5397 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5398 | // If one unique T is found: |
| 5399 | // First, build a candidate set from the previously recorded |
| 5400 | // potentially viable conversions. |
| 5401 | OverloadCandidateSet CandidateSet(Loc); |
| 5402 | collectViableConversionCandidates(*this, From, ToType, ViableConversions, |
| 5403 | CandidateSet); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5404 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5405 | // Then, perform overload resolution over the candidate set. |
| 5406 | OverloadCandidateSet::iterator Best; |
| 5407 | switch (CandidateSet.BestViableFunction(*this, Loc, Best)) { |
| 5408 | case OR_Success: { |
| 5409 | // Apply this conversion. |
| 5410 | DeclAccessPair Found = |
| 5411 | DeclAccessPair::make(Best->Function, Best->FoundDecl.getAccess()); |
| 5412 | if (recordConversion(*this, Loc, From, Converter, T, |
| 5413 | HadMultipleCandidates, Found)) |
| 5414 | return ExprError(); |
| 5415 | break; |
| 5416 | } |
| 5417 | case OR_Ambiguous: |
| 5418 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5419 | ViableConversions); |
| 5420 | case OR_No_Viable_Function: |
| 5421 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5422 | HadMultipleCandidates, |
| 5423 | ExplicitConversions)) |
| 5424 | return ExprError(); |
| 5425 | // fall through 'OR_Deleted' case. |
| 5426 | case OR_Deleted: |
| 5427 | // We'll complain below about a non-integral condition type. |
| 5428 | break; |
| 5429 | } |
| 5430 | } else { |
| 5431 | switch (ViableConversions.size()) { |
| 5432 | case 0: { |
| 5433 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5434 | HadMultipleCandidates, |
| 5435 | ExplicitConversions)) |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5436 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5437 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5438 | // We'll complain below about a non-integral condition type. |
| 5439 | break; |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5440 | } |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5441 | case 1: { |
| 5442 | // Apply this conversion. |
| 5443 | DeclAccessPair Found = ViableConversions[0]; |
| 5444 | if (recordConversion(*this, Loc, From, Converter, T, |
| 5445 | HadMultipleCandidates, Found)) |
| 5446 | return ExprError(); |
| 5447 | break; |
| 5448 | } |
| 5449 | default: |
| 5450 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5451 | ViableConversions); |
| 5452 | } |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5453 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5454 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5455 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5456 | } |
| 5457 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5458 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 5459 | /// candidate functions, using the given function call arguments. If |
| 5460 | /// @p SuppressUserConversions, then don't allow user-defined |
| 5461 | /// conversions via constructors or conversion operators. |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5462 | /// |
James Dennett | 699c904 | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 5463 | /// \param PartialOverloading true if we are performing "partial" overloading |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5464 | /// based on an incomplete set of function arguments. This feature is used by |
| 5465 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5466 | void |
| 5467 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5468 | DeclAccessPair FoundDecl, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5469 | ArrayRef<Expr *> Args, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 5470 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 5471 | bool SuppressUserConversions, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5472 | bool PartialOverloading, |
| 5473 | bool AllowExplicit) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5474 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5475 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5476 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5477 | assert(!Function->getDescribedFunctionTemplate() && |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5478 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5479 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5480 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5481 | if (!isa<CXXConstructorDecl>(Method)) { |
| 5482 | // If we get here, it's because we're calling a member function |
| 5483 | // that is named without a member access expression (e.g., |
| 5484 | // "this->f") that was either written explicitly or created |
| 5485 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5486 | // function, e.g., X::f(). We use an empty type for the implied |
| 5487 | // object argument (C++ [over.call.func]p3), and the acting context |
| 5488 | // is irrelevant. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5489 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5490 | QualType(), Expr::Classification::makeSimpleLValue(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5491 | Args, CandidateSet, SuppressUserConversions); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5492 | return; |
| 5493 | } |
| 5494 | // We treat a constructor like a non-member function, since its object |
| 5495 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5496 | } |
| 5497 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 5498 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5499 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5500 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5501 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5502 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5503 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5504 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 5505 | // C++ [class.copy]p3: |
| 5506 | // A member function template is never instantiated to perform the copy |
| 5507 | // of a class object to an object of its class type. |
| 5508 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5509 | if (Args.size() == 1 && |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 5510 | Constructor->isSpecializationCopyingObject() && |
Douglas Gregor | 1211606 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 5511 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 5512 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5513 | return; |
| 5514 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5515 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5516 | // Add this candidate |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5517 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5518 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5519 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5520 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5521 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5522 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5523 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5524 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5525 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5526 | |
| 5527 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5528 | // parameters is viable only if it has an ellipsis in its parameter |
| 5529 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5530 | if ((Args.size() + (PartialOverloading && Args.size())) > NumArgsInProto && |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 5531 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5532 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5533 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5534 | return; |
| 5535 | } |
| 5536 | |
| 5537 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5538 | // is viable only if the (m+1)st parameter has a default argument |
| 5539 | // (8.3.6). For the purposes of overload resolution, the |
| 5540 | // parameter list is truncated on the right, so that there are |
| 5541 | // exactly m parameters. |
| 5542 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5543 | if (Args.size() < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5544 | // Not enough arguments. |
| 5545 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5546 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5547 | return; |
| 5548 | } |
| 5549 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5550 | // (CUDA B.1): Check for invalid calls between targets. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5551 | if (getLangOpts().CUDA) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5552 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
| 5553 | if (CheckCUDATarget(Caller, Function)) { |
| 5554 | Candidate.Viable = false; |
| 5555 | Candidate.FailureKind = ovl_fail_bad_target; |
| 5556 | return; |
| 5557 | } |
| 5558 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5559 | // Determine the implicit conversion sequences for each of the |
| 5560 | // arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5561 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5562 | if (ArgIdx < NumArgsInProto) { |
| 5563 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5564 | // exist for each argument an implicit conversion sequence |
| 5565 | // (13.3.3.1) that converts that argument to the corresponding |
| 5566 | // parameter of F. |
| 5567 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5568 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5569 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5570 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5571 | /*InOverloadResolution=*/true, |
| 5572 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5573 | getLangOpts().ObjCAutoRefCount, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5574 | AllowExplicit); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5575 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 5576 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5577 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5578 | break; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5579 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5580 | } else { |
| 5581 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5582 | // argument for which there is no corresponding parameter is |
| 5583 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5584 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5585 | } |
| 5586 | } |
| 5587 | } |
| 5588 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5589 | /// \brief Add all of the function declarations in the given function set to |
| 5590 | /// the overload canddiate set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5591 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5592 | ArrayRef<Expr *> Args, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5593 | OverloadCandidateSet& CandidateSet, |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5594 | bool SuppressUserConversions, |
| 5595 | TemplateArgumentListInfo *ExplicitTemplateArgs) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5596 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5597 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 5598 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5599 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5600 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5601 | cast<CXXMethodDecl>(FD)->getParent(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5602 | Args[0]->getType(), Args[0]->Classify(Context), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5603 | Args.slice(1), CandidateSet, |
| 5604 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5605 | else |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5606 | AddOverloadCandidate(FD, F.getPair(), Args, CandidateSet, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5607 | SuppressUserConversions); |
| 5608 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5609 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5610 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 5611 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5612 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5613 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5614 | ExplicitTemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5615 | Args[0]->getType(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5616 | Args[0]->Classify(Context), Args.slice(1), |
| 5617 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5618 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5619 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5620 | ExplicitTemplateArgs, Args, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5621 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5622 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5623 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5624 | } |
| 5625 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5626 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 5627 | /// method) as a method candidate to the given overload set. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5628 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5629 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5630 | Expr::Classification ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5631 | ArrayRef<Expr *> Args, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5632 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5633 | bool SuppressUserConversions) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5634 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5635 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5636 | |
| 5637 | if (isa<UsingShadowDecl>(Decl)) |
| 5638 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5639 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5640 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 5641 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 5642 | "Expected a member function template"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5643 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 5644 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5645 | ObjectType, ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5646 | Args, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5647 | SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5648 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5649 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5650 | ObjectType, ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5651 | Args, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5652 | CandidateSet, SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5653 | } |
| 5654 | } |
| 5655 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5656 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 5657 | /// of candidate functions, using the given function call arguments |
| 5658 | /// and the object argument (@c Object). For example, in a call |
| 5659 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 5660 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 5661 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5662 | /// operators. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5663 | void |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5664 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5665 | CXXRecordDecl *ActingContext, QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5666 | Expr::Classification ObjectClassification, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5667 | ArrayRef<Expr *> Args, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5668 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5669 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5670 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5671 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5672 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5673 | assert(!isa<CXXConstructorDecl>(Method) && |
| 5674 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5675 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5676 | if (!CandidateSet.isNewCandidate(Method)) |
| 5677 | return; |
| 5678 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5679 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5680 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5681 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5682 | // Add this candidate |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5683 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5684 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5685 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5686 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5687 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5688 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5689 | |
| 5690 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5691 | |
| 5692 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5693 | // parameters is viable only if it has an ellipsis in its parameter |
| 5694 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5695 | if (Args.size() > NumArgsInProto && !Proto->isVariadic()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5696 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5697 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5698 | return; |
| 5699 | } |
| 5700 | |
| 5701 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5702 | // is viable only if the (m+1)st parameter has a default argument |
| 5703 | // (8.3.6). For the purposes of overload resolution, the |
| 5704 | // parameter list is truncated on the right, so that there are |
| 5705 | // exactly m parameters. |
| 5706 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5707 | if (Args.size() < MinRequiredArgs) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5708 | // Not enough arguments. |
| 5709 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5710 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5711 | return; |
| 5712 | } |
| 5713 | |
| 5714 | Candidate.Viable = true; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5715 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5716 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5717 | // The implicit object argument is ignored. |
| 5718 | Candidate.IgnoreObjectArgument = true; |
| 5719 | else { |
| 5720 | // Determine the implicit conversion sequence for the object |
| 5721 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5722 | Candidate.Conversions[0] |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5723 | = TryObjectArgumentInitialization(*this, ObjectType, ObjectClassification, |
| 5724 | Method, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5725 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5726 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5727 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5728 | return; |
| 5729 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5730 | } |
| 5731 | |
| 5732 | // Determine the implicit conversion sequences for each of the |
| 5733 | // arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5734 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5735 | if (ArgIdx < NumArgsInProto) { |
| 5736 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5737 | // exist for each argument an implicit conversion sequence |
| 5738 | // (13.3.3.1) that converts that argument to the corresponding |
| 5739 | // parameter of F. |
| 5740 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5741 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5742 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5743 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5744 | /*InOverloadResolution=*/true, |
| 5745 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5746 | getLangOpts().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5747 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5748 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5749 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5750 | break; |
| 5751 | } |
| 5752 | } else { |
| 5753 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5754 | // argument for which there is no corresponding parameter is |
| 5755 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5756 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5757 | } |
| 5758 | } |
| 5759 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5760 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5761 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 5762 | /// set, using template argument deduction to produce an appropriate member |
| 5763 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5764 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5765 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5766 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5767 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5768 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5769 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5770 | Expr::Classification ObjectClassification, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5771 | ArrayRef<Expr *> Args, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5772 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5773 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5774 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 5775 | return; |
| 5776 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5777 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5778 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5779 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5780 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5781 | // candidate functions in the usual way.113) A given name can refer to one |
| 5782 | // or more function templates and also to a set of overloaded non-template |
| 5783 | // functions. In such a case, the candidate functions generated from each |
| 5784 | // function template are combined with the set of non-template candidate |
| 5785 | // functions. |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5786 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5787 | FunctionDecl *Specialization = 0; |
| 5788 | if (TemplateDeductionResult Result |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5789 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, Args, |
| 5790 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5791 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5792 | Candidate.FoundDecl = FoundDecl; |
| 5793 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 5794 | Candidate.Viable = false; |
| 5795 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 5796 | Candidate.IsSurrogate = false; |
| 5797 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5798 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5799 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5800 | Info); |
| 5801 | return; |
| 5802 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5803 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5804 | // Add the function template specialization produced by template argument |
| 5805 | // deduction as a candidate. |
| 5806 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5807 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5808 | "Specialization is not a member function?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5809 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5810 | ActingContext, ObjectType, ObjectClassification, Args, |
| 5811 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5812 | } |
| 5813 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5814 | /// \brief Add a C++ function template specialization as a candidate |
| 5815 | /// in the candidate set, using template argument deduction to produce |
| 5816 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5817 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5818 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5819 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5820 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5821 | ArrayRef<Expr *> Args, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5822 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5823 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5824 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 5825 | return; |
| 5826 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5827 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5828 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5829 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5830 | // 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] | 5831 | // candidate functions in the usual way.113) A given name can refer to one |
| 5832 | // or more function templates and also to a set of overloaded non-template |
| 5833 | // functions. In such a case, the candidate functions generated from each |
| 5834 | // function template are combined with the set of non-template candidate |
| 5835 | // functions. |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5836 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5837 | FunctionDecl *Specialization = 0; |
| 5838 | if (TemplateDeductionResult Result |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5839 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, Args, |
| 5840 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5841 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5842 | Candidate.FoundDecl = FoundDecl; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5843 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 5844 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5845 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5846 | Candidate.IsSurrogate = false; |
| 5847 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5848 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5849 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5850 | Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5851 | return; |
| 5852 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5853 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5854 | // Add the function template specialization produced by template argument |
| 5855 | // deduction as a candidate. |
| 5856 | assert(Specialization && "Missing function template specialization?"); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5857 | AddOverloadCandidate(Specialization, FoundDecl, Args, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5858 | SuppressUserConversions); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5859 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5860 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5861 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5862 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5863 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5864 | /// 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] | 5865 | /// (which may or may not be the same type as the type that the |
| 5866 | /// conversion function produces). |
| 5867 | void |
| 5868 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5869 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5870 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5871 | Expr *From, QualType ToType, |
| 5872 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5873 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 5874 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5875 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5876 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 5877 | return; |
| 5878 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 5879 | // If the conversion function has an undeduced return type, trigger its |
| 5880 | // deduction now. |
| 5881 | if (getLangOpts().CPlusPlus1y && ConvType->isUndeducedType()) { |
| 5882 | if (DeduceReturnType(Conversion, From->getExprLoc())) |
| 5883 | return; |
| 5884 | ConvType = Conversion->getConversionType().getNonReferenceType(); |
| 5885 | } |
| 5886 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5887 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5888 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5889 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5890 | // Add this candidate |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5891 | OverloadCandidate &Candidate = CandidateSet.addCandidate(1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5892 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5893 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5894 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5895 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5896 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5897 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 5898 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5899 | Candidate.Viable = true; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5900 | Candidate.ExplicitCallArguments = 1; |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5901 | |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 5902 | // C++ [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5903 | // For conversion functions, the function is considered to be a member of |
| 5904 | // the class of the implicit implied object argument for the purpose of |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 5905 | // defining the type of the implicit object parameter. |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5906 | // |
| 5907 | // Determine the implicit conversion sequence for the implicit |
| 5908 | // object parameter. |
| 5909 | QualType ImplicitParamType = From->getType(); |
| 5910 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
| 5911 | ImplicitParamType = FromPtrType->getPointeeType(); |
| 5912 | CXXRecordDecl *ConversionContext |
| 5913 | = cast<CXXRecordDecl>(ImplicitParamType->getAs<RecordType>()->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5914 | |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5915 | Candidate.Conversions[0] |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5916 | = TryObjectArgumentInitialization(*this, From->getType(), |
| 5917 | From->Classify(Context), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5918 | Conversion, ConversionContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5919 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5920 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5921 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5922 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5923 | return; |
| 5924 | } |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5925 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5926 | // 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] | 5927 | // derived to base as such conversions are given Conversion Rank. They only |
| 5928 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 5929 | QualType FromCanon |
| 5930 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 5931 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 5932 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 5933 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5934 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 5935 | return; |
| 5936 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5937 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5938 | // To determine what the conversion from the result of calling the |
| 5939 | // conversion function to the type we're eventually trying to |
| 5940 | // convert to (ToType), we need to synthesize a call to the |
| 5941 | // conversion function and attempt copy initialization from it. This |
| 5942 | // makes sure that we get the right semantics with respect to |
| 5943 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 5944 | // call on the stack and we don't need its arguments to be |
| 5945 | // well-formed. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5946 | DeclRefExpr ConversionRef(Conversion, false, Conversion->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5947 | VK_LValue, From->getLocStart()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5948 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 5949 | Context.getPointerType(Conversion->getType()), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5950 | CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5951 | &ConversionRef, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5952 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5953 | QualType ConversionType = Conversion->getConversionType(); |
| 5954 | if (RequireCompleteType(From->getLocStart(), ConversionType, 0)) { |
Douglas Gregor | 7d14d38 | 2010-11-13 19:36:57 +0000 | [diff] [blame] | 5955 | Candidate.Viable = false; |
| 5956 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 5957 | return; |
| 5958 | } |
| 5959 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5960 | ExprValueKind VK = Expr::getValueKindForType(ConversionType); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5961 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5962 | // 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] | 5963 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 5964 | // allocator). |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5965 | QualType CallResultType = ConversionType.getNonLValueExprType(Context); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 5966 | CallExpr Call(Context, &ConversionFn, None, CallResultType, VK, |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 5967 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5968 | ImplicitConversionSequence ICS = |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5969 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 5970 | /*SuppressUserConversions=*/true, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5971 | /*InOverloadResolution=*/false, |
| 5972 | /*AllowObjCWritebackConversion=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5973 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5974 | switch (ICS.getKind()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5975 | case ImplicitConversionSequence::StandardConversion: |
| 5976 | Candidate.FinalConversion = ICS.Standard; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5977 | |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5978 | // C++ [over.ics.user]p3: |
| 5979 | // If the user-defined conversion is specified by a specialization of a |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5980 | // conversion function template, the second standard conversion sequence |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5981 | // shall have exact match rank. |
| 5982 | if (Conversion->getPrimaryTemplate() && |
| 5983 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 5984 | Candidate.Viable = false; |
| 5985 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 5986 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5987 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 5988 | // C++0x [dcl.init.ref]p5: |
| 5989 | // In the second case, if the reference is an rvalue reference and |
| 5990 | // the second standard conversion sequence of the user-defined |
| 5991 | // conversion sequence includes an lvalue-to-rvalue conversion, the |
| 5992 | // program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5993 | if (ToType->isRValueReferenceType() && |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 5994 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 5995 | Candidate.Viable = false; |
| 5996 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 5997 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5998 | break; |
| 5999 | |
| 6000 | case ImplicitConversionSequence::BadConversion: |
| 6001 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6002 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6003 | break; |
| 6004 | |
| 6005 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6006 | llvm_unreachable( |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6007 | "Can only end up with a standard conversion sequence or failure"); |
| 6008 | } |
| 6009 | } |
| 6010 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6011 | /// \brief Adds a conversion function template specialization |
| 6012 | /// candidate to the overload set, using template argument deduction |
| 6013 | /// to deduce the template arguments of the conversion function |
| 6014 | /// template from the type that we are converting to (C++ |
| 6015 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6016 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6017 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6018 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6019 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6020 | Expr *From, QualType ToType, |
| 6021 | OverloadCandidateSet &CandidateSet) { |
| 6022 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 6023 | "Only conversion function templates permitted here"); |
| 6024 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6025 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 6026 | return; |
| 6027 | |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 6028 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6029 | CXXConversionDecl *Specialization = 0; |
| 6030 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6031 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6032 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 6033 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 6034 | Candidate.FoundDecl = FoundDecl; |
| 6035 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 6036 | Candidate.Viable = false; |
| 6037 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 6038 | Candidate.IsSurrogate = false; |
| 6039 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6040 | Candidate.ExplicitCallArguments = 1; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6041 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 6042 | Info); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6043 | return; |
| 6044 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6045 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6046 | // Add the conversion function template specialization produced by |
| 6047 | // template argument deduction as a candidate. |
| 6048 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6049 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 6050 | CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6051 | } |
| 6052 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6053 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 6054 | /// converts the given @c Object to a function pointer via the |
| 6055 | /// conversion function @c Conversion, and then attempts to call it |
| 6056 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 6057 | /// the type of function that we'll eventually be calling. |
| 6058 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6059 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6060 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6061 | const FunctionProtoType *Proto, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6062 | Expr *Object, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6063 | ArrayRef<Expr *> Args, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6064 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6065 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 6066 | return; |
| 6067 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6068 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6069 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6070 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6071 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6072 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6073 | Candidate.Function = 0; |
| 6074 | Candidate.Surrogate = Conversion; |
| 6075 | Candidate.Viable = true; |
| 6076 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6077 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6078 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6079 | |
| 6080 | // Determine the implicit conversion sequence for the implicit |
| 6081 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6082 | ImplicitConversionSequence ObjectInit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6083 | = TryObjectArgumentInitialization(*this, Object->getType(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6084 | Object->Classify(Context), |
| 6085 | Conversion, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6086 | if (ObjectInit.isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6087 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6088 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6089 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6090 | return; |
| 6091 | } |
| 6092 | |
| 6093 | // The first conversion is actually a user-defined conversion whose |
| 6094 | // first conversion is ObjectInit's standard conversion (which is |
| 6095 | // effectively a reference binding). Record it as such. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6096 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6097 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 6098 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6099 | Candidate.Conversions[0].UserDefined.HadMultipleCandidates = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6100 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 6101 | Candidate.Conversions[0].UserDefined.FoundConversionFunction = FoundDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6102 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6103 | = Candidate.Conversions[0].UserDefined.Before; |
| 6104 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 6105 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6106 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6107 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 6108 | |
| 6109 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 6110 | // parameters is viable only if it has an ellipsis in its parameter |
| 6111 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6112 | if (Args.size() > NumArgsInProto && !Proto->isVariadic()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6113 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6114 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6115 | return; |
| 6116 | } |
| 6117 | |
| 6118 | // Function types don't have any default arguments, so just check if |
| 6119 | // we have enough arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6120 | if (Args.size() < NumArgsInProto) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6121 | // Not enough arguments. |
| 6122 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6123 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6124 | return; |
| 6125 | } |
| 6126 | |
| 6127 | // Determine the implicit conversion sequences for each of the |
| 6128 | // arguments. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6129 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6130 | if (ArgIdx < NumArgsInProto) { |
| 6131 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 6132 | // exist for each argument an implicit conversion sequence |
| 6133 | // (13.3.3.1) that converts that argument to the corresponding |
| 6134 | // parameter of F. |
| 6135 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6136 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6137 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6138 | /*SuppressUserConversions=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6139 | /*InOverloadResolution=*/false, |
| 6140 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6141 | getLangOpts().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6142 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6143 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6144 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6145 | break; |
| 6146 | } |
| 6147 | } else { |
| 6148 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 6149 | // argument for which there is no corresponding parameter is |
| 6150 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6151 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6152 | } |
| 6153 | } |
| 6154 | } |
| 6155 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6156 | /// \brief Add overload candidates for overloaded operators that are |
| 6157 | /// member functions. |
| 6158 | /// |
| 6159 | /// Add the overloaded operator candidates that are member functions |
| 6160 | /// for the operator Op that was used in an operator expression such |
| 6161 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 6162 | /// CandidateSet will store the added overload candidates. (C++ |
| 6163 | /// [over.match.oper]). |
| 6164 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 6165 | SourceLocation OpLoc, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6166 | ArrayRef<Expr *> Args, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6167 | OverloadCandidateSet& CandidateSet, |
| 6168 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6169 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 6170 | |
| 6171 | // C++ [over.match.oper]p3: |
| 6172 | // For a unary operator @ with an operand of a type whose |
| 6173 | // cv-unqualified version is T1, and for a binary operator @ with |
| 6174 | // a left operand of a type whose cv-unqualified version is T1 and |
| 6175 | // a right operand of a type whose cv-unqualified version is T2, |
| 6176 | // three sets of candidate functions, designated member |
| 6177 | // candidates, non-member candidates and built-in candidates, are |
| 6178 | // constructed as follows: |
| 6179 | QualType T1 = Args[0]->getType(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6180 | |
Richard Smith | e410be9 | 2013-04-20 12:41:22 +0000 | [diff] [blame] | 6181 | // -- If T1 is a complete class type or a class currently being |
| 6182 | // defined, the set of member candidates is the result of the |
| 6183 | // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, |
| 6184 | // the set of member candidates is empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6185 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Richard Smith | e410be9 | 2013-04-20 12:41:22 +0000 | [diff] [blame] | 6186 | // Complete the type if it can be completed. |
| 6187 | RequireCompleteType(OpLoc, T1, 0); |
| 6188 | // If the type is neither complete nor being defined, bail out now. |
| 6189 | if (!T1Rec->getDecl()->getDefinition()) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 6190 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6191 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6192 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 6193 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 6194 | Operators.suppressDiagnostics(); |
| 6195 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6196 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 6197 | OperEnd = Operators.end(); |
| 6198 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6199 | ++Oper) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6200 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 6201 | Args[0]->Classify(Context), |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6202 | Args.slice(1), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6203 | CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6204 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6205 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6206 | } |
| 6207 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6208 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 6209 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 6210 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6211 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 6212 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6213 | /// operator. NumContextualBoolArguments is the number of arguments |
| 6214 | /// (at the beginning of the argument list) that will be contextually |
| 6215 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6216 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6217 | ArrayRef<Expr *> Args, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6218 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6219 | bool IsAssignmentOperator, |
| 6220 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6221 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6222 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6223 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6224 | // Add this candidate |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6225 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6226 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6227 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 6228 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6229 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6230 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6231 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6232 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 6233 | |
| 6234 | // Determine the implicit conversion sequences for each of the |
| 6235 | // arguments. |
| 6236 | Candidate.Viable = true; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6237 | Candidate.ExplicitCallArguments = Args.size(); |
| 6238 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6239 | // C++ [over.match.oper]p4: |
| 6240 | // For the built-in assignment operators, conversions of the |
| 6241 | // left operand are restricted as follows: |
| 6242 | // -- no temporaries are introduced to hold the left operand, and |
| 6243 | // -- no user-defined conversions are applied to the left |
| 6244 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6245 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6246 | // |
| 6247 | // We block these conversions by turning off user-defined |
| 6248 | // conversions, since that is the only way that initialization of |
| 6249 | // a reference to a non-class type can occur from something that |
| 6250 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6251 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6252 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6253 | "Contextual conversion to bool requires bool type"); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6254 | Candidate.Conversions[ArgIdx] |
| 6255 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6256 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6257 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6258 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6259 | ArgIdx == 0 && IsAssignmentOperator, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6260 | /*InOverloadResolution=*/false, |
| 6261 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6262 | getLangOpts().ObjCAutoRefCount); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6263 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6264 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6265 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6266 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6267 | break; |
| 6268 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6269 | } |
| 6270 | } |
| 6271 | |
| 6272 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 6273 | /// candidate operator functions for built-in operators (C++ |
| 6274 | /// [over.built]). The types are separated into pointer types and |
| 6275 | /// enumeration types. |
| 6276 | class BuiltinCandidateTypeSet { |
| 6277 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6278 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6279 | |
| 6280 | /// PointerTypes - The set of pointer types that will be used in the |
| 6281 | /// built-in candidates. |
| 6282 | TypeSet PointerTypes; |
| 6283 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6284 | /// MemberPointerTypes - The set of member pointer types that will be |
| 6285 | /// used in the built-in candidates. |
| 6286 | TypeSet MemberPointerTypes; |
| 6287 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6288 | /// EnumerationTypes - The set of enumeration types that will be |
| 6289 | /// used in the built-in candidates. |
| 6290 | TypeSet EnumerationTypes; |
| 6291 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6292 | /// \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] | 6293 | /// candidates. |
| 6294 | TypeSet VectorTypes; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6295 | |
| 6296 | /// \brief A flag indicating non-record types are viable candidates |
| 6297 | bool HasNonRecordTypes; |
| 6298 | |
| 6299 | /// \brief A flag indicating whether either arithmetic or enumeration types |
| 6300 | /// were present in the candidate set. |
| 6301 | bool HasArithmeticOrEnumeralTypes; |
| 6302 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6303 | /// \brief A flag indicating whether the nullptr type was present in the |
| 6304 | /// candidate set. |
| 6305 | bool HasNullPtrType; |
| 6306 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 6307 | /// Sema - The semantic analysis instance where we are building the |
| 6308 | /// candidate type set. |
| 6309 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6310 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6311 | /// Context - The AST context in which we will build the type sets. |
| 6312 | ASTContext &Context; |
| 6313 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6314 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6315 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6316 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6317 | |
| 6318 | public: |
| 6319 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6320 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6321 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6322 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6323 | : HasNonRecordTypes(false), |
| 6324 | HasArithmeticOrEnumeralTypes(false), |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6325 | HasNullPtrType(false), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6326 | SemaRef(SemaRef), |
| 6327 | Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6328 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6329 | void AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6330 | SourceLocation Loc, |
| 6331 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6332 | bool AllowExplicitConversions, |
| 6333 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6334 | |
| 6335 | /// pointer_begin - First pointer type found; |
| 6336 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 6337 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6338 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6339 | iterator pointer_end() { return PointerTypes.end(); } |
| 6340 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6341 | /// member_pointer_begin - First member pointer type found; |
| 6342 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 6343 | |
| 6344 | /// member_pointer_end - Past the last member pointer type found; |
| 6345 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 6346 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6347 | /// enumeration_begin - First enumeration type found; |
| 6348 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 6349 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6350 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6351 | iterator enumeration_end() { return EnumerationTypes.end(); } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6352 | |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6353 | iterator vector_begin() { return VectorTypes.begin(); } |
| 6354 | iterator vector_end() { return VectorTypes.end(); } |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6355 | |
| 6356 | bool hasNonRecordTypes() { return HasNonRecordTypes; } |
| 6357 | bool hasArithmeticOrEnumeralTypes() { return HasArithmeticOrEnumeralTypes; } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6358 | bool hasNullPtrType() const { return HasNullPtrType; } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6359 | }; |
| 6360 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6361 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6362 | /// the set of pointer types along with any more-qualified variants of |
| 6363 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6364 | /// will add "int const *", "int const volatile *", "int const |
| 6365 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6366 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6367 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6368 | /// |
| 6369 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6370 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6371 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6372 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6373 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6374 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6375 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6376 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6377 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6378 | QualType PointeeTy; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6379 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6380 | bool buildObjCPtr = false; |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6381 | if (!PointerTy) { |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6382 | const ObjCObjectPointerType *PTy = Ty->castAs<ObjCObjectPointerType>(); |
| 6383 | PointeeTy = PTy->getPointeeType(); |
| 6384 | buildObjCPtr = true; |
| 6385 | } else { |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6386 | PointeeTy = PointerTy->getPointeeType(); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6387 | } |
| 6388 | |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6389 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6390 | // (the qualifier would sink to the element type), and for another, the |
| 6391 | // only overload situation where it matters is subscript or pointer +- int, |
| 6392 | // and those shouldn't have qualifier variants anyway. |
| 6393 | if (PointeeTy->isArrayType()) |
| 6394 | return true; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6395 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6396 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6397 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 6398 | bool hasRestrict = VisibleQuals.hasRestrict(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6399 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6400 | // Iterate through all strict supersets of BaseCVR. |
| 6401 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6402 | if ((CVR | BaseCVR) != CVR) continue; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6403 | // Skip over volatile if no volatile found anywhere in the types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6404 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6405 | |
| 6406 | // Skip over restrict if no restrict found anywhere in the types, or if |
| 6407 | // the type cannot be restrict-qualified. |
| 6408 | if ((CVR & Qualifiers::Restrict) && |
| 6409 | (!hasRestrict || |
| 6410 | (!(PointeeTy->isAnyPointerType() || PointeeTy->isReferenceType())))) |
| 6411 | continue; |
| 6412 | |
| 6413 | // Build qualified pointee type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6414 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6415 | |
| 6416 | // Build qualified pointer type. |
| 6417 | QualType QPointerTy; |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6418 | if (!buildObjCPtr) |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6419 | QPointerTy = Context.getPointerType(QPointeeTy); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6420 | else |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6421 | QPointerTy = Context.getObjCObjectPointerType(QPointeeTy); |
| 6422 | |
| 6423 | // Insert qualified pointer type. |
| 6424 | PointerTypes.insert(QPointerTy); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6425 | } |
| 6426 | |
| 6427 | return true; |
| 6428 | } |
| 6429 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6430 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 6431 | /// to the set of pointer types along with any more-qualified variants of |
| 6432 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6433 | /// will add "int const *", "int const volatile *", "int const |
| 6434 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6435 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6436 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6437 | /// |
| 6438 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6439 | bool |
| 6440 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 6441 | QualType Ty) { |
| 6442 | // Insert this type. |
| 6443 | if (!MemberPointerTypes.insert(Ty)) |
| 6444 | return false; |
| 6445 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6446 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 6447 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6448 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6449 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6450 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6451 | // (the qualifier would sink to the element type), and for another, the |
| 6452 | // only overload situation where it matters is subscript or pointer +- int, |
| 6453 | // and those shouldn't have qualifier variants anyway. |
| 6454 | if (PointeeTy->isArrayType()) |
| 6455 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6456 | const Type *ClassTy = PointerTy->getClass(); |
| 6457 | |
| 6458 | // Iterate through all strict supersets of the pointee type's CVR |
| 6459 | // qualifiers. |
| 6460 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 6461 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6462 | if ((CVR | BaseCVR) != CVR) continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6463 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6464 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6465 | MemberPointerTypes.insert( |
| 6466 | Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6467 | } |
| 6468 | |
| 6469 | return true; |
| 6470 | } |
| 6471 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6472 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 6473 | /// 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] | 6474 | /// primarily interested in pointer types and enumeration types. We also |
| 6475 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6476 | /// AllowUserConversions is true if we should look at the conversion |
| 6477 | /// functions of a class type, and AllowExplicitConversions if we |
| 6478 | /// should also include the explicit conversion functions of a class |
| 6479 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6480 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6481 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6482 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6483 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6484 | bool AllowExplicitConversions, |
| 6485 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6486 | // Only deal with canonical types. |
| 6487 | Ty = Context.getCanonicalType(Ty); |
| 6488 | |
| 6489 | // Look through reference types; they aren't part of the type of an |
| 6490 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6491 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6492 | Ty = RefTy->getPointeeType(); |
| 6493 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 6494 | // If we're dealing with an array type, decay to the pointer. |
| 6495 | if (Ty->isArrayType()) |
| 6496 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 6497 | |
| 6498 | // Otherwise, we don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 6499 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6500 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6501 | // Flag if we ever add a non-record type. |
| 6502 | const RecordType *TyRec = Ty->getAs<RecordType>(); |
| 6503 | HasNonRecordTypes = HasNonRecordTypes || !TyRec; |
| 6504 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6505 | // Flag if we encounter an arithmetic type. |
| 6506 | HasArithmeticOrEnumeralTypes = |
| 6507 | HasArithmeticOrEnumeralTypes || Ty->isArithmeticType(); |
| 6508 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6509 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
| 6510 | PointerTypes.insert(Ty); |
| 6511 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6512 | // Insert our type, and its more-qualified variants, into the set |
| 6513 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6514 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6515 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6516 | } else if (Ty->isMemberPointerType()) { |
| 6517 | // Member pointers are far easier, since the pointee can't be converted. |
| 6518 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 6519 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6520 | } else if (Ty->isEnumeralType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6521 | HasArithmeticOrEnumeralTypes = true; |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6522 | EnumerationTypes.insert(Ty); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6523 | } else if (Ty->isVectorType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6524 | // We treat vector types as arithmetic types in many contexts as an |
| 6525 | // extension. |
| 6526 | HasArithmeticOrEnumeralTypes = true; |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6527 | VectorTypes.insert(Ty); |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6528 | } else if (Ty->isNullPtrType()) { |
| 6529 | HasNullPtrType = true; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6530 | } else if (AllowUserConversions && TyRec) { |
| 6531 | // No conversion functions in incomplete types. |
| 6532 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) |
| 6533 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6534 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6535 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6536 | std::pair<CXXRecordDecl::conversion_iterator, |
| 6537 | CXXRecordDecl::conversion_iterator> |
| 6538 | Conversions = ClassDecl->getVisibleConversionFunctions(); |
| 6539 | for (CXXRecordDecl::conversion_iterator |
| 6540 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6541 | NamedDecl *D = I.getDecl(); |
| 6542 | if (isa<UsingShadowDecl>(D)) |
| 6543 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6544 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6545 | // Skip conversion function templates; they don't tell us anything |
| 6546 | // about which builtin types we can convert to. |
| 6547 | if (isa<FunctionTemplateDecl>(D)) |
| 6548 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6549 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6550 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
| 6551 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
| 6552 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
| 6553 | VisibleQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6554 | } |
| 6555 | } |
| 6556 | } |
| 6557 | } |
| 6558 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6559 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 6560 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 6561 | /// given type to the candidate set. |
| 6562 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 6563 | QualType T, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6564 | ArrayRef<Expr *> Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6565 | OverloadCandidateSet &CandidateSet) { |
| 6566 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6567 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6568 | // T& operator=(T&, T) |
| 6569 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 6570 | ParamTypes[1] = T; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6571 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6572 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6573 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6574 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 6575 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6576 | ParamTypes[0] |
| 6577 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6578 | ParamTypes[1] = T; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6579 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6580 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6581 | } |
| 6582 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6583 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 6584 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 6585 | /// 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] | 6586 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 6587 | Qualifiers VRQuals; |
| 6588 | const RecordType *TyRec; |
| 6589 | if (const MemberPointerType *RHSMPType = |
| 6590 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6591 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6592 | else |
| 6593 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 6594 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6595 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6596 | VRQuals.addVolatile(); |
| 6597 | VRQuals.addRestrict(); |
| 6598 | return VRQuals; |
| 6599 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6600 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6601 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 6602 | if (!ClassDecl->hasDefinition()) |
| 6603 | return VRQuals; |
| 6604 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6605 | std::pair<CXXRecordDecl::conversion_iterator, |
| 6606 | CXXRecordDecl::conversion_iterator> |
| 6607 | Conversions = ClassDecl->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6608 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6609 | for (CXXRecordDecl::conversion_iterator |
| 6610 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 6611 | NamedDecl *D = I.getDecl(); |
| 6612 | if (isa<UsingShadowDecl>(D)) |
| 6613 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6614 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6615 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 6616 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 6617 | CanTy = ResTypeRef->getPointeeType(); |
| 6618 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 6619 | // as see them. |
| 6620 | bool done = false; |
| 6621 | while (!done) { |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6622 | if (CanTy.isRestrictQualified()) |
| 6623 | VRQuals.addRestrict(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6624 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 6625 | CanTy = ResTypePtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6626 | else if (const MemberPointerType *ResTypeMPtr = |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6627 | CanTy->getAs<MemberPointerType>()) |
| 6628 | CanTy = ResTypeMPtr->getPointeeType(); |
| 6629 | else |
| 6630 | done = true; |
| 6631 | if (CanTy.isVolatileQualified()) |
| 6632 | VRQuals.addVolatile(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6633 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 6634 | return VRQuals; |
| 6635 | } |
| 6636 | } |
| 6637 | } |
| 6638 | return VRQuals; |
| 6639 | } |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6640 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6641 | namespace { |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6642 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6643 | /// \brief Helper class to manage the addition of builtin operator overload |
| 6644 | /// candidates. It provides shared state and utility methods used throughout |
| 6645 | /// the process, as well as a helper method to add each group of builtin |
| 6646 | /// operator overloads from the standard to a candidate set. |
| 6647 | class BuiltinOperatorOverloadBuilder { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6648 | // Common instance state available to all overload candidate addition methods. |
| 6649 | Sema &S; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6650 | ArrayRef<Expr *> Args; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6651 | Qualifiers VisibleTypeConversionsQuals; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6652 | bool HasArithmeticOrEnumeralCandidateType; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6653 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6654 | OverloadCandidateSet &CandidateSet; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6655 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6656 | // Define some constants used to index and iterate over the arithemetic types |
| 6657 | // provided via the getArithmeticType() method below. |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6658 | // The "promoted arithmetic types" are the arithmetic |
| 6659 | // types are that preserved by promotion (C++ [over.built]p2). |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6660 | static const unsigned FirstIntegralType = 3; |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6661 | static const unsigned LastIntegralType = 20; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6662 | static const unsigned FirstPromotedIntegralType = 3, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6663 | LastPromotedIntegralType = 11; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6664 | static const unsigned FirstPromotedArithmeticType = 0, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6665 | LastPromotedArithmeticType = 11; |
| 6666 | static const unsigned NumArithmeticTypes = 20; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6667 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6668 | /// \brief Get the canonical type for a given arithmetic type index. |
| 6669 | CanQualType getArithmeticType(unsigned index) { |
| 6670 | assert(index < NumArithmeticTypes); |
| 6671 | static CanQualType ASTContext::* const |
| 6672 | ArithmeticTypes[NumArithmeticTypes] = { |
| 6673 | // Start of promoted types. |
| 6674 | &ASTContext::FloatTy, |
| 6675 | &ASTContext::DoubleTy, |
| 6676 | &ASTContext::LongDoubleTy, |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6677 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6678 | // Start of integral types. |
| 6679 | &ASTContext::IntTy, |
| 6680 | &ASTContext::LongTy, |
| 6681 | &ASTContext::LongLongTy, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6682 | &ASTContext::Int128Ty, |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6683 | &ASTContext::UnsignedIntTy, |
| 6684 | &ASTContext::UnsignedLongTy, |
| 6685 | &ASTContext::UnsignedLongLongTy, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6686 | &ASTContext::UnsignedInt128Ty, |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6687 | // End of promoted types. |
| 6688 | |
| 6689 | &ASTContext::BoolTy, |
| 6690 | &ASTContext::CharTy, |
| 6691 | &ASTContext::WCharTy, |
| 6692 | &ASTContext::Char16Ty, |
| 6693 | &ASTContext::Char32Ty, |
| 6694 | &ASTContext::SignedCharTy, |
| 6695 | &ASTContext::ShortTy, |
| 6696 | &ASTContext::UnsignedCharTy, |
| 6697 | &ASTContext::UnsignedShortTy, |
| 6698 | // End of integral types. |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6699 | // FIXME: What about complex? What about half? |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6700 | }; |
| 6701 | return S.Context.*ArithmeticTypes[index]; |
| 6702 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6703 | |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6704 | /// \brief Gets the canonical type resulting from the usual arithemetic |
| 6705 | /// converions for the given arithmetic types. |
| 6706 | CanQualType getUsualArithmeticConversions(unsigned L, unsigned R) { |
| 6707 | // Accelerator table for performing the usual arithmetic conversions. |
| 6708 | // The rules are basically: |
| 6709 | // - if either is floating-point, use the wider floating-point |
| 6710 | // - if same signedness, use the higher rank |
| 6711 | // - if same size, use unsigned of the higher rank |
| 6712 | // - use the larger type |
| 6713 | // These rules, together with the axiom that higher ranks are |
| 6714 | // never smaller, are sufficient to precompute all of these results |
| 6715 | // *except* when dealing with signed types of higher rank. |
| 6716 | // (we could precompute SLL x UI for all known platforms, but it's |
| 6717 | // better not to make any assumptions). |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6718 | // 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] | 6719 | enum PromotedType { |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6720 | Dep=-1, |
| 6721 | Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6722 | }; |
Nuno Lopes | 79e244f | 2012-04-21 14:45:25 +0000 | [diff] [blame] | 6723 | static const PromotedType ConversionsTable[LastPromotedArithmeticType] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6724 | [LastPromotedArithmeticType] = { |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6725 | /* Flt*/ { Flt, Dbl, LDbl, Flt, Flt, Flt, Flt, Flt, Flt, Flt, Flt }, |
| 6726 | /* Dbl*/ { Dbl, Dbl, LDbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl }, |
| 6727 | /*LDbl*/ { LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl }, |
| 6728 | /* SI*/ { Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 }, |
| 6729 | /* SL*/ { Flt, Dbl, LDbl, SL, SL, SLL, S128, Dep, UL, ULL, U128 }, |
| 6730 | /* SLL*/ { Flt, Dbl, LDbl, SLL, SLL, SLL, S128, Dep, Dep, ULL, U128 }, |
| 6731 | /*S128*/ { Flt, Dbl, LDbl, S128, S128, S128, S128, S128, S128, S128, U128 }, |
| 6732 | /* UI*/ { Flt, Dbl, LDbl, UI, Dep, Dep, S128, UI, UL, ULL, U128 }, |
| 6733 | /* UL*/ { Flt, Dbl, LDbl, UL, UL, Dep, S128, UL, UL, ULL, U128 }, |
| 6734 | /* ULL*/ { Flt, Dbl, LDbl, ULL, ULL, ULL, S128, ULL, ULL, ULL, U128 }, |
| 6735 | /*U128*/ { Flt, Dbl, LDbl, U128, U128, U128, U128, U128, U128, U128, U128 }, |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6736 | }; |
| 6737 | |
| 6738 | assert(L < LastPromotedArithmeticType); |
| 6739 | assert(R < LastPromotedArithmeticType); |
| 6740 | int Idx = ConversionsTable[L][R]; |
| 6741 | |
| 6742 | // Fast path: the table gives us a concrete answer. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6743 | if (Idx != Dep) return getArithmeticType(Idx); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6744 | |
| 6745 | // Slow path: we need to compare widths. |
| 6746 | // An invariant is that the signed type has higher rank. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6747 | CanQualType LT = getArithmeticType(L), |
| 6748 | RT = getArithmeticType(R); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6749 | unsigned LW = S.Context.getIntWidth(LT), |
| 6750 | RW = S.Context.getIntWidth(RT); |
| 6751 | |
| 6752 | // If they're different widths, use the signed type. |
| 6753 | if (LW > RW) return LT; |
| 6754 | else if (LW < RW) return RT; |
| 6755 | |
| 6756 | // Otherwise, use the unsigned type of the signed type's rank. |
| 6757 | if (L == SL || R == SL) return S.Context.UnsignedLongTy; |
| 6758 | assert(L == SLL || R == SLL); |
| 6759 | return S.Context.UnsignedLongLongTy; |
| 6760 | } |
| 6761 | |
Chandler Carruth | 3c69dc4 | 2010-12-12 09:22:45 +0000 | [diff] [blame] | 6762 | /// \brief Helper method to factor out the common pattern of adding overloads |
| 6763 | /// for '++' and '--' builtin operators. |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6764 | void addPlusPlusMinusMinusStyleOverloads(QualType CandidateTy, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6765 | bool HasVolatile, |
| 6766 | bool HasRestrict) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6767 | QualType ParamTypes[2] = { |
| 6768 | S.Context.getLValueReferenceType(CandidateTy), |
| 6769 | S.Context.IntTy |
| 6770 | }; |
| 6771 | |
| 6772 | // Non-volatile version. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6773 | if (Args.size() == 1) |
| 6774 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6775 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6776 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6777 | |
| 6778 | // Use a heuristic to reduce number of builtin candidates in the set: |
| 6779 | // add volatile version only if there are conversions to a volatile type. |
| 6780 | if (HasVolatile) { |
| 6781 | ParamTypes[0] = |
| 6782 | S.Context.getLValueReferenceType( |
| 6783 | S.Context.getVolatileType(CandidateTy)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6784 | if (Args.size() == 1) |
| 6785 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6786 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6787 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6788 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6789 | |
| 6790 | // Add restrict version only if there are conversions to a restrict type |
| 6791 | // and our candidate type is a non-restrict-qualified pointer. |
| 6792 | if (HasRestrict && CandidateTy->isAnyPointerType() && |
| 6793 | !CandidateTy.isRestrictQualified()) { |
| 6794 | ParamTypes[0] |
| 6795 | = S.Context.getLValueReferenceType( |
| 6796 | S.Context.getCVRQualifiedType(CandidateTy, Qualifiers::Restrict)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6797 | if (Args.size() == 1) |
| 6798 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6799 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6800 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6801 | |
| 6802 | if (HasVolatile) { |
| 6803 | ParamTypes[0] |
| 6804 | = S.Context.getLValueReferenceType( |
| 6805 | S.Context.getCVRQualifiedType(CandidateTy, |
| 6806 | (Qualifiers::Volatile | |
| 6807 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6808 | if (Args.size() == 1) |
| 6809 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6810 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6811 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6812 | } |
| 6813 | } |
| 6814 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6815 | } |
| 6816 | |
| 6817 | public: |
| 6818 | BuiltinOperatorOverloadBuilder( |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6819 | Sema &S, ArrayRef<Expr *> Args, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6820 | Qualifiers VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6821 | bool HasArithmeticOrEnumeralCandidateType, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6822 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6823 | OverloadCandidateSet &CandidateSet) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6824 | : S(S), Args(Args), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6825 | VisibleTypeConversionsQuals(VisibleTypeConversionsQuals), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6826 | HasArithmeticOrEnumeralCandidateType( |
| 6827 | HasArithmeticOrEnumeralCandidateType), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6828 | CandidateTypes(CandidateTypes), |
| 6829 | CandidateSet(CandidateSet) { |
| 6830 | // Validate some of our static helper constants in debug builds. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6831 | assert(getArithmeticType(FirstPromotedIntegralType) == S.Context.IntTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6832 | "Invalid first promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6833 | assert(getArithmeticType(LastPromotedIntegralType - 1) |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6834 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6835 | "Invalid last promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6836 | assert(getArithmeticType(FirstPromotedArithmeticType) |
| 6837 | == S.Context.FloatTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6838 | "Invalid first promoted arithmetic type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6839 | assert(getArithmeticType(LastPromotedArithmeticType - 1) |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6840 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6841 | "Invalid last promoted arithmetic type"); |
| 6842 | } |
| 6843 | |
| 6844 | // C++ [over.built]p3: |
| 6845 | // |
| 6846 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 6847 | // is either volatile or empty, there exist candidate operator |
| 6848 | // functions of the form |
| 6849 | // |
| 6850 | // VQ T& operator++(VQ T&); |
| 6851 | // T operator++(VQ T&, int); |
| 6852 | // |
| 6853 | // C++ [over.built]p4: |
| 6854 | // |
| 6855 | // For every pair (T, VQ), where T is an arithmetic type other |
| 6856 | // than bool, and VQ is either volatile or empty, there exist |
| 6857 | // candidate operator functions of the form |
| 6858 | // |
| 6859 | // VQ T& operator--(VQ T&); |
| 6860 | // T operator--(VQ T&, int); |
| 6861 | void addPlusPlusMinusMinusArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6862 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6863 | return; |
| 6864 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6865 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 6866 | Arith < NumArithmeticTypes; ++Arith) { |
| 6867 | addPlusPlusMinusMinusStyleOverloads( |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6868 | getArithmeticType(Arith), |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6869 | VisibleTypeConversionsQuals.hasVolatile(), |
| 6870 | VisibleTypeConversionsQuals.hasRestrict()); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6871 | } |
| 6872 | } |
| 6873 | |
| 6874 | // C++ [over.built]p5: |
| 6875 | // |
| 6876 | // For every pair (T, VQ), where T is a cv-qualified or |
| 6877 | // cv-unqualified object type, and VQ is either volatile or |
| 6878 | // empty, there exist candidate operator functions of the form |
| 6879 | // |
| 6880 | // T*VQ& operator++(T*VQ&); |
| 6881 | // T*VQ& operator--(T*VQ&); |
| 6882 | // T* operator++(T*VQ&, int); |
| 6883 | // T* operator--(T*VQ&, int); |
| 6884 | void addPlusPlusMinusMinusPointerOverloads() { |
| 6885 | for (BuiltinCandidateTypeSet::iterator |
| 6886 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6887 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6888 | Ptr != PtrEnd; ++Ptr) { |
| 6889 | // Skip pointer types that aren't pointers to object types. |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6890 | if (!(*Ptr)->getPointeeType()->isObjectType()) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6891 | continue; |
| 6892 | |
| 6893 | addPlusPlusMinusMinusStyleOverloads(*Ptr, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6894 | (!(*Ptr).isVolatileQualified() && |
| 6895 | VisibleTypeConversionsQuals.hasVolatile()), |
| 6896 | (!(*Ptr).isRestrictQualified() && |
| 6897 | VisibleTypeConversionsQuals.hasRestrict())); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6898 | } |
| 6899 | } |
| 6900 | |
| 6901 | // C++ [over.built]p6: |
| 6902 | // For every cv-qualified or cv-unqualified object type T, there |
| 6903 | // exist candidate operator functions of the form |
| 6904 | // |
| 6905 | // T& operator*(T*); |
| 6906 | // |
| 6907 | // C++ [over.built]p7: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6908 | // 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] | 6909 | // ref-qualifier, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6910 | // T& operator*(T*); |
| 6911 | void addUnaryStarPointerOverloads() { |
| 6912 | for (BuiltinCandidateTypeSet::iterator |
| 6913 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6914 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6915 | Ptr != PtrEnd; ++Ptr) { |
| 6916 | QualType ParamTy = *Ptr; |
| 6917 | QualType PointeeTy = ParamTy->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6918 | if (!PointeeTy->isObjectType() && !PointeeTy->isFunctionType()) |
| 6919 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6920 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6921 | if (const FunctionProtoType *Proto =PointeeTy->getAs<FunctionProtoType>()) |
| 6922 | if (Proto->getTypeQuals() || Proto->getRefQualifier()) |
| 6923 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6924 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6925 | S.AddBuiltinCandidate(S.Context.getLValueReferenceType(PointeeTy), |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6926 | &ParamTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6927 | } |
| 6928 | } |
| 6929 | |
| 6930 | // C++ [over.built]p9: |
| 6931 | // For every promoted arithmetic type T, there exist candidate |
| 6932 | // operator functions of the form |
| 6933 | // |
| 6934 | // T operator+(T); |
| 6935 | // T operator-(T); |
| 6936 | void addUnaryPlusOrMinusArithmeticOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6937 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6938 | return; |
| 6939 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6940 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 6941 | Arith < LastPromotedArithmeticType; ++Arith) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6942 | QualType ArithTy = getArithmeticType(Arith); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6943 | S.AddBuiltinCandidate(ArithTy, &ArithTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6944 | } |
| 6945 | |
| 6946 | // Extension: We also add these operators for vector types. |
| 6947 | for (BuiltinCandidateTypeSet::iterator |
| 6948 | Vec = CandidateTypes[0].vector_begin(), |
| 6949 | VecEnd = CandidateTypes[0].vector_end(); |
| 6950 | Vec != VecEnd; ++Vec) { |
| 6951 | QualType VecTy = *Vec; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6952 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6953 | } |
| 6954 | } |
| 6955 | |
| 6956 | // C++ [over.built]p8: |
| 6957 | // For every type T, there exist candidate operator functions of |
| 6958 | // the form |
| 6959 | // |
| 6960 | // T* operator+(T*); |
| 6961 | void addUnaryPlusPointerOverloads() { |
| 6962 | for (BuiltinCandidateTypeSet::iterator |
| 6963 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6964 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6965 | Ptr != PtrEnd; ++Ptr) { |
| 6966 | QualType ParamTy = *Ptr; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6967 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6968 | } |
| 6969 | } |
| 6970 | |
| 6971 | // C++ [over.built]p10: |
| 6972 | // For every promoted integral type T, there exist candidate |
| 6973 | // operator functions of the form |
| 6974 | // |
| 6975 | // T operator~(T); |
| 6976 | void addUnaryTildePromotedIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6977 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6978 | return; |
| 6979 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6980 | for (unsigned Int = FirstPromotedIntegralType; |
| 6981 | Int < LastPromotedIntegralType; ++Int) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6982 | QualType IntTy = getArithmeticType(Int); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6983 | S.AddBuiltinCandidate(IntTy, &IntTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6984 | } |
| 6985 | |
| 6986 | // Extension: We also add this operator for vector types. |
| 6987 | for (BuiltinCandidateTypeSet::iterator |
| 6988 | Vec = CandidateTypes[0].vector_begin(), |
| 6989 | VecEnd = CandidateTypes[0].vector_end(); |
| 6990 | Vec != VecEnd; ++Vec) { |
| 6991 | QualType VecTy = *Vec; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6992 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6993 | } |
| 6994 | } |
| 6995 | |
| 6996 | // C++ [over.match.oper]p16: |
| 6997 | // For every pointer to member type T, there exist candidate operator |
| 6998 | // functions of the form |
| 6999 | // |
| 7000 | // bool operator==(T,T); |
| 7001 | // bool operator!=(T,T); |
| 7002 | void addEqualEqualOrNotEqualMemberPointerOverloads() { |
| 7003 | /// Set of (canonical) types that we've already handled. |
| 7004 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7005 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7006 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7007 | for (BuiltinCandidateTypeSet::iterator |
| 7008 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7009 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7010 | MemPtr != MemPtrEnd; |
| 7011 | ++MemPtr) { |
| 7012 | // Don't add the same builtin candidate twice. |
| 7013 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7014 | continue; |
| 7015 | |
| 7016 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7017 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7018 | } |
| 7019 | } |
| 7020 | } |
| 7021 | |
| 7022 | // C++ [over.built]p15: |
| 7023 | // |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7024 | // For every T, where T is an enumeration type, a pointer type, or |
| 7025 | // std::nullptr_t, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7026 | // |
| 7027 | // bool operator<(T, T); |
| 7028 | // bool operator>(T, T); |
| 7029 | // bool operator<=(T, T); |
| 7030 | // bool operator>=(T, T); |
| 7031 | // bool operator==(T, T); |
| 7032 | // bool operator!=(T, T); |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7033 | void addRelationalPointerOrEnumeralOverloads() { |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 7034 | // C++ [over.match.oper]p3: |
| 7035 | // [...]the built-in candidates include all of the candidate operator |
| 7036 | // functions defined in 13.6 that, compared to the given operator, [...] |
| 7037 | // do not have the same parameter-type-list as any non-template non-member |
| 7038 | // candidate. |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7039 | // |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 7040 | // Note that in practice, this only affects enumeration types because there |
| 7041 | // aren't any built-in candidates of record type, and a user-defined operator |
| 7042 | // must have an operand of record or enumeration type. Also, the only other |
| 7043 | // overloaded operator with enumeration arguments, operator=, |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7044 | // cannot be overloaded for enumeration types, so this is the only place |
| 7045 | // where we must suppress candidates like this. |
| 7046 | llvm::DenseSet<std::pair<CanQualType, CanQualType> > |
| 7047 | UserDefinedBinaryOperators; |
| 7048 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7049 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7050 | if (CandidateTypes[ArgIdx].enumeration_begin() != |
| 7051 | CandidateTypes[ArgIdx].enumeration_end()) { |
| 7052 | for (OverloadCandidateSet::iterator C = CandidateSet.begin(), |
| 7053 | CEnd = CandidateSet.end(); |
| 7054 | C != CEnd; ++C) { |
| 7055 | if (!C->Viable || !C->Function || C->Function->getNumParams() != 2) |
| 7056 | continue; |
| 7057 | |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 7058 | if (C->Function->isFunctionTemplateSpecialization()) |
| 7059 | continue; |
| 7060 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7061 | QualType FirstParamType = |
| 7062 | C->Function->getParamDecl(0)->getType().getUnqualifiedType(); |
| 7063 | QualType SecondParamType = |
| 7064 | C->Function->getParamDecl(1)->getType().getUnqualifiedType(); |
| 7065 | |
| 7066 | // Skip if either parameter isn't of enumeral type. |
| 7067 | if (!FirstParamType->isEnumeralType() || |
| 7068 | !SecondParamType->isEnumeralType()) |
| 7069 | continue; |
| 7070 | |
| 7071 | // Add this operator to the set of known user-defined operators. |
| 7072 | UserDefinedBinaryOperators.insert( |
| 7073 | std::make_pair(S.Context.getCanonicalType(FirstParamType), |
| 7074 | S.Context.getCanonicalType(SecondParamType))); |
| 7075 | } |
| 7076 | } |
| 7077 | } |
| 7078 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7079 | /// Set of (canonical) types that we've already handled. |
| 7080 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7081 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7082 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7083 | for (BuiltinCandidateTypeSet::iterator |
| 7084 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 7085 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 7086 | Ptr != PtrEnd; ++Ptr) { |
| 7087 | // Don't add the same builtin candidate twice. |
| 7088 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7089 | continue; |
| 7090 | |
| 7091 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7092 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7093 | } |
| 7094 | for (BuiltinCandidateTypeSet::iterator |
| 7095 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7096 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7097 | Enum != EnumEnd; ++Enum) { |
| 7098 | CanQualType CanonType = S.Context.getCanonicalType(*Enum); |
| 7099 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7100 | // Don't add the same builtin candidate twice, or if a user defined |
| 7101 | // candidate exists. |
| 7102 | if (!AddedTypes.insert(CanonType) || |
| 7103 | UserDefinedBinaryOperators.count(std::make_pair(CanonType, |
| 7104 | CanonType))) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7105 | continue; |
| 7106 | |
| 7107 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7108 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7109 | } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7110 | |
| 7111 | if (CandidateTypes[ArgIdx].hasNullPtrType()) { |
| 7112 | CanQualType NullPtrTy = S.Context.getCanonicalType(S.Context.NullPtrTy); |
| 7113 | if (AddedTypes.insert(NullPtrTy) && |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7114 | !UserDefinedBinaryOperators.count(std::make_pair(NullPtrTy, |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7115 | NullPtrTy))) { |
| 7116 | QualType ParamTypes[2] = { NullPtrTy, NullPtrTy }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7117 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7118 | CandidateSet); |
| 7119 | } |
| 7120 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7121 | } |
| 7122 | } |
| 7123 | |
| 7124 | // C++ [over.built]p13: |
| 7125 | // |
| 7126 | // For every cv-qualified or cv-unqualified object type T |
| 7127 | // there exist candidate operator functions of the form |
| 7128 | // |
| 7129 | // T* operator+(T*, ptrdiff_t); |
| 7130 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 7131 | // T* operator-(T*, ptrdiff_t); |
| 7132 | // T* operator+(ptrdiff_t, T*); |
| 7133 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 7134 | // |
| 7135 | // C++ [over.built]p14: |
| 7136 | // |
| 7137 | // For every T, where T is a pointer to object type, there |
| 7138 | // exist candidate operator functions of the form |
| 7139 | // |
| 7140 | // ptrdiff_t operator-(T, T); |
| 7141 | void addBinaryPlusOrMinusPointerOverloads(OverloadedOperatorKind Op) { |
| 7142 | /// Set of (canonical) types that we've already handled. |
| 7143 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7144 | |
| 7145 | for (int Arg = 0; Arg < 2; ++Arg) { |
| 7146 | QualType AsymetricParamTypes[2] = { |
| 7147 | S.Context.getPointerDiffType(), |
| 7148 | S.Context.getPointerDiffType(), |
| 7149 | }; |
| 7150 | for (BuiltinCandidateTypeSet::iterator |
| 7151 | Ptr = CandidateTypes[Arg].pointer_begin(), |
| 7152 | PtrEnd = CandidateTypes[Arg].pointer_end(); |
| 7153 | Ptr != PtrEnd; ++Ptr) { |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7154 | QualType PointeeTy = (*Ptr)->getPointeeType(); |
| 7155 | if (!PointeeTy->isObjectType()) |
| 7156 | continue; |
| 7157 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7158 | AsymetricParamTypes[Arg] = *Ptr; |
| 7159 | if (Arg == 0 || Op == OO_Plus) { |
| 7160 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 7161 | // T* operator+(ptrdiff_t, T*); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7162 | S.AddBuiltinCandidate(*Ptr, AsymetricParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7163 | } |
| 7164 | if (Op == OO_Minus) { |
| 7165 | // ptrdiff_t operator-(T, T); |
| 7166 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7167 | continue; |
| 7168 | |
| 7169 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 7170 | S.AddBuiltinCandidate(S.Context.getPointerDiffType(), ParamTypes, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7171 | Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7172 | } |
| 7173 | } |
| 7174 | } |
| 7175 | } |
| 7176 | |
| 7177 | // C++ [over.built]p12: |
| 7178 | // |
| 7179 | // For every pair of promoted arithmetic types L and R, there |
| 7180 | // exist candidate operator functions of the form |
| 7181 | // |
| 7182 | // LR operator*(L, R); |
| 7183 | // LR operator/(L, R); |
| 7184 | // LR operator+(L, R); |
| 7185 | // LR operator-(L, R); |
| 7186 | // bool operator<(L, R); |
| 7187 | // bool operator>(L, R); |
| 7188 | // bool operator<=(L, R); |
| 7189 | // bool operator>=(L, R); |
| 7190 | // bool operator==(L, R); |
| 7191 | // bool operator!=(L, R); |
| 7192 | // |
| 7193 | // where LR is the result of the usual arithmetic conversions |
| 7194 | // between types L and R. |
| 7195 | // |
| 7196 | // C++ [over.built]p24: |
| 7197 | // |
| 7198 | // For every pair of promoted arithmetic types L and R, there exist |
| 7199 | // candidate operator functions of the form |
| 7200 | // |
| 7201 | // LR operator?(bool, L, R); |
| 7202 | // |
| 7203 | // where LR is the result of the usual arithmetic conversions |
| 7204 | // between types L and R. |
| 7205 | // Our candidates ignore the first parameter. |
| 7206 | void addGenericBinaryArithmeticOverloads(bool isComparison) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7207 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7208 | return; |
| 7209 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7210 | for (unsigned Left = FirstPromotedArithmeticType; |
| 7211 | Left < LastPromotedArithmeticType; ++Left) { |
| 7212 | for (unsigned Right = FirstPromotedArithmeticType; |
| 7213 | Right < LastPromotedArithmeticType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7214 | QualType LandR[2] = { getArithmeticType(Left), |
| 7215 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7216 | QualType Result = |
| 7217 | isComparison ? S.Context.BoolTy |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7218 | : getUsualArithmeticConversions(Left, Right); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7219 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7220 | } |
| 7221 | } |
| 7222 | |
| 7223 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 7224 | // conditional operator for vector types. |
| 7225 | for (BuiltinCandidateTypeSet::iterator |
| 7226 | Vec1 = CandidateTypes[0].vector_begin(), |
| 7227 | Vec1End = CandidateTypes[0].vector_end(); |
| 7228 | Vec1 != Vec1End; ++Vec1) { |
| 7229 | for (BuiltinCandidateTypeSet::iterator |
| 7230 | Vec2 = CandidateTypes[1].vector_begin(), |
| 7231 | Vec2End = CandidateTypes[1].vector_end(); |
| 7232 | Vec2 != Vec2End; ++Vec2) { |
| 7233 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 7234 | QualType Result = S.Context.BoolTy; |
| 7235 | if (!isComparison) { |
| 7236 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 7237 | Result = *Vec1; |
| 7238 | else |
| 7239 | Result = *Vec2; |
| 7240 | } |
| 7241 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7242 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7243 | } |
| 7244 | } |
| 7245 | } |
| 7246 | |
| 7247 | // C++ [over.built]p17: |
| 7248 | // |
| 7249 | // For every pair of promoted integral types L and R, there |
| 7250 | // exist candidate operator functions of the form |
| 7251 | // |
| 7252 | // LR operator%(L, R); |
| 7253 | // LR operator&(L, R); |
| 7254 | // LR operator^(L, R); |
| 7255 | // LR operator|(L, R); |
| 7256 | // L operator<<(L, R); |
| 7257 | // L operator>>(L, R); |
| 7258 | // |
| 7259 | // where LR is the result of the usual arithmetic conversions |
| 7260 | // between types L and R. |
| 7261 | void addBinaryBitwiseArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7262 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7263 | return; |
| 7264 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7265 | for (unsigned Left = FirstPromotedIntegralType; |
| 7266 | Left < LastPromotedIntegralType; ++Left) { |
| 7267 | for (unsigned Right = FirstPromotedIntegralType; |
| 7268 | Right < LastPromotedIntegralType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7269 | QualType LandR[2] = { getArithmeticType(Left), |
| 7270 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7271 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 7272 | ? LandR[0] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7273 | : getUsualArithmeticConversions(Left, Right); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7274 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7275 | } |
| 7276 | } |
| 7277 | } |
| 7278 | |
| 7279 | // C++ [over.built]p20: |
| 7280 | // |
| 7281 | // For every pair (T, VQ), where T is an enumeration or |
| 7282 | // pointer to member type and VQ is either volatile or |
| 7283 | // empty, there exist candidate operator functions of the form |
| 7284 | // |
| 7285 | // VQ T& operator=(VQ T&, T); |
| 7286 | void addAssignmentMemberPointerOrEnumeralOverloads() { |
| 7287 | /// Set of (canonical) types that we've already handled. |
| 7288 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7289 | |
| 7290 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7291 | for (BuiltinCandidateTypeSet::iterator |
| 7292 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7293 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7294 | Enum != EnumEnd; ++Enum) { |
| 7295 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 7296 | continue; |
| 7297 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7298 | AddBuiltinAssignmentOperatorCandidates(S, *Enum, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7299 | } |
| 7300 | |
| 7301 | for (BuiltinCandidateTypeSet::iterator |
| 7302 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7303 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7304 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7305 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7306 | continue; |
| 7307 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7308 | AddBuiltinAssignmentOperatorCandidates(S, *MemPtr, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7309 | } |
| 7310 | } |
| 7311 | } |
| 7312 | |
| 7313 | // C++ [over.built]p19: |
| 7314 | // |
| 7315 | // For every pair (T, VQ), where T is any type and VQ is either |
| 7316 | // volatile or empty, there exist candidate operator functions |
| 7317 | // of the form |
| 7318 | // |
| 7319 | // T*VQ& operator=(T*VQ&, T*); |
| 7320 | // |
| 7321 | // C++ [over.built]p21: |
| 7322 | // |
| 7323 | // For every pair (T, VQ), where T is a cv-qualified or |
| 7324 | // cv-unqualified object type and VQ is either volatile or |
| 7325 | // empty, there exist candidate operator functions of the form |
| 7326 | // |
| 7327 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 7328 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 7329 | void addAssignmentPointerOverloads(bool isEqualOp) { |
| 7330 | /// Set of (canonical) types that we've already handled. |
| 7331 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7332 | |
| 7333 | for (BuiltinCandidateTypeSet::iterator |
| 7334 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7335 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7336 | Ptr != PtrEnd; ++Ptr) { |
| 7337 | // If this is operator=, keep track of the builtin candidates we added. |
| 7338 | if (isEqualOp) |
| 7339 | AddedTypes.insert(S.Context.getCanonicalType(*Ptr)); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7340 | else if (!(*Ptr)->getPointeeType()->isObjectType()) |
| 7341 | continue; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7342 | |
| 7343 | // non-volatile version |
| 7344 | QualType ParamTypes[2] = { |
| 7345 | S.Context.getLValueReferenceType(*Ptr), |
| 7346 | isEqualOp ? *Ptr : S.Context.getPointerDiffType(), |
| 7347 | }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7348 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7349 | /*IsAssigmentOperator=*/ isEqualOp); |
| 7350 | |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7351 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7352 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7353 | if (NeedVolatile) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7354 | // volatile version |
| 7355 | ParamTypes[0] = |
| 7356 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7357 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7358 | /*IsAssigmentOperator=*/isEqualOp); |
| 7359 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7360 | |
| 7361 | if (!(*Ptr).isRestrictQualified() && |
| 7362 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7363 | // restrict version |
| 7364 | ParamTypes[0] |
| 7365 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7366 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7367 | /*IsAssigmentOperator=*/isEqualOp); |
| 7368 | |
| 7369 | if (NeedVolatile) { |
| 7370 | // volatile restrict version |
| 7371 | ParamTypes[0] |
| 7372 | = S.Context.getLValueReferenceType( |
| 7373 | S.Context.getCVRQualifiedType(*Ptr, |
| 7374 | (Qualifiers::Volatile | |
| 7375 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7376 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7377 | /*IsAssigmentOperator=*/isEqualOp); |
| 7378 | } |
| 7379 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7380 | } |
| 7381 | |
| 7382 | if (isEqualOp) { |
| 7383 | for (BuiltinCandidateTypeSet::iterator |
| 7384 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7385 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7386 | Ptr != PtrEnd; ++Ptr) { |
| 7387 | // Make sure we don't add the same candidate twice. |
| 7388 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7389 | continue; |
| 7390 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7391 | QualType ParamTypes[2] = { |
| 7392 | S.Context.getLValueReferenceType(*Ptr), |
| 7393 | *Ptr, |
| 7394 | }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7395 | |
| 7396 | // non-volatile version |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7397 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7398 | /*IsAssigmentOperator=*/true); |
| 7399 | |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7400 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7401 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7402 | if (NeedVolatile) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7403 | // volatile version |
| 7404 | ParamTypes[0] = |
| 7405 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7406 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7407 | /*IsAssigmentOperator=*/true); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7408 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7409 | |
| 7410 | if (!(*Ptr).isRestrictQualified() && |
| 7411 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7412 | // restrict version |
| 7413 | ParamTypes[0] |
| 7414 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7415 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7416 | /*IsAssigmentOperator=*/true); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7417 | |
| 7418 | if (NeedVolatile) { |
| 7419 | // volatile restrict version |
| 7420 | ParamTypes[0] |
| 7421 | = S.Context.getLValueReferenceType( |
| 7422 | S.Context.getCVRQualifiedType(*Ptr, |
| 7423 | (Qualifiers::Volatile | |
| 7424 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7425 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7426 | /*IsAssigmentOperator=*/true); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7427 | } |
| 7428 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7429 | } |
| 7430 | } |
| 7431 | } |
| 7432 | |
| 7433 | // C++ [over.built]p18: |
| 7434 | // |
| 7435 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 7436 | // VQ is either volatile or empty, and R is a promoted |
| 7437 | // arithmetic type, there exist candidate operator functions of |
| 7438 | // the form |
| 7439 | // |
| 7440 | // VQ L& operator=(VQ L&, R); |
| 7441 | // VQ L& operator*=(VQ L&, R); |
| 7442 | // VQ L& operator/=(VQ L&, R); |
| 7443 | // VQ L& operator+=(VQ L&, R); |
| 7444 | // VQ L& operator-=(VQ L&, R); |
| 7445 | void addAssignmentArithmeticOverloads(bool isEqualOp) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7446 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7447 | return; |
| 7448 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7449 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 7450 | for (unsigned Right = FirstPromotedArithmeticType; |
| 7451 | Right < LastPromotedArithmeticType; ++Right) { |
| 7452 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7453 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7454 | |
| 7455 | // Add this built-in operator as a candidate (VQ is empty). |
| 7456 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7457 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7458 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7459 | /*IsAssigmentOperator=*/isEqualOp); |
| 7460 | |
| 7461 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7462 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7463 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7464 | S.Context.getVolatileType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7465 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7466 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7467 | /*IsAssigmentOperator=*/isEqualOp); |
| 7468 | } |
| 7469 | } |
| 7470 | } |
| 7471 | |
| 7472 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 7473 | for (BuiltinCandidateTypeSet::iterator |
| 7474 | Vec1 = CandidateTypes[0].vector_begin(), |
| 7475 | Vec1End = CandidateTypes[0].vector_end(); |
| 7476 | Vec1 != Vec1End; ++Vec1) { |
| 7477 | for (BuiltinCandidateTypeSet::iterator |
| 7478 | Vec2 = CandidateTypes[1].vector_begin(), |
| 7479 | Vec2End = CandidateTypes[1].vector_end(); |
| 7480 | Vec2 != Vec2End; ++Vec2) { |
| 7481 | QualType ParamTypes[2]; |
| 7482 | ParamTypes[1] = *Vec2; |
| 7483 | // Add this built-in operator as a candidate (VQ is empty). |
| 7484 | ParamTypes[0] = S.Context.getLValueReferenceType(*Vec1); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7485 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7486 | /*IsAssigmentOperator=*/isEqualOp); |
| 7487 | |
| 7488 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7489 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7490 | ParamTypes[0] = S.Context.getVolatileType(*Vec1); |
| 7491 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7492 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7493 | /*IsAssigmentOperator=*/isEqualOp); |
| 7494 | } |
| 7495 | } |
| 7496 | } |
| 7497 | } |
| 7498 | |
| 7499 | // C++ [over.built]p22: |
| 7500 | // |
| 7501 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 7502 | // is either volatile or empty, and R is a promoted integral |
| 7503 | // type, there exist candidate operator functions of the form |
| 7504 | // |
| 7505 | // VQ L& operator%=(VQ L&, R); |
| 7506 | // VQ L& operator<<=(VQ L&, R); |
| 7507 | // VQ L& operator>>=(VQ L&, R); |
| 7508 | // VQ L& operator&=(VQ L&, R); |
| 7509 | // VQ L& operator^=(VQ L&, R); |
| 7510 | // VQ L& operator|=(VQ L&, R); |
| 7511 | void addAssignmentIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7512 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7513 | return; |
| 7514 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7515 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 7516 | for (unsigned Right = FirstPromotedIntegralType; |
| 7517 | Right < LastPromotedIntegralType; ++Right) { |
| 7518 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7519 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7520 | |
| 7521 | // Add this built-in operator as a candidate (VQ is empty). |
| 7522 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7523 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7524 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7525 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7526 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7527 | ParamTypes[0] = getArithmeticType(Left); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7528 | ParamTypes[0] = S.Context.getVolatileType(ParamTypes[0]); |
| 7529 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7530 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7531 | } |
| 7532 | } |
| 7533 | } |
| 7534 | } |
| 7535 | |
| 7536 | // C++ [over.operator]p23: |
| 7537 | // |
| 7538 | // There also exist candidate operator functions of the form |
| 7539 | // |
| 7540 | // bool operator!(bool); |
| 7541 | // bool operator&&(bool, bool); |
| 7542 | // bool operator||(bool, bool); |
| 7543 | void addExclaimOverload() { |
| 7544 | QualType ParamTy = S.Context.BoolTy; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7545 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7546 | /*IsAssignmentOperator=*/false, |
| 7547 | /*NumContextualBoolArguments=*/1); |
| 7548 | } |
| 7549 | void addAmpAmpOrPipePipeOverload() { |
| 7550 | QualType ParamTypes[2] = { S.Context.BoolTy, S.Context.BoolTy }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7551 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7552 | /*IsAssignmentOperator=*/false, |
| 7553 | /*NumContextualBoolArguments=*/2); |
| 7554 | } |
| 7555 | |
| 7556 | // C++ [over.built]p13: |
| 7557 | // |
| 7558 | // For every cv-qualified or cv-unqualified object type T there |
| 7559 | // exist candidate operator functions of the form |
| 7560 | // |
| 7561 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 7562 | // T& operator[](T*, ptrdiff_t); |
| 7563 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 7564 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 7565 | // T& operator[](ptrdiff_t, T*); |
| 7566 | void addSubscriptOverloads() { |
| 7567 | for (BuiltinCandidateTypeSet::iterator |
| 7568 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7569 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7570 | Ptr != PtrEnd; ++Ptr) { |
| 7571 | QualType ParamTypes[2] = { *Ptr, S.Context.getPointerDiffType() }; |
| 7572 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7573 | if (!PointeeType->isObjectType()) |
| 7574 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7575 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7576 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7577 | |
| 7578 | // T& operator[](T*, ptrdiff_t) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7579 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7580 | } |
| 7581 | |
| 7582 | for (BuiltinCandidateTypeSet::iterator |
| 7583 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7584 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7585 | Ptr != PtrEnd; ++Ptr) { |
| 7586 | QualType ParamTypes[2] = { S.Context.getPointerDiffType(), *Ptr }; |
| 7587 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7588 | if (!PointeeType->isObjectType()) |
| 7589 | continue; |
| 7590 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7591 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7592 | |
| 7593 | // T& operator[](ptrdiff_t, 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 | // C++ [over.built]p11: |
| 7599 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 7600 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 7601 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 7602 | // there exist candidate operator functions of the form |
| 7603 | // |
| 7604 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 7605 | // |
| 7606 | // where CV12 is the union of CV1 and CV2. |
| 7607 | void addArrowStarOverloads() { |
| 7608 | for (BuiltinCandidateTypeSet::iterator |
| 7609 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7610 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7611 | Ptr != PtrEnd; ++Ptr) { |
| 7612 | QualType C1Ty = (*Ptr); |
| 7613 | QualType C1; |
| 7614 | QualifierCollector Q1; |
| 7615 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
| 7616 | if (!isa<RecordType>(C1)) |
| 7617 | continue; |
| 7618 | // heuristic to reduce number of builtin candidates in the set. |
| 7619 | // Add volatile/restrict version only if there are conversions to a |
| 7620 | // volatile/restrict type. |
| 7621 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 7622 | continue; |
| 7623 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 7624 | continue; |
| 7625 | for (BuiltinCandidateTypeSet::iterator |
| 7626 | MemPtr = CandidateTypes[1].member_pointer_begin(), |
| 7627 | MemPtrEnd = CandidateTypes[1].member_pointer_end(); |
| 7628 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7629 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 7630 | QualType C2 = QualType(mptr->getClass(), 0); |
| 7631 | C2 = C2.getUnqualifiedType(); |
| 7632 | if (C1 != C2 && !S.IsDerivedFrom(C1, C2)) |
| 7633 | break; |
| 7634 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 7635 | // build CV12 T& |
| 7636 | QualType T = mptr->getPointeeType(); |
| 7637 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 7638 | T.isVolatileQualified()) |
| 7639 | continue; |
| 7640 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 7641 | T.isRestrictQualified()) |
| 7642 | continue; |
| 7643 | T = Q1.apply(S.Context, T); |
| 7644 | QualType ResultTy = S.Context.getLValueReferenceType(T); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7645 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7646 | } |
| 7647 | } |
| 7648 | } |
| 7649 | |
| 7650 | // Note that we don't consider the first argument, since it has been |
| 7651 | // contextually converted to bool long ago. The candidates below are |
| 7652 | // therefore added as binary. |
| 7653 | // |
| 7654 | // C++ [over.built]p25: |
| 7655 | // For every type T, where T is a pointer, pointer-to-member, or scoped |
| 7656 | // enumeration type, there exist candidate operator functions of the form |
| 7657 | // |
| 7658 | // T operator?(bool, T, T); |
| 7659 | // |
| 7660 | void addConditionalOperatorOverloads() { |
| 7661 | /// Set of (canonical) types that we've already handled. |
| 7662 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7663 | |
| 7664 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7665 | for (BuiltinCandidateTypeSet::iterator |
| 7666 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 7667 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 7668 | Ptr != PtrEnd; ++Ptr) { |
| 7669 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7670 | continue; |
| 7671 | |
| 7672 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7673 | S.AddBuiltinCandidate(*Ptr, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7674 | } |
| 7675 | |
| 7676 | for (BuiltinCandidateTypeSet::iterator |
| 7677 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7678 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7679 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7680 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7681 | continue; |
| 7682 | |
| 7683 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7684 | S.AddBuiltinCandidate(*MemPtr, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7685 | } |
| 7686 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7687 | if (S.getLangOpts().CPlusPlus11) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7688 | for (BuiltinCandidateTypeSet::iterator |
| 7689 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7690 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7691 | Enum != EnumEnd; ++Enum) { |
| 7692 | if (!(*Enum)->getAs<EnumType>()->getDecl()->isScoped()) |
| 7693 | continue; |
| 7694 | |
| 7695 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 7696 | continue; |
| 7697 | |
| 7698 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7699 | S.AddBuiltinCandidate(*Enum, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7700 | } |
| 7701 | } |
| 7702 | } |
| 7703 | } |
| 7704 | }; |
| 7705 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7706 | } // end anonymous namespace |
| 7707 | |
| 7708 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 7709 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 7710 | /// on the operator @p Op and the arguments given. For example, if the |
| 7711 | /// operator is a binary '+', this routine might add "int |
| 7712 | /// operator+(int, int)" to cover integer addition. |
| 7713 | void |
| 7714 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 7715 | SourceLocation OpLoc, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7716 | llvm::ArrayRef<Expr *> Args, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7717 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7718 | // Find all of the types that the arguments can convert to, but only |
| 7719 | // if the operator we're looking at has built-in operator candidates |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7720 | // that make use of these types. Also record whether we encounter non-record |
| 7721 | // candidate types or either arithmetic or enumeral candidate types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7722 | Qualifiers VisibleTypeConversionsQuals; |
| 7723 | VisibleTypeConversionsQuals.addConst(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7724 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 7725 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7726 | |
| 7727 | bool HasNonRecordCandidateType = false; |
| 7728 | bool HasArithmeticOrEnumeralCandidateType = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7729 | SmallVector<BuiltinCandidateTypeSet, 2> CandidateTypes; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7730 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7731 | CandidateTypes.push_back(BuiltinCandidateTypeSet(*this)); |
| 7732 | CandidateTypes[ArgIdx].AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 7733 | OpLoc, |
| 7734 | true, |
| 7735 | (Op == OO_Exclaim || |
| 7736 | Op == OO_AmpAmp || |
| 7737 | Op == OO_PipePipe), |
| 7738 | VisibleTypeConversionsQuals); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7739 | HasNonRecordCandidateType = HasNonRecordCandidateType || |
| 7740 | CandidateTypes[ArgIdx].hasNonRecordTypes(); |
| 7741 | HasArithmeticOrEnumeralCandidateType = |
| 7742 | HasArithmeticOrEnumeralCandidateType || |
| 7743 | CandidateTypes[ArgIdx].hasArithmeticOrEnumeralTypes(); |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7744 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7745 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7746 | // Exit early when no non-record types have been added to the candidate set |
| 7747 | // for any of the arguments to the operator. |
Douglas Gregor | 25aaff9 | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 7748 | // |
| 7749 | // We can't exit early for !, ||, or &&, since there we have always have |
| 7750 | // 'bool' overloads. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7751 | if (!HasNonRecordCandidateType && |
Douglas Gregor | 25aaff9 | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 7752 | !(Op == OO_Exclaim || Op == OO_AmpAmp || Op == OO_PipePipe)) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7753 | return; |
| 7754 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7755 | // Setup an object to manage the common state for building overloads. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7756 | BuiltinOperatorOverloadBuilder OpBuilder(*this, Args, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7757 | VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7758 | HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7759 | CandidateTypes, CandidateSet); |
| 7760 | |
| 7761 | // Dispatch over the operation to add in only those overloads which apply. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7762 | switch (Op) { |
| 7763 | case OO_None: |
| 7764 | case NUM_OVERLOADED_OPERATORS: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7765 | llvm_unreachable("Expected an overloaded operator"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7766 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7767 | case OO_New: |
| 7768 | case OO_Delete: |
| 7769 | case OO_Array_New: |
| 7770 | case OO_Array_Delete: |
| 7771 | case OO_Call: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7772 | llvm_unreachable( |
| 7773 | "Special operators don't use AddBuiltinOperatorCandidates"); |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7774 | |
| 7775 | case OO_Comma: |
| 7776 | case OO_Arrow: |
| 7777 | // C++ [over.match.oper]p3: |
| 7778 | // -- For the operator ',', the unary operator '&', or the |
| 7779 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7780 | break; |
| 7781 | |
| 7782 | case OO_Plus: // '+' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7783 | if (Args.size() == 1) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7784 | OpBuilder.addUnaryPlusPointerOverloads(); |
Chandler Carruth | 32fe0d0 | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 7785 | // Fall through. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7786 | |
| 7787 | case OO_Minus: // '-' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7788 | if (Args.size() == 1) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7789 | OpBuilder.addUnaryPlusOrMinusArithmeticOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7790 | } else { |
| 7791 | OpBuilder.addBinaryPlusOrMinusPointerOverloads(Op); |
| 7792 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7793 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7794 | break; |
| 7795 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7796 | case OO_Star: // '*' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7797 | if (Args.size() == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7798 | OpBuilder.addUnaryStarPointerOverloads(); |
| 7799 | else |
| 7800 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7801 | break; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7802 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7803 | case OO_Slash: |
| 7804 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 7805 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7806 | |
| 7807 | case OO_PlusPlus: |
| 7808 | case OO_MinusMinus: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7809 | OpBuilder.addPlusPlusMinusMinusArithmeticOverloads(Op); |
| 7810 | OpBuilder.addPlusPlusMinusMinusPointerOverloads(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7811 | break; |
| 7812 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 7813 | case OO_EqualEqual: |
| 7814 | case OO_ExclaimEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7815 | OpBuilder.addEqualEqualOrNotEqualMemberPointerOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 7816 | // Fall through. |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 7817 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7818 | case OO_Less: |
| 7819 | case OO_Greater: |
| 7820 | case OO_LessEqual: |
| 7821 | case OO_GreaterEqual: |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7822 | OpBuilder.addRelationalPointerOrEnumeralOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 7823 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/true); |
| 7824 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7825 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7826 | case OO_Percent: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7827 | case OO_Caret: |
| 7828 | case OO_Pipe: |
| 7829 | case OO_LessLess: |
| 7830 | case OO_GreaterGreater: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7831 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7832 | break; |
| 7833 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7834 | case OO_Amp: // '&' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7835 | if (Args.size() == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7836 | // C++ [over.match.oper]p3: |
| 7837 | // -- For the operator ',', the unary operator '&', or the |
| 7838 | // operator '->', the built-in candidates set is empty. |
| 7839 | break; |
| 7840 | |
| 7841 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
| 7842 | break; |
| 7843 | |
| 7844 | case OO_Tilde: |
| 7845 | OpBuilder.addUnaryTildePromotedIntegralOverloads(); |
| 7846 | break; |
| 7847 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7848 | case OO_Equal: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7849 | OpBuilder.addAssignmentMemberPointerOrEnumeralOverloads(); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7850 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7851 | |
| 7852 | case OO_PlusEqual: |
| 7853 | case OO_MinusEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7854 | OpBuilder.addAssignmentPointerOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7855 | // Fall through. |
| 7856 | |
| 7857 | case OO_StarEqual: |
| 7858 | case OO_SlashEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7859 | OpBuilder.addAssignmentArithmeticOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7860 | break; |
| 7861 | |
| 7862 | case OO_PercentEqual: |
| 7863 | case OO_LessLessEqual: |
| 7864 | case OO_GreaterGreaterEqual: |
| 7865 | case OO_AmpEqual: |
| 7866 | case OO_CaretEqual: |
| 7867 | case OO_PipeEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7868 | OpBuilder.addAssignmentIntegralOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7869 | break; |
| 7870 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7871 | case OO_Exclaim: |
| 7872 | OpBuilder.addExclaimOverload(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7873 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7874 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7875 | case OO_AmpAmp: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7876 | case OO_PipePipe: |
| 7877 | OpBuilder.addAmpAmpOrPipePipeOverload(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7878 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7879 | |
| 7880 | case OO_Subscript: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7881 | OpBuilder.addSubscriptOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7882 | break; |
| 7883 | |
| 7884 | case OO_ArrowStar: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7885 | OpBuilder.addArrowStarOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7886 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 7887 | |
| 7888 | case OO_Conditional: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7889 | OpBuilder.addConditionalOperatorOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7890 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7891 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7892 | } |
| 7893 | } |
| 7894 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7895 | /// \brief Add function candidates found via argument-dependent lookup |
| 7896 | /// to the set of overloading candidates. |
| 7897 | /// |
| 7898 | /// This routine performs argument-dependent name lookup based on the |
| 7899 | /// given function name (which may also be an operator name) and adds |
| 7900 | /// all of the overload candidates found by ADL to the overload |
| 7901 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7902 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7903 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
Richard Smith | f5cd5cc | 2012-02-25 06:24:24 +0000 | [diff] [blame] | 7904 | bool Operator, SourceLocation Loc, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 7905 | ArrayRef<Expr *> Args, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 7906 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7907 | OverloadCandidateSet& CandidateSet, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 7908 | bool PartialOverloading) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7909 | ADLResult Fns; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7910 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 7911 | // FIXME: This approach for uniquing ADL results (and removing |
| 7912 | // redundant candidates from the set) relies on pointer-equality, |
| 7913 | // which means we need to key off the canonical decl. However, |
| 7914 | // always going back to the canonical decl might not get us the |
| 7915 | // right set of default arguments. What default arguments are |
| 7916 | // we supposed to consider on ADL candidates, anyway? |
| 7917 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7918 | // FIXME: Pass in the explicit template arguments? |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 7919 | ArgumentDependentLookup(Name, Operator, Loc, Args, Fns); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7920 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7921 | // Erase all of the candidates we already knew about. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7922 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 7923 | CandEnd = CandidateSet.end(); |
| 7924 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7925 | if (Cand->Function) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7926 | Fns.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7927 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7928 | Fns.erase(FunTmpl); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7929 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7930 | |
| 7931 | // For each of the ADL candidates we found, add it to the overload |
| 7932 | // set. |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7933 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7934 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7935 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7936 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7937 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7938 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7939 | AddOverloadCandidate(FD, FoundDecl, Args, CandidateSet, false, |
| 7940 | PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7941 | } else |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7942 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7943 | FoundDecl, ExplicitTemplateArgs, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7944 | Args, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7945 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7946 | } |
| 7947 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7948 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 7949 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7950 | bool |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7951 | isBetterOverloadCandidate(Sema &S, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 7952 | const OverloadCandidate &Cand1, |
| 7953 | const OverloadCandidate &Cand2, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7954 | SourceLocation Loc, |
| 7955 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7956 | // Define viable functions to be better candidates than non-viable |
| 7957 | // functions. |
| 7958 | if (!Cand2.Viable) |
| 7959 | return Cand1.Viable; |
| 7960 | else if (!Cand1.Viable) |
| 7961 | return false; |
| 7962 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7963 | // C++ [over.match.best]p1: |
| 7964 | // |
| 7965 | // -- if F is a static member function, ICS1(F) is defined such |
| 7966 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 7967 | // any function G, and, symmetrically, ICS1(G) is neither |
| 7968 | // better nor worse than ICS1(F). |
| 7969 | unsigned StartArg = 0; |
| 7970 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 7971 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7972 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7973 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7974 | // A viable function F1 is defined to be a better function than another |
| 7975 | // 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] | 7976 | // conversion sequence than ICSi(F2), and then... |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 7977 | unsigned NumArgs = Cand1.NumConversions; |
| 7978 | assert(Cand2.NumConversions == NumArgs && "Overload candidate mismatch"); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7979 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7980 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7981 | switch (CompareImplicitConversionSequences(S, |
| 7982 | Cand1.Conversions[ArgIdx], |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7983 | Cand2.Conversions[ArgIdx])) { |
| 7984 | case ImplicitConversionSequence::Better: |
| 7985 | // Cand1 has a better conversion sequence. |
| 7986 | HasBetterConversion = true; |
| 7987 | break; |
| 7988 | |
| 7989 | case ImplicitConversionSequence::Worse: |
| 7990 | // Cand1 can't be better than Cand2. |
| 7991 | return false; |
| 7992 | |
| 7993 | case ImplicitConversionSequence::Indistinguishable: |
| 7994 | // Do nothing. |
| 7995 | break; |
| 7996 | } |
| 7997 | } |
| 7998 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7999 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 8000 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8001 | if (HasBetterConversion) |
| 8002 | return true; |
| 8003 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8004 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 8005 | // specialization, or, if not that, |
Douglas Gregor | ccd4713 | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 8006 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 8007 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 8008 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8009 | |
| 8010 | // -- F1 and F2 are function template specializations, and the function |
| 8011 | // template for F1 is more specialized than the template for F2 |
| 8012 | // 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] | 8013 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 8014 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 8015 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 8016 | if (FunctionTemplateDecl *BetterTemplate |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8017 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 8018 | Cand2.Function->getPrimaryTemplate(), |
| 8019 | Loc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8020 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 8021 | : TPOC_Call, |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 8022 | Cand1.ExplicitCallArguments)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 8023 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 8024 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8025 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 8026 | // -- the context is an initialization by user-defined conversion |
| 8027 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 8028 | // from the return type of F1 to the destination type (i.e., |
| 8029 | // the type of the entity being initialized) is a better |
| 8030 | // conversion sequence than the standard conversion sequence |
| 8031 | // from the return type of F2 to the destination type. |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8032 | if (UserDefinedConversion && Cand1.Function && Cand2.Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8033 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 8034 | isa<CXXConversionDecl>(Cand2.Function)) { |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 8035 | // First check whether we prefer one of the conversion functions over the |
| 8036 | // other. This only distinguishes the results in non-standard, extension |
| 8037 | // cases such as the conversion from a lambda closure type to a function |
| 8038 | // pointer or block. |
| 8039 | ImplicitConversionSequence::CompareKind FuncResult |
| 8040 | = compareConversionFunctions(S, Cand1.Function, Cand2.Function); |
| 8041 | if (FuncResult != ImplicitConversionSequence::Indistinguishable) |
| 8042 | return FuncResult; |
| 8043 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8044 | switch (CompareStandardConversionSequences(S, |
| 8045 | Cand1.FinalConversion, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 8046 | Cand2.FinalConversion)) { |
| 8047 | case ImplicitConversionSequence::Better: |
| 8048 | // Cand1 has a better conversion sequence. |
| 8049 | return true; |
| 8050 | |
| 8051 | case ImplicitConversionSequence::Worse: |
| 8052 | // Cand1 can't be better than Cand2. |
| 8053 | return false; |
| 8054 | |
| 8055 | case ImplicitConversionSequence::Indistinguishable: |
| 8056 | // Do nothing |
| 8057 | break; |
| 8058 | } |
| 8059 | } |
| 8060 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8061 | return false; |
| 8062 | } |
| 8063 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8064 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8065 | /// within an overload candidate set. |
| 8066 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 8067 | /// \param Loc The location of the function name (or operator symbol) for |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8068 | /// which overload resolution occurs. |
| 8069 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 8070 | /// \param Best If overload resolution was successful or found a deleted |
| 8071 | /// function, \p Best points to the candidate function found. |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8072 | /// |
| 8073 | /// \returns The result of overload resolution. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8074 | OverloadingResult |
| 8075 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 8076 | iterator &Best, |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8077 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8078 | // Find the best viable function. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8079 | Best = end(); |
| 8080 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
| 8081 | if (Cand->Viable) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8082 | if (Best == end() || isBetterOverloadCandidate(S, *Cand, *Best, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8083 | UserDefinedConversion)) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8084 | Best = Cand; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8085 | } |
| 8086 | |
| 8087 | // If we didn't find any viable functions, abort. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8088 | if (Best == end()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8089 | return OR_No_Viable_Function; |
| 8090 | |
| 8091 | // Make sure that this function is better than every other viable |
| 8092 | // function. If not, we have an ambiguity. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8093 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8094 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8095 | Cand != Best && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8096 | !isBetterOverloadCandidate(S, *Best, *Cand, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8097 | UserDefinedConversion)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8098 | Best = end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8099 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8100 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8101 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8102 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8103 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8104 | if (Best->Function && |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 8105 | (Best->Function->isDeleted() || |
| 8106 | S.isFunctionConsideredUnavailable(Best->Function))) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8107 | return OR_Deleted; |
| 8108 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8109 | return OR_Success; |
| 8110 | } |
| 8111 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8112 | namespace { |
| 8113 | |
| 8114 | enum OverloadCandidateKind { |
| 8115 | oc_function, |
| 8116 | oc_method, |
| 8117 | oc_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8118 | oc_function_template, |
| 8119 | oc_method_template, |
| 8120 | oc_constructor_template, |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8121 | oc_implicit_default_constructor, |
| 8122 | oc_implicit_copy_constructor, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8123 | oc_implicit_move_constructor, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8124 | oc_implicit_copy_assignment, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8125 | oc_implicit_move_assignment, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8126 | oc_implicit_inherited_constructor |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8127 | }; |
| 8128 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8129 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 8130 | FunctionDecl *Fn, |
| 8131 | std::string &Description) { |
| 8132 | bool isTemplate = false; |
| 8133 | |
| 8134 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 8135 | isTemplate = true; |
| 8136 | Description = S.getTemplateArgumentBindingsText( |
| 8137 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 8138 | } |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8139 | |
| 8140 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8141 | if (!Ctor->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8142 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8143 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8144 | if (Ctor->getInheritedConstructor()) |
| 8145 | return oc_implicit_inherited_constructor; |
| 8146 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8147 | if (Ctor->isDefaultConstructor()) |
| 8148 | return oc_implicit_default_constructor; |
| 8149 | |
| 8150 | if (Ctor->isMoveConstructor()) |
| 8151 | return oc_implicit_move_constructor; |
| 8152 | |
| 8153 | assert(Ctor->isCopyConstructor() && |
| 8154 | "unexpected sort of implicit constructor"); |
| 8155 | return oc_implicit_copy_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8156 | } |
| 8157 | |
| 8158 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 8159 | // This actually gets spelled 'candidate function' for now, but |
| 8160 | // it doesn't hurt to split it out. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8161 | if (!Meth->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8162 | return isTemplate ? oc_method_template : oc_method; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8163 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8164 | if (Meth->isMoveAssignmentOperator()) |
| 8165 | return oc_implicit_move_assignment; |
| 8166 | |
Douglas Gregor | ef7d78b | 2012-02-10 08:36:38 +0000 | [diff] [blame] | 8167 | if (Meth->isCopyAssignmentOperator()) |
| 8168 | return oc_implicit_copy_assignment; |
| 8169 | |
| 8170 | assert(isa<CXXConversionDecl>(Meth) && "expected conversion"); |
| 8171 | return oc_method; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8172 | } |
| 8173 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8174 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8175 | } |
| 8176 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8177 | void MaybeEmitInheritedConstructorNote(Sema &S, FunctionDecl *Fn) { |
| 8178 | const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn); |
| 8179 | if (!Ctor) return; |
| 8180 | |
| 8181 | Ctor = Ctor->getInheritedConstructor(); |
| 8182 | if (!Ctor) return; |
| 8183 | |
| 8184 | S.Diag(Ctor->getLocation(), diag::note_ovl_candidate_inherited_constructor); |
| 8185 | } |
| 8186 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8187 | } // end anonymous namespace |
| 8188 | |
| 8189 | // Notes the location of an overload candidate. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8190 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn, QualType DestType) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8191 | std::string FnDesc; |
| 8192 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8193 | PartialDiagnostic PD = PDiag(diag::note_ovl_candidate) |
| 8194 | << (unsigned) K << FnDesc; |
| 8195 | HandleFunctionTypeMismatch(PD, Fn->getType(), DestType); |
| 8196 | Diag(Fn->getLocation(), PD); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8197 | MaybeEmitInheritedConstructorNote(*this, Fn); |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8198 | } |
| 8199 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8200 | //Notes the location of all overload candidates designated through |
| 8201 | // OverloadedExpr |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8202 | void Sema::NoteAllOverloadCandidates(Expr* OverloadedExpr, QualType DestType) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8203 | assert(OverloadedExpr->getType() == Context.OverloadTy); |
| 8204 | |
| 8205 | OverloadExpr::FindResult Ovl = OverloadExpr::find(OverloadedExpr); |
| 8206 | OverloadExpr *OvlExpr = Ovl.Expression; |
| 8207 | |
| 8208 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 8209 | IEnd = OvlExpr->decls_end(); |
| 8210 | I != IEnd; ++I) { |
| 8211 | if (FunctionTemplateDecl *FunTmpl = |
| 8212 | dyn_cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8213 | NoteOverloadCandidate(FunTmpl->getTemplatedDecl(), DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8214 | } else if (FunctionDecl *Fun |
| 8215 | = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8216 | NoteOverloadCandidate(Fun, DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8217 | } |
| 8218 | } |
| 8219 | } |
| 8220 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8221 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 8222 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 8223 | /// target types of the conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8224 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
| 8225 | Sema &S, |
| 8226 | SourceLocation CaretLoc, |
| 8227 | const PartialDiagnostic &PDiag) const { |
| 8228 | S.Diag(CaretLoc, PDiag) |
| 8229 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
Matt Beaumont-Gay | 45a37da | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 8230 | // FIXME: The note limiting machinery is borrowed from |
| 8231 | // OverloadCandidateSet::NoteCandidates; there's an opportunity for |
| 8232 | // refactoring here. |
| 8233 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
| 8234 | unsigned CandsShown = 0; |
| 8235 | AmbiguousConversionSequence::const_iterator I, E; |
| 8236 | for (I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
| 8237 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
| 8238 | break; |
| 8239 | ++CandsShown; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8240 | S.NoteOverloadCandidate(*I); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8241 | } |
Matt Beaumont-Gay | 45a37da | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 8242 | if (I != E) |
| 8243 | S.Diag(SourceLocation(), diag::note_ovl_too_many_candidates) << int(E - I); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8244 | } |
| 8245 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8246 | namespace { |
| 8247 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8248 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 8249 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 8250 | assert(Conv.isBad()); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8251 | assert(Cand->Function && "for now, candidate must be a function"); |
| 8252 | FunctionDecl *Fn = Cand->Function; |
| 8253 | |
| 8254 | // There's a conversion slot for the object argument if this is a |
| 8255 | // non-constructor method. Note that 'I' corresponds the |
| 8256 | // conversion-slot index. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8257 | bool isObjectArgument = false; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8258 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8259 | if (I == 0) |
| 8260 | isObjectArgument = true; |
| 8261 | else |
| 8262 | I--; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8263 | } |
| 8264 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8265 | std::string FnDesc; |
| 8266 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 8267 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8268 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 8269 | QualType FromTy = Conv.Bad.getFromType(); |
| 8270 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8271 | |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8272 | if (FromTy == S.Context.OverloadTy) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8273 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8274 | Expr *E = FromExpr->IgnoreParens(); |
| 8275 | if (isa<UnaryOperator>(E)) |
| 8276 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 8277 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8278 | |
| 8279 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 8280 | << (unsigned) FnKind << FnDesc |
| 8281 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8282 | << ToTy << Name << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8283 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8284 | return; |
| 8285 | } |
| 8286 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8287 | // Do some hand-waving analysis to see if the non-viability is due |
| 8288 | // to a qualifier mismatch. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8289 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 8290 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 8291 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 8292 | CToTy = RT->getPointeeType(); |
| 8293 | else { |
| 8294 | // TODO: detect and diagnose the full richness of const mismatches. |
| 8295 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 8296 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 8297 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 8298 | } |
| 8299 | |
| 8300 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 8301 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8302 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8303 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8304 | |
| 8305 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 8306 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 8307 | << (unsigned) FnKind << FnDesc |
| 8308 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8309 | << FromTy |
| 8310 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 8311 | << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8312 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8313 | return; |
| 8314 | } |
| 8315 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8316 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 8317 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8318 | << (unsigned) FnKind << FnDesc |
| 8319 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8320 | << FromTy |
| 8321 | << FromQs.getObjCLifetime() << ToQs.getObjCLifetime() |
| 8322 | << (unsigned) isObjectArgument << I+1; |
| 8323 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8324 | return; |
| 8325 | } |
| 8326 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 8327 | if (FromQs.getObjCGCAttr() != ToQs.getObjCGCAttr()) { |
| 8328 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_gc) |
| 8329 | << (unsigned) FnKind << FnDesc |
| 8330 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8331 | << FromTy |
| 8332 | << FromQs.getObjCGCAttr() << ToQs.getObjCGCAttr() |
| 8333 | << (unsigned) isObjectArgument << I+1; |
| 8334 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8335 | return; |
| 8336 | } |
| 8337 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8338 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 8339 | assert(CVR && "unexpected qualifiers mismatch"); |
| 8340 | |
| 8341 | if (isObjectArgument) { |
| 8342 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 8343 | << (unsigned) FnKind << FnDesc |
| 8344 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8345 | << FromTy << (CVR - 1); |
| 8346 | } else { |
| 8347 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 8348 | << (unsigned) FnKind << FnDesc |
| 8349 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8350 | << FromTy << (CVR - 1) << I+1; |
| 8351 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8352 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8353 | return; |
| 8354 | } |
| 8355 | |
Sebastian Redl | fd2a00a | 2011-09-24 17:48:32 +0000 | [diff] [blame] | 8356 | // Special diagnostic for failure to convert an initializer list, since |
| 8357 | // telling the user that it has type void is not useful. |
| 8358 | if (FromExpr && isa<InitListExpr>(FromExpr)) { |
| 8359 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_list_argument) |
| 8360 | << (unsigned) FnKind << FnDesc |
| 8361 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8362 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8363 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8364 | return; |
| 8365 | } |
| 8366 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8367 | // Diagnose references or pointers to incomplete types differently, |
| 8368 | // since it's far from impossible that the incompleteness triggered |
| 8369 | // the failure. |
| 8370 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 8371 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 8372 | TempFromTy = PTy->getPointeeType(); |
| 8373 | if (TempFromTy->isIncompleteType()) { |
| 8374 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 8375 | << (unsigned) FnKind << FnDesc |
| 8376 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8377 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8378 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8379 | return; |
| 8380 | } |
| 8381 | |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8382 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8383 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8384 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 8385 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 8386 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8387 | FromPtrTy->getPointeeType()) && |
| 8388 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 8389 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8390 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8391 | FromPtrTy->getPointeeType())) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8392 | BaseToDerivedConversion = 1; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8393 | } |
| 8394 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 8395 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 8396 | if (const ObjCObjectPointerType *ToPtrTy |
| 8397 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 8398 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 8399 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 8400 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8401 | FromPtrTy->getPointeeType()) && |
| 8402 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8403 | BaseToDerivedConversion = 2; |
| 8404 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8405 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 8406 | !FromTy->isIncompleteType() && |
| 8407 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 8408 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) { |
| 8409 | BaseToDerivedConversion = 3; |
| 8410 | } else if (ToTy->isLValueReferenceType() && !FromExpr->isLValue() && |
| 8411 | ToTy.getNonReferenceType().getCanonicalType() == |
| 8412 | FromTy.getNonReferenceType().getCanonicalType()) { |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8413 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_lvalue) |
| 8414 | << (unsigned) FnKind << FnDesc |
| 8415 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8416 | << (unsigned) isObjectArgument << I + 1; |
| 8417 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8418 | return; |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8419 | } |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8420 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8421 | |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8422 | if (BaseToDerivedConversion) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8423 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8424 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8425 | << (unsigned) FnKind << FnDesc |
| 8426 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8427 | << (BaseToDerivedConversion - 1) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8428 | << FromTy << ToTy << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8429 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8430 | return; |
| 8431 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8432 | |
Fariborz Jahanian | 909bcb3 | 2011-07-20 17:14:09 +0000 | [diff] [blame] | 8433 | if (isa<ObjCObjectPointerType>(CFromTy) && |
| 8434 | isa<PointerType>(CToTy)) { |
| 8435 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8436 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8437 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
| 8438 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_arc_conv) |
| 8439 | << (unsigned) FnKind << FnDesc |
| 8440 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8441 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8442 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8443 | return; |
| 8444 | } |
| 8445 | } |
| 8446 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8447 | // Emit the generic diagnostic and, optionally, add the hints to it. |
| 8448 | PartialDiagnostic FDiag = S.PDiag(diag::note_ovl_candidate_bad_conv); |
| 8449 | FDiag << (unsigned) FnKind << FnDesc |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8450 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8451 | << FromTy << ToTy << (unsigned) isObjectArgument << I + 1 |
| 8452 | << (unsigned) (Cand->Fix.Kind); |
| 8453 | |
| 8454 | // If we can fix the conversion, suggest the FixIts. |
Benjamin Kramer | 1136ef0 | 2012-01-14 21:05:10 +0000 | [diff] [blame] | 8455 | for (std::vector<FixItHint>::iterator HI = Cand->Fix.Hints.begin(), |
| 8456 | HE = Cand->Fix.Hints.end(); HI != HE; ++HI) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8457 | FDiag << *HI; |
| 8458 | S.Diag(Fn->getLocation(), FDiag); |
| 8459 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8460 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8461 | } |
| 8462 | |
| 8463 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 8464 | unsigned NumFormalArgs) { |
| 8465 | // TODO: treat calls to a missing default constructor as a special case |
| 8466 | |
| 8467 | FunctionDecl *Fn = Cand->Function; |
| 8468 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 8469 | |
| 8470 | unsigned MinParams = Fn->getMinRequiredArguments(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8471 | |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8472 | // With invalid overloaded operators, it's possible that we think we |
| 8473 | // have an arity mismatch when it fact it looks like we have the |
| 8474 | // right number of arguments, because only overloaded operators have |
| 8475 | // the weird behavior of overloading member and non-member functions. |
| 8476 | // Just don't report anything. |
| 8477 | if (Fn->isInvalidDecl() && |
| 8478 | Fn->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 8479 | return; |
| 8480 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8481 | // at least / at most / exactly |
| 8482 | unsigned mode, modeCount; |
| 8483 | if (NumFormalArgs < MinParams) { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8484 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 8485 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8486 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8487 | if (MinParams != FnTy->getNumArgs() || |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 8488 | FnTy->isVariadic() || FnTy->isTemplateVariadic()) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8489 | mode = 0; // "at least" |
| 8490 | else |
| 8491 | mode = 2; // "exactly" |
| 8492 | modeCount = MinParams; |
| 8493 | } else { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8494 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 8495 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8496 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8497 | if (MinParams != FnTy->getNumArgs()) |
| 8498 | mode = 1; // "at most" |
| 8499 | else |
| 8500 | mode = 2; // "exactly" |
| 8501 | modeCount = FnTy->getNumArgs(); |
| 8502 | } |
| 8503 | |
| 8504 | std::string Description; |
| 8505 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 8506 | |
Richard Smith | f7b8056 | 2012-05-11 05:16:41 +0000 | [diff] [blame] | 8507 | if (modeCount == 1 && Fn->getParamDecl(0)->getDeclName()) |
| 8508 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity_one) |
| 8509 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 8510 | << Fn->getParamDecl(0) << NumFormalArgs; |
| 8511 | else |
| 8512 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
| 8513 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 8514 | << modeCount << NumFormalArgs; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8515 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8516 | } |
| 8517 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8518 | /// Diagnose a failed template-argument deduction. |
| 8519 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8520 | unsigned NumArgs) { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8521 | FunctionDecl *Fn = Cand->Function; // pattern |
| 8522 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8523 | TemplateParameter Param = Cand->DeductionFailure.getTemplateParameter(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8524 | NamedDecl *ParamD; |
| 8525 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 8526 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 8527 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8528 | switch (Cand->DeductionFailure.Result) { |
| 8529 | case Sema::TDK_Success: |
| 8530 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 8531 | |
| 8532 | case Sema::TDK_Incomplete: { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8533 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| 8534 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_incomplete_deduction) |
| 8535 | << ParamD->getDeclName(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8536 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8537 | return; |
| 8538 | } |
| 8539 | |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8540 | case Sema::TDK_Underqualified: { |
| 8541 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 8542 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 8543 | |
| 8544 | QualType Param = Cand->DeductionFailure.getFirstArg()->getAsType(); |
| 8545 | |
| 8546 | // Param will have been canonicalized, but it should just be a |
| 8547 | // qualified version of ParamD, so move the qualifiers to that. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 8548 | QualifierCollector Qs; |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8549 | Qs.strip(Param); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 8550 | QualType NonCanonParam = Qs.apply(S.Context, TParam->getTypeForDecl()); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8551 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 8552 | |
| 8553 | // Arg has also been canonicalized, but there's nothing we can do |
| 8554 | // about that. It also doesn't matter as much, because it won't |
| 8555 | // have any template parameters in it (because deduction isn't |
| 8556 | // done on dependent types). |
| 8557 | QualType Arg = Cand->DeductionFailure.getSecondArg()->getAsType(); |
| 8558 | |
| 8559 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_underqualified) |
| 8560 | << ParamD->getDeclName() << Arg << NonCanonParam; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8561 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8562 | return; |
| 8563 | } |
| 8564 | |
| 8565 | case Sema::TDK_Inconsistent: { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8566 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8567 | int which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8568 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8569 | which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8570 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8571 | which = 1; |
| 8572 | else { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8573 | which = 2; |
| 8574 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8575 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8576 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_inconsistent_deduction) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8577 | << which << ParamD->getDeclName() |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8578 | << *Cand->DeductionFailure.getFirstArg() |
| 8579 | << *Cand->DeductionFailure.getSecondArg(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8580 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8581 | return; |
| 8582 | } |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8583 | |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8584 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8585 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8586 | if (ParamD->getDeclName()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8587 | S.Diag(Fn->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8588 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
| 8589 | << ParamD->getDeclName(); |
| 8590 | else { |
| 8591 | int index = 0; |
| 8592 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 8593 | index = TTP->getIndex(); |
| 8594 | else if (NonTypeTemplateParmDecl *NTTP |
| 8595 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 8596 | index = NTTP->getIndex(); |
| 8597 | else |
| 8598 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8599 | S.Diag(Fn->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8600 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
| 8601 | << (index + 1); |
| 8602 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8603 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8604 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8605 | |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8606 | case Sema::TDK_TooManyArguments: |
| 8607 | case Sema::TDK_TooFewArguments: |
| 8608 | DiagnoseArityMismatch(S, Cand, NumArgs); |
| 8609 | return; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8610 | |
| 8611 | case Sema::TDK_InstantiationDepth: |
| 8612 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_instantiation_depth); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8613 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8614 | return; |
| 8615 | |
| 8616 | case Sema::TDK_SubstitutionFailure: { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8617 | // Format the template argument list into the argument string. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8618 | SmallString<128> TemplateArgString; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8619 | if (TemplateArgumentList *Args = |
| 8620 | Cand->DeductionFailure.getTemplateArgumentList()) { |
| 8621 | TemplateArgString = " "; |
| 8622 | TemplateArgString += S.getTemplateArgumentBindingsText( |
| 8623 | Fn->getDescribedFunctionTemplate()->getTemplateParameters(), *Args); |
| 8624 | } |
| 8625 | |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8626 | // If this candidate was disabled by enable_if, say so. |
| 8627 | PartialDiagnosticAt *PDiag = Cand->DeductionFailure.getSFINAEDiagnostic(); |
| 8628 | if (PDiag && PDiag->second.getDiagID() == |
| 8629 | diag::err_typename_nested_not_found_enable_if) { |
| 8630 | // FIXME: Use the source range of the condition, and the fully-qualified |
| 8631 | // name of the enable_if template. These are both present in PDiag. |
| 8632 | S.Diag(PDiag->first, diag::note_ovl_candidate_disabled_by_enable_if) |
| 8633 | << "'enable_if'" << TemplateArgString; |
| 8634 | return; |
| 8635 | } |
| 8636 | |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8637 | // Format the SFINAE diagnostic into the argument string. |
| 8638 | // FIXME: Add a general mechanism to include a PartialDiagnostic *'s |
| 8639 | // formatted message in another diagnostic. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8640 | SmallString<128> SFINAEArgString; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8641 | SourceRange R; |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8642 | if (PDiag) { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8643 | SFINAEArgString = ": "; |
| 8644 | R = SourceRange(PDiag->first, PDiag->first); |
| 8645 | PDiag->second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 8646 | } |
| 8647 | |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8648 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_substitution_failure) |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8649 | << TemplateArgString << SFINAEArgString << R; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8650 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8651 | return; |
| 8652 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8653 | |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8654 | case Sema::TDK_FailedOverloadResolution: { |
| 8655 | OverloadExpr::FindResult R = |
| 8656 | OverloadExpr::find(Cand->DeductionFailure.getExpr()); |
| 8657 | S.Diag(Fn->getLocation(), |
| 8658 | diag::note_ovl_candidate_failed_overload_resolution) |
| 8659 | << R.Expression->getName(); |
| 8660 | return; |
| 8661 | } |
| 8662 | |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8663 | case Sema::TDK_NonDeducedMismatch: { |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8664 | // FIXME: Provide a source location to indicate what we couldn't match. |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8665 | TemplateArgument FirstTA = *Cand->DeductionFailure.getFirstArg(); |
| 8666 | TemplateArgument SecondTA = *Cand->DeductionFailure.getSecondArg(); |
| 8667 | if (FirstTA.getKind() == TemplateArgument::Template && |
| 8668 | SecondTA.getKind() == TemplateArgument::Template) { |
| 8669 | TemplateName FirstTN = FirstTA.getAsTemplate(); |
| 8670 | TemplateName SecondTN = SecondTA.getAsTemplate(); |
| 8671 | if (FirstTN.getKind() == TemplateName::Template && |
| 8672 | SecondTN.getKind() == TemplateName::Template) { |
| 8673 | if (FirstTN.getAsTemplateDecl()->getName() == |
| 8674 | SecondTN.getAsTemplateDecl()->getName()) { |
| 8675 | // FIXME: This fixes a bad diagnostic where both templates are named |
| 8676 | // the same. This particular case is a bit difficult since: |
| 8677 | // 1) It is passed as a string to the diagnostic printer. |
| 8678 | // 2) The diagnostic printer only attempts to find a better |
| 8679 | // name for types, not decls. |
| 8680 | // Ideally, this should folded into the diagnostic printer. |
| 8681 | S.Diag(Fn->getLocation(), |
| 8682 | diag::note_ovl_candidate_non_deduced_mismatch_qualified) |
| 8683 | << FirstTN.getAsTemplateDecl() << SecondTN.getAsTemplateDecl(); |
| 8684 | return; |
| 8685 | } |
| 8686 | } |
| 8687 | } |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8688 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_non_deduced_mismatch) |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8689 | << FirstTA << SecondTA; |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8690 | return; |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8691 | } |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8692 | // TODO: diagnose these individually, then kill off |
| 8693 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8694 | case Sema::TDK_MiscellaneousDeductionFailure: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8695 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8696 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8697 | return; |
| 8698 | } |
| 8699 | } |
| 8700 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8701 | /// CUDA: diagnose an invalid call across targets. |
| 8702 | void DiagnoseBadTarget(Sema &S, OverloadCandidate *Cand) { |
| 8703 | FunctionDecl *Caller = cast<FunctionDecl>(S.CurContext); |
| 8704 | FunctionDecl *Callee = Cand->Function; |
| 8705 | |
| 8706 | Sema::CUDAFunctionTarget CallerTarget = S.IdentifyCUDATarget(Caller), |
| 8707 | CalleeTarget = S.IdentifyCUDATarget(Callee); |
| 8708 | |
| 8709 | std::string FnDesc; |
| 8710 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Callee, FnDesc); |
| 8711 | |
| 8712 | S.Diag(Callee->getLocation(), diag::note_ovl_candidate_bad_target) |
| 8713 | << (unsigned) FnKind << CalleeTarget << CallerTarget; |
| 8714 | } |
| 8715 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8716 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 8717 | /// already generated a primary error at the call site. |
| 8718 | /// |
| 8719 | /// It really does need to be a single diagnostic with its caret |
| 8720 | /// pointed at the candidate declaration. Yes, this creates some |
| 8721 | /// major challenges of technical writing. Yes, this makes pointing |
| 8722 | /// out problems with specific arguments quite awkward. It's still |
| 8723 | /// better than generating twenty screens of text for every failed |
| 8724 | /// overload. |
| 8725 | /// |
| 8726 | /// It would be great to be able to express per-candidate problems |
| 8727 | /// more richly for those diagnostic clients that cared, but we'd |
| 8728 | /// still have to be just as careful with the default diagnostics. |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8729 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8730 | unsigned NumArgs) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8731 | FunctionDecl *Fn = Cand->Function; |
| 8732 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8733 | // Note deleted candidates, but only if they're viable. |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 8734 | if (Cand->Viable && (Fn->isDeleted() || |
| 8735 | S.isFunctionConsideredUnavailable(Fn))) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8736 | std::string FnDesc; |
| 8737 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8738 | |
| 8739 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 8740 | << FnKind << FnDesc |
| 8741 | << (Fn->isDeleted() ? (Fn->isDeletedAsWritten() ? 1 : 2) : 0); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8742 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8743 | return; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8744 | } |
| 8745 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8746 | // We don't really have anything else to say about viable candidates. |
| 8747 | if (Cand->Viable) { |
| 8748 | S.NoteOverloadCandidate(Fn); |
| 8749 | return; |
| 8750 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8751 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8752 | switch (Cand->FailureKind) { |
| 8753 | case ovl_fail_too_many_arguments: |
| 8754 | case ovl_fail_too_few_arguments: |
| 8755 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8756 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8757 | case ovl_fail_bad_deduction: |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8758 | return DiagnoseBadDeduction(S, Cand, NumArgs); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8759 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8760 | case ovl_fail_trivial_conversion: |
| 8761 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 8762 | case ovl_fail_final_conversion_not_exact: |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8763 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8764 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8765 | case ovl_fail_bad_conversion: { |
| 8766 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8767 | for (unsigned N = Cand->NumConversions; I != N; ++I) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8768 | if (Cand->Conversions[I].isBad()) |
| 8769 | return DiagnoseBadConversion(S, Cand, I); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8770 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8771 | // FIXME: this currently happens when we're called from SemaInit |
| 8772 | // when user-conversion overload fails. Figure out how to handle |
| 8773 | // those conditions and diagnose them well. |
| 8774 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8775 | } |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8776 | |
| 8777 | case ovl_fail_bad_target: |
| 8778 | return DiagnoseBadTarget(S, Cand); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8779 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8780 | } |
| 8781 | |
| 8782 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 8783 | // Desugar the type of the surrogate down to a function type, |
| 8784 | // retaining as many typedefs as possible while still showing |
| 8785 | // the function type (and, therefore, its parameter types). |
| 8786 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 8787 | bool isLValueReference = false; |
| 8788 | bool isRValueReference = false; |
| 8789 | bool isPointer = false; |
| 8790 | if (const LValueReferenceType *FnTypeRef = |
| 8791 | FnType->getAs<LValueReferenceType>()) { |
| 8792 | FnType = FnTypeRef->getPointeeType(); |
| 8793 | isLValueReference = true; |
| 8794 | } else if (const RValueReferenceType *FnTypeRef = |
| 8795 | FnType->getAs<RValueReferenceType>()) { |
| 8796 | FnType = FnTypeRef->getPointeeType(); |
| 8797 | isRValueReference = true; |
| 8798 | } |
| 8799 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 8800 | FnType = FnTypePtr->getPointeeType(); |
| 8801 | isPointer = true; |
| 8802 | } |
| 8803 | // Desugar down to a function type. |
| 8804 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 8805 | // Reconstruct the pointer/reference as appropriate. |
| 8806 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 8807 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 8808 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 8809 | |
| 8810 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 8811 | << FnType; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8812 | MaybeEmitInheritedConstructorNote(S, Cand->Surrogate); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8813 | } |
| 8814 | |
| 8815 | void NoteBuiltinOperatorCandidate(Sema &S, |
David Blaikie | 0bea863 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 8816 | StringRef Opc, |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8817 | SourceLocation OpLoc, |
| 8818 | OverloadCandidate *Cand) { |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8819 | assert(Cand->NumConversions <= 2 && "builtin operator is not binary"); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8820 | std::string TypeStr("operator"); |
| 8821 | TypeStr += Opc; |
| 8822 | TypeStr += "("; |
| 8823 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8824 | if (Cand->NumConversions == 1) { |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8825 | TypeStr += ")"; |
| 8826 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 8827 | } else { |
| 8828 | TypeStr += ", "; |
| 8829 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 8830 | TypeStr += ")"; |
| 8831 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 8832 | } |
| 8833 | } |
| 8834 | |
| 8835 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 8836 | OverloadCandidate *Cand) { |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8837 | unsigned NoOperands = Cand->NumConversions; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8838 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 8839 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8840 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 8841 | if (!ICS.isAmbiguous()) continue; |
| 8842 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8843 | ICS.DiagnoseAmbiguousConversion(S, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8844 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8845 | } |
| 8846 | } |
| 8847 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8848 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 8849 | if (Cand->Function) |
| 8850 | return Cand->Function->getLocation(); |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 8851 | if (Cand->IsSurrogate) |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8852 | return Cand->Surrogate->getLocation(); |
| 8853 | return SourceLocation(); |
| 8854 | } |
| 8855 | |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8856 | static unsigned |
| 8857 | RankDeductionFailure(const OverloadCandidate::DeductionFailureInfo &DFI) { |
Chandler Carruth | 78bf680 | 2011-09-10 00:51:24 +0000 | [diff] [blame] | 8858 | switch ((Sema::TemplateDeductionResult)DFI.Result) { |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8859 | case Sema::TDK_Success: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8860 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8861 | |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 8862 | case Sema::TDK_Invalid: |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8863 | case Sema::TDK_Incomplete: |
| 8864 | return 1; |
| 8865 | |
| 8866 | case Sema::TDK_Underqualified: |
| 8867 | case Sema::TDK_Inconsistent: |
| 8868 | return 2; |
| 8869 | |
| 8870 | case Sema::TDK_SubstitutionFailure: |
| 8871 | case Sema::TDK_NonDeducedMismatch: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8872 | case Sema::TDK_MiscellaneousDeductionFailure: |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8873 | return 3; |
| 8874 | |
| 8875 | case Sema::TDK_InstantiationDepth: |
| 8876 | case Sema::TDK_FailedOverloadResolution: |
| 8877 | return 4; |
| 8878 | |
| 8879 | case Sema::TDK_InvalidExplicitArguments: |
| 8880 | return 5; |
| 8881 | |
| 8882 | case Sema::TDK_TooManyArguments: |
| 8883 | case Sema::TDK_TooFewArguments: |
| 8884 | return 6; |
| 8885 | } |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8886 | llvm_unreachable("Unhandled deduction result"); |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8887 | } |
| 8888 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8889 | struct CompareOverloadCandidatesForDisplay { |
| 8890 | Sema &S; |
| 8891 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8892 | |
| 8893 | bool operator()(const OverloadCandidate *L, |
| 8894 | const OverloadCandidate *R) { |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 8895 | // Fast-path this check. |
| 8896 | if (L == R) return false; |
| 8897 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8898 | // Order first by viability. |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8899 | if (L->Viable) { |
| 8900 | if (!R->Viable) return true; |
| 8901 | |
| 8902 | // TODO: introduce a tri-valued comparison for overload |
| 8903 | // candidates. Would be more worthwhile if we had a sort |
| 8904 | // that could exploit it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8905 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation())) return true; |
| 8906 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation())) return false; |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8907 | } else if (R->Viable) |
| 8908 | return false; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8909 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8910 | assert(L->Viable == R->Viable); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8911 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8912 | // Criteria by which we can sort non-viable candidates: |
| 8913 | if (!L->Viable) { |
| 8914 | // 1. Arity mismatches come after other candidates. |
| 8915 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 8916 | L->FailureKind == ovl_fail_too_few_arguments) |
| 8917 | return false; |
| 8918 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 8919 | R->FailureKind == ovl_fail_too_few_arguments) |
| 8920 | return true; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8921 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8922 | // 2. Bad conversions come first and are ordered by the number |
| 8923 | // of bad conversions and quality of good conversions. |
| 8924 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 8925 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 8926 | return true; |
| 8927 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8928 | // The conversion that can be fixed with a smaller number of changes, |
| 8929 | // comes first. |
| 8930 | unsigned numLFixes = L->Fix.NumConversionsFixed; |
| 8931 | unsigned numRFixes = R->Fix.NumConversionsFixed; |
| 8932 | numLFixes = (numLFixes == 0) ? UINT_MAX : numLFixes; |
| 8933 | numRFixes = (numRFixes == 0) ? UINT_MAX : numRFixes; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 8934 | if (numLFixes != numRFixes) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8935 | if (numLFixes < numRFixes) |
| 8936 | return true; |
| 8937 | else |
| 8938 | return false; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 8939 | } |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8940 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8941 | // If there's any ordering between the defined conversions... |
| 8942 | // FIXME: this might not be transitive. |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8943 | assert(L->NumConversions == R->NumConversions); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8944 | |
| 8945 | int leftBetter = 0; |
John McCall | 3a81337 | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 8946 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8947 | for (unsigned E = L->NumConversions; I != E; ++I) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8948 | switch (CompareImplicitConversionSequences(S, |
| 8949 | L->Conversions[I], |
| 8950 | R->Conversions[I])) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8951 | case ImplicitConversionSequence::Better: |
| 8952 | leftBetter++; |
| 8953 | break; |
| 8954 | |
| 8955 | case ImplicitConversionSequence::Worse: |
| 8956 | leftBetter--; |
| 8957 | break; |
| 8958 | |
| 8959 | case ImplicitConversionSequence::Indistinguishable: |
| 8960 | break; |
| 8961 | } |
| 8962 | } |
| 8963 | if (leftBetter > 0) return true; |
| 8964 | if (leftBetter < 0) return false; |
| 8965 | |
| 8966 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 8967 | return false; |
| 8968 | |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8969 | if (L->FailureKind == ovl_fail_bad_deduction) { |
| 8970 | if (R->FailureKind != ovl_fail_bad_deduction) |
| 8971 | return true; |
| 8972 | |
| 8973 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 8974 | return RankDeductionFailure(L->DeductionFailure) |
Eli Friedman | ce1846e | 2011-10-14 23:10:30 +0000 | [diff] [blame] | 8975 | < RankDeductionFailure(R->DeductionFailure); |
Eli Friedman | 1c522f7 | 2011-10-14 21:52:24 +0000 | [diff] [blame] | 8976 | } else if (R->FailureKind == ovl_fail_bad_deduction) |
| 8977 | return false; |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8978 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8979 | // TODO: others? |
| 8980 | } |
| 8981 | |
| 8982 | // Sort everything else by location. |
| 8983 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 8984 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 8985 | |
| 8986 | // Put candidates without locations (e.g. builtins) at the end. |
| 8987 | if (LLoc.isInvalid()) return false; |
| 8988 | if (RLoc.isInvalid()) return true; |
| 8989 | |
| 8990 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8991 | } |
| 8992 | }; |
| 8993 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8994 | /// CompleteNonViableCandidate - Normally, overload resolution only |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8995 | /// computes up to the first. Produces the FixIt set if possible. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8996 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8997 | ArrayRef<Expr *> Args) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8998 | assert(!Cand->Viable); |
| 8999 | |
| 9000 | // Don't do anything on failures other than bad conversion. |
| 9001 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 9002 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9003 | // We only want the FixIts if all the arguments can be corrected. |
| 9004 | bool Unfixable = false; |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9005 | // Use a implicit copy initialization to check conversion fixes. |
| 9006 | Cand->Fix.setConversionChecker(TryCopyInitialization); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9007 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9008 | // Skip forward to the first bad conversion. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9009 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9010 | unsigned ConvCount = Cand->NumConversions; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9011 | while (true) { |
| 9012 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 9013 | ConvIdx++; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9014 | if (Cand->Conversions[ConvIdx - 1].isBad()) { |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9015 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx - 1, S); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9016 | break; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9017 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9018 | } |
| 9019 | |
| 9020 | if (ConvIdx == ConvCount) |
| 9021 | return; |
| 9022 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9023 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 9024 | "remaining conversion is initialized?"); |
| 9025 | |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 9026 | // FIXME: this should probably be preserved from the overload |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9027 | // operation somehow. |
| 9028 | bool SuppressUserConversions = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9029 | |
| 9030 | const FunctionProtoType* Proto; |
| 9031 | unsigned ArgIdx = ConvIdx; |
| 9032 | |
| 9033 | if (Cand->IsSurrogate) { |
| 9034 | QualType ConvType |
| 9035 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 9036 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 9037 | ConvType = ConvPtrType->getPointeeType(); |
| 9038 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 9039 | ArgIdx--; |
| 9040 | } else if (Cand->Function) { |
| 9041 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 9042 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 9043 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 9044 | ArgIdx--; |
| 9045 | } else { |
| 9046 | // Builtin binary operator with a bad first conversion. |
| 9047 | assert(ConvCount <= 3); |
| 9048 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 9049 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 9050 | = TryCopyInitialization(S, Args[ConvIdx], |
| 9051 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9052 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9053 | /*InOverloadResolution*/ true, |
| 9054 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9055 | S.getLangOpts().ObjCAutoRefCount); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9056 | return; |
| 9057 | } |
| 9058 | |
| 9059 | // Fill in the rest of the conversions. |
| 9060 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 9061 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9062 | if (ArgIdx < NumArgsInProto) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9063 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 9064 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9065 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9066 | /*InOverloadResolution=*/true, |
| 9067 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9068 | S.getLangOpts().ObjCAutoRefCount); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9069 | // Store the FixIt in the candidate if it exists. |
| 9070 | if (!Unfixable && Cand->Conversions[ConvIdx].isBad()) |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9071 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx, S); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9072 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9073 | else |
| 9074 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 9075 | } |
| 9076 | } |
| 9077 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9078 | } // end anonymous namespace |
| 9079 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9080 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 9081 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9082 | /// set. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9083 | void OverloadCandidateSet::NoteCandidates(Sema &S, |
| 9084 | OverloadCandidateDisplayKind OCD, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9085 | ArrayRef<Expr *> Args, |
David Blaikie | 0bea863 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 9086 | StringRef Opc, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9087 | SourceLocation OpLoc) { |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9088 | // Sort the candidates by viability and position. Sorting directly would |
| 9089 | // be prohibitive, so we make a set of pointers and sort those. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9090 | SmallVector<OverloadCandidate*, 32> Cands; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9091 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
| 9092 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9093 | if (Cand->Viable) |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9094 | Cands.push_back(Cand); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9095 | else if (OCD == OCD_AllCandidates) { |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9096 | CompleteNonViableCandidate(S, Cand, Args); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9097 | if (Cand->Function || Cand->IsSurrogate) |
| 9098 | Cands.push_back(Cand); |
| 9099 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 9100 | // want to list every possible builtin candidate. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9101 | } |
| 9102 | } |
| 9103 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 9104 | std::sort(Cands.begin(), Cands.end(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9105 | CompareOverloadCandidatesForDisplay(S)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9106 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9107 | bool ReportedAmbiguousConversions = false; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9108 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9109 | SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 9110 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9111 | unsigned CandsShown = 0; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9112 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 9113 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 9114 | |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9115 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 9116 | // the user with. FIXME: This limit should depend on details of the |
| 9117 | // candidate list. |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 9118 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) { |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9119 | break; |
| 9120 | } |
| 9121 | ++CandsShown; |
| 9122 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9123 | if (Cand->Function) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9124 | NoteFunctionCandidate(S, Cand, Args.size()); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9125 | else if (Cand->IsSurrogate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9126 | NoteSurrogateCandidate(S, Cand); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9127 | else { |
| 9128 | assert(Cand->Viable && |
| 9129 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9130 | // Generally we only see ambiguities including viable builtin |
| 9131 | // operators if overload resolution got screwed up by an |
| 9132 | // ambiguous user-defined conversion. |
| 9133 | // |
| 9134 | // FIXME: It's quite possible for different conversions to see |
| 9135 | // different ambiguities, though. |
| 9136 | if (!ReportedAmbiguousConversions) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9137 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9138 | ReportedAmbiguousConversions = true; |
| 9139 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9140 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9141 | // If this is a viable builtin, print it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9142 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 9143 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9144 | } |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9145 | |
| 9146 | if (I != E) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9147 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9148 | } |
| 9149 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9150 | // [PossiblyAFunctionType] --> [Return] |
| 9151 | // NonFunctionType --> NonFunctionType |
| 9152 | // R (A) --> R(A) |
| 9153 | // R (*)(A) --> R (A) |
| 9154 | // R (&)(A) --> R (A) |
| 9155 | // R (S::*)(A) --> R (A) |
| 9156 | QualType Sema::ExtractUnqualifiedFunctionType(QualType PossiblyAFunctionType) { |
| 9157 | QualType Ret = PossiblyAFunctionType; |
| 9158 | if (const PointerType *ToTypePtr = |
| 9159 | PossiblyAFunctionType->getAs<PointerType>()) |
| 9160 | Ret = ToTypePtr->getPointeeType(); |
| 9161 | else if (const ReferenceType *ToTypeRef = |
| 9162 | PossiblyAFunctionType->getAs<ReferenceType>()) |
| 9163 | Ret = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 9164 | else if (const MemberPointerType *MemTypePtr = |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9165 | PossiblyAFunctionType->getAs<MemberPointerType>()) |
| 9166 | Ret = MemTypePtr->getPointeeType(); |
| 9167 | Ret = |
| 9168 | Context.getCanonicalType(Ret).getUnqualifiedType(); |
| 9169 | return Ret; |
| 9170 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9171 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9172 | // A helper class to help with address of function resolution |
| 9173 | // - allows us to avoid passing around all those ugly parameters |
| 9174 | class AddressOfFunctionResolver |
| 9175 | { |
| 9176 | Sema& S; |
| 9177 | Expr* SourceExpr; |
| 9178 | const QualType& TargetType; |
| 9179 | QualType TargetFunctionType; // Extracted function type from target type |
| 9180 | |
| 9181 | bool Complain; |
| 9182 | //DeclAccessPair& ResultFunctionAccessPair; |
| 9183 | ASTContext& Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9184 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9185 | bool TargetTypeIsNonStaticMemberFunction; |
| 9186 | bool FoundNonTemplateFunction; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9187 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9188 | OverloadExpr::FindResult OvlExprInfo; |
| 9189 | OverloadExpr *OvlExpr; |
| 9190 | TemplateArgumentListInfo OvlExplicitTemplateArgs; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9191 | SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9192 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9193 | public: |
| 9194 | AddressOfFunctionResolver(Sema &S, Expr* SourceExpr, |
| 9195 | const QualType& TargetType, bool Complain) |
| 9196 | : S(S), SourceExpr(SourceExpr), TargetType(TargetType), |
| 9197 | Complain(Complain), Context(S.getASTContext()), |
| 9198 | TargetTypeIsNonStaticMemberFunction( |
| 9199 | !!TargetType->getAs<MemberPointerType>()), |
| 9200 | FoundNonTemplateFunction(false), |
| 9201 | OvlExprInfo(OverloadExpr::find(SourceExpr)), |
| 9202 | OvlExpr(OvlExprInfo.Expression) |
| 9203 | { |
| 9204 | ExtractUnqualifiedFunctionTypeFromTargetType(); |
| 9205 | |
| 9206 | if (!TargetFunctionType->isFunctionType()) { |
| 9207 | if (OvlExpr->hasExplicitTemplateArgs()) { |
| 9208 | DeclAccessPair dap; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9209 | if (FunctionDecl* Fn = S.ResolveSingleFunctionTemplateSpecialization( |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9210 | OvlExpr, false, &dap) ) { |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9211 | |
| 9212 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 9213 | if (!Method->isStatic()) { |
| 9214 | // If the target type is a non-function type and the function |
| 9215 | // found is a non-static member function, pretend as if that was |
| 9216 | // the target, it's the only possible type to end up with. |
| 9217 | TargetTypeIsNonStaticMemberFunction = true; |
| 9218 | |
| 9219 | // And skip adding the function if its not in the proper form. |
| 9220 | // We'll diagnose this due to an empty set of functions. |
| 9221 | if (!OvlExprInfo.HasFormOfMemberPointer) |
| 9222 | return; |
| 9223 | } |
| 9224 | } |
| 9225 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9226 | Matches.push_back(std::make_pair(dap,Fn)); |
| 9227 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 9228 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9229 | return; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 9230 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9231 | |
| 9232 | if (OvlExpr->hasExplicitTemplateArgs()) |
| 9233 | OvlExpr->getExplicitTemplateArgs().copyInto(OvlExplicitTemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9234 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9235 | if (FindAllFunctionsThatMatchTargetTypeExactly()) { |
| 9236 | // C++ [over.over]p4: |
| 9237 | // If more than one function is selected, [...] |
| 9238 | if (Matches.size() > 1) { |
| 9239 | if (FoundNonTemplateFunction) |
| 9240 | EliminateAllTemplateMatches(); |
| 9241 | else |
| 9242 | EliminateAllExceptMostSpecializedTemplate(); |
| 9243 | } |
| 9244 | } |
| 9245 | } |
| 9246 | |
| 9247 | private: |
| 9248 | bool isTargetTypeAFunction() const { |
| 9249 | return TargetFunctionType->isFunctionType(); |
| 9250 | } |
| 9251 | |
| 9252 | // [ToType] [Return] |
| 9253 | |
| 9254 | // R (*)(A) --> R (A), IsNonStaticMemberFunction = false |
| 9255 | // R (&)(A) --> R (A), IsNonStaticMemberFunction = false |
| 9256 | // R (S::*)(A) --> R (A), IsNonStaticMemberFunction = true |
| 9257 | void inline ExtractUnqualifiedFunctionTypeFromTargetType() { |
| 9258 | TargetFunctionType = S.ExtractUnqualifiedFunctionType(TargetType); |
| 9259 | } |
| 9260 | |
| 9261 | // return true if any matching specializations were found |
| 9262 | bool AddMatchingTemplateFunction(FunctionTemplateDecl* FunctionTemplate, |
| 9263 | const DeclAccessPair& CurAccessFunPair) { |
| 9264 | if (CXXMethodDecl *Method |
| 9265 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
| 9266 | // Skip non-static function templates when converting to pointer, and |
| 9267 | // static when converting to member pointer. |
| 9268 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 9269 | return false; |
| 9270 | } |
| 9271 | else if (TargetTypeIsNonStaticMemberFunction) |
| 9272 | return false; |
| 9273 | |
| 9274 | // C++ [over.over]p2: |
| 9275 | // If the name is a function template, template argument deduction is |
| 9276 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 9277 | // resulting template argument list is used to generate a single |
| 9278 | // function template specialization, which is added to the set of |
| 9279 | // overloaded functions considered. |
| 9280 | FunctionDecl *Specialization = 0; |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 9281 | TemplateDeductionInfo Info(OvlExpr->getNameLoc()); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9282 | if (Sema::TemplateDeductionResult Result |
| 9283 | = S.DeduceTemplateArguments(FunctionTemplate, |
| 9284 | &OvlExplicitTemplateArgs, |
| 9285 | TargetFunctionType, Specialization, |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9286 | Info, /*InOverloadResolution=*/true)) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9287 | // FIXME: make a note of the failed deduction for diagnostics. |
| 9288 | (void)Result; |
| 9289 | return false; |
| 9290 | } |
| 9291 | |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9292 | // Template argument deduction ensures that we have an exact match or |
| 9293 | // compatible pointer-to-function arguments that would be adjusted by ICS. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9294 | // This function template specicalization works. |
| 9295 | Specialization = cast<FunctionDecl>(Specialization->getCanonicalDecl()); |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9296 | assert(S.isSameOrCompatibleFunctionType( |
| 9297 | Context.getCanonicalType(Specialization->getType()), |
| 9298 | Context.getCanonicalType(TargetFunctionType))); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9299 | Matches.push_back(std::make_pair(CurAccessFunPair, Specialization)); |
| 9300 | return true; |
| 9301 | } |
| 9302 | |
| 9303 | bool AddMatchingNonTemplateFunction(NamedDecl* Fn, |
| 9304 | const DeclAccessPair& CurAccessFunPair) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9305 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 9306 | // Skip non-static functions when converting to pointer, and static |
| 9307 | // when converting to member pointer. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9308 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 9309 | return false; |
| 9310 | } |
| 9311 | else if (TargetTypeIsNonStaticMemberFunction) |
| 9312 | return false; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9313 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9314 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9315 | if (S.getLangOpts().CUDA) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 9316 | if (FunctionDecl *Caller = dyn_cast<FunctionDecl>(S.CurContext)) |
| 9317 | if (S.CheckCUDATarget(Caller, FunDecl)) |
| 9318 | return false; |
| 9319 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9320 | // If any candidate has a placeholder return type, trigger its deduction |
| 9321 | // now. |
| 9322 | if (S.getLangOpts().CPlusPlus1y && |
| 9323 | FunDecl->getResultType()->isUndeducedType() && |
| 9324 | S.DeduceReturnType(FunDecl, SourceExpr->getLocStart(), Complain)) |
| 9325 | return false; |
| 9326 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 9327 | QualType ResultTy; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9328 | if (Context.hasSameUnqualifiedType(TargetFunctionType, |
| 9329 | FunDecl->getType()) || |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 9330 | S.IsNoReturnConversion(FunDecl->getType(), TargetFunctionType, |
| 9331 | ResultTy)) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9332 | Matches.push_back(std::make_pair(CurAccessFunPair, |
| 9333 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 9334 | FoundNonTemplateFunction = true; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9335 | return true; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 9336 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9337 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9338 | |
| 9339 | return false; |
| 9340 | } |
| 9341 | |
| 9342 | bool FindAllFunctionsThatMatchTargetTypeExactly() { |
| 9343 | bool Ret = false; |
| 9344 | |
| 9345 | // If the overload expression doesn't have the form of a pointer to |
| 9346 | // member, don't try to convert it to a pointer-to-member type. |
| 9347 | if (IsInvalidFormOfPointerToMemberFunction()) |
| 9348 | return false; |
| 9349 | |
| 9350 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 9351 | E = OvlExpr->decls_end(); |
| 9352 | I != E; ++I) { |
| 9353 | // Look through any using declarations to find the underlying function. |
| 9354 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 9355 | |
| 9356 | // C++ [over.over]p3: |
| 9357 | // Non-member functions and static member functions match |
| 9358 | // targets of type "pointer-to-function" or "reference-to-function." |
| 9359 | // Nonstatic member functions match targets of |
| 9360 | // type "pointer-to-member-function." |
| 9361 | // Note that according to DR 247, the containing class does not matter. |
| 9362 | if (FunctionTemplateDecl *FunctionTemplate |
| 9363 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
| 9364 | if (AddMatchingTemplateFunction(FunctionTemplate, I.getPair())) |
| 9365 | Ret = true; |
| 9366 | } |
| 9367 | // If we have explicit template arguments supplied, skip non-templates. |
| 9368 | else if (!OvlExpr->hasExplicitTemplateArgs() && |
| 9369 | AddMatchingNonTemplateFunction(Fn, I.getPair())) |
| 9370 | Ret = true; |
| 9371 | } |
| 9372 | assert(Ret || Matches.empty()); |
| 9373 | return Ret; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9374 | } |
| 9375 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9376 | void EliminateAllExceptMostSpecializedTemplate() { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 9377 | // [...] and any given function template specialization F1 is |
| 9378 | // eliminated if the set contains a second function template |
| 9379 | // specialization whose function template is more specialized |
| 9380 | // than the function template of F1 according to the partial |
| 9381 | // ordering rules of 14.5.5.2. |
| 9382 | |
| 9383 | // The algorithm specified above is quadratic. We instead use a |
| 9384 | // two-pass algorithm (similar to the one used to identify the |
| 9385 | // best viable function in an overload set) that identifies the |
| 9386 | // best function template (if it exists). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9387 | |
| 9388 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 9389 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 9390 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9391 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9392 | UnresolvedSetIterator Result = |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9393 | S.getMostSpecialized(MatchesCopy.begin(), MatchesCopy.end(), |
| 9394 | TPOC_Other, 0, SourceExpr->getLocStart(), |
| 9395 | S.PDiag(), |
| 9396 | S.PDiag(diag::err_addr_ovl_ambiguous) |
| 9397 | << Matches[0].second->getDeclName(), |
| 9398 | S.PDiag(diag::note_ovl_candidate) |
| 9399 | << (unsigned) oc_function_template, |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9400 | Complain, TargetFunctionType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9401 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9402 | if (Result != MatchesCopy.end()) { |
| 9403 | // Make it the first and only element |
| 9404 | Matches[0].first = Matches[Result - MatchesCopy.begin()].first; |
| 9405 | Matches[0].second = cast<FunctionDecl>(*Result); |
| 9406 | Matches.resize(1); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9407 | } |
| 9408 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9409 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9410 | void EliminateAllTemplateMatches() { |
| 9411 | // [...] any function template specializations in the set are |
| 9412 | // eliminated if the set also contains a non-template function, [...] |
| 9413 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
| 9414 | if (Matches[I].second->getPrimaryTemplate() == 0) |
| 9415 | ++I; |
| 9416 | else { |
| 9417 | Matches[I] = Matches[--N]; |
| 9418 | Matches.set_size(N); |
| 9419 | } |
| 9420 | } |
| 9421 | } |
| 9422 | |
| 9423 | public: |
| 9424 | void ComplainNoMatchesFound() const { |
| 9425 | assert(Matches.empty()); |
| 9426 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_no_viable) |
| 9427 | << OvlExpr->getName() << TargetFunctionType |
| 9428 | << OvlExpr->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9429 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9430 | } |
| 9431 | |
| 9432 | bool IsInvalidFormOfPointerToMemberFunction() const { |
| 9433 | return TargetTypeIsNonStaticMemberFunction && |
| 9434 | !OvlExprInfo.HasFormOfMemberPointer; |
| 9435 | } |
| 9436 | |
| 9437 | void ComplainIsInvalidFormOfPointerToMemberFunction() const { |
| 9438 | // TODO: Should we condition this on whether any functions might |
| 9439 | // have matched, or is it more appropriate to do that in callers? |
| 9440 | // TODO: a fixit wouldn't hurt. |
| 9441 | S.Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
| 9442 | << TargetType << OvlExpr->getSourceRange(); |
| 9443 | } |
| 9444 | |
| 9445 | void ComplainOfInvalidConversion() const { |
| 9446 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 9447 | << OvlExpr->getName() << TargetType; |
| 9448 | } |
| 9449 | |
| 9450 | void ComplainMultipleMatchesFound() const { |
| 9451 | assert(Matches.size() > 1); |
| 9452 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 9453 | << OvlExpr->getName() |
| 9454 | << OvlExpr->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9455 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9456 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9457 | |
| 9458 | bool hadMultipleCandidates() const { return (OvlExpr->getNumDecls() > 1); } |
| 9459 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9460 | int getNumMatches() const { return Matches.size(); } |
| 9461 | |
| 9462 | FunctionDecl* getMatchingFunctionDecl() const { |
| 9463 | if (Matches.size() != 1) return 0; |
| 9464 | return Matches[0].second; |
| 9465 | } |
| 9466 | |
| 9467 | const DeclAccessPair* getMatchingFunctionAccessPair() const { |
| 9468 | if (Matches.size() != 1) return 0; |
| 9469 | return &Matches[0].first; |
| 9470 | } |
| 9471 | }; |
| 9472 | |
| 9473 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 9474 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 9475 | /// expression with overloaded function type and @p ToType is the type |
| 9476 | /// we're trying to resolve to. For example: |
| 9477 | /// |
| 9478 | /// @code |
| 9479 | /// int f(double); |
| 9480 | /// int f(int); |
| 9481 | /// |
| 9482 | /// int (*pfd)(double) = f; // selects f(double) |
| 9483 | /// @endcode |
| 9484 | /// |
| 9485 | /// This routine returns the resulting FunctionDecl if it could be |
| 9486 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 9487 | /// routine will emit diagnostics if there is an error. |
| 9488 | FunctionDecl * |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9489 | Sema::ResolveAddressOfOverloadedFunction(Expr *AddressOfExpr, |
| 9490 | QualType TargetType, |
| 9491 | bool Complain, |
| 9492 | DeclAccessPair &FoundResult, |
| 9493 | bool *pHadMultipleCandidates) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9494 | assert(AddressOfExpr->getType() == Context.OverloadTy); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9495 | |
| 9496 | AddressOfFunctionResolver Resolver(*this, AddressOfExpr, TargetType, |
| 9497 | Complain); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9498 | int NumMatches = Resolver.getNumMatches(); |
| 9499 | FunctionDecl* Fn = 0; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9500 | if (NumMatches == 0 && Complain) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9501 | if (Resolver.IsInvalidFormOfPointerToMemberFunction()) |
| 9502 | Resolver.ComplainIsInvalidFormOfPointerToMemberFunction(); |
| 9503 | else |
| 9504 | Resolver.ComplainNoMatchesFound(); |
| 9505 | } |
| 9506 | else if (NumMatches > 1 && Complain) |
| 9507 | Resolver.ComplainMultipleMatchesFound(); |
| 9508 | else if (NumMatches == 1) { |
| 9509 | Fn = Resolver.getMatchingFunctionDecl(); |
| 9510 | assert(Fn); |
| 9511 | FoundResult = *Resolver.getMatchingFunctionAccessPair(); |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 9512 | if (Complain) |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9513 | CheckAddressOfMemberAccess(AddressOfExpr, FoundResult); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 9514 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9515 | |
| 9516 | if (pHadMultipleCandidates) |
| 9517 | *pHadMultipleCandidates = Resolver.hadMultipleCandidates(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9518 | return Fn; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9519 | } |
| 9520 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9521 | /// \brief Given an expression that refers to an overloaded function, try to |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9522 | /// resolve that overloaded function expression down to a single function. |
| 9523 | /// |
| 9524 | /// This routine can only resolve template-ids that refer to a single function |
| 9525 | /// template, where that template-id refers to a single template whose template |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9526 | /// arguments are either provided by the template-id or have defaults, |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9527 | /// as described in C++0x [temp.arg.explicit]p3. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9528 | FunctionDecl * |
| 9529 | Sema::ResolveSingleFunctionTemplateSpecialization(OverloadExpr *ovl, |
| 9530 | bool Complain, |
| 9531 | DeclAccessPair *FoundResult) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9532 | // C++ [over.over]p1: |
| 9533 | // [...] [Note: any redundant set of parentheses surrounding the |
| 9534 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9535 | // C++ [over.over]p1: |
| 9536 | // [...] The overloaded function name can be preceded by the & |
| 9537 | // operator. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9538 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9539 | // If we didn't actually find any template-ids, we're done. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9540 | if (!ovl->hasExplicitTemplateArgs()) |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9541 | return 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 9542 | |
| 9543 | TemplateArgumentListInfo ExplicitTemplateArgs; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9544 | ovl->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9545 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9546 | // Look through all of the overloaded functions, searching for one |
| 9547 | // whose type matches exactly. |
| 9548 | FunctionDecl *Matched = 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9549 | for (UnresolvedSetIterator I = ovl->decls_begin(), |
| 9550 | E = ovl->decls_end(); I != E; ++I) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9551 | // C++0x [temp.arg.explicit]p3: |
| 9552 | // [...] In contexts where deduction is done and fails, or in contexts |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9553 | // where deduction is not done, if a template argument list is |
| 9554 | // specified and it, along with any default template arguments, |
| 9555 | // identifies a single function template specialization, then the |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9556 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | 66a8c9a | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 9557 | FunctionTemplateDecl *FunctionTemplate |
| 9558 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9559 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9560 | // C++ [over.over]p2: |
| 9561 | // If the name is a function template, template argument deduction is |
| 9562 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 9563 | // resulting template argument list is used to generate a single |
| 9564 | // function template specialization, which is added to the set of |
| 9565 | // overloaded functions considered. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9566 | FunctionDecl *Specialization = 0; |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 9567 | TemplateDeductionInfo Info(ovl->getNameLoc()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9568 | if (TemplateDeductionResult Result |
| 9569 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9570 | Specialization, Info, |
| 9571 | /*InOverloadResolution=*/true)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9572 | // FIXME: make a note of the failed deduction for diagnostics. |
| 9573 | (void)Result; |
| 9574 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9575 | } |
| 9576 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9577 | assert(Specialization && "no specialization and no error?"); |
| 9578 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9579 | // Multiple matches; we can't resolve to a single declaration. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9580 | if (Matched) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9581 | if (Complain) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9582 | Diag(ovl->getExprLoc(), diag::err_addr_ovl_ambiguous) |
| 9583 | << ovl->getName(); |
| 9584 | NoteAllOverloadCandidates(ovl); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9585 | } |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9586 | return 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9587 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9588 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9589 | Matched = Specialization; |
| 9590 | if (FoundResult) *FoundResult = I.getPair(); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9591 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9592 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9593 | if (Matched && getLangOpts().CPlusPlus1y && |
| 9594 | Matched->getResultType()->isUndeducedType() && |
| 9595 | DeduceReturnType(Matched, ovl->getExprLoc(), Complain)) |
| 9596 | return 0; |
| 9597 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9598 | return Matched; |
| 9599 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9600 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9601 | |
| 9602 | |
| 9603 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9604 | // Resolve and fix an overloaded expression that can be resolved |
| 9605 | // because it identifies a single function template specialization. |
| 9606 | // |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9607 | // Last three arguments should only be supplied if Complain = true |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9608 | // |
| 9609 | // Return true if it was logically possible to so resolve the |
| 9610 | // expression, regardless of whether or not it succeeded. Always |
| 9611 | // returns true if 'complain' is set. |
| 9612 | bool Sema::ResolveAndFixSingleFunctionTemplateSpecialization( |
| 9613 | ExprResult &SrcExpr, bool doFunctionPointerConverion, |
| 9614 | bool complain, const SourceRange& OpRangeForComplaining, |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9615 | QualType DestTypeForComplaining, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9616 | unsigned DiagIDForComplaining) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9617 | assert(SrcExpr.get()->getType() == Context.OverloadTy); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9618 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9619 | OverloadExpr::FindResult ovl = OverloadExpr::find(SrcExpr.get()); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9620 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9621 | DeclAccessPair found; |
| 9622 | ExprResult SingleFunctionExpression; |
| 9623 | if (FunctionDecl *fn = ResolveSingleFunctionTemplateSpecialization( |
| 9624 | ovl.Expression, /*complain*/ false, &found)) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9625 | if (DiagnoseUseOfDecl(fn, SrcExpr.get()->getLocStart())) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9626 | SrcExpr = ExprError(); |
| 9627 | return true; |
| 9628 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9629 | |
| 9630 | // It is only correct to resolve to an instance method if we're |
| 9631 | // resolving a form that's permitted to be a pointer to member. |
| 9632 | // Otherwise we'll end up making a bound member expression, which |
| 9633 | // is illegal in all the contexts we resolve like this. |
| 9634 | if (!ovl.HasFormOfMemberPointer && |
| 9635 | isa<CXXMethodDecl>(fn) && |
| 9636 | cast<CXXMethodDecl>(fn)->isInstance()) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9637 | if (!complain) return false; |
| 9638 | |
| 9639 | Diag(ovl.Expression->getExprLoc(), |
| 9640 | diag::err_bound_member_function) |
| 9641 | << 0 << ovl.Expression->getSourceRange(); |
| 9642 | |
| 9643 | // TODO: I believe we only end up here if there's a mix of |
| 9644 | // static and non-static candidates (otherwise the expression |
| 9645 | // would have 'bound member' type, not 'overload' type). |
| 9646 | // Ideally we would note which candidate was chosen and why |
| 9647 | // the static candidates were rejected. |
| 9648 | SrcExpr = ExprError(); |
| 9649 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9650 | } |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 9651 | |
Sylvestre Ledru | 43e3dee | 2012-07-31 06:56:50 +0000 | [diff] [blame] | 9652 | // Fix the expression to refer to 'fn'. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9653 | SingleFunctionExpression = |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9654 | Owned(FixOverloadedFunctionReference(SrcExpr.take(), found, fn)); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9655 | |
| 9656 | // If desired, do function-to-pointer decay. |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9657 | if (doFunctionPointerConverion) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9658 | SingleFunctionExpression = |
| 9659 | DefaultFunctionArrayLvalueConversion(SingleFunctionExpression.take()); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9660 | if (SingleFunctionExpression.isInvalid()) { |
| 9661 | SrcExpr = ExprError(); |
| 9662 | return true; |
| 9663 | } |
| 9664 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9665 | } |
| 9666 | |
| 9667 | if (!SingleFunctionExpression.isUsable()) { |
| 9668 | if (complain) { |
| 9669 | Diag(OpRangeForComplaining.getBegin(), DiagIDForComplaining) |
| 9670 | << ovl.Expression->getName() |
| 9671 | << DestTypeForComplaining |
| 9672 | << OpRangeForComplaining |
| 9673 | << ovl.Expression->getQualifierLoc().getSourceRange(); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9674 | NoteAllOverloadCandidates(SrcExpr.get()); |
| 9675 | |
| 9676 | SrcExpr = ExprError(); |
| 9677 | return true; |
| 9678 | } |
| 9679 | |
| 9680 | return false; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9681 | } |
| 9682 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9683 | SrcExpr = SingleFunctionExpression; |
| 9684 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9685 | } |
| 9686 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9687 | /// \brief Add a single candidate to the overload set. |
| 9688 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9689 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 9690 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9691 | ArrayRef<Expr *> Args, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9692 | OverloadCandidateSet &CandidateSet, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9693 | bool PartialOverloading, |
| 9694 | bool KnownValid) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9695 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9696 | if (isa<UsingShadowDecl>(Callee)) |
| 9697 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 9698 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9699 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9700 | if (ExplicitTemplateArgs) { |
| 9701 | assert(!KnownValid && "Explicit template arguments?"); |
| 9702 | return; |
| 9703 | } |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9704 | S.AddOverloadCandidate(Func, FoundDecl, Args, CandidateSet, false, |
| 9705 | PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9706 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9707 | } |
| 9708 | |
| 9709 | if (FunctionTemplateDecl *FuncTemplate |
| 9710 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9711 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9712 | ExplicitTemplateArgs, Args, CandidateSet); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9713 | return; |
| 9714 | } |
| 9715 | |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9716 | assert(!KnownValid && "unhandled case in overloaded call candidate"); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9717 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9718 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9719 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 9720 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9721 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9722 | ArrayRef<Expr *> Args, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9723 | OverloadCandidateSet &CandidateSet, |
| 9724 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9725 | |
| 9726 | #ifndef NDEBUG |
| 9727 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 9728 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9729 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9730 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 9731 | // and let Y be the lookup set produced by argument dependent |
| 9732 | // lookup (defined as follows). If X contains |
| 9733 | // |
| 9734 | // -- a declaration of a class member, or |
| 9735 | // |
| 9736 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9737 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9738 | // |
| 9739 | // -- a declaration that is neither a function or a function |
| 9740 | // template |
| 9741 | // |
| 9742 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9743 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9744 | if (ULE->requiresADL()) { |
| 9745 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 9746 | E = ULE->decls_end(); I != E; ++I) { |
| 9747 | assert(!(*I)->getDeclContext()->isRecord()); |
| 9748 | assert(isa<UsingShadowDecl>(*I) || |
| 9749 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 9750 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9751 | } |
| 9752 | } |
| 9753 | #endif |
| 9754 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9755 | // It would be nice to avoid this copy. |
| 9756 | TemplateArgumentListInfo TABuffer; |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 9757 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9758 | if (ULE->hasExplicitTemplateArgs()) { |
| 9759 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 9760 | ExplicitTemplateArgs = &TABuffer; |
| 9761 | } |
| 9762 | |
| 9763 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 9764 | E = ULE->decls_end(); I != E; ++I) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9765 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, Args, |
| 9766 | CandidateSet, PartialOverloading, |
| 9767 | /*KnownValid*/ true); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9768 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9769 | if (ULE->requiresADL()) |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9770 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
Richard Smith | f5cd5cc | 2012-02-25 06:24:24 +0000 | [diff] [blame] | 9771 | ULE->getExprLoc(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9772 | Args, ExplicitTemplateArgs, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 9773 | CandidateSet, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9774 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9775 | |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 9776 | /// Determine whether a declaration with the specified name could be moved into |
| 9777 | /// a different namespace. |
| 9778 | static bool canBeDeclaredInNamespace(const DeclarationName &Name) { |
| 9779 | switch (Name.getCXXOverloadedOperator()) { |
| 9780 | case OO_New: case OO_Array_New: |
| 9781 | case OO_Delete: case OO_Array_Delete: |
| 9782 | return false; |
| 9783 | |
| 9784 | default: |
| 9785 | return true; |
| 9786 | } |
| 9787 | } |
| 9788 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9789 | /// Attempt to recover from an ill-formed use of a non-dependent name in a |
| 9790 | /// template, where the non-dependent name was declared after the template |
| 9791 | /// was defined. This is common in code written for a compilers which do not |
| 9792 | /// correctly implement two-stage name lookup. |
| 9793 | /// |
| 9794 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 9795 | static bool |
| 9796 | DiagnoseTwoPhaseLookup(Sema &SemaRef, SourceLocation FnLoc, |
| 9797 | const CXXScopeSpec &SS, LookupResult &R, |
| 9798 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9799 | ArrayRef<Expr *> Args) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9800 | if (SemaRef.ActiveTemplateInstantiations.empty() || !SS.isEmpty()) |
| 9801 | return false; |
| 9802 | |
| 9803 | for (DeclContext *DC = SemaRef.CurContext; DC; DC = DC->getParent()) { |
Nick Lewycky | 5a7120c | 2012-03-14 20:41:00 +0000 | [diff] [blame] | 9804 | if (DC->isTransparentContext()) |
| 9805 | continue; |
| 9806 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9807 | SemaRef.LookupQualifiedName(R, DC); |
| 9808 | |
| 9809 | if (!R.empty()) { |
| 9810 | R.suppressDiagnostics(); |
| 9811 | |
| 9812 | if (isa<CXXRecordDecl>(DC)) { |
| 9813 | // Don't diagnose names we find in classes; we get much better |
| 9814 | // diagnostics for these from DiagnoseEmptyLookup. |
| 9815 | R.clear(); |
| 9816 | return false; |
| 9817 | } |
| 9818 | |
| 9819 | OverloadCandidateSet Candidates(FnLoc); |
| 9820 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 9821 | AddOverloadedCallCandidate(SemaRef, I.getPair(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9822 | ExplicitTemplateArgs, Args, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9823 | Candidates, false, /*KnownValid*/ false); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9824 | |
| 9825 | OverloadCandidateSet::iterator Best; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9826 | if (Candidates.BestViableFunction(SemaRef, FnLoc, Best) != OR_Success) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9827 | // No viable functions. Don't bother the user with notes for functions |
| 9828 | // which don't work and shouldn't be found anyway. |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9829 | R.clear(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9830 | return false; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9831 | } |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9832 | |
| 9833 | // Find the namespaces where ADL would have looked, and suggest |
| 9834 | // declaring the function there instead. |
| 9835 | Sema::AssociatedNamespaceSet AssociatedNamespaces; |
| 9836 | Sema::AssociatedClassSet AssociatedClasses; |
John McCall | 42f48fb | 2012-08-24 20:38:34 +0000 | [diff] [blame] | 9837 | SemaRef.FindAssociatedClassesAndNamespaces(FnLoc, Args, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9838 | AssociatedNamespaces, |
| 9839 | AssociatedClasses); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9840 | Sema::AssociatedNamespaceSet SuggestedNamespaces; |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 9841 | if (canBeDeclaredInNamespace(R.getLookupName())) { |
| 9842 | DeclContext *Std = SemaRef.getStdNamespace(); |
| 9843 | for (Sema::AssociatedNamespaceSet::iterator |
| 9844 | it = AssociatedNamespaces.begin(), |
| 9845 | end = AssociatedNamespaces.end(); it != end; ++it) { |
| 9846 | // Never suggest declaring a function within namespace 'std'. |
| 9847 | if (Std && Std->Encloses(*it)) |
| 9848 | continue; |
Richard Smith | 19e0d95 | 2012-12-22 02:46:14 +0000 | [diff] [blame] | 9849 | |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 9850 | // Never suggest declaring a function within a namespace with a |
| 9851 | // reserved name, like __gnu_cxx. |
| 9852 | NamespaceDecl *NS = dyn_cast<NamespaceDecl>(*it); |
| 9853 | if (NS && |
| 9854 | NS->getQualifiedNameAsString().find("__") != std::string::npos) |
| 9855 | continue; |
| 9856 | |
| 9857 | SuggestedNamespaces.insert(*it); |
| 9858 | } |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9859 | } |
| 9860 | |
| 9861 | SemaRef.Diag(R.getNameLoc(), diag::err_not_found_by_two_phase_lookup) |
| 9862 | << R.getLookupName(); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9863 | if (SuggestedNamespaces.empty()) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9864 | SemaRef.Diag(Best->Function->getLocation(), |
| 9865 | diag::note_not_found_by_two_phase_lookup) |
| 9866 | << R.getLookupName() << 0; |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9867 | } else if (SuggestedNamespaces.size() == 1) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9868 | SemaRef.Diag(Best->Function->getLocation(), |
| 9869 | diag::note_not_found_by_two_phase_lookup) |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9870 | << R.getLookupName() << 1 << *SuggestedNamespaces.begin(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9871 | } else { |
| 9872 | // FIXME: It would be useful to list the associated namespaces here, |
| 9873 | // but the diagnostics infrastructure doesn't provide a way to produce |
| 9874 | // a localized representation of a list of items. |
| 9875 | SemaRef.Diag(Best->Function->getLocation(), |
| 9876 | diag::note_not_found_by_two_phase_lookup) |
| 9877 | << R.getLookupName() << 2; |
| 9878 | } |
| 9879 | |
| 9880 | // Try to recover by calling this function. |
| 9881 | return true; |
| 9882 | } |
| 9883 | |
| 9884 | R.clear(); |
| 9885 | } |
| 9886 | |
| 9887 | return false; |
| 9888 | } |
| 9889 | |
| 9890 | /// Attempt to recover from ill-formed use of a non-dependent operator in a |
| 9891 | /// template, where the non-dependent operator was declared after the template |
| 9892 | /// was defined. |
| 9893 | /// |
| 9894 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 9895 | static bool |
| 9896 | DiagnoseTwoPhaseOperatorLookup(Sema &SemaRef, OverloadedOperatorKind Op, |
| 9897 | SourceLocation OpLoc, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9898 | ArrayRef<Expr *> Args) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9899 | DeclarationName OpName = |
| 9900 | SemaRef.Context.DeclarationNames.getCXXOperatorName(Op); |
| 9901 | LookupResult R(SemaRef, OpName, OpLoc, Sema::LookupOperatorName); |
| 9902 | return DiagnoseTwoPhaseLookup(SemaRef, OpLoc, CXXScopeSpec(), R, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9903 | /*ExplicitTemplateArgs=*/0, Args); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9904 | } |
| 9905 | |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 9906 | namespace { |
| 9907 | // Callback to limit the allowed keywords and to only accept typo corrections |
| 9908 | // that are keywords or whose decls refer to functions (or template functions) |
| 9909 | // that accept the given number of arguments. |
| 9910 | class RecoveryCallCCC : public CorrectionCandidateCallback { |
| 9911 | public: |
| 9912 | RecoveryCallCCC(Sema &SemaRef, unsigned NumArgs, bool HasExplicitTemplateArgs) |
| 9913 | : NumArgs(NumArgs), HasExplicitTemplateArgs(HasExplicitTemplateArgs) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9914 | WantTypeSpecifiers = SemaRef.getLangOpts().CPlusPlus; |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 9915 | WantRemainingKeywords = false; |
| 9916 | } |
| 9917 | |
| 9918 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 9919 | if (!candidate.getCorrectionDecl()) |
| 9920 | return candidate.isKeyword(); |
| 9921 | |
| 9922 | for (TypoCorrection::const_decl_iterator DI = candidate.begin(), |
| 9923 | DIEnd = candidate.end(); DI != DIEnd; ++DI) { |
| 9924 | FunctionDecl *FD = 0; |
| 9925 | NamedDecl *ND = (*DI)->getUnderlyingDecl(); |
| 9926 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 9927 | FD = FTD->getTemplatedDecl(); |
| 9928 | if (!HasExplicitTemplateArgs && !FD) { |
| 9929 | if (!(FD = dyn_cast<FunctionDecl>(ND)) && isa<ValueDecl>(ND)) { |
| 9930 | // If the Decl is neither a function nor a template function, |
| 9931 | // determine if it is a pointer or reference to a function. If so, |
| 9932 | // check against the number of arguments expected for the pointee. |
| 9933 | QualType ValType = cast<ValueDecl>(ND)->getType(); |
| 9934 | if (ValType->isAnyPointerType() || ValType->isReferenceType()) |
| 9935 | ValType = ValType->getPointeeType(); |
| 9936 | if (const FunctionProtoType *FPT = ValType->getAs<FunctionProtoType>()) |
| 9937 | if (FPT->getNumArgs() == NumArgs) |
| 9938 | return true; |
| 9939 | } |
| 9940 | } |
| 9941 | if (FD && FD->getNumParams() >= NumArgs && |
| 9942 | FD->getMinRequiredArguments() <= NumArgs) |
| 9943 | return true; |
| 9944 | } |
| 9945 | return false; |
| 9946 | } |
| 9947 | |
| 9948 | private: |
| 9949 | unsigned NumArgs; |
| 9950 | bool HasExplicitTemplateArgs; |
| 9951 | }; |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 9952 | |
| 9953 | // Callback that effectively disabled typo correction |
| 9954 | class NoTypoCorrectionCCC : public CorrectionCandidateCallback { |
| 9955 | public: |
| 9956 | NoTypoCorrectionCCC() { |
| 9957 | WantTypeSpecifiers = false; |
| 9958 | WantExpressionKeywords = false; |
| 9959 | WantCXXNamedCasts = false; |
| 9960 | WantRemainingKeywords = false; |
| 9961 | } |
| 9962 | |
| 9963 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 9964 | return false; |
| 9965 | } |
| 9966 | }; |
Richard Smith | e49ff3e | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 9967 | |
| 9968 | class BuildRecoveryCallExprRAII { |
| 9969 | Sema &SemaRef; |
| 9970 | public: |
| 9971 | BuildRecoveryCallExprRAII(Sema &S) : SemaRef(S) { |
| 9972 | assert(SemaRef.IsBuildingRecoveryCallExpr == false); |
| 9973 | SemaRef.IsBuildingRecoveryCallExpr = true; |
| 9974 | } |
| 9975 | |
| 9976 | ~BuildRecoveryCallExprRAII() { |
| 9977 | SemaRef.IsBuildingRecoveryCallExpr = false; |
| 9978 | } |
| 9979 | }; |
| 9980 | |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 9981 | } |
| 9982 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9983 | /// Attempts to recover from a call where no functions were found. |
| 9984 | /// |
| 9985 | /// Returns true if new candidates were found. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9986 | static ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 9987 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9988 | UnresolvedLookupExpr *ULE, |
| 9989 | SourceLocation LParenLoc, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9990 | llvm::MutableArrayRef<Expr *> Args, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9991 | SourceLocation RParenLoc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 9992 | bool EmptyLookup, bool AllowTypoCorrection) { |
Richard Smith | e49ff3e | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 9993 | // Do not try to recover if it is already building a recovery call. |
| 9994 | // This stops infinite loops for template instantiations like |
| 9995 | // |
| 9996 | // template <typename T> auto foo(T t) -> decltype(foo(t)) {} |
| 9997 | // template <typename T> auto foo(T t) -> decltype(foo(&t)) {} |
| 9998 | // |
| 9999 | if (SemaRef.IsBuildingRecoveryCallExpr) |
| 10000 | return ExprError(); |
| 10001 | BuildRecoveryCallExprRAII RCE(SemaRef); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10002 | |
| 10003 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10004 | SS.Adopt(ULE->getQualifierLoc()); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10005 | SourceLocation TemplateKWLoc = ULE->getTemplateKeywordLoc(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10006 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10007 | TemplateArgumentListInfo TABuffer; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10008 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10009 | if (ULE->hasExplicitTemplateArgs()) { |
| 10010 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 10011 | ExplicitTemplateArgs = &TABuffer; |
| 10012 | } |
| 10013 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10014 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 10015 | Sema::LookupOrdinaryName); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10016 | RecoveryCallCCC Validator(SemaRef, Args.size(), ExplicitTemplateArgs != 0); |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10017 | NoTypoCorrectionCCC RejectAll; |
| 10018 | CorrectionCandidateCallback *CCC = AllowTypoCorrection ? |
| 10019 | (CorrectionCandidateCallback*)&Validator : |
| 10020 | (CorrectionCandidateCallback*)&RejectAll; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10021 | if (!DiagnoseTwoPhaseLookup(SemaRef, Fn->getExprLoc(), SS, R, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10022 | ExplicitTemplateArgs, Args) && |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10023 | (!EmptyLookup || |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10024 | SemaRef.DiagnoseEmptyLookup(S, SS, R, *CCC, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10025 | ExplicitTemplateArgs, Args))) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10026 | return ExprError(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10027 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10028 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 10029 | |
| 10030 | // Build an implicit member call if appropriate. Just drop the |
| 10031 | // casts and such from the call, we don't really care. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10032 | ExprResult NewFn = ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10033 | if ((*R.begin())->isCXXClassMember()) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10034 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, |
| 10035 | R, ExplicitTemplateArgs); |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 10036 | else if (ExplicitTemplateArgs || TemplateKWLoc.isValid()) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10037 | NewFn = SemaRef.BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 10038 | ExplicitTemplateArgs); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10039 | else |
| 10040 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 10041 | |
| 10042 | if (NewFn.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10043 | return ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10044 | |
| 10045 | // This shouldn't cause an infinite loop because we're giving it |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10046 | // an expression with viable lookup results, which should never |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10047 | // end up here. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10048 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, NewFn.take(), LParenLoc, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10049 | MultiExprArg(Args.data(), Args.size()), |
| 10050 | RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10051 | } |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 10052 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10053 | /// \brief Constructs and populates an OverloadedCandidateSet from |
| 10054 | /// the given function. |
| 10055 | /// \returns true when an the ExprResult output parameter has been set. |
| 10056 | bool Sema::buildOverloadedCallSet(Scope *S, Expr *Fn, |
| 10057 | UnresolvedLookupExpr *ULE, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10058 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10059 | SourceLocation RParenLoc, |
| 10060 | OverloadCandidateSet *CandidateSet, |
| 10061 | ExprResult *Result) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10062 | #ifndef NDEBUG |
| 10063 | if (ULE->requiresADL()) { |
| 10064 | // To do ADL, we must have found an unqualified name. |
| 10065 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 10066 | |
| 10067 | // We don't perform ADL for implicit declarations of builtins. |
| 10068 | // Verify that this was correctly set up. |
| 10069 | FunctionDecl *F; |
| 10070 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 10071 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 10072 | F->getBuiltinID() && F->isImplicit()) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 10073 | llvm_unreachable("performing ADL for builtin"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10074 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10075 | // We don't perform ADL in C. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10076 | assert(getLangOpts().CPlusPlus && "ADL enabled in C"); |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 10077 | } |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10078 | #endif |
| 10079 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10080 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10081 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) { |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10082 | *Result = ExprError(); |
| 10083 | return true; |
| 10084 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 10085 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10086 | // Add the functions denoted by the callee to the set of candidate |
| 10087 | // functions, including those from argument-dependent lookup. |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10088 | AddOverloadedCallCandidates(ULE, Args, *CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10089 | |
| 10090 | // If we found nothing, try to recover. |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10091 | // BuildRecoveryCallExpr diagnoses the error itself, so we just bail |
| 10092 | // out if it fails. |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10093 | if (CandidateSet->empty()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10094 | // In Microsoft mode, if we are inside a template class member function then |
| 10095 | // create a type dependent CallExpr. The goal is to postpone name lookup |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 10096 | // to instantiation time to be able to search into type dependent base |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10097 | // classes. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10098 | if (getLangOpts().MicrosoftMode && CurContext->isDependentContext() && |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 10099 | (isa<FunctionDecl>(CurContext) || isa<CXXRecordDecl>(CurContext))) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10100 | CallExpr *CE = new (Context) CallExpr(Context, Fn, Args, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10101 | Context.DependentTy, VK_RValue, |
| 10102 | RParenLoc); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10103 | CE->setTypeDependent(true); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10104 | *Result = Owned(CE); |
| 10105 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10106 | } |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10107 | return false; |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 10108 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10109 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10110 | UnbridgedCasts.restore(); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10111 | return false; |
| 10112 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10113 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10114 | /// FinishOverloadedCallExpr - given an OverloadCandidateSet, builds and returns |
| 10115 | /// the completed call expression. If overload resolution fails, emits |
| 10116 | /// diagnostics and returns ExprError() |
| 10117 | static ExprResult FinishOverloadedCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
| 10118 | UnresolvedLookupExpr *ULE, |
| 10119 | SourceLocation LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10120 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10121 | SourceLocation RParenLoc, |
| 10122 | Expr *ExecConfig, |
| 10123 | OverloadCandidateSet *CandidateSet, |
| 10124 | OverloadCandidateSet::iterator *Best, |
| 10125 | OverloadingResult OverloadResult, |
| 10126 | bool AllowTypoCorrection) { |
| 10127 | if (CandidateSet->empty()) |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10128 | return BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10129 | RParenLoc, /*EmptyLookup=*/true, |
| 10130 | AllowTypoCorrection); |
| 10131 | |
| 10132 | switch (OverloadResult) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10133 | case OR_Success: { |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10134 | FunctionDecl *FDecl = (*Best)->Function; |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10135 | SemaRef.CheckUnresolvedLookupAccess(ULE, (*Best)->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 10136 | if (SemaRef.DiagnoseUseOfDecl(FDecl, ULE->getNameLoc())) |
| 10137 | return ExprError(); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10138 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10139 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
| 10140 | ExecConfig); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10141 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10142 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10143 | case OR_No_Viable_Function: { |
| 10144 | // Try to recover by looking for viable functions which the user might |
| 10145 | // have meant to call. |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10146 | ExprResult Recovery = BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10147 | Args, RParenLoc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10148 | /*EmptyLookup=*/false, |
| 10149 | AllowTypoCorrection); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10150 | if (!Recovery.isInvalid()) |
| 10151 | return Recovery; |
| 10152 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10153 | SemaRef.Diag(Fn->getLocStart(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10154 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10155 | << ULE->getName() << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10156 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, Args); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10157 | break; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10158 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10159 | |
| 10160 | case OR_Ambiguous: |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10161 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10162 | << ULE->getName() << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10163 | CandidateSet->NoteCandidates(SemaRef, OCD_ViableCandidates, Args); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10164 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10165 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10166 | case OR_Deleted: { |
| 10167 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_deleted_call) |
| 10168 | << (*Best)->Function->isDeleted() |
| 10169 | << ULE->getName() |
| 10170 | << SemaRef.getDeletedOrUnavailableSuffix((*Best)->Function) |
| 10171 | << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10172 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, Args); |
Argyrios Kyrtzidis | 0d579b6 | 2011-11-04 15:58:13 +0000 | [diff] [blame] | 10173 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10174 | // We emitted an error for the unvailable/deleted function call but keep |
| 10175 | // the call in the AST. |
| 10176 | FunctionDecl *FDecl = (*Best)->Function; |
| 10177 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10178 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
| 10179 | ExecConfig); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10180 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10181 | } |
| 10182 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 10183 | // Overload resolution failed. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10184 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10185 | } |
| 10186 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10187 | /// BuildOverloadedCallExpr - Given the call expression that calls Fn |
| 10188 | /// (which eventually refers to the declaration Func) and the call |
| 10189 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 10190 | /// to a specific function. If overload resolution succeeds, returns |
| 10191 | /// the call expression produced by overload resolution. |
| 10192 | /// Otherwise, emits diagnostics and returns ExprError. |
| 10193 | ExprResult Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, |
| 10194 | UnresolvedLookupExpr *ULE, |
| 10195 | SourceLocation LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10196 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10197 | SourceLocation RParenLoc, |
| 10198 | Expr *ExecConfig, |
| 10199 | bool AllowTypoCorrection) { |
| 10200 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
| 10201 | ExprResult result; |
| 10202 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10203 | if (buildOverloadedCallSet(S, Fn, ULE, Args, LParenLoc, &CandidateSet, |
| 10204 | &result)) |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10205 | return result; |
| 10206 | |
| 10207 | OverloadCandidateSet::iterator Best; |
| 10208 | OverloadingResult OverloadResult = |
| 10209 | CandidateSet.BestViableFunction(*this, Fn->getLocStart(), Best); |
| 10210 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10211 | return FinishOverloadedCallExpr(*this, S, Fn, ULE, LParenLoc, Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10212 | RParenLoc, ExecConfig, &CandidateSet, |
| 10213 | &Best, OverloadResult, |
| 10214 | AllowTypoCorrection); |
| 10215 | } |
| 10216 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10217 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 10218 | return Functions.size() > 1 || |
| 10219 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 10220 | } |
| 10221 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10222 | /// \brief Create a unary operation that may resolve to an overloaded |
| 10223 | /// operator. |
| 10224 | /// |
| 10225 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 10226 | /// |
| 10227 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 10228 | /// operator. |
| 10229 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 10230 | /// \param Fns The set of non-member functions that will be |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10231 | /// considered by overload resolution. The caller needs to build this |
| 10232 | /// set based on the context using, e.g., |
| 10233 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 10234 | /// set should not contain any member functions; those will be added |
| 10235 | /// by CreateOverloadedUnaryOp(). |
| 10236 | /// |
James Dennett | 8da1687 | 2012-06-22 10:32:46 +0000 | [diff] [blame] | 10237 | /// \param Input The input argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10238 | ExprResult |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10239 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 10240 | const UnresolvedSetImpl &Fns, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10241 | Expr *Input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10242 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10243 | |
| 10244 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 10245 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 10246 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10247 | // TODO: provide better source location info. |
| 10248 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10249 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10250 | if (checkPlaceholderForOverload(*this, Input)) |
| 10251 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10252 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10253 | Expr *Args[2] = { Input, 0 }; |
| 10254 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10255 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10256 | // For post-increment and post-decrement, add the implicit '0' as |
| 10257 | // the second argument, so that we know this is a post-increment or |
| 10258 | // post-decrement. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10259 | if (Opc == UO_PostInc || Opc == UO_PostDec) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10260 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 10261 | Args[1] = IntegerLiteral::Create(Context, Zero, Context.IntTy, |
| 10262 | SourceLocation()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10263 | NumArgs = 2; |
| 10264 | } |
| 10265 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10266 | ArrayRef<Expr *> ArgsArray(Args, NumArgs); |
| 10267 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10268 | if (Input->isTypeDependent()) { |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10269 | if (Fns.empty()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10270 | return Owned(new (Context) UnaryOperator(Input, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10271 | Opc, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10272 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10273 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10274 | OpLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10275 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10276 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10277 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 10278 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10279 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10280 | /*ADL*/ true, IsOverloaded(Fns), |
| 10281 | Fns.begin(), Fns.end()); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10282 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, ArgsArray, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10283 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10284 | VK_RValue, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10285 | OpLoc, false)); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10286 | } |
| 10287 | |
| 10288 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10289 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10290 | |
| 10291 | // Add the candidates from the given function set. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10292 | AddFunctionCandidates(Fns, ArgsArray, CandidateSet, false); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10293 | |
| 10294 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10295 | AddMemberOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10296 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10297 | // Add candidates from ADL. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10298 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, OpLoc, |
| 10299 | ArgsArray, /*ExplicitTemplateArgs*/ 0, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10300 | CandidateSet); |
| 10301 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10302 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10303 | AddBuiltinOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10304 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10305 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10306 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10307 | // Perform overload resolution. |
| 10308 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10309 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10310 | case OR_Success: { |
| 10311 | // We found a built-in operator or an overloaded operator. |
| 10312 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10313 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10314 | if (FnDecl) { |
| 10315 | // We matched an overloaded operator. Build a call to that |
| 10316 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10317 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10318 | // Convert the arguments. |
| 10319 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10320 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10321 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10322 | ExprResult InputRes = |
| 10323 | PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 10324 | Best->FoundDecl, Method); |
| 10325 | if (InputRes.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10326 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10327 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10328 | } else { |
| 10329 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10330 | ExprResult InputInit |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 10331 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10332 | Context, |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 10333 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10334 | SourceLocation(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10335 | Input); |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 10336 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10337 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10338 | Input = InputInit.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10339 | } |
| 10340 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10341 | // Determine the result type. |
| 10342 | QualType ResultTy = FnDecl->getResultType(); |
| 10343 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10344 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10345 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10346 | // Build the actual expression node. |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10347 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10348 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10349 | if (FnExpr.isInvalid()) |
| 10350 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10351 | |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 10352 | Args[0] = Input; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10353 | CallExpr *TheCall = |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10354 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), ArgsArray, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10355 | ResultTy, VK, OpLoc, false); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10356 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10357 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 10358 | FnDecl)) |
| 10359 | return ExprError(); |
| 10360 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10361 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10362 | } else { |
| 10363 | // We matched a built-in operator. Convert the arguments, then |
| 10364 | // break out so that we will build the appropriate built-in |
| 10365 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10366 | ExprResult InputRes = |
| 10367 | PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 10368 | Best->Conversions[0], AA_Passing); |
| 10369 | if (InputRes.isInvalid()) |
| 10370 | return ExprError(); |
| 10371 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10372 | break; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10373 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10374 | } |
| 10375 | |
| 10376 | case OR_No_Viable_Function: |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10377 | // This is an erroneous use of an operator which can be overloaded by |
| 10378 | // a non-member function. Check for non-member operators which were |
| 10379 | // defined too late to be candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10380 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, ArgsArray)) |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10381 | // FIXME: Recover by calling the found function. |
| 10382 | return ExprError(); |
| 10383 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10384 | // No viable function; fall through to handling this as a |
| 10385 | // built-in operator, which will produce an error message for us. |
| 10386 | break; |
| 10387 | |
| 10388 | case OR_Ambiguous: |
| 10389 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 10390 | << UnaryOperator::getOpcodeStr(Opc) |
| 10391 | << Input->getType() |
| 10392 | << Input->getSourceRange(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10393 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, ArgsArray, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10394 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
| 10395 | return ExprError(); |
| 10396 | |
| 10397 | case OR_Deleted: |
| 10398 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 10399 | << Best->Function->isDeleted() |
| 10400 | << UnaryOperator::getOpcodeStr(Opc) |
| 10401 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 10402 | << Input->getSourceRange(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10403 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, ArgsArray, |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 10404 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10405 | return ExprError(); |
| 10406 | } |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10407 | |
| 10408 | // Either we found no viable overloaded operator or we matched a |
| 10409 | // built-in operator. In either case, fall through to trying to |
| 10410 | // build a built-in operation. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10411 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10412 | } |
| 10413 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10414 | /// \brief Create a binary operation that may resolve to an overloaded |
| 10415 | /// operator. |
| 10416 | /// |
| 10417 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 10418 | /// |
| 10419 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 10420 | /// operator. |
| 10421 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 10422 | /// \param Fns The set of non-member functions that will be |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10423 | /// considered by overload resolution. The caller needs to build this |
| 10424 | /// set based on the context using, e.g., |
| 10425 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 10426 | /// set should not contain any member functions; those will be added |
| 10427 | /// by CreateOverloadedBinOp(). |
| 10428 | /// |
| 10429 | /// \param LHS Left-hand argument. |
| 10430 | /// \param RHS Right-hand argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10431 | ExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10432 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10433 | unsigned OpcIn, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10434 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10435 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10436 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10437 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10438 | |
| 10439 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 10440 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 10441 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 10442 | |
| 10443 | // If either side is type-dependent, create an appropriate dependent |
| 10444 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10445 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10446 | if (Fns.empty()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10447 | // If there are no functions to store, just build a dependent |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10448 | // BinaryOperator or CompoundAssignment. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10449 | if (Opc <= BO_Assign || Opc > BO_OrAssign) |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10450 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10451 | Context.DependentTy, |
| 10452 | VK_RValue, OK_Ordinary, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10453 | OpLoc, |
| 10454 | FPFeatures.fp_contract)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10455 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10456 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 10457 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10458 | VK_LValue, |
| 10459 | OK_Ordinary, |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10460 | Context.DependentTy, |
| 10461 | Context.DependentTy, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10462 | OpLoc, |
| 10463 | FPFeatures.fp_contract)); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10464 | } |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10465 | |
| 10466 | // FIXME: save results of ADL from here? |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10467 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10468 | // TODO: provide better source location info in DNLoc component. |
| 10469 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10470 | UnresolvedLookupExpr *Fn |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10471 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
| 10472 | NestedNameSpecifierLoc(), OpNameInfo, |
| 10473 | /*ADL*/ true, IsOverloaded(Fns), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10474 | Fns.begin(), Fns.end()); |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10475 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, Args, |
| 10476 | Context.DependentTy, VK_RValue, |
| 10477 | OpLoc, FPFeatures.fp_contract)); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10478 | } |
| 10479 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10480 | // Always do placeholder-like conversions on the RHS. |
| 10481 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 10482 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10483 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 10484 | // Do placeholder-like conversion on the LHS; note that we should |
| 10485 | // not get here with a PseudoObject LHS. |
| 10486 | assert(Args[0]->getObjectKind() != OK_ObjCProperty); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10487 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 10488 | return ExprError(); |
| 10489 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 10490 | // If this is the assignment operator, we only perform overload resolution |
| 10491 | // if the left-hand side is a class or enumeration type. This is actually |
| 10492 | // a hack. The standard requires that we do overload resolution between the |
| 10493 | // various built-in candidates, but as DR507 points out, this can lead to |
| 10494 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 10495 | // Note that we go the traditional code path for compound assignment forms. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10496 | if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10497 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10498 | |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10499 | // If this is the .* operator, which is not overloadable, just |
| 10500 | // create a built-in binary operator. |
| 10501 | if (Opc == BO_PtrMemD) |
| 10502 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 10503 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10504 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10505 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10506 | |
| 10507 | // Add the candidates from the given function set. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10508 | AddFunctionCandidates(Fns, Args, CandidateSet, false); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10509 | |
| 10510 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10511 | AddMemberOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10512 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10513 | // Add candidates from ADL. |
| 10514 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10515 | OpLoc, Args, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10516 | /*ExplicitTemplateArgs*/ 0, |
| 10517 | CandidateSet); |
| 10518 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10519 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10520 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10521 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10522 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10523 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10524 | // Perform overload resolution. |
| 10525 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10526 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 10527 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10528 | // We found a built-in operator or an overloaded operator. |
| 10529 | FunctionDecl *FnDecl = Best->Function; |
| 10530 | |
| 10531 | if (FnDecl) { |
| 10532 | // We matched an overloaded operator. Build a call to that |
| 10533 | // operator. |
| 10534 | |
| 10535 | // Convert the arguments. |
| 10536 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10537 | // Best->Access is only meaningful for class members. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10538 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10539 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10540 | ExprResult Arg1 = |
| 10541 | PerformCopyInitialization( |
| 10542 | InitializedEntity::InitializeParameter(Context, |
| 10543 | FnDecl->getParamDecl(0)), |
| 10544 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10545 | if (Arg1.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10546 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10547 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10548 | ExprResult Arg0 = |
| 10549 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 10550 | Best->FoundDecl, Method); |
| 10551 | if (Arg0.isInvalid()) |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10552 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10553 | Args[0] = Arg0.takeAs<Expr>(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10554 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10555 | } else { |
| 10556 | // Convert the arguments. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10557 | ExprResult Arg0 = PerformCopyInitialization( |
| 10558 | InitializedEntity::InitializeParameter(Context, |
| 10559 | FnDecl->getParamDecl(0)), |
| 10560 | SourceLocation(), Owned(Args[0])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10561 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10562 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10563 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10564 | ExprResult Arg1 = |
| 10565 | PerformCopyInitialization( |
| 10566 | InitializedEntity::InitializeParameter(Context, |
| 10567 | FnDecl->getParamDecl(1)), |
| 10568 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10569 | if (Arg1.isInvalid()) |
| 10570 | return ExprError(); |
| 10571 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 10572 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10573 | } |
| 10574 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10575 | // Determine the result type. |
| 10576 | QualType ResultTy = FnDecl->getResultType(); |
| 10577 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10578 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10579 | |
| 10580 | // Build the actual expression node. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10581 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10582 | Best->FoundDecl, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10583 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10584 | if (FnExpr.isInvalid()) |
| 10585 | return ExprError(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10586 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10587 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10588 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10589 | Args, ResultTy, VK, OpLoc, |
| 10590 | FPFeatures.fp_contract); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10591 | |
| 10592 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 10593 | FnDecl)) |
| 10594 | return ExprError(); |
| 10595 | |
Nick Lewycky | 9b7ea0d | 2013-01-24 02:03:08 +0000 | [diff] [blame] | 10596 | ArrayRef<const Expr *> ArgsArray(Args, 2); |
| 10597 | // Cut off the implicit 'this'. |
| 10598 | if (isa<CXXMethodDecl>(FnDecl)) |
| 10599 | ArgsArray = ArgsArray.slice(1); |
| 10600 | checkCall(FnDecl, ArgsArray, 0, isa<CXXMethodDecl>(FnDecl), OpLoc, |
| 10601 | TheCall->getSourceRange(), VariadicDoesNotApply); |
| 10602 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10603 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10604 | } else { |
| 10605 | // We matched a built-in operator. Convert the arguments, then |
| 10606 | // break out so that we will build the appropriate built-in |
| 10607 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10608 | ExprResult ArgsRes0 = |
| 10609 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 10610 | Best->Conversions[0], AA_Passing); |
| 10611 | if (ArgsRes0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10612 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10613 | Args[0] = ArgsRes0.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10614 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10615 | ExprResult ArgsRes1 = |
| 10616 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 10617 | Best->Conversions[1], AA_Passing); |
| 10618 | if (ArgsRes1.isInvalid()) |
| 10619 | return ExprError(); |
| 10620 | Args[1] = ArgsRes1.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10621 | break; |
| 10622 | } |
| 10623 | } |
| 10624 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10625 | case OR_No_Viable_Function: { |
| 10626 | // C++ [over.match.oper]p9: |
| 10627 | // If the operator is the operator , [...] and there are no |
| 10628 | // viable functions, then the operator is assumed to be the |
| 10629 | // built-in operator and interpreted according to clause 5. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10630 | if (Opc == BO_Comma) |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10631 | break; |
| 10632 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10633 | // For class as left operand for assignment or compound assigment |
| 10634 | // operator do not fall through to handling in built-in, but report that |
| 10635 | // no overloaded assignment operator found |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10636 | ExprResult Result = ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10637 | if (Args[0]->getType()->isRecordType() && |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10638 | Opc >= BO_Assign && Opc <= BO_OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 10639 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 10640 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10641 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10642 | } else { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10643 | // This is an erroneous use of an operator which can be overloaded by |
| 10644 | // a non-member function. Check for non-member operators which were |
| 10645 | // defined too late to be candidates. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10646 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, Args)) |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10647 | // FIXME: Recover by calling the found function. |
| 10648 | return ExprError(); |
| 10649 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10650 | // No viable function; try to create a built-in operation, which will |
| 10651 | // produce an error. Then, show the non-viable candidates. |
| 10652 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 10653 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10654 | assert(Result.isInvalid() && |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10655 | "C++ binary operator overloading is missing candidates!"); |
| 10656 | if (Result.isInvalid()) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10657 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10658 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10659 | return Result; |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10660 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10661 | |
| 10662 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10663 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_binary) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10664 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10665 | << Args[0]->getType() << Args[1]->getType() |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10666 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10667 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10668 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10669 | return ExprError(); |
| 10670 | |
| 10671 | case OR_Deleted: |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10672 | if (isImplicitlyDeleted(Best->Function)) { |
| 10673 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
| 10674 | Diag(OpLoc, diag::err_ovl_deleted_special_oper) |
Richard Smith | 0f46e64 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 10675 | << Context.getRecordType(Method->getParent()) |
| 10676 | << getSpecialMember(Method); |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 10677 | |
Richard Smith | 0f46e64 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 10678 | // The user probably meant to call this special member. Just |
| 10679 | // explain why it's deleted. |
| 10680 | NoteDeletedFunction(Method); |
| 10681 | return ExprError(); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10682 | } else { |
| 10683 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 10684 | << Best->Function->isDeleted() |
| 10685 | << BinaryOperator::getOpcodeStr(Opc) |
| 10686 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 10687 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10688 | } |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10689 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 10690 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10691 | return ExprError(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 10692 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10693 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10694 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10695 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10696 | } |
| 10697 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10698 | ExprResult |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10699 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 10700 | SourceLocation RLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10701 | Expr *Base, Expr *Idx) { |
| 10702 | Expr *Args[2] = { Base, Idx }; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10703 | DeclarationName OpName = |
| 10704 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 10705 | |
| 10706 | // If either side is type-dependent, create an appropriate dependent |
| 10707 | // expression. |
| 10708 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 10709 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10710 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10711 | // CHECKME: no 'operator' keyword? |
| 10712 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
| 10713 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10714 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 10715 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10716 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10717 | /*ADL*/ true, /*Overloaded*/ false, |
| 10718 | UnresolvedSetIterator(), |
| 10719 | UnresolvedSetIterator()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 10720 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10721 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10722 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10723 | Args, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10724 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10725 | VK_RValue, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10726 | RLoc, false)); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10727 | } |
| 10728 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10729 | // Handle placeholders on both operands. |
| 10730 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 10731 | return ExprError(); |
| 10732 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 10733 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10734 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10735 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10736 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10737 | |
| 10738 | // Subscript can only be overloaded as a member function. |
| 10739 | |
| 10740 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10741 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10742 | |
| 10743 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10744 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10745 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10746 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10747 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10748 | // Perform overload resolution. |
| 10749 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10750 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10751 | case OR_Success: { |
| 10752 | // We found a built-in operator or an overloaded operator. |
| 10753 | FunctionDecl *FnDecl = Best->Function; |
| 10754 | |
| 10755 | if (FnDecl) { |
| 10756 | // We matched an overloaded operator. Build a call to that |
| 10757 | // operator. |
| 10758 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10759 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10760 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10761 | // Convert the arguments. |
| 10762 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10763 | ExprResult Arg0 = |
| 10764 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 10765 | Best->FoundDecl, Method); |
| 10766 | if (Arg0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10767 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10768 | Args[0] = Arg0.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10769 | |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10770 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10771 | ExprResult InputInit |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10772 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10773 | Context, |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10774 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10775 | SourceLocation(), |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10776 | Owned(Args[1])); |
| 10777 | if (InputInit.isInvalid()) |
| 10778 | return ExprError(); |
| 10779 | |
| 10780 | Args[1] = InputInit.takeAs<Expr>(); |
| 10781 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10782 | // Determine the result type |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10783 | QualType ResultTy = FnDecl->getResultType(); |
| 10784 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10785 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10786 | |
| 10787 | // Build the actual expression node. |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10788 | DeclarationNameInfo OpLocInfo(OpName, LLoc); |
| 10789 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10790 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10791 | Best->FoundDecl, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10792 | HadMultipleCandidates, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10793 | OpLocInfo.getLoc(), |
| 10794 | OpLocInfo.getInfo()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10795 | if (FnExpr.isInvalid()) |
| 10796 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10797 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10798 | CXXOperatorCallExpr *TheCall = |
| 10799 | new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10800 | FnExpr.take(), Args, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10801 | ResultTy, VK, RLoc, |
| 10802 | false); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10803 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10804 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10805 | FnDecl)) |
| 10806 | return ExprError(); |
| 10807 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10808 | return MaybeBindToTemporary(TheCall); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10809 | } else { |
| 10810 | // We matched a built-in operator. Convert the arguments, then |
| 10811 | // break out so that we will build the appropriate built-in |
| 10812 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10813 | ExprResult ArgsRes0 = |
| 10814 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 10815 | Best->Conversions[0], AA_Passing); |
| 10816 | if (ArgsRes0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10817 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10818 | Args[0] = ArgsRes0.take(); |
| 10819 | |
| 10820 | ExprResult ArgsRes1 = |
| 10821 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 10822 | Best->Conversions[1], AA_Passing); |
| 10823 | if (ArgsRes1.isInvalid()) |
| 10824 | return ExprError(); |
| 10825 | Args[1] = ArgsRes1.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10826 | |
| 10827 | break; |
| 10828 | } |
| 10829 | } |
| 10830 | |
| 10831 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10832 | if (CandidateSet.empty()) |
| 10833 | Diag(LLoc, diag::err_ovl_no_oper) |
| 10834 | << Args[0]->getType() << /*subscript*/ 0 |
| 10835 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10836 | else |
| 10837 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 10838 | << Args[0]->getType() |
| 10839 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10840 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10841 | "[]", LLoc); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10842 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10843 | } |
| 10844 | |
| 10845 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10846 | Diag(LLoc, diag::err_ovl_ambiguous_oper_binary) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10847 | << "[]" |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10848 | << Args[0]->getType() << Args[1]->getType() |
| 10849 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10850 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10851 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10852 | return ExprError(); |
| 10853 | |
| 10854 | case OR_Deleted: |
| 10855 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 10856 | << Best->Function->isDeleted() << "[]" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 10857 | << getDeletedOrUnavailableSuffix(Best->Function) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10858 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10859 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10860 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10861 | return ExprError(); |
| 10862 | } |
| 10863 | |
| 10864 | // We matched a built-in operator; build it. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10865 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10866 | } |
| 10867 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10868 | /// BuildCallToMemberFunction - Build a call to a member |
| 10869 | /// function. MemExpr is the expression that refers to the member |
| 10870 | /// function (and includes the object parameter), Args/NumArgs are the |
| 10871 | /// arguments to the function call (not including the object |
| 10872 | /// parameter). The caller needs to validate that the member |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10873 | /// expression refers to a non-static member function or an overloaded |
| 10874 | /// member function. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10875 | ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10876 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10877 | SourceLocation LParenLoc, |
| 10878 | MultiExprArg Args, |
| 10879 | SourceLocation RParenLoc) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10880 | assert(MemExprE->getType() == Context.BoundMemberTy || |
| 10881 | MemExprE->getType() == Context.OverloadTy); |
| 10882 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10883 | // Dig out the member expression. This holds both the object |
| 10884 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10885 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10886 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10887 | // Determine whether this is a call to a pointer-to-member function. |
| 10888 | if (BinaryOperator *op = dyn_cast<BinaryOperator>(NakedMemExpr)) { |
| 10889 | assert(op->getType() == Context.BoundMemberTy); |
| 10890 | assert(op->getOpcode() == BO_PtrMemD || op->getOpcode() == BO_PtrMemI); |
| 10891 | |
| 10892 | QualType fnType = |
| 10893 | op->getRHS()->getType()->castAs<MemberPointerType>()->getPointeeType(); |
| 10894 | |
| 10895 | const FunctionProtoType *proto = fnType->castAs<FunctionProtoType>(); |
| 10896 | QualType resultType = proto->getCallResultType(Context); |
| 10897 | ExprValueKind valueKind = Expr::getValueKindForType(proto->getResultType()); |
| 10898 | |
| 10899 | // Check that the object type isn't more qualified than the |
| 10900 | // member function we're calling. |
| 10901 | Qualifiers funcQuals = Qualifiers::fromCVRMask(proto->getTypeQuals()); |
| 10902 | |
| 10903 | QualType objectType = op->getLHS()->getType(); |
| 10904 | if (op->getOpcode() == BO_PtrMemI) |
| 10905 | objectType = objectType->castAs<PointerType>()->getPointeeType(); |
| 10906 | Qualifiers objectQuals = objectType.getQualifiers(); |
| 10907 | |
| 10908 | Qualifiers difference = objectQuals - funcQuals; |
| 10909 | difference.removeObjCGCAttr(); |
| 10910 | difference.removeAddressSpace(); |
| 10911 | if (difference) { |
| 10912 | std::string qualsString = difference.getAsString(); |
| 10913 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
| 10914 | << fnType.getUnqualifiedType() |
| 10915 | << qualsString |
| 10916 | << (qualsString.find(' ') == std::string::npos ? 1 : 2); |
| 10917 | } |
| 10918 | |
| 10919 | CXXMemberCallExpr *call |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10920 | = new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10921 | resultType, valueKind, RParenLoc); |
| 10922 | |
| 10923 | if (CheckCallReturnType(proto->getResultType(), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10924 | op->getRHS()->getLocStart(), |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10925 | call, 0)) |
| 10926 | return ExprError(); |
| 10927 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10928 | if (ConvertArgumentsForCall(call, op, 0, proto, Args, RParenLoc)) |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10929 | return ExprError(); |
| 10930 | |
| 10931 | return MaybeBindToTemporary(call); |
| 10932 | } |
| 10933 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10934 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10935 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10936 | return ExprError(); |
| 10937 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10938 | MemberExpr *MemExpr; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10939 | CXXMethodDecl *Method = 0; |
John McCall | bb6fb46 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 10940 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 10941 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10942 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 10943 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10944 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10945 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 10946 | Qualifier = MemExpr->getQualifier(); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10947 | UnbridgedCasts.restore(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10948 | } else { |
| 10949 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 10950 | Qualifier = UnresExpr->getQualifier(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10951 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 10952 | QualType ObjectType = UnresExpr->getBaseType(); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 10953 | Expr::Classification ObjectClassification |
| 10954 | = UnresExpr->isArrow()? Expr::Classification::makeSimpleLValue() |
| 10955 | : UnresExpr->getBase()->Classify(Context); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10956 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10957 | // Add overload candidates |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10958 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10959 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10960 | // FIXME: avoid copy. |
| 10961 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 10962 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 10963 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 10964 | TemplateArgs = &TemplateArgsBuffer; |
| 10965 | } |
| 10966 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10967 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 10968 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 10969 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 10970 | NamedDecl *Func = *I; |
| 10971 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 10972 | if (isa<UsingShadowDecl>(Func)) |
| 10973 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 10974 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 10975 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 10976 | // Microsoft supports direct constructor calls. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10977 | if (getLangOpts().MicrosoftExt && isa<CXXConstructorDecl>(Func)) { |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10978 | AddOverloadCandidate(cast<CXXConstructorDecl>(Func), I.getPair(), |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10979 | Args, CandidateSet); |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 10980 | } else if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 10981 | // If explicit template arguments were provided, we can't call a |
| 10982 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10983 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 10984 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10985 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10986 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10987 | ObjectClassification, Args, CandidateSet, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 10988 | /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 10989 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10990 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10991 | I.getPair(), ActingDC, TemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10992 | ObjectType, ObjectClassification, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10993 | Args, CandidateSet, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 10994 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 10995 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 10996 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10997 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10998 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 10999 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11000 | UnbridgedCasts.restore(); |
| 11001 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11002 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11003 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getLocStart(), |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 11004 | Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11005 | case OR_Success: |
| 11006 | Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11007 | FoundDecl = Best->FoundDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11008 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 11009 | if (DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc())) |
| 11010 | return ExprError(); |
Faisal Vali | d570a92 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 11011 | // If FoundDecl is different from Method (such as if one is a template |
| 11012 | // and the other a specialization), make sure DiagnoseUseOfDecl is |
| 11013 | // called on both. |
| 11014 | // FIXME: This would be more comprehensively addressed by modifying |
| 11015 | // DiagnoseUseOfDecl to accept both the FoundDecl and the decl |
| 11016 | // being used. |
| 11017 | if (Method != FoundDecl.getDecl() && |
| 11018 | DiagnoseUseOfDecl(Method, UnresExpr->getNameLoc())) |
| 11019 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11020 | break; |
| 11021 | |
| 11022 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11023 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11024 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 11025 | << DeclName << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11026 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11027 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11028 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11029 | |
| 11030 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11031 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 11032 | << DeclName << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11033 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11034 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11035 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11036 | |
| 11037 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11038 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11039 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11040 | << DeclName |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11041 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11042 | << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11043 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11044 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11045 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11046 | } |
| 11047 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11048 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11049 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11050 | // If overload resolution picked a static member, build a |
| 11051 | // non-member call based on that function. |
| 11052 | if (Method->isStatic()) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11053 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, Args, |
| 11054 | RParenLoc); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11055 | } |
| 11056 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11057 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11058 | } |
| 11059 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11060 | QualType ResultType = Method->getResultType(); |
| 11061 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 11062 | ResultType = ResultType.getNonLValueExprType(Context); |
| 11063 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11064 | assert(Method && "Member call to something that isn't a method?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11065 | CXXMemberCallExpr *TheCall = |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11066 | new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11067 | ResultType, VK, RParenLoc); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11068 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 11069 | // Check for a valid return type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11070 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11071 | TheCall, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11072 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11073 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11074 | // Convert the object argument (for a non-static member function call). |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11075 | // We only need to do this if there was actually an overload; otherwise |
| 11076 | // it was done at lookup. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11077 | if (!Method->isStatic()) { |
| 11078 | ExprResult ObjectArg = |
| 11079 | PerformObjectArgumentInitialization(MemExpr->getBase(), Qualifier, |
| 11080 | FoundDecl, Method); |
| 11081 | if (ObjectArg.isInvalid()) |
| 11082 | return ExprError(); |
| 11083 | MemExpr->setBase(ObjectArg.take()); |
| 11084 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11085 | |
| 11086 | // Convert the rest of the arguments |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11087 | const FunctionProtoType *Proto = |
| 11088 | Method->getType()->getAs<FunctionProtoType>(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11089 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11090 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11091 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11092 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11093 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11094 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11095 | if (CheckFunctionCall(Method, TheCall, Proto)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11096 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 11097 | |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11098 | if ((isa<CXXConstructorDecl>(CurContext) || |
| 11099 | isa<CXXDestructorDecl>(CurContext)) && |
| 11100 | TheCall->getMethodDecl()->isPure()) { |
| 11101 | const CXXMethodDecl *MD = TheCall->getMethodDecl(); |
| 11102 | |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 11103 | if (isa<CXXThisExpr>(MemExpr->getBase()->IgnoreParenCasts())) { |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11104 | Diag(MemExpr->getLocStart(), |
| 11105 | diag::warn_call_to_pure_virtual_member_function_from_ctor_dtor) |
| 11106 | << MD->getDeclName() << isa<CXXDestructorDecl>(CurContext) |
| 11107 | << MD->getParent()->getDeclName(); |
| 11108 | |
| 11109 | Diag(MD->getLocStart(), diag::note_previous_decl) << MD->getDeclName(); |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 11110 | } |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11111 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11112 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11113 | } |
| 11114 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11115 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 11116 | /// type (C++ [over.call.object]), which can end up invoking an |
| 11117 | /// overloaded function call operator (@c operator()) or performing a |
| 11118 | /// user-defined conversion on the object argument. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 11119 | ExprResult |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11120 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 11121 | SourceLocation LParenLoc, |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11122 | MultiExprArg Args, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11123 | SourceLocation RParenLoc) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11124 | if (checkPlaceholderForOverload(*this, Obj)) |
| 11125 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11126 | ExprResult Object = Owned(Obj); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11127 | |
| 11128 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11129 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11130 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11131 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11132 | assert(Object.get()->getType()->isRecordType() && "Requires object type argument"); |
| 11133 | const RecordType *Record = Object.get()->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11134 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11135 | // C++ [over.call.object]p1: |
| 11136 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 11137 | // 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] | 11138 | // candidate functions includes at least the function call |
| 11139 | // operators of T. The function call operators of T are obtained by |
| 11140 | // ordinary lookup of the name operator() in the context of |
| 11141 | // (E).operator(). |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11142 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 11143 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11144 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11145 | if (RequireCompleteType(LParenLoc, Object.get()->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11146 | diag::err_incomplete_object_call, Object.get())) |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11147 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11148 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 11149 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 11150 | LookupQualifiedName(R, Record->getDecl()); |
| 11151 | R.suppressDiagnostics(); |
| 11152 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11153 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 11154 | Oper != OperEnd; ++Oper) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11155 | AddMethodCandidate(Oper.getPair(), Object.get()->getType(), |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11156 | Object.get()->Classify(Context), |
| 11157 | Args, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 11158 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 11159 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11160 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11161 | // C++ [over.call.object]p2: |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11162 | // In addition, for each (non-explicit in C++0x) conversion function |
| 11163 | // declared in T of the form |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11164 | // |
| 11165 | // operator conversion-type-id () cv-qualifier; |
| 11166 | // |
| 11167 | // where cv-qualifier is the same cv-qualification as, or a |
| 11168 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 11169 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 11170 | // R", or the type "reference to pointer to function of |
| 11171 | // (P1,...,Pn) returning R", or the type "reference to function |
| 11172 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11173 | // is also considered as a candidate function. Similarly, |
| 11174 | // surrogate call functions are added to the set of candidate |
| 11175 | // functions for each conversion function declared in an |
| 11176 | // accessible base class provided the function is not hidden |
| 11177 | // within T by another intervening declaration. |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 11178 | std::pair<CXXRecordDecl::conversion_iterator, |
| 11179 | CXXRecordDecl::conversion_iterator> Conversions |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 11180 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 11181 | for (CXXRecordDecl::conversion_iterator |
| 11182 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11183 | NamedDecl *D = *I; |
| 11184 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 11185 | if (isa<UsingShadowDecl>(D)) |
| 11186 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11187 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 11188 | // Skip over templated conversion functions; they aren't |
| 11189 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11190 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 11191 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 11192 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11193 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11194 | if (!Conv->isExplicit()) { |
| 11195 | // Strip the reference type (if any) and then the pointer type (if |
| 11196 | // any) to get down to what might be a function type. |
| 11197 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 11198 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 11199 | ConvType = ConvPtrType->getPointeeType(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11200 | |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11201 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
| 11202 | { |
| 11203 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11204 | Object.get(), Args, CandidateSet); |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11205 | } |
| 11206 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11207 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11208 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11209 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11210 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11211 | // Perform overload resolution. |
| 11212 | OverloadCandidateSet::iterator Best; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11213 | switch (CandidateSet.BestViableFunction(*this, Object.get()->getLocStart(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11214 | Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11215 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11216 | // Overload resolution succeeded; we'll build the appropriate call |
| 11217 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11218 | break; |
| 11219 | |
| 11220 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11221 | if (CandidateSet.empty()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11222 | Diag(Object.get()->getLocStart(), diag::err_ovl_no_oper) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11223 | << Object.get()->getType() << /*call*/ 1 |
| 11224 | << Object.get()->getSourceRange(); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11225 | else |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11226 | Diag(Object.get()->getLocStart(), |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11227 | diag::err_ovl_no_viable_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11228 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11229 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11230 | break; |
| 11231 | |
| 11232 | case OR_Ambiguous: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11233 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11234 | diag::err_ovl_ambiguous_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11235 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11236 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11237 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11238 | |
| 11239 | case OR_Deleted: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11240 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11241 | diag::err_ovl_deleted_object_call) |
| 11242 | << Best->Function->isDeleted() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11243 | << Object.get()->getType() |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11244 | << getDeletedOrUnavailableSuffix(Best->Function) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11245 | << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11246 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11247 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11248 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11249 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 11250 | if (Best == CandidateSet.end()) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11251 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11252 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11253 | UnbridgedCasts.restore(); |
| 11254 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11255 | if (Best->Function == 0) { |
| 11256 | // Since there is no function declaration, this is one of the |
| 11257 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11258 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11259 | = cast<CXXConversionDecl>( |
| 11260 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 11261 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11262 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 11263 | if (DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc)) |
| 11264 | return ExprError(); |
Faisal Vali | d570a92 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 11265 | assert(Conv == Best->FoundDecl.getDecl() && |
| 11266 | "Found Decl & conversion-to-functionptr should be same, right?!"); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11267 | // We selected one of the surrogate functions that converts the |
| 11268 | // object parameter to a function pointer. Perform the conversion |
| 11269 | // on the object argument, then let ActOnCallExpr finish the job. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11270 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 11271 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 11272 | // and then call it. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11273 | ExprResult Call = BuildCXXMemberCallExpr(Object.get(), Best->FoundDecl, |
| 11274 | Conv, HadMultipleCandidates); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 11275 | if (Call.isInvalid()) |
| 11276 | return ExprError(); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 11277 | // Record usage of conversion in an implicit cast. |
| 11278 | Call = Owned(ImplicitCastExpr::Create(Context, Call.get()->getType(), |
| 11279 | CK_UserDefinedConversion, |
| 11280 | Call.get(), 0, VK_RValue)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11281 | |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11282 | return ActOnCallExpr(S, Call.get(), LParenLoc, Args, RParenLoc); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11283 | } |
| 11284 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11285 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 11286 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11287 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 11288 | // that calls this method, using Object for the implicit object |
| 11289 | // parameter and passing along the remaining arguments. |
| 11290 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Nico Weber | e0ff690 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 11291 | |
| 11292 | // An error diagnostic has already been printed when parsing the declaration. |
Nico Weber | 1a52a4d | 2012-11-09 08:38:04 +0000 | [diff] [blame] | 11293 | if (Method->isInvalidDecl()) |
Nico Weber | e0ff690 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 11294 | return ExprError(); |
| 11295 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11296 | const FunctionProtoType *Proto = |
| 11297 | Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11298 | |
| 11299 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11300 | unsigned NumArgsToCheck = Args.size(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11301 | |
| 11302 | // Build the full argument list for the method call (the |
| 11303 | // implicit object parameter is placed at the beginning of the |
| 11304 | // list). |
| 11305 | Expr **MethodArgs; |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11306 | if (Args.size() < NumArgsInProto) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11307 | NumArgsToCheck = NumArgsInProto; |
| 11308 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 11309 | } else { |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11310 | MethodArgs = new Expr*[Args.size() + 1]; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11311 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11312 | MethodArgs[0] = Object.get(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11313 | for (unsigned ArgIdx = 0, e = Args.size(); ArgIdx != e; ++ArgIdx) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11314 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11315 | |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11316 | DeclarationNameInfo OpLocInfo( |
| 11317 | Context.DeclarationNames.getCXXOperatorName(OO_Call), LParenLoc); |
| 11318 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LParenLoc, RParenLoc)); |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11319 | ExprResult NewFn = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11320 | HadMultipleCandidates, |
| 11321 | OpLocInfo.getLoc(), |
| 11322 | OpLocInfo.getInfo()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11323 | if (NewFn.isInvalid()) |
| 11324 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11325 | |
| 11326 | // Once we've built TheCall, all of the expressions are properly |
| 11327 | // owned. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11328 | QualType ResultTy = Method->getResultType(); |
| 11329 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11330 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11331 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11332 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11333 | new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn.take(), |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11334 | llvm::makeArrayRef(MethodArgs, Args.size()+1), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 11335 | ResultTy, VK, RParenLoc, false); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11336 | delete [] MethodArgs; |
| 11337 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11338 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall, |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 11339 | Method)) |
| 11340 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11341 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11342 | // We may have default arguments. If so, we need to allocate more |
| 11343 | // slots in the call for them. |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11344 | if (Args.size() < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 11345 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11346 | else if (Args.size() > NumArgsInProto) |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11347 | NumArgsToCheck = NumArgsInProto; |
| 11348 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11349 | bool IsError = false; |
| 11350 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11351 | // Initialize the implicit object parameter. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11352 | ExprResult ObjRes = |
| 11353 | PerformObjectArgumentInitialization(Object.get(), /*Qualifier=*/0, |
| 11354 | Best->FoundDecl, Method); |
| 11355 | if (ObjRes.isInvalid()) |
| 11356 | IsError = true; |
| 11357 | else |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11358 | Object = ObjRes; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11359 | TheCall->setArg(0, Object.take()); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11360 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11361 | // Check the argument types. |
| 11362 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11363 | Expr *Arg; |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11364 | if (i < Args.size()) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11365 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11366 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11367 | // Pass the argument. |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11368 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11369 | ExprResult InputInit |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11370 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 11371 | Context, |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11372 | Method->getParamDecl(i)), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11373 | SourceLocation(), Arg); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11374 | |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11375 | IsError |= InputInit.isInvalid(); |
| 11376 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11377 | } else { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11378 | ExprResult DefArg |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 11379 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 11380 | if (DefArg.isInvalid()) { |
| 11381 | IsError = true; |
| 11382 | break; |
| 11383 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11384 | |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 11385 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11386 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11387 | |
| 11388 | TheCall->setArg(i + 1, Arg); |
| 11389 | } |
| 11390 | |
| 11391 | // If this is a variadic call, handle args passed through "...". |
| 11392 | if (Proto->isVariadic()) { |
| 11393 | // Promote the arguments (C99 6.5.2.2p7). |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11394 | for (unsigned i = NumArgsInProto, e = Args.size(); i < e; i++) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11395 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0); |
| 11396 | IsError |= Arg.isInvalid(); |
| 11397 | TheCall->setArg(i + 1, Arg.take()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11398 | } |
| 11399 | } |
| 11400 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11401 | if (IsError) return true; |
| 11402 | |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11403 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11404 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11405 | if (CheckFunctionCall(Method, TheCall, Proto)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 11406 | return true; |
| 11407 | |
John McCall | 182f709 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 11408 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11409 | } |
| 11410 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11411 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11412 | /// (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] | 11413 | /// @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] | 11414 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11415 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc) { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11416 | assert(Base->getType()->isRecordType() && |
| 11417 | "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11418 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11419 | if (checkPlaceholderForOverload(*this, Base)) |
| 11420 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11421 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11422 | SourceLocation Loc = Base->getExprLoc(); |
| 11423 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11424 | // C++ [over.ref]p1: |
| 11425 | // |
| 11426 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 11427 | // for a class object x of type T if T::operator->() exists and if |
| 11428 | // the operator is selected as the best match function by the |
| 11429 | // overload resolution mechanism (13.3). |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11430 | DeclarationName OpName = |
| 11431 | Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11432 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 11433 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11434 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11435 | if (RequireCompleteType(Loc, Base->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11436 | diag::err_typecheck_incomplete_tag, Base)) |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 11437 | return ExprError(); |
| 11438 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 11439 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 11440 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 11441 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 11442 | |
| 11443 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11444 | Oper != OperEnd; ++Oper) { |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11445 | AddMethodCandidate(Oper.getPair(), Base->getType(), Base->Classify(Context), |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 11446 | None, CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11447 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11448 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11449 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11450 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11451 | // Perform overload resolution. |
| 11452 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11453 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11454 | case OR_Success: |
| 11455 | // Overload resolution succeeded; we'll build the call below. |
| 11456 | break; |
| 11457 | |
| 11458 | case OR_No_Viable_Function: |
| 11459 | if (CandidateSet.empty()) |
| 11460 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11461 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11462 | else |
| 11463 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11464 | << "operator->" << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11465 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11466 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11467 | |
| 11468 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 11469 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 11470 | << "->" << Base->getType() << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11471 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11472 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11473 | |
| 11474 | case OR_Deleted: |
| 11475 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 11476 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11477 | << "->" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11478 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11479 | << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11480 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11481 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11482 | } |
| 11483 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11484 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
| 11485 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11486 | // Convert the object parameter. |
| 11487 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11488 | ExprResult BaseResult = |
| 11489 | PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 11490 | Best->FoundDecl, Method); |
| 11491 | if (BaseResult.isInvalid()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11492 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11493 | Base = BaseResult.take(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 11494 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11495 | // Build the operator call. |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11496 | ExprResult FnExpr = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11497 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11498 | if (FnExpr.isInvalid()) |
| 11499 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11500 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11501 | QualType ResultTy = Method->getResultType(); |
| 11502 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11503 | ResultTy = ResultTy.getNonLValueExprType(Context); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11504 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11505 | new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr.take(), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 11506 | Base, ResultTy, VK, OpLoc, false); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 11507 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11508 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 11509 | Method)) |
| 11510 | return ExprError(); |
Eli Friedman | d593190 | 2011-04-04 01:18:25 +0000 | [diff] [blame] | 11511 | |
| 11512 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11513 | } |
| 11514 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11515 | /// BuildLiteralOperatorCall - Build a UserDefinedLiteral by creating a call to |
| 11516 | /// a literal operator described by the provided lookup results. |
| 11517 | ExprResult Sema::BuildLiteralOperatorCall(LookupResult &R, |
| 11518 | DeclarationNameInfo &SuffixInfo, |
| 11519 | ArrayRef<Expr*> Args, |
| 11520 | SourceLocation LitEndLoc, |
| 11521 | TemplateArgumentListInfo *TemplateArgs) { |
| 11522 | SourceLocation UDSuffixLoc = SuffixInfo.getCXXLiteralOperatorNameLoc(); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11523 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11524 | OverloadCandidateSet CandidateSet(UDSuffixLoc); |
| 11525 | AddFunctionCandidates(R.asUnresolvedSet(), Args, CandidateSet, true, |
| 11526 | TemplateArgs); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11527 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11528 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11529 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11530 | // Perform overload resolution. This will usually be trivial, but might need |
| 11531 | // to perform substitutions for a literal operator template. |
| 11532 | OverloadCandidateSet::iterator Best; |
| 11533 | switch (CandidateSet.BestViableFunction(*this, UDSuffixLoc, Best)) { |
| 11534 | case OR_Success: |
| 11535 | case OR_Deleted: |
| 11536 | break; |
| 11537 | |
| 11538 | case OR_No_Viable_Function: |
| 11539 | Diag(UDSuffixLoc, diag::err_ovl_no_viable_function_in_call) |
| 11540 | << R.getLookupName(); |
| 11541 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
| 11542 | return ExprError(); |
| 11543 | |
| 11544 | case OR_Ambiguous: |
| 11545 | Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) << R.getLookupName(); |
| 11546 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
| 11547 | return ExprError(); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11548 | } |
| 11549 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11550 | FunctionDecl *FD = Best->Function; |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11551 | ExprResult Fn = CreateFunctionRefExpr(*this, FD, Best->FoundDecl, |
| 11552 | HadMultipleCandidates, |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11553 | SuffixInfo.getLoc(), |
| 11554 | SuffixInfo.getInfo()); |
| 11555 | if (Fn.isInvalid()) |
| 11556 | return true; |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11557 | |
| 11558 | // Check the argument types. This should almost always be a no-op, except |
| 11559 | // that array-to-pointer decay is applied to string literals. |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11560 | Expr *ConvArgs[2]; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 11561 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11562 | ExprResult InputInit = PerformCopyInitialization( |
| 11563 | InitializedEntity::InitializeParameter(Context, FD->getParamDecl(ArgIdx)), |
| 11564 | SourceLocation(), Args[ArgIdx]); |
| 11565 | if (InputInit.isInvalid()) |
| 11566 | return true; |
| 11567 | ConvArgs[ArgIdx] = InputInit.take(); |
| 11568 | } |
| 11569 | |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11570 | QualType ResultTy = FD->getResultType(); |
| 11571 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11572 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11573 | |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11574 | UserDefinedLiteral *UDL = |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 11575 | new (Context) UserDefinedLiteral(Context, Fn.take(), |
| 11576 | llvm::makeArrayRef(ConvArgs, Args.size()), |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11577 | ResultTy, VK, LitEndLoc, UDSuffixLoc); |
| 11578 | |
| 11579 | if (CheckCallReturnType(FD->getResultType(), UDSuffixLoc, UDL, FD)) |
| 11580 | return ExprError(); |
| 11581 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11582 | if (CheckFunctionCall(FD, UDL, NULL)) |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11583 | return ExprError(); |
| 11584 | |
| 11585 | return MaybeBindToTemporary(UDL); |
| 11586 | } |
| 11587 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11588 | /// Build a call to 'begin' or 'end' for a C++11 for-range statement. If the |
| 11589 | /// given LookupResult is non-empty, it is assumed to describe a member which |
| 11590 | /// will be invoked. Otherwise, the function will be found via argument |
| 11591 | /// dependent lookup. |
| 11592 | /// CallExpr is set to a valid expression and FRS_Success returned on success, |
| 11593 | /// otherwise CallExpr is set to ExprError() and some non-success value |
| 11594 | /// is returned. |
| 11595 | Sema::ForRangeStatus |
| 11596 | Sema::BuildForRangeBeginEndCall(Scope *S, SourceLocation Loc, |
| 11597 | SourceLocation RangeLoc, VarDecl *Decl, |
| 11598 | BeginEndFunction BEF, |
| 11599 | const DeclarationNameInfo &NameInfo, |
| 11600 | LookupResult &MemberLookup, |
| 11601 | OverloadCandidateSet *CandidateSet, |
| 11602 | Expr *Range, ExprResult *CallExpr) { |
| 11603 | CandidateSet->clear(); |
| 11604 | if (!MemberLookup.empty()) { |
| 11605 | ExprResult MemberRef = |
| 11606 | BuildMemberReferenceExpr(Range, Range->getType(), Loc, |
| 11607 | /*IsPtr=*/false, CXXScopeSpec(), |
| 11608 | /*TemplateKWLoc=*/SourceLocation(), |
| 11609 | /*FirstQualifierInScope=*/0, |
| 11610 | MemberLookup, |
| 11611 | /*TemplateArgs=*/0); |
| 11612 | if (MemberRef.isInvalid()) { |
| 11613 | *CallExpr = ExprError(); |
| 11614 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11615 | << RangeLoc << BEF << Range->getType(); |
| 11616 | return FRS_DiagnosticIssued; |
| 11617 | } |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 11618 | *CallExpr = ActOnCallExpr(S, MemberRef.get(), Loc, None, Loc, 0); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11619 | if (CallExpr->isInvalid()) { |
| 11620 | *CallExpr = ExprError(); |
| 11621 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11622 | << RangeLoc << BEF << Range->getType(); |
| 11623 | return FRS_DiagnosticIssued; |
| 11624 | } |
| 11625 | } else { |
| 11626 | UnresolvedSet<0> FoundNames; |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11627 | UnresolvedLookupExpr *Fn = |
| 11628 | UnresolvedLookupExpr::Create(Context, /*NamingClass=*/0, |
| 11629 | NestedNameSpecifierLoc(), NameInfo, |
| 11630 | /*NeedsADL=*/true, /*Overloaded=*/false, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 11631 | FoundNames.begin(), FoundNames.end()); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11632 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11633 | bool CandidateSetError = buildOverloadedCallSet(S, Fn, Fn, Range, Loc, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11634 | CandidateSet, CallExpr); |
| 11635 | if (CandidateSet->empty() || CandidateSetError) { |
| 11636 | *CallExpr = ExprError(); |
| 11637 | return FRS_NoViableFunction; |
| 11638 | } |
| 11639 | OverloadCandidateSet::iterator Best; |
| 11640 | OverloadingResult OverloadResult = |
| 11641 | CandidateSet->BestViableFunction(*this, Fn->getLocStart(), Best); |
| 11642 | |
| 11643 | if (OverloadResult == OR_No_Viable_Function) { |
| 11644 | *CallExpr = ExprError(); |
| 11645 | return FRS_NoViableFunction; |
| 11646 | } |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11647 | *CallExpr = FinishOverloadedCallExpr(*this, S, Fn, Fn, Loc, Range, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11648 | Loc, 0, CandidateSet, &Best, |
| 11649 | OverloadResult, |
| 11650 | /*AllowTypoCorrection=*/false); |
| 11651 | if (CallExpr->isInvalid() || OverloadResult != OR_Success) { |
| 11652 | *CallExpr = ExprError(); |
| 11653 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11654 | << RangeLoc << BEF << Range->getType(); |
| 11655 | return FRS_DiagnosticIssued; |
| 11656 | } |
| 11657 | } |
| 11658 | return FRS_Success; |
| 11659 | } |
| 11660 | |
| 11661 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11662 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 11663 | /// a C++ overloaded function (possibly with some parentheses and |
| 11664 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 11665 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 11666 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 11667 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11668 | FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11669 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11670 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 11671 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11672 | if (SubExpr == PE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11673 | return PE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11674 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11675 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11676 | } |
| 11677 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11678 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11679 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 11680 | Found, Fn); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11681 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11682 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 11683 | "Implicit cast type cannot be determined from overload"); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 11684 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11685 | if (SubExpr == ICE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11686 | return ICE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11687 | |
| 11688 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 11689 | ICE->getCastKind(), |
| 11690 | SubExpr, 0, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 11691 | ICE->getValueKind()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11692 | } |
| 11693 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11694 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11695 | assert(UnOp->getOpcode() == UO_AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11696 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 11697 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 11698 | if (Method->isStatic()) { |
| 11699 | // Do nothing: static member functions aren't any different |
| 11700 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11701 | } else { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11702 | // Fix the sub expression, which really has to be an |
| 11703 | // UnresolvedLookupExpr holding an overloaded member function |
| 11704 | // or template. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11705 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 11706 | Found, Fn); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11707 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11708 | return UnOp; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11709 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11710 | assert(isa<DeclRefExpr>(SubExpr) |
| 11711 | && "fixed to something other than a decl ref"); |
| 11712 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 11713 | && "fixed to a member ref with no nested name qualifier"); |
| 11714 | |
| 11715 | // We have taken the address of a pointer to member |
| 11716 | // function. Perform the computation here so that we get the |
| 11717 | // appropriate pointer to member type. |
| 11718 | QualType ClassType |
| 11719 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 11720 | QualType MemPtrType |
| 11721 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 11722 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11723 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, MemPtrType, |
| 11724 | VK_RValue, OK_Ordinary, |
| 11725 | UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 11726 | } |
| 11727 | } |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11728 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 11729 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11730 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11731 | return UnOp; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11732 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11733 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11734 | Context.getPointerType(SubExpr->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11735 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11736 | UnOp->getOperatorLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11737 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11738 | |
| 11739 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11740 | // FIXME: avoid copy. |
| 11741 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11742 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11743 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11744 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11745 | } |
| 11746 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11747 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 11748 | ULE->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11749 | ULE->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11750 | Fn, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 11751 | /*enclosing*/ false, // FIXME? |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11752 | ULE->getNameLoc(), |
| 11753 | Fn->getType(), |
| 11754 | VK_LValue, |
| 11755 | Found.getDecl(), |
| 11756 | TemplateArgs); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 11757 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11758 | DRE->setHadMultipleCandidates(ULE->getNumDecls() > 1); |
| 11759 | return DRE; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11760 | } |
| 11761 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11762 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11763 | // FIXME: avoid copy. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11764 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 11765 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 11766 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11767 | TemplateArgs = &TemplateArgsBuffer; |
| 11768 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11769 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11770 | Expr *Base; |
| 11771 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11772 | // If we're filling in a static method where we used to have an |
| 11773 | // implicit member access, rewrite to a simple decl ref. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11774 | if (MemExpr->isImplicitAccess()) { |
| 11775 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11776 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 11777 | MemExpr->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11778 | MemExpr->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11779 | Fn, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 11780 | /*enclosing*/ false, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11781 | MemExpr->getMemberLoc(), |
| 11782 | Fn->getType(), |
| 11783 | VK_LValue, |
| 11784 | Found.getDecl(), |
| 11785 | TemplateArgs); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 11786 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11787 | DRE->setHadMultipleCandidates(MemExpr->getNumDecls() > 1); |
| 11788 | return DRE; |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 11789 | } else { |
| 11790 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 11791 | if (MemExpr->getQualifier()) |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 11792 | Loc = MemExpr->getQualifierLoc().getBeginLoc(); |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 11793 | CheckCXXThisCapture(Loc); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 11794 | Base = new (Context) CXXThisExpr(Loc, |
| 11795 | MemExpr->getBaseType(), |
| 11796 | /*isImplicit=*/true); |
| 11797 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11798 | } else |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11799 | Base = MemExpr->getBase(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11800 | |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11801 | ExprValueKind valueKind; |
| 11802 | QualType type; |
| 11803 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 11804 | valueKind = VK_LValue; |
| 11805 | type = Fn->getType(); |
| 11806 | } else { |
| 11807 | valueKind = VK_RValue; |
| 11808 | type = Context.BoundMemberTy; |
| 11809 | } |
| 11810 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11811 | MemberExpr *ME = MemberExpr::Create(Context, Base, |
| 11812 | MemExpr->isArrow(), |
| 11813 | MemExpr->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11814 | MemExpr->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11815 | Fn, |
| 11816 | Found, |
| 11817 | MemExpr->getMemberNameInfo(), |
| 11818 | TemplateArgs, |
| 11819 | type, valueKind, OK_Ordinary); |
| 11820 | ME->setHadMultipleCandidates(true); |
Richard Smith | 4a03022 | 2012-11-14 07:06:31 +0000 | [diff] [blame] | 11821 | MarkMemberReferenced(ME); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11822 | return ME; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11823 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11824 | |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11825 | llvm_unreachable("Invalid reference to overloaded function"); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11826 | } |
| 11827 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11828 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11829 | DeclAccessPair Found, |
| 11830 | FunctionDecl *Fn) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11831 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 11832 | } |
| 11833 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 11834 | } // end namespace clang |