Nick Lewycky | 5d9484d | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1 | //===--- SemaOverload.cpp - C++ Overloading -------------------------------===// |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides Sema routines for C++ overloading. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Overload.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 16 | #include "clang/AST/CXXInheritance.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprCXX.h" |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeOrdering.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Diagnostic.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 23 | #include "clang/Basic/PartialDiagnostic.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Lex/Preprocessor.h" |
| 25 | #include "clang/Sema/Initialization.h" |
| 26 | #include "clang/Sema/Lookup.h" |
| 27 | #include "clang/Sema/SemaInternal.h" |
| 28 | #include "clang/Sema/Template.h" |
| 29 | #include "clang/Sema/TemplateDeduction.h" |
Douglas Gregor | 661b493 | 2010-09-12 04:28:07 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/DenseSet.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallPtrSet.h" |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 34 | #include <algorithm> |
| 35 | |
| 36 | namespace clang { |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 37 | using namespace sema; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 38 | |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 39 | /// A convenience routine for creating a decayed reference to a function. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 40 | static ExprResult |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 41 | CreateFunctionRefExpr(Sema &S, FunctionDecl *Fn, NamedDecl *FoundDecl, |
| 42 | bool HadMultipleCandidates, |
Douglas Gregor | 5b8968c | 2011-07-15 16:25:15 +0000 | [diff] [blame] | 43 | SourceLocation Loc = SourceLocation(), |
| 44 | const DeclarationNameLoc &LocInfo = DeclarationNameLoc()){ |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 45 | if (S.DiagnoseUseOfDecl(FoundDecl, Loc)) |
Faisal Vali | d570a92 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 46 | return ExprError(); |
| 47 | // If FoundDecl is different from Fn (such as if one is a template |
| 48 | // and the other a specialization), make sure DiagnoseUseOfDecl is |
| 49 | // called on both. |
| 50 | // FIXME: This would be more comprehensively addressed by modifying |
| 51 | // DiagnoseUseOfDecl to accept both the FoundDecl and the decl |
| 52 | // being used. |
| 53 | if (FoundDecl != Fn && S.DiagnoseUseOfDecl(Fn, Loc)) |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 54 | return ExprError(); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 55 | DeclRefExpr *DRE = new (S.Context) DeclRefExpr(Fn, false, Fn->getType(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 56 | VK_LValue, Loc, LocInfo); |
| 57 | if (HadMultipleCandidates) |
| 58 | DRE->setHadMultipleCandidates(true); |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 59 | |
| 60 | S.MarkDeclRefReferenced(DRE); |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 61 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 62 | ExprResult E = S.Owned(DRE); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 63 | E = S.DefaultFunctionArrayConversion(E.take()); |
| 64 | if (E.isInvalid()) |
| 65 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 66 | return E; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 67 | } |
| 68 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 69 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 70 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 71 | StandardConversionSequence &SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 72 | bool CStyle, |
| 73 | bool AllowObjCWritebackConversion); |
Sam Panzer | d012586 | 2012-08-16 02:38:47 +0000 | [diff] [blame] | 74 | |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 75 | static bool IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 76 | QualType &ToType, |
| 77 | bool InOverloadResolution, |
| 78 | StandardConversionSequence &SCS, |
| 79 | bool CStyle); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 80 | static OverloadingResult |
| 81 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 82 | UserDefinedConversionSequence& User, |
| 83 | OverloadCandidateSet& Conversions, |
| 84 | bool AllowExplicit); |
| 85 | |
| 86 | |
| 87 | static ImplicitConversionSequence::CompareKind |
| 88 | CompareStandardConversionSequences(Sema &S, |
| 89 | const StandardConversionSequence& SCS1, |
| 90 | const StandardConversionSequence& SCS2); |
| 91 | |
| 92 | static ImplicitConversionSequence::CompareKind |
| 93 | CompareQualificationConversions(Sema &S, |
| 94 | const StandardConversionSequence& SCS1, |
| 95 | const StandardConversionSequence& SCS2); |
| 96 | |
| 97 | static ImplicitConversionSequence::CompareKind |
| 98 | CompareDerivedToBaseConversions(Sema &S, |
| 99 | const StandardConversionSequence& SCS1, |
| 100 | const StandardConversionSequence& SCS2); |
| 101 | |
| 102 | |
| 103 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 104 | /// GetConversionCategory - Retrieve the implicit conversion |
| 105 | /// category corresponding to the given implicit conversion kind. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | ImplicitConversionCategory |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 107 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 108 | static const ImplicitConversionCategory |
| 109 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 110 | ICC_Identity, |
| 111 | ICC_Lvalue_Transformation, |
| 112 | ICC_Lvalue_Transformation, |
| 113 | ICC_Lvalue_Transformation, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 114 | ICC_Identity, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 115 | ICC_Qualification_Adjustment, |
| 116 | ICC_Promotion, |
| 117 | ICC_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 118 | ICC_Promotion, |
| 119 | ICC_Conversion, |
| 120 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 121 | ICC_Conversion, |
| 122 | ICC_Conversion, |
| 123 | ICC_Conversion, |
| 124 | ICC_Conversion, |
| 125 | ICC_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 126 | ICC_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 127 | ICC_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 128 | ICC_Conversion, |
| 129 | ICC_Conversion, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 130 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 131 | ICC_Conversion |
| 132 | }; |
| 133 | return Category[(int)Kind]; |
| 134 | } |
| 135 | |
| 136 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 137 | /// corresponding to the given implicit conversion kind. |
| 138 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 139 | static const ImplicitConversionRank |
| 140 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 141 | ICR_Exact_Match, |
| 142 | ICR_Exact_Match, |
| 143 | ICR_Exact_Match, |
| 144 | ICR_Exact_Match, |
| 145 | ICR_Exact_Match, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 146 | ICR_Exact_Match, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 147 | ICR_Promotion, |
| 148 | ICR_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 149 | ICR_Promotion, |
| 150 | ICR_Conversion, |
| 151 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 152 | ICR_Conversion, |
| 153 | ICR_Conversion, |
| 154 | ICR_Conversion, |
| 155 | ICR_Conversion, |
| 156 | ICR_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 157 | ICR_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 158 | ICR_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 159 | ICR_Conversion, |
| 160 | ICR_Conversion, |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 161 | ICR_Complex_Real_Conversion, |
| 162 | ICR_Conversion, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 163 | ICR_Conversion, |
| 164 | ICR_Writeback_Conversion |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 165 | }; |
| 166 | return Rank[(int)Kind]; |
| 167 | } |
| 168 | |
| 169 | /// GetImplicitConversionName - Return the name of this kind of |
| 170 | /// implicit conversion. |
| 171 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | 2550d70 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 172 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 173 | "No conversion", |
| 174 | "Lvalue-to-rvalue", |
| 175 | "Array-to-pointer", |
| 176 | "Function-to-pointer", |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 177 | "Noreturn adjustment", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 178 | "Qualification", |
| 179 | "Integral promotion", |
| 180 | "Floating point promotion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 181 | "Complex promotion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 182 | "Integral conversion", |
| 183 | "Floating conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 184 | "Complex conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 185 | "Floating-integral conversion", |
| 186 | "Pointer conversion", |
| 187 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 188 | "Boolean conversion", |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 189 | "Compatible-types conversion", |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 190 | "Derived-to-base conversion", |
| 191 | "Vector conversion", |
| 192 | "Vector splat", |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 193 | "Complex-real conversion", |
| 194 | "Block Pointer conversion", |
| 195 | "Transparent Union Conversion" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 196 | "Writeback conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 197 | }; |
| 198 | return Name[Kind]; |
| 199 | } |
| 200 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 201 | /// StandardConversionSequence - Set the standard conversion |
| 202 | /// sequence to the identity conversion. |
| 203 | void StandardConversionSequence::setAsIdentityConversion() { |
| 204 | First = ICK_Identity; |
| 205 | Second = ICK_Identity; |
| 206 | Third = ICK_Identity; |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 207 | DeprecatedStringLiteralToCharPtr = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 208 | QualificationIncludesObjCLifetime = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 209 | ReferenceBinding = false; |
| 210 | DirectBinding = false; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 211 | IsLvalueReference = true; |
| 212 | BindsToFunctionLvalue = false; |
| 213 | BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 214 | BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 215 | ObjCLifetimeConversionBinding = false; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 216 | CopyConstructor = 0; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 219 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 220 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 221 | /// implicit conversions. |
| 222 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 223 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 224 | if (GetConversionRank(First) > Rank) |
| 225 | Rank = GetConversionRank(First); |
| 226 | if (GetConversionRank(Second) > Rank) |
| 227 | Rank = GetConversionRank(Second); |
| 228 | if (GetConversionRank(Third) > Rank) |
| 229 | Rank = GetConversionRank(Third); |
| 230 | return Rank; |
| 231 | } |
| 232 | |
| 233 | /// isPointerConversionToBool - Determines whether this conversion is |
| 234 | /// a conversion of a pointer or pointer-to-member to bool. This is |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 235 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 236 | /// (C++ 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 237 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 238 | // Note that FromType has not necessarily been transformed by the |
| 239 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 240 | // check for their presence as well as checking whether FromType is |
| 241 | // a pointer. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 242 | if (getToType(1)->isBooleanType() && |
John McCall | ddb0ce7 | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 243 | (getFromType()->isPointerType() || |
| 244 | getFromType()->isObjCObjectPointerType() || |
| 245 | getFromType()->isBlockPointerType() || |
Anders Carlsson | c8df0b6 | 2010-11-05 00:12:09 +0000 | [diff] [blame] | 246 | getFromType()->isNullPtrType() || |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 247 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 248 | return true; |
| 249 | |
| 250 | return false; |
| 251 | } |
| 252 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 253 | /// isPointerConversionToVoidPointer - Determines whether this |
| 254 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 255 | /// used as part of the ranking of standard conversion sequences (C++ |
| 256 | /// 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 257 | bool |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 258 | StandardConversionSequence:: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 260 | QualType FromType = getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 261 | QualType ToType = getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 262 | |
| 263 | // Note that FromType has not necessarily been transformed by the |
| 264 | // array-to-pointer implicit conversion, so check for its presence |
| 265 | // and redo the conversion to get a pointer. |
| 266 | if (First == ICK_Array_To_Pointer) |
| 267 | FromType = Context.getArrayDecayedType(FromType); |
| 268 | |
Douglas Gregor | f9af524 | 2011-04-15 20:45:44 +0000 | [diff] [blame] | 269 | if (Second == ICK_Pointer_Conversion && FromType->isAnyPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 270 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 271 | return ToPtrType->getPointeeType()->isVoidType(); |
| 272 | |
| 273 | return false; |
| 274 | } |
| 275 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 276 | /// Skip any implicit casts which could be either part of a narrowing conversion |
| 277 | /// or after one in an implicit conversion. |
| 278 | static const Expr *IgnoreNarrowingConversion(const Expr *Converted) { |
| 279 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Converted)) { |
| 280 | switch (ICE->getCastKind()) { |
| 281 | case CK_NoOp: |
| 282 | case CK_IntegralCast: |
| 283 | case CK_IntegralToBoolean: |
| 284 | case CK_IntegralToFloating: |
| 285 | case CK_FloatingToIntegral: |
| 286 | case CK_FloatingToBoolean: |
| 287 | case CK_FloatingCast: |
| 288 | Converted = ICE->getSubExpr(); |
| 289 | continue; |
| 290 | |
| 291 | default: |
| 292 | return Converted; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | return Converted; |
| 297 | } |
| 298 | |
| 299 | /// Check if this standard conversion sequence represents a narrowing |
| 300 | /// conversion, according to C++11 [dcl.init.list]p7. |
| 301 | /// |
| 302 | /// \param Ctx The AST context. |
| 303 | /// \param Converted The result of applying this standard conversion sequence. |
| 304 | /// \param ConstantValue If this is an NK_Constant_Narrowing conversion, the |
| 305 | /// value of the expression prior to the narrowing conversion. |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 306 | /// \param ConstantType If this is an NK_Constant_Narrowing conversion, the |
| 307 | /// type of the expression prior to the narrowing conversion. |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 308 | NarrowingKind |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 309 | StandardConversionSequence::getNarrowingKind(ASTContext &Ctx, |
| 310 | const Expr *Converted, |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 311 | APValue &ConstantValue, |
| 312 | QualType &ConstantType) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 313 | assert(Ctx.getLangOpts().CPlusPlus && "narrowing check outside C++"); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 314 | |
| 315 | // C++11 [dcl.init.list]p7: |
| 316 | // A narrowing conversion is an implicit conversion ... |
| 317 | QualType FromType = getToType(0); |
| 318 | QualType ToType = getToType(1); |
| 319 | switch (Second) { |
| 320 | // -- from a floating-point type to an integer type, or |
| 321 | // |
| 322 | // -- from an integer type or unscoped enumeration type to a floating-point |
| 323 | // type, except where the source is a constant expression and the actual |
| 324 | // value after conversion will fit into the target type and will produce |
| 325 | // the original value when converted back to the original type, or |
| 326 | case ICK_Floating_Integral: |
| 327 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
| 328 | return NK_Type_Narrowing; |
| 329 | } else if (FromType->isIntegralType(Ctx) && ToType->isRealFloatingType()) { |
| 330 | llvm::APSInt IntConstantValue; |
| 331 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| 332 | if (Initializer && |
| 333 | Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { |
| 334 | // Convert the integer to the floating type. |
| 335 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
| 336 | Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), |
| 337 | llvm::APFloat::rmNearestTiesToEven); |
| 338 | // And back. |
| 339 | llvm::APSInt ConvertedValue = IntConstantValue; |
| 340 | bool ignored; |
| 341 | Result.convertToInteger(ConvertedValue, |
| 342 | llvm::APFloat::rmTowardZero, &ignored); |
| 343 | // If the resulting value is different, this was a narrowing conversion. |
| 344 | if (IntConstantValue != ConvertedValue) { |
| 345 | ConstantValue = APValue(IntConstantValue); |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 346 | ConstantType = Initializer->getType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 347 | return NK_Constant_Narrowing; |
| 348 | } |
| 349 | } else { |
| 350 | // Variables are always narrowings. |
| 351 | return NK_Variable_Narrowing; |
| 352 | } |
| 353 | } |
| 354 | return NK_Not_Narrowing; |
| 355 | |
| 356 | // -- from long double to double or float, or from double to float, except |
| 357 | // where the source is a constant expression and the actual value after |
| 358 | // conversion is within the range of values that can be represented (even |
| 359 | // if it cannot be represented exactly), or |
| 360 | case ICK_Floating_Conversion: |
| 361 | if (FromType->isRealFloatingType() && ToType->isRealFloatingType() && |
| 362 | Ctx.getFloatingTypeOrder(FromType, ToType) == 1) { |
| 363 | // FromType is larger than ToType. |
| 364 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| 365 | if (Initializer->isCXX11ConstantExpr(Ctx, &ConstantValue)) { |
| 366 | // Constant! |
| 367 | assert(ConstantValue.isFloat()); |
| 368 | llvm::APFloat FloatVal = ConstantValue.getFloat(); |
| 369 | // Convert the source value into the target type. |
| 370 | bool ignored; |
| 371 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
| 372 | Ctx.getFloatTypeSemantics(ToType), |
| 373 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 374 | // If there was no overflow, the source value is within the range of |
| 375 | // values that can be represented. |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 376 | if (ConvertStatus & llvm::APFloat::opOverflow) { |
| 377 | ConstantType = Initializer->getType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 378 | return NK_Constant_Narrowing; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 379 | } |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 380 | } else { |
| 381 | return NK_Variable_Narrowing; |
| 382 | } |
| 383 | } |
| 384 | return NK_Not_Narrowing; |
| 385 | |
| 386 | // -- from an integer type or unscoped enumeration type to an integer type |
| 387 | // that cannot represent all the values of the original type, except where |
| 388 | // the source is a constant expression and the actual value after |
| 389 | // conversion will fit into the target type and will produce the original |
| 390 | // value when converted back to the original type. |
| 391 | case ICK_Boolean_Conversion: // Bools are integers too. |
| 392 | if (!FromType->isIntegralOrUnscopedEnumerationType()) { |
| 393 | // Boolean conversions can be from pointers and pointers to members |
| 394 | // [conv.bool], and those aren't considered narrowing conversions. |
| 395 | return NK_Not_Narrowing; |
| 396 | } // Otherwise, fall through to the integral case. |
| 397 | case ICK_Integral_Conversion: { |
| 398 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
| 399 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
| 400 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
| 401 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
| 402 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
| 403 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
| 404 | |
| 405 | if (FromWidth > ToWidth || |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 406 | (FromWidth == ToWidth && FromSigned != ToSigned) || |
| 407 | (FromSigned && !ToSigned)) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 408 | // Not all values of FromType can be represented in ToType. |
| 409 | llvm::APSInt InitializerValue; |
| 410 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 411 | if (!Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { |
| 412 | // Such conversions on variables are always narrowing. |
| 413 | return NK_Variable_Narrowing; |
Richard Smith | 5d7700e | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 414 | } |
| 415 | bool Narrowing = false; |
| 416 | if (FromWidth < ToWidth) { |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 417 | // Negative -> unsigned is narrowing. Otherwise, more bits is never |
| 418 | // narrowing. |
| 419 | if (InitializerValue.isSigned() && InitializerValue.isNegative()) |
Richard Smith | 5d7700e | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 420 | Narrowing = true; |
Richard Smith | cd65f49 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 421 | } else { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 422 | // Add a bit to the InitializerValue so we don't have to worry about |
| 423 | // signed vs. unsigned comparisons. |
| 424 | InitializerValue = InitializerValue.extend( |
| 425 | InitializerValue.getBitWidth() + 1); |
| 426 | // Convert the initializer to and from the target width and signed-ness. |
| 427 | llvm::APSInt ConvertedValue = InitializerValue; |
| 428 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
| 429 | ConvertedValue.setIsSigned(ToSigned); |
| 430 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
| 431 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
| 432 | // If the result is different, this was a narrowing conversion. |
Richard Smith | 5d7700e | 2012-06-19 21:28:35 +0000 | [diff] [blame] | 433 | if (ConvertedValue != InitializerValue) |
| 434 | Narrowing = true; |
| 435 | } |
| 436 | if (Narrowing) { |
| 437 | ConstantType = Initializer->getType(); |
| 438 | ConstantValue = APValue(InitializerValue); |
| 439 | return NK_Constant_Narrowing; |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | return NK_Not_Narrowing; |
| 443 | } |
| 444 | |
| 445 | default: |
| 446 | // Other kinds of conversions are not narrowings. |
| 447 | return NK_Not_Narrowing; |
| 448 | } |
| 449 | } |
| 450 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 451 | /// DebugPrint - Print this standard conversion sequence to standard |
| 452 | /// error. Useful for debugging overloading issues. |
| 453 | void StandardConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 454 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 455 | bool PrintedSomething = false; |
| 456 | if (First != ICK_Identity) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 457 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 458 | PrintedSomething = true; |
| 459 | } |
| 460 | |
| 461 | if (Second != ICK_Identity) { |
| 462 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 463 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 464 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 465 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 466 | |
| 467 | if (CopyConstructor) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 468 | OS << " (by copy constructor)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 469 | } else if (DirectBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 470 | OS << " (direct reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 471 | } else if (ReferenceBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 472 | OS << " (reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 473 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 474 | PrintedSomething = true; |
| 475 | } |
| 476 | |
| 477 | if (Third != ICK_Identity) { |
| 478 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 479 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 480 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 481 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 482 | PrintedSomething = true; |
| 483 | } |
| 484 | |
| 485 | if (!PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 486 | OS << "No conversions required"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
| 490 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 491 | /// error. Useful for debugging overloading issues. |
| 492 | void UserDefinedConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 493 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 494 | if (Before.First || Before.Second || Before.Third) { |
| 495 | Before.DebugPrint(); |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 496 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 497 | } |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 498 | if (ConversionFunction) |
| 499 | OS << '\'' << *ConversionFunction << '\''; |
| 500 | else |
| 501 | OS << "aggregate initialization"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 502 | if (After.First || After.Second || After.Third) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 503 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 504 | After.DebugPrint(); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 509 | /// error. Useful for debugging overloading issues. |
| 510 | void ImplicitConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 511 | raw_ostream &OS = llvm::errs(); |
Richard Smith | 798186a | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 512 | if (isStdInitializerListElement()) |
| 513 | OS << "Worst std::initializer_list element conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 514 | switch (ConversionKind) { |
| 515 | case StandardConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 516 | OS << "Standard conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 517 | Standard.DebugPrint(); |
| 518 | break; |
| 519 | case UserDefinedConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 520 | OS << "User-defined conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 521 | UserDefined.DebugPrint(); |
| 522 | break; |
| 523 | case EllipsisConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 524 | OS << "Ellipsis conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 525 | break; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 526 | case AmbiguousConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 527 | OS << "Ambiguous conversion"; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 528 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 529 | case BadConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 530 | OS << "Bad conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 531 | break; |
| 532 | } |
| 533 | |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 534 | OS << "\n"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 535 | } |
| 536 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 537 | void AmbiguousConversionSequence::construct() { |
| 538 | new (&conversions()) ConversionSet(); |
| 539 | } |
| 540 | |
| 541 | void AmbiguousConversionSequence::destruct() { |
| 542 | conversions().~ConversionSet(); |
| 543 | } |
| 544 | |
| 545 | void |
| 546 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 547 | FromTypePtr = O.FromTypePtr; |
| 548 | ToTypePtr = O.ToTypePtr; |
| 549 | new (&conversions()) ConversionSet(O.conversions()); |
| 550 | } |
| 551 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 552 | namespace { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 553 | // Structure used by DeductionFailureInfo to store |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 554 | // template argument information. |
| 555 | struct DFIArguments { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 556 | TemplateArgument FirstArg; |
| 557 | TemplateArgument SecondArg; |
| 558 | }; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 559 | // Structure used by DeductionFailureInfo to store |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 560 | // template parameter and template argument information. |
| 561 | struct DFIParamWithArguments : DFIArguments { |
| 562 | TemplateParameter Param; |
| 563 | }; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 564 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 565 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 566 | /// \brief Convert from Sema's representation of template deduction information |
| 567 | /// to the form used in overload-candidate information. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 568 | DeductionFailureInfo MakeDeductionFailureInfo(ASTContext &Context, |
| 569 | Sema::TemplateDeductionResult TDK, |
| 570 | TemplateDeductionInfo &Info) { |
| 571 | DeductionFailureInfo Result; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 572 | Result.Result = static_cast<unsigned>(TDK); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 573 | Result.HasDiagnostic = false; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 574 | Result.Data = 0; |
| 575 | switch (TDK) { |
| 576 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 577 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 578 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 579 | case Sema::TDK_TooManyArguments: |
| 580 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 581 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 582 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 583 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 584 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 585 | Result.Data = Info.Param.getOpaqueValue(); |
| 586 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 587 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 588 | case Sema::TDK_NonDeducedMismatch: { |
| 589 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 590 | DFIArguments *Saved = new (Context) DFIArguments; |
| 591 | Saved->FirstArg = Info.FirstArg; |
| 592 | Saved->SecondArg = Info.SecondArg; |
| 593 | Result.Data = Saved; |
| 594 | break; |
| 595 | } |
| 596 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 597 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 598 | case Sema::TDK_Underqualified: { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 599 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 600 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 601 | Saved->Param = Info.Param; |
| 602 | Saved->FirstArg = Info.FirstArg; |
| 603 | Saved->SecondArg = Info.SecondArg; |
| 604 | Result.Data = Saved; |
| 605 | break; |
| 606 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 607 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 608 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 609 | Result.Data = Info.take(); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 610 | if (Info.hasSFINAEDiagnostic()) { |
| 611 | PartialDiagnosticAt *Diag = new (Result.Diagnostic) PartialDiagnosticAt( |
| 612 | SourceLocation(), PartialDiagnostic::NullDiagnostic()); |
| 613 | Info.takeSFINAEDiagnostic(*Diag); |
| 614 | Result.HasDiagnostic = true; |
| 615 | } |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 616 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 617 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 618 | case Sema::TDK_FailedOverloadResolution: |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 619 | Result.Data = Info.Expression; |
| 620 | break; |
| 621 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 622 | case Sema::TDK_MiscellaneousDeductionFailure: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 623 | break; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 624 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 625 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 626 | return Result; |
| 627 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 628 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 629 | void DeductionFailureInfo::Destroy() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 630 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 631 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 632 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 633 | case Sema::TDK_InstantiationDepth: |
| 634 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 635 | case Sema::TDK_TooManyArguments: |
| 636 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 637 | case Sema::TDK_InvalidExplicitArguments: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 638 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 639 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 640 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 641 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 642 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 643 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | aaa045d | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 644 | // FIXME: Destroy the data? |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 645 | Data = 0; |
| 646 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 647 | |
| 648 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 649 | // FIXME: Destroy the template argument list? |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 650 | Data = 0; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 651 | if (PartialDiagnosticAt *Diag = getSFINAEDiagnostic()) { |
| 652 | Diag->~PartialDiagnosticAt(); |
| 653 | HasDiagnostic = false; |
| 654 | } |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 655 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 656 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 657 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 658 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 659 | break; |
| 660 | } |
| 661 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 662 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 663 | PartialDiagnosticAt *DeductionFailureInfo::getSFINAEDiagnostic() { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 664 | if (HasDiagnostic) |
| 665 | return static_cast<PartialDiagnosticAt*>(static_cast<void*>(Diagnostic)); |
| 666 | return 0; |
| 667 | } |
| 668 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 669 | TemplateParameter DeductionFailureInfo::getTemplateParameter() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 670 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 671 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 672 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 673 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 674 | case Sema::TDK_TooManyArguments: |
| 675 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 676 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 677 | case Sema::TDK_NonDeducedMismatch: |
| 678 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 679 | return TemplateParameter(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 680 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 681 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 682 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 683 | return TemplateParameter::getFromOpaqueValue(Data); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 684 | |
| 685 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 686 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 687 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 688 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 689 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 690 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 691 | break; |
| 692 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 693 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 694 | return TemplateParameter(); |
| 695 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 696 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 697 | TemplateArgumentList *DeductionFailureInfo::getTemplateArgumentList() { |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 698 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 699 | case Sema::TDK_Success: |
| 700 | case Sema::TDK_Invalid: |
| 701 | case Sema::TDK_InstantiationDepth: |
| 702 | case Sema::TDK_TooManyArguments: |
| 703 | case Sema::TDK_TooFewArguments: |
| 704 | case Sema::TDK_Incomplete: |
| 705 | case Sema::TDK_InvalidExplicitArguments: |
| 706 | case Sema::TDK_Inconsistent: |
| 707 | case Sema::TDK_Underqualified: |
| 708 | case Sema::TDK_NonDeducedMismatch: |
| 709 | case Sema::TDK_FailedOverloadResolution: |
| 710 | return 0; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 711 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 712 | case Sema::TDK_SubstitutionFailure: |
| 713 | return static_cast<TemplateArgumentList*>(Data); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 714 | |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 715 | // Unhandled |
| 716 | case Sema::TDK_MiscellaneousDeductionFailure: |
| 717 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 723 | const TemplateArgument *DeductionFailureInfo::getFirstArg() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 724 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 725 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 726 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 727 | case Sema::TDK_InstantiationDepth: |
| 728 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 729 | case Sema::TDK_TooManyArguments: |
| 730 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 731 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 732 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 733 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 734 | return 0; |
| 735 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 736 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 737 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 738 | case Sema::TDK_NonDeducedMismatch: |
| 739 | return &static_cast<DFIArguments*>(Data)->FirstArg; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 740 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 741 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 742 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 743 | break; |
| 744 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 745 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 746 | return 0; |
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 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 749 | const TemplateArgument *DeductionFailureInfo::getSecondArg() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 750 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 751 | case Sema::TDK_Success: |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 752 | case Sema::TDK_Invalid: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 753 | case Sema::TDK_InstantiationDepth: |
| 754 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 755 | case Sema::TDK_TooManyArguments: |
| 756 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 757 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 758 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 759 | case Sema::TDK_FailedOverloadResolution: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 760 | return 0; |
| 761 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 762 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 763 | case Sema::TDK_Underqualified: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 764 | case Sema::TDK_NonDeducedMismatch: |
| 765 | return &static_cast<DFIArguments*>(Data)->SecondArg; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 766 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 767 | // Unhandled |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 768 | case Sema::TDK_MiscellaneousDeductionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 769 | break; |
| 770 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 771 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 772 | return 0; |
| 773 | } |
| 774 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 775 | Expr *DeductionFailureInfo::getExpr() { |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 776 | if (static_cast<Sema::TemplateDeductionResult>(Result) == |
| 777 | Sema::TDK_FailedOverloadResolution) |
| 778 | return static_cast<Expr*>(Data); |
| 779 | |
| 780 | return 0; |
| 781 | } |
| 782 | |
Benjamin Kramer | f5b132f | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 783 | void OverloadCandidateSet::destroyCandidates() { |
Richard Smith | e3898ac | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 784 | for (iterator i = begin(), e = end(); i != e; ++i) { |
Benjamin Kramer | 9e2822b | 2012-01-14 20:16:52 +0000 | [diff] [blame] | 785 | for (unsigned ii = 0, ie = i->NumConversions; ii != ie; ++ii) |
| 786 | i->Conversions[ii].~ImplicitConversionSequence(); |
Richard Smith | e3898ac | 2012-07-18 23:52:59 +0000 | [diff] [blame] | 787 | if (!i->Viable && i->FailureKind == ovl_fail_bad_deduction) |
| 788 | i->DeductionFailure.Destroy(); |
| 789 | } |
Benjamin Kramer | f5b132f | 2012-10-09 15:52:25 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | void OverloadCandidateSet::clear() { |
| 793 | destroyCandidates(); |
Benjamin Kramer | 314f554 | 2012-01-14 19:31:39 +0000 | [diff] [blame] | 794 | NumInlineSequences = 0; |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 795 | Candidates.clear(); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 796 | Functions.clear(); |
| 797 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 798 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 799 | namespace { |
| 800 | class UnbridgedCastsSet { |
| 801 | struct Entry { |
| 802 | Expr **Addr; |
| 803 | Expr *Saved; |
| 804 | }; |
| 805 | SmallVector<Entry, 2> Entries; |
| 806 | |
| 807 | public: |
| 808 | void save(Sema &S, Expr *&E) { |
| 809 | assert(E->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 810 | Entry entry = { &E, E }; |
| 811 | Entries.push_back(entry); |
| 812 | E = S.stripARCUnbridgedCast(E); |
| 813 | } |
| 814 | |
| 815 | void restore() { |
| 816 | for (SmallVectorImpl<Entry>::iterator |
| 817 | i = Entries.begin(), e = Entries.end(); i != e; ++i) |
| 818 | *i->Addr = i->Saved; |
| 819 | } |
| 820 | }; |
| 821 | } |
| 822 | |
| 823 | /// checkPlaceholderForOverload - Do any interesting placeholder-like |
| 824 | /// preprocessing on the given expression. |
| 825 | /// |
| 826 | /// \param unbridgedCasts a collection to which to add unbridged casts; |
| 827 | /// without this, they will be immediately diagnosed as errors |
| 828 | /// |
| 829 | /// Return true on unrecoverable error. |
| 830 | static bool checkPlaceholderForOverload(Sema &S, Expr *&E, |
| 831 | UnbridgedCastsSet *unbridgedCasts = 0) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 832 | if (const BuiltinType *placeholder = E->getType()->getAsPlaceholderType()) { |
| 833 | // We can't handle overloaded expressions here because overload |
| 834 | // resolution might reasonably tweak them. |
| 835 | if (placeholder->getKind() == BuiltinType::Overload) return false; |
| 836 | |
| 837 | // If the context potentially accepts unbridged ARC casts, strip |
| 838 | // the unbridged cast and add it to the collection for later restoration. |
| 839 | if (placeholder->getKind() == BuiltinType::ARCUnbridgedCast && |
| 840 | unbridgedCasts) { |
| 841 | unbridgedCasts->save(S, E); |
| 842 | return false; |
| 843 | } |
| 844 | |
| 845 | // Go ahead and check everything else. |
| 846 | ExprResult result = S.CheckPlaceholderExpr(E); |
| 847 | if (result.isInvalid()) |
| 848 | return true; |
| 849 | |
| 850 | E = result.take(); |
| 851 | return false; |
| 852 | } |
| 853 | |
| 854 | // Nothing to do. |
| 855 | return false; |
| 856 | } |
| 857 | |
| 858 | /// checkArgPlaceholdersForOverload - Check a set of call operands for |
| 859 | /// placeholders. |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 860 | static bool checkArgPlaceholdersForOverload(Sema &S, |
| 861 | MultiExprArg Args, |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 862 | UnbridgedCastsSet &unbridged) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 863 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 864 | if (checkPlaceholderForOverload(S, Args[i], &unbridged)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 865 | return true; |
| 866 | |
| 867 | return false; |
| 868 | } |
| 869 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 870 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 871 | // overload of the declarations in Old. This routine returns false if |
| 872 | // New and Old cannot be overloaded, e.g., if New has the same |
| 873 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 874 | // declarations aren't functions (or function templates) at all. When |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 875 | // it does return false, MatchedDecl will point to the decl that New |
| 876 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 877 | // top of the underlying declaration. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 878 | // |
| 879 | // Example: Given the following input: |
| 880 | // |
| 881 | // void f(int, float); // #1 |
| 882 | // void f(int, int); // #2 |
| 883 | // int f(int, int); // #3 |
| 884 | // |
| 885 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 886 | // so IsOverload will not be used. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 887 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 888 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 889 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 890 | // (since they have different signatures), so this routine returns |
| 891 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 892 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 893 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 894 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 895 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 896 | // identical (return types of functions are not part of the |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 897 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 898 | // point to the FunctionDecl for #2. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 899 | // |
| 900 | // 'NewIsUsingShadowDecl' indicates that 'New' is being introduced |
| 901 | // into a class by a using declaration. The rules for whether to hide |
| 902 | // shadow declarations ignore some properties which otherwise figure |
| 903 | // into a function template's signature. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 904 | Sema::OverloadKind |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 905 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
| 906 | NamedDecl *&Match, bool NewIsUsingDecl) { |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 907 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 908 | I != E; ++I) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 909 | NamedDecl *OldD = *I; |
| 910 | |
| 911 | bool OldIsUsingDecl = false; |
| 912 | if (isa<UsingShadowDecl>(OldD)) { |
| 913 | OldIsUsingDecl = true; |
| 914 | |
| 915 | // We can always introduce two using declarations into the same |
| 916 | // context, even if they have identical signatures. |
| 917 | if (NewIsUsingDecl) continue; |
| 918 | |
| 919 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 920 | } |
| 921 | |
| 922 | // If either declaration was introduced by a using declaration, |
| 923 | // we'll need to use slightly different rules for matching. |
| 924 | // Essentially, these rules are the normal rules, except that |
| 925 | // function templates hide function templates with different |
| 926 | // return types or template parameter lists. |
| 927 | bool UseMemberUsingDeclRules = |
John McCall | 78037ac | 2013-04-03 21:19:47 +0000 | [diff] [blame] | 928 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord() && |
| 929 | !New->getFriendObjectKind(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 930 | |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 931 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 932 | if (!IsOverload(New, OldT->getTemplatedDecl(), UseMemberUsingDeclRules)) { |
| 933 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 934 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 935 | continue; |
| 936 | } |
| 937 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 938 | Match = *I; |
| 939 | return Ovl_Match; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 940 | } |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 941 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 942 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
| 943 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 944 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 945 | continue; |
| 946 | } |
| 947 | |
Rafael Espindola | 90cc390 | 2013-04-15 12:49:13 +0000 | [diff] [blame] | 948 | if (!shouldLinkPossiblyHiddenDecl(*I, New)) |
| 949 | continue; |
| 950 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 951 | Match = *I; |
| 952 | return Ovl_Match; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 953 | } |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 954 | } else if (isa<UsingDecl>(OldD)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 955 | // We can overload with these, which can show up when doing |
| 956 | // redeclaration checks for UsingDecls. |
| 957 | assert(Old.getLookupKind() == LookupUsingDeclName); |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 958 | } else if (isa<TagDecl>(OldD)) { |
| 959 | // We can always overload with tags by hiding them. |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 960 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 961 | // Optimistically assume that an unresolved using decl will |
| 962 | // overload; if it doesn't, we'll have to diagnose during |
| 963 | // template instantiation. |
| 964 | } else { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 965 | // (C++ 13p1): |
| 966 | // Only function declarations can be overloaded; object and type |
| 967 | // declarations cannot be overloaded. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 968 | Match = *I; |
| 969 | return Ovl_NonFunction; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 970 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 971 | } |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 972 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 973 | return Ovl_Overload; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 976 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| 977 | bool UseUsingDeclRules) { |
| 978 | // C++ [basic.start.main]p2: This function shall not be overloaded. |
| 979 | if (New->isMain()) |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 980 | return false; |
Rafael Espindola | 7a525ac | 2013-01-12 01:47:40 +0000 | [diff] [blame] | 981 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 982 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 983 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 984 | |
| 985 | // C++ [temp.fct]p2: |
| 986 | // A function template can be overloaded with other function templates |
| 987 | // and with normal (non-template) functions. |
| 988 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 989 | return true; |
| 990 | |
| 991 | // Is the function New an overload of the function Old? |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 992 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 993 | QualType NewQType = Context.getCanonicalType(New->getType()); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 994 | |
| 995 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 996 | // determine whether they are overloads. If we find any mismatch |
| 997 | // in the signature, they are overloads. |
| 998 | |
| 999 | // If either of these functions is a K&R-style function (no |
| 1000 | // prototype), then we consider them to have matching signatures. |
| 1001 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 1002 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 1003 | return false; |
| 1004 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1005 | const FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 1006 | const FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1007 | |
| 1008 | // The signature of a function includes the types of its |
| 1009 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 1010 | // of the ellipsis; see C++ DR 357). |
| 1011 | if (OldQType != NewQType && |
| 1012 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 1013 | OldType->isVariadic() != NewType->isVariadic() || |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1014 | !FunctionArgTypesAreEqual(OldType, NewType))) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1015 | return true; |
| 1016 | |
| 1017 | // C++ [temp.over.link]p4: |
| 1018 | // The signature of a function template consists of its function |
| 1019 | // signature, its return type and its template parameter list. The names |
| 1020 | // of the template parameters are significant only for establishing the |
| 1021 | // relationship between the template parameters and the rest of the |
| 1022 | // signature. |
| 1023 | // |
| 1024 | // We check the return type and template parameter lists for function |
| 1025 | // templates first; the remaining checks follow. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 1026 | // |
| 1027 | // However, we don't consider either of these when deciding whether |
| 1028 | // a member introduced by a shadow declaration is hidden. |
| 1029 | if (!UseUsingDeclRules && NewTemplate && |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1030 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 1031 | OldTemplate->getTemplateParameters(), |
| 1032 | false, TPL_TemplateMatch) || |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1033 | OldType->getResultType() != NewType->getResultType())) |
| 1034 | return true; |
| 1035 | |
| 1036 | // If the function is a class member, its signature includes the |
Douglas Gregor | 57c9f4f | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 1037 | // 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] | 1038 | // |
| 1039 | // As part of this, also check whether one of the member functions |
| 1040 | // is static, in which case they are not overloads (C++ |
| 1041 | // 13.1p2). While not part of the definition of the signature, |
| 1042 | // this check is important to determine whether these functions |
| 1043 | // can be overloaded. |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1044 | CXXMethodDecl *OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 1045 | CXXMethodDecl *NewMethod = dyn_cast<CXXMethodDecl>(New); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1046 | if (OldMethod && NewMethod && |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1047 | !OldMethod->isStatic() && !NewMethod->isStatic()) { |
| 1048 | if (OldMethod->getRefQualifier() != NewMethod->getRefQualifier()) { |
| 1049 | if (!UseUsingDeclRules && |
| 1050 | (OldMethod->getRefQualifier() == RQ_None || |
| 1051 | NewMethod->getRefQualifier() == RQ_None)) { |
| 1052 | // C++0x [over.load]p2: |
| 1053 | // - Member function declarations with the same name and the same |
| 1054 | // parameter-type-list as well as member function template |
| 1055 | // declarations with the same name, the same parameter-type-list, and |
| 1056 | // the same template parameter lists cannot be overloaded if any of |
| 1057 | // them, but not all, have a ref-qualifier (8.3.5). |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1058 | Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1059 | << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1060 | Diag(OldMethod->getLocation(), diag::note_previous_declaration); |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1061 | } |
| 1062 | return true; |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1063 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1064 | |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1065 | // We may not have applied the implicit const for a constexpr member |
| 1066 | // function yet (because we haven't yet resolved whether this is a static |
| 1067 | // or non-static member function). Add it now, on the assumption that this |
| 1068 | // is a redeclaration of OldMethod. |
| 1069 | unsigned NewQuals = NewMethod->getTypeQualifiers(); |
Richard Smith | aa4bc18 | 2013-06-30 09:48:50 +0000 | [diff] [blame] | 1070 | if (!getLangOpts().CPlusPlus1y && NewMethod->isConstexpr() && |
Richard Smith | db2fe73 | 2013-06-25 18:46:26 +0000 | [diff] [blame] | 1071 | !isa<CXXConstructorDecl>(NewMethod)) |
Richard Smith | 21c8fa8 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 1072 | NewQuals |= Qualifiers::Const; |
| 1073 | if (OldMethod->getTypeQualifiers() != NewQuals) |
| 1074 | return true; |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1075 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1076 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1077 | // The signatures match; this is not an overload. |
| 1078 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 1081 | /// \brief Checks availability of the function depending on the current |
| 1082 | /// function context. Inside an unavailable function, unavailability is ignored. |
| 1083 | /// |
| 1084 | /// \returns true if \arg FD is unavailable and current context is inside |
| 1085 | /// an available function, false otherwise. |
| 1086 | bool Sema::isFunctionConsideredUnavailable(FunctionDecl *FD) { |
| 1087 | return FD->isUnavailable() && !cast<Decl>(CurContext)->isUnavailable(); |
| 1088 | } |
| 1089 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1090 | /// \brief Tries a user-defined conversion from From to ToType. |
| 1091 | /// |
| 1092 | /// Produces an implicit conversion sequence for when a standard conversion |
| 1093 | /// is not an option. See TryImplicitConversion for more information. |
| 1094 | static ImplicitConversionSequence |
| 1095 | TryUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 1096 | bool SuppressUserConversions, |
| 1097 | bool AllowExplicit, |
| 1098 | bool InOverloadResolution, |
| 1099 | bool CStyle, |
| 1100 | bool AllowObjCWritebackConversion) { |
| 1101 | ImplicitConversionSequence ICS; |
| 1102 | |
| 1103 | if (SuppressUserConversions) { |
| 1104 | // We're not in the case above, so there is no conversion that |
| 1105 | // we can perform. |
| 1106 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1107 | return ICS; |
| 1108 | } |
| 1109 | |
| 1110 | // Attempt user-defined conversion. |
| 1111 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 1112 | OverloadingResult UserDefResult |
| 1113 | = IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, Conversions, |
| 1114 | AllowExplicit); |
| 1115 | |
| 1116 | if (UserDefResult == OR_Success) { |
| 1117 | ICS.setUserDefined(); |
| 1118 | // C++ [over.ics.user]p4: |
| 1119 | // A conversion of an expression of class type to the same class |
| 1120 | // type is given Exact Match rank, and a conversion of an |
| 1121 | // expression of class type to a base class of that type is |
| 1122 | // given Conversion rank, in spite of the fact that a copy |
| 1123 | // constructor (i.e., a user-defined conversion function) is |
| 1124 | // called for those cases. |
| 1125 | if (CXXConstructorDecl *Constructor |
| 1126 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
| 1127 | QualType FromCanon |
| 1128 | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 1129 | QualType ToCanon |
| 1130 | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 1131 | if (Constructor->isCopyConstructor() && |
| 1132 | (FromCanon == ToCanon || S.IsDerivedFrom(FromCanon, ToCanon))) { |
| 1133 | // Turn this into a "standard" conversion sequence, so that it |
| 1134 | // gets ranked with standard conversion sequences. |
| 1135 | ICS.setStandard(); |
| 1136 | ICS.Standard.setAsIdentityConversion(); |
| 1137 | ICS.Standard.setFromType(From->getType()); |
| 1138 | ICS.Standard.setAllToTypes(ToType); |
| 1139 | ICS.Standard.CopyConstructor = Constructor; |
| 1140 | if (ToCanon != FromCanon) |
| 1141 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | // C++ [over.best.ics]p4: |
| 1146 | // However, when considering the argument of a user-defined |
| 1147 | // conversion function that is a candidate by 13.3.1.3 when |
| 1148 | // invoked for the copying of the temporary in the second step |
| 1149 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 1150 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 1151 | // ellipsis conversion sequences are allowed. |
| 1152 | if (SuppressUserConversions && ICS.isUserDefined()) { |
| 1153 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
| 1154 | } |
| 1155 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
| 1156 | ICS.setAmbiguous(); |
| 1157 | ICS.Ambiguous.setFromType(From->getType()); |
| 1158 | ICS.Ambiguous.setToType(ToType); |
| 1159 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 1160 | Cand != Conversions.end(); ++Cand) |
| 1161 | if (Cand->Viable) |
| 1162 | ICS.Ambiguous.addConversion(Cand->Function); |
| 1163 | } else { |
| 1164 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1165 | } |
| 1166 | |
| 1167 | return ICS; |
| 1168 | } |
| 1169 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1170 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 1171 | /// from the given expression (Expr) to the given type (ToType). This |
| 1172 | /// function returns an implicit conversion sequence that can be used |
| 1173 | /// to perform the initialization. Given |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1174 | /// |
| 1175 | /// void f(float f); |
| 1176 | /// void g(int i) { f(i); } |
| 1177 | /// |
| 1178 | /// this routine would produce an implicit conversion sequence to |
| 1179 | /// describe the initialization of f from i, which will be a standard |
| 1180 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 1181 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 1182 | // |
| 1183 | /// Note that this routine only determines how the conversion can be |
| 1184 | /// performed; it does not actually perform the conversion. As such, |
| 1185 | /// it will not produce any diagnostics if no conversion is available, |
| 1186 | /// but will instead return an implicit conversion sequence of kind |
| 1187 | /// "BadConversion". |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1188 | /// |
| 1189 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 1190 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1191 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 1192 | /// permitted. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1193 | /// |
| 1194 | /// \param AllowObjCWritebackConversion Whether we allow the Objective-C |
| 1195 | /// writeback conversion, which allows __autoreleasing id* parameters to |
| 1196 | /// be initialized with __strong id* or __weak id* arguments. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1197 | static ImplicitConversionSequence |
| 1198 | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
| 1199 | bool SuppressUserConversions, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1200 | bool AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1201 | bool InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1202 | bool CStyle, |
| 1203 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1204 | ImplicitConversionSequence ICS; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1205 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1206 | ICS.Standard, CStyle, AllowObjCWritebackConversion)){ |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1207 | ICS.setStandard(); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1208 | return ICS; |
| 1209 | } |
| 1210 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1211 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 1212 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1213 | return ICS; |
| 1214 | } |
| 1215 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1216 | // C++ [over.ics.user]p4: |
| 1217 | // A conversion of an expression of class type to the same class |
| 1218 | // type is given Exact Match rank, and a conversion of an |
| 1219 | // expression of class type to a base class of that type is |
| 1220 | // given Conversion rank, in spite of the fact that a copy/move |
| 1221 | // constructor (i.e., a user-defined conversion function) is |
| 1222 | // called for those cases. |
| 1223 | QualType FromType = From->getType(); |
| 1224 | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1225 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
| 1226 | S.IsDerivedFrom(FromType, ToType))) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1227 | ICS.setStandard(); |
| 1228 | ICS.Standard.setAsIdentityConversion(); |
| 1229 | ICS.Standard.setFromType(FromType); |
| 1230 | ICS.Standard.setAllToTypes(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1231 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1232 | // We don't actually check at this point whether there is a valid |
| 1233 | // copy/move constructor, since overloading just assumes that it |
| 1234 | // exists. When we actually perform initialization, we'll find the |
| 1235 | // appropriate constructor to copy the returned object, if needed. |
| 1236 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1237 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1238 | // Determine whether this is considered a derived-to-base conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1239 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1240 | ICS.Standard.Second = ICK_Derived_To_Base; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1241 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1242 | return ICS; |
| 1243 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1244 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1245 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 1246 | AllowExplicit, InOverloadResolution, CStyle, |
| 1247 | AllowObjCWritebackConversion); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1250 | ImplicitConversionSequence |
| 1251 | Sema::TryImplicitConversion(Expr *From, QualType ToType, |
| 1252 | bool SuppressUserConversions, |
| 1253 | bool AllowExplicit, |
| 1254 | bool InOverloadResolution, |
| 1255 | bool CStyle, |
| 1256 | bool AllowObjCWritebackConversion) { |
| 1257 | return clang::TryImplicitConversion(*this, From, ToType, |
| 1258 | SuppressUserConversions, AllowExplicit, |
| 1259 | InOverloadResolution, CStyle, |
| 1260 | AllowObjCWritebackConversion); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1263 | /// PerformImplicitConversion - Perform an implicit conversion of the |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1264 | /// expression From to the type ToType. Returns the |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1265 | /// converted expression. Flavor is the kind of conversion we're |
| 1266 | /// performing, used in the error message. If @p AllowExplicit, |
| 1267 | /// explicit user-defined conversions are permitted. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1268 | ExprResult |
| 1269 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1270 | AssignmentAction Action, bool AllowExplicit) { |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1271 | ImplicitConversionSequence ICS; |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1272 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1275 | ExprResult |
| 1276 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1277 | AssignmentAction Action, bool AllowExplicit, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1278 | ImplicitConversionSequence& ICS) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1279 | if (checkPlaceholderForOverload(*this, From)) |
| 1280 | return ExprError(); |
| 1281 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1282 | // Objective-C ARC: Determine whether we will allow the writeback conversion. |
| 1283 | bool AllowObjCWritebackConversion |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1284 | = getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1285 | (Action == AA_Passing || Action == AA_Sending); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1286 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1287 | ICS = clang::TryImplicitConversion(*this, From, ToType, |
| 1288 | /*SuppressUserConversions=*/false, |
| 1289 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1290 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1291 | /*CStyle=*/false, |
| 1292 | AllowObjCWritebackConversion); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1293 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 1294 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1295 | |
| 1296 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1297 | /// conversion that strips "noreturn" off the nested function type. |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1298 | bool Sema::IsNoReturnConversion(QualType FromType, QualType ToType, |
| 1299 | QualType &ResultTy) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1300 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1301 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1302 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1303 | // Permit the conversion F(t __attribute__((noreturn))) -> F(t) |
| 1304 | // where F adds one of the following at most once: |
| 1305 | // - a pointer |
| 1306 | // - a member pointer |
| 1307 | // - a block pointer |
| 1308 | CanQualType CanTo = Context.getCanonicalType(ToType); |
| 1309 | CanQualType CanFrom = Context.getCanonicalType(FromType); |
| 1310 | Type::TypeClass TyClass = CanTo->getTypeClass(); |
| 1311 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1312 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) { |
| 1313 | if (TyClass == Type::Pointer) { |
| 1314 | CanTo = CanTo.getAs<PointerType>()->getPointeeType(); |
| 1315 | CanFrom = CanFrom.getAs<PointerType>()->getPointeeType(); |
| 1316 | } else if (TyClass == Type::BlockPointer) { |
| 1317 | CanTo = CanTo.getAs<BlockPointerType>()->getPointeeType(); |
| 1318 | CanFrom = CanFrom.getAs<BlockPointerType>()->getPointeeType(); |
| 1319 | } else if (TyClass == Type::MemberPointer) { |
| 1320 | CanTo = CanTo.getAs<MemberPointerType>()->getPointeeType(); |
| 1321 | CanFrom = CanFrom.getAs<MemberPointerType>()->getPointeeType(); |
| 1322 | } else { |
| 1323 | return false; |
| 1324 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1325 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1326 | TyClass = CanTo->getTypeClass(); |
| 1327 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1328 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) |
| 1329 | return false; |
| 1330 | } |
| 1331 | |
| 1332 | const FunctionType *FromFn = cast<FunctionType>(CanFrom); |
| 1333 | FunctionType::ExtInfo EInfo = FromFn->getExtInfo(); |
| 1334 | if (!EInfo.getNoReturn()) return false; |
| 1335 | |
| 1336 | FromFn = Context.adjustFunctionType(FromFn, EInfo.withNoReturn(false)); |
| 1337 | assert(QualType(FromFn, 0).isCanonical()); |
| 1338 | if (QualType(FromFn, 0) != CanTo) return false; |
| 1339 | |
| 1340 | ResultTy = ToType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1341 | return true; |
| 1342 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1343 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1344 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 1345 | /// vector conversion. |
| 1346 | /// |
| 1347 | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
| 1348 | /// conversion. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1349 | static bool IsVectorConversion(ASTContext &Context, QualType FromType, |
| 1350 | QualType ToType, ImplicitConversionKind &ICK) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1351 | // We need at least one of these types to be a vector type to have a vector |
| 1352 | // conversion. |
| 1353 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
| 1354 | return false; |
| 1355 | |
| 1356 | // Identical types require no conversions. |
| 1357 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1358 | return false; |
| 1359 | |
| 1360 | // There are no conversions between extended vector types, only identity. |
| 1361 | if (ToType->isExtVectorType()) { |
| 1362 | // There are no conversions between extended vector types other than the |
| 1363 | // identity conversion. |
| 1364 | if (FromType->isExtVectorType()) |
| 1365 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1366 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1367 | // Vector splat from any arithmetic type to a vector. |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 1368 | if (FromType->isArithmeticType()) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1369 | ICK = ICK_Vector_Splat; |
| 1370 | return true; |
| 1371 | } |
| 1372 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1373 | |
| 1374 | // We can perform the conversion between vector types in the following cases: |
| 1375 | // 1)vector types are equivalent AltiVec and GCC vector types |
| 1376 | // 2)lax vector conversions are permitted and the vector types are of the |
| 1377 | // same size |
| 1378 | if (ToType->isVectorType() && FromType->isVectorType()) { |
| 1379 | if (Context.areCompatibleVectorTypes(FromType, ToType) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1380 | (Context.getLangOpts().LaxVectorConversions && |
Chandler Carruth | c45eb9c | 2010-08-08 05:02:51 +0000 | [diff] [blame] | 1381 | (Context.getTypeSize(FromType) == Context.getTypeSize(ToType)))) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1382 | ICK = ICK_Vector_Conversion; |
| 1383 | return true; |
| 1384 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1385 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1386 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1387 | return false; |
| 1388 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1389 | |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1390 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 1391 | bool InOverloadResolution, |
| 1392 | StandardConversionSequence &SCS, |
| 1393 | bool CStyle); |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1394 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1395 | /// IsStandardConversion - Determines whether there is a standard |
| 1396 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 1397 | /// expression From to the type ToType. Standard conversion sequences |
| 1398 | /// only consider non-class types; for conversions that involve class |
| 1399 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 1400 | /// contain the standard conversion sequence required to perform this |
| 1401 | /// conversion and this routine will return true. Otherwise, this |
| 1402 | /// routine will return false and the value of SCS is unspecified. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1403 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 1404 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1405 | StandardConversionSequence &SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1406 | bool CStyle, |
| 1407 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1408 | QualType FromType = From->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1409 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1410 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1411 | SCS.setAsIdentityConversion(); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1412 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1413 | SCS.IncompatibleObjC = false; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1414 | SCS.setFromType(FromType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1415 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1416 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1417 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1419 | if (FromType->isRecordType() || ToType->isRecordType()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1420 | if (S.getLangOpts().CPlusPlus) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1421 | return false; |
| 1422 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1423 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1426 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 1427 | // array-to-pointer conversion, or function-to-pointer conversion |
| 1428 | // (C++ 4p1). |
| 1429 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1430 | if (FromType == S.Context.OverloadTy) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1431 | DeclAccessPair AccessPair; |
| 1432 | if (FunctionDecl *Fn |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1433 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1434 | AccessPair)) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1435 | // We were able to resolve the address of the overloaded function, |
| 1436 | // so we can convert to the type of that function. |
| 1437 | FromType = Fn->getType(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1438 | |
| 1439 | // we can sometimes resolve &foo<int> regardless of ToType, so check |
| 1440 | // if the type matches (identity) or we are converting to bool |
| 1441 | if (!S.Context.hasSameUnqualifiedType( |
| 1442 | S.ExtractUnqualifiedFunctionType(ToType), FromType)) { |
| 1443 | QualType resultTy; |
| 1444 | // if the function type matches except for [[noreturn]], it's ok |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1445 | if (!S.IsNoReturnConversion(FromType, |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1446 | S.ExtractUnqualifiedFunctionType(ToType), resultTy)) |
| 1447 | // otherwise, only a boolean conversion is standard |
| 1448 | if (!ToType->isBooleanType()) |
| 1449 | return false; |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1450 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1451 | |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1452 | // Check if the "from" expression is taking the address of an overloaded |
| 1453 | // function and recompute the FromType accordingly. Take advantage of the |
| 1454 | // fact that non-static member functions *must* have such an address-of |
| 1455 | // expression. |
| 1456 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn); |
| 1457 | if (Method && !Method->isStatic()) { |
| 1458 | assert(isa<UnaryOperator>(From->IgnoreParens()) && |
| 1459 | "Non-unary operator on non-static member address"); |
| 1460 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() |
| 1461 | == UO_AddrOf && |
| 1462 | "Non-address-of operator on non-static member address"); |
| 1463 | const Type *ClassType |
| 1464 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 1465 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
Chandler Carruth | fc5c8fc | 2011-03-29 18:38:10 +0000 | [diff] [blame] | 1466 | } else if (isa<UnaryOperator>(From->IgnoreParens())) { |
| 1467 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() == |
| 1468 | UO_AddrOf && |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1469 | "Non-address-of operator for overloaded function expression"); |
| 1470 | FromType = S.Context.getPointerType(FromType); |
| 1471 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1472 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1473 | // Check that we've computed the proper type after overload resolution. |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1474 | assert(S.Context.hasSameType( |
| 1475 | FromType, |
| 1476 | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1477 | } else { |
| 1478 | return false; |
| 1479 | } |
Anders Carlsson | 2bd6250 | 2010-11-04 05:28:09 +0000 | [diff] [blame] | 1480 | } |
John McCall | 2148011 | 2011-08-30 00:57:29 +0000 | [diff] [blame] | 1481 | // Lvalue-to-rvalue conversion (C++11 4.1): |
| 1482 | // A glvalue (3.10) of a non-function, non-array type T can |
| 1483 | // be converted to a prvalue. |
| 1484 | bool argIsLValue = From->isGLValue(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1485 | if (argIsLValue && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1486 | !FromType->isFunctionType() && !FromType->isArrayType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1487 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1488 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1489 | |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1490 | // C11 6.3.2.1p2: |
| 1491 | // ... if the lvalue has atomic type, the value has the non-atomic version |
| 1492 | // of the type of the lvalue ... |
| 1493 | if (const AtomicType *Atomic = FromType->getAs<AtomicType>()) |
| 1494 | FromType = Atomic->getValueType(); |
| 1495 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1496 | // If T is a non-class type, the type of the rvalue is the |
| 1497 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1498 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 1499 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1500 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1501 | } else if (FromType->isArrayType()) { |
| 1502 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1503 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1504 | |
| 1505 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 1506 | // bound of T" can be converted to an rvalue of type "pointer to |
| 1507 | // T" (C++ 4.2p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1508 | FromType = S.Context.getArrayDecayedType(FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1509 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1510 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1511 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1512 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1513 | |
| 1514 | // For the purpose of ranking in overload resolution |
| 1515 | // (13.3.3.1.1), this conversion is considered an |
| 1516 | // array-to-pointer conversion followed by a qualification |
| 1517 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1518 | SCS.Second = ICK_Identity; |
| 1519 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1520 | SCS.QualificationIncludesObjCLifetime = false; |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1521 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1522 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1523 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1524 | } else if (FromType->isFunctionType() && argIsLValue) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1525 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1526 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1527 | |
| 1528 | // An lvalue of function type T can be converted to an rvalue of |
| 1529 | // type "pointer to T." The result is a pointer to the |
| 1530 | // function. (C++ 4.3p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1531 | FromType = S.Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1532 | } else { |
| 1533 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1534 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1535 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1536 | SCS.setToType(0, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1537 | |
| 1538 | // The second conversion can be an integral promotion, floating |
| 1539 | // point promotion, integral conversion, floating point conversion, |
| 1540 | // floating-integral conversion, pointer conversion, |
| 1541 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1542 | // For overloading in C, this can also be a "compatible-type" |
| 1543 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1544 | bool IncompatibleObjC = false; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1545 | ImplicitConversionKind SecondICK = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1546 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1547 | // The unqualified versions of the types are the same: there's no |
| 1548 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1549 | SCS.Second = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1550 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1551 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1552 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1553 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1554 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1555 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1556 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1557 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1558 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1559 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1560 | SCS.Second = ICK_Complex_Promotion; |
| 1561 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1562 | } else if (ToType->isBooleanType() && |
| 1563 | (FromType->isArithmeticType() || |
| 1564 | FromType->isAnyPointerType() || |
| 1565 | FromType->isBlockPointerType() || |
| 1566 | FromType->isMemberPointerType() || |
| 1567 | FromType->isNullPtrType())) { |
| 1568 | // Boolean conversions (C++ 4.12). |
| 1569 | SCS.Second = ICK_Boolean_Conversion; |
| 1570 | FromType = S.Context.BoolTy; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1571 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1572 | ToType->isIntegralType(S.Context)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1573 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1574 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1575 | FromType = ToType.getUnqualifiedType(); |
Richard Smith | 42860f1 | 2013-05-10 20:29:50 +0000 | [diff] [blame] | 1576 | } else if (FromType->isAnyComplexType() && ToType->isAnyComplexType()) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1577 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1578 | SCS.Second = ICK_Complex_Conversion; |
| 1579 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1580 | } else if ((FromType->isAnyComplexType() && ToType->isArithmeticType()) || |
| 1581 | (ToType->isAnyComplexType() && FromType->isArithmeticType())) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1582 | // Complex-real conversions (C99 6.3.1.7) |
| 1583 | SCS.Second = ICK_Complex_Real; |
| 1584 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1585 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1586 | // Floating point conversions (C++ 4.8). |
| 1587 | SCS.Second = ICK_Floating_Conversion; |
| 1588 | FromType = ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1589 | } else if ((FromType->isRealFloatingType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1590 | ToType->isIntegralType(S.Context)) || |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1591 | (FromType->isIntegralOrUnscopedEnumerationType() && |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1592 | ToType->isRealFloatingType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1593 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1594 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1595 | FromType = ToType.getUnqualifiedType(); |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1596 | } else if (S.IsBlockPointerConversion(FromType, ToType, FromType)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1597 | SCS.Second = ICK_Block_Pointer_Conversion; |
| 1598 | } else if (AllowObjCWritebackConversion && |
| 1599 | S.isObjCWritebackConversion(FromType, ToType, FromType)) { |
| 1600 | SCS.Second = ICK_Writeback_Conversion; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1601 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1602 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1603 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1604 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1605 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1606 | FromType = FromType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1607 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1608 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1609 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1610 | SCS.Second = ICK_Pointer_Member; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1611 | } else if (IsVectorConversion(S.Context, FromType, ToType, SecondICK)) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1612 | SCS.Second = SecondICK; |
| 1613 | FromType = ToType.getUnqualifiedType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1614 | } else if (!S.getLangOpts().CPlusPlus && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1615 | S.Context.typesAreCompatible(ToType, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1616 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1617 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1618 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1619 | } else if (S.IsNoReturnConversion(FromType, ToType, FromType)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1620 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1621 | SCS.Second = ICK_NoReturn_Adjustment; |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1622 | } else if (IsTransparentUnionStandardConversion(S, From, ToType, |
| 1623 | InOverloadResolution, |
| 1624 | SCS, CStyle)) { |
| 1625 | SCS.Second = ICK_TransparentUnionConversion; |
| 1626 | FromType = ToType; |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1627 | } else if (tryAtomicConversion(S, From, ToType, InOverloadResolution, SCS, |
| 1628 | CStyle)) { |
| 1629 | // tryAtomicConversion has updated the standard conversion sequence |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1630 | // appropriately. |
| 1631 | return true; |
Guy Benyei | 6959acd | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 1632 | } else if (ToType->isEventT() && |
| 1633 | From->isIntegerConstantExpr(S.getASTContext()) && |
| 1634 | (From->EvaluateKnownConstInt(S.getASTContext()) == 0)) { |
| 1635 | SCS.Second = ICK_Zero_Event_Conversion; |
| 1636 | FromType = ToType; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1637 | } else { |
| 1638 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1639 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1640 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1641 | SCS.setToType(1, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1642 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1643 | QualType CanonFrom; |
| 1644 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1645 | // The third conversion can be a qualification conversion (C++ 4p1). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1646 | bool ObjCLifetimeConversion; |
| 1647 | if (S.IsQualificationConversion(FromType, ToType, CStyle, |
| 1648 | ObjCLifetimeConversion)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1649 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1650 | SCS.QualificationIncludesObjCLifetime = ObjCLifetimeConversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1651 | FromType = ToType; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1652 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1653 | CanonTo = S.Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1654 | } else { |
| 1655 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1656 | SCS.Third = ICK_Identity; |
| 1657 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1658 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1659 | // [...] Any difference in top-level cv-qualification is |
| 1660 | // subsumed by the initialization itself and does not constitute |
| 1661 | // a conversion. [...] |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1662 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1663 | CanonTo = S.Context.getCanonicalType(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1664 | if (CanonFrom.getLocalUnqualifiedType() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1665 | == CanonTo.getLocalUnqualifiedType() && |
Matt Arsenault | 5509f37 | 2013-02-26 21:15:54 +0000 | [diff] [blame] | 1666 | CanonFrom.getLocalQualifiers() != CanonTo.getLocalQualifiers()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1667 | FromType = ToType; |
| 1668 | CanonFrom = CanonTo; |
| 1669 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1670 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1671 | SCS.setToType(2, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1672 | |
| 1673 | // If we have not converted the argument type to the parameter type, |
| 1674 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1675 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1676 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1677 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1678 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1679 | } |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1680 | |
| 1681 | static bool |
| 1682 | IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 1683 | QualType &ToType, |
| 1684 | bool InOverloadResolution, |
| 1685 | StandardConversionSequence &SCS, |
| 1686 | bool CStyle) { |
| 1687 | |
| 1688 | const RecordType *UT = ToType->getAsUnionType(); |
| 1689 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 1690 | return false; |
| 1691 | // The field to initialize within the transparent union. |
| 1692 | RecordDecl *UD = UT->getDecl(); |
| 1693 | // It's compatible if the expression matches any of the fields. |
| 1694 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1695 | itend = UD->field_end(); |
| 1696 | it != itend; ++it) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1697 | if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS, |
| 1698 | CStyle, /*ObjCWritebackConversion=*/false)) { |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1699 | ToType = it->getType(); |
| 1700 | return true; |
| 1701 | } |
| 1702 | } |
| 1703 | return false; |
| 1704 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1705 | |
| 1706 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1707 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1708 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1709 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1710 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1711 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1712 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1713 | if (!To) { |
| 1714 | return false; |
| 1715 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1716 | |
| 1717 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1718 | // unsigned short int can be converted to an rvalue of type int if |
| 1719 | // int can represent all the values of the source type; otherwise, |
| 1720 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1721 | // int (C++ 4.5p1). |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1722 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1723 | !FromType->isEnumeralType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1724 | if (// We can promote any signed, promotable integer type to an int |
| 1725 | (FromType->isSignedIntegerType() || |
| 1726 | // We can promote any unsigned integer type whose size is |
| 1727 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1728 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1729 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1730 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1733 | return To->getKind() == BuiltinType::UInt; |
| 1734 | } |
| 1735 | |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1736 | // C++11 [conv.prom]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1737 | // A prvalue of an unscoped enumeration type whose underlying type is not |
| 1738 | // fixed (7.2) can be converted to an rvalue a prvalue of the first of the |
| 1739 | // following types that can represent all the values of the enumeration |
| 1740 | // (i.e., the values in the range bmin to bmax as described in 7.2): int, |
| 1741 | // unsigned int, long int, unsigned long int, long long int, or unsigned |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1742 | // 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] | 1743 | // values of the enumeration, an rvalue a prvalue of an unscoped enumeration |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1744 | // 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] | 1745 | // with lowest integer conversion rank (4.13) greater than the rank of long |
| 1746 | // 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] | 1747 | // there are two such extended types, the signed one is chosen. |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1748 | // C++11 [conv.prom]p4: |
| 1749 | // A prvalue of an unscoped enumeration type whose underlying type is fixed |
| 1750 | // can be converted to a prvalue of its underlying type. Moreover, if |
| 1751 | // integral promotion can be applied to its underlying type, a prvalue of an |
| 1752 | // unscoped enumeration type whose underlying type is fixed can also be |
| 1753 | // converted to a prvalue of the promoted underlying type. |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1754 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
| 1755 | // C++0x 7.2p9: Note that this implicit enum to int conversion is not |
| 1756 | // provided for a scoped enumeration. |
| 1757 | if (FromEnumType->getDecl()->isScoped()) |
| 1758 | return false; |
| 1759 | |
Richard Smith | e7ff919 | 2012-09-13 21:18:54 +0000 | [diff] [blame] | 1760 | // We can perform an integral promotion to the underlying type of the enum, |
| 1761 | // even if that's not the promoted type. |
| 1762 | if (FromEnumType->getDecl()->isFixed()) { |
| 1763 | QualType Underlying = FromEnumType->getDecl()->getIntegerType(); |
| 1764 | return Context.hasSameUnqualifiedType(Underlying, ToType) || |
| 1765 | IsIntegralPromotion(From, Underlying, ToType); |
| 1766 | } |
| 1767 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1768 | // We have already pre-calculated the promotion type, so this is trivial. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1769 | if (ToType->isIntegerType() && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1770 | !RequireCompleteType(From->getLocStart(), FromType, 0)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1771 | return Context.hasSameUnqualifiedType(ToType, |
| 1772 | FromEnumType->getDecl()->getPromotionType()); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1773 | } |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1774 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1775 | // C++0x [conv.prom]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1776 | // A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted |
| 1777 | // to an rvalue a prvalue of the first of the following types that can |
| 1778 | // represent all the values of its underlying type: int, unsigned int, |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1779 | // 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] | 1780 | // 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] | 1781 | // underlying type, an rvalue a prvalue of type char16_t, char32_t, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1782 | // 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] | 1783 | // type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1784 | if (FromType->isAnyCharacterType() && !FromType->isCharType() && |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1785 | ToType->isIntegerType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1786 | // Determine whether the type we're converting from is signed or |
| 1787 | // unsigned. |
David Majnemer | 0ad9231 | 2011-07-22 21:09:04 +0000 | [diff] [blame] | 1788 | bool FromIsSigned = FromType->isSignedIntegerType(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1789 | uint64_t FromSize = Context.getTypeSize(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1790 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1791 | // The types we'll try to promote to, in the appropriate |
| 1792 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1793 | QualType PromoteTypes[6] = { |
| 1794 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1795 | Context.LongTy, Context.UnsignedLongTy , |
| 1796 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1797 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1798 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1799 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1800 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1801 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1802 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1803 | // We found the type that we can promote to. If this is the |
| 1804 | // type we wanted, we have a promotion. Otherwise, no |
| 1805 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1806 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1807 | } |
| 1808 | } |
| 1809 | } |
| 1810 | |
| 1811 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1812 | // rvalue of type int if int can represent all the values of the |
| 1813 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1814 | // unsigned int can represent all the values of the bit-field. If |
| 1815 | // the bit-field is larger yet, no integral promotion applies to |
| 1816 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1817 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1818 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1819 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1820 | using llvm::APSInt; |
| 1821 | if (From) |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 1822 | if (FieldDecl *MemberDecl = From->getSourceBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1823 | APSInt BitWidth; |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1824 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1825 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1826 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1827 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1828 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1829 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1830 | if (BitWidth < ToSize || |
| 1831 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1832 | return To->getKind() == BuiltinType::Int; |
| 1833 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1834 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1835 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1836 | // that fits into an unsigned int? |
| 1837 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1838 | return To->getKind() == BuiltinType::UInt; |
| 1839 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1840 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1841 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1842 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1843 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1844 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1845 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1846 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1847 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1848 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1849 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1850 | |
| 1851 | return false; |
| 1852 | } |
| 1853 | |
| 1854 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1855 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1856 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1857 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1858 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1859 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1860 | /// An rvalue of type float can be converted to an rvalue of type |
| 1861 | /// double. (C++ 4.6p1). |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1862 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1863 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1864 | return true; |
| 1865 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1866 | // C99 6.3.1.5p1: |
| 1867 | // When a float is promoted to double or long double, or a |
| 1868 | // double is promoted to long double [...]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1869 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1870 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1871 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1872 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1873 | return true; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1874 | |
| 1875 | // Half can be promoted to float. |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 1876 | if (!getLangOpts().NativeHalfType && |
| 1877 | FromBuiltin->getKind() == BuiltinType::Half && |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1878 | ToBuiltin->getKind() == BuiltinType::Float) |
| 1879 | return true; |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1882 | return false; |
| 1883 | } |
| 1884 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1885 | /// \brief Determine if a conversion is a complex promotion. |
| 1886 | /// |
| 1887 | /// A complex promotion is defined as a complex -> complex conversion |
| 1888 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1889 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1890 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1891 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1892 | if (!FromComplex) |
| 1893 | return false; |
| 1894 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1895 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1896 | if (!ToComplex) |
| 1897 | return false; |
| 1898 | |
| 1899 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1900 | ToComplex->getElementType()) || |
| 1901 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1902 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1905 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1906 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1907 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1908 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1909 | /// the right set of qualifiers on its pointee. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1910 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1911 | static QualType |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1912 | BuildSimilarlyQualifiedPointerType(const Type *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1913 | QualType ToPointee, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1914 | ASTContext &Context, |
| 1915 | bool StripObjCLifetime = false) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1916 | assert((FromPtr->getTypeClass() == Type::Pointer || |
| 1917 | FromPtr->getTypeClass() == Type::ObjCObjectPointer) && |
| 1918 | "Invalid similarly-qualified pointer type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1919 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1920 | /// Conversions to 'id' subsume cv-qualifier conversions. |
| 1921 | if (ToType->isObjCIdType() || ToType->isObjCQualifiedIdType()) |
Douglas Gregor | 143c7ac | 2010-12-06 22:09:19 +0000 | [diff] [blame] | 1922 | return ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1923 | |
| 1924 | QualType CanonFromPointee |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1925 | = Context.getCanonicalType(FromPtr->getPointeeType()); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1926 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1927 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1928 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1929 | if (StripObjCLifetime) |
| 1930 | Quals.removeObjCLifetime(); |
| 1931 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1932 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1933 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1934 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1935 | if (!ToType.isNull()) |
Douglas Gregor | af7bea5 | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1936 | return ToType.getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1937 | |
| 1938 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1939 | // already. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1940 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1941 | return Context.getObjCObjectPointerType(ToPointee); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1942 | return Context.getPointerType(ToPointee); |
| 1943 | } |
| 1944 | |
| 1945 | // Just build a canonical type that has the right qualifiers. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1946 | QualType QualifiedCanonToPointee |
| 1947 | = Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1948 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1949 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1950 | return Context.getObjCObjectPointerType(QualifiedCanonToPointee); |
| 1951 | return Context.getPointerType(QualifiedCanonToPointee); |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1952 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1953 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1954 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1955 | bool InOverloadResolution, |
| 1956 | ASTContext &Context) { |
| 1957 | // Handle value-dependent integral null pointer constants correctly. |
| 1958 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1959 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1960 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1961 | return !InOverloadResolution; |
| 1962 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1963 | return Expr->isNullPointerConstant(Context, |
| 1964 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1965 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1966 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1967 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1968 | /// IsPointerConversion - Determines whether the conversion of the |
| 1969 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1970 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1971 | /// 4.10). If so, returns true and places the converted type (that |
| 1972 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1973 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1974 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1975 | /// This routine also supports conversions to and from block pointers |
| 1976 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1977 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1978 | /// appropriate overloading rules for Objective-C, we may want to |
| 1979 | /// split the Objective-C checks into a different routine; however, |
| 1980 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1981 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1982 | /// set if the conversion is an allowed Objective-C conversion that |
| 1983 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1984 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1985 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1986 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1987 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1988 | IncompatibleObjC = false; |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1989 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, |
| 1990 | IncompatibleObjC)) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1991 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1992 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1993 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1994 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1995 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1996 | ConvertedType = ToType; |
| 1997 | return true; |
| 1998 | } |
| 1999 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2000 | // Blocks: Block pointers can be converted to void*. |
| 2001 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2002 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2003 | ConvertedType = ToType; |
| 2004 | return true; |
| 2005 | } |
| 2006 | // Blocks: A null pointer constant can be converted to a block |
| 2007 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2008 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2009 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 2010 | ConvertedType = ToType; |
| 2011 | return true; |
| 2012 | } |
| 2013 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2014 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 2015 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2016 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 2017 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2018 | ConvertedType = ToType; |
| 2019 | return true; |
| 2020 | } |
| 2021 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2022 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2023 | if (!ToTypePtr) |
| 2024 | return false; |
| 2025 | |
| 2026 | // 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] | 2027 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2028 | ConvertedType = ToType; |
| 2029 | return true; |
| 2030 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2031 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2032 | // Beyond this point, both types need to be pointers |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2033 | // , including objective-c pointers. |
| 2034 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2035 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2036 | !getLangOpts().ObjCAutoRefCount) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2037 | ConvertedType = BuildSimilarlyQualifiedPointerType( |
| 2038 | FromType->getAs<ObjCObjectPointerType>(), |
| 2039 | ToPointeeType, |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2040 | ToType, Context); |
| 2041 | return true; |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 2042 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2043 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2044 | if (!FromTypePtr) |
| 2045 | return false; |
| 2046 | |
| 2047 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2048 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2049 | // 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] | 2050 | // pointer conversion, so don't do all of the work below. |
| 2051 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
| 2052 | return false; |
| 2053 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2054 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 2055 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 2056 | // 4.10p2). |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 2057 | if (FromPointeeType->isIncompleteOrObjectType() && |
| 2058 | ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2059 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2060 | ToPointeeType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2061 | ToType, Context, |
| 2062 | /*StripObjCLifetime=*/true); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2063 | return true; |
| 2064 | } |
| 2065 | |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2066 | // MSVC allows implicit function to void* type conversion. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2067 | if (getLangOpts().MicrosoftExt && FromPointeeType->isFunctionType() && |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 2068 | ToPointeeType->isVoidType()) { |
| 2069 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2070 | ToPointeeType, |
| 2071 | ToType, Context); |
| 2072 | return true; |
| 2073 | } |
| 2074 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2075 | // When we're overloading in C, we allow a special kind of pointer |
| 2076 | // conversion for compatible-but-not-identical pointee types. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2077 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2078 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2079 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2080 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2081 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2082 | return true; |
| 2083 | } |
| 2084 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2085 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2086 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2087 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 2088 | // can be converted to an rvalue of type "pointer to cv B," where |
| 2089 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 2090 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 2091 | // necessitates this conversion is ill-formed. The result of the |
| 2092 | // conversion is a pointer to the base class sub-object of the |
| 2093 | // derived class object. The null pointer value is converted to |
| 2094 | // the null pointer value of the destination type. |
| 2095 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2096 | // Note that we do not check for ambiguity or inaccessibility |
| 2097 | // here. That is handled by CheckPointerConversion. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2098 | if (getLangOpts().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2099 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | bf1764c | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 2100 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2101 | !RequireCompleteType(From->getLocStart(), FromPointeeType, 0) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2102 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2103 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2104 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2105 | ToType, Context); |
| 2106 | return true; |
| 2107 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2108 | |
Fariborz Jahanian | 5da3c08 | 2011-04-14 20:33:36 +0000 | [diff] [blame] | 2109 | if (FromPointeeType->isVectorType() && ToPointeeType->isVectorType() && |
| 2110 | Context.areCompatibleVectorTypes(FromPointeeType, ToPointeeType)) { |
| 2111 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2112 | ToPointeeType, |
| 2113 | ToType, Context); |
| 2114 | return true; |
| 2115 | } |
| 2116 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2117 | return false; |
| 2118 | } |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2119 | |
| 2120 | /// \brief Adopt the given qualifiers for the given type. |
| 2121 | static QualType AdoptQualifiers(ASTContext &Context, QualType T, Qualifiers Qs){ |
| 2122 | Qualifiers TQs = T.getQualifiers(); |
| 2123 | |
| 2124 | // Check whether qualifiers already match. |
| 2125 | if (TQs == Qs) |
| 2126 | return T; |
| 2127 | |
| 2128 | if (Qs.compatiblyIncludes(TQs)) |
| 2129 | return Context.getQualifiedType(T, Qs); |
| 2130 | |
| 2131 | return Context.getQualifiedType(T.getUnqualifiedType(), Qs); |
| 2132 | } |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2133 | |
| 2134 | /// isObjCPointerConversion - Determines whether this is an |
| 2135 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 2136 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2137 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2138 | QualType& ConvertedType, |
| 2139 | bool &IncompatibleObjC) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2140 | if (!getLangOpts().ObjC1) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2141 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2142 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2143 | // The set of qualifiers on the type we're converting from. |
| 2144 | Qualifiers FromQualifiers = FromType.getQualifiers(); |
| 2145 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2146 | // First, we handle all conversions on ObjC object pointer types. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2147 | const ObjCObjectPointerType* ToObjCPtr = |
| 2148 | ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2149 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2150 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2151 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2152 | if (ToObjCPtr && FromObjCPtr) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2153 | // If the pointee types are the same (ignoring qualifications), |
| 2154 | // then this is not a pointer conversion. |
| 2155 | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
| 2156 | FromObjCPtr->getPointeeType())) |
| 2157 | return false; |
| 2158 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2159 | // Check for compatible |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2160 | // 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] | 2161 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2162 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2163 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2164 | return true; |
| 2165 | } |
| 2166 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2167 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2168 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2169 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 2170 | /*compare=*/false)) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2171 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2172 | return true; |
| 2173 | } |
| 2174 | // Objective C++: We're able to convert from a pointer to an |
| 2175 | // interface to a pointer to a different interface. |
| 2176 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2177 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 2178 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2179 | if (getLangOpts().CPlusPlus && LHS && RHS && |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2180 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 2181 | FromObjCPtr->getPointeeType())) |
| 2182 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2183 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2184 | ToObjCPtr->getPointeeType(), |
| 2185 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2186 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2187 | return true; |
| 2188 | } |
| 2189 | |
| 2190 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 2191 | // Okay: this is some kind of implicit downcast of Objective-C |
| 2192 | // interfaces, which is permitted. However, we're going to |
| 2193 | // complain about it. |
| 2194 | IncompatibleObjC = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2195 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2196 | ToObjCPtr->getPointeeType(), |
| 2197 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2198 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2199 | return true; |
| 2200 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2201 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2202 | // 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] | 2203 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2204 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2205 | ToPointeeType = ToCPtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2206 | else if (const BlockPointerType *ToBlockPtr = |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2207 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 2208 | // 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] | 2209 | // to a block pointer type. |
| 2210 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2211 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2212 | return true; |
| 2213 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2214 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2215 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2216 | else if (FromType->getAs<BlockPointerType>() && |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2217 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2218 | // 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] | 2219 | // pointer to any object. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2220 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2221 | return true; |
| 2222 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2223 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2224 | return false; |
| 2225 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2226 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2227 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2228 | FromPointeeType = FromCPtr->getPointeeType(); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2229 | else if (const BlockPointerType *FromBlockPtr = |
| 2230 | FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2231 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2232 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2233 | return false; |
| 2234 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2235 | // If we have pointers to pointers, recursively check whether this |
| 2236 | // is an Objective-C conversion. |
| 2237 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 2238 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2239 | IncompatibleObjC)) { |
| 2240 | // We always complain about this conversion. |
| 2241 | IncompatibleObjC = true; |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2242 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2243 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2244 | return true; |
| 2245 | } |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2246 | // Allow conversion of pointee being objective-c pointer to another one; |
| 2247 | // as in I* to id. |
| 2248 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 2249 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 2250 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2251 | IncompatibleObjC)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2252 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2253 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2254 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2255 | return true; |
| 2256 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2257 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2258 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2259 | // differences in the argument and result types are in Objective-C |
| 2260 | // pointer conversions. If so, we permit the conversion (but |
| 2261 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2262 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2263 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2264 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2265 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2266 | if (FromFunctionType && ToFunctionType) { |
| 2267 | // If the function types are exactly the same, this isn't an |
| 2268 | // Objective-C pointer conversion. |
| 2269 | if (Context.getCanonicalType(FromPointeeType) |
| 2270 | == Context.getCanonicalType(ToPointeeType)) |
| 2271 | return false; |
| 2272 | |
| 2273 | // Perform the quick checks that will tell us whether these |
| 2274 | // function types are obviously different. |
| 2275 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2276 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 2277 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 2278 | return false; |
| 2279 | |
| 2280 | bool HasObjCConversion = false; |
| 2281 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 2282 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 2283 | // Okay, the types match exactly. Nothing to do. |
| 2284 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 2285 | ToFunctionType->getResultType(), |
| 2286 | ConvertedType, IncompatibleObjC)) { |
| 2287 | // Okay, we have an Objective-C pointer conversion. |
| 2288 | HasObjCConversion = true; |
| 2289 | } else { |
| 2290 | // Function types are too different. Abort. |
| 2291 | return false; |
| 2292 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2293 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2294 | // Check argument types. |
| 2295 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2296 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2297 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2298 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2299 | if (Context.getCanonicalType(FromArgType) |
| 2300 | == Context.getCanonicalType(ToArgType)) { |
| 2301 | // Okay, the types match exactly. Nothing to do. |
| 2302 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 2303 | ConvertedType, IncompatibleObjC)) { |
| 2304 | // Okay, we have an Objective-C pointer conversion. |
| 2305 | HasObjCConversion = true; |
| 2306 | } else { |
| 2307 | // Argument types are too different. Abort. |
| 2308 | return false; |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | if (HasObjCConversion) { |
| 2313 | // We had an Objective-C conversion. Allow this pointer |
| 2314 | // conversion, but complain about it. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2315 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2316 | IncompatibleObjC = true; |
| 2317 | return true; |
| 2318 | } |
| 2319 | } |
| 2320 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2321 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2322 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2323 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2324 | /// \brief Determine whether this is an Objective-C writeback conversion, |
| 2325 | /// used for parameter passing when performing automatic reference counting. |
| 2326 | /// |
| 2327 | /// \param FromType The type we're converting form. |
| 2328 | /// |
| 2329 | /// \param ToType The type we're converting to. |
| 2330 | /// |
| 2331 | /// \param ConvertedType The type that will be produced after applying |
| 2332 | /// this conversion. |
| 2333 | bool Sema::isObjCWritebackConversion(QualType FromType, QualType ToType, |
| 2334 | QualType &ConvertedType) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2335 | if (!getLangOpts().ObjCAutoRefCount || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2336 | Context.hasSameUnqualifiedType(FromType, ToType)) |
| 2337 | return false; |
| 2338 | |
| 2339 | // Parameter must be a pointer to __autoreleasing (with no other qualifiers). |
| 2340 | QualType ToPointee; |
| 2341 | if (const PointerType *ToPointer = ToType->getAs<PointerType>()) |
| 2342 | ToPointee = ToPointer->getPointeeType(); |
| 2343 | else |
| 2344 | return false; |
| 2345 | |
| 2346 | Qualifiers ToQuals = ToPointee.getQualifiers(); |
| 2347 | if (!ToPointee->isObjCLifetimeType() || |
| 2348 | ToQuals.getObjCLifetime() != Qualifiers::OCL_Autoreleasing || |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2349 | !ToQuals.withoutObjCLifetime().empty()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2350 | return false; |
| 2351 | |
| 2352 | // Argument must be a pointer to __strong to __weak. |
| 2353 | QualType FromPointee; |
| 2354 | if (const PointerType *FromPointer = FromType->getAs<PointerType>()) |
| 2355 | FromPointee = FromPointer->getPointeeType(); |
| 2356 | else |
| 2357 | return false; |
| 2358 | |
| 2359 | Qualifiers FromQuals = FromPointee.getQualifiers(); |
| 2360 | if (!FromPointee->isObjCLifetimeType() || |
| 2361 | (FromQuals.getObjCLifetime() != Qualifiers::OCL_Strong && |
| 2362 | FromQuals.getObjCLifetime() != Qualifiers::OCL_Weak)) |
| 2363 | return false; |
| 2364 | |
| 2365 | // Make sure that we have compatible qualifiers. |
| 2366 | FromQuals.setObjCLifetime(Qualifiers::OCL_Autoreleasing); |
| 2367 | if (!ToQuals.compatiblyIncludes(FromQuals)) |
| 2368 | return false; |
| 2369 | |
| 2370 | // Remove qualifiers from the pointee type we're converting from; they |
| 2371 | // aren't used in the compatibility check belong, and we'll be adding back |
| 2372 | // qualifiers (with __autoreleasing) if the compatibility check succeeds. |
| 2373 | FromPointee = FromPointee.getUnqualifiedType(); |
| 2374 | |
| 2375 | // The unqualified form of the pointee types must be compatible. |
| 2376 | ToPointee = ToPointee.getUnqualifiedType(); |
| 2377 | bool IncompatibleObjC; |
| 2378 | if (Context.typesAreCompatible(FromPointee, ToPointee)) |
| 2379 | FromPointee = ToPointee; |
| 2380 | else if (!isObjCPointerConversion(FromPointee, ToPointee, FromPointee, |
| 2381 | IncompatibleObjC)) |
| 2382 | return false; |
| 2383 | |
| 2384 | /// \brief Construct the type we're converting to, which is a pointer to |
| 2385 | /// __autoreleasing pointee. |
| 2386 | FromPointee = Context.getQualifiedType(FromPointee, FromQuals); |
| 2387 | ConvertedType = Context.getPointerType(FromPointee); |
| 2388 | return true; |
| 2389 | } |
| 2390 | |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2391 | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
| 2392 | QualType& ConvertedType) { |
| 2393 | QualType ToPointeeType; |
| 2394 | if (const BlockPointerType *ToBlockPtr = |
| 2395 | ToType->getAs<BlockPointerType>()) |
| 2396 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 2397 | else |
| 2398 | return false; |
| 2399 | |
| 2400 | QualType FromPointeeType; |
| 2401 | if (const BlockPointerType *FromBlockPtr = |
| 2402 | FromType->getAs<BlockPointerType>()) |
| 2403 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2404 | else |
| 2405 | return false; |
| 2406 | // We have pointer to blocks, check whether the only |
| 2407 | // differences in the argument and result types are in Objective-C |
| 2408 | // pointer conversions. If so, we permit the conversion. |
| 2409 | |
| 2410 | const FunctionProtoType *FromFunctionType |
| 2411 | = FromPointeeType->getAs<FunctionProtoType>(); |
| 2412 | const FunctionProtoType *ToFunctionType |
| 2413 | = ToPointeeType->getAs<FunctionProtoType>(); |
| 2414 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2415 | if (!FromFunctionType || !ToFunctionType) |
| 2416 | return false; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2417 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2418 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2419 | return true; |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2420 | |
| 2421 | // Perform the quick checks that will tell us whether these |
| 2422 | // function types are obviously different. |
| 2423 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2424 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
| 2425 | return false; |
| 2426 | |
| 2427 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
| 2428 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
| 2429 | if (FromEInfo != ToEInfo) |
| 2430 | return false; |
| 2431 | |
| 2432 | bool IncompatibleObjC = false; |
Fariborz Jahanian | 462dae5 | 2011-02-13 20:11:42 +0000 | [diff] [blame] | 2433 | if (Context.hasSameType(FromFunctionType->getResultType(), |
| 2434 | ToFunctionType->getResultType())) { |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2435 | // Okay, the types match exactly. Nothing to do. |
| 2436 | } else { |
| 2437 | QualType RHS = FromFunctionType->getResultType(); |
| 2438 | QualType LHS = ToFunctionType->getResultType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2439 | if ((!getLangOpts().CPlusPlus || !RHS->isRecordType()) && |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2440 | !RHS.hasQualifiers() && LHS.hasQualifiers()) |
| 2441 | LHS = LHS.getUnqualifiedType(); |
| 2442 | |
| 2443 | if (Context.hasSameType(RHS,LHS)) { |
| 2444 | // OK exact match. |
| 2445 | } else if (isObjCPointerConversion(RHS, LHS, |
| 2446 | ConvertedType, IncompatibleObjC)) { |
| 2447 | if (IncompatibleObjC) |
| 2448 | return false; |
| 2449 | // Okay, we have an Objective-C pointer conversion. |
| 2450 | } |
| 2451 | else |
| 2452 | return false; |
| 2453 | } |
| 2454 | |
| 2455 | // Check argument types. |
| 2456 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2457 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2458 | IncompatibleObjC = false; |
| 2459 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2460 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2461 | if (Context.hasSameType(FromArgType, ToArgType)) { |
| 2462 | // Okay, the types match exactly. Nothing to do. |
| 2463 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
| 2464 | ConvertedType, IncompatibleObjC)) { |
| 2465 | if (IncompatibleObjC) |
| 2466 | return false; |
| 2467 | // Okay, we have an Objective-C pointer conversion. |
| 2468 | } else |
| 2469 | // Argument types are too different. Abort. |
| 2470 | return false; |
| 2471 | } |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 2472 | if (LangOpts.ObjCAutoRefCount && |
| 2473 | !Context.FunctionTypesMatchOnNSConsumedAttrs(FromFunctionType, |
| 2474 | ToFunctionType)) |
| 2475 | return false; |
Fariborz Jahanian | f9d9527 | 2011-09-28 20:22:05 +0000 | [diff] [blame] | 2476 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2477 | ConvertedType = ToType; |
| 2478 | return true; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2481 | enum { |
| 2482 | ft_default, |
| 2483 | ft_different_class, |
| 2484 | ft_parameter_arity, |
| 2485 | ft_parameter_mismatch, |
| 2486 | ft_return_type, |
| 2487 | ft_qualifer_mismatch |
| 2488 | }; |
| 2489 | |
| 2490 | /// HandleFunctionTypeMismatch - Gives diagnostic information for differeing |
| 2491 | /// function types. Catches different number of parameter, mismatch in |
| 2492 | /// parameter types, and different return types. |
| 2493 | void Sema::HandleFunctionTypeMismatch(PartialDiagnostic &PDiag, |
| 2494 | QualType FromType, QualType ToType) { |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2495 | // If either type is not valid, include no extra info. |
| 2496 | if (FromType.isNull() || ToType.isNull()) { |
| 2497 | PDiag << ft_default; |
| 2498 | return; |
| 2499 | } |
| 2500 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2501 | // Get the function type from the pointers. |
| 2502 | if (FromType->isMemberPointerType() && ToType->isMemberPointerType()) { |
| 2503 | const MemberPointerType *FromMember = FromType->getAs<MemberPointerType>(), |
| 2504 | *ToMember = ToType->getAs<MemberPointerType>(); |
| 2505 | if (FromMember->getClass() != ToMember->getClass()) { |
| 2506 | PDiag << ft_different_class << QualType(ToMember->getClass(), 0) |
| 2507 | << QualType(FromMember->getClass(), 0); |
| 2508 | return; |
| 2509 | } |
| 2510 | FromType = FromMember->getPointeeType(); |
| 2511 | ToType = ToMember->getPointeeType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2514 | if (FromType->isPointerType()) |
| 2515 | FromType = FromType->getPointeeType(); |
| 2516 | if (ToType->isPointerType()) |
| 2517 | ToType = ToType->getPointeeType(); |
| 2518 | |
| 2519 | // Remove references. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2520 | FromType = FromType.getNonReferenceType(); |
| 2521 | ToType = ToType.getNonReferenceType(); |
| 2522 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2523 | // Don't print extra info for non-specialized template functions. |
| 2524 | if (FromType->isInstantiationDependentType() && |
| 2525 | !FromType->getAs<TemplateSpecializationType>()) { |
| 2526 | PDiag << ft_default; |
| 2527 | return; |
| 2528 | } |
| 2529 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2530 | // No extra info for same types. |
| 2531 | if (Context.hasSameType(FromType, ToType)) { |
| 2532 | PDiag << ft_default; |
| 2533 | return; |
| 2534 | } |
| 2535 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2536 | const FunctionProtoType *FromFunction = FromType->getAs<FunctionProtoType>(), |
| 2537 | *ToFunction = ToType->getAs<FunctionProtoType>(); |
| 2538 | |
| 2539 | // Both types need to be function types. |
| 2540 | if (!FromFunction || !ToFunction) { |
| 2541 | PDiag << ft_default; |
| 2542 | return; |
| 2543 | } |
| 2544 | |
| 2545 | if (FromFunction->getNumArgs() != ToFunction->getNumArgs()) { |
| 2546 | PDiag << ft_parameter_arity << ToFunction->getNumArgs() |
| 2547 | << FromFunction->getNumArgs(); |
| 2548 | return; |
| 2549 | } |
| 2550 | |
| 2551 | // Handle different parameter types. |
| 2552 | unsigned ArgPos; |
| 2553 | if (!FunctionArgTypesAreEqual(FromFunction, ToFunction, &ArgPos)) { |
| 2554 | PDiag << ft_parameter_mismatch << ArgPos + 1 |
| 2555 | << ToFunction->getArgType(ArgPos) |
| 2556 | << FromFunction->getArgType(ArgPos); |
| 2557 | return; |
| 2558 | } |
| 2559 | |
| 2560 | // Handle different return type. |
| 2561 | if (!Context.hasSameType(FromFunction->getResultType(), |
| 2562 | ToFunction->getResultType())) { |
| 2563 | PDiag << ft_return_type << ToFunction->getResultType() |
| 2564 | << FromFunction->getResultType(); |
| 2565 | return; |
| 2566 | } |
| 2567 | |
| 2568 | unsigned FromQuals = FromFunction->getTypeQuals(), |
| 2569 | ToQuals = ToFunction->getTypeQuals(); |
| 2570 | if (FromQuals != ToQuals) { |
| 2571 | PDiag << ft_qualifer_mismatch << ToQuals << FromQuals; |
| 2572 | return; |
| 2573 | } |
| 2574 | |
| 2575 | // Unable to find a difference, so add no extra info. |
| 2576 | PDiag << ft_default; |
| 2577 | } |
| 2578 | |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2579 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
Douglas Gregor | dec1cc4 | 2011-12-15 17:15:07 +0000 | [diff] [blame] | 2580 | /// for equality of their argument types. Caller has already checked that |
Eli Friedman | 0601700 | 2013-06-18 22:41:37 +0000 | [diff] [blame] | 2581 | /// they have same number of arguments. If the parameters are different, |
| 2582 | /// ArgPos will have the parameter index of the first different parameter. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2583 | bool Sema::FunctionArgTypesAreEqual(const FunctionProtoType *OldType, |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2584 | const FunctionProtoType *NewType, |
| 2585 | unsigned *ArgPos) { |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2586 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 2587 | N = NewType->arg_type_begin(), |
| 2588 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
Richard Trieu | 7ea491c | 2013-08-09 21:42:32 +0000 | [diff] [blame] | 2589 | if (!Context.hasSameType(O->getUnqualifiedType(), |
| 2590 | N->getUnqualifiedType())) { |
Larisse Voufo | 3151b7c | 2013-08-06 03:57:41 +0000 | [diff] [blame] | 2591 | if (ArgPos) *ArgPos = O - OldType->arg_type_begin(); |
| 2592 | return false; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2593 | } |
| 2594 | } |
| 2595 | return true; |
| 2596 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2597 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2598 | /// CheckPointerConversion - Check the pointer conversion from the |
| 2599 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2600 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2601 | /// conversions for which IsPointerConversion has already returned |
| 2602 | /// true. It returns true and produces a diagnostic if there was an |
| 2603 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2604 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2605 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2606 | CXXCastPath& BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2607 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2608 | QualType FromType = From->getType(); |
Argyrios Kyrtzidis | b335872 | 2010-09-28 14:54:11 +0000 | [diff] [blame] | 2609 | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2610 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2611 | Kind = CK_BitCast; |
| 2612 | |
David Blaikie | 50800fc | 2012-08-08 17:33:31 +0000 | [diff] [blame] | 2613 | if (!IsCStyleOrFunctionalCast && !FromType->isAnyPointerType() && |
| 2614 | From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull) == |
| 2615 | Expr::NPCK_ZeroExpression) { |
| 2616 | if (Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy)) |
| 2617 | DiagRuntimeBehavior(From->getExprLoc(), From, |
| 2618 | PDiag(diag::warn_impcast_bool_to_null_pointer) |
| 2619 | << ToType << From->getSourceRange()); |
| 2620 | else if (!isUnevaluatedContext()) |
| 2621 | Diag(From->getExprLoc(), diag::warn_non_literal_null_pointer) |
| 2622 | << ToType << From->getSourceRange(); |
| 2623 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2624 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
| 2625 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2626 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 2627 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 2628 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2629 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 2630 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2631 | // We must have a derived-to-base conversion. Check an |
| 2632 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2633 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 2634 | From->getExprLoc(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2635 | From->getSourceRange(), &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2636 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2637 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2638 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2639 | // The conversion was successful. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2640 | Kind = CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2641 | } |
| 2642 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2643 | } else if (const ObjCObjectPointerType *ToPtrType = |
| 2644 | ToType->getAs<ObjCObjectPointerType>()) { |
| 2645 | if (const ObjCObjectPointerType *FromPtrType = |
| 2646 | FromType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2647 | // Objective-C++ conversions are always okay. |
| 2648 | // FIXME: We should have a different class of conversions for the |
| 2649 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2650 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2651 | return false; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2652 | } else if (FromType->isBlockPointerType()) { |
| 2653 | Kind = CK_BlockPointerToObjCPointerCast; |
| 2654 | } else { |
| 2655 | Kind = CK_CPointerToObjCPointerCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2656 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2657 | } else if (ToType->isBlockPointerType()) { |
| 2658 | if (!FromType->isBlockPointerType()) |
| 2659 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2660 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2661 | |
| 2662 | // We shouldn't fall into this case unless it's valid for other |
| 2663 | // reasons. |
| 2664 | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 2665 | Kind = CK_NullToPointer; |
| 2666 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2667 | return false; |
| 2668 | } |
| 2669 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2670 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 2671 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 2672 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 2673 | /// If so, returns true and places the converted type (that might differ from |
| 2674 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 2675 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2676 | QualType ToType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2677 | bool InOverloadResolution, |
| 2678 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2679 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2680 | if (!ToTypePtr) |
| 2681 | return false; |
| 2682 | |
| 2683 | // 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] | 2684 | if (From->isNullPointerConstant(Context, |
| 2685 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 2686 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2687 | ConvertedType = ToType; |
| 2688 | return true; |
| 2689 | } |
| 2690 | |
| 2691 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2692 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2693 | if (!FromTypePtr) |
| 2694 | return false; |
| 2695 | |
| 2696 | // A pointer to member of B can be converted to a pointer to member of D, |
| 2697 | // where D is derived from B (C++ 4.11p2). |
| 2698 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 2699 | QualType ToClass(ToTypePtr->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2700 | |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2701 | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2702 | !RequireCompleteType(From->getLocStart(), ToClass, 0) && |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2703 | IsDerivedFrom(ToClass, FromClass)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2704 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 2705 | ToClass.getTypePtr()); |
| 2706 | return true; |
| 2707 | } |
| 2708 | |
| 2709 | return false; |
| 2710 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2711 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2712 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 2713 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2714 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2715 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 2716 | /// true and produces a diagnostic if there was an error, or returns false |
| 2717 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2718 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2719 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2720 | CXXCastPath &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2721 | bool IgnoreBaseAccess) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2722 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2723 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2724 | if (!FromPtrType) { |
| 2725 | // This must be a null pointer to member pointer conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2726 | assert(From->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2727 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2728 | "Expr must be null pointer constant!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2729 | Kind = CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2730 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2731 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2732 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2733 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2734 | assert(ToPtrType && "No member pointer cast has a target type " |
| 2735 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2736 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2737 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 2738 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2739 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2740 | // FIXME: What about dependent types? |
| 2741 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 2742 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2743 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2744 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 2745 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2746 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 2747 | assert(DerivationOkay && |
| 2748 | "Should not have been called if derivation isn't OK."); |
| 2749 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2750 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2751 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 2752 | getUnqualifiedType())) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2753 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 2754 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 2755 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 2756 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2757 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2758 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2759 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2760 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 2761 | << FromClass << ToClass << QualType(VBase, 0) |
| 2762 | << From->getSourceRange(); |
| 2763 | return true; |
| 2764 | } |
| 2765 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2766 | if (!IgnoreBaseAccess) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2767 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 2768 | Paths.front(), |
| 2769 | diag::err_downcast_from_inaccessible_base); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2770 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2771 | // Must be a base to derived member conversion. |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2772 | BuildBasePathArray(Paths, BasePath); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2773 | Kind = CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2774 | return false; |
| 2775 | } |
| 2776 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2777 | /// IsQualificationConversion - Determines whether the conversion from |
| 2778 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 2779 | /// (C++ 4.4). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2780 | /// |
| 2781 | /// \param ObjCLifetimeConversion Output parameter that will be set to indicate |
| 2782 | /// when the qualification conversion involves a change in the Objective-C |
| 2783 | /// object lifetime. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2784 | bool |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2785 | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2786 | bool CStyle, bool &ObjCLifetimeConversion) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2787 | FromType = Context.getCanonicalType(FromType); |
| 2788 | ToType = Context.getCanonicalType(ToType); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2789 | ObjCLifetimeConversion = false; |
| 2790 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2791 | // If FromType and ToType are the same type, this is not a |
| 2792 | // qualification conversion. |
Sebastian Redl | 22c9240 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 2793 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2794 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2795 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2796 | // (C++ 4.4p4): |
| 2797 | // A conversion can add cv-qualifiers at levels other than the first |
| 2798 | // in multi-level pointers, subject to the following rules: [...] |
| 2799 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2800 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2801 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2802 | // Within each iteration of the loop, we check the qualifiers to |
| 2803 | // determine if this still looks like a qualification |
| 2804 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2805 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2806 | // until there are no more pointers or pointers-to-members left to |
| 2807 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2808 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2809 | |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2810 | Qualifiers FromQuals = FromType.getQualifiers(); |
| 2811 | Qualifiers ToQuals = ToType.getQualifiers(); |
| 2812 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2813 | // Objective-C ARC: |
| 2814 | // Check Objective-C lifetime conversions. |
| 2815 | if (FromQuals.getObjCLifetime() != ToQuals.getObjCLifetime() && |
| 2816 | UnwrappedAnyPointer) { |
| 2817 | if (ToQuals.compatiblyIncludesObjCLifetime(FromQuals)) { |
| 2818 | ObjCLifetimeConversion = true; |
| 2819 | FromQuals.removeObjCLifetime(); |
| 2820 | ToQuals.removeObjCLifetime(); |
| 2821 | } else { |
| 2822 | // Qualification conversions cannot cast between different |
| 2823 | // Objective-C lifetime qualifiers. |
| 2824 | return false; |
| 2825 | } |
| 2826 | } |
| 2827 | |
Douglas Gregor | 377e1bd | 2011-05-08 06:09:53 +0000 | [diff] [blame] | 2828 | // Allow addition/removal of GC attributes but not changing GC attributes. |
| 2829 | if (FromQuals.getObjCGCAttr() != ToQuals.getObjCGCAttr() && |
| 2830 | (!FromQuals.hasObjCGCAttr() || !ToQuals.hasObjCGCAttr())) { |
| 2831 | FromQuals.removeObjCGCAttr(); |
| 2832 | ToQuals.removeObjCGCAttr(); |
| 2833 | } |
| 2834 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2835 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 2836 | // 2,j, and similarly for volatile. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2837 | if (!CStyle && !ToQuals.compatiblyIncludes(FromQuals)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2838 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2839 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2840 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 2841 | // every cv for 0 < k < j. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2842 | if (!CStyle && FromQuals.getCVRQualifiers() != ToQuals.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2843 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2844 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2845 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2846 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 2847 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2848 | PreviousToQualsIncludeConst |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2849 | = PreviousToQualsIncludeConst && ToQuals.hasConst(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2850 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2851 | |
| 2852 | // We are left with FromType and ToType being the pointee types |
| 2853 | // after unwrapping the original FromType and ToType the same number |
| 2854 | // of types. If we unwrapped any pointers, and if FromType and |
| 2855 | // ToType have the same unqualified type (since we checked |
| 2856 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2857 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2860 | /// \brief - Determine whether this is a conversion from a scalar type to an |
| 2861 | /// atomic type. |
| 2862 | /// |
| 2863 | /// If successful, updates \c SCS's second and third steps in the conversion |
| 2864 | /// sequence to finish the conversion. |
Douglas Gregor | 7d00065 | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 2865 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 2866 | bool InOverloadResolution, |
| 2867 | StandardConversionSequence &SCS, |
| 2868 | bool CStyle) { |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2869 | const AtomicType *ToAtomic = ToType->getAs<AtomicType>(); |
| 2870 | if (!ToAtomic) |
| 2871 | return false; |
| 2872 | |
| 2873 | StandardConversionSequence InnerSCS; |
| 2874 | if (!IsStandardConversion(S, From, ToAtomic->getValueType(), |
| 2875 | InOverloadResolution, InnerSCS, |
| 2876 | CStyle, /*AllowObjCWritebackConversion=*/false)) |
| 2877 | return false; |
| 2878 | |
| 2879 | SCS.Second = InnerSCS.Second; |
| 2880 | SCS.setToType(1, InnerSCS.getToType(1)); |
| 2881 | SCS.Third = InnerSCS.Third; |
| 2882 | SCS.QualificationIncludesObjCLifetime |
| 2883 | = InnerSCS.QualificationIncludesObjCLifetime; |
| 2884 | SCS.setToType(2, InnerSCS.getToType(2)); |
| 2885 | return true; |
| 2886 | } |
| 2887 | |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2888 | static bool isFirstArgumentCompatibleWithType(ASTContext &Context, |
| 2889 | CXXConstructorDecl *Constructor, |
| 2890 | QualType Type) { |
| 2891 | const FunctionProtoType *CtorType = |
| 2892 | Constructor->getType()->getAs<FunctionProtoType>(); |
| 2893 | if (CtorType->getNumArgs() > 0) { |
| 2894 | QualType FirstArg = CtorType->getArgType(0); |
| 2895 | if (Context.hasSameUnqualifiedType(Type, FirstArg.getNonReferenceType())) |
| 2896 | return true; |
| 2897 | } |
| 2898 | return false; |
| 2899 | } |
| 2900 | |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2901 | static OverloadingResult |
| 2902 | IsInitializerListConstructorConversion(Sema &S, Expr *From, QualType ToType, |
| 2903 | CXXRecordDecl *To, |
| 2904 | UserDefinedConversionSequence &User, |
| 2905 | OverloadCandidateSet &CandidateSet, |
| 2906 | bool AllowExplicit) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2907 | DeclContext::lookup_result R = S.LookupConstructors(To); |
| 2908 | for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2909 | Con != ConEnd; ++Con) { |
| 2910 | NamedDecl *D = *Con; |
| 2911 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2912 | |
| 2913 | // Find the constructor (which may be a template). |
| 2914 | CXXConstructorDecl *Constructor = 0; |
| 2915 | FunctionTemplateDecl *ConstructorTmpl |
| 2916 | = dyn_cast<FunctionTemplateDecl>(D); |
| 2917 | if (ConstructorTmpl) |
| 2918 | Constructor |
| 2919 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 2920 | else |
| 2921 | Constructor = cast<CXXConstructorDecl>(D); |
| 2922 | |
| 2923 | bool Usable = !Constructor->isInvalidDecl() && |
| 2924 | S.isInitListConstructor(Constructor) && |
| 2925 | (AllowExplicit || !Constructor->isExplicit()); |
| 2926 | if (Usable) { |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2927 | // If the first argument is (a reference to) the target type, |
| 2928 | // suppress conversions. |
| 2929 | bool SuppressUserConversions = |
| 2930 | isFirstArgumentCompatibleWithType(S.Context, Constructor, ToType); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2931 | if (ConstructorTmpl) |
| 2932 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2933 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2934 | From, CandidateSet, |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2935 | SuppressUserConversions); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2936 | else |
| 2937 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2938 | From, CandidateSet, |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2939 | SuppressUserConversions); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2940 | } |
| 2941 | } |
| 2942 | |
| 2943 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 2944 | |
| 2945 | OverloadCandidateSet::iterator Best; |
| 2946 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
| 2947 | case OR_Success: { |
| 2948 | // Record the standard conversion we used and the conversion function. |
| 2949 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2950 | QualType ThisType = Constructor->getThisType(S.Context); |
| 2951 | // Initializer lists don't have conversions as such. |
| 2952 | User.Before.setAsIdentityConversion(); |
| 2953 | User.HadMultipleCandidates = HadMultipleCandidates; |
| 2954 | User.ConversionFunction = Constructor; |
| 2955 | User.FoundConversionFunction = Best->FoundDecl; |
| 2956 | User.After.setAsIdentityConversion(); |
| 2957 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 2958 | User.After.setAllToTypes(ToType); |
| 2959 | return OR_Success; |
| 2960 | } |
| 2961 | |
| 2962 | case OR_No_Viable_Function: |
| 2963 | return OR_No_Viable_Function; |
| 2964 | case OR_Deleted: |
| 2965 | return OR_Deleted; |
| 2966 | case OR_Ambiguous: |
| 2967 | return OR_Ambiguous; |
| 2968 | } |
| 2969 | |
| 2970 | llvm_unreachable("Invalid OverloadResult!"); |
| 2971 | } |
| 2972 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2973 | /// Determines whether there is a user-defined conversion sequence |
| 2974 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 2975 | /// ToType. If such a conversion exists, User will contain the |
| 2976 | /// user-defined conversion sequence that performs such a conversion |
| 2977 | /// and this routine will return true. Otherwise, this routine returns |
| 2978 | /// false and User is unspecified. |
| 2979 | /// |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2980 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 2981 | /// "explicit" conversion functions as well as non-explicit conversion |
| 2982 | /// functions (C++0x [class.conv.fct]p2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2983 | static OverloadingResult |
| 2984 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2985 | UserDefinedConversionSequence &User, |
| 2986 | OverloadCandidateSet &CandidateSet, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2987 | bool AllowExplicit) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2988 | // Whether we will only visit constructors. |
| 2989 | bool ConstructorsOnly = false; |
| 2990 | |
| 2991 | // If the type we are conversion to is a class type, enumerate its |
| 2992 | // constructors. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2993 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2994 | // C++ [over.match.ctor]p1: |
| 2995 | // When objects of class type are direct-initialized (8.5), or |
| 2996 | // copy-initialized from an expression of the same or a |
| 2997 | // derived class type (8.5), overload resolution selects the |
| 2998 | // constructor. [...] For copy-initialization, the candidate |
| 2999 | // functions are all the converting constructors (12.3.1) of |
| 3000 | // that class. The argument list is the expression-list within |
| 3001 | // the parentheses of the initializer. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3002 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3003 | (From->getType()->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3004 | S.IsDerivedFrom(From->getType(), ToType))) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3005 | ConstructorsOnly = true; |
| 3006 | |
Benjamin Kramer | 63b6ebe | 2012-11-23 17:04:52 +0000 | [diff] [blame] | 3007 | S.RequireCompleteType(From->getExprLoc(), ToType, 0); |
Argyrios Kyrtzidis | e36bca6 | 2011-04-22 17:45:37 +0000 | [diff] [blame] | 3008 | // RequireCompleteType may have returned true due to some invalid decl |
| 3009 | // during template instantiation, but ToType may be complete enough now |
| 3010 | // to try to recover. |
| 3011 | if (ToType->isIncompleteType()) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 3012 | // We're not going to find any constructors. |
| 3013 | } else if (CXXRecordDecl *ToRecordDecl |
| 3014 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3015 | |
| 3016 | Expr **Args = &From; |
| 3017 | unsigned NumArgs = 1; |
| 3018 | bool ListInitializing = false; |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3019 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(From)) { |
Benjamin Kramer | e575359 | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 3020 | // But first, see if there is an init-list-constructor that will work. |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 3021 | OverloadingResult Result = IsInitializerListConstructorConversion( |
| 3022 | S, From, ToType, ToRecordDecl, User, CandidateSet, AllowExplicit); |
| 3023 | if (Result != OR_No_Viable_Function) |
| 3024 | return Result; |
| 3025 | // Never mind. |
| 3026 | CandidateSet.clear(); |
| 3027 | |
| 3028 | // If we're list-initializing, we pass the individual elements as |
| 3029 | // arguments, not the entire list. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3030 | Args = InitList->getInits(); |
| 3031 | NumArgs = InitList->getNumInits(); |
| 3032 | ListInitializing = true; |
| 3033 | } |
| 3034 | |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3035 | DeclContext::lookup_result R = S.LookupConstructors(ToRecordDecl); |
| 3036 | for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3037 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3038 | NamedDecl *D = *Con; |
| 3039 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3040 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3041 | // Find the constructor (which may be a template). |
| 3042 | CXXConstructorDecl *Constructor = 0; |
| 3043 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3044 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3045 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3046 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3047 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 3048 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3049 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3050 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3051 | bool Usable = !Constructor->isInvalidDecl(); |
| 3052 | if (ListInitializing) |
| 3053 | Usable = Usable && (AllowExplicit || !Constructor->isExplicit()); |
| 3054 | else |
| 3055 | Usable = Usable &&Constructor->isConvertingConstructor(AllowExplicit); |
| 3056 | if (Usable) { |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3057 | bool SuppressUserConversions = !ConstructorsOnly; |
| 3058 | if (SuppressUserConversions && ListInitializing) { |
| 3059 | SuppressUserConversions = false; |
| 3060 | if (NumArgs == 1) { |
| 3061 | // If the first argument is (a reference to) the target type, |
| 3062 | // suppress conversions. |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 3063 | SuppressUserConversions = isFirstArgumentCompatibleWithType( |
| 3064 | S.Context, Constructor, ToType); |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3065 | } |
| 3066 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3067 | if (ConstructorTmpl) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3068 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3069 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3070 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3071 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3072 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 3073 | // Allow one user-defined conversion when user specifies a |
| 3074 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3075 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3076 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3077 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3078 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3079 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3080 | } |
| 3081 | } |
| 3082 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3083 | // Enumerate conversion functions, if we're allowed to. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3084 | if (ConstructorsOnly || isa<InitListExpr>(From)) { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3085 | } else if (S.RequireCompleteType(From->getLocStart(), From->getType(), 0)) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3086 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3087 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3088 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3089 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3090 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 3091 | // Add all of the conversion functions as candidates. |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3092 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3093 | CXXRecordDecl::conversion_iterator> |
| 3094 | Conversions = FromRecordDecl->getVisibleConversionFunctions(); |
| 3095 | for (CXXRecordDecl::conversion_iterator |
| 3096 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3097 | DeclAccessPair FoundDecl = I.getPair(); |
| 3098 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3099 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3100 | if (isa<UsingShadowDecl>(D)) |
| 3101 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3102 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3103 | CXXConversionDecl *Conv; |
| 3104 | FunctionTemplateDecl *ConvTemplate; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3105 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 3106 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3107 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3108 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3109 | |
| 3110 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3111 | if (ConvTemplate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3112 | S.AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
| 3113 | ActingContext, From, ToType, |
| 3114 | CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3115 | else |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3116 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, |
| 3117 | From, ToType, CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3118 | } |
| 3119 | } |
| 3120 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3121 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3122 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3123 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3124 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3125 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3126 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3127 | case OR_Success: |
| 3128 | // Record the standard conversion we used and the conversion function. |
| 3129 | if (CXXConstructorDecl *Constructor |
| 3130 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 3131 | // C++ [over.ics.user]p1: |
| 3132 | // If the user-defined conversion is specified by a |
| 3133 | // constructor (12.3.1), the initial standard conversion |
| 3134 | // sequence converts the source type to the type required by |
| 3135 | // the argument of the constructor. |
| 3136 | // |
| 3137 | QualType ThisType = Constructor->getThisType(S.Context); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3138 | if (isa<InitListExpr>(From)) { |
| 3139 | // Initializer lists don't have conversions as such. |
| 3140 | User.Before.setAsIdentityConversion(); |
| 3141 | } else { |
| 3142 | if (Best->Conversions[0].isEllipsis()) |
| 3143 | User.EllipsisConversion = true; |
| 3144 | else { |
| 3145 | User.Before = Best->Conversions[0].Standard; |
| 3146 | User.EllipsisConversion = false; |
| 3147 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3148 | } |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3149 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3150 | User.ConversionFunction = Constructor; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3151 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3152 | User.After.setAsIdentityConversion(); |
| 3153 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 3154 | User.After.setAllToTypes(ToType); |
| 3155 | return OR_Success; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3156 | } |
| 3157 | if (CXXConversionDecl *Conversion |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3158 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 3159 | // C++ [over.ics.user]p1: |
| 3160 | // |
| 3161 | // [...] If the user-defined conversion is specified by a |
| 3162 | // conversion function (12.3.2), the initial standard |
| 3163 | // conversion sequence converts the source type to the |
| 3164 | // implicit object parameter of the conversion function. |
| 3165 | User.Before = Best->Conversions[0].Standard; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3166 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3167 | User.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3168 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3169 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3170 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3171 | // C++ [over.ics.user]p2: |
| 3172 | // The second standard conversion sequence converts the |
| 3173 | // result of the user-defined conversion to the target type |
| 3174 | // for the sequence. Since an implicit conversion sequence |
| 3175 | // is an initialization, the special rules for |
| 3176 | // initialization by user-defined conversion apply when |
| 3177 | // selecting the best user-defined conversion for a |
| 3178 | // user-defined conversion sequence (see 13.3.3 and |
| 3179 | // 13.3.3.1). |
| 3180 | User.After = Best->FinalConversion; |
| 3181 | return OR_Success; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3182 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3183 | llvm_unreachable("Not a constructor or conversion function?"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3184 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3185 | case OR_No_Viable_Function: |
| 3186 | return OR_No_Viable_Function; |
| 3187 | case OR_Deleted: |
| 3188 | // No conversion here! We're done. |
| 3189 | return OR_Deleted; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3190 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3191 | case OR_Ambiguous: |
| 3192 | return OR_Ambiguous; |
| 3193 | } |
| 3194 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3195 | llvm_unreachable("Invalid OverloadResult!"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3196 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3197 | |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3198 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3199 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3200 | ImplicitConversionSequence ICS; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3201 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3202 | OverloadingResult OvResult = |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3203 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3204 | CandidateSet, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3205 | if (OvResult == OR_Ambiguous) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3206 | Diag(From->getLocStart(), |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3207 | diag::err_typecheck_ambiguous_condition) |
| 3208 | << From->getType() << ToType << From->getSourceRange(); |
Larisse Voufo | 7419d01 | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 3209 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) { |
Larisse Voufo | 288f76a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 3210 | if (!RequireCompleteType(From->getLocStart(), ToType, |
Larisse Voufo | 7419d01 | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 3211 | diag::err_typecheck_nonviable_condition_incomplete, |
| 3212 | From->getType(), From->getSourceRange())) |
| 3213 | Diag(From->getLocStart(), |
| 3214 | diag::err_typecheck_nonviable_condition) |
| 3215 | << From->getType() << From->getSourceRange() << ToType; |
| 3216 | } |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3217 | else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3218 | return false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3219 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3220 | return true; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3221 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3222 | |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3223 | /// \brief Compare the user-defined conversion functions or constructors |
| 3224 | /// of two user-defined conversion sequences to determine whether any ordering |
| 3225 | /// is possible. |
| 3226 | static ImplicitConversionSequence::CompareKind |
| 3227 | compareConversionFunctions(Sema &S, |
| 3228 | FunctionDecl *Function1, |
| 3229 | FunctionDecl *Function2) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3230 | if (!S.getLangOpts().ObjC1 || !S.getLangOpts().CPlusPlus11) |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3231 | return ImplicitConversionSequence::Indistinguishable; |
| 3232 | |
| 3233 | // Objective-C++: |
| 3234 | // If both conversion functions are implicitly-declared conversions from |
| 3235 | // a lambda closure type to a function pointer and a block pointer, |
| 3236 | // respectively, always prefer the conversion to a function pointer, |
| 3237 | // because the function pointer is more lightweight and is more likely |
| 3238 | // to keep code working. |
| 3239 | CXXConversionDecl *Conv1 = dyn_cast<CXXConversionDecl>(Function1); |
| 3240 | if (!Conv1) |
| 3241 | return ImplicitConversionSequence::Indistinguishable; |
| 3242 | |
| 3243 | CXXConversionDecl *Conv2 = dyn_cast<CXXConversionDecl>(Function2); |
| 3244 | if (!Conv2) |
| 3245 | return ImplicitConversionSequence::Indistinguishable; |
| 3246 | |
| 3247 | if (Conv1->getParent()->isLambda() && Conv2->getParent()->isLambda()) { |
| 3248 | bool Block1 = Conv1->getConversionType()->isBlockPointerType(); |
| 3249 | bool Block2 = Conv2->getConversionType()->isBlockPointerType(); |
| 3250 | if (Block1 != Block2) |
| 3251 | return Block1? ImplicitConversionSequence::Worse |
| 3252 | : ImplicitConversionSequence::Better; |
| 3253 | } |
| 3254 | |
| 3255 | return ImplicitConversionSequence::Indistinguishable; |
| 3256 | } |
| 3257 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3258 | /// CompareImplicitConversionSequences - Compare two implicit |
| 3259 | /// conversion sequences to determine whether one is better than the |
| 3260 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3261 | static ImplicitConversionSequence::CompareKind |
| 3262 | CompareImplicitConversionSequences(Sema &S, |
| 3263 | const ImplicitConversionSequence& ICS1, |
| 3264 | const ImplicitConversionSequence& ICS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3265 | { |
| 3266 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 3267 | // conversion sequences (as defined in 13.3.3.1) |
| 3268 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 3269 | // conversion sequence than a user-defined conversion sequence or |
| 3270 | // an ellipsis conversion sequence, and |
| 3271 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 3272 | // conversion sequence than an ellipsis conversion sequence |
| 3273 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3274 | // |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3275 | // C++0x [over.best.ics]p10: |
| 3276 | // For the purpose of ranking implicit conversion sequences as |
| 3277 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 3278 | // treated as a user-defined sequence that is indistinguishable |
| 3279 | // from any other user-defined conversion sequence. |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3280 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 3281 | return ImplicitConversionSequence::Better; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3282 | if (ICS2.getKindRank() < ICS1.getKindRank()) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3283 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3284 | |
Benjamin Kramer | b6eee07 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 3285 | // The following checks require both conversion sequences to be of |
| 3286 | // the same kind. |
| 3287 | if (ICS1.getKind() != ICS2.getKind()) |
| 3288 | return ImplicitConversionSequence::Indistinguishable; |
| 3289 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3290 | ImplicitConversionSequence::CompareKind Result = |
| 3291 | ImplicitConversionSequence::Indistinguishable; |
| 3292 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3293 | // Two implicit conversion sequences of the same form are |
| 3294 | // indistinguishable conversion sequences unless one of the |
| 3295 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3296 | if (ICS1.isStandard()) |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3297 | Result = CompareStandardConversionSequences(S, |
| 3298 | ICS1.Standard, ICS2.Standard); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3299 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3300 | // User-defined conversion sequence U1 is a better conversion |
| 3301 | // sequence than another user-defined conversion sequence U2 if |
| 3302 | // they contain the same user-defined conversion function or |
| 3303 | // constructor and if the second standard conversion sequence of |
| 3304 | // U1 is better than the second standard conversion sequence of |
| 3305 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3306 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3307 | ICS2.UserDefined.ConversionFunction) |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3308 | Result = CompareStandardConversionSequences(S, |
| 3309 | ICS1.UserDefined.After, |
| 3310 | ICS2.UserDefined.After); |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3311 | else |
| 3312 | Result = compareConversionFunctions(S, |
| 3313 | ICS1.UserDefined.ConversionFunction, |
| 3314 | ICS2.UserDefined.ConversionFunction); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3315 | } |
| 3316 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3317 | // List-initialization sequence L1 is a better conversion sequence than |
| 3318 | // list-initialization sequence L2 if L1 converts to std::initializer_list<X> |
| 3319 | // for some X and L2 does not. |
| 3320 | if (Result == ImplicitConversionSequence::Indistinguishable && |
Richard Smith | 798186a | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 3321 | !ICS1.isBad()) { |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 3322 | if (ICS1.isStdInitializerListElement() && |
| 3323 | !ICS2.isStdInitializerListElement()) |
| 3324 | return ImplicitConversionSequence::Better; |
| 3325 | if (!ICS1.isStdInitializerListElement() && |
| 3326 | ICS2.isStdInitializerListElement()) |
| 3327 | return ImplicitConversionSequence::Worse; |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3328 | } |
| 3329 | |
| 3330 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3331 | } |
| 3332 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3333 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 3334 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 3335 | Qualifiers Quals; |
| 3336 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3337 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3338 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3339 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3340 | return Context.hasSameUnqualifiedType(T1, T2); |
| 3341 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3342 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3343 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 3344 | // determine if one is a proper subset of the other. |
| 3345 | static ImplicitConversionSequence::CompareKind |
| 3346 | compareStandardConversionSubsets(ASTContext &Context, |
| 3347 | const StandardConversionSequence& SCS1, |
| 3348 | const StandardConversionSequence& SCS2) { |
| 3349 | ImplicitConversionSequence::CompareKind Result |
| 3350 | = ImplicitConversionSequence::Indistinguishable; |
| 3351 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3352 | // the identity conversion sequence is considered to be a subsequence of |
Douglas Gregor | ae65f4b | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 3353 | // any non-identity conversion sequence |
Douglas Gregor | 4ae5b72 | 2011-06-05 06:15:20 +0000 | [diff] [blame] | 3354 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 3355 | return ImplicitConversionSequence::Better; |
| 3356 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 3357 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3358 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3359 | if (SCS1.Second != SCS2.Second) { |
| 3360 | if (SCS1.Second == ICK_Identity) |
| 3361 | Result = ImplicitConversionSequence::Better; |
| 3362 | else if (SCS2.Second == ICK_Identity) |
| 3363 | Result = ImplicitConversionSequence::Worse; |
| 3364 | else |
| 3365 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3366 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3367 | return ImplicitConversionSequence::Indistinguishable; |
| 3368 | |
| 3369 | if (SCS1.Third == SCS2.Third) { |
| 3370 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 3371 | : ImplicitConversionSequence::Indistinguishable; |
| 3372 | } |
| 3373 | |
| 3374 | if (SCS1.Third == ICK_Identity) |
| 3375 | return Result == ImplicitConversionSequence::Worse |
| 3376 | ? ImplicitConversionSequence::Indistinguishable |
| 3377 | : ImplicitConversionSequence::Better; |
| 3378 | |
| 3379 | if (SCS2.Third == ICK_Identity) |
| 3380 | return Result == ImplicitConversionSequence::Better |
| 3381 | ? ImplicitConversionSequence::Indistinguishable |
| 3382 | : ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3383 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3384 | return ImplicitConversionSequence::Indistinguishable; |
| 3385 | } |
| 3386 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3387 | /// \brief Determine whether one of the given reference bindings is better |
| 3388 | /// than the other based on what kind of bindings they are. |
| 3389 | static bool isBetterReferenceBindingKind(const StandardConversionSequence &SCS1, |
| 3390 | const StandardConversionSequence &SCS2) { |
| 3391 | // C++0x [over.ics.rank]p3b4: |
| 3392 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 3393 | // implicit object parameter of a non-static member function declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3394 | // without a ref-qualifier, and *either* S1 binds an rvalue reference |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3395 | // to an rvalue and S2 binds an lvalue reference *or S1 binds an |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3396 | // lvalue reference to a function lvalue and S2 binds an rvalue |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3397 | // reference*. |
| 3398 | // |
| 3399 | // FIXME: Rvalue references. We're going rogue with the above edits, |
| 3400 | // because the semantics in the current C++0x working paper (N3225 at the |
| 3401 | // time of this writing) break the standard definition of std::forward |
| 3402 | // and std::reference_wrapper when dealing with references to functions. |
| 3403 | // Proposed wording changes submitted to CWG for consideration. |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3404 | if (SCS1.BindsImplicitObjectArgumentWithoutRefQualifier || |
| 3405 | SCS2.BindsImplicitObjectArgumentWithoutRefQualifier) |
| 3406 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3407 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3408 | return (!SCS1.IsLvalueReference && SCS1.BindsToRvalue && |
| 3409 | SCS2.IsLvalueReference) || |
| 3410 | (SCS1.IsLvalueReference && SCS1.BindsToFunctionLvalue && |
| 3411 | !SCS2.IsLvalueReference); |
| 3412 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3413 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3414 | /// CompareStandardConversionSequences - Compare two standard |
| 3415 | /// conversion sequences to determine whether one is better than the |
| 3416 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3417 | static ImplicitConversionSequence::CompareKind |
| 3418 | CompareStandardConversionSequences(Sema &S, |
| 3419 | const StandardConversionSequence& SCS1, |
| 3420 | const StandardConversionSequence& SCS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3421 | { |
| 3422 | // Standard conversion sequence S1 is a better conversion sequence |
| 3423 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 3424 | |
| 3425 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 3426 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 3427 | // excluding any Lvalue Transformation; the identity conversion |
| 3428 | // sequence is considered to be a subsequence of any |
| 3429 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3430 | if (ImplicitConversionSequence::CompareKind CK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3431 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3432 | return CK; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3433 | |
| 3434 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 3435 | // defined below), or, if not that, |
| 3436 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 3437 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 3438 | if (Rank1 < Rank2) |
| 3439 | return ImplicitConversionSequence::Better; |
| 3440 | else if (Rank2 < Rank1) |
| 3441 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3442 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3443 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 3444 | // are indistinguishable unless one of the following rules |
| 3445 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3446 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3447 | // A conversion that is not a conversion of a pointer, or |
| 3448 | // pointer to member, to bool is better than another conversion |
| 3449 | // that is such a conversion. |
| 3450 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 3451 | return SCS2.isPointerConversionToBool() |
| 3452 | ? ImplicitConversionSequence::Better |
| 3453 | : ImplicitConversionSequence::Worse; |
| 3454 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3455 | // C++ [over.ics.rank]p4b2: |
| 3456 | // |
| 3457 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3458 | // conversion of B* to A* is better than conversion of B* to |
| 3459 | // void*, and conversion of A* to void* is better than conversion |
| 3460 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3461 | bool SCS1ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3462 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3463 | bool SCS2ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3464 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3465 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 3466 | // Exactly one of the conversion sequences is a conversion to |
| 3467 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3468 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 3469 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3470 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 3471 | // Neither conversion sequence converts to a void pointer; compare |
| 3472 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3473 | if (ImplicitConversionSequence::CompareKind DerivedCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3474 | = CompareDerivedToBaseConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3475 | return DerivedCK; |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3476 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid && |
| 3477 | !S.Context.hasSameType(SCS1.getFromType(), SCS2.getFromType())) { |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3478 | // Both conversion sequences are conversions to void |
| 3479 | // pointers. Compare the source types to determine if there's an |
| 3480 | // inheritance relationship in their sources. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3481 | QualType FromType1 = SCS1.getFromType(); |
| 3482 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3483 | |
| 3484 | // Adjust the types we're converting from via the array-to-pointer |
| 3485 | // conversion, if we need to. |
| 3486 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3487 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3488 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3489 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3490 | |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3491 | QualType FromPointee1 = FromType1->getPointeeType().getUnqualifiedType(); |
| 3492 | QualType FromPointee2 = FromType2->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3493 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3494 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3495 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3496 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3497 | return ImplicitConversionSequence::Worse; |
| 3498 | |
| 3499 | // Objective-C++: If one interface is more specific than the |
| 3500 | // other, it is the better one. |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3501 | const ObjCObjectPointerType* FromObjCPtr1 |
| 3502 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3503 | const ObjCObjectPointerType* FromObjCPtr2 |
| 3504 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3505 | if (FromObjCPtr1 && FromObjCPtr2) { |
| 3506 | bool AssignLeft = S.Context.canAssignObjCInterfaces(FromObjCPtr1, |
| 3507 | FromObjCPtr2); |
| 3508 | bool AssignRight = S.Context.canAssignObjCInterfaces(FromObjCPtr2, |
| 3509 | FromObjCPtr1); |
| 3510 | if (AssignLeft != AssignRight) { |
| 3511 | return AssignLeft? ImplicitConversionSequence::Better |
| 3512 | : ImplicitConversionSequence::Worse; |
| 3513 | } |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3514 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3515 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3516 | |
| 3517 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 3518 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3519 | if (ImplicitConversionSequence::CompareKind QualCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3520 | = CompareQualificationConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3521 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3522 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3523 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3524 | // Check for a better reference binding based on the kind of bindings. |
| 3525 | if (isBetterReferenceBindingKind(SCS1, SCS2)) |
| 3526 | return ImplicitConversionSequence::Better; |
| 3527 | else if (isBetterReferenceBindingKind(SCS2, SCS1)) |
| 3528 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3529 | |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 3530 | // C++ [over.ics.rank]p3b4: |
| 3531 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 3532 | // which the references refer are the same type except for |
| 3533 | // top-level cv-qualifiers, and the type to which the reference |
| 3534 | // initialized by S2 refers is more cv-qualified than the type |
| 3535 | // to which the reference initialized by S1 refers. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3536 | QualType T1 = SCS1.getToType(2); |
| 3537 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3538 | T1 = S.Context.getCanonicalType(T1); |
| 3539 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3540 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3541 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3542 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3543 | if (UnqualT1 == UnqualT2) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3544 | // Objective-C++ ARC: If the references refer to objects with different |
| 3545 | // lifetimes, prefer bindings that don't change lifetime. |
| 3546 | if (SCS1.ObjCLifetimeConversionBinding != |
| 3547 | SCS2.ObjCLifetimeConversionBinding) { |
| 3548 | return SCS1.ObjCLifetimeConversionBinding |
| 3549 | ? ImplicitConversionSequence::Worse |
| 3550 | : ImplicitConversionSequence::Better; |
| 3551 | } |
| 3552 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3553 | // If the type is an array type, promote the element qualifiers to the |
| 3554 | // type for comparison. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3555 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3556 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3557 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3558 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3559 | if (T2.isMoreQualifiedThan(T1)) |
| 3560 | return ImplicitConversionSequence::Better; |
| 3561 | else if (T1.isMoreQualifiedThan(T2)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3562 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3563 | } |
| 3564 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3565 | |
Francois Pichet | 1c98d62 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3566 | // In Microsoft mode, prefer an integral conversion to a |
| 3567 | // floating-to-integral conversion if the integral conversion |
| 3568 | // is between types of the same size. |
| 3569 | // For example: |
| 3570 | // void f(float); |
| 3571 | // void f(int); |
| 3572 | // int main { |
| 3573 | // long a; |
| 3574 | // f(a); |
| 3575 | // } |
| 3576 | // Here, MSVC will call f(int) instead of generating a compile error |
| 3577 | // as clang will do in standard mode. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3578 | if (S.getLangOpts().MicrosoftMode && |
Francois Pichet | 1c98d62 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3579 | SCS1.Second == ICK_Integral_Conversion && |
| 3580 | SCS2.Second == ICK_Floating_Integral && |
| 3581 | S.Context.getTypeSize(SCS1.getFromType()) == |
| 3582 | S.Context.getTypeSize(SCS1.getToType(2))) |
| 3583 | return ImplicitConversionSequence::Better; |
| 3584 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3585 | return ImplicitConversionSequence::Indistinguishable; |
| 3586 | } |
| 3587 | |
| 3588 | /// CompareQualificationConversions - Compares two standard conversion |
| 3589 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3590 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 3591 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3592 | CompareQualificationConversions(Sema &S, |
| 3593 | const StandardConversionSequence& SCS1, |
| 3594 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 3595 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3596 | // -- S1 and S2 differ only in their qualification conversion and |
| 3597 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 3598 | // cv-qualification signature of type T1 is a proper subset of |
| 3599 | // the cv-qualification signature of type T2, and S1 is not the |
| 3600 | // deprecated string literal array-to-pointer conversion (4.2). |
| 3601 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 3602 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 3603 | return ImplicitConversionSequence::Indistinguishable; |
| 3604 | |
| 3605 | // FIXME: the example in the standard doesn't use a qualification |
| 3606 | // conversion (!) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3607 | QualType T1 = SCS1.getToType(2); |
| 3608 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3609 | T1 = S.Context.getCanonicalType(T1); |
| 3610 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3611 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3612 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3613 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3614 | |
| 3615 | // If the types are the same, we won't learn anything by unwrapped |
| 3616 | // them. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3617 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3618 | return ImplicitConversionSequence::Indistinguishable; |
| 3619 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3620 | // If the type is an array type, promote the element qualifiers to the type |
| 3621 | // for comparison. |
| 3622 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3623 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3624 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3625 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3626 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3627 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3628 | = ImplicitConversionSequence::Indistinguishable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3629 | |
| 3630 | // Objective-C++ ARC: |
| 3631 | // Prefer qualification conversions not involving a change in lifetime |
| 3632 | // to qualification conversions that do not change lifetime. |
| 3633 | if (SCS1.QualificationIncludesObjCLifetime != |
| 3634 | SCS2.QualificationIncludesObjCLifetime) { |
| 3635 | Result = SCS1.QualificationIncludesObjCLifetime |
| 3636 | ? ImplicitConversionSequence::Worse |
| 3637 | : ImplicitConversionSequence::Better; |
| 3638 | } |
| 3639 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3640 | while (S.Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3641 | // Within each iteration of the loop, we check the qualifiers to |
| 3642 | // determine if this still looks like a qualification |
| 3643 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 3644 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3645 | // until there are no more pointers or pointers-to-members left |
| 3646 | // to unwrap. This essentially mimics what |
| 3647 | // IsQualificationConversion does, but here we're checking for a |
| 3648 | // strict subset of qualifiers. |
| 3649 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 3650 | // The qualifiers are the same, so this doesn't tell us anything |
| 3651 | // about how the sequences rank. |
| 3652 | ; |
| 3653 | else if (T2.isMoreQualifiedThan(T1)) { |
| 3654 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 3655 | if (Result == ImplicitConversionSequence::Worse) |
| 3656 | // Neither has qualifiers that are a subset of the other's |
| 3657 | // qualifiers. |
| 3658 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3659 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3660 | Result = ImplicitConversionSequence::Better; |
| 3661 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 3662 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 3663 | if (Result == ImplicitConversionSequence::Better) |
| 3664 | // Neither has qualifiers that are a subset of the other's |
| 3665 | // qualifiers. |
| 3666 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3667 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3668 | Result = ImplicitConversionSequence::Worse; |
| 3669 | } else { |
| 3670 | // Qualifiers are disjoint. |
| 3671 | return ImplicitConversionSequence::Indistinguishable; |
| 3672 | } |
| 3673 | |
| 3674 | // If the types after this point are equivalent, we're done. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3675 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3676 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3677 | } |
| 3678 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3679 | // Check that the winning standard conversion sequence isn't using |
| 3680 | // the deprecated string literal array to pointer conversion. |
| 3681 | switch (Result) { |
| 3682 | case ImplicitConversionSequence::Better: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3683 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3684 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3685 | break; |
| 3686 | |
| 3687 | case ImplicitConversionSequence::Indistinguishable: |
| 3688 | break; |
| 3689 | |
| 3690 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3691 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3692 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3693 | break; |
| 3694 | } |
| 3695 | |
| 3696 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3697 | } |
| 3698 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3699 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 3700 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3701 | /// various kinds of derived-to-base conversions (C++ |
| 3702 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 3703 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3704 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3705 | CompareDerivedToBaseConversions(Sema &S, |
| 3706 | const StandardConversionSequence& SCS1, |
| 3707 | const StandardConversionSequence& SCS2) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3708 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3709 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3710 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3711 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3712 | |
| 3713 | // Adjust the types we're converting from via the array-to-pointer |
| 3714 | // conversion, if we need to. |
| 3715 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3716 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3717 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3718 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3719 | |
| 3720 | // Canonicalize all of the types. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3721 | FromType1 = S.Context.getCanonicalType(FromType1); |
| 3722 | ToType1 = S.Context.getCanonicalType(ToType1); |
| 3723 | FromType2 = S.Context.getCanonicalType(FromType2); |
| 3724 | ToType2 = S.Context.getCanonicalType(ToType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3725 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3726 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3727 | // |
| 3728 | // If class B is derived directly or indirectly from class A and |
| 3729 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3730 | // |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3731 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3732 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 3733 | SCS2.Second == ICK_Pointer_Conversion && |
| 3734 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 3735 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 3736 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3737 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3738 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3739 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3740 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3741 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3742 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3743 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3744 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3745 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3746 | // -- conversion of C* to B* is better than conversion of C* to A*, |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3747 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3748 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3749 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3750 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3751 | return ImplicitConversionSequence::Worse; |
| 3752 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3753 | |
| 3754 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 3755 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3756 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3757 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3758 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3759 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3760 | } |
| 3761 | } else if (SCS1.Second == ICK_Pointer_Conversion && |
| 3762 | SCS2.Second == ICK_Pointer_Conversion) { |
| 3763 | const ObjCObjectPointerType *FromPtr1 |
| 3764 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3765 | const ObjCObjectPointerType *FromPtr2 |
| 3766 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3767 | const ObjCObjectPointerType *ToPtr1 |
| 3768 | = ToType1->getAs<ObjCObjectPointerType>(); |
| 3769 | const ObjCObjectPointerType *ToPtr2 |
| 3770 | = ToType2->getAs<ObjCObjectPointerType>(); |
| 3771 | |
| 3772 | if (FromPtr1 && FromPtr2 && ToPtr1 && ToPtr2) { |
| 3773 | // Apply the same conversion ranking rules for Objective-C pointer types |
| 3774 | // that we do for C++ pointers to class types. However, we employ the |
| 3775 | // Objective-C pseudo-subtyping relationship used for assignment of |
| 3776 | // Objective-C pointer types. |
| 3777 | bool FromAssignLeft |
| 3778 | = S.Context.canAssignObjCInterfaces(FromPtr1, FromPtr2); |
| 3779 | bool FromAssignRight |
| 3780 | = S.Context.canAssignObjCInterfaces(FromPtr2, FromPtr1); |
| 3781 | bool ToAssignLeft |
| 3782 | = S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2); |
| 3783 | bool ToAssignRight |
| 3784 | = S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1); |
| 3785 | |
| 3786 | // A conversion to an a non-id object pointer type or qualified 'id' |
| 3787 | // type is better than a conversion to 'id'. |
| 3788 | if (ToPtr1->isObjCIdType() && |
| 3789 | (ToPtr2->isObjCQualifiedIdType() || ToPtr2->getInterfaceDecl())) |
| 3790 | return ImplicitConversionSequence::Worse; |
| 3791 | if (ToPtr2->isObjCIdType() && |
| 3792 | (ToPtr1->isObjCQualifiedIdType() || ToPtr1->getInterfaceDecl())) |
| 3793 | return ImplicitConversionSequence::Better; |
| 3794 | |
| 3795 | // A conversion to a non-id object pointer type is better than a |
| 3796 | // conversion to a qualified 'id' type |
| 3797 | if (ToPtr1->isObjCQualifiedIdType() && ToPtr2->getInterfaceDecl()) |
| 3798 | return ImplicitConversionSequence::Worse; |
| 3799 | if (ToPtr2->isObjCQualifiedIdType() && ToPtr1->getInterfaceDecl()) |
| 3800 | return ImplicitConversionSequence::Better; |
| 3801 | |
| 3802 | // A conversion to an a non-Class object pointer type or qualified 'Class' |
| 3803 | // type is better than a conversion to 'Class'. |
| 3804 | if (ToPtr1->isObjCClassType() && |
| 3805 | (ToPtr2->isObjCQualifiedClassType() || ToPtr2->getInterfaceDecl())) |
| 3806 | return ImplicitConversionSequence::Worse; |
| 3807 | if (ToPtr2->isObjCClassType() && |
| 3808 | (ToPtr1->isObjCQualifiedClassType() || ToPtr1->getInterfaceDecl())) |
| 3809 | return ImplicitConversionSequence::Better; |
| 3810 | |
| 3811 | // A conversion to a non-Class object pointer type is better than a |
| 3812 | // conversion to a qualified 'Class' type. |
| 3813 | if (ToPtr1->isObjCQualifiedClassType() && ToPtr2->getInterfaceDecl()) |
| 3814 | return ImplicitConversionSequence::Worse; |
| 3815 | if (ToPtr2->isObjCQualifiedClassType() && ToPtr1->getInterfaceDecl()) |
| 3816 | return ImplicitConversionSequence::Better; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3817 | |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3818 | // -- "conversion of C* to B* is better than conversion of C* to A*," |
| 3819 | if (S.Context.hasSameType(FromType1, FromType2) && |
| 3820 | !FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType() && |
| 3821 | (ToAssignLeft != ToAssignRight)) |
| 3822 | return ToAssignLeft? ImplicitConversionSequence::Worse |
| 3823 | : ImplicitConversionSequence::Better; |
| 3824 | |
| 3825 | // -- "conversion of B* to A* is better than conversion of C* to A*," |
| 3826 | if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) && |
| 3827 | (FromAssignLeft != FromAssignRight)) |
| 3828 | return FromAssignLeft? ImplicitConversionSequence::Better |
| 3829 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3830 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3831 | } |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3832 | |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3833 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3834 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 3835 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 3836 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3837 | const MemberPointerType * FromMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3838 | FromType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3839 | const MemberPointerType * ToMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3840 | ToType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3841 | const MemberPointerType * FromMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3842 | FromType2->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3843 | const MemberPointerType * ToMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3844 | ToType2->getAs<MemberPointerType>(); |
| 3845 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 3846 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 3847 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 3848 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 3849 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 3850 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 3851 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 3852 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3853 | // conversion of A::* to B::* is better than conversion of A::* to C::*, |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3854 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3855 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3856 | return ImplicitConversionSequence::Worse; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3857 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3858 | return ImplicitConversionSequence::Better; |
| 3859 | } |
| 3860 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 3861 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3862 | if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3863 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3864 | else if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3865 | return ImplicitConversionSequence::Worse; |
| 3866 | } |
| 3867 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3868 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3869 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3870 | // -- conversion of C to B is better than conversion of C to A, |
Douglas Gregor | 9e23932 | 2010-02-25 19:01:05 +0000 | [diff] [blame] | 3871 | // -- binding of an expression of type C to a reference of type |
| 3872 | // B& is better than binding an expression of type C to a |
| 3873 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3874 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3875 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3876 | if (S.IsDerivedFrom(ToType1, ToType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3877 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3878 | else if (S.IsDerivedFrom(ToType2, ToType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3879 | return ImplicitConversionSequence::Worse; |
| 3880 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3881 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3882 | // -- conversion of B to A is better than conversion of C to A. |
Douglas Gregor | 9e23932 | 2010-02-25 19:01:05 +0000 | [diff] [blame] | 3883 | // -- binding of an expression of type B to a reference of type |
| 3884 | // A& is better than binding an expression of type C to a |
| 3885 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3886 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3887 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3888 | if (S.IsDerivedFrom(FromType2, FromType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3889 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3890 | else if (S.IsDerivedFrom(FromType1, FromType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3891 | return ImplicitConversionSequence::Worse; |
| 3892 | } |
| 3893 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3894 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3895 | return ImplicitConversionSequence::Indistinguishable; |
| 3896 | } |
| 3897 | |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 3898 | /// \brief Determine whether the given type is valid, e.g., it is not an invalid |
| 3899 | /// C++ class. |
| 3900 | static bool isTypeValid(QualType T) { |
| 3901 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) |
| 3902 | return !Record->isInvalidDecl(); |
| 3903 | |
| 3904 | return true; |
| 3905 | } |
| 3906 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3907 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 3908 | /// determine whether they are reference-related, |
| 3909 | /// reference-compatible, reference-compatible with added |
| 3910 | /// qualification, or incompatible, for use in C++ initialization by |
| 3911 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 3912 | /// type, and the first type (T1) is the pointee type of the reference |
| 3913 | /// type being initialized. |
| 3914 | Sema::ReferenceCompareResult |
| 3915 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 3916 | QualType OrigT1, QualType OrigT2, |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3917 | bool &DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3918 | bool &ObjCConversion, |
| 3919 | bool &ObjCLifetimeConversion) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3920 | assert(!OrigT1->isReferenceType() && |
| 3921 | "T1 must be the pointee type of the reference type"); |
| 3922 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 3923 | |
| 3924 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 3925 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 3926 | Qualifiers T1Quals, T2Quals; |
| 3927 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3928 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 3929 | |
| 3930 | // C++ [dcl.init.ref]p4: |
| 3931 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 3932 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 3933 | // T1 is a base class of T2. |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3934 | DerivedToBase = false; |
| 3935 | ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3936 | ObjCLifetimeConversion = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3937 | if (UnqualT1 == UnqualT2) { |
| 3938 | // Nothing to do. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3939 | } else if (!RequireCompleteType(Loc, OrigT2, 0) && |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 3940 | isTypeValid(UnqualT1) && isTypeValid(UnqualT2) && |
| 3941 | IsDerivedFrom(UnqualT2, UnqualT1)) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3942 | DerivedToBase = true; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3943 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
| 3944 | UnqualT2->isObjCObjectOrInterfaceType() && |
| 3945 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
| 3946 | ObjCConversion = true; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3947 | else |
| 3948 | return Ref_Incompatible; |
| 3949 | |
| 3950 | // At this point, we know that T1 and T2 are reference-related (at |
| 3951 | // least). |
| 3952 | |
| 3953 | // If the type is an array type, promote the element qualifiers to the type |
| 3954 | // for comparison. |
| 3955 | if (isa<ArrayType>(T1) && T1Quals) |
| 3956 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 3957 | if (isa<ArrayType>(T2) && T2Quals) |
| 3958 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 3959 | |
| 3960 | // C++ [dcl.init.ref]p4: |
| 3961 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 3962 | // reference-related to T2 and cv1 is the same cv-qualification |
| 3963 | // as, or greater cv-qualification than, cv2. For purposes of |
| 3964 | // overload resolution, cases for which cv1 is greater |
| 3965 | // cv-qualification than cv2 are identified as |
| 3966 | // reference-compatible with added qualification (see 13.3.3.2). |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 3967 | // |
| 3968 | // Note that we also require equivalence of Objective-C GC and address-space |
| 3969 | // qualifiers when performing these computations, so that e.g., an int in |
| 3970 | // address space 1 is not reference-compatible with an int in address |
| 3971 | // space 2. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3972 | if (T1Quals.getObjCLifetime() != T2Quals.getObjCLifetime() && |
| 3973 | T1Quals.compatiblyIncludesObjCLifetime(T2Quals)) { |
| 3974 | T1Quals.removeObjCLifetime(); |
| 3975 | T2Quals.removeObjCLifetime(); |
| 3976 | ObjCLifetimeConversion = true; |
| 3977 | } |
| 3978 | |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 3979 | if (T1Quals == T2Quals) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3980 | return Ref_Compatible; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3981 | else if (T1Quals.compatiblyIncludes(T2Quals)) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3982 | return Ref_Compatible_With_Added_Qualification; |
| 3983 | else |
| 3984 | return Ref_Related; |
| 3985 | } |
| 3986 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3987 | /// \brief Look for a user-defined conversion to an value reference-compatible |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3988 | /// with DeclType. Return true if something definite is found. |
| 3989 | static bool |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3990 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
| 3991 | QualType DeclType, SourceLocation DeclLoc, |
| 3992 | Expr *Init, QualType T2, bool AllowRvalues, |
| 3993 | bool AllowExplicit) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3994 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 3995 | CXXRecordDecl *T2RecordDecl |
| 3996 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 3997 | |
| 3998 | OverloadCandidateSet CandidateSet(DeclLoc); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3999 | std::pair<CXXRecordDecl::conversion_iterator, |
| 4000 | CXXRecordDecl::conversion_iterator> |
| 4001 | Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 4002 | for (CXXRecordDecl::conversion_iterator |
| 4003 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4004 | NamedDecl *D = *I; |
| 4005 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4006 | if (isa<UsingShadowDecl>(D)) |
| 4007 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 4008 | |
| 4009 | FunctionTemplateDecl *ConvTemplate |
| 4010 | = dyn_cast<FunctionTemplateDecl>(D); |
| 4011 | CXXConversionDecl *Conv; |
| 4012 | if (ConvTemplate) |
| 4013 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 4014 | else |
| 4015 | Conv = cast<CXXConversionDecl>(D); |
| 4016 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4017 | // If this is an explicit conversion, and we're not allowed to consider |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4018 | // explicit conversions, skip it. |
| 4019 | if (!AllowExplicit && Conv->isExplicit()) |
| 4020 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4021 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4022 | if (AllowRvalues) { |
| 4023 | bool DerivedToBase = false; |
| 4024 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4025 | bool ObjCLifetimeConversion = false; |
Douglas Gregor | 203050c | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4026 | |
| 4027 | // If we are initializing an rvalue reference, don't permit conversion |
| 4028 | // functions that return lvalues. |
| 4029 | if (!ConvTemplate && DeclType->isRValueReferenceType()) { |
| 4030 | const ReferenceType *RefType |
| 4031 | = Conv->getConversionType()->getAs<LValueReferenceType>(); |
| 4032 | if (RefType && !RefType->getPointeeType()->isFunctionType()) |
| 4033 | continue; |
| 4034 | } |
| 4035 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4036 | if (!ConvTemplate && |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4037 | S.CompareReferenceRelationship( |
| 4038 | DeclLoc, |
| 4039 | Conv->getConversionType().getNonReferenceType() |
| 4040 | .getUnqualifiedType(), |
| 4041 | DeclType.getNonReferenceType().getUnqualifiedType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4042 | DerivedToBase, ObjCConversion, ObjCLifetimeConversion) == |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4043 | Sema::Ref_Incompatible) |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4044 | continue; |
| 4045 | } else { |
| 4046 | // If the conversion function doesn't return a reference type, |
| 4047 | // it can't be considered for this conversion. An rvalue reference |
| 4048 | // is only acceptable if its referencee is a function type. |
| 4049 | |
| 4050 | const ReferenceType *RefType = |
| 4051 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 4052 | if (!RefType || |
| 4053 | (!RefType->isLValueReferenceType() && |
| 4054 | !RefType->getPointeeType()->isFunctionType())) |
| 4055 | continue; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4056 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4057 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4058 | if (ConvTemplate) |
| 4059 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4060 | Init, DeclType, CandidateSet); |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4061 | else |
| 4062 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4063 | DeclType, CandidateSet); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4064 | } |
| 4065 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4066 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4067 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4068 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4069 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4070 | case OR_Success: |
| 4071 | // C++ [over.ics.ref]p1: |
| 4072 | // |
| 4073 | // [...] If the parameter binds directly to the result of |
| 4074 | // applying a conversion function to the argument |
| 4075 | // expression, the implicit conversion sequence is a |
| 4076 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 4077 | // second standard conversion sequence either an identity |
| 4078 | // conversion or, if the conversion function returns an |
| 4079 | // entity of a type that is a derived class of the parameter |
| 4080 | // type, a derived-to-base Conversion. |
| 4081 | if (!Best->FinalConversion.DirectBinding) |
| 4082 | return false; |
| 4083 | |
| 4084 | ICS.setUserDefined(); |
| 4085 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 4086 | ICS.UserDefined.After = Best->FinalConversion; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4087 | ICS.UserDefined.HadMultipleCandidates = HadMultipleCandidates; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4088 | ICS.UserDefined.ConversionFunction = Best->Function; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 4089 | ICS.UserDefined.FoundConversionFunction = Best->FoundDecl; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4090 | ICS.UserDefined.EllipsisConversion = false; |
| 4091 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 4092 | ICS.UserDefined.After.DirectBinding && |
| 4093 | "Expected a direct reference binding!"); |
| 4094 | return true; |
| 4095 | |
| 4096 | case OR_Ambiguous: |
| 4097 | ICS.setAmbiguous(); |
| 4098 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4099 | Cand != CandidateSet.end(); ++Cand) |
| 4100 | if (Cand->Viable) |
| 4101 | ICS.Ambiguous.addConversion(Cand->Function); |
| 4102 | return true; |
| 4103 | |
| 4104 | case OR_No_Viable_Function: |
| 4105 | case OR_Deleted: |
| 4106 | // There was no suitable conversion, or we found a deleted |
| 4107 | // conversion; continue with other checks. |
| 4108 | return false; |
| 4109 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4110 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4111 | llvm_unreachable("Invalid OverloadResult!"); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4114 | /// \brief Compute an implicit conversion sequence for reference |
| 4115 | /// initialization. |
| 4116 | static ImplicitConversionSequence |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4117 | TryReferenceInit(Sema &S, Expr *Init, QualType DeclType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4118 | SourceLocation DeclLoc, |
| 4119 | bool SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 4120 | bool AllowExplicit) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4121 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 4122 | |
| 4123 | // Most paths end in a failed conversion. |
| 4124 | ImplicitConversionSequence ICS; |
| 4125 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4126 | |
| 4127 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 4128 | QualType T2 = Init->getType(); |
| 4129 | |
| 4130 | // If the initializer is the address of an overloaded function, try |
| 4131 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4132 | // type of the resulting function. |
| 4133 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4134 | DeclAccessPair Found; |
| 4135 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 4136 | false, Found)) |
| 4137 | T2 = Fn->getType(); |
| 4138 | } |
| 4139 | |
| 4140 | // Compute some basic properties of the types and the initializer. |
| 4141 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 4142 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4143 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4144 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4145 | Expr::Classification InitCategory = Init->Classify(S.Context); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4146 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4147 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4148 | ObjCConversion, ObjCLifetimeConversion); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4149 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4150 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4151 | // C++0x [dcl.init.ref]p5: |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 4152 | // A reference to type "cv1 T1" is initialized by an expression |
| 4153 | // of type "cv2 T2" as follows: |
| 4154 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4155 | // -- If reference is an lvalue reference and the initializer expression |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4156 | if (!isRValRef) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4157 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 4158 | // reference-compatible with "cv2 T2," or |
| 4159 | // |
| 4160 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 4161 | if (InitCategory.isLValue() && |
| 4162 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4163 | // C++ [over.ics.ref]p1: |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4164 | // When a parameter of reference type binds directly (8.5.3) |
| 4165 | // to an argument expression, the implicit conversion sequence |
| 4166 | // is the identity conversion, unless the argument expression |
| 4167 | // has a type that is a derived class of the parameter type, |
| 4168 | // in which case the implicit conversion sequence is a |
| 4169 | // derived-to-base Conversion (13.3.3.1). |
| 4170 | ICS.setStandard(); |
| 4171 | ICS.Standard.First = ICK_Identity; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4172 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 4173 | : ObjCConversion? ICK_Compatible_Conversion |
| 4174 | : ICK_Identity; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4175 | ICS.Standard.Third = ICK_Identity; |
| 4176 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4177 | ICS.Standard.setToType(0, T2); |
| 4178 | ICS.Standard.setToType(1, T1); |
| 4179 | ICS.Standard.setToType(2, T1); |
| 4180 | ICS.Standard.ReferenceBinding = true; |
| 4181 | ICS.Standard.DirectBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4182 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4183 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4184 | ICS.Standard.BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4185 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4186 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4187 | ICS.Standard.CopyConstructor = 0; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4188 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4189 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 4190 | // derived-to-base conversions is suppressed when we're |
| 4191 | // computing the implicit conversion sequence (C++ |
| 4192 | // [over.best.ics]p2). |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4193 | return ICS; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4194 | } |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4195 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4196 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 4197 | // not reference-related to T2, and can be implicitly |
| 4198 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 4199 | // is reference-compatible with "cv3 T3" 92) (this |
| 4200 | // conversion is selected by enumerating the applicable |
| 4201 | // conversion functions (13.3.1.6) and choosing the best |
| 4202 | // one through overload resolution (13.3)), |
| 4203 | if (!SuppressUserConversions && T2->isRecordType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4204 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4205 | RefRelationship == Sema::Ref_Incompatible) { |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4206 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4207 | Init, T2, /*AllowRvalues=*/false, |
| 4208 | AllowExplicit)) |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4209 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4210 | } |
| 4211 | } |
| 4212 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4213 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 4214 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4215 | // shall be an rvalue reference. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4216 | // |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 4217 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 4218 | // point, which is that, due to p2 (which short-circuits reference |
| 4219 | // binding by only attempting a simple conversion for non-direct |
| 4220 | // bindings) and p3's strange wording, we allow a const volatile |
| 4221 | // reference to bind to an rvalue. Hence the check for the presence |
| 4222 | // of "const" rather than checking for "const" being the only |
| 4223 | // qualifier. |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4224 | // This is also the point where rvalue references and lvalue inits no longer |
| 4225 | // go together. |
Richard Smith | 8ab10aa | 2012-05-24 04:29:20 +0000 | [diff] [blame] | 4226 | if (!isRValRef && (!T1.isConstQualified() || T1.isVolatileQualified())) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4227 | return ICS; |
| 4228 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4229 | // -- If the initializer expression |
| 4230 | // |
| 4231 | // -- is an xvalue, class prvalue, array prvalue or function |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4232 | // lvalue and "cv1 T1" is reference-compatible with "cv2 T2", or |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4233 | if (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification && |
| 4234 | (InitCategory.isXValue() || |
| 4235 | (InitCategory.isPRValue() && (T2->isRecordType() || T2->isArrayType())) || |
| 4236 | (InitCategory.isLValue() && T2->isFunctionType()))) { |
| 4237 | ICS.setStandard(); |
| 4238 | ICS.Standard.First = ICK_Identity; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4239 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4240 | : ObjCConversion? ICK_Compatible_Conversion |
| 4241 | : ICK_Identity; |
| 4242 | ICS.Standard.Third = ICK_Identity; |
| 4243 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4244 | ICS.Standard.setToType(0, T2); |
| 4245 | ICS.Standard.setToType(1, T1); |
| 4246 | ICS.Standard.setToType(2, T1); |
| 4247 | ICS.Standard.ReferenceBinding = true; |
| 4248 | // In C++0x, this is always a direct binding. In C++98/03, it's a direct |
| 4249 | // binding unless we're binding to a class prvalue. |
| 4250 | // Note: Although xvalues wouldn't normally show up in C++98/03 code, we |
| 4251 | // allow the use of rvalue references in C++98/03 for the benefit of |
| 4252 | // standard library implementors; therefore, we need the xvalue check here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4253 | ICS.Standard.DirectBinding = |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4254 | S.getLangOpts().CPlusPlus11 || |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4255 | (InitCategory.isPRValue() && !T2->isRecordType()); |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4256 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4257 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4258 | ICS.Standard.BindsToRvalue = InitCategory.isRValue(); |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4259 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4260 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4261 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4262 | return ICS; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4263 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4264 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4265 | // -- has a class type (i.e., T2 is a class type), where T1 is not |
| 4266 | // reference-related to T2, and can be implicitly converted to |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4267 | // an xvalue, class prvalue, or function lvalue of type |
| 4268 | // "cv3 T3", where "cv1 T1" is reference-compatible with |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4269 | // "cv3 T3", |
| 4270 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4271 | // then the reference is bound to the value of the initializer |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4272 | // expression in the first case and to the result of the conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4273 | // in the second case (or, in either case, to an appropriate base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4274 | // class subobject). |
| 4275 | if (!SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4276 | T2->isRecordType() && !S.RequireCompleteType(DeclLoc, T2, 0) && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4277 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4278 | Init, T2, /*AllowRvalues=*/true, |
| 4279 | AllowExplicit)) { |
| 4280 | // In the second case, if the reference is an rvalue reference |
| 4281 | // and the second standard conversion sequence of the |
| 4282 | // user-defined conversion sequence includes an lvalue-to-rvalue |
| 4283 | // conversion, the program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4284 | if (ICS.isUserDefined() && isRValRef && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4285 | ICS.UserDefined.After.First == ICK_Lvalue_To_Rvalue) |
| 4286 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4287 | |
Douglas Gregor | 68ed68b | 2011-01-21 16:36:05 +0000 | [diff] [blame] | 4288 | return ICS; |
Rafael Espindola | aa5952c | 2011-01-22 15:32:35 +0000 | [diff] [blame] | 4289 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4290 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4291 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 4292 | // initialized from the initializer expression using the |
| 4293 | // rules for a non-reference copy initialization (8.5). The |
| 4294 | // reference is then bound to the temporary. If T1 is |
| 4295 | // reference-related to T2, cv1 must be the same |
| 4296 | // cv-qualification as, or greater cv-qualification than, |
| 4297 | // cv2; otherwise, the program is ill-formed. |
| 4298 | if (RefRelationship == Sema::Ref_Related) { |
| 4299 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 4300 | // we would be reference-compatible or reference-compatible with |
| 4301 | // added qualification. But that wasn't the case, so the reference |
| 4302 | // initialization fails. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4303 | // |
| 4304 | // Note that we only want to check address spaces and cvr-qualifiers here. |
| 4305 | // ObjC GC and lifetime qualifiers aren't important. |
| 4306 | Qualifiers T1Quals = T1.getQualifiers(); |
| 4307 | Qualifiers T2Quals = T2.getQualifiers(); |
| 4308 | T1Quals.removeObjCGCAttr(); |
| 4309 | T1Quals.removeObjCLifetime(); |
| 4310 | T2Quals.removeObjCGCAttr(); |
| 4311 | T2Quals.removeObjCLifetime(); |
| 4312 | if (!T1Quals.compatiblyIncludes(T2Quals)) |
| 4313 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4314 | } |
| 4315 | |
| 4316 | // If at least one of the types is a class type, the types are not |
| 4317 | // related, and we aren't allowed any user conversions, the |
| 4318 | // reference binding fails. This case is important for breaking |
| 4319 | // recursion, since TryImplicitConversion below will attempt to |
| 4320 | // create a temporary through the use of a copy constructor. |
| 4321 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 4322 | (T1->isRecordType() || T2->isRecordType())) |
| 4323 | return ICS; |
| 4324 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4325 | // If T1 is reference-related to T2 and the reference is an rvalue |
| 4326 | // reference, the initializer expression shall not be an lvalue. |
| 4327 | if (RefRelationship >= Sema::Ref_Related && |
| 4328 | isRValRef && Init->Classify(S.Context).isLValue()) |
| 4329 | return ICS; |
| 4330 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4331 | // C++ [over.ics.ref]p2: |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4332 | // When a parameter of reference type is not bound directly to |
| 4333 | // an argument expression, the conversion sequence is the one |
| 4334 | // required to convert the argument expression to the |
| 4335 | // underlying type of the reference according to |
| 4336 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 4337 | // to copy-initializing a temporary of the underlying type with |
| 4338 | // the argument expression. Any difference in top-level |
| 4339 | // cv-qualification is subsumed by the initialization itself |
| 4340 | // and does not constitute a conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4341 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
| 4342 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4343 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4344 | /*CStyle=*/false, |
| 4345 | /*AllowObjCWritebackConversion=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4346 | |
| 4347 | // Of course, that's still a reference binding. |
| 4348 | if (ICS.isStandard()) { |
| 4349 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4350 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4351 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4352 | ICS.Standard.BindsToRvalue = true; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4353 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4354 | ICS.Standard.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4355 | } else if (ICS.isUserDefined()) { |
Douglas Gregor | 203050c | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4356 | // Don't allow rvalue references to bind to lvalues. |
| 4357 | if (DeclType->isRValueReferenceType()) { |
| 4358 | if (const ReferenceType *RefType |
| 4359 | = ICS.UserDefined.ConversionFunction->getResultType() |
| 4360 | ->getAs<LValueReferenceType>()) { |
| 4361 | if (!RefType->getPointeeType()->isFunctionType()) { |
| 4362 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, Init, |
| 4363 | DeclType); |
| 4364 | return ICS; |
| 4365 | } |
| 4366 | } |
| 4367 | } |
| 4368 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4369 | ICS.UserDefined.After.ReferenceBinding = true; |
Douglas Gregor | f20d272 | 2011-08-15 13:59:46 +0000 | [diff] [blame] | 4370 | ICS.UserDefined.After.IsLvalueReference = !isRValRef; |
| 4371 | ICS.UserDefined.After.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4372 | ICS.UserDefined.After.BindsToRvalue = true; |
| 4373 | ICS.UserDefined.After.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4374 | ICS.UserDefined.After.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4375 | } |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4376 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4377 | return ICS; |
| 4378 | } |
| 4379 | |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4380 | static ImplicitConversionSequence |
| 4381 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
| 4382 | bool SuppressUserConversions, |
| 4383 | bool InOverloadResolution, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4384 | bool AllowObjCWritebackConversion, |
| 4385 | bool AllowExplicit = false); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4386 | |
| 4387 | /// TryListConversion - Try to copy-initialize a value of type ToType from the |
| 4388 | /// initializer list From. |
| 4389 | static ImplicitConversionSequence |
| 4390 | TryListConversion(Sema &S, InitListExpr *From, QualType ToType, |
| 4391 | bool SuppressUserConversions, |
| 4392 | bool InOverloadResolution, |
| 4393 | bool AllowObjCWritebackConversion) { |
| 4394 | // C++11 [over.ics.list]p1: |
| 4395 | // When an argument is an initializer list, it is not an expression and |
| 4396 | // special rules apply for converting it to a parameter type. |
| 4397 | |
| 4398 | ImplicitConversionSequence Result; |
| 4399 | Result.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 4400 | |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4401 | // 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] | 4402 | // initialized from init lists. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4403 | if (S.RequireCompleteType(From->getLocStart(), ToType, 0)) |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4404 | return Result; |
| 4405 | |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4406 | // C++11 [over.ics.list]p2: |
| 4407 | // If the parameter type is std::initializer_list<X> or "array of X" and |
| 4408 | // all the elements can be implicitly converted to X, the implicit |
| 4409 | // conversion sequence is the worst conversion necessary to convert an |
| 4410 | // element of the list to X. |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4411 | bool toStdInitializerList = false; |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4412 | QualType X; |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4413 | if (ToType->isArrayType()) |
Richard Smith | 2801d9a | 2012-12-09 06:48:56 +0000 | [diff] [blame] | 4414 | X = S.Context.getAsArrayType(ToType)->getElementType(); |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4415 | else |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4416 | toStdInitializerList = S.isStdInitializerList(ToType, &X); |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4417 | if (!X.isNull()) { |
| 4418 | for (unsigned i = 0, e = From->getNumInits(); i < e; ++i) { |
| 4419 | Expr *Init = From->getInit(i); |
| 4420 | ImplicitConversionSequence ICS = |
| 4421 | TryCopyInitialization(S, Init, X, SuppressUserConversions, |
| 4422 | InOverloadResolution, |
| 4423 | AllowObjCWritebackConversion); |
| 4424 | // If a single element isn't convertible, fail. |
| 4425 | if (ICS.isBad()) { |
| 4426 | Result = ICS; |
| 4427 | break; |
| 4428 | } |
| 4429 | // Otherwise, look for the worst conversion. |
| 4430 | if (Result.isBad() || |
| 4431 | CompareImplicitConversionSequences(S, ICS, Result) == |
| 4432 | ImplicitConversionSequence::Worse) |
| 4433 | Result = ICS; |
| 4434 | } |
Douglas Gregor | 5b4bf13 | 2012-04-04 23:09:20 +0000 | [diff] [blame] | 4435 | |
| 4436 | // For an empty list, we won't have computed any conversion sequence. |
| 4437 | // Introduce the identity conversion sequence. |
| 4438 | if (From->getNumInits() == 0) { |
| 4439 | Result.setStandard(); |
| 4440 | Result.Standard.setAsIdentityConversion(); |
| 4441 | Result.Standard.setFromType(ToType); |
| 4442 | Result.Standard.setAllToTypes(ToType); |
| 4443 | } |
| 4444 | |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4445 | Result.setStdInitializerListElement(toStdInitializerList); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4446 | return Result; |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4447 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4448 | |
| 4449 | // C++11 [over.ics.list]p3: |
| 4450 | // Otherwise, if the parameter is a non-aggregate class X and overload |
| 4451 | // resolution chooses a single best constructor [...] the implicit |
| 4452 | // conversion sequence is a user-defined conversion sequence. If multiple |
| 4453 | // constructors are viable but none is better than the others, the |
| 4454 | // implicit conversion sequence is a user-defined conversion sequence. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4455 | if (ToType->isRecordType() && !ToType->isAggregateType()) { |
| 4456 | // This function can deal with initializer lists. |
Richard Smith | 798186a | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 4457 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 4458 | /*AllowExplicit=*/false, |
| 4459 | InOverloadResolution, /*CStyle=*/false, |
| 4460 | AllowObjCWritebackConversion); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4461 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4462 | |
| 4463 | // C++11 [over.ics.list]p4: |
| 4464 | // Otherwise, if the parameter has an aggregate type which can be |
| 4465 | // initialized from the initializer list [...] the implicit conversion |
| 4466 | // sequence is a user-defined conversion sequence. |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4467 | if (ToType->isAggregateType()) { |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4468 | // Type is an aggregate, argument is an init list. At this point it comes |
| 4469 | // down to checking whether the initialization works. |
| 4470 | // FIXME: Find out whether this parameter is consumed or not. |
| 4471 | InitializedEntity Entity = |
| 4472 | InitializedEntity::InitializeParameter(S.Context, ToType, |
| 4473 | /*Consumed=*/false); |
| 4474 | if (S.CanPerformCopyInitialization(Entity, S.Owned(From))) { |
| 4475 | Result.setUserDefined(); |
| 4476 | Result.UserDefined.Before.setAsIdentityConversion(); |
| 4477 | // Initializer lists don't have a type. |
| 4478 | Result.UserDefined.Before.setFromType(QualType()); |
| 4479 | Result.UserDefined.Before.setAllToTypes(QualType()); |
| 4480 | |
| 4481 | Result.UserDefined.After.setAsIdentityConversion(); |
| 4482 | Result.UserDefined.After.setFromType(ToType); |
| 4483 | Result.UserDefined.After.setAllToTypes(ToType); |
Benjamin Kramer | 83db10e | 2012-02-02 19:35:29 +0000 | [diff] [blame] | 4484 | Result.UserDefined.ConversionFunction = 0; |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4485 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4486 | return Result; |
| 4487 | } |
| 4488 | |
| 4489 | // C++11 [over.ics.list]p5: |
| 4490 | // 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] | 4491 | if (ToType->isReferenceType()) { |
| 4492 | // The standard is notoriously unclear here, since 13.3.3.1.4 doesn't |
| 4493 | // mention initializer lists in any way. So we go by what list- |
| 4494 | // initialization would do and try to extrapolate from that. |
| 4495 | |
| 4496 | QualType T1 = ToType->getAs<ReferenceType>()->getPointeeType(); |
| 4497 | |
| 4498 | // If the initializer list has a single element that is reference-related |
| 4499 | // to the parameter type, we initialize the reference from that. |
| 4500 | if (From->getNumInits() == 1) { |
| 4501 | Expr *Init = From->getInit(0); |
| 4502 | |
| 4503 | QualType T2 = Init->getType(); |
| 4504 | |
| 4505 | // If the initializer is the address of an overloaded function, try |
| 4506 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4507 | // type of the resulting function. |
| 4508 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4509 | DeclAccessPair Found; |
| 4510 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction( |
| 4511 | Init, ToType, false, Found)) |
| 4512 | T2 = Fn->getType(); |
| 4513 | } |
| 4514 | |
| 4515 | // Compute some basic properties of the types and the initializer. |
| 4516 | bool dummy1 = false; |
| 4517 | bool dummy2 = false; |
| 4518 | bool dummy3 = false; |
| 4519 | Sema::ReferenceCompareResult RefRelationship |
| 4520 | = S.CompareReferenceRelationship(From->getLocStart(), T1, T2, dummy1, |
| 4521 | dummy2, dummy3); |
| 4522 | |
Richard Smith | 3082be2 | 2013-09-06 01:22:42 +0000 | [diff] [blame] | 4523 | if (RefRelationship >= Sema::Ref_Related) { |
Richard Smith | 798186a | 2013-09-06 22:30:28 +0000 | [diff] [blame] | 4524 | return TryReferenceInit(S, Init, ToType, /*FIXME*/From->getLocStart(), |
| 4525 | SuppressUserConversions, |
| 4526 | /*AllowExplicit=*/false); |
Richard Smith | 3082be2 | 2013-09-06 01:22:42 +0000 | [diff] [blame] | 4527 | } |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4528 | } |
| 4529 | |
| 4530 | // Otherwise, we bind the reference to a temporary created from the |
| 4531 | // initializer list. |
| 4532 | Result = TryListConversion(S, From, T1, SuppressUserConversions, |
| 4533 | InOverloadResolution, |
| 4534 | AllowObjCWritebackConversion); |
| 4535 | if (Result.isFailure()) |
| 4536 | return Result; |
| 4537 | assert(!Result.isEllipsis() && |
| 4538 | "Sub-initialization cannot result in ellipsis conversion."); |
| 4539 | |
| 4540 | // Can we even bind to a temporary? |
| 4541 | if (ToType->isRValueReferenceType() || |
| 4542 | (T1.isConstQualified() && !T1.isVolatileQualified())) { |
| 4543 | StandardConversionSequence &SCS = Result.isStandard() ? Result.Standard : |
| 4544 | Result.UserDefined.After; |
| 4545 | SCS.ReferenceBinding = true; |
| 4546 | SCS.IsLvalueReference = ToType->isLValueReferenceType(); |
| 4547 | SCS.BindsToRvalue = true; |
| 4548 | SCS.BindsToFunctionLvalue = false; |
| 4549 | SCS.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4550 | SCS.ObjCLifetimeConversionBinding = false; |
| 4551 | } else |
| 4552 | Result.setBad(BadConversionSequence::lvalue_ref_to_rvalue, |
| 4553 | From, ToType); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4554 | return Result; |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4555 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4556 | |
| 4557 | // C++11 [over.ics.list]p6: |
| 4558 | // Otherwise, if the parameter type is not a class: |
| 4559 | if (!ToType->isRecordType()) { |
| 4560 | // - if the initializer list has one element, the implicit conversion |
| 4561 | // sequence is the one required to convert the element to the |
| 4562 | // parameter type. |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4563 | unsigned NumInits = From->getNumInits(); |
| 4564 | if (NumInits == 1) |
| 4565 | Result = TryCopyInitialization(S, From->getInit(0), ToType, |
| 4566 | SuppressUserConversions, |
| 4567 | InOverloadResolution, |
| 4568 | AllowObjCWritebackConversion); |
| 4569 | // - if the initializer list has no elements, the implicit conversion |
| 4570 | // sequence is the identity conversion. |
| 4571 | else if (NumInits == 0) { |
| 4572 | Result.setStandard(); |
| 4573 | Result.Standard.setAsIdentityConversion(); |
John McCall | e14ba2c | 2012-04-04 02:40:27 +0000 | [diff] [blame] | 4574 | Result.Standard.setFromType(ToType); |
| 4575 | Result.Standard.setAllToTypes(ToType); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4576 | } |
| 4577 | return Result; |
| 4578 | } |
| 4579 | |
| 4580 | // C++11 [over.ics.list]p7: |
| 4581 | // In all cases other than those enumerated above, no conversion is possible |
| 4582 | return Result; |
| 4583 | } |
| 4584 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4585 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 4586 | /// ToType from the expression From. Return the implicit conversion |
| 4587 | /// sequence required to pass this argument, which may be a bad |
| 4588 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4589 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | 74e386e | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 4590 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4591 | static ImplicitConversionSequence |
| 4592 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4593 | bool SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4594 | bool InOverloadResolution, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4595 | bool AllowObjCWritebackConversion, |
| 4596 | bool AllowExplicit) { |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4597 | if (InitListExpr *FromInitList = dyn_cast<InitListExpr>(From)) |
| 4598 | return TryListConversion(S, FromInitList, ToType, SuppressUserConversions, |
| 4599 | InOverloadResolution,AllowObjCWritebackConversion); |
| 4600 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4601 | if (ToType->isReferenceType()) |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4602 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4603 | /*FIXME:*/From->getLocStart(), |
| 4604 | SuppressUserConversions, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4605 | AllowExplicit); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4606 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4607 | return TryImplicitConversion(S, From, ToType, |
| 4608 | SuppressUserConversions, |
| 4609 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4610 | InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4611 | /*CStyle=*/false, |
| 4612 | AllowObjCWritebackConversion); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4613 | } |
| 4614 | |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 4615 | static bool TryCopyInitialization(const CanQualType FromQTy, |
| 4616 | const CanQualType ToQTy, |
| 4617 | Sema &S, |
| 4618 | SourceLocation Loc, |
| 4619 | ExprValueKind FromVK) { |
| 4620 | OpaqueValueExpr TmpExpr(Loc, FromQTy, FromVK); |
| 4621 | ImplicitConversionSequence ICS = |
| 4622 | TryCopyInitialization(S, &TmpExpr, ToQTy, true, true, false); |
| 4623 | |
| 4624 | return !ICS.isBad(); |
| 4625 | } |
| 4626 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4627 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 4628 | /// parameter of the given member function (@c Method) from the |
| 4629 | /// expression @p From. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4630 | static ImplicitConversionSequence |
Richard Smith | 98bfbf5 | 2013-01-26 02:07:32 +0000 | [diff] [blame] | 4631 | TryObjectArgumentInitialization(Sema &S, QualType FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4632 | Expr::Classification FromClassification, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4633 | CXXMethodDecl *Method, |
| 4634 | CXXRecordDecl *ActingContext) { |
| 4635 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 4636 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 4637 | // const volatile object. |
| 4638 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 4639 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4640 | QualType ImplicitParamType = S.Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4641 | |
| 4642 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 4643 | // to exit early. |
| 4644 | ImplicitConversionSequence ICS; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4645 | |
| 4646 | // We need to have an object of class type. |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4647 | if (const PointerType *PT = FromType->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4648 | FromType = PT->getPointeeType(); |
| 4649 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4650 | // When we had a pointer, it's implicitly dereferenced, so we |
| 4651 | // better have an lvalue. |
| 4652 | assert(FromClassification.isLValue()); |
| 4653 | } |
| 4654 | |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4655 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4656 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4657 | // C++0x [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4658 | // For non-static member functions, the type of the implicit object |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4659 | // parameter is |
| 4660 | // |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4661 | // - "lvalue reference to cv X" for functions declared without a |
| 4662 | // ref-qualifier or with the & ref-qualifier |
| 4663 | // - "rvalue reference to cv X" for functions declared with the && |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4664 | // ref-qualifier |
| 4665 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4666 | // 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] | 4667 | // cv-qualification on the member function declaration. |
| 4668 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4669 | // However, when finding an implicit conversion sequence for the argument, we |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4670 | // are not allowed to create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4671 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 4672 | // reference binding here, that allows class rvalues to bind to |
| 4673 | // non-constant references. |
| 4674 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4675 | // First check the qualifiers. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4676 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4677 | if (ImplicitParamType.getCVRQualifiers() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4678 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4679 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4680 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
Richard Smith | 98bfbf5 | 2013-01-26 02:07:32 +0000 | [diff] [blame] | 4681 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4682 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4683 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4684 | |
| 4685 | // Check that we have either the same type or a derived type. It |
| 4686 | // affects the conversion rank. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4687 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4688 | ImplicitConversionKind SecondKind; |
| 4689 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 4690 | SecondKind = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4691 | } else if (S.IsDerivedFrom(FromType, ClassType)) |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4692 | SecondKind = ICK_Derived_To_Base; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4693 | else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4694 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 4695 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4696 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4697 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4698 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4699 | // Check the ref-qualifier. |
| 4700 | switch (Method->getRefQualifier()) { |
| 4701 | case RQ_None: |
| 4702 | // Do nothing; we don't care about lvalueness or rvalueness. |
| 4703 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4704 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4705 | case RQ_LValue: |
| 4706 | if (!FromClassification.isLValue() && Quals != Qualifiers::Const) { |
| 4707 | // non-const lvalue reference cannot bind to an rvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4708 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4709 | ImplicitParamType); |
| 4710 | return ICS; |
| 4711 | } |
| 4712 | break; |
| 4713 | |
| 4714 | case RQ_RValue: |
| 4715 | if (!FromClassification.isRValue()) { |
| 4716 | // rvalue reference cannot bind to an lvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4717 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4718 | ImplicitParamType); |
| 4719 | return ICS; |
| 4720 | } |
| 4721 | break; |
| 4722 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4723 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4724 | // Success. Mark this as a reference binding. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4725 | ICS.setStandard(); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4726 | ICS.Standard.setAsIdentityConversion(); |
| 4727 | ICS.Standard.Second = SecondKind; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4728 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 4729 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4730 | ICS.Standard.ReferenceBinding = true; |
| 4731 | ICS.Standard.DirectBinding = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4732 | ICS.Standard.IsLvalueReference = Method->getRefQualifier() != RQ_RValue; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4733 | ICS.Standard.BindsToFunctionLvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4734 | ICS.Standard.BindsToRvalue = FromClassification.isRValue(); |
| 4735 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier |
| 4736 | = (Method->getRefQualifier() == RQ_None); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4737 | return ICS; |
| 4738 | } |
| 4739 | |
| 4740 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 4741 | /// the implicit object parameter for the given Method with the given |
| 4742 | /// expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4743 | ExprResult |
| 4744 | Sema::PerformObjectArgumentInitialization(Expr *From, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4745 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4746 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4747 | CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4748 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4749 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4750 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4751 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4752 | Expr::Classification FromClassification; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4753 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4754 | FromRecordType = PT->getPointeeType(); |
| 4755 | DestType = Method->getThisType(Context); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4756 | FromClassification = Expr::Classification::makeSimpleLValue(); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4757 | } else { |
| 4758 | FromRecordType = From->getType(); |
| 4759 | DestType = ImplicitParamRecordType; |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4760 | FromClassification = From->Classify(Context); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4761 | } |
| 4762 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4763 | // Note that we always use the true parent context when performing |
| 4764 | // the actual argument initialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4765 | ImplicitConversionSequence ICS |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4766 | = TryObjectArgumentInitialization(*this, From->getType(), FromClassification, |
| 4767 | Method, Method->getParent()); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4768 | if (ICS.isBad()) { |
| 4769 | if (ICS.Bad.Kind == BadConversionSequence::bad_qualifiers) { |
| 4770 | Qualifiers FromQs = FromRecordType.getQualifiers(); |
| 4771 | Qualifiers ToQs = DestType.getQualifiers(); |
| 4772 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 4773 | if (CVR) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4774 | Diag(From->getLocStart(), |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4775 | diag::err_member_function_call_bad_cvr) |
| 4776 | << Method->getDeclName() << FromRecordType << (CVR - 1) |
| 4777 | << From->getSourceRange(); |
| 4778 | Diag(Method->getLocation(), diag::note_previous_decl) |
| 4779 | << Method->getDeclName(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4780 | return ExprError(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4781 | } |
| 4782 | } |
| 4783 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4784 | return Diag(From->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4785 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4786 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4787 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4788 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4789 | if (ICS.Standard.Second == ICK_Derived_To_Base) { |
| 4790 | ExprResult FromRes = |
| 4791 | PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
| 4792 | if (FromRes.isInvalid()) |
| 4793 | return ExprError(); |
| 4794 | From = FromRes.take(); |
| 4795 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4796 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4797 | if (!Context.hasSameType(From->getType(), DestType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4798 | From = ImpCastExprToType(From, DestType, CK_NoOp, |
Richard Smith | acdfa4d | 2011-11-10 23:32:36 +0000 | [diff] [blame] | 4799 | From->getValueKind()).take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4800 | return Owned(From); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4801 | } |
| 4802 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4803 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 4804 | /// expression From to bool (C++0x [conv]p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4805 | static ImplicitConversionSequence |
| 4806 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
Douglas Gregor | c6dfe19 | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 4807 | // FIXME: This is pretty broken. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4808 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 4809 | // FIXME: Are these flags correct? |
| 4810 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4811 | /*AllowExplicit=*/true, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4812 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4813 | /*CStyle=*/false, |
| 4814 | /*AllowObjCWritebackConversion=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4815 | } |
| 4816 | |
| 4817 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 4818 | /// of the expression From to bool (C++0x [conv]p3). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4819 | ExprResult Sema::PerformContextuallyConvertToBool(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4820 | if (checkPlaceholderForOverload(*this, From)) |
| 4821 | return ExprError(); |
| 4822 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4823 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4824 | if (!ICS.isBad()) |
| 4825 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4826 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 4827 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4828 | return Diag(From->getLocStart(), |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4829 | diag::err_typecheck_bool_condition) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 4830 | << From->getType() << From->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4831 | return ExprError(); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4832 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4833 | |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4834 | /// Check that the specified conversion is permitted in a converted constant |
| 4835 | /// expression, according to C++11 [expr.const]p3. Return true if the conversion |
| 4836 | /// is acceptable. |
| 4837 | static bool CheckConvertedConstantConversions(Sema &S, |
| 4838 | StandardConversionSequence &SCS) { |
| 4839 | // Since we know that the target type is an integral or unscoped enumeration |
| 4840 | // type, most conversion kinds are impossible. All possible First and Third |
| 4841 | // conversions are fine. |
| 4842 | switch (SCS.Second) { |
| 4843 | case ICK_Identity: |
| 4844 | case ICK_Integral_Promotion: |
| 4845 | case ICK_Integral_Conversion: |
Guy Benyei | 6959acd | 2013-02-07 16:05:33 +0000 | [diff] [blame] | 4846 | case ICK_Zero_Event_Conversion: |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4847 | return true; |
| 4848 | |
| 4849 | case ICK_Boolean_Conversion: |
Richard Smith | 2bcb984 | 2012-09-13 22:00:12 +0000 | [diff] [blame] | 4850 | // Conversion from an integral or unscoped enumeration type to bool is |
| 4851 | // classified as ICK_Boolean_Conversion, but it's also an integral |
| 4852 | // conversion, so it's permitted in a converted constant expression. |
| 4853 | return SCS.getFromType()->isIntegralOrUnscopedEnumerationType() && |
| 4854 | SCS.getToType(2)->isBooleanType(); |
| 4855 | |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4856 | case ICK_Floating_Integral: |
| 4857 | case ICK_Complex_Real: |
| 4858 | return false; |
| 4859 | |
| 4860 | case ICK_Lvalue_To_Rvalue: |
| 4861 | case ICK_Array_To_Pointer: |
| 4862 | case ICK_Function_To_Pointer: |
| 4863 | case ICK_NoReturn_Adjustment: |
| 4864 | case ICK_Qualification: |
| 4865 | case ICK_Compatible_Conversion: |
| 4866 | case ICK_Vector_Conversion: |
| 4867 | case ICK_Vector_Splat: |
| 4868 | case ICK_Derived_To_Base: |
| 4869 | case ICK_Pointer_Conversion: |
| 4870 | case ICK_Pointer_Member: |
| 4871 | case ICK_Block_Pointer_Conversion: |
| 4872 | case ICK_Writeback_Conversion: |
| 4873 | case ICK_Floating_Promotion: |
| 4874 | case ICK_Complex_Promotion: |
| 4875 | case ICK_Complex_Conversion: |
| 4876 | case ICK_Floating_Conversion: |
| 4877 | case ICK_TransparentUnionConversion: |
| 4878 | llvm_unreachable("unexpected second conversion kind"); |
| 4879 | |
| 4880 | case ICK_Num_Conversion_Kinds: |
| 4881 | break; |
| 4882 | } |
| 4883 | |
| 4884 | llvm_unreachable("unknown conversion kind"); |
| 4885 | } |
| 4886 | |
| 4887 | /// CheckConvertedConstantExpression - Check that the expression From is a |
| 4888 | /// converted constant expression of type T, perform the conversion and produce |
| 4889 | /// the converted expression, per C++11 [expr.const]p3. |
| 4890 | ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, |
| 4891 | llvm::APSInt &Value, |
| 4892 | CCEKind CCE) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4893 | assert(LangOpts.CPlusPlus11 && "converted constant expression outside C++11"); |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4894 | assert(T->isIntegralOrEnumerationType() && "unexpected converted const type"); |
| 4895 | |
| 4896 | if (checkPlaceholderForOverload(*this, From)) |
| 4897 | return ExprError(); |
| 4898 | |
| 4899 | // C++11 [expr.const]p3 with proposed wording fixes: |
| 4900 | // A converted constant expression of type T is a core constant expression, |
| 4901 | // implicitly converted to a prvalue of type T, where the converted |
| 4902 | // expression is a literal constant expression and the implicit conversion |
| 4903 | // sequence contains only user-defined conversions, lvalue-to-rvalue |
| 4904 | // conversions, integral promotions, and integral conversions other than |
| 4905 | // narrowing conversions. |
| 4906 | ImplicitConversionSequence ICS = |
| 4907 | TryImplicitConversion(From, T, |
| 4908 | /*SuppressUserConversions=*/false, |
| 4909 | /*AllowExplicit=*/false, |
| 4910 | /*InOverloadResolution=*/false, |
| 4911 | /*CStyle=*/false, |
| 4912 | /*AllowObjcWritebackConversion=*/false); |
| 4913 | StandardConversionSequence *SCS = 0; |
| 4914 | switch (ICS.getKind()) { |
| 4915 | case ImplicitConversionSequence::StandardConversion: |
| 4916 | if (!CheckConvertedConstantConversions(*this, ICS.Standard)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4917 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4918 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 4919 | << From->getType() << From->getSourceRange() << T; |
| 4920 | SCS = &ICS.Standard; |
| 4921 | break; |
| 4922 | case ImplicitConversionSequence::UserDefinedConversion: |
| 4923 | // We are converting from class type to an integral or enumeration type, so |
| 4924 | // the Before sequence must be trivial. |
| 4925 | if (!CheckConvertedConstantConversions(*this, ICS.UserDefined.After)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4926 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4927 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 4928 | << From->getType() << From->getSourceRange() << T; |
| 4929 | SCS = &ICS.UserDefined.After; |
| 4930 | break; |
| 4931 | case ImplicitConversionSequence::AmbiguousConversion: |
| 4932 | case ImplicitConversionSequence::BadConversion: |
| 4933 | if (!DiagnoseMultipleUserDefinedConversion(From, T)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4934 | return Diag(From->getLocStart(), |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4935 | diag::err_typecheck_converted_constant_expression) |
| 4936 | << From->getType() << From->getSourceRange() << T; |
| 4937 | return ExprError(); |
| 4938 | |
| 4939 | case ImplicitConversionSequence::EllipsisConversion: |
| 4940 | llvm_unreachable("ellipsis conversion in converted constant expression"); |
| 4941 | } |
| 4942 | |
| 4943 | ExprResult Result = PerformImplicitConversion(From, T, ICS, AA_Converting); |
| 4944 | if (Result.isInvalid()) |
| 4945 | return Result; |
| 4946 | |
| 4947 | // Check for a narrowing implicit conversion. |
| 4948 | APValue PreNarrowingValue; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4949 | QualType PreNarrowingType; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4950 | switch (SCS->getNarrowingKind(Context, Result.get(), PreNarrowingValue, |
| 4951 | PreNarrowingType)) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4952 | case NK_Variable_Narrowing: |
| 4953 | // Implicit conversion to a narrower type, and the value is not a constant |
| 4954 | // expression. We'll diagnose this in a moment. |
| 4955 | case NK_Not_Narrowing: |
| 4956 | break; |
| 4957 | |
| 4958 | case NK_Constant_Narrowing: |
Eli Friedman | 1ef28db | 2012-03-29 23:39:39 +0000 | [diff] [blame] | 4959 | Diag(From->getLocStart(), |
| 4960 | isSFINAEContext() ? diag::err_cce_narrowing_sfinae : |
| 4961 | diag::err_cce_narrowing) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4962 | << CCE << /*Constant*/1 |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4963 | << PreNarrowingValue.getAsString(Context, PreNarrowingType) << T; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4964 | break; |
| 4965 | |
| 4966 | case NK_Type_Narrowing: |
Eli Friedman | 1ef28db | 2012-03-29 23:39:39 +0000 | [diff] [blame] | 4967 | Diag(From->getLocStart(), |
| 4968 | isSFINAEContext() ? diag::err_cce_narrowing_sfinae : |
| 4969 | diag::err_cce_narrowing) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4970 | << CCE << /*Constant*/0 << From->getType() << T; |
| 4971 | break; |
| 4972 | } |
| 4973 | |
| 4974 | // Check the expression is a constant expression. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4975 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4976 | Expr::EvalResult Eval; |
| 4977 | Eval.Diag = &Notes; |
| 4978 | |
Douglas Gregor | 484f6fa | 2013-04-08 23:24:07 +0000 | [diff] [blame] | 4979 | if (!Result.get()->EvaluateAsRValue(Eval, Context) || !Eval.Val.isInt()) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4980 | // The expression can't be folded, so we can't keep it at this position in |
| 4981 | // the AST. |
| 4982 | Result = ExprError(); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 4983 | } else { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4984 | Value = Eval.Val.getInt(); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 4985 | |
| 4986 | if (Notes.empty()) { |
| 4987 | // It's a constant expression. |
| 4988 | return Result; |
| 4989 | } |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4990 | } |
| 4991 | |
| 4992 | // It's not a constant expression. Produce an appropriate diagnostic. |
| 4993 | if (Notes.size() == 1 && |
| 4994 | Notes[0].second.getDiagID() == diag::note_invalid_subexpr_in_const_expr) |
| 4995 | Diag(Notes[0].first, diag::err_expr_not_cce) << CCE; |
| 4996 | else { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4997 | Diag(From->getLocStart(), diag::err_expr_not_cce) |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4998 | << CCE << From->getSourceRange(); |
| 4999 | for (unsigned I = 0; I < Notes.size(); ++I) |
| 5000 | Diag(Notes[I].first, Notes[I].second); |
| 5001 | } |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5002 | return Result; |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5003 | } |
| 5004 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5005 | /// dropPointerConversions - If the given standard conversion sequence |
| 5006 | /// involves any pointer conversions, remove them. This may change |
| 5007 | /// the result type of the conversion sequence. |
| 5008 | static void dropPointerConversion(StandardConversionSequence &SCS) { |
| 5009 | if (SCS.Second == ICK_Pointer_Conversion) { |
| 5010 | SCS.Second = ICK_Identity; |
| 5011 | SCS.Third = ICK_Identity; |
| 5012 | SCS.ToTypePtrs[2] = SCS.ToTypePtrs[1] = SCS.ToTypePtrs[0]; |
| 5013 | } |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5014 | } |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5015 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5016 | /// TryContextuallyConvertToObjCPointer - Attempt to contextually |
| 5017 | /// convert the expression From to an Objective-C pointer type. |
| 5018 | static ImplicitConversionSequence |
| 5019 | TryContextuallyConvertToObjCPointer(Sema &S, Expr *From) { |
| 5020 | // Do an implicit conversion to 'id'. |
| 5021 | QualType Ty = S.Context.getObjCIdType(); |
| 5022 | ImplicitConversionSequence ICS |
| 5023 | = TryImplicitConversion(S, From, Ty, |
| 5024 | // FIXME: Are these flags correct? |
| 5025 | /*SuppressUserConversions=*/false, |
| 5026 | /*AllowExplicit=*/true, |
| 5027 | /*InOverloadResolution=*/false, |
| 5028 | /*CStyle=*/false, |
| 5029 | /*AllowObjCWritebackConversion=*/false); |
| 5030 | |
| 5031 | // Strip off any final conversions to 'id'. |
| 5032 | switch (ICS.getKind()) { |
| 5033 | case ImplicitConversionSequence::BadConversion: |
| 5034 | case ImplicitConversionSequence::AmbiguousConversion: |
| 5035 | case ImplicitConversionSequence::EllipsisConversion: |
| 5036 | break; |
| 5037 | |
| 5038 | case ImplicitConversionSequence::UserDefinedConversion: |
| 5039 | dropPointerConversion(ICS.UserDefined.After); |
| 5040 | break; |
| 5041 | |
| 5042 | case ImplicitConversionSequence::StandardConversion: |
| 5043 | dropPointerConversion(ICS.Standard); |
| 5044 | break; |
| 5045 | } |
| 5046 | |
| 5047 | return ICS; |
| 5048 | } |
| 5049 | |
| 5050 | /// PerformContextuallyConvertToObjCPointer - Perform a contextual |
| 5051 | /// conversion of the expression From to an Objective-C pointer type. |
| 5052 | ExprResult Sema::PerformContextuallyConvertToObjCPointer(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 5053 | if (checkPlaceholderForOverload(*this, From)) |
| 5054 | return ExprError(); |
| 5055 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5056 | QualType Ty = Context.getObjCIdType(); |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 5057 | ImplicitConversionSequence ICS = |
| 5058 | TryContextuallyConvertToObjCPointer(*this, From); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5059 | if (!ICS.isBad()) |
| 5060 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5061 | return ExprError(); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5062 | } |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5063 | |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5064 | /// Determine whether the provided type is an integral type, or an enumeration |
| 5065 | /// type of a permitted flavor. |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5066 | bool Sema::ICEConvertDiagnoser::match(QualType T) { |
| 5067 | return AllowScopedEnumerations ? T->isIntegralOrEnumerationType() |
| 5068 | : T->isIntegralOrUnscopedEnumerationType(); |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5069 | } |
| 5070 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5071 | static ExprResult |
| 5072 | diagnoseAmbiguousConversion(Sema &SemaRef, SourceLocation Loc, Expr *From, |
| 5073 | Sema::ContextualImplicitConverter &Converter, |
| 5074 | QualType T, UnresolvedSetImpl &ViableConversions) { |
| 5075 | |
| 5076 | if (Converter.Suppress) |
| 5077 | return ExprError(); |
| 5078 | |
| 5079 | Converter.diagnoseAmbiguous(SemaRef, Loc, T) << From->getSourceRange(); |
| 5080 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5081 | CXXConversionDecl *Conv = |
| 5082 | cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 5083 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 5084 | Converter.noteAmbiguous(SemaRef, Conv, ConvTy); |
| 5085 | } |
| 5086 | return SemaRef.Owned(From); |
| 5087 | } |
| 5088 | |
| 5089 | static bool |
| 5090 | diagnoseNoViableConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
| 5091 | Sema::ContextualImplicitConverter &Converter, |
| 5092 | QualType T, bool HadMultipleCandidates, |
| 5093 | UnresolvedSetImpl &ExplicitConversions) { |
| 5094 | if (ExplicitConversions.size() == 1 && !Converter.Suppress) { |
| 5095 | DeclAccessPair Found = ExplicitConversions[0]; |
| 5096 | CXXConversionDecl *Conversion = |
| 5097 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 5098 | |
| 5099 | // The user probably meant to invoke the given explicit |
| 5100 | // conversion; use it. |
| 5101 | QualType ConvTy = Conversion->getConversionType().getNonReferenceType(); |
| 5102 | std::string TypeStr; |
| 5103 | ConvTy.getAsStringInternal(TypeStr, SemaRef.getPrintingPolicy()); |
| 5104 | |
| 5105 | Converter.diagnoseExplicitConv(SemaRef, Loc, T, ConvTy) |
| 5106 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 5107 | "static_cast<" + TypeStr + ">(") |
| 5108 | << FixItHint::CreateInsertion( |
| 5109 | SemaRef.PP.getLocForEndOfToken(From->getLocEnd()), ")"); |
| 5110 | Converter.noteExplicitConv(SemaRef, Conversion, ConvTy); |
| 5111 | |
| 5112 | // If we aren't in a SFINAE context, build a call to the |
| 5113 | // explicit conversion function. |
| 5114 | if (SemaRef.isSFINAEContext()) |
| 5115 | return true; |
| 5116 | |
| 5117 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
| 5118 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5119 | HadMultipleCandidates); |
| 5120 | if (Result.isInvalid()) |
| 5121 | return true; |
| 5122 | // Record usage of conversion in an implicit cast. |
| 5123 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
| 5124 | CK_UserDefinedConversion, Result.get(), 0, |
| 5125 | Result.get()->getValueKind()); |
| 5126 | } |
| 5127 | return false; |
| 5128 | } |
| 5129 | |
| 5130 | static bool recordConversion(Sema &SemaRef, SourceLocation Loc, Expr *&From, |
| 5131 | Sema::ContextualImplicitConverter &Converter, |
| 5132 | QualType T, bool HadMultipleCandidates, |
| 5133 | DeclAccessPair &Found) { |
| 5134 | CXXConversionDecl *Conversion = |
| 5135 | cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 5136 | SemaRef.CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
| 5137 | |
| 5138 | QualType ToType = Conversion->getConversionType().getNonReferenceType(); |
| 5139 | if (!Converter.SuppressConversion) { |
| 5140 | if (SemaRef.isSFINAEContext()) |
| 5141 | return true; |
| 5142 | |
| 5143 | Converter.diagnoseConversion(SemaRef, Loc, T, ToType) |
| 5144 | << From->getSourceRange(); |
| 5145 | } |
| 5146 | |
| 5147 | ExprResult Result = SemaRef.BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5148 | HadMultipleCandidates); |
| 5149 | if (Result.isInvalid()) |
| 5150 | return true; |
| 5151 | // Record usage of conversion in an implicit cast. |
| 5152 | From = ImplicitCastExpr::Create(SemaRef.Context, Result.get()->getType(), |
| 5153 | CK_UserDefinedConversion, Result.get(), 0, |
| 5154 | Result.get()->getValueKind()); |
| 5155 | return false; |
| 5156 | } |
| 5157 | |
| 5158 | static ExprResult finishContextualImplicitConversion( |
| 5159 | Sema &SemaRef, SourceLocation Loc, Expr *From, |
| 5160 | Sema::ContextualImplicitConverter &Converter) { |
| 5161 | if (!Converter.match(From->getType()) && !Converter.Suppress) |
| 5162 | Converter.diagnoseNoMatch(SemaRef, Loc, From->getType()) |
| 5163 | << From->getSourceRange(); |
| 5164 | |
| 5165 | return SemaRef.DefaultLvalueConversion(From); |
| 5166 | } |
| 5167 | |
| 5168 | static void |
| 5169 | collectViableConversionCandidates(Sema &SemaRef, Expr *From, QualType ToType, |
| 5170 | UnresolvedSetImpl &ViableConversions, |
| 5171 | OverloadCandidateSet &CandidateSet) { |
| 5172 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5173 | DeclAccessPair FoundDecl = ViableConversions[I]; |
| 5174 | NamedDecl *D = FoundDecl.getDecl(); |
| 5175 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 5176 | if (isa<UsingShadowDecl>(D)) |
| 5177 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 5178 | |
| 5179 | CXXConversionDecl *Conv; |
| 5180 | FunctionTemplateDecl *ConvTemplate; |
| 5181 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 5182 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 5183 | else |
| 5184 | Conv = cast<CXXConversionDecl>(D); |
| 5185 | |
| 5186 | if (ConvTemplate) |
| 5187 | SemaRef.AddTemplateConversionCandidate( |
| 5188 | ConvTemplate, FoundDecl, ActingContext, From, ToType, CandidateSet); |
| 5189 | else |
| 5190 | SemaRef.AddConversionCandidate(Conv, FoundDecl, ActingContext, From, |
| 5191 | ToType, CandidateSet); |
| 5192 | } |
| 5193 | } |
| 5194 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5195 | /// \brief Attempt to convert the given expression to a type which is accepted |
| 5196 | /// by the given converter. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5197 | /// |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5198 | /// This routine will attempt to convert an expression of class type to a |
| 5199 | /// type accepted by the specified converter. In C++11 and before, the class |
| 5200 | /// must have a single non-explicit conversion function converting to a matching |
| 5201 | /// type. In C++1y, there can be multiple such conversion functions, but only |
| 5202 | /// one target type. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5203 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5204 | /// \param Loc The source location of the construct that requires the |
| 5205 | /// conversion. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5206 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 5207 | /// \param From The expression we're converting from. |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5208 | /// |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5209 | /// \param Converter Used to control and diagnose the conversion process. |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5210 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5211 | /// \returns The expression, converted to an integral or enumeration type if |
| 5212 | /// successful. |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5213 | ExprResult Sema::PerformContextualImplicitConversion( |
| 5214 | SourceLocation Loc, Expr *From, ContextualImplicitConverter &Converter) { |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5215 | // We can't perform any more checking for type-dependent expressions. |
| 5216 | if (From->isTypeDependent()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5217 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5218 | |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5219 | // Process placeholders immediately. |
| 5220 | if (From->hasPlaceholderType()) { |
| 5221 | ExprResult result = CheckPlaceholderExpr(From); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5222 | if (result.isInvalid()) |
| 5223 | return result; |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5224 | From = result.take(); |
| 5225 | } |
| 5226 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5227 | // If the expression already has a matching type, we're golden. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5228 | QualType T = From->getType(); |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5229 | if (Converter.match(T)) |
Eli Friedman | ceccab9 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5230 | return DefaultLvalueConversion(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5231 | |
| 5232 | // FIXME: Check for missing '()' if T is a function type? |
| 5233 | |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5234 | // We can only perform contextual implicit conversions on objects of class |
| 5235 | // type. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5236 | const RecordType *RecordTy = T->getAs<RecordType>(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5237 | if (!RecordTy || !getLangOpts().CPlusPlus) { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5238 | if (!Converter.Suppress) |
| 5239 | Converter.diagnoseNoMatch(*this, Loc, T) << From->getSourceRange(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5240 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5241 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5242 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5243 | // We must have a complete class type. |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 5244 | struct TypeDiagnoserPartialDiag : TypeDiagnoser { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5245 | ContextualImplicitConverter &Converter; |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5246 | Expr *From; |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5247 | |
| 5248 | TypeDiagnoserPartialDiag(ContextualImplicitConverter &Converter, Expr *From) |
| 5249 | : TypeDiagnoser(Converter.Suppress), Converter(Converter), From(From) {} |
| 5250 | |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5251 | virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) { |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5252 | Converter.diagnoseIncomplete(S, Loc, T) << From->getSourceRange(); |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5253 | } |
Richard Smith | 097e0a2 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 5254 | } IncompleteDiagnoser(Converter, From); |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5255 | |
| 5256 | if (RequireCompleteType(Loc, T, IncompleteDiagnoser)) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5257 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5258 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5259 | // Look for a conversion to an integral or enumeration type. |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5260 | UnresolvedSet<4> |
| 5261 | ViableConversions; // These are *potentially* viable in C++1y. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5262 | UnresolvedSet<4> ExplicitConversions; |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 5263 | std::pair<CXXRecordDecl::conversion_iterator, |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5264 | CXXRecordDecl::conversion_iterator> Conversions = |
| 5265 | cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5266 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5267 | bool HadMultipleCandidates = |
| 5268 | (std::distance(Conversions.first, Conversions.second) > 1); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5269 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5270 | // To check that there is only one target type, in C++1y: |
| 5271 | QualType ToType; |
| 5272 | bool HasUniqueTargetType = true; |
| 5273 | |
| 5274 | // Collect explicit or viable (potentially in C++1y) conversions. |
| 5275 | for (CXXRecordDecl::conversion_iterator I = Conversions.first, |
| 5276 | E = Conversions.second; |
| 5277 | I != E; ++I) { |
| 5278 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5279 | CXXConversionDecl *Conversion; |
| 5280 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 5281 | if (ConvTemplate) { |
| 5282 | if (getLangOpts().CPlusPlus1y) |
| 5283 | Conversion = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 5284 | else |
| 5285 | continue; // C++11 does not consider conversion operator templates(?). |
| 5286 | } else |
| 5287 | Conversion = cast<CXXConversionDecl>(D); |
| 5288 | |
| 5289 | assert((!ConvTemplate || getLangOpts().CPlusPlus1y) && |
| 5290 | "Conversion operator templates are considered potentially " |
| 5291 | "viable in C++1y"); |
| 5292 | |
| 5293 | QualType CurToType = Conversion->getConversionType().getNonReferenceType(); |
| 5294 | if (Converter.match(CurToType) || ConvTemplate) { |
| 5295 | |
| 5296 | if (Conversion->isExplicit()) { |
| 5297 | // FIXME: For C++1y, do we need this restriction? |
| 5298 | // cf. diagnoseNoViableConversion() |
| 5299 | if (!ConvTemplate) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5300 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5301 | } else { |
| 5302 | if (!ConvTemplate && getLangOpts().CPlusPlus1y) { |
| 5303 | if (ToType.isNull()) |
| 5304 | ToType = CurToType.getUnqualifiedType(); |
| 5305 | else if (HasUniqueTargetType && |
| 5306 | (CurToType.getUnqualifiedType() != ToType)) |
| 5307 | HasUniqueTargetType = false; |
| 5308 | } |
| 5309 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5310 | } |
Richard Smith | f39aec1 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5311 | } |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5312 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5313 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5314 | if (getLangOpts().CPlusPlus1y) { |
| 5315 | // C++1y [conv]p6: |
| 5316 | // ... An expression e of class type E appearing in such a context |
| 5317 | // is said to be contextually implicitly converted to a specified |
| 5318 | // type T and is well-formed if and only if e can be implicitly |
| 5319 | // 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] | 5320 | // for conversion functions whose return type is cv T or reference to |
| 5321 | // 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] | 5322 | // exactly one such T. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5323 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5324 | // If no unique T is found: |
| 5325 | if (ToType.isNull()) { |
| 5326 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5327 | HadMultipleCandidates, |
| 5328 | ExplicitConversions)) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5329 | return ExprError(); |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5330 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5331 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5332 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5333 | // If more than one unique Ts are found: |
| 5334 | if (!HasUniqueTargetType) |
| 5335 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5336 | ViableConversions); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5337 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5338 | // If one unique T is found: |
| 5339 | // First, build a candidate set from the previously recorded |
| 5340 | // potentially viable conversions. |
| 5341 | OverloadCandidateSet CandidateSet(Loc); |
| 5342 | collectViableConversionCandidates(*this, From, ToType, ViableConversions, |
| 5343 | CandidateSet); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5344 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5345 | // Then, perform overload resolution over the candidate set. |
| 5346 | OverloadCandidateSet::iterator Best; |
| 5347 | switch (CandidateSet.BestViableFunction(*this, Loc, Best)) { |
| 5348 | case OR_Success: { |
| 5349 | // Apply this conversion. |
| 5350 | DeclAccessPair Found = |
| 5351 | DeclAccessPair::make(Best->Function, Best->FoundDecl.getAccess()); |
| 5352 | if (recordConversion(*this, Loc, From, Converter, T, |
| 5353 | HadMultipleCandidates, Found)) |
| 5354 | return ExprError(); |
| 5355 | break; |
| 5356 | } |
| 5357 | case OR_Ambiguous: |
| 5358 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5359 | ViableConversions); |
| 5360 | case OR_No_Viable_Function: |
| 5361 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5362 | HadMultipleCandidates, |
| 5363 | ExplicitConversions)) |
| 5364 | return ExprError(); |
| 5365 | // fall through 'OR_Deleted' case. |
| 5366 | case OR_Deleted: |
| 5367 | // We'll complain below about a non-integral condition type. |
| 5368 | break; |
| 5369 | } |
| 5370 | } else { |
| 5371 | switch (ViableConversions.size()) { |
| 5372 | case 0: { |
| 5373 | if (diagnoseNoViableConversion(*this, Loc, From, Converter, T, |
| 5374 | HadMultipleCandidates, |
| 5375 | ExplicitConversions)) |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5376 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5377 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5378 | // We'll complain below about a non-integral condition type. |
| 5379 | break; |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5380 | } |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5381 | case 1: { |
| 5382 | // Apply this conversion. |
| 5383 | DeclAccessPair Found = ViableConversions[0]; |
| 5384 | if (recordConversion(*this, Loc, From, Converter, T, |
| 5385 | HadMultipleCandidates, Found)) |
| 5386 | return ExprError(); |
| 5387 | break; |
| 5388 | } |
| 5389 | default: |
| 5390 | return diagnoseAmbiguousConversion(*this, Loc, From, Converter, T, |
| 5391 | ViableConversions); |
| 5392 | } |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5393 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5394 | |
Larisse Voufo | 50b60b3 | 2013-06-10 06:50:24 +0000 | [diff] [blame] | 5395 | return finishContextualImplicitConversion(*this, Loc, From, Converter); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5396 | } |
| 5397 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5398 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 5399 | /// candidate functions, using the given function call arguments. If |
| 5400 | /// @p SuppressUserConversions, then don't allow user-defined |
| 5401 | /// conversions via constructors or conversion operators. |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5402 | /// |
James Dennett | 699c904 | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 5403 | /// \param PartialOverloading true if we are performing "partial" overloading |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5404 | /// based on an incomplete set of function arguments. This feature is used by |
| 5405 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5406 | void |
| 5407 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5408 | DeclAccessPair FoundDecl, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5409 | ArrayRef<Expr *> Args, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 5410 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 5411 | bool SuppressUserConversions, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5412 | bool PartialOverloading, |
| 5413 | bool AllowExplicit) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5414 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5415 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5416 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5417 | assert(!Function->getDescribedFunctionTemplate() && |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5418 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5419 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5420 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5421 | if (!isa<CXXConstructorDecl>(Method)) { |
| 5422 | // If we get here, it's because we're calling a member function |
| 5423 | // that is named without a member access expression (e.g., |
| 5424 | // "this->f") that was either written explicitly or created |
| 5425 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5426 | // function, e.g., X::f(). We use an empty type for the implied |
| 5427 | // object argument (C++ [over.call.func]p3), and the acting context |
| 5428 | // is irrelevant. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5429 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5430 | QualType(), Expr::Classification::makeSimpleLValue(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5431 | Args, CandidateSet, SuppressUserConversions); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5432 | return; |
| 5433 | } |
| 5434 | // We treat a constructor like a non-member function, since its object |
| 5435 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5436 | } |
| 5437 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 5438 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5439 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5440 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5441 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5442 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5443 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5444 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 5445 | // C++ [class.copy]p3: |
| 5446 | // A member function template is never instantiated to perform the copy |
| 5447 | // of a class object to an object of its class type. |
| 5448 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5449 | if (Args.size() == 1 && |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 5450 | Constructor->isSpecializationCopyingObject() && |
Douglas Gregor | 1211606 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 5451 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 5452 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5453 | return; |
| 5454 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5455 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5456 | // Add this candidate |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5457 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5458 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5459 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5460 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5461 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5462 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5463 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5464 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5465 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5466 | |
| 5467 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5468 | // parameters is viable only if it has an ellipsis in its parameter |
| 5469 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5470 | if ((Args.size() + (PartialOverloading && Args.size())) > NumArgsInProto && |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 5471 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5472 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5473 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5474 | return; |
| 5475 | } |
| 5476 | |
| 5477 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5478 | // is viable only if the (m+1)st parameter has a default argument |
| 5479 | // (8.3.6). For the purposes of overload resolution, the |
| 5480 | // parameter list is truncated on the right, so that there are |
| 5481 | // exactly m parameters. |
| 5482 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5483 | if (Args.size() < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5484 | // Not enough arguments. |
| 5485 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5486 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5487 | return; |
| 5488 | } |
| 5489 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5490 | // (CUDA B.1): Check for invalid calls between targets. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5491 | if (getLangOpts().CUDA) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5492 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
| 5493 | if (CheckCUDATarget(Caller, Function)) { |
| 5494 | Candidate.Viable = false; |
| 5495 | Candidate.FailureKind = ovl_fail_bad_target; |
| 5496 | return; |
| 5497 | } |
| 5498 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5499 | // Determine the implicit conversion sequences for each of the |
| 5500 | // arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5501 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5502 | if (ArgIdx < NumArgsInProto) { |
| 5503 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5504 | // exist for each argument an implicit conversion sequence |
| 5505 | // (13.3.3.1) that converts that argument to the corresponding |
| 5506 | // parameter of F. |
| 5507 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5508 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5509 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5510 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5511 | /*InOverloadResolution=*/true, |
| 5512 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5513 | getLangOpts().ObjCAutoRefCount, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5514 | AllowExplicit); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5515 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 5516 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5517 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5518 | break; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5519 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5520 | } else { |
| 5521 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5522 | // argument for which there is no corresponding parameter is |
| 5523 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5524 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5525 | } |
| 5526 | } |
| 5527 | } |
| 5528 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5529 | /// \brief Add all of the function declarations in the given function set to |
| 5530 | /// the overload canddiate set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5531 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5532 | ArrayRef<Expr *> Args, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5533 | OverloadCandidateSet& CandidateSet, |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5534 | bool SuppressUserConversions, |
| 5535 | TemplateArgumentListInfo *ExplicitTemplateArgs) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5536 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5537 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 5538 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5539 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5540 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5541 | cast<CXXMethodDecl>(FD)->getParent(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5542 | Args[0]->getType(), Args[0]->Classify(Context), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5543 | Args.slice(1), CandidateSet, |
| 5544 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5545 | else |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5546 | AddOverloadCandidate(FD, F.getPair(), Args, CandidateSet, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5547 | SuppressUserConversions); |
| 5548 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5549 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5550 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 5551 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5552 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5553 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5554 | ExplicitTemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5555 | Args[0]->getType(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5556 | Args[0]->Classify(Context), Args.slice(1), |
| 5557 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5558 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5559 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5560 | ExplicitTemplateArgs, Args, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5561 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5562 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5563 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5564 | } |
| 5565 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5566 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 5567 | /// method) as a method candidate to the given overload set. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5568 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5569 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5570 | Expr::Classification ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5571 | ArrayRef<Expr *> Args, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5572 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5573 | bool SuppressUserConversions) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5574 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5575 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5576 | |
| 5577 | if (isa<UsingShadowDecl>(Decl)) |
| 5578 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5579 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5580 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 5581 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 5582 | "Expected a member function template"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5583 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 5584 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5585 | ObjectType, ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5586 | Args, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5587 | SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5588 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5589 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5590 | ObjectType, ObjectClassification, |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 5591 | Args, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5592 | CandidateSet, SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5593 | } |
| 5594 | } |
| 5595 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5596 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 5597 | /// of candidate functions, using the given function call arguments |
| 5598 | /// and the object argument (@c Object). For example, in a call |
| 5599 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 5600 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 5601 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5602 | /// operators. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5603 | void |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5604 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5605 | CXXRecordDecl *ActingContext, QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5606 | Expr::Classification ObjectClassification, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5607 | ArrayRef<Expr *> Args, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5608 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5609 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5610 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5611 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5612 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5613 | assert(!isa<CXXConstructorDecl>(Method) && |
| 5614 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5615 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5616 | if (!CandidateSet.isNewCandidate(Method)) |
| 5617 | return; |
| 5618 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5619 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5620 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5621 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5622 | // Add this candidate |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5623 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5624 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5625 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5626 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5627 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5628 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5629 | |
| 5630 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5631 | |
| 5632 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5633 | // parameters is viable only if it has an ellipsis in its parameter |
| 5634 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5635 | if (Args.size() > NumArgsInProto && !Proto->isVariadic()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5636 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5637 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5638 | return; |
| 5639 | } |
| 5640 | |
| 5641 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5642 | // is viable only if the (m+1)st parameter has a default argument |
| 5643 | // (8.3.6). For the purposes of overload resolution, the |
| 5644 | // parameter list is truncated on the right, so that there are |
| 5645 | // exactly m parameters. |
| 5646 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5647 | if (Args.size() < MinRequiredArgs) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5648 | // Not enough arguments. |
| 5649 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5650 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5651 | return; |
| 5652 | } |
| 5653 | |
| 5654 | Candidate.Viable = true; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5655 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5656 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5657 | // The implicit object argument is ignored. |
| 5658 | Candidate.IgnoreObjectArgument = true; |
| 5659 | else { |
| 5660 | // Determine the implicit conversion sequence for the object |
| 5661 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5662 | Candidate.Conversions[0] |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5663 | = TryObjectArgumentInitialization(*this, ObjectType, ObjectClassification, |
| 5664 | Method, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5665 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5666 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5667 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5668 | return; |
| 5669 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5670 | } |
| 5671 | |
| 5672 | // Determine the implicit conversion sequences for each of the |
| 5673 | // arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5674 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5675 | if (ArgIdx < NumArgsInProto) { |
| 5676 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5677 | // exist for each argument an implicit conversion sequence |
| 5678 | // (13.3.3.1) that converts that argument to the corresponding |
| 5679 | // parameter of F. |
| 5680 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5681 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5682 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5683 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5684 | /*InOverloadResolution=*/true, |
| 5685 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5686 | getLangOpts().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5687 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5688 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5689 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5690 | break; |
| 5691 | } |
| 5692 | } else { |
| 5693 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5694 | // argument for which there is no corresponding parameter is |
| 5695 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5696 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5697 | } |
| 5698 | } |
| 5699 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5700 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5701 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 5702 | /// set, using template argument deduction to produce an appropriate member |
| 5703 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5704 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5705 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5706 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5707 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5708 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5709 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5710 | Expr::Classification ObjectClassification, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5711 | ArrayRef<Expr *> Args, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5712 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5713 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5714 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 5715 | return; |
| 5716 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5717 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5718 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5719 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5720 | // 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] | 5721 | // candidate functions in the usual way.113) A given name can refer to one |
| 5722 | // or more function templates and also to a set of overloaded non-template |
| 5723 | // functions. In such a case, the candidate functions generated from each |
| 5724 | // function template are combined with the set of non-template candidate |
| 5725 | // functions. |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5726 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5727 | FunctionDecl *Specialization = 0; |
| 5728 | if (TemplateDeductionResult Result |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5729 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, Args, |
| 5730 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5731 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5732 | Candidate.FoundDecl = FoundDecl; |
| 5733 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 5734 | Candidate.Viable = false; |
| 5735 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 5736 | Candidate.IsSurrogate = false; |
| 5737 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5738 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5739 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5740 | Info); |
| 5741 | return; |
| 5742 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5743 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5744 | // Add the function template specialization produced by template argument |
| 5745 | // deduction as a candidate. |
| 5746 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5747 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5748 | "Specialization is not a member function?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5749 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5750 | ActingContext, ObjectType, ObjectClassification, Args, |
| 5751 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5752 | } |
| 5753 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5754 | /// \brief Add a C++ function template specialization as a candidate |
| 5755 | /// in the candidate set, using template argument deduction to produce |
| 5756 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5757 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5758 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5759 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5760 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5761 | ArrayRef<Expr *> Args, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5762 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5763 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5764 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 5765 | return; |
| 5766 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5767 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5768 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5769 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5770 | // 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] | 5771 | // candidate functions in the usual way.113) A given name can refer to one |
| 5772 | // or more function templates and also to a set of overloaded non-template |
| 5773 | // functions. In such a case, the candidate functions generated from each |
| 5774 | // function template are combined with the set of non-template candidate |
| 5775 | // functions. |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5776 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5777 | FunctionDecl *Specialization = 0; |
| 5778 | if (TemplateDeductionResult Result |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5779 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, Args, |
| 5780 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5781 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5782 | Candidate.FoundDecl = FoundDecl; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5783 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 5784 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5785 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5786 | Candidate.IsSurrogate = false; |
| 5787 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5788 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5789 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5790 | Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5791 | return; |
| 5792 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5793 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5794 | // Add the function template specialization produced by template argument |
| 5795 | // deduction as a candidate. |
| 5796 | assert(Specialization && "Missing function template specialization?"); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5797 | AddOverloadCandidate(Specialization, FoundDecl, Args, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5798 | SuppressUserConversions); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5799 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5800 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5801 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5802 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5803 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5804 | /// 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] | 5805 | /// (which may or may not be the same type as the type that the |
| 5806 | /// conversion function produces). |
| 5807 | void |
| 5808 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5809 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5810 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5811 | Expr *From, QualType ToType, |
| 5812 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5813 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 5814 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5815 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5816 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 5817 | return; |
| 5818 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 5819 | // If the conversion function has an undeduced return type, trigger its |
| 5820 | // deduction now. |
| 5821 | if (getLangOpts().CPlusPlus1y && ConvType->isUndeducedType()) { |
| 5822 | if (DeduceReturnType(Conversion, From->getExprLoc())) |
| 5823 | return; |
| 5824 | ConvType = Conversion->getConversionType().getNonReferenceType(); |
| 5825 | } |
| 5826 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5827 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5828 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5829 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5830 | // Add this candidate |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5831 | OverloadCandidate &Candidate = CandidateSet.addCandidate(1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5832 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5833 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5834 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5835 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5836 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5837 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 5838 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5839 | Candidate.Viable = true; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5840 | Candidate.ExplicitCallArguments = 1; |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5841 | |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 5842 | // C++ [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5843 | // For conversion functions, the function is considered to be a member of |
| 5844 | // the class of the implicit implied object argument for the purpose of |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 5845 | // defining the type of the implicit object parameter. |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5846 | // |
| 5847 | // Determine the implicit conversion sequence for the implicit |
| 5848 | // object parameter. |
| 5849 | QualType ImplicitParamType = From->getType(); |
| 5850 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
| 5851 | ImplicitParamType = FromPtrType->getPointeeType(); |
| 5852 | CXXRecordDecl *ConversionContext |
| 5853 | = cast<CXXRecordDecl>(ImplicitParamType->getAs<RecordType>()->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5854 | |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5855 | Candidate.Conversions[0] |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5856 | = TryObjectArgumentInitialization(*this, From->getType(), |
| 5857 | From->Classify(Context), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5858 | Conversion, ConversionContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5859 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5860 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5861 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5862 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5863 | return; |
| 5864 | } |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5865 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5866 | // 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] | 5867 | // derived to base as such conversions are given Conversion Rank. They only |
| 5868 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 5869 | QualType FromCanon |
| 5870 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 5871 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 5872 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 5873 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5874 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 5875 | return; |
| 5876 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5877 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5878 | // To determine what the conversion from the result of calling the |
| 5879 | // conversion function to the type we're eventually trying to |
| 5880 | // convert to (ToType), we need to synthesize a call to the |
| 5881 | // conversion function and attempt copy initialization from it. This |
| 5882 | // makes sure that we get the right semantics with respect to |
| 5883 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 5884 | // call on the stack and we don't need its arguments to be |
| 5885 | // well-formed. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5886 | DeclRefExpr ConversionRef(Conversion, false, Conversion->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5887 | VK_LValue, From->getLocStart()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5888 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 5889 | Context.getPointerType(Conversion->getType()), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5890 | CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5891 | &ConversionRef, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5892 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5893 | QualType ConversionType = Conversion->getConversionType(); |
| 5894 | if (RequireCompleteType(From->getLocStart(), ConversionType, 0)) { |
Douglas Gregor | 7d14d38 | 2010-11-13 19:36:57 +0000 | [diff] [blame] | 5895 | Candidate.Viable = false; |
| 5896 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 5897 | return; |
| 5898 | } |
| 5899 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5900 | ExprValueKind VK = Expr::getValueKindForType(ConversionType); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5901 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5902 | // 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] | 5903 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 5904 | // allocator). |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5905 | QualType CallResultType = ConversionType.getNonLValueExprType(Context); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 5906 | CallExpr Call(Context, &ConversionFn, None, CallResultType, VK, |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 5907 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5908 | ImplicitConversionSequence ICS = |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5909 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 5910 | /*SuppressUserConversions=*/true, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5911 | /*InOverloadResolution=*/false, |
| 5912 | /*AllowObjCWritebackConversion=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5913 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5914 | switch (ICS.getKind()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5915 | case ImplicitConversionSequence::StandardConversion: |
| 5916 | Candidate.FinalConversion = ICS.Standard; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5917 | |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5918 | // C++ [over.ics.user]p3: |
| 5919 | // If the user-defined conversion is specified by a specialization of a |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5920 | // conversion function template, the second standard conversion sequence |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5921 | // shall have exact match rank. |
| 5922 | if (Conversion->getPrimaryTemplate() && |
| 5923 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 5924 | Candidate.Viable = false; |
| 5925 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 5926 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5927 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 5928 | // C++0x [dcl.init.ref]p5: |
| 5929 | // In the second case, if the reference is an rvalue reference and |
| 5930 | // the second standard conversion sequence of the user-defined |
| 5931 | // conversion sequence includes an lvalue-to-rvalue conversion, the |
| 5932 | // program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5933 | if (ToType->isRValueReferenceType() && |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 5934 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 5935 | Candidate.Viable = false; |
| 5936 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 5937 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5938 | break; |
| 5939 | |
| 5940 | case ImplicitConversionSequence::BadConversion: |
| 5941 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5942 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5943 | break; |
| 5944 | |
| 5945 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5946 | llvm_unreachable( |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5947 | "Can only end up with a standard conversion sequence or failure"); |
| 5948 | } |
| 5949 | } |
| 5950 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5951 | /// \brief Adds a conversion function template specialization |
| 5952 | /// candidate to the overload set, using template argument deduction |
| 5953 | /// to deduce the template arguments of the conversion function |
| 5954 | /// template from the type that we are converting to (C++ |
| 5955 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5956 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5957 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5958 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5959 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5960 | Expr *From, QualType ToType, |
| 5961 | OverloadCandidateSet &CandidateSet) { |
| 5962 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 5963 | "Only conversion function templates permitted here"); |
| 5964 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5965 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 5966 | return; |
| 5967 | |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5968 | TemplateDeductionInfo Info(CandidateSet.getLocation()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5969 | CXXConversionDecl *Specialization = 0; |
| 5970 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5971 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5972 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5973 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5974 | Candidate.FoundDecl = FoundDecl; |
| 5975 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 5976 | Candidate.Viable = false; |
| 5977 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 5978 | Candidate.IsSurrogate = false; |
| 5979 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5980 | Candidate.ExplicitCallArguments = 1; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5981 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5982 | Info); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5983 | return; |
| 5984 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5985 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5986 | // Add the conversion function template specialization produced by |
| 5987 | // template argument deduction as a candidate. |
| 5988 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5989 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5990 | CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5991 | } |
| 5992 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5993 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 5994 | /// converts the given @c Object to a function pointer via the |
| 5995 | /// conversion function @c Conversion, and then attempts to call it |
| 5996 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 5997 | /// the type of function that we'll eventually be calling. |
| 5998 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5999 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6000 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6001 | const FunctionProtoType *Proto, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6002 | Expr *Object, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6003 | ArrayRef<Expr *> Args, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6004 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 6005 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 6006 | return; |
| 6007 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6008 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6009 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6010 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6011 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6012 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6013 | Candidate.Function = 0; |
| 6014 | Candidate.Surrogate = Conversion; |
| 6015 | Candidate.Viable = true; |
| 6016 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6017 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6018 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6019 | |
| 6020 | // Determine the implicit conversion sequence for the implicit |
| 6021 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6022 | ImplicitConversionSequence ObjectInit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6023 | = TryObjectArgumentInitialization(*this, Object->getType(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6024 | Object->Classify(Context), |
| 6025 | Conversion, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6026 | if (ObjectInit.isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6027 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6028 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6029 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6030 | return; |
| 6031 | } |
| 6032 | |
| 6033 | // The first conversion is actually a user-defined conversion whose |
| 6034 | // first conversion is ObjectInit's standard conversion (which is |
| 6035 | // effectively a reference binding). Record it as such. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6036 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6037 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 6038 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6039 | Candidate.Conversions[0].UserDefined.HadMultipleCandidates = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6040 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 6041 | Candidate.Conversions[0].UserDefined.FoundConversionFunction = FoundDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6042 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6043 | = Candidate.Conversions[0].UserDefined.Before; |
| 6044 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 6045 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6046 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6047 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 6048 | |
| 6049 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 6050 | // parameters is viable only if it has an ellipsis in its parameter |
| 6051 | // list (8.3.5). |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6052 | if (Args.size() > NumArgsInProto && !Proto->isVariadic()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6053 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6054 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6055 | return; |
| 6056 | } |
| 6057 | |
| 6058 | // Function types don't have any default arguments, so just check if |
| 6059 | // we have enough arguments. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 6060 | if (Args.size() < NumArgsInProto) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6061 | // Not enough arguments. |
| 6062 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6063 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6064 | return; |
| 6065 | } |
| 6066 | |
| 6067 | // Determine the implicit conversion sequences for each of the |
| 6068 | // arguments. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6069 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6070 | if (ArgIdx < NumArgsInProto) { |
| 6071 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 6072 | // exist for each argument an implicit conversion sequence |
| 6073 | // (13.3.3.1) that converts that argument to the corresponding |
| 6074 | // parameter of F. |
| 6075 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6076 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6077 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6078 | /*SuppressUserConversions=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6079 | /*InOverloadResolution=*/false, |
| 6080 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6081 | getLangOpts().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6082 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6083 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6084 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6085 | break; |
| 6086 | } |
| 6087 | } else { |
| 6088 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 6089 | // argument for which there is no corresponding parameter is |
| 6090 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6091 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6092 | } |
| 6093 | } |
| 6094 | } |
| 6095 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6096 | /// \brief Add overload candidates for overloaded operators that are |
| 6097 | /// member functions. |
| 6098 | /// |
| 6099 | /// Add the overloaded operator candidates that are member functions |
| 6100 | /// for the operator Op that was used in an operator expression such |
| 6101 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 6102 | /// CandidateSet will store the added overload candidates. (C++ |
| 6103 | /// [over.match.oper]). |
| 6104 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 6105 | SourceLocation OpLoc, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6106 | ArrayRef<Expr *> Args, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6107 | OverloadCandidateSet& CandidateSet, |
| 6108 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6109 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 6110 | |
| 6111 | // C++ [over.match.oper]p3: |
| 6112 | // For a unary operator @ with an operand of a type whose |
| 6113 | // cv-unqualified version is T1, and for a binary operator @ with |
| 6114 | // a left operand of a type whose cv-unqualified version is T1 and |
| 6115 | // a right operand of a type whose cv-unqualified version is T2, |
| 6116 | // three sets of candidate functions, designated member |
| 6117 | // candidates, non-member candidates and built-in candidates, are |
| 6118 | // constructed as follows: |
| 6119 | QualType T1 = Args[0]->getType(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6120 | |
Richard Smith | e410be9 | 2013-04-20 12:41:22 +0000 | [diff] [blame] | 6121 | // -- If T1 is a complete class type or a class currently being |
| 6122 | // defined, the set of member candidates is the result of the |
| 6123 | // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, |
| 6124 | // the set of member candidates is empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6125 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Richard Smith | e410be9 | 2013-04-20 12:41:22 +0000 | [diff] [blame] | 6126 | // Complete the type if it can be completed. |
| 6127 | RequireCompleteType(OpLoc, T1, 0); |
| 6128 | // If the type is neither complete nor being defined, bail out now. |
| 6129 | if (!T1Rec->getDecl()->getDefinition()) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 6130 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6131 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6132 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 6133 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 6134 | Operators.suppressDiagnostics(); |
| 6135 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6136 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 6137 | OperEnd = Operators.end(); |
| 6138 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6139 | ++Oper) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6140 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
Rafael Espindola | 548107e | 2013-04-29 19:29:25 +0000 | [diff] [blame] | 6141 | Args[0]->Classify(Context), |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6142 | Args.slice(1), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6143 | CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6144 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6145 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6146 | } |
| 6147 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6148 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 6149 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 6150 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6151 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 6152 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6153 | /// operator. NumContextualBoolArguments is the number of arguments |
| 6154 | /// (at the beginning of the argument list) that will be contextually |
| 6155 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6156 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6157 | ArrayRef<Expr *> Args, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6158 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6159 | bool IsAssignmentOperator, |
| 6160 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6161 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6162 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6163 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6164 | // Add this candidate |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6165 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6166 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6167 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 6168 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6169 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6170 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6171 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6172 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 6173 | |
| 6174 | // Determine the implicit conversion sequences for each of the |
| 6175 | // arguments. |
| 6176 | Candidate.Viable = true; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6177 | Candidate.ExplicitCallArguments = Args.size(); |
| 6178 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6179 | // C++ [over.match.oper]p4: |
| 6180 | // For the built-in assignment operators, conversions of the |
| 6181 | // left operand are restricted as follows: |
| 6182 | // -- no temporaries are introduced to hold the left operand, and |
| 6183 | // -- no user-defined conversions are applied to the left |
| 6184 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6185 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 6186 | // |
| 6187 | // We block these conversions by turning off user-defined |
| 6188 | // conversions, since that is the only way that initialization of |
| 6189 | // a reference to a non-class type can occur from something that |
| 6190 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6191 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6192 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6193 | "Contextual conversion to bool requires bool type"); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6194 | Candidate.Conversions[ArgIdx] |
| 6195 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6196 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6197 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6198 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6199 | ArgIdx == 0 && IsAssignmentOperator, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6200 | /*InOverloadResolution=*/false, |
| 6201 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6202 | getLangOpts().ObjCAutoRefCount); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6203 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6204 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6205 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6206 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6207 | break; |
| 6208 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6209 | } |
| 6210 | } |
| 6211 | |
Craig Topper | fe09f3f | 2013-07-01 06:29:40 +0000 | [diff] [blame] | 6212 | namespace { |
| 6213 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6214 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 6215 | /// candidate operator functions for built-in operators (C++ |
| 6216 | /// [over.built]). The types are separated into pointer types and |
| 6217 | /// enumeration types. |
| 6218 | class BuiltinCandidateTypeSet { |
| 6219 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6220 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6221 | |
| 6222 | /// PointerTypes - The set of pointer types that will be used in the |
| 6223 | /// built-in candidates. |
| 6224 | TypeSet PointerTypes; |
| 6225 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6226 | /// MemberPointerTypes - The set of member pointer types that will be |
| 6227 | /// used in the built-in candidates. |
| 6228 | TypeSet MemberPointerTypes; |
| 6229 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6230 | /// EnumerationTypes - The set of enumeration types that will be |
| 6231 | /// used in the built-in candidates. |
| 6232 | TypeSet EnumerationTypes; |
| 6233 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6234 | /// \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] | 6235 | /// candidates. |
| 6236 | TypeSet VectorTypes; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6237 | |
| 6238 | /// \brief A flag indicating non-record types are viable candidates |
| 6239 | bool HasNonRecordTypes; |
| 6240 | |
| 6241 | /// \brief A flag indicating whether either arithmetic or enumeration types |
| 6242 | /// were present in the candidate set. |
| 6243 | bool HasArithmeticOrEnumeralTypes; |
| 6244 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6245 | /// \brief A flag indicating whether the nullptr type was present in the |
| 6246 | /// candidate set. |
| 6247 | bool HasNullPtrType; |
| 6248 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 6249 | /// Sema - The semantic analysis instance where we are building the |
| 6250 | /// candidate type set. |
| 6251 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6252 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6253 | /// Context - The AST context in which we will build the type sets. |
| 6254 | ASTContext &Context; |
| 6255 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6256 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6257 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6258 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6259 | |
| 6260 | public: |
| 6261 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6262 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6263 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6264 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6265 | : HasNonRecordTypes(false), |
| 6266 | HasArithmeticOrEnumeralTypes(false), |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6267 | HasNullPtrType(false), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6268 | SemaRef(SemaRef), |
| 6269 | Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6270 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6271 | void AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6272 | SourceLocation Loc, |
| 6273 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6274 | bool AllowExplicitConversions, |
| 6275 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6276 | |
| 6277 | /// pointer_begin - First pointer type found; |
| 6278 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 6279 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6280 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6281 | iterator pointer_end() { return PointerTypes.end(); } |
| 6282 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6283 | /// member_pointer_begin - First member pointer type found; |
| 6284 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 6285 | |
| 6286 | /// member_pointer_end - Past the last member pointer type found; |
| 6287 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 6288 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6289 | /// enumeration_begin - First enumeration type found; |
| 6290 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 6291 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6292 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6293 | iterator enumeration_end() { return EnumerationTypes.end(); } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6294 | |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6295 | iterator vector_begin() { return VectorTypes.begin(); } |
| 6296 | iterator vector_end() { return VectorTypes.end(); } |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6297 | |
| 6298 | bool hasNonRecordTypes() { return HasNonRecordTypes; } |
| 6299 | bool hasArithmeticOrEnumeralTypes() { return HasArithmeticOrEnumeralTypes; } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6300 | bool hasNullPtrType() const { return HasNullPtrType; } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6301 | }; |
| 6302 | |
Craig Topper | fe09f3f | 2013-07-01 06:29:40 +0000 | [diff] [blame] | 6303 | } // end anonymous namespace |
| 6304 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6305 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6306 | /// the set of pointer types along with any more-qualified variants of |
| 6307 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6308 | /// will add "int const *", "int const volatile *", "int const |
| 6309 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6310 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6311 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6312 | /// |
| 6313 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6314 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6315 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6316 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6317 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6318 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6319 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6320 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6321 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6322 | QualType PointeeTy; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6323 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6324 | bool buildObjCPtr = false; |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6325 | if (!PointerTy) { |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6326 | const ObjCObjectPointerType *PTy = Ty->castAs<ObjCObjectPointerType>(); |
| 6327 | PointeeTy = PTy->getPointeeType(); |
| 6328 | buildObjCPtr = true; |
| 6329 | } else { |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6330 | PointeeTy = PointerTy->getPointeeType(); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6331 | } |
| 6332 | |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6333 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6334 | // (the qualifier would sink to the element type), and for another, the |
| 6335 | // only overload situation where it matters is subscript or pointer +- int, |
| 6336 | // and those shouldn't have qualifier variants anyway. |
| 6337 | if (PointeeTy->isArrayType()) |
| 6338 | return true; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6339 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6340 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6341 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 6342 | bool hasRestrict = VisibleQuals.hasRestrict(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6343 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6344 | // Iterate through all strict supersets of BaseCVR. |
| 6345 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6346 | if ((CVR | BaseCVR) != CVR) continue; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6347 | // Skip over volatile if no volatile found anywhere in the types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6348 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6349 | |
| 6350 | // Skip over restrict if no restrict found anywhere in the types, or if |
| 6351 | // the type cannot be restrict-qualified. |
| 6352 | if ((CVR & Qualifiers::Restrict) && |
| 6353 | (!hasRestrict || |
| 6354 | (!(PointeeTy->isAnyPointerType() || PointeeTy->isReferenceType())))) |
| 6355 | continue; |
| 6356 | |
| 6357 | // Build qualified pointee type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6358 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6359 | |
| 6360 | // Build qualified pointer type. |
| 6361 | QualType QPointerTy; |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6362 | if (!buildObjCPtr) |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6363 | QPointerTy = Context.getPointerType(QPointeeTy); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6364 | else |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6365 | QPointerTy = Context.getObjCObjectPointerType(QPointeeTy); |
| 6366 | |
| 6367 | // Insert qualified pointer type. |
| 6368 | PointerTypes.insert(QPointerTy); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6369 | } |
| 6370 | |
| 6371 | return true; |
| 6372 | } |
| 6373 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6374 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 6375 | /// to the set of pointer types along with any more-qualified variants of |
| 6376 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6377 | /// will add "int const *", "int const volatile *", "int const |
| 6378 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6379 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6380 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6381 | /// |
| 6382 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6383 | bool |
| 6384 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 6385 | QualType Ty) { |
| 6386 | // Insert this type. |
| 6387 | if (!MemberPointerTypes.insert(Ty)) |
| 6388 | return false; |
| 6389 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6390 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 6391 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6392 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6393 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6394 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6395 | // (the qualifier would sink to the element type), and for another, the |
| 6396 | // only overload situation where it matters is subscript or pointer +- int, |
| 6397 | // and those shouldn't have qualifier variants anyway. |
| 6398 | if (PointeeTy->isArrayType()) |
| 6399 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6400 | const Type *ClassTy = PointerTy->getClass(); |
| 6401 | |
| 6402 | // Iterate through all strict supersets of the pointee type's CVR |
| 6403 | // qualifiers. |
| 6404 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 6405 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6406 | if ((CVR | BaseCVR) != CVR) continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6407 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6408 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6409 | MemberPointerTypes.insert( |
| 6410 | Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6411 | } |
| 6412 | |
| 6413 | return true; |
| 6414 | } |
| 6415 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6416 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 6417 | /// 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] | 6418 | /// primarily interested in pointer types and enumeration types. We also |
| 6419 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6420 | /// AllowUserConversions is true if we should look at the conversion |
| 6421 | /// functions of a class type, and AllowExplicitConversions if we |
| 6422 | /// should also include the explicit conversion functions of a class |
| 6423 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6424 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6425 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6426 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6427 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6428 | bool AllowExplicitConversions, |
| 6429 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6430 | // Only deal with canonical types. |
| 6431 | Ty = Context.getCanonicalType(Ty); |
| 6432 | |
| 6433 | // Look through reference types; they aren't part of the type of an |
| 6434 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6435 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6436 | Ty = RefTy->getPointeeType(); |
| 6437 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 6438 | // If we're dealing with an array type, decay to the pointer. |
| 6439 | if (Ty->isArrayType()) |
| 6440 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 6441 | |
| 6442 | // Otherwise, we don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 6443 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6444 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6445 | // Flag if we ever add a non-record type. |
| 6446 | const RecordType *TyRec = Ty->getAs<RecordType>(); |
| 6447 | HasNonRecordTypes = HasNonRecordTypes || !TyRec; |
| 6448 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6449 | // Flag if we encounter an arithmetic type. |
| 6450 | HasArithmeticOrEnumeralTypes = |
| 6451 | HasArithmeticOrEnumeralTypes || Ty->isArithmeticType(); |
| 6452 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6453 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
| 6454 | PointerTypes.insert(Ty); |
| 6455 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6456 | // Insert our type, and its more-qualified variants, into the set |
| 6457 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6458 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6459 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6460 | } else if (Ty->isMemberPointerType()) { |
| 6461 | // Member pointers are far easier, since the pointee can't be converted. |
| 6462 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 6463 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6464 | } else if (Ty->isEnumeralType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6465 | HasArithmeticOrEnumeralTypes = true; |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6466 | EnumerationTypes.insert(Ty); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6467 | } else if (Ty->isVectorType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6468 | // We treat vector types as arithmetic types in many contexts as an |
| 6469 | // extension. |
| 6470 | HasArithmeticOrEnumeralTypes = true; |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6471 | VectorTypes.insert(Ty); |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6472 | } else if (Ty->isNullPtrType()) { |
| 6473 | HasNullPtrType = true; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6474 | } else if (AllowUserConversions && TyRec) { |
| 6475 | // No conversion functions in incomplete types. |
| 6476 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) |
| 6477 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6478 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6479 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6480 | std::pair<CXXRecordDecl::conversion_iterator, |
| 6481 | CXXRecordDecl::conversion_iterator> |
| 6482 | Conversions = ClassDecl->getVisibleConversionFunctions(); |
| 6483 | for (CXXRecordDecl::conversion_iterator |
| 6484 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6485 | NamedDecl *D = I.getDecl(); |
| 6486 | if (isa<UsingShadowDecl>(D)) |
| 6487 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6488 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6489 | // Skip conversion function templates; they don't tell us anything |
| 6490 | // about which builtin types we can convert to. |
| 6491 | if (isa<FunctionTemplateDecl>(D)) |
| 6492 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6493 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6494 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
| 6495 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
| 6496 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
| 6497 | VisibleQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6498 | } |
| 6499 | } |
| 6500 | } |
| 6501 | } |
| 6502 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6503 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 6504 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 6505 | /// given type to the candidate set. |
| 6506 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 6507 | QualType T, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6508 | ArrayRef<Expr *> Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6509 | OverloadCandidateSet &CandidateSet) { |
| 6510 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6511 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6512 | // T& operator=(T&, T) |
| 6513 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 6514 | ParamTypes[1] = T; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6515 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6516 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6517 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6518 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 6519 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6520 | ParamTypes[0] |
| 6521 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6522 | ParamTypes[1] = T; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6523 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6524 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6525 | } |
| 6526 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6527 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 6528 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 6529 | /// 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] | 6530 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 6531 | Qualifiers VRQuals; |
| 6532 | const RecordType *TyRec; |
| 6533 | if (const MemberPointerType *RHSMPType = |
| 6534 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6535 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6536 | else |
| 6537 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 6538 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6539 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6540 | VRQuals.addVolatile(); |
| 6541 | VRQuals.addRestrict(); |
| 6542 | return VRQuals; |
| 6543 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6544 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6545 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 6546 | if (!ClassDecl->hasDefinition()) |
| 6547 | return VRQuals; |
| 6548 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6549 | std::pair<CXXRecordDecl::conversion_iterator, |
| 6550 | CXXRecordDecl::conversion_iterator> |
| 6551 | Conversions = ClassDecl->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6552 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 6553 | for (CXXRecordDecl::conversion_iterator |
| 6554 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 6555 | NamedDecl *D = I.getDecl(); |
| 6556 | if (isa<UsingShadowDecl>(D)) |
| 6557 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6558 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6559 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 6560 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 6561 | CanTy = ResTypeRef->getPointeeType(); |
| 6562 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 6563 | // as see them. |
| 6564 | bool done = false; |
| 6565 | while (!done) { |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6566 | if (CanTy.isRestrictQualified()) |
| 6567 | VRQuals.addRestrict(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6568 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 6569 | CanTy = ResTypePtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6570 | else if (const MemberPointerType *ResTypeMPtr = |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6571 | CanTy->getAs<MemberPointerType>()) |
| 6572 | CanTy = ResTypeMPtr->getPointeeType(); |
| 6573 | else |
| 6574 | done = true; |
| 6575 | if (CanTy.isVolatileQualified()) |
| 6576 | VRQuals.addVolatile(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6577 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 6578 | return VRQuals; |
| 6579 | } |
| 6580 | } |
| 6581 | } |
| 6582 | return VRQuals; |
| 6583 | } |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6584 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6585 | namespace { |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6586 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6587 | /// \brief Helper class to manage the addition of builtin operator overload |
| 6588 | /// candidates. It provides shared state and utility methods used throughout |
| 6589 | /// the process, as well as a helper method to add each group of builtin |
| 6590 | /// operator overloads from the standard to a candidate set. |
| 6591 | class BuiltinOperatorOverloadBuilder { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6592 | // Common instance state available to all overload candidate addition methods. |
| 6593 | Sema &S; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6594 | ArrayRef<Expr *> Args; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6595 | Qualifiers VisibleTypeConversionsQuals; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6596 | bool HasArithmeticOrEnumeralCandidateType; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6597 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6598 | OverloadCandidateSet &CandidateSet; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6599 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6600 | // Define some constants used to index and iterate over the arithemetic types |
| 6601 | // provided via the getArithmeticType() method below. |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6602 | // The "promoted arithmetic types" are the arithmetic |
| 6603 | // types are that preserved by promotion (C++ [over.built]p2). |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6604 | static const unsigned FirstIntegralType = 3; |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6605 | static const unsigned LastIntegralType = 20; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6606 | static const unsigned FirstPromotedIntegralType = 3, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6607 | LastPromotedIntegralType = 11; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6608 | static const unsigned FirstPromotedArithmeticType = 0, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6609 | LastPromotedArithmeticType = 11; |
| 6610 | static const unsigned NumArithmeticTypes = 20; |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6611 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6612 | /// \brief Get the canonical type for a given arithmetic type index. |
| 6613 | CanQualType getArithmeticType(unsigned index) { |
| 6614 | assert(index < NumArithmeticTypes); |
| 6615 | static CanQualType ASTContext::* const |
| 6616 | ArithmeticTypes[NumArithmeticTypes] = { |
| 6617 | // Start of promoted types. |
| 6618 | &ASTContext::FloatTy, |
| 6619 | &ASTContext::DoubleTy, |
| 6620 | &ASTContext::LongDoubleTy, |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6621 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6622 | // Start of integral types. |
| 6623 | &ASTContext::IntTy, |
| 6624 | &ASTContext::LongTy, |
| 6625 | &ASTContext::LongLongTy, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6626 | &ASTContext::Int128Ty, |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6627 | &ASTContext::UnsignedIntTy, |
| 6628 | &ASTContext::UnsignedLongTy, |
| 6629 | &ASTContext::UnsignedLongLongTy, |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6630 | &ASTContext::UnsignedInt128Ty, |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6631 | // End of promoted types. |
| 6632 | |
| 6633 | &ASTContext::BoolTy, |
| 6634 | &ASTContext::CharTy, |
| 6635 | &ASTContext::WCharTy, |
| 6636 | &ASTContext::Char16Ty, |
| 6637 | &ASTContext::Char32Ty, |
| 6638 | &ASTContext::SignedCharTy, |
| 6639 | &ASTContext::ShortTy, |
| 6640 | &ASTContext::UnsignedCharTy, |
| 6641 | &ASTContext::UnsignedShortTy, |
| 6642 | // End of integral types. |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6643 | // FIXME: What about complex? What about half? |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6644 | }; |
| 6645 | return S.Context.*ArithmeticTypes[index]; |
| 6646 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6647 | |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6648 | /// \brief Gets the canonical type resulting from the usual arithemetic |
| 6649 | /// converions for the given arithmetic types. |
| 6650 | CanQualType getUsualArithmeticConversions(unsigned L, unsigned R) { |
| 6651 | // Accelerator table for performing the usual arithmetic conversions. |
| 6652 | // The rules are basically: |
| 6653 | // - if either is floating-point, use the wider floating-point |
| 6654 | // - if same signedness, use the higher rank |
| 6655 | // - if same size, use unsigned of the higher rank |
| 6656 | // - use the larger type |
| 6657 | // These rules, together with the axiom that higher ranks are |
| 6658 | // never smaller, are sufficient to precompute all of these results |
| 6659 | // *except* when dealing with signed types of higher rank. |
| 6660 | // (we could precompute SLL x UI for all known platforms, but it's |
| 6661 | // better not to make any assumptions). |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6662 | // 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] | 6663 | enum PromotedType { |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6664 | Dep=-1, |
| 6665 | Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6666 | }; |
Nuno Lopes | 79e244f | 2012-04-21 14:45:25 +0000 | [diff] [blame] | 6667 | static const PromotedType ConversionsTable[LastPromotedArithmeticType] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6668 | [LastPromotedArithmeticType] = { |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6669 | /* Flt*/ { Flt, Dbl, LDbl, Flt, Flt, Flt, Flt, Flt, Flt, Flt, Flt }, |
| 6670 | /* Dbl*/ { Dbl, Dbl, LDbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl }, |
| 6671 | /*LDbl*/ { LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl }, |
| 6672 | /* SI*/ { Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 }, |
| 6673 | /* SL*/ { Flt, Dbl, LDbl, SL, SL, SLL, S128, Dep, UL, ULL, U128 }, |
| 6674 | /* SLL*/ { Flt, Dbl, LDbl, SLL, SLL, SLL, S128, Dep, Dep, ULL, U128 }, |
| 6675 | /*S128*/ { Flt, Dbl, LDbl, S128, S128, S128, S128, S128, S128, S128, U128 }, |
| 6676 | /* UI*/ { Flt, Dbl, LDbl, UI, Dep, Dep, S128, UI, UL, ULL, U128 }, |
| 6677 | /* UL*/ { Flt, Dbl, LDbl, UL, UL, Dep, S128, UL, UL, ULL, U128 }, |
| 6678 | /* ULL*/ { Flt, Dbl, LDbl, ULL, ULL, ULL, S128, ULL, ULL, ULL, U128 }, |
| 6679 | /*U128*/ { Flt, Dbl, LDbl, U128, U128, U128, U128, U128, U128, U128, U128 }, |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6680 | }; |
| 6681 | |
| 6682 | assert(L < LastPromotedArithmeticType); |
| 6683 | assert(R < LastPromotedArithmeticType); |
| 6684 | int Idx = ConversionsTable[L][R]; |
| 6685 | |
| 6686 | // Fast path: the table gives us a concrete answer. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6687 | if (Idx != Dep) return getArithmeticType(Idx); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6688 | |
| 6689 | // Slow path: we need to compare widths. |
| 6690 | // An invariant is that the signed type has higher rank. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6691 | CanQualType LT = getArithmeticType(L), |
| 6692 | RT = getArithmeticType(R); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6693 | unsigned LW = S.Context.getIntWidth(LT), |
| 6694 | RW = S.Context.getIntWidth(RT); |
| 6695 | |
| 6696 | // If they're different widths, use the signed type. |
| 6697 | if (LW > RW) return LT; |
| 6698 | else if (LW < RW) return RT; |
| 6699 | |
| 6700 | // Otherwise, use the unsigned type of the signed type's rank. |
| 6701 | if (L == SL || R == SL) return S.Context.UnsignedLongTy; |
| 6702 | assert(L == SLL || R == SLL); |
| 6703 | return S.Context.UnsignedLongLongTy; |
| 6704 | } |
| 6705 | |
Chandler Carruth | 3c69dc4 | 2010-12-12 09:22:45 +0000 | [diff] [blame] | 6706 | /// \brief Helper method to factor out the common pattern of adding overloads |
| 6707 | /// for '++' and '--' builtin operators. |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6708 | void addPlusPlusMinusMinusStyleOverloads(QualType CandidateTy, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6709 | bool HasVolatile, |
| 6710 | bool HasRestrict) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6711 | QualType ParamTypes[2] = { |
| 6712 | S.Context.getLValueReferenceType(CandidateTy), |
| 6713 | S.Context.IntTy |
| 6714 | }; |
| 6715 | |
| 6716 | // Non-volatile version. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6717 | if (Args.size() == 1) |
| 6718 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6719 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6720 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6721 | |
| 6722 | // Use a heuristic to reduce number of builtin candidates in the set: |
| 6723 | // add volatile version only if there are conversions to a volatile type. |
| 6724 | if (HasVolatile) { |
| 6725 | ParamTypes[0] = |
| 6726 | S.Context.getLValueReferenceType( |
| 6727 | S.Context.getVolatileType(CandidateTy)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6728 | if (Args.size() == 1) |
| 6729 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6730 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6731 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6732 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6733 | |
| 6734 | // Add restrict version only if there are conversions to a restrict type |
| 6735 | // and our candidate type is a non-restrict-qualified pointer. |
| 6736 | if (HasRestrict && CandidateTy->isAnyPointerType() && |
| 6737 | !CandidateTy.isRestrictQualified()) { |
| 6738 | ParamTypes[0] |
| 6739 | = S.Context.getLValueReferenceType( |
| 6740 | S.Context.getCVRQualifiedType(CandidateTy, Qualifiers::Restrict)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6741 | if (Args.size() == 1) |
| 6742 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6743 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6744 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6745 | |
| 6746 | if (HasVolatile) { |
| 6747 | ParamTypes[0] |
| 6748 | = S.Context.getLValueReferenceType( |
| 6749 | S.Context.getCVRQualifiedType(CandidateTy, |
| 6750 | (Qualifiers::Volatile | |
| 6751 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6752 | if (Args.size() == 1) |
| 6753 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6754 | else |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6755 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, CandidateSet); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6756 | } |
| 6757 | } |
| 6758 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6759 | } |
| 6760 | |
| 6761 | public: |
| 6762 | BuiltinOperatorOverloadBuilder( |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6763 | Sema &S, ArrayRef<Expr *> Args, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6764 | Qualifiers VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6765 | bool HasArithmeticOrEnumeralCandidateType, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6766 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6767 | OverloadCandidateSet &CandidateSet) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6768 | : S(S), Args(Args), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6769 | VisibleTypeConversionsQuals(VisibleTypeConversionsQuals), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6770 | HasArithmeticOrEnumeralCandidateType( |
| 6771 | HasArithmeticOrEnumeralCandidateType), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6772 | CandidateTypes(CandidateTypes), |
| 6773 | CandidateSet(CandidateSet) { |
| 6774 | // Validate some of our static helper constants in debug builds. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6775 | assert(getArithmeticType(FirstPromotedIntegralType) == S.Context.IntTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6776 | "Invalid first promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6777 | assert(getArithmeticType(LastPromotedIntegralType - 1) |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6778 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6779 | "Invalid last promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6780 | assert(getArithmeticType(FirstPromotedArithmeticType) |
| 6781 | == S.Context.FloatTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6782 | "Invalid first promoted arithmetic type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6783 | assert(getArithmeticType(LastPromotedArithmeticType - 1) |
Richard Smith | 3c2fcf8 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6784 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6785 | "Invalid last promoted arithmetic type"); |
| 6786 | } |
| 6787 | |
| 6788 | // C++ [over.built]p3: |
| 6789 | // |
| 6790 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 6791 | // is either volatile or empty, there exist candidate operator |
| 6792 | // functions of the form |
| 6793 | // |
| 6794 | // VQ T& operator++(VQ T&); |
| 6795 | // T operator++(VQ T&, int); |
| 6796 | // |
| 6797 | // C++ [over.built]p4: |
| 6798 | // |
| 6799 | // For every pair (T, VQ), where T is an arithmetic type other |
| 6800 | // than bool, and VQ is either volatile or empty, there exist |
| 6801 | // candidate operator functions of the form |
| 6802 | // |
| 6803 | // VQ T& operator--(VQ T&); |
| 6804 | // T operator--(VQ T&, int); |
| 6805 | void addPlusPlusMinusMinusArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6806 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6807 | return; |
| 6808 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6809 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 6810 | Arith < NumArithmeticTypes; ++Arith) { |
| 6811 | addPlusPlusMinusMinusStyleOverloads( |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6812 | getArithmeticType(Arith), |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6813 | VisibleTypeConversionsQuals.hasVolatile(), |
| 6814 | VisibleTypeConversionsQuals.hasRestrict()); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6815 | } |
| 6816 | } |
| 6817 | |
| 6818 | // C++ [over.built]p5: |
| 6819 | // |
| 6820 | // For every pair (T, VQ), where T is a cv-qualified or |
| 6821 | // cv-unqualified object type, and VQ is either volatile or |
| 6822 | // empty, there exist candidate operator functions of the form |
| 6823 | // |
| 6824 | // T*VQ& operator++(T*VQ&); |
| 6825 | // T*VQ& operator--(T*VQ&); |
| 6826 | // T* operator++(T*VQ&, int); |
| 6827 | // T* operator--(T*VQ&, int); |
| 6828 | void addPlusPlusMinusMinusPointerOverloads() { |
| 6829 | for (BuiltinCandidateTypeSet::iterator |
| 6830 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6831 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6832 | Ptr != PtrEnd; ++Ptr) { |
| 6833 | // Skip pointer types that aren't pointers to object types. |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6834 | if (!(*Ptr)->getPointeeType()->isObjectType()) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6835 | continue; |
| 6836 | |
| 6837 | addPlusPlusMinusMinusStyleOverloads(*Ptr, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6838 | (!(*Ptr).isVolatileQualified() && |
| 6839 | VisibleTypeConversionsQuals.hasVolatile()), |
| 6840 | (!(*Ptr).isRestrictQualified() && |
| 6841 | VisibleTypeConversionsQuals.hasRestrict())); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6842 | } |
| 6843 | } |
| 6844 | |
| 6845 | // C++ [over.built]p6: |
| 6846 | // For every cv-qualified or cv-unqualified object type T, there |
| 6847 | // exist candidate operator functions of the form |
| 6848 | // |
| 6849 | // T& operator*(T*); |
| 6850 | // |
| 6851 | // C++ [over.built]p7: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6852 | // 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] | 6853 | // ref-qualifier, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6854 | // T& operator*(T*); |
| 6855 | void addUnaryStarPointerOverloads() { |
| 6856 | for (BuiltinCandidateTypeSet::iterator |
| 6857 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6858 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6859 | Ptr != PtrEnd; ++Ptr) { |
| 6860 | QualType ParamTy = *Ptr; |
| 6861 | QualType PointeeTy = ParamTy->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6862 | if (!PointeeTy->isObjectType() && !PointeeTy->isFunctionType()) |
| 6863 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6864 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6865 | if (const FunctionProtoType *Proto =PointeeTy->getAs<FunctionProtoType>()) |
| 6866 | if (Proto->getTypeQuals() || Proto->getRefQualifier()) |
| 6867 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6868 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6869 | S.AddBuiltinCandidate(S.Context.getLValueReferenceType(PointeeTy), |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6870 | &ParamTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6871 | } |
| 6872 | } |
| 6873 | |
| 6874 | // C++ [over.built]p9: |
| 6875 | // For every promoted arithmetic type T, there exist candidate |
| 6876 | // operator functions of the form |
| 6877 | // |
| 6878 | // T operator+(T); |
| 6879 | // T operator-(T); |
| 6880 | void addUnaryPlusOrMinusArithmeticOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6881 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6882 | return; |
| 6883 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6884 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 6885 | Arith < LastPromotedArithmeticType; ++Arith) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6886 | QualType ArithTy = getArithmeticType(Arith); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6887 | S.AddBuiltinCandidate(ArithTy, &ArithTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6888 | } |
| 6889 | |
| 6890 | // Extension: We also add these operators for vector types. |
| 6891 | for (BuiltinCandidateTypeSet::iterator |
| 6892 | Vec = CandidateTypes[0].vector_begin(), |
| 6893 | VecEnd = CandidateTypes[0].vector_end(); |
| 6894 | Vec != VecEnd; ++Vec) { |
| 6895 | QualType VecTy = *Vec; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6896 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6897 | } |
| 6898 | } |
| 6899 | |
| 6900 | // C++ [over.built]p8: |
| 6901 | // For every type T, there exist candidate operator functions of |
| 6902 | // the form |
| 6903 | // |
| 6904 | // T* operator+(T*); |
| 6905 | void addUnaryPlusPointerOverloads() { |
| 6906 | for (BuiltinCandidateTypeSet::iterator |
| 6907 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6908 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6909 | Ptr != PtrEnd; ++Ptr) { |
| 6910 | QualType ParamTy = *Ptr; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6911 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6912 | } |
| 6913 | } |
| 6914 | |
| 6915 | // C++ [over.built]p10: |
| 6916 | // For every promoted integral type T, there exist candidate |
| 6917 | // operator functions of the form |
| 6918 | // |
| 6919 | // T operator~(T); |
| 6920 | void addUnaryTildePromotedIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6921 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6922 | return; |
| 6923 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6924 | for (unsigned Int = FirstPromotedIntegralType; |
| 6925 | Int < LastPromotedIntegralType; ++Int) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6926 | QualType IntTy = getArithmeticType(Int); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6927 | S.AddBuiltinCandidate(IntTy, &IntTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6928 | } |
| 6929 | |
| 6930 | // Extension: We also add this operator for vector types. |
| 6931 | for (BuiltinCandidateTypeSet::iterator |
| 6932 | Vec = CandidateTypes[0].vector_begin(), |
| 6933 | VecEnd = CandidateTypes[0].vector_end(); |
| 6934 | Vec != VecEnd; ++Vec) { |
| 6935 | QualType VecTy = *Vec; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6936 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6937 | } |
| 6938 | } |
| 6939 | |
| 6940 | // C++ [over.match.oper]p16: |
| 6941 | // For every pointer to member type T, there exist candidate operator |
| 6942 | // functions of the form |
| 6943 | // |
| 6944 | // bool operator==(T,T); |
| 6945 | // bool operator!=(T,T); |
| 6946 | void addEqualEqualOrNotEqualMemberPointerOverloads() { |
| 6947 | /// Set of (canonical) types that we've already handled. |
| 6948 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6949 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6950 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6951 | for (BuiltinCandidateTypeSet::iterator |
| 6952 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 6953 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 6954 | MemPtr != MemPtrEnd; |
| 6955 | ++MemPtr) { |
| 6956 | // Don't add the same builtin candidate twice. |
| 6957 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 6958 | continue; |
| 6959 | |
| 6960 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6961 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6962 | } |
| 6963 | } |
| 6964 | } |
| 6965 | |
| 6966 | // C++ [over.built]p15: |
| 6967 | // |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6968 | // For every T, where T is an enumeration type, a pointer type, or |
| 6969 | // std::nullptr_t, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6970 | // |
| 6971 | // bool operator<(T, T); |
| 6972 | // bool operator>(T, T); |
| 6973 | // bool operator<=(T, T); |
| 6974 | // bool operator>=(T, T); |
| 6975 | // bool operator==(T, T); |
| 6976 | // bool operator!=(T, T); |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6977 | void addRelationalPointerOrEnumeralOverloads() { |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 6978 | // C++ [over.match.oper]p3: |
| 6979 | // [...]the built-in candidates include all of the candidate operator |
| 6980 | // functions defined in 13.6 that, compared to the given operator, [...] |
| 6981 | // do not have the same parameter-type-list as any non-template non-member |
| 6982 | // candidate. |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6983 | // |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 6984 | // Note that in practice, this only affects enumeration types because there |
| 6985 | // aren't any built-in candidates of record type, and a user-defined operator |
| 6986 | // must have an operand of record or enumeration type. Also, the only other |
| 6987 | // overloaded operator with enumeration arguments, operator=, |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6988 | // cannot be overloaded for enumeration types, so this is the only place |
| 6989 | // where we must suppress candidates like this. |
| 6990 | llvm::DenseSet<std::pair<CanQualType, CanQualType> > |
| 6991 | UserDefinedBinaryOperators; |
| 6992 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 6993 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6994 | if (CandidateTypes[ArgIdx].enumeration_begin() != |
| 6995 | CandidateTypes[ArgIdx].enumeration_end()) { |
| 6996 | for (OverloadCandidateSet::iterator C = CandidateSet.begin(), |
| 6997 | CEnd = CandidateSet.end(); |
| 6998 | C != CEnd; ++C) { |
| 6999 | if (!C->Viable || !C->Function || C->Function->getNumParams() != 2) |
| 7000 | continue; |
| 7001 | |
Eli Friedman | 97c6739 | 2012-09-18 21:52:24 +0000 | [diff] [blame] | 7002 | if (C->Function->isFunctionTemplateSpecialization()) |
| 7003 | continue; |
| 7004 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7005 | QualType FirstParamType = |
| 7006 | C->Function->getParamDecl(0)->getType().getUnqualifiedType(); |
| 7007 | QualType SecondParamType = |
| 7008 | C->Function->getParamDecl(1)->getType().getUnqualifiedType(); |
| 7009 | |
| 7010 | // Skip if either parameter isn't of enumeral type. |
| 7011 | if (!FirstParamType->isEnumeralType() || |
| 7012 | !SecondParamType->isEnumeralType()) |
| 7013 | continue; |
| 7014 | |
| 7015 | // Add this operator to the set of known user-defined operators. |
| 7016 | UserDefinedBinaryOperators.insert( |
| 7017 | std::make_pair(S.Context.getCanonicalType(FirstParamType), |
| 7018 | S.Context.getCanonicalType(SecondParamType))); |
| 7019 | } |
| 7020 | } |
| 7021 | } |
| 7022 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7023 | /// Set of (canonical) types that we've already handled. |
| 7024 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7025 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7026 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7027 | for (BuiltinCandidateTypeSet::iterator |
| 7028 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 7029 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 7030 | Ptr != PtrEnd; ++Ptr) { |
| 7031 | // Don't add the same builtin candidate twice. |
| 7032 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7033 | continue; |
| 7034 | |
| 7035 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7036 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7037 | } |
| 7038 | for (BuiltinCandidateTypeSet::iterator |
| 7039 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7040 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7041 | Enum != EnumEnd; ++Enum) { |
| 7042 | CanQualType CanonType = S.Context.getCanonicalType(*Enum); |
| 7043 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7044 | // Don't add the same builtin candidate twice, or if a user defined |
| 7045 | // candidate exists. |
| 7046 | if (!AddedTypes.insert(CanonType) || |
| 7047 | UserDefinedBinaryOperators.count(std::make_pair(CanonType, |
| 7048 | CanonType))) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7049 | continue; |
| 7050 | |
| 7051 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7052 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7053 | } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7054 | |
| 7055 | if (CandidateTypes[ArgIdx].hasNullPtrType()) { |
| 7056 | CanQualType NullPtrTy = S.Context.getCanonicalType(S.Context.NullPtrTy); |
| 7057 | if (AddedTypes.insert(NullPtrTy) && |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7058 | !UserDefinedBinaryOperators.count(std::make_pair(NullPtrTy, |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7059 | NullPtrTy))) { |
| 7060 | QualType ParamTypes[2] = { NullPtrTy, NullPtrTy }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7061 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 7062 | CandidateSet); |
| 7063 | } |
| 7064 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7065 | } |
| 7066 | } |
| 7067 | |
| 7068 | // C++ [over.built]p13: |
| 7069 | // |
| 7070 | // For every cv-qualified or cv-unqualified object type T |
| 7071 | // there exist candidate operator functions of the form |
| 7072 | // |
| 7073 | // T* operator+(T*, ptrdiff_t); |
| 7074 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 7075 | // T* operator-(T*, ptrdiff_t); |
| 7076 | // T* operator+(ptrdiff_t, T*); |
| 7077 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 7078 | // |
| 7079 | // C++ [over.built]p14: |
| 7080 | // |
| 7081 | // For every T, where T is a pointer to object type, there |
| 7082 | // exist candidate operator functions of the form |
| 7083 | // |
| 7084 | // ptrdiff_t operator-(T, T); |
| 7085 | void addBinaryPlusOrMinusPointerOverloads(OverloadedOperatorKind Op) { |
| 7086 | /// Set of (canonical) types that we've already handled. |
| 7087 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7088 | |
| 7089 | for (int Arg = 0; Arg < 2; ++Arg) { |
| 7090 | QualType AsymetricParamTypes[2] = { |
| 7091 | S.Context.getPointerDiffType(), |
| 7092 | S.Context.getPointerDiffType(), |
| 7093 | }; |
| 7094 | for (BuiltinCandidateTypeSet::iterator |
| 7095 | Ptr = CandidateTypes[Arg].pointer_begin(), |
| 7096 | PtrEnd = CandidateTypes[Arg].pointer_end(); |
| 7097 | Ptr != PtrEnd; ++Ptr) { |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7098 | QualType PointeeTy = (*Ptr)->getPointeeType(); |
| 7099 | if (!PointeeTy->isObjectType()) |
| 7100 | continue; |
| 7101 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7102 | AsymetricParamTypes[Arg] = *Ptr; |
| 7103 | if (Arg == 0 || Op == OO_Plus) { |
| 7104 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 7105 | // T* operator+(ptrdiff_t, T*); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7106 | S.AddBuiltinCandidate(*Ptr, AsymetricParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7107 | } |
| 7108 | if (Op == OO_Minus) { |
| 7109 | // ptrdiff_t operator-(T, T); |
| 7110 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7111 | continue; |
| 7112 | |
| 7113 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 7114 | S.AddBuiltinCandidate(S.Context.getPointerDiffType(), ParamTypes, |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7115 | Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7116 | } |
| 7117 | } |
| 7118 | } |
| 7119 | } |
| 7120 | |
| 7121 | // C++ [over.built]p12: |
| 7122 | // |
| 7123 | // For every pair of promoted arithmetic types L and R, there |
| 7124 | // exist candidate operator functions of the form |
| 7125 | // |
| 7126 | // LR operator*(L, R); |
| 7127 | // LR operator/(L, R); |
| 7128 | // LR operator+(L, R); |
| 7129 | // LR operator-(L, R); |
| 7130 | // bool operator<(L, R); |
| 7131 | // bool operator>(L, R); |
| 7132 | // bool operator<=(L, R); |
| 7133 | // bool operator>=(L, R); |
| 7134 | // bool operator==(L, R); |
| 7135 | // bool operator!=(L, R); |
| 7136 | // |
| 7137 | // where LR is the result of the usual arithmetic conversions |
| 7138 | // between types L and R. |
| 7139 | // |
| 7140 | // C++ [over.built]p24: |
| 7141 | // |
| 7142 | // For every pair of promoted arithmetic types L and R, there exist |
| 7143 | // candidate operator functions of the form |
| 7144 | // |
| 7145 | // LR operator?(bool, L, R); |
| 7146 | // |
| 7147 | // where LR is the result of the usual arithmetic conversions |
| 7148 | // between types L and R. |
| 7149 | // Our candidates ignore the first parameter. |
| 7150 | void addGenericBinaryArithmeticOverloads(bool isComparison) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7151 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7152 | return; |
| 7153 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7154 | for (unsigned Left = FirstPromotedArithmeticType; |
| 7155 | Left < LastPromotedArithmeticType; ++Left) { |
| 7156 | for (unsigned Right = FirstPromotedArithmeticType; |
| 7157 | Right < LastPromotedArithmeticType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7158 | QualType LandR[2] = { getArithmeticType(Left), |
| 7159 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7160 | QualType Result = |
| 7161 | isComparison ? S.Context.BoolTy |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7162 | : getUsualArithmeticConversions(Left, Right); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7163 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7164 | } |
| 7165 | } |
| 7166 | |
| 7167 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 7168 | // conditional operator for vector types. |
| 7169 | for (BuiltinCandidateTypeSet::iterator |
| 7170 | Vec1 = CandidateTypes[0].vector_begin(), |
| 7171 | Vec1End = CandidateTypes[0].vector_end(); |
| 7172 | Vec1 != Vec1End; ++Vec1) { |
| 7173 | for (BuiltinCandidateTypeSet::iterator |
| 7174 | Vec2 = CandidateTypes[1].vector_begin(), |
| 7175 | Vec2End = CandidateTypes[1].vector_end(); |
| 7176 | Vec2 != Vec2End; ++Vec2) { |
| 7177 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 7178 | QualType Result = S.Context.BoolTy; |
| 7179 | if (!isComparison) { |
| 7180 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 7181 | Result = *Vec1; |
| 7182 | else |
| 7183 | Result = *Vec2; |
| 7184 | } |
| 7185 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7186 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7187 | } |
| 7188 | } |
| 7189 | } |
| 7190 | |
| 7191 | // C++ [over.built]p17: |
| 7192 | // |
| 7193 | // For every pair of promoted integral types L and R, there |
| 7194 | // exist candidate operator functions of the form |
| 7195 | // |
| 7196 | // LR operator%(L, R); |
| 7197 | // LR operator&(L, R); |
| 7198 | // LR operator^(L, R); |
| 7199 | // LR operator|(L, R); |
| 7200 | // L operator<<(L, R); |
| 7201 | // L operator>>(L, R); |
| 7202 | // |
| 7203 | // where LR is the result of the usual arithmetic conversions |
| 7204 | // between types L and R. |
| 7205 | void addBinaryBitwiseArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7206 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7207 | return; |
| 7208 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7209 | for (unsigned Left = FirstPromotedIntegralType; |
| 7210 | Left < LastPromotedIntegralType; ++Left) { |
| 7211 | for (unsigned Right = FirstPromotedIntegralType; |
| 7212 | Right < LastPromotedIntegralType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7213 | QualType LandR[2] = { getArithmeticType(Left), |
| 7214 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7215 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 7216 | ? LandR[0] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7217 | : getUsualArithmeticConversions(Left, Right); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7218 | S.AddBuiltinCandidate(Result, LandR, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7219 | } |
| 7220 | } |
| 7221 | } |
| 7222 | |
| 7223 | // C++ [over.built]p20: |
| 7224 | // |
| 7225 | // For every pair (T, VQ), where T is an enumeration or |
| 7226 | // pointer to member type and VQ is either volatile or |
| 7227 | // empty, there exist candidate operator functions of the form |
| 7228 | // |
| 7229 | // VQ T& operator=(VQ T&, T); |
| 7230 | void addAssignmentMemberPointerOrEnumeralOverloads() { |
| 7231 | /// Set of (canonical) types that we've already handled. |
| 7232 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7233 | |
| 7234 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7235 | for (BuiltinCandidateTypeSet::iterator |
| 7236 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7237 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7238 | Enum != EnumEnd; ++Enum) { |
| 7239 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 7240 | continue; |
| 7241 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7242 | AddBuiltinAssignmentOperatorCandidates(S, *Enum, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7243 | } |
| 7244 | |
| 7245 | for (BuiltinCandidateTypeSet::iterator |
| 7246 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7247 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7248 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7249 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7250 | continue; |
| 7251 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7252 | AddBuiltinAssignmentOperatorCandidates(S, *MemPtr, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7253 | } |
| 7254 | } |
| 7255 | } |
| 7256 | |
| 7257 | // C++ [over.built]p19: |
| 7258 | // |
| 7259 | // For every pair (T, VQ), where T is any type and VQ is either |
| 7260 | // volatile or empty, there exist candidate operator functions |
| 7261 | // of the form |
| 7262 | // |
| 7263 | // T*VQ& operator=(T*VQ&, T*); |
| 7264 | // |
| 7265 | // C++ [over.built]p21: |
| 7266 | // |
| 7267 | // For every pair (T, VQ), where T is a cv-qualified or |
| 7268 | // cv-unqualified object type and VQ is either volatile or |
| 7269 | // empty, there exist candidate operator functions of the form |
| 7270 | // |
| 7271 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 7272 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 7273 | void addAssignmentPointerOverloads(bool isEqualOp) { |
| 7274 | /// Set of (canonical) types that we've already handled. |
| 7275 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7276 | |
| 7277 | for (BuiltinCandidateTypeSet::iterator |
| 7278 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7279 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7280 | Ptr != PtrEnd; ++Ptr) { |
| 7281 | // If this is operator=, keep track of the builtin candidates we added. |
| 7282 | if (isEqualOp) |
| 7283 | AddedTypes.insert(S.Context.getCanonicalType(*Ptr)); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7284 | else if (!(*Ptr)->getPointeeType()->isObjectType()) |
| 7285 | continue; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7286 | |
| 7287 | // non-volatile version |
| 7288 | QualType ParamTypes[2] = { |
| 7289 | S.Context.getLValueReferenceType(*Ptr), |
| 7290 | isEqualOp ? *Ptr : S.Context.getPointerDiffType(), |
| 7291 | }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7292 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7293 | /*IsAssigmentOperator=*/ isEqualOp); |
| 7294 | |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7295 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7296 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7297 | if (NeedVolatile) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7298 | // volatile version |
| 7299 | ParamTypes[0] = |
| 7300 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7301 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7302 | /*IsAssigmentOperator=*/isEqualOp); |
| 7303 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7304 | |
| 7305 | if (!(*Ptr).isRestrictQualified() && |
| 7306 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7307 | // restrict version |
| 7308 | ParamTypes[0] |
| 7309 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7310 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7311 | /*IsAssigmentOperator=*/isEqualOp); |
| 7312 | |
| 7313 | if (NeedVolatile) { |
| 7314 | // volatile restrict version |
| 7315 | ParamTypes[0] |
| 7316 | = S.Context.getLValueReferenceType( |
| 7317 | S.Context.getCVRQualifiedType(*Ptr, |
| 7318 | (Qualifiers::Volatile | |
| 7319 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7320 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7321 | /*IsAssigmentOperator=*/isEqualOp); |
| 7322 | } |
| 7323 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7324 | } |
| 7325 | |
| 7326 | if (isEqualOp) { |
| 7327 | for (BuiltinCandidateTypeSet::iterator |
| 7328 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7329 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7330 | Ptr != PtrEnd; ++Ptr) { |
| 7331 | // Make sure we don't add the same candidate twice. |
| 7332 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7333 | continue; |
| 7334 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7335 | QualType ParamTypes[2] = { |
| 7336 | S.Context.getLValueReferenceType(*Ptr), |
| 7337 | *Ptr, |
| 7338 | }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7339 | |
| 7340 | // non-volatile version |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7341 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7342 | /*IsAssigmentOperator=*/true); |
| 7343 | |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7344 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7345 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7346 | if (NeedVolatile) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7347 | // volatile version |
| 7348 | ParamTypes[0] = |
| 7349 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7350 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7351 | /*IsAssigmentOperator=*/true); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7352 | } |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7353 | |
| 7354 | if (!(*Ptr).isRestrictQualified() && |
| 7355 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7356 | // restrict version |
| 7357 | ParamTypes[0] |
| 7358 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7359 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7360 | /*IsAssigmentOperator=*/true); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7361 | |
| 7362 | if (NeedVolatile) { |
| 7363 | // volatile restrict version |
| 7364 | ParamTypes[0] |
| 7365 | = S.Context.getLValueReferenceType( |
| 7366 | S.Context.getCVRQualifiedType(*Ptr, |
| 7367 | (Qualifiers::Volatile | |
| 7368 | Qualifiers::Restrict))); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7369 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
| 7370 | /*IsAssigmentOperator=*/true); |
Douglas Gregor | b1c6f5f | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7371 | } |
| 7372 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7373 | } |
| 7374 | } |
| 7375 | } |
| 7376 | |
| 7377 | // C++ [over.built]p18: |
| 7378 | // |
| 7379 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 7380 | // VQ is either volatile or empty, and R is a promoted |
| 7381 | // arithmetic type, there exist candidate operator functions of |
| 7382 | // the form |
| 7383 | // |
| 7384 | // VQ L& operator=(VQ L&, R); |
| 7385 | // VQ L& operator*=(VQ L&, R); |
| 7386 | // VQ L& operator/=(VQ L&, R); |
| 7387 | // VQ L& operator+=(VQ L&, R); |
| 7388 | // VQ L& operator-=(VQ L&, R); |
| 7389 | void addAssignmentArithmeticOverloads(bool isEqualOp) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7390 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7391 | return; |
| 7392 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7393 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 7394 | for (unsigned Right = FirstPromotedArithmeticType; |
| 7395 | Right < LastPromotedArithmeticType; ++Right) { |
| 7396 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7397 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7398 | |
| 7399 | // Add this built-in operator as a candidate (VQ is empty). |
| 7400 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7401 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7402 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7403 | /*IsAssigmentOperator=*/isEqualOp); |
| 7404 | |
| 7405 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7406 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7407 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7408 | S.Context.getVolatileType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7409 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7410 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7411 | /*IsAssigmentOperator=*/isEqualOp); |
| 7412 | } |
| 7413 | } |
| 7414 | } |
| 7415 | |
| 7416 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 7417 | for (BuiltinCandidateTypeSet::iterator |
| 7418 | Vec1 = CandidateTypes[0].vector_begin(), |
| 7419 | Vec1End = CandidateTypes[0].vector_end(); |
| 7420 | Vec1 != Vec1End; ++Vec1) { |
| 7421 | for (BuiltinCandidateTypeSet::iterator |
| 7422 | Vec2 = CandidateTypes[1].vector_begin(), |
| 7423 | Vec2End = CandidateTypes[1].vector_end(); |
| 7424 | Vec2 != Vec2End; ++Vec2) { |
| 7425 | QualType ParamTypes[2]; |
| 7426 | ParamTypes[1] = *Vec2; |
| 7427 | // Add this built-in operator as a candidate (VQ is empty). |
| 7428 | ParamTypes[0] = S.Context.getLValueReferenceType(*Vec1); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7429 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7430 | /*IsAssigmentOperator=*/isEqualOp); |
| 7431 | |
| 7432 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7433 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7434 | ParamTypes[0] = S.Context.getVolatileType(*Vec1); |
| 7435 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7436 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7437 | /*IsAssigmentOperator=*/isEqualOp); |
| 7438 | } |
| 7439 | } |
| 7440 | } |
| 7441 | } |
| 7442 | |
| 7443 | // C++ [over.built]p22: |
| 7444 | // |
| 7445 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 7446 | // is either volatile or empty, and R is a promoted integral |
| 7447 | // type, there exist candidate operator functions of the form |
| 7448 | // |
| 7449 | // VQ L& operator%=(VQ L&, R); |
| 7450 | // VQ L& operator<<=(VQ L&, R); |
| 7451 | // VQ L& operator>>=(VQ L&, R); |
| 7452 | // VQ L& operator&=(VQ L&, R); |
| 7453 | // VQ L& operator^=(VQ L&, R); |
| 7454 | // VQ L& operator|=(VQ L&, R); |
| 7455 | void addAssignmentIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7456 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7457 | return; |
| 7458 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7459 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 7460 | for (unsigned Right = FirstPromotedIntegralType; |
| 7461 | Right < LastPromotedIntegralType; ++Right) { |
| 7462 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7463 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7464 | |
| 7465 | // Add this built-in operator as a candidate (VQ is empty). |
| 7466 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7467 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7468 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7469 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7470 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7471 | ParamTypes[0] = getArithmeticType(Left); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7472 | ParamTypes[0] = S.Context.getVolatileType(ParamTypes[0]); |
| 7473 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7474 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7475 | } |
| 7476 | } |
| 7477 | } |
| 7478 | } |
| 7479 | |
| 7480 | // C++ [over.operator]p23: |
| 7481 | // |
| 7482 | // There also exist candidate operator functions of the form |
| 7483 | // |
| 7484 | // bool operator!(bool); |
| 7485 | // bool operator&&(bool, bool); |
| 7486 | // bool operator||(bool, bool); |
| 7487 | void addExclaimOverload() { |
| 7488 | QualType ParamTy = S.Context.BoolTy; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7489 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7490 | /*IsAssignmentOperator=*/false, |
| 7491 | /*NumContextualBoolArguments=*/1); |
| 7492 | } |
| 7493 | void addAmpAmpOrPipePipeOverload() { |
| 7494 | QualType ParamTypes[2] = { S.Context.BoolTy, S.Context.BoolTy }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7495 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7496 | /*IsAssignmentOperator=*/false, |
| 7497 | /*NumContextualBoolArguments=*/2); |
| 7498 | } |
| 7499 | |
| 7500 | // C++ [over.built]p13: |
| 7501 | // |
| 7502 | // For every cv-qualified or cv-unqualified object type T there |
| 7503 | // exist candidate operator functions of the form |
| 7504 | // |
| 7505 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 7506 | // T& operator[](T*, ptrdiff_t); |
| 7507 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 7508 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 7509 | // T& operator[](ptrdiff_t, T*); |
| 7510 | void addSubscriptOverloads() { |
| 7511 | for (BuiltinCandidateTypeSet::iterator |
| 7512 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7513 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7514 | Ptr != PtrEnd; ++Ptr) { |
| 7515 | QualType ParamTypes[2] = { *Ptr, S.Context.getPointerDiffType() }; |
| 7516 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7517 | if (!PointeeType->isObjectType()) |
| 7518 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7519 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7520 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7521 | |
| 7522 | // T& operator[](T*, ptrdiff_t) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7523 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7524 | } |
| 7525 | |
| 7526 | for (BuiltinCandidateTypeSet::iterator |
| 7527 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7528 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7529 | Ptr != PtrEnd; ++Ptr) { |
| 7530 | QualType ParamTypes[2] = { S.Context.getPointerDiffType(), *Ptr }; |
| 7531 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7532 | if (!PointeeType->isObjectType()) |
| 7533 | continue; |
| 7534 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7535 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7536 | |
| 7537 | // T& operator[](ptrdiff_t, T*) |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7538 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7539 | } |
| 7540 | } |
| 7541 | |
| 7542 | // C++ [over.built]p11: |
| 7543 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 7544 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 7545 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 7546 | // there exist candidate operator functions of the form |
| 7547 | // |
| 7548 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 7549 | // |
| 7550 | // where CV12 is the union of CV1 and CV2. |
| 7551 | void addArrowStarOverloads() { |
| 7552 | for (BuiltinCandidateTypeSet::iterator |
| 7553 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7554 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7555 | Ptr != PtrEnd; ++Ptr) { |
| 7556 | QualType C1Ty = (*Ptr); |
| 7557 | QualType C1; |
| 7558 | QualifierCollector Q1; |
| 7559 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
| 7560 | if (!isa<RecordType>(C1)) |
| 7561 | continue; |
| 7562 | // heuristic to reduce number of builtin candidates in the set. |
| 7563 | // Add volatile/restrict version only if there are conversions to a |
| 7564 | // volatile/restrict type. |
| 7565 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 7566 | continue; |
| 7567 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 7568 | continue; |
| 7569 | for (BuiltinCandidateTypeSet::iterator |
| 7570 | MemPtr = CandidateTypes[1].member_pointer_begin(), |
| 7571 | MemPtrEnd = CandidateTypes[1].member_pointer_end(); |
| 7572 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7573 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 7574 | QualType C2 = QualType(mptr->getClass(), 0); |
| 7575 | C2 = C2.getUnqualifiedType(); |
| 7576 | if (C1 != C2 && !S.IsDerivedFrom(C1, C2)) |
| 7577 | break; |
| 7578 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 7579 | // build CV12 T& |
| 7580 | QualType T = mptr->getPointeeType(); |
| 7581 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 7582 | T.isVolatileQualified()) |
| 7583 | continue; |
| 7584 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 7585 | T.isRestrictQualified()) |
| 7586 | continue; |
| 7587 | T = Q1.apply(S.Context, T); |
| 7588 | QualType ResultTy = S.Context.getLValueReferenceType(T); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7589 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7590 | } |
| 7591 | } |
| 7592 | } |
| 7593 | |
| 7594 | // Note that we don't consider the first argument, since it has been |
| 7595 | // contextually converted to bool long ago. The candidates below are |
| 7596 | // therefore added as binary. |
| 7597 | // |
| 7598 | // C++ [over.built]p25: |
| 7599 | // For every type T, where T is a pointer, pointer-to-member, or scoped |
| 7600 | // enumeration type, there exist candidate operator functions of the form |
| 7601 | // |
| 7602 | // T operator?(bool, T, T); |
| 7603 | // |
| 7604 | void addConditionalOperatorOverloads() { |
| 7605 | /// Set of (canonical) types that we've already handled. |
| 7606 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7607 | |
| 7608 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7609 | for (BuiltinCandidateTypeSet::iterator |
| 7610 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 7611 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 7612 | Ptr != PtrEnd; ++Ptr) { |
| 7613 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7614 | continue; |
| 7615 | |
| 7616 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7617 | S.AddBuiltinCandidate(*Ptr, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7618 | } |
| 7619 | |
| 7620 | for (BuiltinCandidateTypeSet::iterator |
| 7621 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7622 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7623 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7624 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7625 | continue; |
| 7626 | |
| 7627 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7628 | S.AddBuiltinCandidate(*MemPtr, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7629 | } |
| 7630 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7631 | if (S.getLangOpts().CPlusPlus11) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7632 | for (BuiltinCandidateTypeSet::iterator |
| 7633 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7634 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7635 | Enum != EnumEnd; ++Enum) { |
| 7636 | if (!(*Enum)->getAs<EnumType>()->getDecl()->isScoped()) |
| 7637 | continue; |
| 7638 | |
| 7639 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 7640 | continue; |
| 7641 | |
| 7642 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7643 | S.AddBuiltinCandidate(*Enum, ParamTypes, Args, CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7644 | } |
| 7645 | } |
| 7646 | } |
| 7647 | } |
| 7648 | }; |
| 7649 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7650 | } // end anonymous namespace |
| 7651 | |
| 7652 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 7653 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 7654 | /// on the operator @p Op and the arguments given. For example, if the |
| 7655 | /// operator is a binary '+', this routine might add "int |
| 7656 | /// operator+(int, int)" to cover integer addition. |
Robert Wilhelm | 834c058 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 7657 | void Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 7658 | SourceLocation OpLoc, |
| 7659 | ArrayRef<Expr *> Args, |
| 7660 | OverloadCandidateSet &CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7661 | // Find all of the types that the arguments can convert to, but only |
| 7662 | // if the operator we're looking at has built-in operator candidates |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7663 | // that make use of these types. Also record whether we encounter non-record |
| 7664 | // candidate types or either arithmetic or enumeral candidate types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7665 | Qualifiers VisibleTypeConversionsQuals; |
| 7666 | VisibleTypeConversionsQuals.addConst(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7667 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 7668 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7669 | |
| 7670 | bool HasNonRecordCandidateType = false; |
| 7671 | bool HasArithmeticOrEnumeralCandidateType = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7672 | SmallVector<BuiltinCandidateTypeSet, 2> CandidateTypes; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7673 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7674 | CandidateTypes.push_back(BuiltinCandidateTypeSet(*this)); |
| 7675 | CandidateTypes[ArgIdx].AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 7676 | OpLoc, |
| 7677 | true, |
| 7678 | (Op == OO_Exclaim || |
| 7679 | Op == OO_AmpAmp || |
| 7680 | Op == OO_PipePipe), |
| 7681 | VisibleTypeConversionsQuals); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7682 | HasNonRecordCandidateType = HasNonRecordCandidateType || |
| 7683 | CandidateTypes[ArgIdx].hasNonRecordTypes(); |
| 7684 | HasArithmeticOrEnumeralCandidateType = |
| 7685 | HasArithmeticOrEnumeralCandidateType || |
| 7686 | CandidateTypes[ArgIdx].hasArithmeticOrEnumeralTypes(); |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7687 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7688 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7689 | // Exit early when no non-record types have been added to the candidate set |
| 7690 | // for any of the arguments to the operator. |
Douglas Gregor | 25aaff9 | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 7691 | // |
| 7692 | // We can't exit early for !, ||, or &&, since there we have always have |
| 7693 | // 'bool' overloads. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7694 | if (!HasNonRecordCandidateType && |
Douglas Gregor | 25aaff9 | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 7695 | !(Op == OO_Exclaim || Op == OO_AmpAmp || Op == OO_PipePipe)) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7696 | return; |
| 7697 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7698 | // Setup an object to manage the common state for building overloads. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7699 | BuiltinOperatorOverloadBuilder OpBuilder(*this, Args, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7700 | VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7701 | HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7702 | CandidateTypes, CandidateSet); |
| 7703 | |
| 7704 | // Dispatch over the operation to add in only those overloads which apply. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7705 | switch (Op) { |
| 7706 | case OO_None: |
| 7707 | case NUM_OVERLOADED_OPERATORS: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7708 | llvm_unreachable("Expected an overloaded operator"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7709 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7710 | case OO_New: |
| 7711 | case OO_Delete: |
| 7712 | case OO_Array_New: |
| 7713 | case OO_Array_Delete: |
| 7714 | case OO_Call: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7715 | llvm_unreachable( |
| 7716 | "Special operators don't use AddBuiltinOperatorCandidates"); |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7717 | |
| 7718 | case OO_Comma: |
| 7719 | case OO_Arrow: |
| 7720 | // C++ [over.match.oper]p3: |
| 7721 | // -- For the operator ',', the unary operator '&', or the |
| 7722 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7723 | break; |
| 7724 | |
| 7725 | case OO_Plus: // '+' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7726 | if (Args.size() == 1) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7727 | OpBuilder.addUnaryPlusPointerOverloads(); |
Chandler Carruth | 32fe0d0 | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 7728 | // Fall through. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7729 | |
| 7730 | case OO_Minus: // '-' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7731 | if (Args.size() == 1) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7732 | OpBuilder.addUnaryPlusOrMinusArithmeticOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7733 | } else { |
| 7734 | OpBuilder.addBinaryPlusOrMinusPointerOverloads(Op); |
| 7735 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7736 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7737 | break; |
| 7738 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7739 | case OO_Star: // '*' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7740 | if (Args.size() == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7741 | OpBuilder.addUnaryStarPointerOverloads(); |
| 7742 | else |
| 7743 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7744 | break; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7745 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7746 | case OO_Slash: |
| 7747 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 7748 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7749 | |
| 7750 | case OO_PlusPlus: |
| 7751 | case OO_MinusMinus: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7752 | OpBuilder.addPlusPlusMinusMinusArithmeticOverloads(Op); |
| 7753 | OpBuilder.addPlusPlusMinusMinusPointerOverloads(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7754 | break; |
| 7755 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 7756 | case OO_EqualEqual: |
| 7757 | case OO_ExclaimEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7758 | OpBuilder.addEqualEqualOrNotEqualMemberPointerOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 7759 | // Fall through. |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 7760 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7761 | case OO_Less: |
| 7762 | case OO_Greater: |
| 7763 | case OO_LessEqual: |
| 7764 | case OO_GreaterEqual: |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7765 | OpBuilder.addRelationalPointerOrEnumeralOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 7766 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/true); |
| 7767 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7768 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7769 | case OO_Percent: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7770 | case OO_Caret: |
| 7771 | case OO_Pipe: |
| 7772 | case OO_LessLess: |
| 7773 | case OO_GreaterGreater: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7774 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7775 | break; |
| 7776 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7777 | case OO_Amp: // '&' is either unary or binary |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 7778 | if (Args.size() == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7779 | // C++ [over.match.oper]p3: |
| 7780 | // -- For the operator ',', the unary operator '&', or the |
| 7781 | // operator '->', the built-in candidates set is empty. |
| 7782 | break; |
| 7783 | |
| 7784 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
| 7785 | break; |
| 7786 | |
| 7787 | case OO_Tilde: |
| 7788 | OpBuilder.addUnaryTildePromotedIntegralOverloads(); |
| 7789 | break; |
| 7790 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7791 | case OO_Equal: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7792 | OpBuilder.addAssignmentMemberPointerOrEnumeralOverloads(); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7793 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7794 | |
| 7795 | case OO_PlusEqual: |
| 7796 | case OO_MinusEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7797 | OpBuilder.addAssignmentPointerOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7798 | // Fall through. |
| 7799 | |
| 7800 | case OO_StarEqual: |
| 7801 | case OO_SlashEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7802 | OpBuilder.addAssignmentArithmeticOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7803 | break; |
| 7804 | |
| 7805 | case OO_PercentEqual: |
| 7806 | case OO_LessLessEqual: |
| 7807 | case OO_GreaterGreaterEqual: |
| 7808 | case OO_AmpEqual: |
| 7809 | case OO_CaretEqual: |
| 7810 | case OO_PipeEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7811 | OpBuilder.addAssignmentIntegralOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7812 | break; |
| 7813 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7814 | case OO_Exclaim: |
| 7815 | OpBuilder.addExclaimOverload(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7816 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7817 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7818 | case OO_AmpAmp: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7819 | case OO_PipePipe: |
| 7820 | OpBuilder.addAmpAmpOrPipePipeOverload(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7821 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7822 | |
| 7823 | case OO_Subscript: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7824 | OpBuilder.addSubscriptOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7825 | break; |
| 7826 | |
| 7827 | case OO_ArrowStar: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7828 | OpBuilder.addArrowStarOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7829 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 7830 | |
| 7831 | case OO_Conditional: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7832 | OpBuilder.addConditionalOperatorOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7833 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7834 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7835 | } |
| 7836 | } |
| 7837 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7838 | /// \brief Add function candidates found via argument-dependent lookup |
| 7839 | /// to the set of overloading candidates. |
| 7840 | /// |
| 7841 | /// This routine performs argument-dependent name lookup based on the |
| 7842 | /// given function name (which may also be an operator name) and adds |
| 7843 | /// all of the overload candidates found by ADL to the overload |
| 7844 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7845 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7846 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
Richard Smith | f5cd5cc | 2012-02-25 06:24:24 +0000 | [diff] [blame] | 7847 | bool Operator, SourceLocation Loc, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 7848 | ArrayRef<Expr *> Args, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 7849 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7850 | OverloadCandidateSet& CandidateSet, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 7851 | bool PartialOverloading) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7852 | ADLResult Fns; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7853 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 7854 | // FIXME: This approach for uniquing ADL results (and removing |
| 7855 | // redundant candidates from the set) relies on pointer-equality, |
| 7856 | // which means we need to key off the canonical decl. However, |
| 7857 | // always going back to the canonical decl might not get us the |
| 7858 | // right set of default arguments. What default arguments are |
| 7859 | // we supposed to consider on ADL candidates, anyway? |
| 7860 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7861 | // FIXME: Pass in the explicit template arguments? |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 7862 | ArgumentDependentLookup(Name, Operator, Loc, Args, Fns); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7863 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7864 | // Erase all of the candidates we already knew about. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7865 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 7866 | CandEnd = CandidateSet.end(); |
| 7867 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7868 | if (Cand->Function) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7869 | Fns.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7870 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7871 | Fns.erase(FunTmpl); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7872 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7873 | |
| 7874 | // For each of the ADL candidates we found, add it to the overload |
| 7875 | // set. |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7876 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7877 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7878 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7879 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7880 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7881 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7882 | AddOverloadCandidate(FD, FoundDecl, Args, CandidateSet, false, |
| 7883 | PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7884 | } else |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7885 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7886 | FoundDecl, ExplicitTemplateArgs, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7887 | Args, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7888 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7889 | } |
| 7890 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7891 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 7892 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7893 | bool |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7894 | isBetterOverloadCandidate(Sema &S, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 7895 | const OverloadCandidate &Cand1, |
| 7896 | const OverloadCandidate &Cand2, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7897 | SourceLocation Loc, |
| 7898 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7899 | // Define viable functions to be better candidates than non-viable |
| 7900 | // functions. |
| 7901 | if (!Cand2.Viable) |
| 7902 | return Cand1.Viable; |
| 7903 | else if (!Cand1.Viable) |
| 7904 | return false; |
| 7905 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7906 | // C++ [over.match.best]p1: |
| 7907 | // |
| 7908 | // -- if F is a static member function, ICS1(F) is defined such |
| 7909 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 7910 | // any function G, and, symmetrically, ICS1(G) is neither |
| 7911 | // better nor worse than ICS1(F). |
| 7912 | unsigned StartArg = 0; |
| 7913 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 7914 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7915 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7916 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7917 | // A viable function F1 is defined to be a better function than another |
| 7918 | // 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] | 7919 | // conversion sequence than ICSi(F2), and then... |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 7920 | unsigned NumArgs = Cand1.NumConversions; |
| 7921 | assert(Cand2.NumConversions == NumArgs && "Overload candidate mismatch"); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7922 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7923 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7924 | switch (CompareImplicitConversionSequences(S, |
| 7925 | Cand1.Conversions[ArgIdx], |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7926 | Cand2.Conversions[ArgIdx])) { |
| 7927 | case ImplicitConversionSequence::Better: |
| 7928 | // Cand1 has a better conversion sequence. |
| 7929 | HasBetterConversion = true; |
| 7930 | break; |
| 7931 | |
| 7932 | case ImplicitConversionSequence::Worse: |
| 7933 | // Cand1 can't be better than Cand2. |
| 7934 | return false; |
| 7935 | |
| 7936 | case ImplicitConversionSequence::Indistinguishable: |
| 7937 | // Do nothing. |
| 7938 | break; |
| 7939 | } |
| 7940 | } |
| 7941 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7942 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7943 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7944 | if (HasBetterConversion) |
| 7945 | return true; |
| 7946 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7947 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7948 | // specialization, or, if not that, |
Douglas Gregor | ccd4713 | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 7949 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7950 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 7951 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7952 | |
| 7953 | // -- F1 and F2 are function template specializations, and the function |
| 7954 | // template for F1 is more specialized than the template for F2 |
| 7955 | // 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] | 7956 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 7957 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7958 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7959 | if (FunctionTemplateDecl *BetterTemplate |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7960 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 7961 | Cand2.Function->getPrimaryTemplate(), |
| 7962 | Loc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7963 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 7964 | : TPOC_Call, |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7965 | Cand1.ExplicitCallArguments)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7966 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7967 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7968 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7969 | // -- the context is an initialization by user-defined conversion |
| 7970 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 7971 | // from the return type of F1 to the destination type (i.e., |
| 7972 | // the type of the entity being initialized) is a better |
| 7973 | // conversion sequence than the standard conversion sequence |
| 7974 | // from the return type of F2 to the destination type. |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7975 | if (UserDefinedConversion && Cand1.Function && Cand2.Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7976 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7977 | isa<CXXConversionDecl>(Cand2.Function)) { |
Douglas Gregor | b734e24 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 7978 | // First check whether we prefer one of the conversion functions over the |
| 7979 | // other. This only distinguishes the results in non-standard, extension |
| 7980 | // cases such as the conversion from a lambda closure type to a function |
| 7981 | // pointer or block. |
| 7982 | ImplicitConversionSequence::CompareKind FuncResult |
| 7983 | = compareConversionFunctions(S, Cand1.Function, Cand2.Function); |
| 7984 | if (FuncResult != ImplicitConversionSequence::Indistinguishable) |
| 7985 | return FuncResult; |
| 7986 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7987 | switch (CompareStandardConversionSequences(S, |
| 7988 | Cand1.FinalConversion, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7989 | Cand2.FinalConversion)) { |
| 7990 | case ImplicitConversionSequence::Better: |
| 7991 | // Cand1 has a better conversion sequence. |
| 7992 | return true; |
| 7993 | |
| 7994 | case ImplicitConversionSequence::Worse: |
| 7995 | // Cand1 can't be better than Cand2. |
| 7996 | return false; |
| 7997 | |
| 7998 | case ImplicitConversionSequence::Indistinguishable: |
| 7999 | // Do nothing |
| 8000 | break; |
| 8001 | } |
| 8002 | } |
| 8003 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8004 | return false; |
| 8005 | } |
| 8006 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8007 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8008 | /// within an overload candidate set. |
| 8009 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 8010 | /// \param Loc The location of the function name (or operator symbol) for |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8011 | /// which overload resolution occurs. |
| 8012 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 8013 | /// \param Best If overload resolution was successful or found a deleted |
| 8014 | /// function, \p Best points to the candidate function found. |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8015 | /// |
| 8016 | /// \returns The result of overload resolution. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8017 | OverloadingResult |
| 8018 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 8019 | iterator &Best, |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8020 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8021 | // Find the best viable function. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8022 | Best = end(); |
| 8023 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
| 8024 | if (Cand->Viable) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8025 | if (Best == end() || isBetterOverloadCandidate(S, *Cand, *Best, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8026 | UserDefinedConversion)) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8027 | Best = Cand; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8028 | } |
| 8029 | |
| 8030 | // If we didn't find any viable functions, abort. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8031 | if (Best == end()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8032 | return OR_No_Viable_Function; |
| 8033 | |
| 8034 | // Make sure that this function is better than every other viable |
| 8035 | // function. If not, we have an ambiguity. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8036 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8037 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8038 | Cand != Best && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8039 | !isBetterOverloadCandidate(S, *Best, *Cand, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 8040 | UserDefinedConversion)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8041 | Best = end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8042 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8043 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8044 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8045 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8046 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8047 | if (Best->Function && |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 8048 | (Best->Function->isDeleted() || |
| 8049 | S.isFunctionConsideredUnavailable(Best->Function))) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8050 | return OR_Deleted; |
| 8051 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8052 | return OR_Success; |
| 8053 | } |
| 8054 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8055 | namespace { |
| 8056 | |
| 8057 | enum OverloadCandidateKind { |
| 8058 | oc_function, |
| 8059 | oc_method, |
| 8060 | oc_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8061 | oc_function_template, |
| 8062 | oc_method_template, |
| 8063 | oc_constructor_template, |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8064 | oc_implicit_default_constructor, |
| 8065 | oc_implicit_copy_constructor, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8066 | oc_implicit_move_constructor, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8067 | oc_implicit_copy_assignment, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8068 | oc_implicit_move_assignment, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8069 | oc_implicit_inherited_constructor |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8070 | }; |
| 8071 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8072 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 8073 | FunctionDecl *Fn, |
| 8074 | std::string &Description) { |
| 8075 | bool isTemplate = false; |
| 8076 | |
| 8077 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 8078 | isTemplate = true; |
| 8079 | Description = S.getTemplateArgumentBindingsText( |
| 8080 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 8081 | } |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8082 | |
| 8083 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8084 | if (!Ctor->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8085 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8086 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8087 | if (Ctor->getInheritedConstructor()) |
| 8088 | return oc_implicit_inherited_constructor; |
| 8089 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8090 | if (Ctor->isDefaultConstructor()) |
| 8091 | return oc_implicit_default_constructor; |
| 8092 | |
| 8093 | if (Ctor->isMoveConstructor()) |
| 8094 | return oc_implicit_move_constructor; |
| 8095 | |
| 8096 | assert(Ctor->isCopyConstructor() && |
| 8097 | "unexpected sort of implicit constructor"); |
| 8098 | return oc_implicit_copy_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8099 | } |
| 8100 | |
| 8101 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 8102 | // This actually gets spelled 'candidate function' for now, but |
| 8103 | // it doesn't hurt to split it out. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8104 | if (!Meth->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8105 | return isTemplate ? oc_method_template : oc_method; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8106 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8107 | if (Meth->isMoveAssignmentOperator()) |
| 8108 | return oc_implicit_move_assignment; |
| 8109 | |
Douglas Gregor | ef7d78b | 2012-02-10 08:36:38 +0000 | [diff] [blame] | 8110 | if (Meth->isCopyAssignmentOperator()) |
| 8111 | return oc_implicit_copy_assignment; |
| 8112 | |
| 8113 | assert(isa<CXXConversionDecl>(Meth) && "expected conversion"); |
| 8114 | return oc_method; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8115 | } |
| 8116 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8117 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8118 | } |
| 8119 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8120 | void MaybeEmitInheritedConstructorNote(Sema &S, Decl *Fn) { |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8121 | const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn); |
| 8122 | if (!Ctor) return; |
| 8123 | |
| 8124 | Ctor = Ctor->getInheritedConstructor(); |
| 8125 | if (!Ctor) return; |
| 8126 | |
| 8127 | S.Diag(Ctor->getLocation(), diag::note_ovl_candidate_inherited_constructor); |
| 8128 | } |
| 8129 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8130 | } // end anonymous namespace |
| 8131 | |
| 8132 | // Notes the location of an overload candidate. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8133 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn, QualType DestType) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8134 | std::string FnDesc; |
| 8135 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8136 | PartialDiagnostic PD = PDiag(diag::note_ovl_candidate) |
| 8137 | << (unsigned) K << FnDesc; |
| 8138 | HandleFunctionTypeMismatch(PD, Fn->getType(), DestType); |
| 8139 | Diag(Fn->getLocation(), PD); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8140 | MaybeEmitInheritedConstructorNote(*this, Fn); |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 8141 | } |
| 8142 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8143 | //Notes the location of all overload candidates designated through |
| 8144 | // OverloadedExpr |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8145 | void Sema::NoteAllOverloadCandidates(Expr* OverloadedExpr, QualType DestType) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8146 | assert(OverloadedExpr->getType() == Context.OverloadTy); |
| 8147 | |
| 8148 | OverloadExpr::FindResult Ovl = OverloadExpr::find(OverloadedExpr); |
| 8149 | OverloadExpr *OvlExpr = Ovl.Expression; |
| 8150 | |
| 8151 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 8152 | IEnd = OvlExpr->decls_end(); |
| 8153 | I != IEnd; ++I) { |
| 8154 | if (FunctionTemplateDecl *FunTmpl = |
| 8155 | dyn_cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8156 | NoteOverloadCandidate(FunTmpl->getTemplatedDecl(), DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8157 | } else if (FunctionDecl *Fun |
| 8158 | = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8159 | NoteOverloadCandidate(Fun, DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8160 | } |
| 8161 | } |
| 8162 | } |
| 8163 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8164 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 8165 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 8166 | /// target types of the conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8167 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
| 8168 | Sema &S, |
| 8169 | SourceLocation CaretLoc, |
| 8170 | const PartialDiagnostic &PDiag) const { |
| 8171 | S.Diag(CaretLoc, PDiag) |
| 8172 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
Matt Beaumont-Gay | 45a37da | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 8173 | // FIXME: The note limiting machinery is borrowed from |
| 8174 | // OverloadCandidateSet::NoteCandidates; there's an opportunity for |
| 8175 | // refactoring here. |
| 8176 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
| 8177 | unsigned CandsShown = 0; |
| 8178 | AmbiguousConversionSequence::const_iterator I, E; |
| 8179 | for (I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
| 8180 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
| 8181 | break; |
| 8182 | ++CandsShown; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8183 | S.NoteOverloadCandidate(*I); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8184 | } |
Matt Beaumont-Gay | 45a37da | 2012-11-08 20:50:02 +0000 | [diff] [blame] | 8185 | if (I != E) |
| 8186 | S.Diag(SourceLocation(), diag::note_ovl_too_many_candidates) << int(E - I); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8187 | } |
| 8188 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8189 | namespace { |
| 8190 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8191 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 8192 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 8193 | assert(Conv.isBad()); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8194 | assert(Cand->Function && "for now, candidate must be a function"); |
| 8195 | FunctionDecl *Fn = Cand->Function; |
| 8196 | |
| 8197 | // There's a conversion slot for the object argument if this is a |
| 8198 | // non-constructor method. Note that 'I' corresponds the |
| 8199 | // conversion-slot index. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8200 | bool isObjectArgument = false; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8201 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8202 | if (I == 0) |
| 8203 | isObjectArgument = true; |
| 8204 | else |
| 8205 | I--; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8206 | } |
| 8207 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8208 | std::string FnDesc; |
| 8209 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 8210 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8211 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 8212 | QualType FromTy = Conv.Bad.getFromType(); |
| 8213 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8214 | |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8215 | if (FromTy == S.Context.OverloadTy) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8216 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8217 | Expr *E = FromExpr->IgnoreParens(); |
| 8218 | if (isa<UnaryOperator>(E)) |
| 8219 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 8220 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8221 | |
| 8222 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 8223 | << (unsigned) FnKind << FnDesc |
| 8224 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8225 | << ToTy << Name << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8226 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8227 | return; |
| 8228 | } |
| 8229 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8230 | // Do some hand-waving analysis to see if the non-viability is due |
| 8231 | // to a qualifier mismatch. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8232 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 8233 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 8234 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 8235 | CToTy = RT->getPointeeType(); |
| 8236 | else { |
| 8237 | // TODO: detect and diagnose the full richness of const mismatches. |
| 8238 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 8239 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 8240 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 8241 | } |
| 8242 | |
| 8243 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 8244 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8245 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8246 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8247 | |
| 8248 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 8249 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 8250 | << (unsigned) FnKind << FnDesc |
| 8251 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8252 | << FromTy |
| 8253 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 8254 | << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8255 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8256 | return; |
| 8257 | } |
| 8258 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8259 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 8260 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8261 | << (unsigned) FnKind << FnDesc |
| 8262 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8263 | << FromTy |
| 8264 | << FromQs.getObjCLifetime() << ToQs.getObjCLifetime() |
| 8265 | << (unsigned) isObjectArgument << I+1; |
| 8266 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8267 | return; |
| 8268 | } |
| 8269 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 8270 | if (FromQs.getObjCGCAttr() != ToQs.getObjCGCAttr()) { |
| 8271 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_gc) |
| 8272 | << (unsigned) FnKind << FnDesc |
| 8273 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8274 | << FromTy |
| 8275 | << FromQs.getObjCGCAttr() << ToQs.getObjCGCAttr() |
| 8276 | << (unsigned) isObjectArgument << I+1; |
| 8277 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8278 | return; |
| 8279 | } |
| 8280 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8281 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 8282 | assert(CVR && "unexpected qualifiers mismatch"); |
| 8283 | |
| 8284 | if (isObjectArgument) { |
| 8285 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 8286 | << (unsigned) FnKind << FnDesc |
| 8287 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8288 | << FromTy << (CVR - 1); |
| 8289 | } else { |
| 8290 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 8291 | << (unsigned) FnKind << FnDesc |
| 8292 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8293 | << FromTy << (CVR - 1) << I+1; |
| 8294 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8295 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8296 | return; |
| 8297 | } |
| 8298 | |
Sebastian Redl | fd2a00a | 2011-09-24 17:48:32 +0000 | [diff] [blame] | 8299 | // Special diagnostic for failure to convert an initializer list, since |
| 8300 | // telling the user that it has type void is not useful. |
| 8301 | if (FromExpr && isa<InitListExpr>(FromExpr)) { |
| 8302 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_list_argument) |
| 8303 | << (unsigned) FnKind << FnDesc |
| 8304 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8305 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8306 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8307 | return; |
| 8308 | } |
| 8309 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8310 | // Diagnose references or pointers to incomplete types differently, |
| 8311 | // since it's far from impossible that the incompleteness triggered |
| 8312 | // the failure. |
| 8313 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 8314 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 8315 | TempFromTy = PTy->getPointeeType(); |
| 8316 | if (TempFromTy->isIncompleteType()) { |
| 8317 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 8318 | << (unsigned) FnKind << FnDesc |
| 8319 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8320 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8321 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8322 | return; |
| 8323 | } |
| 8324 | |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8325 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8326 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8327 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 8328 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 8329 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8330 | FromPtrTy->getPointeeType()) && |
| 8331 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 8332 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8333 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8334 | FromPtrTy->getPointeeType())) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8335 | BaseToDerivedConversion = 1; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8336 | } |
| 8337 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 8338 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 8339 | if (const ObjCObjectPointerType *ToPtrTy |
| 8340 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 8341 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 8342 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 8343 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8344 | FromPtrTy->getPointeeType()) && |
| 8345 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8346 | BaseToDerivedConversion = 2; |
| 8347 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8348 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 8349 | !FromTy->isIncompleteType() && |
| 8350 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 8351 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) { |
| 8352 | BaseToDerivedConversion = 3; |
| 8353 | } else if (ToTy->isLValueReferenceType() && !FromExpr->isLValue() && |
| 8354 | ToTy.getNonReferenceType().getCanonicalType() == |
| 8355 | FromTy.getNonReferenceType().getCanonicalType()) { |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8356 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_lvalue) |
| 8357 | << (unsigned) FnKind << FnDesc |
| 8358 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8359 | << (unsigned) isObjectArgument << I + 1; |
| 8360 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8361 | return; |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8362 | } |
Kaelyn Uhrain | 0d3317e | 2012-06-19 00:37:47 +0000 | [diff] [blame] | 8363 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8364 | |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8365 | if (BaseToDerivedConversion) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8366 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8367 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8368 | << (unsigned) FnKind << FnDesc |
| 8369 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8370 | << (BaseToDerivedConversion - 1) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8371 | << FromTy << ToTy << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8372 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8373 | return; |
| 8374 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8375 | |
Fariborz Jahanian | 909bcb3 | 2011-07-20 17:14:09 +0000 | [diff] [blame] | 8376 | if (isa<ObjCObjectPointerType>(CFromTy) && |
| 8377 | isa<PointerType>(CToTy)) { |
| 8378 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8379 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8380 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
| 8381 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_arc_conv) |
| 8382 | << (unsigned) FnKind << FnDesc |
| 8383 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8384 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8385 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8386 | return; |
| 8387 | } |
| 8388 | } |
| 8389 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8390 | // Emit the generic diagnostic and, optionally, add the hints to it. |
| 8391 | PartialDiagnostic FDiag = S.PDiag(diag::note_ovl_candidate_bad_conv); |
| 8392 | FDiag << (unsigned) FnKind << FnDesc |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8393 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8394 | << FromTy << ToTy << (unsigned) isObjectArgument << I + 1 |
| 8395 | << (unsigned) (Cand->Fix.Kind); |
| 8396 | |
| 8397 | // If we can fix the conversion, suggest the FixIts. |
Benjamin Kramer | 1136ef0 | 2012-01-14 21:05:10 +0000 | [diff] [blame] | 8398 | for (std::vector<FixItHint>::iterator HI = Cand->Fix.Hints.begin(), |
| 8399 | HE = Cand->Fix.Hints.end(); HI != HE; ++HI) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8400 | FDiag << *HI; |
| 8401 | S.Diag(Fn->getLocation(), FDiag); |
| 8402 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8403 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8404 | } |
| 8405 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8406 | /// Additional arity mismatch diagnosis specific to a function overload |
| 8407 | /// candidates. This is not covered by the more general DiagnoseArityMismatch() |
| 8408 | /// over a candidate in any candidate set. |
| 8409 | bool CheckArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 8410 | unsigned NumArgs) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8411 | FunctionDecl *Fn = Cand->Function; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8412 | unsigned MinParams = Fn->getMinRequiredArguments(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8413 | |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8414 | // With invalid overloaded operators, it's possible that we think we |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8415 | // have an arity mismatch when in fact it looks like we have the |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8416 | // right number of arguments, because only overloaded operators have |
| 8417 | // the weird behavior of overloading member and non-member functions. |
| 8418 | // Just don't report anything. |
| 8419 | if (Fn->isInvalidDecl() && |
| 8420 | Fn->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8421 | return true; |
| 8422 | |
| 8423 | if (NumArgs < MinParams) { |
| 8424 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 8425 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8426 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
| 8427 | } else { |
| 8428 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 8429 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8430 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
| 8431 | } |
| 8432 | |
| 8433 | return false; |
| 8434 | } |
| 8435 | |
| 8436 | /// General arity mismatch diagnosis over a candidate in a candidate set. |
| 8437 | void DiagnoseArityMismatch(Sema &S, Decl *D, unsigned NumFormalArgs) { |
| 8438 | assert(isa<FunctionDecl>(D) && |
| 8439 | "The templated declaration should at least be a function" |
| 8440 | " when diagnosing bad template argument deduction due to too many" |
| 8441 | " or too few arguments"); |
| 8442 | |
| 8443 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| 8444 | |
| 8445 | // TODO: treat calls to a missing default constructor as a special case |
| 8446 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 8447 | unsigned MinParams = Fn->getMinRequiredArguments(); |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8448 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8449 | // at least / at most / exactly |
| 8450 | unsigned mode, modeCount; |
| 8451 | if (NumFormalArgs < MinParams) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8452 | if (MinParams != FnTy->getNumArgs() || |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 8453 | FnTy->isVariadic() || FnTy->isTemplateVariadic()) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8454 | mode = 0; // "at least" |
| 8455 | else |
| 8456 | mode = 2; // "exactly" |
| 8457 | modeCount = MinParams; |
| 8458 | } else { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8459 | if (MinParams != FnTy->getNumArgs()) |
| 8460 | mode = 1; // "at most" |
| 8461 | else |
| 8462 | mode = 2; // "exactly" |
| 8463 | modeCount = FnTy->getNumArgs(); |
| 8464 | } |
| 8465 | |
| 8466 | std::string Description; |
| 8467 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 8468 | |
Richard Smith | f7b8056 | 2012-05-11 05:16:41 +0000 | [diff] [blame] | 8469 | if (modeCount == 1 && Fn->getParamDecl(0)->getDeclName()) |
| 8470 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity_one) |
| 8471 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 8472 | << Fn->getParamDecl(0) << NumFormalArgs; |
| 8473 | else |
| 8474 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
| 8475 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 8476 | << modeCount << NumFormalArgs; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8477 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8478 | } |
| 8479 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8480 | /// Arity mismatch diagnosis specific to a function overload candidate. |
| 8481 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 8482 | unsigned NumFormalArgs) { |
| 8483 | if (!CheckArityMismatch(S, Cand, NumFormalArgs)) |
| 8484 | DiagnoseArityMismatch(S, Cand->Function, NumFormalArgs); |
| 8485 | } |
Larisse Voufo | 8c5d407 | 2013-07-19 22:53:23 +0000 | [diff] [blame] | 8486 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8487 | TemplateDecl *getDescribedTemplate(Decl *Templated) { |
| 8488 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Templated)) |
| 8489 | return FD->getDescribedFunctionTemplate(); |
| 8490 | else if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Templated)) |
| 8491 | return RD->getDescribedClassTemplate(); |
| 8492 | |
| 8493 | llvm_unreachable("Unsupported: Getting the described template declaration" |
| 8494 | " for bad deduction diagnosis"); |
| 8495 | } |
| 8496 | |
| 8497 | /// Diagnose a failed template-argument deduction. |
| 8498 | void DiagnoseBadDeduction(Sema &S, Decl *Templated, |
| 8499 | DeductionFailureInfo &DeductionFailure, |
| 8500 | unsigned NumArgs) { |
| 8501 | TemplateParameter Param = DeductionFailure.getTemplateParameter(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8502 | NamedDecl *ParamD; |
| 8503 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 8504 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 8505 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8506 | switch (DeductionFailure.Result) { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8507 | case Sema::TDK_Success: |
| 8508 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 8509 | |
| 8510 | case Sema::TDK_Incomplete: { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8511 | assert(ParamD && "no parameter found for incomplete deduction result"); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8512 | S.Diag(Templated->getLocation(), |
| 8513 | diag::note_ovl_candidate_incomplete_deduction) |
| 8514 | << ParamD->getDeclName(); |
| 8515 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8516 | return; |
| 8517 | } |
| 8518 | |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8519 | case Sema::TDK_Underqualified: { |
| 8520 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 8521 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 8522 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8523 | QualType Param = DeductionFailure.getFirstArg()->getAsType(); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8524 | |
| 8525 | // Param will have been canonicalized, but it should just be a |
| 8526 | // qualified version of ParamD, so move the qualifiers to that. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 8527 | QualifierCollector Qs; |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8528 | Qs.strip(Param); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 8529 | QualType NonCanonParam = Qs.apply(S.Context, TParam->getTypeForDecl()); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8530 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 8531 | |
| 8532 | // Arg has also been canonicalized, but there's nothing we can do |
| 8533 | // about that. It also doesn't matter as much, because it won't |
| 8534 | // have any template parameters in it (because deduction isn't |
| 8535 | // done on dependent types). |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8536 | QualType Arg = DeductionFailure.getSecondArg()->getAsType(); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8537 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8538 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_underqualified) |
| 8539 | << ParamD->getDeclName() << Arg << NonCanonParam; |
| 8540 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8541 | return; |
| 8542 | } |
| 8543 | |
| 8544 | case Sema::TDK_Inconsistent: { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8545 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8546 | int which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8547 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8548 | which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8549 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8550 | which = 1; |
| 8551 | else { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8552 | which = 2; |
| 8553 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8554 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8555 | S.Diag(Templated->getLocation(), |
| 8556 | diag::note_ovl_candidate_inconsistent_deduction) |
| 8557 | << which << ParamD->getDeclName() << *DeductionFailure.getFirstArg() |
| 8558 | << *DeductionFailure.getSecondArg(); |
| 8559 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8560 | return; |
| 8561 | } |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8562 | |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8563 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8564 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8565 | if (ParamD->getDeclName()) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8566 | S.Diag(Templated->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8567 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8568 | << ParamD->getDeclName(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8569 | else { |
| 8570 | int index = 0; |
| 8571 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 8572 | index = TTP->getIndex(); |
| 8573 | else if (NonTypeTemplateParmDecl *NTTP |
| 8574 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 8575 | index = NTTP->getIndex(); |
| 8576 | else |
| 8577 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8578 | S.Diag(Templated->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8579 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8580 | << (index + 1); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8581 | } |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8582 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8583 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8584 | |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8585 | case Sema::TDK_TooManyArguments: |
| 8586 | case Sema::TDK_TooFewArguments: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8587 | DiagnoseArityMismatch(S, Templated, NumArgs); |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8588 | return; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8589 | |
| 8590 | case Sema::TDK_InstantiationDepth: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8591 | S.Diag(Templated->getLocation(), |
| 8592 | diag::note_ovl_candidate_instantiation_depth); |
| 8593 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8594 | return; |
| 8595 | |
| 8596 | case Sema::TDK_SubstitutionFailure: { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8597 | // Format the template argument list into the argument string. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8598 | SmallString<128> TemplateArgString; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8599 | if (TemplateArgumentList *Args = |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8600 | DeductionFailure.getTemplateArgumentList()) { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8601 | TemplateArgString = " "; |
| 8602 | TemplateArgString += S.getTemplateArgumentBindingsText( |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8603 | getDescribedTemplate(Templated)->getTemplateParameters(), *Args); |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8604 | } |
| 8605 | |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8606 | // If this candidate was disabled by enable_if, say so. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8607 | PartialDiagnosticAt *PDiag = DeductionFailure.getSFINAEDiagnostic(); |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8608 | if (PDiag && PDiag->second.getDiagID() == |
| 8609 | diag::err_typename_nested_not_found_enable_if) { |
| 8610 | // FIXME: Use the source range of the condition, and the fully-qualified |
| 8611 | // name of the enable_if template. These are both present in PDiag. |
| 8612 | S.Diag(PDiag->first, diag::note_ovl_candidate_disabled_by_enable_if) |
| 8613 | << "'enable_if'" << TemplateArgString; |
| 8614 | return; |
| 8615 | } |
| 8616 | |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8617 | // Format the SFINAE diagnostic into the argument string. |
| 8618 | // FIXME: Add a general mechanism to include a PartialDiagnostic *'s |
| 8619 | // formatted message in another diagnostic. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8620 | SmallString<128> SFINAEArgString; |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8621 | SourceRange R; |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8622 | if (PDiag) { |
Richard Smith | b8590f3 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8623 | SFINAEArgString = ": "; |
| 8624 | R = SourceRange(PDiag->first, PDiag->first); |
| 8625 | PDiag->second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 8626 | } |
| 8627 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8628 | S.Diag(Templated->getLocation(), |
| 8629 | diag::note_ovl_candidate_substitution_failure) |
| 8630 | << TemplateArgString << SFINAEArgString << R; |
| 8631 | MaybeEmitInheritedConstructorNote(S, Templated); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8632 | return; |
| 8633 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8634 | |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8635 | case Sema::TDK_FailedOverloadResolution: { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8636 | OverloadExpr::FindResult R = OverloadExpr::find(DeductionFailure.getExpr()); |
| 8637 | S.Diag(Templated->getLocation(), |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8638 | diag::note_ovl_candidate_failed_overload_resolution) |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8639 | << R.Expression->getName(); |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 8640 | return; |
| 8641 | } |
| 8642 | |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8643 | case Sema::TDK_NonDeducedMismatch: { |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8644 | // FIXME: Provide a source location to indicate what we couldn't match. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8645 | TemplateArgument FirstTA = *DeductionFailure.getFirstArg(); |
| 8646 | TemplateArgument SecondTA = *DeductionFailure.getSecondArg(); |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8647 | if (FirstTA.getKind() == TemplateArgument::Template && |
| 8648 | SecondTA.getKind() == TemplateArgument::Template) { |
| 8649 | TemplateName FirstTN = FirstTA.getAsTemplate(); |
| 8650 | TemplateName SecondTN = SecondTA.getAsTemplate(); |
| 8651 | if (FirstTN.getKind() == TemplateName::Template && |
| 8652 | SecondTN.getKind() == TemplateName::Template) { |
| 8653 | if (FirstTN.getAsTemplateDecl()->getName() == |
| 8654 | SecondTN.getAsTemplateDecl()->getName()) { |
| 8655 | // FIXME: This fixes a bad diagnostic where both templates are named |
| 8656 | // the same. This particular case is a bit difficult since: |
| 8657 | // 1) It is passed as a string to the diagnostic printer. |
| 8658 | // 2) The diagnostic printer only attempts to find a better |
| 8659 | // name for types, not decls. |
| 8660 | // Ideally, this should folded into the diagnostic printer. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8661 | S.Diag(Templated->getLocation(), |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8662 | diag::note_ovl_candidate_non_deduced_mismatch_qualified) |
| 8663 | << FirstTN.getAsTemplateDecl() << SecondTN.getAsTemplateDecl(); |
| 8664 | return; |
| 8665 | } |
| 8666 | } |
| 8667 | } |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8668 | S.Diag(Templated->getLocation(), |
| 8669 | diag::note_ovl_candidate_non_deduced_mismatch) |
| 8670 | << FirstTA << SecondTA; |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8671 | return; |
Richard Trieu | eef35f8 | 2013-04-08 21:11:40 +0000 | [diff] [blame] | 8672 | } |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8673 | // TODO: diagnose these individually, then kill off |
| 8674 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8675 | case Sema::TDK_MiscellaneousDeductionFailure: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8676 | S.Diag(Templated->getLocation(), diag::note_ovl_candidate_bad_deduction); |
| 8677 | MaybeEmitInheritedConstructorNote(S, Templated); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8678 | return; |
| 8679 | } |
| 8680 | } |
| 8681 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8682 | /// Diagnose a failed template-argument deduction, for function calls. |
| 8683 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, unsigned NumArgs) { |
| 8684 | unsigned TDK = Cand->DeductionFailure.Result; |
| 8685 | if (TDK == Sema::TDK_TooFewArguments || TDK == Sema::TDK_TooManyArguments) { |
| 8686 | if (CheckArityMismatch(S, Cand, NumArgs)) |
| 8687 | return; |
| 8688 | } |
| 8689 | DiagnoseBadDeduction(S, Cand->Function, // pattern |
| 8690 | Cand->DeductionFailure, NumArgs); |
| 8691 | } |
| 8692 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8693 | /// CUDA: diagnose an invalid call across targets. |
| 8694 | void DiagnoseBadTarget(Sema &S, OverloadCandidate *Cand) { |
| 8695 | FunctionDecl *Caller = cast<FunctionDecl>(S.CurContext); |
| 8696 | FunctionDecl *Callee = Cand->Function; |
| 8697 | |
| 8698 | Sema::CUDAFunctionTarget CallerTarget = S.IdentifyCUDATarget(Caller), |
| 8699 | CalleeTarget = S.IdentifyCUDATarget(Callee); |
| 8700 | |
| 8701 | std::string FnDesc; |
| 8702 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Callee, FnDesc); |
| 8703 | |
| 8704 | S.Diag(Callee->getLocation(), diag::note_ovl_candidate_bad_target) |
| 8705 | << (unsigned) FnKind << CalleeTarget << CallerTarget; |
| 8706 | } |
| 8707 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8708 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 8709 | /// already generated a primary error at the call site. |
| 8710 | /// |
| 8711 | /// It really does need to be a single diagnostic with its caret |
| 8712 | /// pointed at the candidate declaration. Yes, this creates some |
| 8713 | /// major challenges of technical writing. Yes, this makes pointing |
| 8714 | /// out problems with specific arguments quite awkward. It's still |
| 8715 | /// better than generating twenty screens of text for every failed |
| 8716 | /// overload. |
| 8717 | /// |
| 8718 | /// It would be great to be able to express per-candidate problems |
| 8719 | /// more richly for those diagnostic clients that cared, but we'd |
| 8720 | /// still have to be just as careful with the default diagnostics. |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8721 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8722 | unsigned NumArgs) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8723 | FunctionDecl *Fn = Cand->Function; |
| 8724 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8725 | // Note deleted candidates, but only if they're viable. |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 8726 | if (Cand->Viable && (Fn->isDeleted() || |
| 8727 | S.isFunctionConsideredUnavailable(Fn))) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8728 | std::string FnDesc; |
| 8729 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8730 | |
| 8731 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 8732 | << FnKind << FnDesc |
| 8733 | << (Fn->isDeleted() ? (Fn->isDeletedAsWritten() ? 1 : 2) : 0); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8734 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8735 | return; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8736 | } |
| 8737 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8738 | // We don't really have anything else to say about viable candidates. |
| 8739 | if (Cand->Viable) { |
| 8740 | S.NoteOverloadCandidate(Fn); |
| 8741 | return; |
| 8742 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8743 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8744 | switch (Cand->FailureKind) { |
| 8745 | case ovl_fail_too_many_arguments: |
| 8746 | case ovl_fail_too_few_arguments: |
| 8747 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8748 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8749 | case ovl_fail_bad_deduction: |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8750 | return DiagnoseBadDeduction(S, Cand, NumArgs); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8751 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8752 | case ovl_fail_trivial_conversion: |
| 8753 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 8754 | case ovl_fail_final_conversion_not_exact: |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8755 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8756 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8757 | case ovl_fail_bad_conversion: { |
| 8758 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8759 | for (unsigned N = Cand->NumConversions; I != N; ++I) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8760 | if (Cand->Conversions[I].isBad()) |
| 8761 | return DiagnoseBadConversion(S, Cand, I); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8762 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8763 | // FIXME: this currently happens when we're called from SemaInit |
| 8764 | // when user-conversion overload fails. Figure out how to handle |
| 8765 | // those conditions and diagnose them well. |
| 8766 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8767 | } |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8768 | |
| 8769 | case ovl_fail_bad_target: |
| 8770 | return DiagnoseBadTarget(S, Cand); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8771 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8772 | } |
| 8773 | |
| 8774 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 8775 | // Desugar the type of the surrogate down to a function type, |
| 8776 | // retaining as many typedefs as possible while still showing |
| 8777 | // the function type (and, therefore, its parameter types). |
| 8778 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 8779 | bool isLValueReference = false; |
| 8780 | bool isRValueReference = false; |
| 8781 | bool isPointer = false; |
| 8782 | if (const LValueReferenceType *FnTypeRef = |
| 8783 | FnType->getAs<LValueReferenceType>()) { |
| 8784 | FnType = FnTypeRef->getPointeeType(); |
| 8785 | isLValueReference = true; |
| 8786 | } else if (const RValueReferenceType *FnTypeRef = |
| 8787 | FnType->getAs<RValueReferenceType>()) { |
| 8788 | FnType = FnTypeRef->getPointeeType(); |
| 8789 | isRValueReference = true; |
| 8790 | } |
| 8791 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 8792 | FnType = FnTypePtr->getPointeeType(); |
| 8793 | isPointer = true; |
| 8794 | } |
| 8795 | // Desugar down to a function type. |
| 8796 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 8797 | // Reconstruct the pointer/reference as appropriate. |
| 8798 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 8799 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 8800 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 8801 | |
| 8802 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 8803 | << FnType; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8804 | MaybeEmitInheritedConstructorNote(S, Cand->Surrogate); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8805 | } |
| 8806 | |
| 8807 | void NoteBuiltinOperatorCandidate(Sema &S, |
David Blaikie | 0bea863 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 8808 | StringRef Opc, |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8809 | SourceLocation OpLoc, |
| 8810 | OverloadCandidate *Cand) { |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8811 | assert(Cand->NumConversions <= 2 && "builtin operator is not binary"); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8812 | std::string TypeStr("operator"); |
| 8813 | TypeStr += Opc; |
| 8814 | TypeStr += "("; |
| 8815 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8816 | if (Cand->NumConversions == 1) { |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8817 | TypeStr += ")"; |
| 8818 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 8819 | } else { |
| 8820 | TypeStr += ", "; |
| 8821 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 8822 | TypeStr += ")"; |
| 8823 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 8824 | } |
| 8825 | } |
| 8826 | |
| 8827 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 8828 | OverloadCandidate *Cand) { |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8829 | unsigned NoOperands = Cand->NumConversions; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8830 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 8831 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8832 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 8833 | if (!ICS.isAmbiguous()) continue; |
| 8834 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8835 | ICS.DiagnoseAmbiguousConversion(S, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8836 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8837 | } |
| 8838 | } |
| 8839 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8840 | static SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8841 | if (Cand->Function) |
| 8842 | return Cand->Function->getLocation(); |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 8843 | if (Cand->IsSurrogate) |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8844 | return Cand->Surrogate->getLocation(); |
| 8845 | return SourceLocation(); |
| 8846 | } |
| 8847 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 8848 | static unsigned RankDeductionFailure(const DeductionFailureInfo &DFI) { |
Chandler Carruth | 78bf680 | 2011-09-10 00:51:24 +0000 | [diff] [blame] | 8849 | switch ((Sema::TemplateDeductionResult)DFI.Result) { |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8850 | case Sema::TDK_Success: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8851 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8852 | |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 8853 | case Sema::TDK_Invalid: |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8854 | case Sema::TDK_Incomplete: |
| 8855 | return 1; |
| 8856 | |
| 8857 | case Sema::TDK_Underqualified: |
| 8858 | case Sema::TDK_Inconsistent: |
| 8859 | return 2; |
| 8860 | |
| 8861 | case Sema::TDK_SubstitutionFailure: |
| 8862 | case Sema::TDK_NonDeducedMismatch: |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 8863 | case Sema::TDK_MiscellaneousDeductionFailure: |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8864 | return 3; |
| 8865 | |
| 8866 | case Sema::TDK_InstantiationDepth: |
| 8867 | case Sema::TDK_FailedOverloadResolution: |
| 8868 | return 4; |
| 8869 | |
| 8870 | case Sema::TDK_InvalidExplicitArguments: |
| 8871 | return 5; |
| 8872 | |
| 8873 | case Sema::TDK_TooManyArguments: |
| 8874 | case Sema::TDK_TooFewArguments: |
| 8875 | return 6; |
| 8876 | } |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8877 | llvm_unreachable("Unhandled deduction result"); |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8878 | } |
| 8879 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8880 | struct CompareOverloadCandidatesForDisplay { |
| 8881 | Sema &S; |
| 8882 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8883 | |
| 8884 | bool operator()(const OverloadCandidate *L, |
| 8885 | const OverloadCandidate *R) { |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 8886 | // Fast-path this check. |
| 8887 | if (L == R) return false; |
| 8888 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8889 | // Order first by viability. |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8890 | if (L->Viable) { |
| 8891 | if (!R->Viable) return true; |
| 8892 | |
| 8893 | // TODO: introduce a tri-valued comparison for overload |
| 8894 | // candidates. Would be more worthwhile if we had a sort |
| 8895 | // that could exploit it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8896 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation())) return true; |
| 8897 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation())) return false; |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8898 | } else if (R->Viable) |
| 8899 | return false; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8900 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8901 | assert(L->Viable == R->Viable); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8902 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8903 | // Criteria by which we can sort non-viable candidates: |
| 8904 | if (!L->Viable) { |
| 8905 | // 1. Arity mismatches come after other candidates. |
| 8906 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 8907 | L->FailureKind == ovl_fail_too_few_arguments) |
| 8908 | return false; |
| 8909 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 8910 | R->FailureKind == ovl_fail_too_few_arguments) |
| 8911 | return true; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8912 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8913 | // 2. Bad conversions come first and are ordered by the number |
| 8914 | // of bad conversions and quality of good conversions. |
| 8915 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 8916 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 8917 | return true; |
| 8918 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8919 | // The conversion that can be fixed with a smaller number of changes, |
| 8920 | // comes first. |
| 8921 | unsigned numLFixes = L->Fix.NumConversionsFixed; |
| 8922 | unsigned numRFixes = R->Fix.NumConversionsFixed; |
| 8923 | numLFixes = (numLFixes == 0) ? UINT_MAX : numLFixes; |
| 8924 | numRFixes = (numRFixes == 0) ? UINT_MAX : numRFixes; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 8925 | if (numLFixes != numRFixes) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8926 | if (numLFixes < numRFixes) |
| 8927 | return true; |
| 8928 | else |
| 8929 | return false; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 8930 | } |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8931 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8932 | // If there's any ordering between the defined conversions... |
| 8933 | // FIXME: this might not be transitive. |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8934 | assert(L->NumConversions == R->NumConversions); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8935 | |
| 8936 | int leftBetter = 0; |
John McCall | 3a81337 | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 8937 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8938 | for (unsigned E = L->NumConversions; I != E; ++I) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8939 | switch (CompareImplicitConversionSequences(S, |
| 8940 | L->Conversions[I], |
| 8941 | R->Conversions[I])) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8942 | case ImplicitConversionSequence::Better: |
| 8943 | leftBetter++; |
| 8944 | break; |
| 8945 | |
| 8946 | case ImplicitConversionSequence::Worse: |
| 8947 | leftBetter--; |
| 8948 | break; |
| 8949 | |
| 8950 | case ImplicitConversionSequence::Indistinguishable: |
| 8951 | break; |
| 8952 | } |
| 8953 | } |
| 8954 | if (leftBetter > 0) return true; |
| 8955 | if (leftBetter < 0) return false; |
| 8956 | |
| 8957 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 8958 | return false; |
| 8959 | |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8960 | if (L->FailureKind == ovl_fail_bad_deduction) { |
| 8961 | if (R->FailureKind != ovl_fail_bad_deduction) |
| 8962 | return true; |
| 8963 | |
| 8964 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 8965 | return RankDeductionFailure(L->DeductionFailure) |
Eli Friedman | ce1846e | 2011-10-14 23:10:30 +0000 | [diff] [blame] | 8966 | < RankDeductionFailure(R->DeductionFailure); |
Eli Friedman | 1c522f7 | 2011-10-14 21:52:24 +0000 | [diff] [blame] | 8967 | } else if (R->FailureKind == ovl_fail_bad_deduction) |
| 8968 | return false; |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8969 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8970 | // TODO: others? |
| 8971 | } |
| 8972 | |
| 8973 | // Sort everything else by location. |
| 8974 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 8975 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 8976 | |
| 8977 | // Put candidates without locations (e.g. builtins) at the end. |
| 8978 | if (LLoc.isInvalid()) return false; |
| 8979 | if (RLoc.isInvalid()) return true; |
| 8980 | |
| 8981 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8982 | } |
| 8983 | }; |
| 8984 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8985 | /// CompleteNonViableCandidate - Normally, overload resolution only |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8986 | /// computes up to the first. Produces the FixIt set if possible. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8987 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8988 | ArrayRef<Expr *> Args) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8989 | assert(!Cand->Viable); |
| 8990 | |
| 8991 | // Don't do anything on failures other than bad conversion. |
| 8992 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 8993 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8994 | // We only want the FixIts if all the arguments can be corrected. |
| 8995 | bool Unfixable = false; |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 8996 | // Use a implicit copy initialization to check conversion fixes. |
| 8997 | Cand->Fix.setConversionChecker(TryCopyInitialization); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8998 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8999 | // Skip forward to the first bad conversion. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9000 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 9001 | unsigned ConvCount = Cand->NumConversions; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9002 | while (true) { |
| 9003 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 9004 | ConvIdx++; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9005 | if (Cand->Conversions[ConvIdx - 1].isBad()) { |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9006 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx - 1, S); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9007 | break; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9008 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9009 | } |
| 9010 | |
| 9011 | if (ConvIdx == ConvCount) |
| 9012 | return; |
| 9013 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 9014 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 9015 | "remaining conversion is initialized?"); |
| 9016 | |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 9017 | // FIXME: this should probably be preserved from the overload |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9018 | // operation somehow. |
| 9019 | bool SuppressUserConversions = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9020 | |
| 9021 | const FunctionProtoType* Proto; |
| 9022 | unsigned ArgIdx = ConvIdx; |
| 9023 | |
| 9024 | if (Cand->IsSurrogate) { |
| 9025 | QualType ConvType |
| 9026 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 9027 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 9028 | ConvType = ConvPtrType->getPointeeType(); |
| 9029 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 9030 | ArgIdx--; |
| 9031 | } else if (Cand->Function) { |
| 9032 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 9033 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 9034 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 9035 | ArgIdx--; |
| 9036 | } else { |
| 9037 | // Builtin binary operator with a bad first conversion. |
| 9038 | assert(ConvCount <= 3); |
| 9039 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 9040 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 9041 | = TryCopyInitialization(S, Args[ConvIdx], |
| 9042 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9043 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9044 | /*InOverloadResolution*/ true, |
| 9045 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9046 | S.getLangOpts().ObjCAutoRefCount); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9047 | return; |
| 9048 | } |
| 9049 | |
| 9050 | // Fill in the rest of the conversions. |
| 9051 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 9052 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9053 | if (ArgIdx < NumArgsInProto) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9054 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 9055 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9056 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9057 | /*InOverloadResolution=*/true, |
| 9058 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9059 | S.getLangOpts().ObjCAutoRefCount); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9060 | // Store the FixIt in the candidate if it exists. |
| 9061 | if (!Unfixable && Cand->Conversions[ConvIdx].isBad()) |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 9062 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx, S); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 9063 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9064 | else |
| 9065 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 9066 | } |
| 9067 | } |
| 9068 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9069 | } // end anonymous namespace |
| 9070 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9071 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 9072 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9073 | /// set. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9074 | void OverloadCandidateSet::NoteCandidates(Sema &S, |
| 9075 | OverloadCandidateDisplayKind OCD, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9076 | ArrayRef<Expr *> Args, |
David Blaikie | 0bea863 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 9077 | StringRef Opc, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9078 | SourceLocation OpLoc) { |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9079 | // Sort the candidates by viability and position. Sorting directly would |
| 9080 | // be prohibitive, so we make a set of pointers and sort those. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9081 | SmallVector<OverloadCandidate*, 32> Cands; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9082 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
| 9083 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9084 | if (Cand->Viable) |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9085 | Cands.push_back(Cand); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9086 | else if (OCD == OCD_AllCandidates) { |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9087 | CompleteNonViableCandidate(S, Cand, Args); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9088 | if (Cand->Function || Cand->IsSurrogate) |
| 9089 | Cands.push_back(Cand); |
| 9090 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 9091 | // want to list every possible builtin candidate. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 9092 | } |
| 9093 | } |
| 9094 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 9095 | std::sort(Cands.begin(), Cands.end(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9096 | CompareOverloadCandidatesForDisplay(S)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9097 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9098 | bool ReportedAmbiguousConversions = false; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9099 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9100 | SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 9101 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9102 | unsigned CandsShown = 0; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 9103 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 9104 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 9105 | |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9106 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 9107 | // the user with. FIXME: This limit should depend on details of the |
| 9108 | // candidate list. |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 9109 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) { |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9110 | break; |
| 9111 | } |
| 9112 | ++CandsShown; |
| 9113 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9114 | if (Cand->Function) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9115 | NoteFunctionCandidate(S, Cand, Args.size()); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9116 | else if (Cand->IsSurrogate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9117 | NoteSurrogateCandidate(S, Cand); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9118 | else { |
| 9119 | assert(Cand->Viable && |
| 9120 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9121 | // Generally we only see ambiguities including viable builtin |
| 9122 | // operators if overload resolution got screwed up by an |
| 9123 | // ambiguous user-defined conversion. |
| 9124 | // |
| 9125 | // FIXME: It's quite possible for different conversions to see |
| 9126 | // different ambiguities, though. |
| 9127 | if (!ReportedAmbiguousConversions) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9128 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9129 | ReportedAmbiguousConversions = true; |
| 9130 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 9131 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9132 | // If this is a viable builtin, print it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9133 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 9134 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9135 | } |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 9136 | |
| 9137 | if (I != E) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9138 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9139 | } |
| 9140 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9141 | static SourceLocation |
| 9142 | GetLocationForCandidate(const TemplateSpecCandidate *Cand) { |
| 9143 | return Cand->Specialization ? Cand->Specialization->getLocation() |
| 9144 | : SourceLocation(); |
| 9145 | } |
| 9146 | |
| 9147 | struct CompareTemplateSpecCandidatesForDisplay { |
| 9148 | Sema &S; |
| 9149 | CompareTemplateSpecCandidatesForDisplay(Sema &S) : S(S) {} |
| 9150 | |
| 9151 | bool operator()(const TemplateSpecCandidate *L, |
| 9152 | const TemplateSpecCandidate *R) { |
| 9153 | // Fast-path this check. |
| 9154 | if (L == R) |
| 9155 | return false; |
| 9156 | |
| 9157 | // Assuming that both candidates are not matches... |
| 9158 | |
| 9159 | // Sort by the ranking of deduction failures. |
| 9160 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 9161 | return RankDeductionFailure(L->DeductionFailure) < |
| 9162 | RankDeductionFailure(R->DeductionFailure); |
| 9163 | |
| 9164 | // Sort everything else by location. |
| 9165 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 9166 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 9167 | |
| 9168 | // Put candidates without locations (e.g. builtins) at the end. |
| 9169 | if (LLoc.isInvalid()) |
| 9170 | return false; |
| 9171 | if (RLoc.isInvalid()) |
| 9172 | return true; |
| 9173 | |
| 9174 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
| 9175 | } |
| 9176 | }; |
| 9177 | |
| 9178 | /// Diagnose a template argument deduction failure. |
| 9179 | /// We are treating these failures as overload failures due to bad |
| 9180 | /// deductions. |
| 9181 | void TemplateSpecCandidate::NoteDeductionFailure(Sema &S) { |
| 9182 | DiagnoseBadDeduction(S, Specialization, // pattern |
| 9183 | DeductionFailure, /*NumArgs=*/0); |
| 9184 | } |
| 9185 | |
| 9186 | void TemplateSpecCandidateSet::destroyCandidates() { |
| 9187 | for (iterator i = begin(), e = end(); i != e; ++i) { |
| 9188 | i->DeductionFailure.Destroy(); |
| 9189 | } |
| 9190 | } |
| 9191 | |
| 9192 | void TemplateSpecCandidateSet::clear() { |
| 9193 | destroyCandidates(); |
| 9194 | Candidates.clear(); |
| 9195 | } |
| 9196 | |
| 9197 | /// NoteCandidates - When no template specialization match is found, prints |
| 9198 | /// diagnostic messages containing the non-matching specializations that form |
| 9199 | /// the candidate set. |
| 9200 | /// This is analoguous to OverloadCandidateSet::NoteCandidates() with |
| 9201 | /// OCD == OCD_AllCandidates and Cand->Viable == false. |
| 9202 | void TemplateSpecCandidateSet::NoteCandidates(Sema &S, SourceLocation Loc) { |
| 9203 | // Sort the candidates by position (assuming no candidate is a match). |
| 9204 | // Sorting directly would be prohibitive, so we make a set of pointers |
| 9205 | // and sort those. |
| 9206 | SmallVector<TemplateSpecCandidate *, 32> Cands; |
| 9207 | Cands.reserve(size()); |
| 9208 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
| 9209 | if (Cand->Specialization) |
| 9210 | Cands.push_back(Cand); |
| 9211 | // Otherwise, this is a non matching builtin candidate. We do not, |
| 9212 | // in general, want to list every possible builtin candidate. |
| 9213 | } |
| 9214 | |
| 9215 | std::sort(Cands.begin(), Cands.end(), |
| 9216 | CompareTemplateSpecCandidatesForDisplay(S)); |
| 9217 | |
| 9218 | // FIXME: Perhaps rename OverloadsShown and getShowOverloads() |
| 9219 | // for generalization purposes (?). |
| 9220 | const OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
| 9221 | |
| 9222 | SmallVectorImpl<TemplateSpecCandidate *>::iterator I, E; |
| 9223 | unsigned CandsShown = 0; |
| 9224 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 9225 | TemplateSpecCandidate *Cand = *I; |
| 9226 | |
| 9227 | // Set an arbitrary limit on the number of candidates we'll spam |
| 9228 | // the user with. FIXME: This limit should depend on details of the |
| 9229 | // candidate list. |
| 9230 | if (CandsShown >= 4 && ShowOverloads == Ovl_Best) |
| 9231 | break; |
| 9232 | ++CandsShown; |
| 9233 | |
| 9234 | assert(Cand->Specialization && |
| 9235 | "Non-matching built-in candidates are not added to Cands."); |
| 9236 | Cand->NoteDeductionFailure(S); |
| 9237 | } |
| 9238 | |
| 9239 | if (I != E) |
| 9240 | S.Diag(Loc, diag::note_ovl_too_many_candidates) << int(E - I); |
| 9241 | } |
| 9242 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9243 | // [PossiblyAFunctionType] --> [Return] |
| 9244 | // NonFunctionType --> NonFunctionType |
| 9245 | // R (A) --> R(A) |
| 9246 | // R (*)(A) --> R (A) |
| 9247 | // R (&)(A) --> R (A) |
| 9248 | // R (S::*)(A) --> R (A) |
| 9249 | QualType Sema::ExtractUnqualifiedFunctionType(QualType PossiblyAFunctionType) { |
| 9250 | QualType Ret = PossiblyAFunctionType; |
| 9251 | if (const PointerType *ToTypePtr = |
| 9252 | PossiblyAFunctionType->getAs<PointerType>()) |
| 9253 | Ret = ToTypePtr->getPointeeType(); |
| 9254 | else if (const ReferenceType *ToTypeRef = |
| 9255 | PossiblyAFunctionType->getAs<ReferenceType>()) |
| 9256 | Ret = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 9257 | else if (const MemberPointerType *MemTypePtr = |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9258 | PossiblyAFunctionType->getAs<MemberPointerType>()) |
| 9259 | Ret = MemTypePtr->getPointeeType(); |
| 9260 | Ret = |
| 9261 | Context.getCanonicalType(Ret).getUnqualifiedType(); |
| 9262 | return Ret; |
| 9263 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9264 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9265 | // A helper class to help with address of function resolution |
| 9266 | // - allows us to avoid passing around all those ugly parameters |
| 9267 | class AddressOfFunctionResolver |
| 9268 | { |
| 9269 | Sema& S; |
| 9270 | Expr* SourceExpr; |
| 9271 | const QualType& TargetType; |
| 9272 | QualType TargetFunctionType; // Extracted function type from target type |
| 9273 | |
| 9274 | bool Complain; |
| 9275 | //DeclAccessPair& ResultFunctionAccessPair; |
| 9276 | ASTContext& Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9277 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9278 | bool TargetTypeIsNonStaticMemberFunction; |
| 9279 | bool FoundNonTemplateFunction; |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9280 | bool StaticMemberFunctionFromBoundPointer; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9281 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9282 | OverloadExpr::FindResult OvlExprInfo; |
| 9283 | OverloadExpr *OvlExpr; |
| 9284 | TemplateArgumentListInfo OvlExplicitTemplateArgs; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9285 | SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9286 | TemplateSpecCandidateSet FailedCandidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9287 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9288 | public: |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9289 | AddressOfFunctionResolver(Sema &S, Expr *SourceExpr, |
| 9290 | const QualType &TargetType, bool Complain) |
| 9291 | : S(S), SourceExpr(SourceExpr), TargetType(TargetType), |
| 9292 | Complain(Complain), Context(S.getASTContext()), |
| 9293 | TargetTypeIsNonStaticMemberFunction( |
| 9294 | !!TargetType->getAs<MemberPointerType>()), |
| 9295 | FoundNonTemplateFunction(false), |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9296 | StaticMemberFunctionFromBoundPointer(false), |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9297 | OvlExprInfo(OverloadExpr::find(SourceExpr)), |
| 9298 | OvlExpr(OvlExprInfo.Expression), |
| 9299 | FailedCandidates(OvlExpr->getNameLoc()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9300 | ExtractUnqualifiedFunctionTypeFromTargetType(); |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9301 | |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9302 | if (TargetFunctionType->isFunctionType()) { |
| 9303 | if (UnresolvedMemberExpr *UME = dyn_cast<UnresolvedMemberExpr>(OvlExpr)) |
| 9304 | if (!UME->isImplicitAccess() && |
| 9305 | !S.ResolveSingleFunctionTemplateSpecialization(UME)) |
| 9306 | StaticMemberFunctionFromBoundPointer = true; |
| 9307 | } else if (OvlExpr->hasExplicitTemplateArgs()) { |
| 9308 | DeclAccessPair dap; |
| 9309 | if (FunctionDecl *Fn = S.ResolveSingleFunctionTemplateSpecialization( |
| 9310 | OvlExpr, false, &dap)) { |
| 9311 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
| 9312 | if (!Method->isStatic()) { |
| 9313 | // If the target type is a non-function type and the function found |
| 9314 | // is a non-static member function, pretend as if that was the |
| 9315 | // target, it's the only possible type to end up with. |
| 9316 | TargetTypeIsNonStaticMemberFunction = true; |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9317 | |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9318 | // And skip adding the function if its not in the proper form. |
| 9319 | // We'll diagnose this due to an empty set of functions. |
| 9320 | if (!OvlExprInfo.HasFormOfMemberPointer) |
| 9321 | return; |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 9322 | } |
| 9323 | |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9324 | Matches.push_back(std::make_pair(dap, Fn)); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 9325 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9326 | return; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 9327 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9328 | |
| 9329 | if (OvlExpr->hasExplicitTemplateArgs()) |
| 9330 | OvlExpr->getExplicitTemplateArgs().copyInto(OvlExplicitTemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9331 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9332 | if (FindAllFunctionsThatMatchTargetTypeExactly()) { |
| 9333 | // C++ [over.over]p4: |
| 9334 | // If more than one function is selected, [...] |
| 9335 | if (Matches.size() > 1) { |
| 9336 | if (FoundNonTemplateFunction) |
| 9337 | EliminateAllTemplateMatches(); |
| 9338 | else |
| 9339 | EliminateAllExceptMostSpecializedTemplate(); |
| 9340 | } |
| 9341 | } |
| 9342 | } |
| 9343 | |
| 9344 | private: |
| 9345 | bool isTargetTypeAFunction() const { |
| 9346 | return TargetFunctionType->isFunctionType(); |
| 9347 | } |
| 9348 | |
| 9349 | // [ToType] [Return] |
| 9350 | |
| 9351 | // R (*)(A) --> R (A), IsNonStaticMemberFunction = false |
| 9352 | // R (&)(A) --> R (A), IsNonStaticMemberFunction = false |
| 9353 | // R (S::*)(A) --> R (A), IsNonStaticMemberFunction = true |
| 9354 | void inline ExtractUnqualifiedFunctionTypeFromTargetType() { |
| 9355 | TargetFunctionType = S.ExtractUnqualifiedFunctionType(TargetType); |
| 9356 | } |
| 9357 | |
| 9358 | // return true if any matching specializations were found |
| 9359 | bool AddMatchingTemplateFunction(FunctionTemplateDecl* FunctionTemplate, |
| 9360 | const DeclAccessPair& CurAccessFunPair) { |
| 9361 | if (CXXMethodDecl *Method |
| 9362 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
| 9363 | // Skip non-static function templates when converting to pointer, and |
| 9364 | // static when converting to member pointer. |
| 9365 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 9366 | return false; |
| 9367 | } |
| 9368 | else if (TargetTypeIsNonStaticMemberFunction) |
| 9369 | return false; |
| 9370 | |
| 9371 | // C++ [over.over]p2: |
| 9372 | // If the name is a function template, template argument deduction is |
| 9373 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 9374 | // resulting template argument list is used to generate a single |
| 9375 | // function template specialization, which is added to the set of |
| 9376 | // overloaded functions considered. |
| 9377 | FunctionDecl *Specialization = 0; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9378 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9379 | if (Sema::TemplateDeductionResult Result |
| 9380 | = S.DeduceTemplateArguments(FunctionTemplate, |
| 9381 | &OvlExplicitTemplateArgs, |
| 9382 | TargetFunctionType, Specialization, |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9383 | Info, /*InOverloadResolution=*/true)) { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9384 | // Make a note of the failed deduction for diagnostics. |
| 9385 | FailedCandidates.addCandidate() |
| 9386 | .set(FunctionTemplate->getTemplatedDecl(), |
| 9387 | MakeDeductionFailureInfo(Context, Result, Info)); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9388 | return false; |
| 9389 | } |
| 9390 | |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9391 | // Template argument deduction ensures that we have an exact match or |
| 9392 | // compatible pointer-to-function arguments that would be adjusted by ICS. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9393 | // This function template specicalization works. |
| 9394 | Specialization = cast<FunctionDecl>(Specialization->getCanonicalDecl()); |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9395 | assert(S.isSameOrCompatibleFunctionType( |
| 9396 | Context.getCanonicalType(Specialization->getType()), |
| 9397 | Context.getCanonicalType(TargetFunctionType))); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9398 | Matches.push_back(std::make_pair(CurAccessFunPair, Specialization)); |
| 9399 | return true; |
| 9400 | } |
| 9401 | |
| 9402 | bool AddMatchingNonTemplateFunction(NamedDecl* Fn, |
| 9403 | const DeclAccessPair& CurAccessFunPair) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9404 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 9405 | // Skip non-static functions when converting to pointer, and static |
| 9406 | // when converting to member pointer. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9407 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 9408 | return false; |
| 9409 | } |
| 9410 | else if (TargetTypeIsNonStaticMemberFunction) |
| 9411 | return false; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9412 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9413 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9414 | if (S.getLangOpts().CUDA) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 9415 | if (FunctionDecl *Caller = dyn_cast<FunctionDecl>(S.CurContext)) |
| 9416 | if (S.CheckCUDATarget(Caller, FunDecl)) |
| 9417 | return false; |
| 9418 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9419 | // If any candidate has a placeholder return type, trigger its deduction |
| 9420 | // now. |
| 9421 | if (S.getLangOpts().CPlusPlus1y && |
| 9422 | FunDecl->getResultType()->isUndeducedType() && |
| 9423 | S.DeduceReturnType(FunDecl, SourceExpr->getLocStart(), Complain)) |
| 9424 | return false; |
| 9425 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 9426 | QualType ResultTy; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9427 | if (Context.hasSameUnqualifiedType(TargetFunctionType, |
| 9428 | FunDecl->getType()) || |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 9429 | S.IsNoReturnConversion(FunDecl->getType(), TargetFunctionType, |
| 9430 | ResultTy)) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9431 | Matches.push_back(std::make_pair(CurAccessFunPair, |
| 9432 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 9433 | FoundNonTemplateFunction = true; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9434 | return true; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 9435 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9436 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9437 | |
| 9438 | return false; |
| 9439 | } |
| 9440 | |
| 9441 | bool FindAllFunctionsThatMatchTargetTypeExactly() { |
| 9442 | bool Ret = false; |
| 9443 | |
| 9444 | // If the overload expression doesn't have the form of a pointer to |
| 9445 | // member, don't try to convert it to a pointer-to-member type. |
| 9446 | if (IsInvalidFormOfPointerToMemberFunction()) |
| 9447 | return false; |
| 9448 | |
| 9449 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 9450 | E = OvlExpr->decls_end(); |
| 9451 | I != E; ++I) { |
| 9452 | // Look through any using declarations to find the underlying function. |
| 9453 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 9454 | |
| 9455 | // C++ [over.over]p3: |
| 9456 | // Non-member functions and static member functions match |
| 9457 | // targets of type "pointer-to-function" or "reference-to-function." |
| 9458 | // Nonstatic member functions match targets of |
| 9459 | // type "pointer-to-member-function." |
| 9460 | // Note that according to DR 247, the containing class does not matter. |
| 9461 | if (FunctionTemplateDecl *FunctionTemplate |
| 9462 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
| 9463 | if (AddMatchingTemplateFunction(FunctionTemplate, I.getPair())) |
| 9464 | Ret = true; |
| 9465 | } |
| 9466 | // If we have explicit template arguments supplied, skip non-templates. |
| 9467 | else if (!OvlExpr->hasExplicitTemplateArgs() && |
| 9468 | AddMatchingNonTemplateFunction(Fn, I.getPair())) |
| 9469 | Ret = true; |
| 9470 | } |
| 9471 | assert(Ret || Matches.empty()); |
| 9472 | return Ret; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9473 | } |
| 9474 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9475 | void EliminateAllExceptMostSpecializedTemplate() { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 9476 | // [...] and any given function template specialization F1 is |
| 9477 | // eliminated if the set contains a second function template |
| 9478 | // specialization whose function template is more specialized |
| 9479 | // than the function template of F1 according to the partial |
| 9480 | // ordering rules of 14.5.5.2. |
| 9481 | |
| 9482 | // The algorithm specified above is quadratic. We instead use a |
| 9483 | // two-pass algorithm (similar to the one used to identify the |
| 9484 | // best viable function in an overload set) that identifies the |
| 9485 | // best function template (if it exists). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9486 | |
| 9487 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 9488 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 9489 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9490 | |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9491 | // TODO: It looks like FailedCandidates does not serve much purpose |
| 9492 | // here, since the no_viable diagnostic has index 0. |
| 9493 | UnresolvedSetIterator Result = S.getMostSpecialized( |
Richard Smith | 4ad09e6 | 2013-09-10 22:59:25 +0000 | [diff] [blame^] | 9494 | MatchesCopy.begin(), MatchesCopy.end(), FailedCandidates, |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9495 | SourceExpr->getLocStart(), S.PDiag(), |
| 9496 | S.PDiag(diag::err_addr_ovl_ambiguous) << Matches[0] |
| 9497 | .second->getDeclName(), |
| 9498 | S.PDiag(diag::note_ovl_candidate) << (unsigned)oc_function_template, |
| 9499 | Complain, TargetFunctionType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9500 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9501 | if (Result != MatchesCopy.end()) { |
| 9502 | // Make it the first and only element |
| 9503 | Matches[0].first = Matches[Result - MatchesCopy.begin()].first; |
| 9504 | Matches[0].second = cast<FunctionDecl>(*Result); |
| 9505 | Matches.resize(1); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9506 | } |
| 9507 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9508 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9509 | void EliminateAllTemplateMatches() { |
| 9510 | // [...] any function template specializations in the set are |
| 9511 | // eliminated if the set also contains a non-template function, [...] |
| 9512 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
| 9513 | if (Matches[I].second->getPrimaryTemplate() == 0) |
| 9514 | ++I; |
| 9515 | else { |
| 9516 | Matches[I] = Matches[--N]; |
| 9517 | Matches.set_size(N); |
| 9518 | } |
| 9519 | } |
| 9520 | } |
| 9521 | |
| 9522 | public: |
| 9523 | void ComplainNoMatchesFound() const { |
| 9524 | assert(Matches.empty()); |
| 9525 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_no_viable) |
| 9526 | << OvlExpr->getName() << TargetFunctionType |
| 9527 | << OvlExpr->getSourceRange(); |
Richard Smith | 72a36a1 | 2013-08-14 00:00:44 +0000 | [diff] [blame] | 9528 | if (FailedCandidates.empty()) |
| 9529 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
| 9530 | else { |
| 9531 | // We have some deduction failure messages. Use them to diagnose |
| 9532 | // the function templates, and diagnose the non-template candidates |
| 9533 | // normally. |
| 9534 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 9535 | IEnd = OvlExpr->decls_end(); |
| 9536 | I != IEnd; ++I) |
| 9537 | if (FunctionDecl *Fun = |
| 9538 | dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl())) |
| 9539 | S.NoteOverloadCandidate(Fun, TargetFunctionType); |
| 9540 | FailedCandidates.NoteCandidates(S, OvlExpr->getLocStart()); |
| 9541 | } |
| 9542 | } |
| 9543 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9544 | bool IsInvalidFormOfPointerToMemberFunction() const { |
| 9545 | return TargetTypeIsNonStaticMemberFunction && |
| 9546 | !OvlExprInfo.HasFormOfMemberPointer; |
| 9547 | } |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9548 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9549 | void ComplainIsInvalidFormOfPointerToMemberFunction() const { |
| 9550 | // TODO: Should we condition this on whether any functions might |
| 9551 | // have matched, or is it more appropriate to do that in callers? |
| 9552 | // TODO: a fixit wouldn't hurt. |
| 9553 | S.Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
| 9554 | << TargetType << OvlExpr->getSourceRange(); |
| 9555 | } |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9556 | |
| 9557 | bool IsStaticMemberFunctionFromBoundPointer() const { |
| 9558 | return StaticMemberFunctionFromBoundPointer; |
| 9559 | } |
| 9560 | |
| 9561 | void ComplainIsStaticMemberFunctionFromBoundPointer() const { |
| 9562 | S.Diag(OvlExpr->getLocStart(), |
| 9563 | diag::err_invalid_form_pointer_member_function) |
| 9564 | << OvlExpr->getSourceRange(); |
| 9565 | } |
| 9566 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9567 | void ComplainOfInvalidConversion() const { |
| 9568 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 9569 | << OvlExpr->getName() << TargetType; |
| 9570 | } |
| 9571 | |
| 9572 | void ComplainMultipleMatchesFound() const { |
| 9573 | assert(Matches.size() > 1); |
| 9574 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 9575 | << OvlExpr->getName() |
| 9576 | << OvlExpr->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9577 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9578 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9579 | |
| 9580 | bool hadMultipleCandidates() const { return (OvlExpr->getNumDecls() > 1); } |
| 9581 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9582 | int getNumMatches() const { return Matches.size(); } |
| 9583 | |
| 9584 | FunctionDecl* getMatchingFunctionDecl() const { |
| 9585 | if (Matches.size() != 1) return 0; |
| 9586 | return Matches[0].second; |
| 9587 | } |
| 9588 | |
| 9589 | const DeclAccessPair* getMatchingFunctionAccessPair() const { |
| 9590 | if (Matches.size() != 1) return 0; |
| 9591 | return &Matches[0].first; |
| 9592 | } |
| 9593 | }; |
| 9594 | |
| 9595 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 9596 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 9597 | /// expression with overloaded function type and @p ToType is the type |
| 9598 | /// we're trying to resolve to. For example: |
| 9599 | /// |
| 9600 | /// @code |
| 9601 | /// int f(double); |
| 9602 | /// int f(int); |
| 9603 | /// |
| 9604 | /// int (*pfd)(double) = f; // selects f(double) |
| 9605 | /// @endcode |
| 9606 | /// |
| 9607 | /// This routine returns the resulting FunctionDecl if it could be |
| 9608 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 9609 | /// routine will emit diagnostics if there is an error. |
| 9610 | FunctionDecl * |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9611 | Sema::ResolveAddressOfOverloadedFunction(Expr *AddressOfExpr, |
| 9612 | QualType TargetType, |
| 9613 | bool Complain, |
| 9614 | DeclAccessPair &FoundResult, |
| 9615 | bool *pHadMultipleCandidates) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9616 | assert(AddressOfExpr->getType() == Context.OverloadTy); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9617 | |
| 9618 | AddressOfFunctionResolver Resolver(*this, AddressOfExpr, TargetType, |
| 9619 | Complain); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9620 | int NumMatches = Resolver.getNumMatches(); |
| 9621 | FunctionDecl* Fn = 0; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9622 | if (NumMatches == 0 && Complain) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9623 | if (Resolver.IsInvalidFormOfPointerToMemberFunction()) |
| 9624 | Resolver.ComplainIsInvalidFormOfPointerToMemberFunction(); |
| 9625 | else |
| 9626 | Resolver.ComplainNoMatchesFound(); |
| 9627 | } |
| 9628 | else if (NumMatches > 1 && Complain) |
| 9629 | Resolver.ComplainMultipleMatchesFound(); |
| 9630 | else if (NumMatches == 1) { |
| 9631 | Fn = Resolver.getMatchingFunctionDecl(); |
| 9632 | assert(Fn); |
| 9633 | FoundResult = *Resolver.getMatchingFunctionAccessPair(); |
David Majnemer | 576a9af | 2013-08-01 06:13:59 +0000 | [diff] [blame] | 9634 | if (Complain) { |
| 9635 | if (Resolver.IsStaticMemberFunctionFromBoundPointer()) |
| 9636 | Resolver.ComplainIsStaticMemberFunctionFromBoundPointer(); |
| 9637 | else |
| 9638 | CheckAddressOfMemberAccess(AddressOfExpr, FoundResult); |
| 9639 | } |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 9640 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9641 | |
| 9642 | if (pHadMultipleCandidates) |
| 9643 | *pHadMultipleCandidates = Resolver.hadMultipleCandidates(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9644 | return Fn; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9645 | } |
| 9646 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9647 | /// \brief Given an expression that refers to an overloaded function, try to |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9648 | /// resolve that overloaded function expression down to a single function. |
| 9649 | /// |
| 9650 | /// This routine can only resolve template-ids that refer to a single function |
| 9651 | /// template, where that template-id refers to a single template whose template |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9652 | /// arguments are either provided by the template-id or have defaults, |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9653 | /// as described in C++0x [temp.arg.explicit]p3. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9654 | FunctionDecl * |
| 9655 | Sema::ResolveSingleFunctionTemplateSpecialization(OverloadExpr *ovl, |
| 9656 | bool Complain, |
| 9657 | DeclAccessPair *FoundResult) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9658 | // C++ [over.over]p1: |
| 9659 | // [...] [Note: any redundant set of parentheses surrounding the |
| 9660 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9661 | // C++ [over.over]p1: |
| 9662 | // [...] The overloaded function name can be preceded by the & |
| 9663 | // operator. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9664 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9665 | // If we didn't actually find any template-ids, we're done. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9666 | if (!ovl->hasExplicitTemplateArgs()) |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9667 | return 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 9668 | |
| 9669 | TemplateArgumentListInfo ExplicitTemplateArgs; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9670 | ovl->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9671 | TemplateSpecCandidateSet FailedCandidates(ovl->getNameLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9672 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9673 | // Look through all of the overloaded functions, searching for one |
| 9674 | // whose type matches exactly. |
| 9675 | FunctionDecl *Matched = 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9676 | for (UnresolvedSetIterator I = ovl->decls_begin(), |
| 9677 | E = ovl->decls_end(); I != E; ++I) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9678 | // C++0x [temp.arg.explicit]p3: |
| 9679 | // [...] In contexts where deduction is done and fails, or in contexts |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9680 | // where deduction is not done, if a template argument list is |
| 9681 | // specified and it, along with any default template arguments, |
| 9682 | // identifies a single function template specialization, then the |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9683 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | 66a8c9a | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 9684 | FunctionTemplateDecl *FunctionTemplate |
| 9685 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9686 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9687 | // C++ [over.over]p2: |
| 9688 | // If the name is a function template, template argument deduction is |
| 9689 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 9690 | // resulting template argument list is used to generate a single |
| 9691 | // function template specialization, which is added to the set of |
| 9692 | // overloaded functions considered. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9693 | FunctionDecl *Specialization = 0; |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9694 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9695 | if (TemplateDeductionResult Result |
| 9696 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 9697 | Specialization, Info, |
| 9698 | /*InOverloadResolution=*/true)) { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 9699 | // Make a note of the failed deduction for diagnostics. |
| 9700 | // TODO: Actually use the failed-deduction info? |
| 9701 | FailedCandidates.addCandidate() |
| 9702 | .set(FunctionTemplate->getTemplatedDecl(), |
| 9703 | MakeDeductionFailureInfo(Context, Result, Info)); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9704 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9705 | } |
| 9706 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9707 | assert(Specialization && "no specialization and no error?"); |
| 9708 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9709 | // Multiple matches; we can't resolve to a single declaration. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9710 | if (Matched) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9711 | if (Complain) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9712 | Diag(ovl->getExprLoc(), diag::err_addr_ovl_ambiguous) |
| 9713 | << ovl->getName(); |
| 9714 | NoteAllOverloadCandidates(ovl); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9715 | } |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9716 | return 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9717 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9718 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9719 | Matched = Specialization; |
| 9720 | if (FoundResult) *FoundResult = I.getPair(); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9721 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9722 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 9723 | if (Matched && getLangOpts().CPlusPlus1y && |
| 9724 | Matched->getResultType()->isUndeducedType() && |
| 9725 | DeduceReturnType(Matched, ovl->getExprLoc(), Complain)) |
| 9726 | return 0; |
| 9727 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9728 | return Matched; |
| 9729 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9730 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9731 | |
| 9732 | |
| 9733 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9734 | // Resolve and fix an overloaded expression that can be resolved |
| 9735 | // because it identifies a single function template specialization. |
| 9736 | // |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9737 | // Last three arguments should only be supplied if Complain = true |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9738 | // |
| 9739 | // Return true if it was logically possible to so resolve the |
| 9740 | // expression, regardless of whether or not it succeeded. Always |
| 9741 | // returns true if 'complain' is set. |
| 9742 | bool Sema::ResolveAndFixSingleFunctionTemplateSpecialization( |
| 9743 | ExprResult &SrcExpr, bool doFunctionPointerConverion, |
| 9744 | bool complain, const SourceRange& OpRangeForComplaining, |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9745 | QualType DestTypeForComplaining, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9746 | unsigned DiagIDForComplaining) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9747 | assert(SrcExpr.get()->getType() == Context.OverloadTy); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9748 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9749 | OverloadExpr::FindResult ovl = OverloadExpr::find(SrcExpr.get()); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9750 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9751 | DeclAccessPair found; |
| 9752 | ExprResult SingleFunctionExpression; |
| 9753 | if (FunctionDecl *fn = ResolveSingleFunctionTemplateSpecialization( |
| 9754 | ovl.Expression, /*complain*/ false, &found)) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9755 | if (DiagnoseUseOfDecl(fn, SrcExpr.get()->getLocStart())) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9756 | SrcExpr = ExprError(); |
| 9757 | return true; |
| 9758 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9759 | |
| 9760 | // It is only correct to resolve to an instance method if we're |
| 9761 | // resolving a form that's permitted to be a pointer to member. |
| 9762 | // Otherwise we'll end up making a bound member expression, which |
| 9763 | // is illegal in all the contexts we resolve like this. |
| 9764 | if (!ovl.HasFormOfMemberPointer && |
| 9765 | isa<CXXMethodDecl>(fn) && |
| 9766 | cast<CXXMethodDecl>(fn)->isInstance()) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9767 | if (!complain) return false; |
| 9768 | |
| 9769 | Diag(ovl.Expression->getExprLoc(), |
| 9770 | diag::err_bound_member_function) |
| 9771 | << 0 << ovl.Expression->getSourceRange(); |
| 9772 | |
| 9773 | // TODO: I believe we only end up here if there's a mix of |
| 9774 | // static and non-static candidates (otherwise the expression |
| 9775 | // would have 'bound member' type, not 'overload' type). |
| 9776 | // Ideally we would note which candidate was chosen and why |
| 9777 | // the static candidates were rejected. |
| 9778 | SrcExpr = ExprError(); |
| 9779 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9780 | } |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 9781 | |
Sylvestre Ledru | 43e3dee | 2012-07-31 06:56:50 +0000 | [diff] [blame] | 9782 | // Fix the expression to refer to 'fn'. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9783 | SingleFunctionExpression = |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9784 | Owned(FixOverloadedFunctionReference(SrcExpr.take(), found, fn)); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9785 | |
| 9786 | // If desired, do function-to-pointer decay. |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9787 | if (doFunctionPointerConverion) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9788 | SingleFunctionExpression = |
| 9789 | DefaultFunctionArrayLvalueConversion(SingleFunctionExpression.take()); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9790 | if (SingleFunctionExpression.isInvalid()) { |
| 9791 | SrcExpr = ExprError(); |
| 9792 | return true; |
| 9793 | } |
| 9794 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9795 | } |
| 9796 | |
| 9797 | if (!SingleFunctionExpression.isUsable()) { |
| 9798 | if (complain) { |
| 9799 | Diag(OpRangeForComplaining.getBegin(), DiagIDForComplaining) |
| 9800 | << ovl.Expression->getName() |
| 9801 | << DestTypeForComplaining |
| 9802 | << OpRangeForComplaining |
| 9803 | << ovl.Expression->getQualifierLoc().getSourceRange(); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9804 | NoteAllOverloadCandidates(SrcExpr.get()); |
| 9805 | |
| 9806 | SrcExpr = ExprError(); |
| 9807 | return true; |
| 9808 | } |
| 9809 | |
| 9810 | return false; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9811 | } |
| 9812 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9813 | SrcExpr = SingleFunctionExpression; |
| 9814 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9815 | } |
| 9816 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9817 | /// \brief Add a single candidate to the overload set. |
| 9818 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9819 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 9820 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9821 | ArrayRef<Expr *> Args, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9822 | OverloadCandidateSet &CandidateSet, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9823 | bool PartialOverloading, |
| 9824 | bool KnownValid) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9825 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9826 | if (isa<UsingShadowDecl>(Callee)) |
| 9827 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 9828 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9829 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9830 | if (ExplicitTemplateArgs) { |
| 9831 | assert(!KnownValid && "Explicit template arguments?"); |
| 9832 | return; |
| 9833 | } |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9834 | S.AddOverloadCandidate(Func, FoundDecl, Args, CandidateSet, false, |
| 9835 | PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9836 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9837 | } |
| 9838 | |
| 9839 | if (FunctionTemplateDecl *FuncTemplate |
| 9840 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9841 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9842 | ExplicitTemplateArgs, Args, CandidateSet); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9843 | return; |
| 9844 | } |
| 9845 | |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9846 | assert(!KnownValid && "unhandled case in overloaded call candidate"); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9847 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9848 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9849 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 9850 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9851 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9852 | ArrayRef<Expr *> Args, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9853 | OverloadCandidateSet &CandidateSet, |
| 9854 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9855 | |
| 9856 | #ifndef NDEBUG |
| 9857 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 9858 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9859 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9860 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 9861 | // and let Y be the lookup set produced by argument dependent |
| 9862 | // lookup (defined as follows). If X contains |
| 9863 | // |
| 9864 | // -- a declaration of a class member, or |
| 9865 | // |
| 9866 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9867 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9868 | // |
| 9869 | // -- a declaration that is neither a function or a function |
| 9870 | // template |
| 9871 | // |
| 9872 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9873 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9874 | if (ULE->requiresADL()) { |
| 9875 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 9876 | E = ULE->decls_end(); I != E; ++I) { |
| 9877 | assert(!(*I)->getDeclContext()->isRecord()); |
| 9878 | assert(isa<UsingShadowDecl>(*I) || |
| 9879 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 9880 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9881 | } |
| 9882 | } |
| 9883 | #endif |
| 9884 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9885 | // It would be nice to avoid this copy. |
| 9886 | TemplateArgumentListInfo TABuffer; |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 9887 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9888 | if (ULE->hasExplicitTemplateArgs()) { |
| 9889 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 9890 | ExplicitTemplateArgs = &TABuffer; |
| 9891 | } |
| 9892 | |
| 9893 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 9894 | E = ULE->decls_end(); I != E; ++I) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9895 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, Args, |
| 9896 | CandidateSet, PartialOverloading, |
| 9897 | /*KnownValid*/ true); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9898 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9899 | if (ULE->requiresADL()) |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9900 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
Richard Smith | f5cd5cc | 2012-02-25 06:24:24 +0000 | [diff] [blame] | 9901 | ULE->getExprLoc(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9902 | Args, ExplicitTemplateArgs, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 9903 | CandidateSet, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9904 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9905 | |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 9906 | /// Determine whether a declaration with the specified name could be moved into |
| 9907 | /// a different namespace. |
| 9908 | static bool canBeDeclaredInNamespace(const DeclarationName &Name) { |
| 9909 | switch (Name.getCXXOverloadedOperator()) { |
| 9910 | case OO_New: case OO_Array_New: |
| 9911 | case OO_Delete: case OO_Array_Delete: |
| 9912 | return false; |
| 9913 | |
| 9914 | default: |
| 9915 | return true; |
| 9916 | } |
| 9917 | } |
| 9918 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9919 | /// Attempt to recover from an ill-formed use of a non-dependent name in a |
| 9920 | /// template, where the non-dependent name was declared after the template |
| 9921 | /// was defined. This is common in code written for a compilers which do not |
| 9922 | /// correctly implement two-stage name lookup. |
| 9923 | /// |
| 9924 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 9925 | static bool |
| 9926 | DiagnoseTwoPhaseLookup(Sema &SemaRef, SourceLocation FnLoc, |
| 9927 | const CXXScopeSpec &SS, LookupResult &R, |
| 9928 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9929 | ArrayRef<Expr *> Args) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9930 | if (SemaRef.ActiveTemplateInstantiations.empty() || !SS.isEmpty()) |
| 9931 | return false; |
| 9932 | |
| 9933 | for (DeclContext *DC = SemaRef.CurContext; DC; DC = DC->getParent()) { |
Nick Lewycky | 5a7120c | 2012-03-14 20:41:00 +0000 | [diff] [blame] | 9934 | if (DC->isTransparentContext()) |
| 9935 | continue; |
| 9936 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9937 | SemaRef.LookupQualifiedName(R, DC); |
| 9938 | |
| 9939 | if (!R.empty()) { |
| 9940 | R.suppressDiagnostics(); |
| 9941 | |
| 9942 | if (isa<CXXRecordDecl>(DC)) { |
| 9943 | // Don't diagnose names we find in classes; we get much better |
| 9944 | // diagnostics for these from DiagnoseEmptyLookup. |
| 9945 | R.clear(); |
| 9946 | return false; |
| 9947 | } |
| 9948 | |
| 9949 | OverloadCandidateSet Candidates(FnLoc); |
| 9950 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 9951 | AddOverloadedCallCandidate(SemaRef, I.getPair(), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9952 | ExplicitTemplateArgs, Args, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9953 | Candidates, false, /*KnownValid*/ false); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9954 | |
| 9955 | OverloadCandidateSet::iterator Best; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9956 | if (Candidates.BestViableFunction(SemaRef, FnLoc, Best) != OR_Success) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9957 | // No viable functions. Don't bother the user with notes for functions |
| 9958 | // which don't work and shouldn't be found anyway. |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9959 | R.clear(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9960 | return false; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9961 | } |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9962 | |
| 9963 | // Find the namespaces where ADL would have looked, and suggest |
| 9964 | // declaring the function there instead. |
| 9965 | Sema::AssociatedNamespaceSet AssociatedNamespaces; |
| 9966 | Sema::AssociatedClassSet AssociatedClasses; |
John McCall | 42f48fb | 2012-08-24 20:38:34 +0000 | [diff] [blame] | 9967 | SemaRef.FindAssociatedClassesAndNamespaces(FnLoc, Args, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9968 | AssociatedNamespaces, |
| 9969 | AssociatedClasses); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9970 | Sema::AssociatedNamespaceSet SuggestedNamespaces; |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 9971 | if (canBeDeclaredInNamespace(R.getLookupName())) { |
| 9972 | DeclContext *Std = SemaRef.getStdNamespace(); |
| 9973 | for (Sema::AssociatedNamespaceSet::iterator |
| 9974 | it = AssociatedNamespaces.begin(), |
| 9975 | end = AssociatedNamespaces.end(); it != end; ++it) { |
| 9976 | // Never suggest declaring a function within namespace 'std'. |
| 9977 | if (Std && Std->Encloses(*it)) |
| 9978 | continue; |
Richard Smith | 19e0d95 | 2012-12-22 02:46:14 +0000 | [diff] [blame] | 9979 | |
Richard Smith | d3ff325 | 2013-06-12 22:56:54 +0000 | [diff] [blame] | 9980 | // Never suggest declaring a function within a namespace with a |
| 9981 | // reserved name, like __gnu_cxx. |
| 9982 | NamespaceDecl *NS = dyn_cast<NamespaceDecl>(*it); |
| 9983 | if (NS && |
| 9984 | NS->getQualifiedNameAsString().find("__") != std::string::npos) |
| 9985 | continue; |
| 9986 | |
| 9987 | SuggestedNamespaces.insert(*it); |
| 9988 | } |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9989 | } |
| 9990 | |
| 9991 | SemaRef.Diag(R.getNameLoc(), diag::err_not_found_by_two_phase_lookup) |
| 9992 | << R.getLookupName(); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9993 | if (SuggestedNamespaces.empty()) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9994 | SemaRef.Diag(Best->Function->getLocation(), |
| 9995 | diag::note_not_found_by_two_phase_lookup) |
| 9996 | << R.getLookupName() << 0; |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9997 | } else if (SuggestedNamespaces.size() == 1) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9998 | SemaRef.Diag(Best->Function->getLocation(), |
| 9999 | diag::note_not_found_by_two_phase_lookup) |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 10000 | << R.getLookupName() << 1 << *SuggestedNamespaces.begin(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10001 | } else { |
| 10002 | // FIXME: It would be useful to list the associated namespaces here, |
| 10003 | // but the diagnostics infrastructure doesn't provide a way to produce |
| 10004 | // a localized representation of a list of items. |
| 10005 | SemaRef.Diag(Best->Function->getLocation(), |
| 10006 | diag::note_not_found_by_two_phase_lookup) |
| 10007 | << R.getLookupName() << 2; |
| 10008 | } |
| 10009 | |
| 10010 | // Try to recover by calling this function. |
| 10011 | return true; |
| 10012 | } |
| 10013 | |
| 10014 | R.clear(); |
| 10015 | } |
| 10016 | |
| 10017 | return false; |
| 10018 | } |
| 10019 | |
| 10020 | /// Attempt to recover from ill-formed use of a non-dependent operator in a |
| 10021 | /// template, where the non-dependent operator was declared after the template |
| 10022 | /// was defined. |
| 10023 | /// |
| 10024 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 10025 | static bool |
| 10026 | DiagnoseTwoPhaseOperatorLookup(Sema &SemaRef, OverloadedOperatorKind Op, |
| 10027 | SourceLocation OpLoc, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10028 | ArrayRef<Expr *> Args) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10029 | DeclarationName OpName = |
| 10030 | SemaRef.Context.DeclarationNames.getCXXOperatorName(Op); |
| 10031 | LookupResult R(SemaRef, OpName, OpLoc, Sema::LookupOperatorName); |
| 10032 | return DiagnoseTwoPhaseLookup(SemaRef, OpLoc, CXXScopeSpec(), R, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10033 | /*ExplicitTemplateArgs=*/0, Args); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10034 | } |
| 10035 | |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 10036 | namespace { |
Richard Smith | e49ff3e | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 10037 | class BuildRecoveryCallExprRAII { |
| 10038 | Sema &SemaRef; |
| 10039 | public: |
| 10040 | BuildRecoveryCallExprRAII(Sema &S) : SemaRef(S) { |
| 10041 | assert(SemaRef.IsBuildingRecoveryCallExpr == false); |
| 10042 | SemaRef.IsBuildingRecoveryCallExpr = true; |
| 10043 | } |
| 10044 | |
| 10045 | ~BuildRecoveryCallExprRAII() { |
| 10046 | SemaRef.IsBuildingRecoveryCallExpr = false; |
| 10047 | } |
| 10048 | }; |
| 10049 | |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 10050 | } |
| 10051 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10052 | /// Attempts to recover from a call where no functions were found. |
| 10053 | /// |
| 10054 | /// Returns true if new candidates were found. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10055 | static ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 10056 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10057 | UnresolvedLookupExpr *ULE, |
| 10058 | SourceLocation LParenLoc, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10059 | llvm::MutableArrayRef<Expr *> Args, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10060 | SourceLocation RParenLoc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10061 | bool EmptyLookup, bool AllowTypoCorrection) { |
Richard Smith | e49ff3e | 2012-09-25 04:46:05 +0000 | [diff] [blame] | 10062 | // Do not try to recover if it is already building a recovery call. |
| 10063 | // This stops infinite loops for template instantiations like |
| 10064 | // |
| 10065 | // template <typename T> auto foo(T t) -> decltype(foo(t)) {} |
| 10066 | // template <typename T> auto foo(T t) -> decltype(foo(&t)) {} |
| 10067 | // |
| 10068 | if (SemaRef.IsBuildingRecoveryCallExpr) |
| 10069 | return ExprError(); |
| 10070 | BuildRecoveryCallExprRAII RCE(SemaRef); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10071 | |
| 10072 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10073 | SS.Adopt(ULE->getQualifierLoc()); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10074 | SourceLocation TemplateKWLoc = ULE->getTemplateKeywordLoc(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10075 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10076 | TemplateArgumentListInfo TABuffer; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10077 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10078 | if (ULE->hasExplicitTemplateArgs()) { |
| 10079 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 10080 | ExplicitTemplateArgs = &TABuffer; |
| 10081 | } |
| 10082 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10083 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 10084 | Sema::LookupOrdinaryName); |
Kaelyn Uhrain | 761695f | 2013-07-08 23:13:39 +0000 | [diff] [blame] | 10085 | FunctionCallFilterCCC Validator(SemaRef, Args.size(), |
| 10086 | ExplicitTemplateArgs != 0); |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10087 | NoTypoCorrectionCCC RejectAll; |
| 10088 | CorrectionCandidateCallback *CCC = AllowTypoCorrection ? |
| 10089 | (CorrectionCandidateCallback*)&Validator : |
| 10090 | (CorrectionCandidateCallback*)&RejectAll; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10091 | if (!DiagnoseTwoPhaseLookup(SemaRef, Fn->getExprLoc(), SS, R, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10092 | ExplicitTemplateArgs, Args) && |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10093 | (!EmptyLookup || |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10094 | SemaRef.DiagnoseEmptyLookup(S, SS, R, *CCC, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10095 | ExplicitTemplateArgs, Args))) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10096 | return ExprError(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10097 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10098 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 10099 | |
| 10100 | // Build an implicit member call if appropriate. Just drop the |
| 10101 | // casts and such from the call, we don't really care. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10102 | ExprResult NewFn = ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10103 | if ((*R.begin())->isCXXClassMember()) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10104 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, |
| 10105 | R, ExplicitTemplateArgs); |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 10106 | else if (ExplicitTemplateArgs || TemplateKWLoc.isValid()) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 10107 | NewFn = SemaRef.BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 10108 | ExplicitTemplateArgs); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10109 | else |
| 10110 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 10111 | |
| 10112 | if (NewFn.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10113 | return ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10114 | |
| 10115 | // This shouldn't cause an infinite loop because we're giving it |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10116 | // an expression with viable lookup results, which should never |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10117 | // end up here. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10118 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, NewFn.take(), LParenLoc, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10119 | MultiExprArg(Args.data(), Args.size()), |
| 10120 | RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10121 | } |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 10122 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10123 | /// \brief Constructs and populates an OverloadedCandidateSet from |
| 10124 | /// the given function. |
| 10125 | /// \returns true when an the ExprResult output parameter has been set. |
| 10126 | bool Sema::buildOverloadedCallSet(Scope *S, Expr *Fn, |
| 10127 | UnresolvedLookupExpr *ULE, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10128 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10129 | SourceLocation RParenLoc, |
| 10130 | OverloadCandidateSet *CandidateSet, |
| 10131 | ExprResult *Result) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10132 | #ifndef NDEBUG |
| 10133 | if (ULE->requiresADL()) { |
| 10134 | // To do ADL, we must have found an unqualified name. |
| 10135 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 10136 | |
| 10137 | // We don't perform ADL for implicit declarations of builtins. |
| 10138 | // Verify that this was correctly set up. |
| 10139 | FunctionDecl *F; |
| 10140 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 10141 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 10142 | F->getBuiltinID() && F->isImplicit()) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 10143 | llvm_unreachable("performing ADL for builtin"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10144 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10145 | // We don't perform ADL in C. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10146 | assert(getLangOpts().CPlusPlus && "ADL enabled in C"); |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 10147 | } |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10148 | #endif |
| 10149 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10150 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10151 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) { |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10152 | *Result = ExprError(); |
| 10153 | return true; |
| 10154 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 10155 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10156 | // Add the functions denoted by the callee to the set of candidate |
| 10157 | // functions, including those from argument-dependent lookup. |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10158 | AddOverloadedCallCandidates(ULE, Args, *CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10159 | |
| 10160 | // If we found nothing, try to recover. |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10161 | // BuildRecoveryCallExpr diagnoses the error itself, so we just bail |
| 10162 | // out if it fails. |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10163 | if (CandidateSet->empty()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10164 | // In Microsoft mode, if we are inside a template class member function then |
| 10165 | // create a type dependent CallExpr. The goal is to postpone name lookup |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 10166 | // to instantiation time to be able to search into type dependent base |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10167 | // classes. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10168 | if (getLangOpts().MicrosoftMode && CurContext->isDependentContext() && |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 10169 | (isa<FunctionDecl>(CurContext) || isa<CXXRecordDecl>(CurContext))) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10170 | CallExpr *CE = new (Context) CallExpr(Context, Fn, Args, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10171 | Context.DependentTy, VK_RValue, |
| 10172 | RParenLoc); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10173 | CE->setTypeDependent(true); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10174 | *Result = Owned(CE); |
| 10175 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 10176 | } |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10177 | return false; |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 10178 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10179 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10180 | UnbridgedCasts.restore(); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10181 | return false; |
| 10182 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10183 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10184 | /// FinishOverloadedCallExpr - given an OverloadCandidateSet, builds and returns |
| 10185 | /// the completed call expression. If overload resolution fails, emits |
| 10186 | /// diagnostics and returns ExprError() |
| 10187 | static ExprResult FinishOverloadedCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
| 10188 | UnresolvedLookupExpr *ULE, |
| 10189 | SourceLocation LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10190 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10191 | SourceLocation RParenLoc, |
| 10192 | Expr *ExecConfig, |
| 10193 | OverloadCandidateSet *CandidateSet, |
| 10194 | OverloadCandidateSet::iterator *Best, |
| 10195 | OverloadingResult OverloadResult, |
| 10196 | bool AllowTypoCorrection) { |
| 10197 | if (CandidateSet->empty()) |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10198 | return BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10199 | RParenLoc, /*EmptyLookup=*/true, |
| 10200 | AllowTypoCorrection); |
| 10201 | |
| 10202 | switch (OverloadResult) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10203 | case OR_Success: { |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10204 | FunctionDecl *FDecl = (*Best)->Function; |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10205 | SemaRef.CheckUnresolvedLookupAccess(ULE, (*Best)->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 10206 | if (SemaRef.DiagnoseUseOfDecl(FDecl, ULE->getNameLoc())) |
| 10207 | return ExprError(); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10208 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10209 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
| 10210 | ExecConfig); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10211 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10212 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10213 | case OR_No_Viable_Function: { |
| 10214 | // Try to recover by looking for viable functions which the user might |
| 10215 | // have meant to call. |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10216 | ExprResult Recovery = BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10217 | Args, RParenLoc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 10218 | /*EmptyLookup=*/false, |
| 10219 | AllowTypoCorrection); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10220 | if (!Recovery.isInvalid()) |
| 10221 | return Recovery; |
| 10222 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10223 | SemaRef.Diag(Fn->getLocStart(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10224 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10225 | << ULE->getName() << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10226 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, Args); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10227 | break; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10228 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10229 | |
| 10230 | case OR_Ambiguous: |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10231 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10232 | << ULE->getName() << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10233 | CandidateSet->NoteCandidates(SemaRef, OCD_ViableCandidates, Args); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10234 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10235 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10236 | case OR_Deleted: { |
| 10237 | SemaRef.Diag(Fn->getLocStart(), diag::err_ovl_deleted_call) |
| 10238 | << (*Best)->Function->isDeleted() |
| 10239 | << ULE->getName() |
| 10240 | << SemaRef.getDeletedOrUnavailableSuffix((*Best)->Function) |
| 10241 | << Fn->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10242 | CandidateSet->NoteCandidates(SemaRef, OCD_AllCandidates, Args); |
Argyrios Kyrtzidis | 0d579b6 | 2011-11-04 15:58:13 +0000 | [diff] [blame] | 10243 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10244 | // We emitted an error for the unvailable/deleted function call but keep |
| 10245 | // the call in the AST. |
| 10246 | FunctionDecl *FDecl = (*Best)->Function; |
| 10247 | Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10248 | return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, |
| 10249 | ExecConfig); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10250 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10251 | } |
| 10252 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 10253 | // Overload resolution failed. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 10254 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 10255 | } |
| 10256 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10257 | /// BuildOverloadedCallExpr - Given the call expression that calls Fn |
| 10258 | /// (which eventually refers to the declaration Func) and the call |
| 10259 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 10260 | /// to a specific function. If overload resolution succeeds, returns |
| 10261 | /// the call expression produced by overload resolution. |
| 10262 | /// Otherwise, emits diagnostics and returns ExprError. |
| 10263 | ExprResult Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, |
| 10264 | UnresolvedLookupExpr *ULE, |
| 10265 | SourceLocation LParenLoc, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10266 | MultiExprArg Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10267 | SourceLocation RParenLoc, |
| 10268 | Expr *ExecConfig, |
| 10269 | bool AllowTypoCorrection) { |
| 10270 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
| 10271 | ExprResult result; |
| 10272 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10273 | if (buildOverloadedCallSet(S, Fn, ULE, Args, LParenLoc, &CandidateSet, |
| 10274 | &result)) |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10275 | return result; |
| 10276 | |
| 10277 | OverloadCandidateSet::iterator Best; |
| 10278 | OverloadingResult OverloadResult = |
| 10279 | CandidateSet.BestViableFunction(*this, Fn->getLocStart(), Best); |
| 10280 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10281 | return FinishOverloadedCallExpr(*this, S, Fn, ULE, LParenLoc, Args, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 10282 | RParenLoc, ExecConfig, &CandidateSet, |
| 10283 | &Best, OverloadResult, |
| 10284 | AllowTypoCorrection); |
| 10285 | } |
| 10286 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10287 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 10288 | return Functions.size() > 1 || |
| 10289 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 10290 | } |
| 10291 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10292 | /// \brief Create a unary operation that may resolve to an overloaded |
| 10293 | /// operator. |
| 10294 | /// |
| 10295 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 10296 | /// |
| 10297 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 10298 | /// operator. |
| 10299 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 10300 | /// \param Fns The set of non-member functions that will be |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10301 | /// considered by overload resolution. The caller needs to build this |
| 10302 | /// set based on the context using, e.g., |
| 10303 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 10304 | /// set should not contain any member functions; those will be added |
| 10305 | /// by CreateOverloadedUnaryOp(). |
| 10306 | /// |
James Dennett | 8da1687 | 2012-06-22 10:32:46 +0000 | [diff] [blame] | 10307 | /// \param Input The input argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10308 | ExprResult |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10309 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 10310 | const UnresolvedSetImpl &Fns, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10311 | Expr *Input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10312 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10313 | |
| 10314 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 10315 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 10316 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10317 | // TODO: provide better source location info. |
| 10318 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10319 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10320 | if (checkPlaceholderForOverload(*this, Input)) |
| 10321 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10322 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10323 | Expr *Args[2] = { Input, 0 }; |
| 10324 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10325 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10326 | // For post-increment and post-decrement, add the implicit '0' as |
| 10327 | // the second argument, so that we know this is a post-increment or |
| 10328 | // post-decrement. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10329 | if (Opc == UO_PostInc || Opc == UO_PostDec) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10330 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 10331 | Args[1] = IntegerLiteral::Create(Context, Zero, Context.IntTy, |
| 10332 | SourceLocation()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10333 | NumArgs = 2; |
| 10334 | } |
| 10335 | |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10336 | ArrayRef<Expr *> ArgsArray(Args, NumArgs); |
| 10337 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10338 | if (Input->isTypeDependent()) { |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10339 | if (Fns.empty()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10340 | return Owned(new (Context) UnaryOperator(Input, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10341 | Opc, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10342 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10343 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 10344 | OpLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10345 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10346 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10347 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 10348 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10349 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10350 | /*ADL*/ true, IsOverloaded(Fns), |
| 10351 | Fns.begin(), Fns.end()); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10352 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, ArgsArray, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10353 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10354 | VK_RValue, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10355 | OpLoc, false)); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10356 | } |
| 10357 | |
| 10358 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10359 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10360 | |
| 10361 | // Add the candidates from the given function set. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10362 | AddFunctionCandidates(Fns, ArgsArray, CandidateSet, false); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10363 | |
| 10364 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10365 | AddMemberOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10366 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10367 | // Add candidates from ADL. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10368 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, OpLoc, |
| 10369 | ArgsArray, /*ExplicitTemplateArgs*/ 0, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10370 | CandidateSet); |
| 10371 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10372 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10373 | AddBuiltinOperatorCandidates(Op, OpLoc, ArgsArray, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10374 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10375 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10376 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10377 | // Perform overload resolution. |
| 10378 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10379 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10380 | case OR_Success: { |
| 10381 | // We found a built-in operator or an overloaded operator. |
| 10382 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10383 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10384 | if (FnDecl) { |
| 10385 | // We matched an overloaded operator. Build a call to that |
| 10386 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10387 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10388 | // Convert the arguments. |
| 10389 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10390 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10391 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10392 | ExprResult InputRes = |
| 10393 | PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 10394 | Best->FoundDecl, Method); |
| 10395 | if (InputRes.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10396 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10397 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10398 | } else { |
| 10399 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10400 | ExprResult InputInit |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 10401 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10402 | Context, |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 10403 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10404 | SourceLocation(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10405 | Input); |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 10406 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10407 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10408 | Input = InputInit.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10409 | } |
| 10410 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10411 | // Determine the result type. |
| 10412 | QualType ResultTy = FnDecl->getResultType(); |
| 10413 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10414 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10415 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10416 | // Build the actual expression node. |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10417 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10418 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10419 | if (FnExpr.isInvalid()) |
| 10420 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10421 | |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 10422 | Args[0] = Input; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10423 | CallExpr *TheCall = |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10424 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), ArgsArray, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10425 | ResultTy, VK, OpLoc, false); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10426 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10427 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 10428 | FnDecl)) |
| 10429 | return ExprError(); |
| 10430 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10431 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10432 | } else { |
| 10433 | // We matched a built-in operator. Convert the arguments, then |
| 10434 | // break out so that we will build the appropriate built-in |
| 10435 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10436 | ExprResult InputRes = |
| 10437 | PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 10438 | Best->Conversions[0], AA_Passing); |
| 10439 | if (InputRes.isInvalid()) |
| 10440 | return ExprError(); |
| 10441 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10442 | break; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10443 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10444 | } |
| 10445 | |
| 10446 | case OR_No_Viable_Function: |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10447 | // This is an erroneous use of an operator which can be overloaded by |
| 10448 | // a non-member function. Check for non-member operators which were |
| 10449 | // defined too late to be candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10450 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, ArgsArray)) |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10451 | // FIXME: Recover by calling the found function. |
| 10452 | return ExprError(); |
| 10453 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10454 | // No viable function; fall through to handling this as a |
| 10455 | // built-in operator, which will produce an error message for us. |
| 10456 | break; |
| 10457 | |
| 10458 | case OR_Ambiguous: |
| 10459 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 10460 | << UnaryOperator::getOpcodeStr(Opc) |
| 10461 | << Input->getType() |
| 10462 | << Input->getSourceRange(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10463 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, ArgsArray, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10464 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
| 10465 | return ExprError(); |
| 10466 | |
| 10467 | case OR_Deleted: |
| 10468 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 10469 | << Best->Function->isDeleted() |
| 10470 | << UnaryOperator::getOpcodeStr(Opc) |
| 10471 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 10472 | << Input->getSourceRange(); |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10473 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, ArgsArray, |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 10474 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10475 | return ExprError(); |
| 10476 | } |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10477 | |
| 10478 | // Either we found no viable overloaded operator or we matched a |
| 10479 | // built-in operator. In either case, fall through to trying to |
| 10480 | // build a built-in operation. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10481 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10482 | } |
| 10483 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10484 | /// \brief Create a binary operation that may resolve to an overloaded |
| 10485 | /// operator. |
| 10486 | /// |
| 10487 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 10488 | /// |
| 10489 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 10490 | /// operator. |
| 10491 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 10492 | /// \param Fns The set of non-member functions that will be |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10493 | /// considered by overload resolution. The caller needs to build this |
| 10494 | /// set based on the context using, e.g., |
| 10495 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 10496 | /// set should not contain any member functions; those will be added |
| 10497 | /// by CreateOverloadedBinOp(). |
| 10498 | /// |
| 10499 | /// \param LHS Left-hand argument. |
| 10500 | /// \param RHS Right-hand argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10501 | ExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10502 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10503 | unsigned OpcIn, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10504 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10505 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10506 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10507 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10508 | |
| 10509 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 10510 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 10511 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 10512 | |
| 10513 | // If either side is type-dependent, create an appropriate dependent |
| 10514 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10515 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10516 | if (Fns.empty()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10517 | // If there are no functions to store, just build a dependent |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10518 | // BinaryOperator or CompoundAssignment. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10519 | if (Opc <= BO_Assign || Opc > BO_OrAssign) |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10520 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10521 | Context.DependentTy, |
| 10522 | VK_RValue, OK_Ordinary, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10523 | OpLoc, |
| 10524 | FPFeatures.fp_contract)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10525 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10526 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 10527 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10528 | VK_LValue, |
| 10529 | OK_Ordinary, |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10530 | Context.DependentTy, |
| 10531 | Context.DependentTy, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10532 | OpLoc, |
| 10533 | FPFeatures.fp_contract)); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10534 | } |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10535 | |
| 10536 | // FIXME: save results of ADL from here? |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10537 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10538 | // TODO: provide better source location info in DNLoc component. |
| 10539 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10540 | UnresolvedLookupExpr *Fn |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10541 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
| 10542 | NestedNameSpecifierLoc(), OpNameInfo, |
| 10543 | /*ADL*/ true, IsOverloaded(Fns), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10544 | Fns.begin(), Fns.end()); |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10545 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, Args, |
| 10546 | Context.DependentTy, VK_RValue, |
| 10547 | OpLoc, FPFeatures.fp_contract)); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10548 | } |
| 10549 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10550 | // Always do placeholder-like conversions on the RHS. |
| 10551 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 10552 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10553 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 10554 | // Do placeholder-like conversion on the LHS; note that we should |
| 10555 | // not get here with a PseudoObject LHS. |
| 10556 | assert(Args[0]->getObjectKind() != OK_ObjCProperty); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10557 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 10558 | return ExprError(); |
| 10559 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 10560 | // If this is the assignment operator, we only perform overload resolution |
| 10561 | // if the left-hand side is a class or enumeration type. This is actually |
| 10562 | // a hack. The standard requires that we do overload resolution between the |
| 10563 | // various built-in candidates, but as DR507 points out, this can lead to |
| 10564 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 10565 | // Note that we go the traditional code path for compound assignment forms. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10566 | if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10567 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10568 | |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10569 | // If this is the .* operator, which is not overloadable, just |
| 10570 | // create a built-in binary operator. |
| 10571 | if (Opc == BO_PtrMemD) |
| 10572 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 10573 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10574 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10575 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10576 | |
| 10577 | // Add the candidates from the given function set. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10578 | AddFunctionCandidates(Fns, Args, CandidateSet, false); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10579 | |
| 10580 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10581 | AddMemberOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10582 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10583 | // Add candidates from ADL. |
| 10584 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10585 | OpLoc, Args, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10586 | /*ExplicitTemplateArgs*/ 0, |
| 10587 | CandidateSet); |
| 10588 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10589 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10590 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10591 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10592 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10593 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10594 | // Perform overload resolution. |
| 10595 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10596 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 10597 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10598 | // We found a built-in operator or an overloaded operator. |
| 10599 | FunctionDecl *FnDecl = Best->Function; |
| 10600 | |
| 10601 | if (FnDecl) { |
| 10602 | // We matched an overloaded operator. Build a call to that |
| 10603 | // operator. |
| 10604 | |
| 10605 | // Convert the arguments. |
| 10606 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10607 | // Best->Access is only meaningful for class members. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10608 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10609 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10610 | ExprResult Arg1 = |
| 10611 | PerformCopyInitialization( |
| 10612 | InitializedEntity::InitializeParameter(Context, |
| 10613 | FnDecl->getParamDecl(0)), |
| 10614 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10615 | if (Arg1.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10616 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10617 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10618 | ExprResult Arg0 = |
| 10619 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 10620 | Best->FoundDecl, Method); |
| 10621 | if (Arg0.isInvalid()) |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10622 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10623 | Args[0] = Arg0.takeAs<Expr>(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10624 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10625 | } else { |
| 10626 | // Convert the arguments. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10627 | ExprResult Arg0 = PerformCopyInitialization( |
| 10628 | InitializedEntity::InitializeParameter(Context, |
| 10629 | FnDecl->getParamDecl(0)), |
| 10630 | SourceLocation(), Owned(Args[0])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10631 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10632 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10633 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10634 | ExprResult Arg1 = |
| 10635 | PerformCopyInitialization( |
| 10636 | InitializedEntity::InitializeParameter(Context, |
| 10637 | FnDecl->getParamDecl(1)), |
| 10638 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10639 | if (Arg1.isInvalid()) |
| 10640 | return ExprError(); |
| 10641 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 10642 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10643 | } |
| 10644 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10645 | // Determine the result type. |
| 10646 | QualType ResultTy = FnDecl->getResultType(); |
| 10647 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10648 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10649 | |
| 10650 | // Build the actual expression node. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10651 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10652 | Best->FoundDecl, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10653 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10654 | if (FnExpr.isInvalid()) |
| 10655 | return ExprError(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10656 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10657 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10658 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10659 | Args, ResultTy, VK, OpLoc, |
| 10660 | FPFeatures.fp_contract); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10661 | |
| 10662 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 10663 | FnDecl)) |
| 10664 | return ExprError(); |
| 10665 | |
Nick Lewycky | 9b7ea0d | 2013-01-24 02:03:08 +0000 | [diff] [blame] | 10666 | ArrayRef<const Expr *> ArgsArray(Args, 2); |
| 10667 | // Cut off the implicit 'this'. |
| 10668 | if (isa<CXXMethodDecl>(FnDecl)) |
| 10669 | ArgsArray = ArgsArray.slice(1); |
| 10670 | checkCall(FnDecl, ArgsArray, 0, isa<CXXMethodDecl>(FnDecl), OpLoc, |
| 10671 | TheCall->getSourceRange(), VariadicDoesNotApply); |
| 10672 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10673 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10674 | } else { |
| 10675 | // We matched a built-in operator. Convert the arguments, then |
| 10676 | // break out so that we will build the appropriate built-in |
| 10677 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10678 | ExprResult ArgsRes0 = |
| 10679 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 10680 | Best->Conversions[0], AA_Passing); |
| 10681 | if (ArgsRes0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10682 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10683 | Args[0] = ArgsRes0.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10684 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10685 | ExprResult ArgsRes1 = |
| 10686 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 10687 | Best->Conversions[1], AA_Passing); |
| 10688 | if (ArgsRes1.isInvalid()) |
| 10689 | return ExprError(); |
| 10690 | Args[1] = ArgsRes1.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10691 | break; |
| 10692 | } |
| 10693 | } |
| 10694 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10695 | case OR_No_Viable_Function: { |
| 10696 | // C++ [over.match.oper]p9: |
| 10697 | // If the operator is the operator , [...] and there are no |
| 10698 | // viable functions, then the operator is assumed to be the |
| 10699 | // built-in operator and interpreted according to clause 5. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10700 | if (Opc == BO_Comma) |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10701 | break; |
| 10702 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10703 | // For class as left operand for assignment or compound assigment |
| 10704 | // operator do not fall through to handling in built-in, but report that |
| 10705 | // no overloaded assignment operator found |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10706 | ExprResult Result = ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10707 | if (Args[0]->getType()->isRecordType() && |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10708 | Opc >= BO_Assign && Opc <= BO_OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 10709 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 10710 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10711 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Eli Friedman | 3f93d4c | 2013-08-28 20:35:35 +0000 | [diff] [blame] | 10712 | if (Args[0]->getType()->isIncompleteType()) { |
| 10713 | Diag(OpLoc, diag::note_assign_lhs_incomplete) |
| 10714 | << Args[0]->getType() |
| 10715 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10716 | } |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10717 | } else { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10718 | // This is an erroneous use of an operator which can be overloaded by |
| 10719 | // a non-member function. Check for non-member operators which were |
| 10720 | // defined too late to be candidates. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10721 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, Args)) |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10722 | // FIXME: Recover by calling the found function. |
| 10723 | return ExprError(); |
| 10724 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10725 | // No viable function; try to create a built-in operation, which will |
| 10726 | // produce an error. Then, show the non-viable candidates. |
| 10727 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 10728 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10729 | assert(Result.isInvalid() && |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10730 | "C++ binary operator overloading is missing candidates!"); |
| 10731 | if (Result.isInvalid()) |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10732 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10733 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10734 | return Result; |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10735 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10736 | |
| 10737 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10738 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_binary) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10739 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10740 | << Args[0]->getType() << Args[1]->getType() |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10741 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10742 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10743 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10744 | return ExprError(); |
| 10745 | |
| 10746 | case OR_Deleted: |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10747 | if (isImplicitlyDeleted(Best->Function)) { |
| 10748 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
| 10749 | Diag(OpLoc, diag::err_ovl_deleted_special_oper) |
Richard Smith | 0f46e64 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 10750 | << Context.getRecordType(Method->getParent()) |
| 10751 | << getSpecialMember(Method); |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 10752 | |
Richard Smith | 0f46e64 | 2012-12-28 12:23:24 +0000 | [diff] [blame] | 10753 | // The user probably meant to call this special member. Just |
| 10754 | // explain why it's deleted. |
| 10755 | NoteDeletedFunction(Method); |
| 10756 | return ExprError(); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10757 | } else { |
| 10758 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 10759 | << Best->Function->isDeleted() |
| 10760 | << BinaryOperator::getOpcodeStr(Opc) |
| 10761 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 10762 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10763 | } |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10764 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 10765 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10766 | return ExprError(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 10767 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10768 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10769 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10770 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10771 | } |
| 10772 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10773 | ExprResult |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10774 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 10775 | SourceLocation RLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10776 | Expr *Base, Expr *Idx) { |
| 10777 | Expr *Args[2] = { Base, Idx }; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10778 | DeclarationName OpName = |
| 10779 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 10780 | |
| 10781 | // If either side is type-dependent, create an appropriate dependent |
| 10782 | // expression. |
| 10783 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 10784 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10785 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10786 | // CHECKME: no 'operator' keyword? |
| 10787 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
| 10788 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10789 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 10790 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10791 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10792 | /*ADL*/ true, /*Overloaded*/ false, |
| 10793 | UnresolvedSetIterator(), |
| 10794 | UnresolvedSetIterator()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 10795 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10796 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10797 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10798 | Args, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10799 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10800 | VK_RValue, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10801 | RLoc, false)); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10802 | } |
| 10803 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10804 | // Handle placeholders on both operands. |
| 10805 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 10806 | return ExprError(); |
| 10807 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 10808 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10809 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10810 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10811 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10812 | |
| 10813 | // Subscript can only be overloaded as a member function. |
| 10814 | |
| 10815 | // Add operator candidates that are member functions. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10816 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10817 | |
| 10818 | // Add builtin operator candidates. |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 10819 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, CandidateSet); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10820 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10821 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10822 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10823 | // Perform overload resolution. |
| 10824 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10825 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10826 | case OR_Success: { |
| 10827 | // We found a built-in operator or an overloaded operator. |
| 10828 | FunctionDecl *FnDecl = Best->Function; |
| 10829 | |
| 10830 | if (FnDecl) { |
| 10831 | // We matched an overloaded operator. Build a call to that |
| 10832 | // operator. |
| 10833 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10834 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10835 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10836 | // Convert the arguments. |
| 10837 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10838 | ExprResult Arg0 = |
| 10839 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 10840 | Best->FoundDecl, Method); |
| 10841 | if (Arg0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10842 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10843 | Args[0] = Arg0.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10844 | |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10845 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10846 | ExprResult InputInit |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10847 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10848 | Context, |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10849 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10850 | SourceLocation(), |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10851 | Owned(Args[1])); |
| 10852 | if (InputInit.isInvalid()) |
| 10853 | return ExprError(); |
| 10854 | |
| 10855 | Args[1] = InputInit.takeAs<Expr>(); |
| 10856 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10857 | // Determine the result type |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10858 | QualType ResultTy = FnDecl->getResultType(); |
| 10859 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10860 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10861 | |
| 10862 | // Build the actual expression node. |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10863 | DeclarationNameInfo OpLocInfo(OpName, LLoc); |
| 10864 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10865 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 10866 | Best->FoundDecl, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10867 | HadMultipleCandidates, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10868 | OpLocInfo.getLoc(), |
| 10869 | OpLocInfo.getInfo()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10870 | if (FnExpr.isInvalid()) |
| 10871 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10872 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10873 | CXXOperatorCallExpr *TheCall = |
| 10874 | new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10875 | FnExpr.take(), Args, |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 10876 | ResultTy, VK, RLoc, |
| 10877 | false); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10878 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10879 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10880 | FnDecl)) |
| 10881 | return ExprError(); |
| 10882 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10883 | return MaybeBindToTemporary(TheCall); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10884 | } else { |
| 10885 | // We matched a built-in operator. Convert the arguments, then |
| 10886 | // break out so that we will build the appropriate built-in |
| 10887 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10888 | ExprResult ArgsRes0 = |
| 10889 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 10890 | Best->Conversions[0], AA_Passing); |
| 10891 | if (ArgsRes0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10892 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10893 | Args[0] = ArgsRes0.take(); |
| 10894 | |
| 10895 | ExprResult ArgsRes1 = |
| 10896 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 10897 | Best->Conversions[1], AA_Passing); |
| 10898 | if (ArgsRes1.isInvalid()) |
| 10899 | return ExprError(); |
| 10900 | Args[1] = ArgsRes1.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10901 | |
| 10902 | break; |
| 10903 | } |
| 10904 | } |
| 10905 | |
| 10906 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10907 | if (CandidateSet.empty()) |
| 10908 | Diag(LLoc, diag::err_ovl_no_oper) |
| 10909 | << Args[0]->getType() << /*subscript*/ 0 |
| 10910 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10911 | else |
| 10912 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 10913 | << Args[0]->getType() |
| 10914 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10915 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10916 | "[]", LLoc); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10917 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10918 | } |
| 10919 | |
| 10920 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10921 | Diag(LLoc, diag::err_ovl_ambiguous_oper_binary) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10922 | << "[]" |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10923 | << Args[0]->getType() << Args[1]->getType() |
| 10924 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10925 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10926 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10927 | return ExprError(); |
| 10928 | |
| 10929 | case OR_Deleted: |
| 10930 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 10931 | << Best->Function->isDeleted() << "[]" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 10932 | << getDeletedOrUnavailableSuffix(Best->Function) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10933 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10934 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10935 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10936 | return ExprError(); |
| 10937 | } |
| 10938 | |
| 10939 | // We matched a built-in operator; build it. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10940 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10941 | } |
| 10942 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10943 | /// BuildCallToMemberFunction - Build a call to a member |
| 10944 | /// function. MemExpr is the expression that refers to the member |
| 10945 | /// function (and includes the object parameter), Args/NumArgs are the |
| 10946 | /// arguments to the function call (not including the object |
| 10947 | /// parameter). The caller needs to validate that the member |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10948 | /// expression refers to a non-static member function or an overloaded |
| 10949 | /// member function. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10950 | ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10951 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10952 | SourceLocation LParenLoc, |
| 10953 | MultiExprArg Args, |
| 10954 | SourceLocation RParenLoc) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10955 | assert(MemExprE->getType() == Context.BoundMemberTy || |
| 10956 | MemExprE->getType() == Context.OverloadTy); |
| 10957 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10958 | // Dig out the member expression. This holds both the object |
| 10959 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10960 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10961 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10962 | // Determine whether this is a call to a pointer-to-member function. |
| 10963 | if (BinaryOperator *op = dyn_cast<BinaryOperator>(NakedMemExpr)) { |
| 10964 | assert(op->getType() == Context.BoundMemberTy); |
| 10965 | assert(op->getOpcode() == BO_PtrMemD || op->getOpcode() == BO_PtrMemI); |
| 10966 | |
| 10967 | QualType fnType = |
| 10968 | op->getRHS()->getType()->castAs<MemberPointerType>()->getPointeeType(); |
| 10969 | |
| 10970 | const FunctionProtoType *proto = fnType->castAs<FunctionProtoType>(); |
| 10971 | QualType resultType = proto->getCallResultType(Context); |
| 10972 | ExprValueKind valueKind = Expr::getValueKindForType(proto->getResultType()); |
| 10973 | |
| 10974 | // Check that the object type isn't more qualified than the |
| 10975 | // member function we're calling. |
| 10976 | Qualifiers funcQuals = Qualifiers::fromCVRMask(proto->getTypeQuals()); |
| 10977 | |
| 10978 | QualType objectType = op->getLHS()->getType(); |
| 10979 | if (op->getOpcode() == BO_PtrMemI) |
| 10980 | objectType = objectType->castAs<PointerType>()->getPointeeType(); |
| 10981 | Qualifiers objectQuals = objectType.getQualifiers(); |
| 10982 | |
| 10983 | Qualifiers difference = objectQuals - funcQuals; |
| 10984 | difference.removeObjCGCAttr(); |
| 10985 | difference.removeAddressSpace(); |
| 10986 | if (difference) { |
| 10987 | std::string qualsString = difference.getAsString(); |
| 10988 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
| 10989 | << fnType.getUnqualifiedType() |
| 10990 | << qualsString |
| 10991 | << (qualsString.find(' ') == std::string::npos ? 1 : 2); |
| 10992 | } |
| 10993 | |
| 10994 | CXXMemberCallExpr *call |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10995 | = new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10996 | resultType, valueKind, RParenLoc); |
| 10997 | |
| 10998 | if (CheckCallReturnType(proto->getResultType(), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10999 | op->getRHS()->getLocStart(), |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11000 | call, 0)) |
| 11001 | return ExprError(); |
| 11002 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11003 | if (ConvertArgumentsForCall(call, op, 0, proto, Args, RParenLoc)) |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11004 | return ExprError(); |
| 11005 | |
Richard Trieu | e2a90b8 | 2013-06-22 02:30:38 +0000 | [diff] [blame] | 11006 | if (CheckOtherCall(call, proto)) |
| 11007 | return ExprError(); |
| 11008 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11009 | return MaybeBindToTemporary(call); |
| 11010 | } |
| 11011 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11012 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11013 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11014 | return ExprError(); |
| 11015 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11016 | MemberExpr *MemExpr; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11017 | CXXMethodDecl *Method = 0; |
John McCall | bb6fb46 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 11018 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11019 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11020 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 11021 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11022 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11023 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11024 | Qualifier = MemExpr->getQualifier(); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11025 | UnbridgedCasts.restore(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11026 | } else { |
| 11027 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 11028 | Qualifier = UnresExpr->getQualifier(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11029 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11030 | QualType ObjectType = UnresExpr->getBaseType(); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11031 | Expr::Classification ObjectClassification |
| 11032 | = UnresExpr->isArrow()? Expr::Classification::makeSimpleLValue() |
| 11033 | : UnresExpr->getBase()->Classify(Context); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11034 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11035 | // Add overload candidates |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11036 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11037 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11038 | // FIXME: avoid copy. |
| 11039 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 11040 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 11041 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11042 | TemplateArgs = &TemplateArgsBuffer; |
| 11043 | } |
| 11044 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11045 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 11046 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 11047 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11048 | NamedDecl *Func = *I; |
| 11049 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 11050 | if (isa<UsingShadowDecl>(Func)) |
| 11051 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 11052 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11053 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 11054 | // Microsoft supports direct constructor calls. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11055 | if (getLangOpts().MicrosoftExt && isa<CXXConstructorDecl>(Func)) { |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11056 | AddOverloadCandidate(cast<CXXConstructorDecl>(Func), I.getPair(), |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11057 | Args, CandidateSet); |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 11058 | } else if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 11059 | // If explicit template arguments were provided, we can't call a |
| 11060 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11061 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 11062 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11063 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11064 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11065 | ObjectClassification, Args, CandidateSet, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11066 | /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11067 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11068 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11069 | I.getPair(), ActingDC, TemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11070 | ObjectType, ObjectClassification, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11071 | Args, CandidateSet, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 11072 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11073 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 11074 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11075 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11076 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 11077 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11078 | UnbridgedCasts.restore(); |
| 11079 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11080 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11081 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getLocStart(), |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 11082 | Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11083 | case OR_Success: |
| 11084 | Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11085 | FoundDecl = Best->FoundDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11086 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 11087 | if (DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc())) |
| 11088 | return ExprError(); |
Faisal Vali | d570a92 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 11089 | // If FoundDecl is different from Method (such as if one is a template |
| 11090 | // and the other a specialization), make sure DiagnoseUseOfDecl is |
| 11091 | // called on both. |
| 11092 | // FIXME: This would be more comprehensively addressed by modifying |
| 11093 | // DiagnoseUseOfDecl to accept both the FoundDecl and the decl |
| 11094 | // being used. |
| 11095 | if (Method != FoundDecl.getDecl() && |
| 11096 | DiagnoseUseOfDecl(Method, UnresExpr->getNameLoc())) |
| 11097 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11098 | break; |
| 11099 | |
| 11100 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11101 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11102 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 11103 | << DeclName << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11104 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11105 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11106 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11107 | |
| 11108 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11109 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 11110 | << DeclName << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11111 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11112 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11113 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11114 | |
| 11115 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11116 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11117 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11118 | << DeclName |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11119 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11120 | << MemExprE->getSourceRange(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11121 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11122 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11123 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11124 | } |
| 11125 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11126 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11127 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11128 | // If overload resolution picked a static member, build a |
| 11129 | // non-member call based on that function. |
| 11130 | if (Method->isStatic()) { |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11131 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, Args, |
| 11132 | RParenLoc); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11133 | } |
| 11134 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11135 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11136 | } |
| 11137 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11138 | QualType ResultType = Method->getResultType(); |
| 11139 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 11140 | ResultType = ResultType.getNonLValueExprType(Context); |
| 11141 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11142 | assert(Method && "Member call to something that isn't a method?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11143 | CXXMemberCallExpr *TheCall = |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11144 | new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11145 | ResultType, VK, RParenLoc); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11146 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 11147 | // Check for a valid return type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11148 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11149 | TheCall, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11150 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11151 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11152 | // Convert the object argument (for a non-static member function call). |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11153 | // We only need to do this if there was actually an overload; otherwise |
| 11154 | // it was done at lookup. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11155 | if (!Method->isStatic()) { |
| 11156 | ExprResult ObjectArg = |
| 11157 | PerformObjectArgumentInitialization(MemExpr->getBase(), Qualifier, |
| 11158 | FoundDecl, Method); |
| 11159 | if (ObjectArg.isInvalid()) |
| 11160 | return ExprError(); |
| 11161 | MemExpr->setBase(ObjectArg.take()); |
| 11162 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11163 | |
| 11164 | // Convert the rest of the arguments |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11165 | const FunctionProtoType *Proto = |
| 11166 | Method->getType()->getAs<FunctionProtoType>(); |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11167 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11168 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11169 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11170 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11171 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11172 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11173 | if (CheckFunctionCall(Method, TheCall, Proto)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11174 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 11175 | |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11176 | if ((isa<CXXConstructorDecl>(CurContext) || |
| 11177 | isa<CXXDestructorDecl>(CurContext)) && |
| 11178 | TheCall->getMethodDecl()->isPure()) { |
| 11179 | const CXXMethodDecl *MD = TheCall->getMethodDecl(); |
| 11180 | |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 11181 | if (isa<CXXThisExpr>(MemExpr->getBase()->IgnoreParenCasts())) { |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11182 | Diag(MemExpr->getLocStart(), |
| 11183 | diag::warn_call_to_pure_virtual_member_function_from_ctor_dtor) |
| 11184 | << MD->getDeclName() << isa<CXXDestructorDecl>(CurContext) |
| 11185 | << MD->getParent()->getDeclName(); |
| 11186 | |
| 11187 | Diag(MD->getLocStart(), diag::note_previous_decl) << MD->getDeclName(); |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 11188 | } |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 11189 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11190 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 11191 | } |
| 11192 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11193 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 11194 | /// type (C++ [over.call.object]), which can end up invoking an |
| 11195 | /// overloaded function call operator (@c operator()) or performing a |
| 11196 | /// user-defined conversion on the object argument. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 11197 | ExprResult |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11198 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 11199 | SourceLocation LParenLoc, |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11200 | MultiExprArg Args, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11201 | SourceLocation RParenLoc) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11202 | if (checkPlaceholderForOverload(*this, Obj)) |
| 11203 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11204 | ExprResult Object = Owned(Obj); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11205 | |
| 11206 | UnbridgedCastsSet UnbridgedCasts; |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11207 | if (checkArgPlaceholdersForOverload(*this, Args, UnbridgedCasts)) |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11208 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11209 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11210 | assert(Object.get()->getType()->isRecordType() && "Requires object type argument"); |
| 11211 | const RecordType *Record = Object.get()->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11212 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11213 | // C++ [over.call.object]p1: |
| 11214 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 11215 | // 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] | 11216 | // candidate functions includes at least the function call |
| 11217 | // operators of T. The function call operators of T are obtained by |
| 11218 | // ordinary lookup of the name operator() in the context of |
| 11219 | // (E).operator(). |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11220 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 11221 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11222 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11223 | if (RequireCompleteType(LParenLoc, Object.get()->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11224 | diag::err_incomplete_object_call, Object.get())) |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11225 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11226 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 11227 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 11228 | LookupQualifiedName(R, Record->getDecl()); |
| 11229 | R.suppressDiagnostics(); |
| 11230 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 11231 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 11232 | Oper != OperEnd; ++Oper) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11233 | AddMethodCandidate(Oper.getPair(), Object.get()->getType(), |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11234 | Object.get()->Classify(Context), |
| 11235 | Args, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 11236 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 11237 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11238 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11239 | // C++ [over.call.object]p2: |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11240 | // In addition, for each (non-explicit in C++0x) conversion function |
| 11241 | // declared in T of the form |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11242 | // |
| 11243 | // operator conversion-type-id () cv-qualifier; |
| 11244 | // |
| 11245 | // where cv-qualifier is the same cv-qualification as, or a |
| 11246 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 11247 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 11248 | // R", or the type "reference to pointer to function of |
| 11249 | // (P1,...,Pn) returning R", or the type "reference to function |
| 11250 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11251 | // is also considered as a candidate function. Similarly, |
| 11252 | // surrogate call functions are added to the set of candidate |
| 11253 | // functions for each conversion function declared in an |
| 11254 | // accessible base class provided the function is not hidden |
| 11255 | // within T by another intervening declaration. |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 11256 | std::pair<CXXRecordDecl::conversion_iterator, |
| 11257 | CXXRecordDecl::conversion_iterator> Conversions |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 11258 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 11259 | for (CXXRecordDecl::conversion_iterator |
| 11260 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11261 | NamedDecl *D = *I; |
| 11262 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 11263 | if (isa<UsingShadowDecl>(D)) |
| 11264 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11265 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 11266 | // Skip over templated conversion functions; they aren't |
| 11267 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11268 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 11269 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 11270 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11271 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11272 | if (!Conv->isExplicit()) { |
| 11273 | // Strip the reference type (if any) and then the pointer type (if |
| 11274 | // any) to get down to what might be a function type. |
| 11275 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 11276 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 11277 | ConvType = ConvPtrType->getPointeeType(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11278 | |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11279 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
| 11280 | { |
| 11281 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11282 | Object.get(), Args, CandidateSet); |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 11283 | } |
| 11284 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11285 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11286 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11287 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11288 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11289 | // Perform overload resolution. |
| 11290 | OverloadCandidateSet::iterator Best; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11291 | switch (CandidateSet.BestViableFunction(*this, Object.get()->getLocStart(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11292 | Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11293 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11294 | // Overload resolution succeeded; we'll build the appropriate call |
| 11295 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11296 | break; |
| 11297 | |
| 11298 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11299 | if (CandidateSet.empty()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11300 | Diag(Object.get()->getLocStart(), diag::err_ovl_no_oper) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11301 | << Object.get()->getType() << /*call*/ 1 |
| 11302 | << Object.get()->getSourceRange(); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11303 | else |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11304 | Diag(Object.get()->getLocStart(), |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 11305 | diag::err_ovl_no_viable_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11306 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11307 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11308 | break; |
| 11309 | |
| 11310 | case OR_Ambiguous: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11311 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11312 | diag::err_ovl_ambiguous_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11313 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11314 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11315 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11316 | |
| 11317 | case OR_Deleted: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11318 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11319 | diag::err_ovl_deleted_object_call) |
| 11320 | << Best->Function->isDeleted() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11321 | << Object.get()->getType() |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11322 | << getDeletedOrUnavailableSuffix(Best->Function) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11323 | << Object.get()->getSourceRange(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11324 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11325 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11326 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11327 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 11328 | if (Best == CandidateSet.end()) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11329 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11330 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11331 | UnbridgedCasts.restore(); |
| 11332 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11333 | if (Best->Function == 0) { |
| 11334 | // Since there is no function declaration, this is one of the |
| 11335 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11336 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11337 | = cast<CXXConversionDecl>( |
| 11338 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 11339 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11340 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 11341 | if (DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc)) |
| 11342 | return ExprError(); |
Faisal Vali | d570a92 | 2013-06-15 11:54:37 +0000 | [diff] [blame] | 11343 | assert(Conv == Best->FoundDecl.getDecl() && |
| 11344 | "Found Decl & conversion-to-functionptr should be same, right?!"); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11345 | // We selected one of the surrogate functions that converts the |
| 11346 | // object parameter to a function pointer. Perform the conversion |
| 11347 | // on the object argument, then let ActOnCallExpr finish the job. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11348 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 11349 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 11350 | // and then call it. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11351 | ExprResult Call = BuildCXXMemberCallExpr(Object.get(), Best->FoundDecl, |
| 11352 | Conv, HadMultipleCandidates); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 11353 | if (Call.isInvalid()) |
| 11354 | return ExprError(); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 11355 | // Record usage of conversion in an implicit cast. |
| 11356 | Call = Owned(ImplicitCastExpr::Create(Context, Call.get()->getType(), |
| 11357 | CK_UserDefinedConversion, |
| 11358 | Call.get(), 0, VK_RValue)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11359 | |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11360 | return ActOnCallExpr(S, Call.get(), LParenLoc, Args, RParenLoc); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11361 | } |
| 11362 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11363 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 11364 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 11365 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 11366 | // that calls this method, using Object for the implicit object |
| 11367 | // parameter and passing along the remaining arguments. |
| 11368 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Nico Weber | e0ff690 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 11369 | |
| 11370 | // An error diagnostic has already been printed when parsing the declaration. |
Nico Weber | 1a52a4d | 2012-11-09 08:38:04 +0000 | [diff] [blame] | 11371 | if (Method->isInvalidDecl()) |
Nico Weber | e0ff690 | 2012-11-09 06:06:14 +0000 | [diff] [blame] | 11372 | return ExprError(); |
| 11373 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11374 | const FunctionProtoType *Proto = |
| 11375 | Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11376 | |
| 11377 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11378 | unsigned NumArgsToCheck = Args.size(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11379 | |
| 11380 | // Build the full argument list for the method call (the |
| 11381 | // implicit object parameter is placed at the beginning of the |
| 11382 | // list). |
| 11383 | Expr **MethodArgs; |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11384 | if (Args.size() < NumArgsInProto) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11385 | NumArgsToCheck = NumArgsInProto; |
| 11386 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 11387 | } else { |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11388 | MethodArgs = new Expr*[Args.size() + 1]; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11389 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11390 | MethodArgs[0] = Object.get(); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11391 | for (unsigned ArgIdx = 0, e = Args.size(); ArgIdx != e; ++ArgIdx) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11392 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11393 | |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11394 | DeclarationNameInfo OpLocInfo( |
| 11395 | Context.DeclarationNames.getCXXOperatorName(OO_Call), LParenLoc); |
| 11396 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LParenLoc, RParenLoc)); |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11397 | ExprResult NewFn = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11398 | HadMultipleCandidates, |
| 11399 | OpLocInfo.getLoc(), |
| 11400 | OpLocInfo.getInfo()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11401 | if (NewFn.isInvalid()) |
| 11402 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11403 | |
| 11404 | // Once we've built TheCall, all of the expressions are properly |
| 11405 | // owned. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11406 | QualType ResultTy = Method->getResultType(); |
| 11407 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11408 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11409 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11410 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11411 | new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn.take(), |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11412 | llvm::makeArrayRef(MethodArgs, Args.size()+1), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 11413 | ResultTy, VK, RParenLoc, false); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11414 | delete [] MethodArgs; |
| 11415 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11416 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall, |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 11417 | Method)) |
| 11418 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11419 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11420 | // We may have default arguments. If so, we need to allocate more |
| 11421 | // slots in the call for them. |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11422 | if (Args.size() < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 11423 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11424 | else if (Args.size() > NumArgsInProto) |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11425 | NumArgsToCheck = NumArgsInProto; |
| 11426 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11427 | bool IsError = false; |
| 11428 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11429 | // Initialize the implicit object parameter. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11430 | ExprResult ObjRes = |
| 11431 | PerformObjectArgumentInitialization(Object.get(), /*Qualifier=*/0, |
| 11432 | Best->FoundDecl, Method); |
| 11433 | if (ObjRes.isInvalid()) |
| 11434 | IsError = true; |
| 11435 | else |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11436 | Object = ObjRes; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11437 | TheCall->setArg(0, Object.take()); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11438 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11439 | // Check the argument types. |
| 11440 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11441 | Expr *Arg; |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11442 | if (i < Args.size()) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11443 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11444 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11445 | // Pass the argument. |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11446 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11447 | ExprResult InputInit |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11448 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 11449 | Context, |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11450 | Method->getParamDecl(i)), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11451 | SourceLocation(), Arg); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11452 | |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11453 | IsError |= InputInit.isInvalid(); |
| 11454 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11455 | } else { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11456 | ExprResult DefArg |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 11457 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 11458 | if (DefArg.isInvalid()) { |
| 11459 | IsError = true; |
| 11460 | break; |
| 11461 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11462 | |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 11463 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11464 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11465 | |
| 11466 | TheCall->setArg(i + 1, Arg); |
| 11467 | } |
| 11468 | |
| 11469 | // If this is a variadic call, handle args passed through "...". |
| 11470 | if (Proto->isVariadic()) { |
| 11471 | // Promote the arguments (C99 6.5.2.2p7). |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11472 | for (unsigned i = NumArgsInProto, e = Args.size(); i < e; i++) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11473 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0); |
| 11474 | IsError |= Arg.isInvalid(); |
| 11475 | TheCall->setArg(i + 1, Arg.take()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11476 | } |
| 11477 | } |
| 11478 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11479 | if (IsError) return true; |
| 11480 | |
Dmitri Gribenko | 7297a2e | 2013-05-09 23:32:58 +0000 | [diff] [blame] | 11481 | DiagnoseSentinelCalls(Method, LParenLoc, Args); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11482 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11483 | if (CheckFunctionCall(Method, TheCall, Proto)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 11484 | return true; |
| 11485 | |
John McCall | 182f709 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 11486 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11487 | } |
| 11488 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11489 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11490 | /// (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] | 11491 | /// @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] | 11492 | ExprResult |
Kaelyn Uhrain | baaeb85 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 11493 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc, |
| 11494 | bool *NoArrowOperatorFound) { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11495 | assert(Base->getType()->isRecordType() && |
| 11496 | "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11497 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11498 | if (checkPlaceholderForOverload(*this, Base)) |
| 11499 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11500 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11501 | SourceLocation Loc = Base->getExprLoc(); |
| 11502 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11503 | // C++ [over.ref]p1: |
| 11504 | // |
| 11505 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 11506 | // for a class object x of type T if T::operator->() exists and if |
| 11507 | // the operator is selected as the best match function by the |
| 11508 | // overload resolution mechanism (13.3). |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11509 | DeclarationName OpName = |
| 11510 | Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11511 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 11512 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11513 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11514 | if (RequireCompleteType(Loc, Base->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11515 | diag::err_typecheck_incomplete_tag, Base)) |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 11516 | return ExprError(); |
| 11517 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 11518 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 11519 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 11520 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 11521 | |
| 11522 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11523 | Oper != OperEnd; ++Oper) { |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11524 | AddMethodCandidate(Oper.getPair(), Base->getType(), Base->Classify(Context), |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 11525 | None, CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11526 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11527 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11528 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11529 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11530 | // Perform overload resolution. |
| 11531 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11532 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11533 | case OR_Success: |
| 11534 | // Overload resolution succeeded; we'll build the call below. |
| 11535 | break; |
| 11536 | |
| 11537 | case OR_No_Viable_Function: |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11538 | if (CandidateSet.empty()) { |
| 11539 | QualType BaseType = Base->getType(); |
Kaelyn Uhrain | baaeb85 | 2013-07-31 17:38:24 +0000 | [diff] [blame] | 11540 | if (NoArrowOperatorFound) { |
| 11541 | // Report this specific error to the caller instead of emitting a |
| 11542 | // diagnostic, as requested. |
| 11543 | *NoArrowOperatorFound = true; |
| 11544 | return ExprError(); |
| 11545 | } |
Kaelyn Uhrain | d422434 | 2013-07-15 19:54:54 +0000 | [diff] [blame] | 11546 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
| 11547 | << BaseType << Base->getSourceRange(); |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11548 | if (BaseType->isRecordType() && !BaseType->isPointerType()) { |
Kaelyn Uhrain | d422434 | 2013-07-15 19:54:54 +0000 | [diff] [blame] | 11549 | Diag(OpLoc, diag::note_typecheck_member_reference_suggestion) |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11550 | << FixItHint::CreateReplacement(OpLoc, "."); |
Kaelyn Uhrain | 45c3ba7 | 2013-07-11 22:38:30 +0000 | [diff] [blame] | 11551 | } |
| 11552 | } else |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11553 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11554 | << "operator->" << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11555 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11556 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11557 | |
| 11558 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 11559 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 11560 | << "->" << Base->getType() << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11561 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11562 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11563 | |
| 11564 | case OR_Deleted: |
| 11565 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 11566 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11567 | << "->" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11568 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11569 | << Base->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11570 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11571 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11572 | } |
| 11573 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11574 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
| 11575 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11576 | // Convert the object parameter. |
| 11577 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11578 | ExprResult BaseResult = |
| 11579 | PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 11580 | Best->FoundDecl, Method); |
| 11581 | if (BaseResult.isInvalid()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11582 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11583 | Base = BaseResult.take(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 11584 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11585 | // Build the operator call. |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11586 | ExprResult FnExpr = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, |
Argyrios Kyrtzidis | 46e7547 | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11587 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11588 | if (FnExpr.isInvalid()) |
| 11589 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11590 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11591 | QualType ResultTy = Method->getResultType(); |
| 11592 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11593 | ResultTy = ResultTy.getNonLValueExprType(Context); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11594 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11595 | new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr.take(), |
Lang Hames | be9af12 | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 11596 | Base, ResultTy, VK, OpLoc, false); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 11597 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11598 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 11599 | Method)) |
| 11600 | return ExprError(); |
Eli Friedman | d593190 | 2011-04-04 01:18:25 +0000 | [diff] [blame] | 11601 | |
| 11602 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11603 | } |
| 11604 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11605 | /// BuildLiteralOperatorCall - Build a UserDefinedLiteral by creating a call to |
| 11606 | /// a literal operator described by the provided lookup results. |
| 11607 | ExprResult Sema::BuildLiteralOperatorCall(LookupResult &R, |
| 11608 | DeclarationNameInfo &SuffixInfo, |
| 11609 | ArrayRef<Expr*> Args, |
| 11610 | SourceLocation LitEndLoc, |
| 11611 | TemplateArgumentListInfo *TemplateArgs) { |
| 11612 | SourceLocation UDSuffixLoc = SuffixInfo.getCXXLiteralOperatorNameLoc(); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11613 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11614 | OverloadCandidateSet CandidateSet(UDSuffixLoc); |
| 11615 | AddFunctionCandidates(R.asUnresolvedSet(), Args, CandidateSet, true, |
| 11616 | TemplateArgs); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11617 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11618 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11619 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11620 | // Perform overload resolution. This will usually be trivial, but might need |
| 11621 | // to perform substitutions for a literal operator template. |
| 11622 | OverloadCandidateSet::iterator Best; |
| 11623 | switch (CandidateSet.BestViableFunction(*this, UDSuffixLoc, Best)) { |
| 11624 | case OR_Success: |
| 11625 | case OR_Deleted: |
| 11626 | break; |
| 11627 | |
| 11628 | case OR_No_Viable_Function: |
| 11629 | Diag(UDSuffixLoc, diag::err_ovl_no_viable_function_in_call) |
| 11630 | << R.getLookupName(); |
| 11631 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
| 11632 | return ExprError(); |
| 11633 | |
| 11634 | case OR_Ambiguous: |
| 11635 | Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) << R.getLookupName(); |
| 11636 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
| 11637 | return ExprError(); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11638 | } |
| 11639 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11640 | FunctionDecl *FD = Best->Function; |
Nick Lewycky | f5a6aef | 2013-02-07 05:08:22 +0000 | [diff] [blame] | 11641 | ExprResult Fn = CreateFunctionRefExpr(*this, FD, Best->FoundDecl, |
| 11642 | HadMultipleCandidates, |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11643 | SuffixInfo.getLoc(), |
| 11644 | SuffixInfo.getInfo()); |
| 11645 | if (Fn.isInvalid()) |
| 11646 | return true; |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11647 | |
| 11648 | // Check the argument types. This should almost always be a no-op, except |
| 11649 | // that array-to-pointer decay is applied to string literals. |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11650 | Expr *ConvArgs[2]; |
Richard Smith | 958ba64 | 2013-05-05 15:51:06 +0000 | [diff] [blame] | 11651 | for (unsigned ArgIdx = 0, N = Args.size(); ArgIdx != N; ++ArgIdx) { |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11652 | ExprResult InputInit = PerformCopyInitialization( |
| 11653 | InitializedEntity::InitializeParameter(Context, FD->getParamDecl(ArgIdx)), |
| 11654 | SourceLocation(), Args[ArgIdx]); |
| 11655 | if (InputInit.isInvalid()) |
| 11656 | return true; |
| 11657 | ConvArgs[ArgIdx] = InputInit.take(); |
| 11658 | } |
| 11659 | |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11660 | QualType ResultTy = FD->getResultType(); |
| 11661 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11662 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11663 | |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11664 | UserDefinedLiteral *UDL = |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 11665 | new (Context) UserDefinedLiteral(Context, Fn.take(), |
| 11666 | llvm::makeArrayRef(ConvArgs, Args.size()), |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11667 | ResultTy, VK, LitEndLoc, UDSuffixLoc); |
| 11668 | |
| 11669 | if (CheckCallReturnType(FD->getResultType(), UDSuffixLoc, UDL, FD)) |
| 11670 | return ExprError(); |
| 11671 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11672 | if (CheckFunctionCall(FD, UDL, NULL)) |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11673 | return ExprError(); |
| 11674 | |
| 11675 | return MaybeBindToTemporary(UDL); |
| 11676 | } |
| 11677 | |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11678 | /// Build a call to 'begin' or 'end' for a C++11 for-range statement. If the |
| 11679 | /// given LookupResult is non-empty, it is assumed to describe a member which |
| 11680 | /// will be invoked. Otherwise, the function will be found via argument |
| 11681 | /// dependent lookup. |
| 11682 | /// CallExpr is set to a valid expression and FRS_Success returned on success, |
| 11683 | /// otherwise CallExpr is set to ExprError() and some non-success value |
| 11684 | /// is returned. |
| 11685 | Sema::ForRangeStatus |
| 11686 | Sema::BuildForRangeBeginEndCall(Scope *S, SourceLocation Loc, |
| 11687 | SourceLocation RangeLoc, VarDecl *Decl, |
| 11688 | BeginEndFunction BEF, |
| 11689 | const DeclarationNameInfo &NameInfo, |
| 11690 | LookupResult &MemberLookup, |
| 11691 | OverloadCandidateSet *CandidateSet, |
| 11692 | Expr *Range, ExprResult *CallExpr) { |
| 11693 | CandidateSet->clear(); |
| 11694 | if (!MemberLookup.empty()) { |
| 11695 | ExprResult MemberRef = |
| 11696 | BuildMemberReferenceExpr(Range, Range->getType(), Loc, |
| 11697 | /*IsPtr=*/false, CXXScopeSpec(), |
| 11698 | /*TemplateKWLoc=*/SourceLocation(), |
| 11699 | /*FirstQualifierInScope=*/0, |
| 11700 | MemberLookup, |
| 11701 | /*TemplateArgs=*/0); |
| 11702 | if (MemberRef.isInvalid()) { |
| 11703 | *CallExpr = ExprError(); |
| 11704 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11705 | << RangeLoc << BEF << Range->getType(); |
| 11706 | return FRS_DiagnosticIssued; |
| 11707 | } |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 11708 | *CallExpr = ActOnCallExpr(S, MemberRef.get(), Loc, None, Loc, 0); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11709 | if (CallExpr->isInvalid()) { |
| 11710 | *CallExpr = ExprError(); |
| 11711 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11712 | << RangeLoc << BEF << Range->getType(); |
| 11713 | return FRS_DiagnosticIssued; |
| 11714 | } |
| 11715 | } else { |
| 11716 | UnresolvedSet<0> FoundNames; |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11717 | UnresolvedLookupExpr *Fn = |
| 11718 | UnresolvedLookupExpr::Create(Context, /*NamingClass=*/0, |
| 11719 | NestedNameSpecifierLoc(), NameInfo, |
| 11720 | /*NeedsADL=*/true, /*Overloaded=*/false, |
Richard Smith | b1502bc | 2012-10-18 17:56:02 +0000 | [diff] [blame] | 11721 | FoundNames.begin(), FoundNames.end()); |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11722 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11723 | bool CandidateSetError = buildOverloadedCallSet(S, Fn, Fn, Range, Loc, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11724 | CandidateSet, CallExpr); |
| 11725 | if (CandidateSet->empty() || CandidateSetError) { |
| 11726 | *CallExpr = ExprError(); |
| 11727 | return FRS_NoViableFunction; |
| 11728 | } |
| 11729 | OverloadCandidateSet::iterator Best; |
| 11730 | OverloadingResult OverloadResult = |
| 11731 | CandidateSet->BestViableFunction(*this, Fn->getLocStart(), Best); |
| 11732 | |
| 11733 | if (OverloadResult == OR_No_Viable_Function) { |
| 11734 | *CallExpr = ExprError(); |
| 11735 | return FRS_NoViableFunction; |
| 11736 | } |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11737 | *CallExpr = FinishOverloadedCallExpr(*this, S, Fn, Fn, Loc, Range, |
Sam Panzer | e1715b6 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 11738 | Loc, 0, CandidateSet, &Best, |
| 11739 | OverloadResult, |
| 11740 | /*AllowTypoCorrection=*/false); |
| 11741 | if (CallExpr->isInvalid() || OverloadResult != OR_Success) { |
| 11742 | *CallExpr = ExprError(); |
| 11743 | Diag(Range->getLocStart(), diag::note_in_for_range) |
| 11744 | << RangeLoc << BEF << Range->getType(); |
| 11745 | return FRS_DiagnosticIssued; |
| 11746 | } |
| 11747 | } |
| 11748 | return FRS_Success; |
| 11749 | } |
| 11750 | |
| 11751 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11752 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 11753 | /// a C++ overloaded function (possibly with some parentheses and |
| 11754 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 11755 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 11756 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 11757 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11758 | FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11759 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11760 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 11761 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11762 | if (SubExpr == PE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11763 | return PE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11764 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11765 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11766 | } |
| 11767 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11768 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11769 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 11770 | Found, Fn); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11771 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11772 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 11773 | "Implicit cast type cannot be determined from overload"); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 11774 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11775 | if (SubExpr == ICE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11776 | return ICE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11777 | |
| 11778 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 11779 | ICE->getCastKind(), |
| 11780 | SubExpr, 0, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 11781 | ICE->getValueKind()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11782 | } |
| 11783 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11784 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11785 | assert(UnOp->getOpcode() == UO_AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11786 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 11787 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 11788 | if (Method->isStatic()) { |
| 11789 | // Do nothing: static member functions aren't any different |
| 11790 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11791 | } else { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11792 | // Fix the sub expression, which really has to be an |
| 11793 | // UnresolvedLookupExpr holding an overloaded member function |
| 11794 | // or template. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11795 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 11796 | Found, Fn); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11797 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11798 | return UnOp; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11799 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11800 | assert(isa<DeclRefExpr>(SubExpr) |
| 11801 | && "fixed to something other than a decl ref"); |
| 11802 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 11803 | && "fixed to a member ref with no nested name qualifier"); |
| 11804 | |
| 11805 | // We have taken the address of a pointer to member |
| 11806 | // function. Perform the computation here so that we get the |
| 11807 | // appropriate pointer to member type. |
| 11808 | QualType ClassType |
| 11809 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 11810 | QualType MemPtrType |
| 11811 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 11812 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11813 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, MemPtrType, |
| 11814 | VK_RValue, OK_Ordinary, |
| 11815 | UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 11816 | } |
| 11817 | } |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11818 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 11819 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11820 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11821 | return UnOp; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11822 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11823 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11824 | Context.getPointerType(SubExpr->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11825 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11826 | UnOp->getOperatorLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11827 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11828 | |
| 11829 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11830 | // FIXME: avoid copy. |
| 11831 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11832 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11833 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11834 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11835 | } |
| 11836 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11837 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 11838 | ULE->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11839 | ULE->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11840 | Fn, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 11841 | /*enclosing*/ false, // FIXME? |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11842 | ULE->getNameLoc(), |
| 11843 | Fn->getType(), |
| 11844 | VK_LValue, |
| 11845 | Found.getDecl(), |
| 11846 | TemplateArgs); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 11847 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11848 | DRE->setHadMultipleCandidates(ULE->getNumDecls() > 1); |
| 11849 | return DRE; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11850 | } |
| 11851 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11852 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11853 | // FIXME: avoid copy. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11854 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 11855 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 11856 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11857 | TemplateArgs = &TemplateArgsBuffer; |
| 11858 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11859 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11860 | Expr *Base; |
| 11861 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11862 | // If we're filling in a static method where we used to have an |
| 11863 | // implicit member access, rewrite to a simple decl ref. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11864 | if (MemExpr->isImplicitAccess()) { |
| 11865 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11866 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 11867 | MemExpr->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11868 | MemExpr->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11869 | Fn, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 11870 | /*enclosing*/ false, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11871 | MemExpr->getMemberLoc(), |
| 11872 | Fn->getType(), |
| 11873 | VK_LValue, |
| 11874 | Found.getDecl(), |
| 11875 | TemplateArgs); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 11876 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11877 | DRE->setHadMultipleCandidates(MemExpr->getNumDecls() > 1); |
| 11878 | return DRE; |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 11879 | } else { |
| 11880 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 11881 | if (MemExpr->getQualifier()) |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 11882 | Loc = MemExpr->getQualifierLoc().getBeginLoc(); |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 11883 | CheckCXXThisCapture(Loc); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 11884 | Base = new (Context) CXXThisExpr(Loc, |
| 11885 | MemExpr->getBaseType(), |
| 11886 | /*isImplicit=*/true); |
| 11887 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11888 | } else |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11889 | Base = MemExpr->getBase(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11890 | |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11891 | ExprValueKind valueKind; |
| 11892 | QualType type; |
| 11893 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 11894 | valueKind = VK_LValue; |
| 11895 | type = Fn->getType(); |
| 11896 | } else { |
| 11897 | valueKind = VK_RValue; |
| 11898 | type = Context.BoundMemberTy; |
| 11899 | } |
| 11900 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11901 | MemberExpr *ME = MemberExpr::Create(Context, Base, |
| 11902 | MemExpr->isArrow(), |
| 11903 | MemExpr->getQualifierLoc(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11904 | MemExpr->getTemplateKeywordLoc(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11905 | Fn, |
| 11906 | Found, |
| 11907 | MemExpr->getMemberNameInfo(), |
| 11908 | TemplateArgs, |
| 11909 | type, valueKind, OK_Ordinary); |
| 11910 | ME->setHadMultipleCandidates(true); |
Richard Smith | 4a03022 | 2012-11-14 07:06:31 +0000 | [diff] [blame] | 11911 | MarkMemberReferenced(ME); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11912 | return ME; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11913 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11914 | |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11915 | llvm_unreachable("Invalid reference to overloaded function"); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11916 | } |
| 11917 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11918 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11919 | DeclAccessPair Found, |
| 11920 | FunctionDecl *Fn) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11921 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 11922 | } |
| 11923 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 11924 | } // end namespace clang |