Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1 | //===--- SemaOverload.cpp - C++ Overloading ---------------------*- C++ -*-===// |
| 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 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Lookup.h" |
| 16 | #include "clang/Sema/Initialization.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Template.h" |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 18 | #include "clang/Sema/TemplateDeduction.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 22 | #include "clang/AST/CXXInheritance.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 24 | #include "clang/AST/Expr.h" |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeOrdering.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | 661b493 | 2010-09-12 04:28:07 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseSet.h" |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 32 | #include <algorithm> |
| 33 | |
| 34 | namespace clang { |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 35 | using namespace sema; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 36 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 37 | /// A convenience routine for creating a decayed reference to a |
| 38 | /// function. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 39 | static ExprResult |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 40 | CreateFunctionRefExpr(Sema &S, FunctionDecl *Fn, bool HadMultipleCandidates, |
Douglas Gregor | 5b8968c | 2011-07-15 16:25:15 +0000 | [diff] [blame] | 41 | SourceLocation Loc = SourceLocation(), |
| 42 | const DeclarationNameLoc &LocInfo = DeclarationNameLoc()){ |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 43 | DeclRefExpr *DRE = new (S.Context) DeclRefExpr(Fn, Fn->getType(), |
| 44 | VK_LValue, Loc, LocInfo); |
| 45 | if (HadMultipleCandidates) |
| 46 | DRE->setHadMultipleCandidates(true); |
| 47 | ExprResult E = S.Owned(DRE); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 48 | E = S.DefaultFunctionArrayConversion(E.take()); |
| 49 | if (E.isInvalid()) |
| 50 | return ExprError(); |
| 51 | return move(E); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 52 | } |
| 53 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 54 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 55 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 56 | StandardConversionSequence &SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 57 | bool CStyle, |
| 58 | bool AllowObjCWritebackConversion); |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 59 | |
| 60 | static bool IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 61 | QualType &ToType, |
| 62 | bool InOverloadResolution, |
| 63 | StandardConversionSequence &SCS, |
| 64 | bool CStyle); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 65 | static OverloadingResult |
| 66 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 67 | UserDefinedConversionSequence& User, |
| 68 | OverloadCandidateSet& Conversions, |
| 69 | bool AllowExplicit); |
| 70 | |
| 71 | |
| 72 | static ImplicitConversionSequence::CompareKind |
| 73 | CompareStandardConversionSequences(Sema &S, |
| 74 | const StandardConversionSequence& SCS1, |
| 75 | const StandardConversionSequence& SCS2); |
| 76 | |
| 77 | static ImplicitConversionSequence::CompareKind |
| 78 | CompareQualificationConversions(Sema &S, |
| 79 | const StandardConversionSequence& SCS1, |
| 80 | const StandardConversionSequence& SCS2); |
| 81 | |
| 82 | static ImplicitConversionSequence::CompareKind |
| 83 | CompareDerivedToBaseConversions(Sema &S, |
| 84 | const StandardConversionSequence& SCS1, |
| 85 | const StandardConversionSequence& SCS2); |
| 86 | |
| 87 | |
| 88 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 89 | /// GetConversionCategory - Retrieve the implicit conversion |
| 90 | /// category corresponding to the given implicit conversion kind. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | ImplicitConversionCategory |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 92 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 93 | static const ImplicitConversionCategory |
| 94 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 95 | ICC_Identity, |
| 96 | ICC_Lvalue_Transformation, |
| 97 | ICC_Lvalue_Transformation, |
| 98 | ICC_Lvalue_Transformation, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 99 | ICC_Identity, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 100 | ICC_Qualification_Adjustment, |
| 101 | ICC_Promotion, |
| 102 | ICC_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 103 | ICC_Promotion, |
| 104 | ICC_Conversion, |
| 105 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 106 | ICC_Conversion, |
| 107 | ICC_Conversion, |
| 108 | ICC_Conversion, |
| 109 | ICC_Conversion, |
| 110 | ICC_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 111 | ICC_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 112 | ICC_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 113 | ICC_Conversion, |
| 114 | ICC_Conversion, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 115 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 116 | ICC_Conversion |
| 117 | }; |
| 118 | return Category[(int)Kind]; |
| 119 | } |
| 120 | |
| 121 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 122 | /// corresponding to the given implicit conversion kind. |
| 123 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 124 | static const ImplicitConversionRank |
| 125 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 126 | ICR_Exact_Match, |
| 127 | ICR_Exact_Match, |
| 128 | ICR_Exact_Match, |
| 129 | ICR_Exact_Match, |
| 130 | ICR_Exact_Match, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 131 | ICR_Exact_Match, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 132 | ICR_Promotion, |
| 133 | ICR_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 134 | ICR_Promotion, |
| 135 | ICR_Conversion, |
| 136 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 137 | ICR_Conversion, |
| 138 | ICR_Conversion, |
| 139 | ICR_Conversion, |
| 140 | ICR_Conversion, |
| 141 | ICR_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 142 | ICR_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 143 | ICR_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 144 | ICR_Conversion, |
| 145 | ICR_Conversion, |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 146 | ICR_Complex_Real_Conversion, |
| 147 | ICR_Conversion, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 148 | ICR_Conversion, |
| 149 | ICR_Writeback_Conversion |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 150 | }; |
| 151 | return Rank[(int)Kind]; |
| 152 | } |
| 153 | |
| 154 | /// GetImplicitConversionName - Return the name of this kind of |
| 155 | /// implicit conversion. |
| 156 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | 2550d70 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 157 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 158 | "No conversion", |
| 159 | "Lvalue-to-rvalue", |
| 160 | "Array-to-pointer", |
| 161 | "Function-to-pointer", |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 162 | "Noreturn adjustment", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 163 | "Qualification", |
| 164 | "Integral promotion", |
| 165 | "Floating point promotion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 166 | "Complex promotion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 167 | "Integral conversion", |
| 168 | "Floating conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 169 | "Complex conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 170 | "Floating-integral conversion", |
| 171 | "Pointer conversion", |
| 172 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 173 | "Boolean conversion", |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 174 | "Compatible-types conversion", |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 175 | "Derived-to-base conversion", |
| 176 | "Vector conversion", |
| 177 | "Vector splat", |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 178 | "Complex-real conversion", |
| 179 | "Block Pointer conversion", |
| 180 | "Transparent Union Conversion" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 181 | "Writeback conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 182 | }; |
| 183 | return Name[Kind]; |
| 184 | } |
| 185 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 186 | /// StandardConversionSequence - Set the standard conversion |
| 187 | /// sequence to the identity conversion. |
| 188 | void StandardConversionSequence::setAsIdentityConversion() { |
| 189 | First = ICK_Identity; |
| 190 | Second = ICK_Identity; |
| 191 | Third = ICK_Identity; |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 192 | DeprecatedStringLiteralToCharPtr = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 193 | QualificationIncludesObjCLifetime = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 194 | ReferenceBinding = false; |
| 195 | DirectBinding = false; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 196 | IsLvalueReference = true; |
| 197 | BindsToFunctionLvalue = false; |
| 198 | BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 199 | BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 200 | ObjCLifetimeConversionBinding = false; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 201 | CopyConstructor = 0; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 204 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 205 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 206 | /// implicit conversions. |
| 207 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 208 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 209 | if (GetConversionRank(First) > Rank) |
| 210 | Rank = GetConversionRank(First); |
| 211 | if (GetConversionRank(Second) > Rank) |
| 212 | Rank = GetConversionRank(Second); |
| 213 | if (GetConversionRank(Third) > Rank) |
| 214 | Rank = GetConversionRank(Third); |
| 215 | return Rank; |
| 216 | } |
| 217 | |
| 218 | /// isPointerConversionToBool - Determines whether this conversion is |
| 219 | /// 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] | 220 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 221 | /// (C++ 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 222 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 223 | // Note that FromType has not necessarily been transformed by the |
| 224 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 225 | // check for their presence as well as checking whether FromType is |
| 226 | // a pointer. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 227 | if (getToType(1)->isBooleanType() && |
John McCall | ddb0ce7 | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 228 | (getFromType()->isPointerType() || |
| 229 | getFromType()->isObjCObjectPointerType() || |
| 230 | getFromType()->isBlockPointerType() || |
Anders Carlsson | c8df0b6 | 2010-11-05 00:12:09 +0000 | [diff] [blame] | 231 | getFromType()->isNullPtrType() || |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 232 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 233 | return true; |
| 234 | |
| 235 | return false; |
| 236 | } |
| 237 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 238 | /// isPointerConversionToVoidPointer - Determines whether this |
| 239 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 240 | /// used as part of the ranking of standard conversion sequences (C++ |
| 241 | /// 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | bool |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 243 | StandardConversionSequence:: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 245 | QualType FromType = getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 246 | QualType ToType = getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 247 | |
| 248 | // Note that FromType has not necessarily been transformed by the |
| 249 | // array-to-pointer implicit conversion, so check for its presence |
| 250 | // and redo the conversion to get a pointer. |
| 251 | if (First == ICK_Array_To_Pointer) |
| 252 | FromType = Context.getArrayDecayedType(FromType); |
| 253 | |
Douglas Gregor | f9af524 | 2011-04-15 20:45:44 +0000 | [diff] [blame] | 254 | if (Second == ICK_Pointer_Conversion && FromType->isAnyPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 255 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 256 | return ToPtrType->getPointeeType()->isVoidType(); |
| 257 | |
| 258 | return false; |
| 259 | } |
| 260 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 261 | /// Skip any implicit casts which could be either part of a narrowing conversion |
| 262 | /// or after one in an implicit conversion. |
| 263 | static const Expr *IgnoreNarrowingConversion(const Expr *Converted) { |
| 264 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Converted)) { |
| 265 | switch (ICE->getCastKind()) { |
| 266 | case CK_NoOp: |
| 267 | case CK_IntegralCast: |
| 268 | case CK_IntegralToBoolean: |
| 269 | case CK_IntegralToFloating: |
| 270 | case CK_FloatingToIntegral: |
| 271 | case CK_FloatingToBoolean: |
| 272 | case CK_FloatingCast: |
| 273 | Converted = ICE->getSubExpr(); |
| 274 | continue; |
| 275 | |
| 276 | default: |
| 277 | return Converted; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | return Converted; |
| 282 | } |
| 283 | |
| 284 | /// Check if this standard conversion sequence represents a narrowing |
| 285 | /// conversion, according to C++11 [dcl.init.list]p7. |
| 286 | /// |
| 287 | /// \param Ctx The AST context. |
| 288 | /// \param Converted The result of applying this standard conversion sequence. |
| 289 | /// \param ConstantValue If this is an NK_Constant_Narrowing conversion, the |
| 290 | /// value of the expression prior to the narrowing conversion. |
| 291 | NarrowingKind |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 292 | StandardConversionSequence::getNarrowingKind(ASTContext &Ctx, |
| 293 | const Expr *Converted, |
| 294 | APValue &ConstantValue) const { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 295 | assert(Ctx.getLangOptions().CPlusPlus && "narrowing check outside C++"); |
| 296 | |
| 297 | // C++11 [dcl.init.list]p7: |
| 298 | // A narrowing conversion is an implicit conversion ... |
| 299 | QualType FromType = getToType(0); |
| 300 | QualType ToType = getToType(1); |
| 301 | switch (Second) { |
| 302 | // -- from a floating-point type to an integer type, or |
| 303 | // |
| 304 | // -- from an integer type or unscoped enumeration type to a floating-point |
| 305 | // type, except where the source is a constant expression and the actual |
| 306 | // value after conversion will fit into the target type and will produce |
| 307 | // the original value when converted back to the original type, or |
| 308 | case ICK_Floating_Integral: |
| 309 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
| 310 | return NK_Type_Narrowing; |
| 311 | } else if (FromType->isIntegralType(Ctx) && ToType->isRealFloatingType()) { |
| 312 | llvm::APSInt IntConstantValue; |
| 313 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| 314 | if (Initializer && |
| 315 | Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { |
| 316 | // Convert the integer to the floating type. |
| 317 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
| 318 | Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), |
| 319 | llvm::APFloat::rmNearestTiesToEven); |
| 320 | // And back. |
| 321 | llvm::APSInt ConvertedValue = IntConstantValue; |
| 322 | bool ignored; |
| 323 | Result.convertToInteger(ConvertedValue, |
| 324 | llvm::APFloat::rmTowardZero, &ignored); |
| 325 | // If the resulting value is different, this was a narrowing conversion. |
| 326 | if (IntConstantValue != ConvertedValue) { |
| 327 | ConstantValue = APValue(IntConstantValue); |
| 328 | return NK_Constant_Narrowing; |
| 329 | } |
| 330 | } else { |
| 331 | // Variables are always narrowings. |
| 332 | return NK_Variable_Narrowing; |
| 333 | } |
| 334 | } |
| 335 | return NK_Not_Narrowing; |
| 336 | |
| 337 | // -- from long double to double or float, or from double to float, except |
| 338 | // where the source is a constant expression and the actual value after |
| 339 | // conversion is within the range of values that can be represented (even |
| 340 | // if it cannot be represented exactly), or |
| 341 | case ICK_Floating_Conversion: |
| 342 | if (FromType->isRealFloatingType() && ToType->isRealFloatingType() && |
| 343 | Ctx.getFloatingTypeOrder(FromType, ToType) == 1) { |
| 344 | // FromType is larger than ToType. |
| 345 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| 346 | if (Initializer->isCXX11ConstantExpr(Ctx, &ConstantValue)) { |
| 347 | // Constant! |
| 348 | assert(ConstantValue.isFloat()); |
| 349 | llvm::APFloat FloatVal = ConstantValue.getFloat(); |
| 350 | // Convert the source value into the target type. |
| 351 | bool ignored; |
| 352 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
| 353 | Ctx.getFloatTypeSemantics(ToType), |
| 354 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 355 | // If there was no overflow, the source value is within the range of |
| 356 | // values that can be represented. |
| 357 | if (ConvertStatus & llvm::APFloat::opOverflow) |
| 358 | return NK_Constant_Narrowing; |
| 359 | } else { |
| 360 | return NK_Variable_Narrowing; |
| 361 | } |
| 362 | } |
| 363 | return NK_Not_Narrowing; |
| 364 | |
| 365 | // -- from an integer type or unscoped enumeration type to an integer type |
| 366 | // that cannot represent all the values of the original type, except where |
| 367 | // the source is a constant expression and the actual value after |
| 368 | // conversion will fit into the target type and will produce the original |
| 369 | // value when converted back to the original type. |
| 370 | case ICK_Boolean_Conversion: // Bools are integers too. |
| 371 | if (!FromType->isIntegralOrUnscopedEnumerationType()) { |
| 372 | // Boolean conversions can be from pointers and pointers to members |
| 373 | // [conv.bool], and those aren't considered narrowing conversions. |
| 374 | return NK_Not_Narrowing; |
| 375 | } // Otherwise, fall through to the integral case. |
| 376 | case ICK_Integral_Conversion: { |
| 377 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
| 378 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
| 379 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
| 380 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
| 381 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
| 382 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
| 383 | |
| 384 | if (FromWidth > ToWidth || |
| 385 | (FromWidth == ToWidth && FromSigned != ToSigned)) { |
| 386 | // Not all values of FromType can be represented in ToType. |
| 387 | llvm::APSInt InitializerValue; |
| 388 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| 389 | if (Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { |
| 390 | ConstantValue = APValue(InitializerValue); |
| 391 | |
| 392 | // Add a bit to the InitializerValue so we don't have to worry about |
| 393 | // signed vs. unsigned comparisons. |
| 394 | InitializerValue = InitializerValue.extend( |
| 395 | InitializerValue.getBitWidth() + 1); |
| 396 | // Convert the initializer to and from the target width and signed-ness. |
| 397 | llvm::APSInt ConvertedValue = InitializerValue; |
| 398 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
| 399 | ConvertedValue.setIsSigned(ToSigned); |
| 400 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
| 401 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
| 402 | // If the result is different, this was a narrowing conversion. |
| 403 | if (ConvertedValue != InitializerValue) |
| 404 | return NK_Constant_Narrowing; |
| 405 | } else { |
| 406 | // Variables are always narrowings. |
| 407 | return NK_Variable_Narrowing; |
| 408 | } |
| 409 | } |
| 410 | return NK_Not_Narrowing; |
| 411 | } |
| 412 | |
| 413 | default: |
| 414 | // Other kinds of conversions are not narrowings. |
| 415 | return NK_Not_Narrowing; |
| 416 | } |
| 417 | } |
| 418 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 419 | /// DebugPrint - Print this standard conversion sequence to standard |
| 420 | /// error. Useful for debugging overloading issues. |
| 421 | void StandardConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 422 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 423 | bool PrintedSomething = false; |
| 424 | if (First != ICK_Identity) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 425 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 426 | PrintedSomething = true; |
| 427 | } |
| 428 | |
| 429 | if (Second != ICK_Identity) { |
| 430 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 431 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 432 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 433 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 434 | |
| 435 | if (CopyConstructor) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 436 | OS << " (by copy constructor)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 437 | } else if (DirectBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 438 | OS << " (direct reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 439 | } else if (ReferenceBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 440 | OS << " (reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 441 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 442 | PrintedSomething = true; |
| 443 | } |
| 444 | |
| 445 | if (Third != ICK_Identity) { |
| 446 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 447 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 448 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 449 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 450 | PrintedSomething = true; |
| 451 | } |
| 452 | |
| 453 | if (!PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 454 | OS << "No conversions required"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
| 458 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 459 | /// error. Useful for debugging overloading issues. |
| 460 | void UserDefinedConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 461 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 462 | if (Before.First || Before.Second || Before.Third) { |
| 463 | Before.DebugPrint(); |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 464 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 465 | } |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 466 | if (ConversionFunction) |
| 467 | OS << '\'' << *ConversionFunction << '\''; |
| 468 | else |
| 469 | OS << "aggregate initialization"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 470 | if (After.First || After.Second || After.Third) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 471 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 472 | After.DebugPrint(); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 477 | /// error. Useful for debugging overloading issues. |
| 478 | void ImplicitConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 479 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 480 | switch (ConversionKind) { |
| 481 | case StandardConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 482 | OS << "Standard conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 483 | Standard.DebugPrint(); |
| 484 | break; |
| 485 | case UserDefinedConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 486 | OS << "User-defined conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 487 | UserDefined.DebugPrint(); |
| 488 | break; |
| 489 | case EllipsisConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 490 | OS << "Ellipsis conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 491 | break; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 492 | case AmbiguousConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 493 | OS << "Ambiguous conversion"; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 494 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 495 | case BadConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 496 | OS << "Bad conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 497 | break; |
| 498 | } |
| 499 | |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 500 | OS << "\n"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 501 | } |
| 502 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 503 | void AmbiguousConversionSequence::construct() { |
| 504 | new (&conversions()) ConversionSet(); |
| 505 | } |
| 506 | |
| 507 | void AmbiguousConversionSequence::destruct() { |
| 508 | conversions().~ConversionSet(); |
| 509 | } |
| 510 | |
| 511 | void |
| 512 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 513 | FromTypePtr = O.FromTypePtr; |
| 514 | ToTypePtr = O.ToTypePtr; |
| 515 | new (&conversions()) ConversionSet(O.conversions()); |
| 516 | } |
| 517 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 518 | namespace { |
| 519 | // Structure used by OverloadCandidate::DeductionFailureInfo to store |
| 520 | // template parameter and template argument information. |
| 521 | struct DFIParamWithArguments { |
| 522 | TemplateParameter Param; |
| 523 | TemplateArgument FirstArg; |
| 524 | TemplateArgument SecondArg; |
| 525 | }; |
| 526 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 527 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 528 | /// \brief Convert from Sema's representation of template deduction information |
| 529 | /// to the form used in overload-candidate information. |
| 530 | OverloadCandidate::DeductionFailureInfo |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 531 | static MakeDeductionFailureInfo(ASTContext &Context, |
| 532 | Sema::TemplateDeductionResult TDK, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 533 | TemplateDeductionInfo &Info) { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 534 | OverloadCandidate::DeductionFailureInfo Result; |
| 535 | Result.Result = static_cast<unsigned>(TDK); |
| 536 | Result.Data = 0; |
| 537 | switch (TDK) { |
| 538 | case Sema::TDK_Success: |
| 539 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 540 | case Sema::TDK_TooManyArguments: |
| 541 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 542 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 543 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 544 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 545 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 546 | Result.Data = Info.Param.getOpaqueValue(); |
| 547 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 548 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 549 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 550 | case Sema::TDK_Underqualified: { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 551 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 552 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 553 | Saved->Param = Info.Param; |
| 554 | Saved->FirstArg = Info.FirstArg; |
| 555 | Saved->SecondArg = Info.SecondArg; |
| 556 | Result.Data = Saved; |
| 557 | break; |
| 558 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 559 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 560 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 561 | Result.Data = Info.take(); |
| 562 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 563 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 564 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 565 | case Sema::TDK_FailedOverloadResolution: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 566 | break; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 567 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 568 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 569 | return Result; |
| 570 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 571 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 572 | void OverloadCandidate::DeductionFailureInfo::Destroy() { |
| 573 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 574 | case Sema::TDK_Success: |
| 575 | case Sema::TDK_InstantiationDepth: |
| 576 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 577 | case Sema::TDK_TooManyArguments: |
| 578 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 579 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 580 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 581 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 582 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 583 | case Sema::TDK_Underqualified: |
Douglas Gregor | aaa045d | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 584 | // FIXME: Destroy the data? |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 585 | Data = 0; |
| 586 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 587 | |
| 588 | case Sema::TDK_SubstitutionFailure: |
| 589 | // FIXME: Destroy the template arugment list? |
| 590 | Data = 0; |
| 591 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 592 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 593 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 594 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 595 | case Sema::TDK_FailedOverloadResolution: |
| 596 | break; |
| 597 | } |
| 598 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 599 | |
| 600 | TemplateParameter |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 601 | OverloadCandidate::DeductionFailureInfo::getTemplateParameter() { |
| 602 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 603 | case Sema::TDK_Success: |
| 604 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 605 | case Sema::TDK_TooManyArguments: |
| 606 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 607 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 608 | return TemplateParameter(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 609 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 610 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 611 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 612 | return TemplateParameter::getFromOpaqueValue(Data); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 613 | |
| 614 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 615 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 616 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
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 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 619 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 620 | case Sema::TDK_FailedOverloadResolution: |
| 621 | break; |
| 622 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 623 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 624 | return TemplateParameter(); |
| 625 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 626 | |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 627 | TemplateArgumentList * |
| 628 | OverloadCandidate::DeductionFailureInfo::getTemplateArgumentList() { |
| 629 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 630 | case Sema::TDK_Success: |
| 631 | case Sema::TDK_InstantiationDepth: |
| 632 | case Sema::TDK_TooManyArguments: |
| 633 | case Sema::TDK_TooFewArguments: |
| 634 | case Sema::TDK_Incomplete: |
| 635 | case Sema::TDK_InvalidExplicitArguments: |
| 636 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 637 | case Sema::TDK_Underqualified: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 638 | return 0; |
| 639 | |
| 640 | case Sema::TDK_SubstitutionFailure: |
| 641 | return static_cast<TemplateArgumentList*>(Data); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 642 | |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 643 | // Unhandled |
| 644 | case Sema::TDK_NonDeducedMismatch: |
| 645 | case Sema::TDK_FailedOverloadResolution: |
| 646 | break; |
| 647 | } |
| 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 652 | const TemplateArgument *OverloadCandidate::DeductionFailureInfo::getFirstArg() { |
| 653 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 654 | case Sema::TDK_Success: |
| 655 | case Sema::TDK_InstantiationDepth: |
| 656 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 657 | case Sema::TDK_TooManyArguments: |
| 658 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 659 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 660 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 661 | return 0; |
| 662 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 663 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 664 | case Sema::TDK_Underqualified: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 665 | return &static_cast<DFIParamWithArguments*>(Data)->FirstArg; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 666 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 667 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 668 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 669 | case Sema::TDK_FailedOverloadResolution: |
| 670 | break; |
| 671 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 672 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 673 | return 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 674 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 675 | |
| 676 | const TemplateArgument * |
| 677 | OverloadCandidate::DeductionFailureInfo::getSecondArg() { |
| 678 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 679 | case Sema::TDK_Success: |
| 680 | case Sema::TDK_InstantiationDepth: |
| 681 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 682 | case Sema::TDK_TooManyArguments: |
| 683 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 684 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 685 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 686 | return 0; |
| 687 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 688 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 689 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 690 | return &static_cast<DFIParamWithArguments*>(Data)->SecondArg; |
| 691 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 692 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 693 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 694 | case Sema::TDK_FailedOverloadResolution: |
| 695 | break; |
| 696 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 697 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | void OverloadCandidateSet::clear() { |
Benjamin Kramer | 9e2822b | 2012-01-14 20:16:52 +0000 | [diff] [blame] | 702 | for (iterator i = begin(), e = end(); i != e; ++i) |
| 703 | for (unsigned ii = 0, ie = i->NumConversions; ii != ie; ++ii) |
| 704 | i->Conversions[ii].~ImplicitConversionSequence(); |
Benjamin Kramer | 314f554 | 2012-01-14 19:31:39 +0000 | [diff] [blame] | 705 | NumInlineSequences = 0; |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 706 | Candidates.clear(); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 707 | Functions.clear(); |
| 708 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 709 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 710 | namespace { |
| 711 | class UnbridgedCastsSet { |
| 712 | struct Entry { |
| 713 | Expr **Addr; |
| 714 | Expr *Saved; |
| 715 | }; |
| 716 | SmallVector<Entry, 2> Entries; |
| 717 | |
| 718 | public: |
| 719 | void save(Sema &S, Expr *&E) { |
| 720 | assert(E->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 721 | Entry entry = { &E, E }; |
| 722 | Entries.push_back(entry); |
| 723 | E = S.stripARCUnbridgedCast(E); |
| 724 | } |
| 725 | |
| 726 | void restore() { |
| 727 | for (SmallVectorImpl<Entry>::iterator |
| 728 | i = Entries.begin(), e = Entries.end(); i != e; ++i) |
| 729 | *i->Addr = i->Saved; |
| 730 | } |
| 731 | }; |
| 732 | } |
| 733 | |
| 734 | /// checkPlaceholderForOverload - Do any interesting placeholder-like |
| 735 | /// preprocessing on the given expression. |
| 736 | /// |
| 737 | /// \param unbridgedCasts a collection to which to add unbridged casts; |
| 738 | /// without this, they will be immediately diagnosed as errors |
| 739 | /// |
| 740 | /// Return true on unrecoverable error. |
| 741 | static bool checkPlaceholderForOverload(Sema &S, Expr *&E, |
| 742 | UnbridgedCastsSet *unbridgedCasts = 0) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 743 | if (const BuiltinType *placeholder = E->getType()->getAsPlaceholderType()) { |
| 744 | // We can't handle overloaded expressions here because overload |
| 745 | // resolution might reasonably tweak them. |
| 746 | if (placeholder->getKind() == BuiltinType::Overload) return false; |
| 747 | |
| 748 | // If the context potentially accepts unbridged ARC casts, strip |
| 749 | // the unbridged cast and add it to the collection for later restoration. |
| 750 | if (placeholder->getKind() == BuiltinType::ARCUnbridgedCast && |
| 751 | unbridgedCasts) { |
| 752 | unbridgedCasts->save(S, E); |
| 753 | return false; |
| 754 | } |
| 755 | |
| 756 | // Go ahead and check everything else. |
| 757 | ExprResult result = S.CheckPlaceholderExpr(E); |
| 758 | if (result.isInvalid()) |
| 759 | return true; |
| 760 | |
| 761 | E = result.take(); |
| 762 | return false; |
| 763 | } |
| 764 | |
| 765 | // Nothing to do. |
| 766 | return false; |
| 767 | } |
| 768 | |
| 769 | /// checkArgPlaceholdersForOverload - Check a set of call operands for |
| 770 | /// placeholders. |
| 771 | static bool checkArgPlaceholdersForOverload(Sema &S, Expr **args, |
| 772 | unsigned numArgs, |
| 773 | UnbridgedCastsSet &unbridged) { |
| 774 | for (unsigned i = 0; i != numArgs; ++i) |
| 775 | if (checkPlaceholderForOverload(S, args[i], &unbridged)) |
| 776 | return true; |
| 777 | |
| 778 | return false; |
| 779 | } |
| 780 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 781 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 782 | // overload of the declarations in Old. This routine returns false if |
| 783 | // New and Old cannot be overloaded, e.g., if New has the same |
| 784 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 785 | // declarations aren't functions (or function templates) at all. When |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 786 | // it does return false, MatchedDecl will point to the decl that New |
| 787 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 788 | // top of the underlying declaration. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 789 | // |
| 790 | // Example: Given the following input: |
| 791 | // |
| 792 | // void f(int, float); // #1 |
| 793 | // void f(int, int); // #2 |
| 794 | // int f(int, int); // #3 |
| 795 | // |
| 796 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 797 | // so IsOverload will not be used. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 798 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 799 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 800 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 801 | // (since they have different signatures), so this routine returns |
| 802 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 803 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 804 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 805 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 806 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 807 | // identical (return types of functions are not part of the |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 808 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 809 | // point to the FunctionDecl for #2. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 810 | // |
| 811 | // 'NewIsUsingShadowDecl' indicates that 'New' is being introduced |
| 812 | // into a class by a using declaration. The rules for whether to hide |
| 813 | // shadow declarations ignore some properties which otherwise figure |
| 814 | // into a function template's signature. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 815 | Sema::OverloadKind |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 816 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
| 817 | NamedDecl *&Match, bool NewIsUsingDecl) { |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 818 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 819 | I != E; ++I) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 820 | NamedDecl *OldD = *I; |
| 821 | |
| 822 | bool OldIsUsingDecl = false; |
| 823 | if (isa<UsingShadowDecl>(OldD)) { |
| 824 | OldIsUsingDecl = true; |
| 825 | |
| 826 | // We can always introduce two using declarations into the same |
| 827 | // context, even if they have identical signatures. |
| 828 | if (NewIsUsingDecl) continue; |
| 829 | |
| 830 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 831 | } |
| 832 | |
| 833 | // If either declaration was introduced by a using declaration, |
| 834 | // we'll need to use slightly different rules for matching. |
| 835 | // Essentially, these rules are the normal rules, except that |
| 836 | // function templates hide function templates with different |
| 837 | // return types or template parameter lists. |
| 838 | bool UseMemberUsingDeclRules = |
| 839 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord(); |
| 840 | |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 841 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 842 | if (!IsOverload(New, OldT->getTemplatedDecl(), UseMemberUsingDeclRules)) { |
| 843 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 844 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 845 | continue; |
| 846 | } |
| 847 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 848 | Match = *I; |
| 849 | return Ovl_Match; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 850 | } |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 851 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 852 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
| 853 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 854 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 855 | continue; |
| 856 | } |
| 857 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 858 | Match = *I; |
| 859 | return Ovl_Match; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 860 | } |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 861 | } else if (isa<UsingDecl>(OldD)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 862 | // We can overload with these, which can show up when doing |
| 863 | // redeclaration checks for UsingDecls. |
| 864 | assert(Old.getLookupKind() == LookupUsingDeclName); |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 865 | } else if (isa<TagDecl>(OldD)) { |
| 866 | // We can always overload with tags by hiding them. |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 867 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 868 | // Optimistically assume that an unresolved using decl will |
| 869 | // overload; if it doesn't, we'll have to diagnose during |
| 870 | // template instantiation. |
| 871 | } else { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 872 | // (C++ 13p1): |
| 873 | // Only function declarations can be overloaded; object and type |
| 874 | // declarations cannot be overloaded. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 875 | Match = *I; |
| 876 | return Ovl_NonFunction; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 877 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 878 | } |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 879 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 880 | return Ovl_Overload; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 881 | } |
| 882 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 883 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| 884 | bool UseUsingDeclRules) { |
John McCall | 7b49202 | 2010-08-12 07:09:11 +0000 | [diff] [blame] | 885 | // If both of the functions are extern "C", then they are not |
| 886 | // overloads. |
| 887 | if (Old->isExternC() && New->isExternC()) |
| 888 | return false; |
| 889 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 890 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 891 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 892 | |
| 893 | // C++ [temp.fct]p2: |
| 894 | // A function template can be overloaded with other function templates |
| 895 | // and with normal (non-template) functions. |
| 896 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 897 | return true; |
| 898 | |
| 899 | // Is the function New an overload of the function Old? |
| 900 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 901 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 902 | |
| 903 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 904 | // determine whether they are overloads. If we find any mismatch |
| 905 | // in the signature, they are overloads. |
| 906 | |
| 907 | // If either of these functions is a K&R-style function (no |
| 908 | // prototype), then we consider them to have matching signatures. |
| 909 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 910 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 911 | return false; |
| 912 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 913 | const FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 914 | const FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 915 | |
| 916 | // The signature of a function includes the types of its |
| 917 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 918 | // of the ellipsis; see C++ DR 357). |
| 919 | if (OldQType != NewQType && |
| 920 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 921 | OldType->isVariadic() != NewType->isVariadic() || |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 922 | !FunctionArgTypesAreEqual(OldType, NewType))) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 923 | return true; |
| 924 | |
| 925 | // C++ [temp.over.link]p4: |
| 926 | // The signature of a function template consists of its function |
| 927 | // signature, its return type and its template parameter list. The names |
| 928 | // of the template parameters are significant only for establishing the |
| 929 | // relationship between the template parameters and the rest of the |
| 930 | // signature. |
| 931 | // |
| 932 | // We check the return type and template parameter lists for function |
| 933 | // templates first; the remaining checks follow. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 934 | // |
| 935 | // However, we don't consider either of these when deciding whether |
| 936 | // a member introduced by a shadow declaration is hidden. |
| 937 | if (!UseUsingDeclRules && NewTemplate && |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 938 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 939 | OldTemplate->getTemplateParameters(), |
| 940 | false, TPL_TemplateMatch) || |
| 941 | OldType->getResultType() != NewType->getResultType())) |
| 942 | return true; |
| 943 | |
| 944 | // If the function is a class member, its signature includes the |
Douglas Gregor | 57c9f4f | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 945 | // 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] | 946 | // |
| 947 | // As part of this, also check whether one of the member functions |
| 948 | // is static, in which case they are not overloads (C++ |
| 949 | // 13.1p2). While not part of the definition of the signature, |
| 950 | // this check is important to determine whether these functions |
| 951 | // can be overloaded. |
| 952 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 953 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 954 | if (OldMethod && NewMethod && |
| 955 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
Douglas Gregor | 57c9f4f | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 956 | (OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers() || |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 957 | OldMethod->getRefQualifier() != NewMethod->getRefQualifier())) { |
| 958 | if (!UseUsingDeclRules && |
| 959 | OldMethod->getRefQualifier() != NewMethod->getRefQualifier() && |
| 960 | (OldMethod->getRefQualifier() == RQ_None || |
| 961 | NewMethod->getRefQualifier() == RQ_None)) { |
| 962 | // C++0x [over.load]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 963 | // - Member function declarations with the same name and the same |
| 964 | // parameter-type-list as well as member function template |
| 965 | // declarations with the same name, the same parameter-type-list, and |
| 966 | // the same template parameter lists cannot be overloaded if any of |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 967 | // them, but not all, have a ref-qualifier (8.3.5). |
| 968 | Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) |
| 969 | << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); |
| 970 | Diag(OldMethod->getLocation(), diag::note_previous_declaration); |
| 971 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 972 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 973 | return true; |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 974 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 975 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 976 | // The signatures match; this is not an overload. |
| 977 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 980 | /// \brief Checks availability of the function depending on the current |
| 981 | /// function context. Inside an unavailable function, unavailability is ignored. |
| 982 | /// |
| 983 | /// \returns true if \arg FD is unavailable and current context is inside |
| 984 | /// an available function, false otherwise. |
| 985 | bool Sema::isFunctionConsideredUnavailable(FunctionDecl *FD) { |
| 986 | return FD->isUnavailable() && !cast<Decl>(CurContext)->isUnavailable(); |
| 987 | } |
| 988 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 989 | /// \brief Tries a user-defined conversion from From to ToType. |
| 990 | /// |
| 991 | /// Produces an implicit conversion sequence for when a standard conversion |
| 992 | /// is not an option. See TryImplicitConversion for more information. |
| 993 | static ImplicitConversionSequence |
| 994 | TryUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 995 | bool SuppressUserConversions, |
| 996 | bool AllowExplicit, |
| 997 | bool InOverloadResolution, |
| 998 | bool CStyle, |
| 999 | bool AllowObjCWritebackConversion) { |
| 1000 | ImplicitConversionSequence ICS; |
| 1001 | |
| 1002 | if (SuppressUserConversions) { |
| 1003 | // We're not in the case above, so there is no conversion that |
| 1004 | // we can perform. |
| 1005 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1006 | return ICS; |
| 1007 | } |
| 1008 | |
| 1009 | // Attempt user-defined conversion. |
| 1010 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 1011 | OverloadingResult UserDefResult |
| 1012 | = IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, Conversions, |
| 1013 | AllowExplicit); |
| 1014 | |
| 1015 | if (UserDefResult == OR_Success) { |
| 1016 | ICS.setUserDefined(); |
| 1017 | // C++ [over.ics.user]p4: |
| 1018 | // A conversion of an expression of class type to the same class |
| 1019 | // type is given Exact Match rank, and a conversion of an |
| 1020 | // expression of class type to a base class of that type is |
| 1021 | // given Conversion rank, in spite of the fact that a copy |
| 1022 | // constructor (i.e., a user-defined conversion function) is |
| 1023 | // called for those cases. |
| 1024 | if (CXXConstructorDecl *Constructor |
| 1025 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
| 1026 | QualType FromCanon |
| 1027 | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 1028 | QualType ToCanon |
| 1029 | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 1030 | if (Constructor->isCopyConstructor() && |
| 1031 | (FromCanon == ToCanon || S.IsDerivedFrom(FromCanon, ToCanon))) { |
| 1032 | // Turn this into a "standard" conversion sequence, so that it |
| 1033 | // gets ranked with standard conversion sequences. |
| 1034 | ICS.setStandard(); |
| 1035 | ICS.Standard.setAsIdentityConversion(); |
| 1036 | ICS.Standard.setFromType(From->getType()); |
| 1037 | ICS.Standard.setAllToTypes(ToType); |
| 1038 | ICS.Standard.CopyConstructor = Constructor; |
| 1039 | if (ToCanon != FromCanon) |
| 1040 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | // C++ [over.best.ics]p4: |
| 1045 | // However, when considering the argument of a user-defined |
| 1046 | // conversion function that is a candidate by 13.3.1.3 when |
| 1047 | // invoked for the copying of the temporary in the second step |
| 1048 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 1049 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 1050 | // ellipsis conversion sequences are allowed. |
| 1051 | if (SuppressUserConversions && ICS.isUserDefined()) { |
| 1052 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
| 1053 | } |
| 1054 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
| 1055 | ICS.setAmbiguous(); |
| 1056 | ICS.Ambiguous.setFromType(From->getType()); |
| 1057 | ICS.Ambiguous.setToType(ToType); |
| 1058 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 1059 | Cand != Conversions.end(); ++Cand) |
| 1060 | if (Cand->Viable) |
| 1061 | ICS.Ambiguous.addConversion(Cand->Function); |
| 1062 | } else { |
| 1063 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1064 | } |
| 1065 | |
| 1066 | return ICS; |
| 1067 | } |
| 1068 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1069 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 1070 | /// from the given expression (Expr) to the given type (ToType). This |
| 1071 | /// function returns an implicit conversion sequence that can be used |
| 1072 | /// to perform the initialization. Given |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1073 | /// |
| 1074 | /// void f(float f); |
| 1075 | /// void g(int i) { f(i); } |
| 1076 | /// |
| 1077 | /// this routine would produce an implicit conversion sequence to |
| 1078 | /// describe the initialization of f from i, which will be a standard |
| 1079 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 1080 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 1081 | // |
| 1082 | /// Note that this routine only determines how the conversion can be |
| 1083 | /// performed; it does not actually perform the conversion. As such, |
| 1084 | /// it will not produce any diagnostics if no conversion is available, |
| 1085 | /// but will instead return an implicit conversion sequence of kind |
| 1086 | /// "BadConversion". |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1087 | /// |
| 1088 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 1089 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1090 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 1091 | /// permitted. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1092 | /// |
| 1093 | /// \param AllowObjCWritebackConversion Whether we allow the Objective-C |
| 1094 | /// writeback conversion, which allows __autoreleasing id* parameters to |
| 1095 | /// be initialized with __strong id* or __weak id* arguments. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1096 | static ImplicitConversionSequence |
| 1097 | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
| 1098 | bool SuppressUserConversions, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1099 | bool AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1100 | bool InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1101 | bool CStyle, |
| 1102 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1103 | ImplicitConversionSequence ICS; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1104 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1105 | ICS.Standard, CStyle, AllowObjCWritebackConversion)){ |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1106 | ICS.setStandard(); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1107 | return ICS; |
| 1108 | } |
| 1109 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1110 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 1111 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1112 | return ICS; |
| 1113 | } |
| 1114 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1115 | // C++ [over.ics.user]p4: |
| 1116 | // A conversion of an expression of class type to the same class |
| 1117 | // type is given Exact Match rank, and a conversion of an |
| 1118 | // expression of class type to a base class of that type is |
| 1119 | // given Conversion rank, in spite of the fact that a copy/move |
| 1120 | // constructor (i.e., a user-defined conversion function) is |
| 1121 | // called for those cases. |
| 1122 | QualType FromType = From->getType(); |
| 1123 | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1124 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
| 1125 | S.IsDerivedFrom(FromType, ToType))) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1126 | ICS.setStandard(); |
| 1127 | ICS.Standard.setAsIdentityConversion(); |
| 1128 | ICS.Standard.setFromType(FromType); |
| 1129 | ICS.Standard.setAllToTypes(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1130 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1131 | // We don't actually check at this point whether there is a valid |
| 1132 | // copy/move constructor, since overloading just assumes that it |
| 1133 | // exists. When we actually perform initialization, we'll find the |
| 1134 | // appropriate constructor to copy the returned object, if needed. |
| 1135 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1136 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1137 | // Determine whether this is considered a derived-to-base conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1138 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1139 | ICS.Standard.Second = ICK_Derived_To_Base; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1140 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1141 | return ICS; |
| 1142 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1143 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1144 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 1145 | AllowExplicit, InOverloadResolution, CStyle, |
| 1146 | AllowObjCWritebackConversion); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1149 | ImplicitConversionSequence |
| 1150 | Sema::TryImplicitConversion(Expr *From, QualType ToType, |
| 1151 | bool SuppressUserConversions, |
| 1152 | bool AllowExplicit, |
| 1153 | bool InOverloadResolution, |
| 1154 | bool CStyle, |
| 1155 | bool AllowObjCWritebackConversion) { |
| 1156 | return clang::TryImplicitConversion(*this, From, ToType, |
| 1157 | SuppressUserConversions, AllowExplicit, |
| 1158 | InOverloadResolution, CStyle, |
| 1159 | AllowObjCWritebackConversion); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1162 | /// PerformImplicitConversion - Perform an implicit conversion of the |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1163 | /// expression From to the type ToType. Returns the |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1164 | /// converted expression. Flavor is the kind of conversion we're |
| 1165 | /// performing, used in the error message. If @p AllowExplicit, |
| 1166 | /// explicit user-defined conversions are permitted. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1167 | ExprResult |
| 1168 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1169 | AssignmentAction Action, bool AllowExplicit) { |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1170 | ImplicitConversionSequence ICS; |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1171 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1174 | ExprResult |
| 1175 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1176 | AssignmentAction Action, bool AllowExplicit, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1177 | ImplicitConversionSequence& ICS) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1178 | if (checkPlaceholderForOverload(*this, From)) |
| 1179 | return ExprError(); |
| 1180 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1181 | // Objective-C ARC: Determine whether we will allow the writeback conversion. |
| 1182 | bool AllowObjCWritebackConversion |
| 1183 | = getLangOptions().ObjCAutoRefCount && |
| 1184 | (Action == AA_Passing || Action == AA_Sending); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1185 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1186 | ICS = clang::TryImplicitConversion(*this, From, ToType, |
| 1187 | /*SuppressUserConversions=*/false, |
| 1188 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1189 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1190 | /*CStyle=*/false, |
| 1191 | AllowObjCWritebackConversion); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1192 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 1193 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1194 | |
| 1195 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1196 | /// conversion that strips "noreturn" off the nested function type. |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1197 | bool Sema::IsNoReturnConversion(QualType FromType, QualType ToType, |
| 1198 | QualType &ResultTy) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1199 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1200 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1201 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1202 | // Permit the conversion F(t __attribute__((noreturn))) -> F(t) |
| 1203 | // where F adds one of the following at most once: |
| 1204 | // - a pointer |
| 1205 | // - a member pointer |
| 1206 | // - a block pointer |
| 1207 | CanQualType CanTo = Context.getCanonicalType(ToType); |
| 1208 | CanQualType CanFrom = Context.getCanonicalType(FromType); |
| 1209 | Type::TypeClass TyClass = CanTo->getTypeClass(); |
| 1210 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1211 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) { |
| 1212 | if (TyClass == Type::Pointer) { |
| 1213 | CanTo = CanTo.getAs<PointerType>()->getPointeeType(); |
| 1214 | CanFrom = CanFrom.getAs<PointerType>()->getPointeeType(); |
| 1215 | } else if (TyClass == Type::BlockPointer) { |
| 1216 | CanTo = CanTo.getAs<BlockPointerType>()->getPointeeType(); |
| 1217 | CanFrom = CanFrom.getAs<BlockPointerType>()->getPointeeType(); |
| 1218 | } else if (TyClass == Type::MemberPointer) { |
| 1219 | CanTo = CanTo.getAs<MemberPointerType>()->getPointeeType(); |
| 1220 | CanFrom = CanFrom.getAs<MemberPointerType>()->getPointeeType(); |
| 1221 | } else { |
| 1222 | return false; |
| 1223 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1224 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1225 | TyClass = CanTo->getTypeClass(); |
| 1226 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1227 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) |
| 1228 | return false; |
| 1229 | } |
| 1230 | |
| 1231 | const FunctionType *FromFn = cast<FunctionType>(CanFrom); |
| 1232 | FunctionType::ExtInfo EInfo = FromFn->getExtInfo(); |
| 1233 | if (!EInfo.getNoReturn()) return false; |
| 1234 | |
| 1235 | FromFn = Context.adjustFunctionType(FromFn, EInfo.withNoReturn(false)); |
| 1236 | assert(QualType(FromFn, 0).isCanonical()); |
| 1237 | if (QualType(FromFn, 0) != CanTo) return false; |
| 1238 | |
| 1239 | ResultTy = ToType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1240 | return true; |
| 1241 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1242 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1243 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 1244 | /// vector conversion. |
| 1245 | /// |
| 1246 | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
| 1247 | /// conversion. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1248 | static bool IsVectorConversion(ASTContext &Context, QualType FromType, |
| 1249 | QualType ToType, ImplicitConversionKind &ICK) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1250 | // We need at least one of these types to be a vector type to have a vector |
| 1251 | // conversion. |
| 1252 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
| 1253 | return false; |
| 1254 | |
| 1255 | // Identical types require no conversions. |
| 1256 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1257 | return false; |
| 1258 | |
| 1259 | // There are no conversions between extended vector types, only identity. |
| 1260 | if (ToType->isExtVectorType()) { |
| 1261 | // There are no conversions between extended vector types other than the |
| 1262 | // identity conversion. |
| 1263 | if (FromType->isExtVectorType()) |
| 1264 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1265 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1266 | // Vector splat from any arithmetic type to a vector. |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 1267 | if (FromType->isArithmeticType()) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1268 | ICK = ICK_Vector_Splat; |
| 1269 | return true; |
| 1270 | } |
| 1271 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1272 | |
| 1273 | // We can perform the conversion between vector types in the following cases: |
| 1274 | // 1)vector types are equivalent AltiVec and GCC vector types |
| 1275 | // 2)lax vector conversions are permitted and the vector types are of the |
| 1276 | // same size |
| 1277 | if (ToType->isVectorType() && FromType->isVectorType()) { |
| 1278 | if (Context.areCompatibleVectorTypes(FromType, ToType) || |
Chandler Carruth | c45eb9c | 2010-08-08 05:02:51 +0000 | [diff] [blame] | 1279 | (Context.getLangOptions().LaxVectorConversions && |
| 1280 | (Context.getTypeSize(FromType) == Context.getTypeSize(ToType)))) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1281 | ICK = ICK_Vector_Conversion; |
| 1282 | return true; |
| 1283 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1284 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1285 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1286 | return false; |
| 1287 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1288 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1289 | /// IsStandardConversion - Determines whether there is a standard |
| 1290 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 1291 | /// expression From to the type ToType. Standard conversion sequences |
| 1292 | /// only consider non-class types; for conversions that involve class |
| 1293 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 1294 | /// contain the standard conversion sequence required to perform this |
| 1295 | /// conversion and this routine will return true. Otherwise, this |
| 1296 | /// routine will return false and the value of SCS is unspecified. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1297 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 1298 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1299 | StandardConversionSequence &SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1300 | bool CStyle, |
| 1301 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1302 | QualType FromType = From->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1303 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1304 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1305 | SCS.setAsIdentityConversion(); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1306 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1307 | SCS.IncompatibleObjC = false; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1308 | SCS.setFromType(FromType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1309 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1310 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1311 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1312 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1313 | if (FromType->isRecordType() || ToType->isRecordType()) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1314 | if (S.getLangOptions().CPlusPlus) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1315 | return false; |
| 1316 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1317 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1320 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 1321 | // array-to-pointer conversion, or function-to-pointer conversion |
| 1322 | // (C++ 4p1). |
| 1323 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1324 | if (FromType == S.Context.OverloadTy) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1325 | DeclAccessPair AccessPair; |
| 1326 | if (FunctionDecl *Fn |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1327 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1328 | AccessPair)) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1329 | // We were able to resolve the address of the overloaded function, |
| 1330 | // so we can convert to the type of that function. |
| 1331 | FromType = Fn->getType(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1332 | |
| 1333 | // we can sometimes resolve &foo<int> regardless of ToType, so check |
| 1334 | // if the type matches (identity) or we are converting to bool |
| 1335 | if (!S.Context.hasSameUnqualifiedType( |
| 1336 | S.ExtractUnqualifiedFunctionType(ToType), FromType)) { |
| 1337 | QualType resultTy; |
| 1338 | // if the function type matches except for [[noreturn]], it's ok |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1339 | if (!S.IsNoReturnConversion(FromType, |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1340 | S.ExtractUnqualifiedFunctionType(ToType), resultTy)) |
| 1341 | // otherwise, only a boolean conversion is standard |
| 1342 | if (!ToType->isBooleanType()) |
| 1343 | return false; |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1344 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1345 | |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1346 | // Check if the "from" expression is taking the address of an overloaded |
| 1347 | // function and recompute the FromType accordingly. Take advantage of the |
| 1348 | // fact that non-static member functions *must* have such an address-of |
| 1349 | // expression. |
| 1350 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn); |
| 1351 | if (Method && !Method->isStatic()) { |
| 1352 | assert(isa<UnaryOperator>(From->IgnoreParens()) && |
| 1353 | "Non-unary operator on non-static member address"); |
| 1354 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() |
| 1355 | == UO_AddrOf && |
| 1356 | "Non-address-of operator on non-static member address"); |
| 1357 | const Type *ClassType |
| 1358 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 1359 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
Chandler Carruth | fc5c8fc | 2011-03-29 18:38:10 +0000 | [diff] [blame] | 1360 | } else if (isa<UnaryOperator>(From->IgnoreParens())) { |
| 1361 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() == |
| 1362 | UO_AddrOf && |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1363 | "Non-address-of operator for overloaded function expression"); |
| 1364 | FromType = S.Context.getPointerType(FromType); |
| 1365 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1366 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1367 | // Check that we've computed the proper type after overload resolution. |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1368 | assert(S.Context.hasSameType( |
| 1369 | FromType, |
| 1370 | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1371 | } else { |
| 1372 | return false; |
| 1373 | } |
Anders Carlsson | 2bd6250 | 2010-11-04 05:28:09 +0000 | [diff] [blame] | 1374 | } |
John McCall | 2148011 | 2011-08-30 00:57:29 +0000 | [diff] [blame] | 1375 | // Lvalue-to-rvalue conversion (C++11 4.1): |
| 1376 | // A glvalue (3.10) of a non-function, non-array type T can |
| 1377 | // be converted to a prvalue. |
| 1378 | bool argIsLValue = From->isGLValue(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1379 | if (argIsLValue && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1380 | !FromType->isFunctionType() && !FromType->isArrayType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1381 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1382 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1383 | |
| 1384 | // If T is a non-class type, the type of the rvalue is the |
| 1385 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1386 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 1387 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1388 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1389 | } else if (FromType->isArrayType()) { |
| 1390 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1391 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1392 | |
| 1393 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 1394 | // bound of T" can be converted to an rvalue of type "pointer to |
| 1395 | // T" (C++ 4.2p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1396 | FromType = S.Context.getArrayDecayedType(FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1397 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1398 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1399 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1400 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1401 | |
| 1402 | // For the purpose of ranking in overload resolution |
| 1403 | // (13.3.3.1.1), this conversion is considered an |
| 1404 | // array-to-pointer conversion followed by a qualification |
| 1405 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1406 | SCS.Second = ICK_Identity; |
| 1407 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1408 | SCS.QualificationIncludesObjCLifetime = false; |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1409 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1410 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1411 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1412 | } else if (FromType->isFunctionType() && argIsLValue) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1413 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1414 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1415 | |
| 1416 | // An lvalue of function type T can be converted to an rvalue of |
| 1417 | // type "pointer to T." The result is a pointer to the |
| 1418 | // function. (C++ 4.3p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1419 | FromType = S.Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1420 | } else { |
| 1421 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1422 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1423 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1424 | SCS.setToType(0, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1425 | |
| 1426 | // The second conversion can be an integral promotion, floating |
| 1427 | // point promotion, integral conversion, floating point conversion, |
| 1428 | // floating-integral conversion, pointer conversion, |
| 1429 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1430 | // For overloading in C, this can also be a "compatible-type" |
| 1431 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1432 | bool IncompatibleObjC = false; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1433 | ImplicitConversionKind SecondICK = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1434 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1435 | // The unqualified versions of the types are the same: there's no |
| 1436 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1437 | SCS.Second = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1438 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1439 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1440 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1441 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1442 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1443 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1444 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1445 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1446 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1447 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1448 | SCS.Second = ICK_Complex_Promotion; |
| 1449 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1450 | } else if (ToType->isBooleanType() && |
| 1451 | (FromType->isArithmeticType() || |
| 1452 | FromType->isAnyPointerType() || |
| 1453 | FromType->isBlockPointerType() || |
| 1454 | FromType->isMemberPointerType() || |
| 1455 | FromType->isNullPtrType())) { |
| 1456 | // Boolean conversions (C++ 4.12). |
| 1457 | SCS.Second = ICK_Boolean_Conversion; |
| 1458 | FromType = S.Context.BoolTy; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1459 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1460 | ToType->isIntegralType(S.Context)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1461 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1462 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1463 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1464 | } else if (FromType->isAnyComplexType() && ToType->isComplexType()) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1465 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1466 | SCS.Second = ICK_Complex_Conversion; |
| 1467 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1468 | } else if ((FromType->isAnyComplexType() && ToType->isArithmeticType()) || |
| 1469 | (ToType->isAnyComplexType() && FromType->isArithmeticType())) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1470 | // Complex-real conversions (C99 6.3.1.7) |
| 1471 | SCS.Second = ICK_Complex_Real; |
| 1472 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1473 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1474 | // Floating point conversions (C++ 4.8). |
| 1475 | SCS.Second = ICK_Floating_Conversion; |
| 1476 | FromType = ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1477 | } else if ((FromType->isRealFloatingType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1478 | ToType->isIntegralType(S.Context)) || |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1479 | (FromType->isIntegralOrUnscopedEnumerationType() && |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1480 | ToType->isRealFloatingType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1481 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1482 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1483 | FromType = ToType.getUnqualifiedType(); |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1484 | } else if (S.IsBlockPointerConversion(FromType, ToType, FromType)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1485 | SCS.Second = ICK_Block_Pointer_Conversion; |
| 1486 | } else if (AllowObjCWritebackConversion && |
| 1487 | S.isObjCWritebackConversion(FromType, ToType, FromType)) { |
| 1488 | SCS.Second = ICK_Writeback_Conversion; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1489 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1490 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1491 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1492 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1493 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1494 | FromType = FromType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1495 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1496 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1497 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1498 | SCS.Second = ICK_Pointer_Member; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1499 | } else if (IsVectorConversion(S.Context, FromType, ToType, SecondICK)) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1500 | SCS.Second = SecondICK; |
| 1501 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1502 | } else if (!S.getLangOptions().CPlusPlus && |
| 1503 | S.Context.typesAreCompatible(ToType, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1504 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1505 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1506 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1507 | } else if (S.IsNoReturnConversion(FromType, ToType, FromType)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1508 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1509 | SCS.Second = ICK_NoReturn_Adjustment; |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1510 | } else if (IsTransparentUnionStandardConversion(S, From, ToType, |
| 1511 | InOverloadResolution, |
| 1512 | SCS, CStyle)) { |
| 1513 | SCS.Second = ICK_TransparentUnionConversion; |
| 1514 | FromType = ToType; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1515 | } else { |
| 1516 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1517 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1518 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1519 | SCS.setToType(1, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1520 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1521 | QualType CanonFrom; |
| 1522 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1523 | // The third conversion can be a qualification conversion (C++ 4p1). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1524 | bool ObjCLifetimeConversion; |
| 1525 | if (S.IsQualificationConversion(FromType, ToType, CStyle, |
| 1526 | ObjCLifetimeConversion)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1527 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1528 | SCS.QualificationIncludesObjCLifetime = ObjCLifetimeConversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1529 | FromType = ToType; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1530 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1531 | CanonTo = S.Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1532 | } else { |
| 1533 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1534 | SCS.Third = ICK_Identity; |
| 1535 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1536 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1537 | // [...] Any difference in top-level cv-qualification is |
| 1538 | // subsumed by the initialization itself and does not constitute |
| 1539 | // a conversion. [...] |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1540 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1541 | CanonTo = S.Context.getCanonicalType(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1542 | if (CanonFrom.getLocalUnqualifiedType() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1543 | == CanonTo.getLocalUnqualifiedType() && |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 1544 | (CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers() |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1545 | || CanonFrom.getObjCGCAttr() != CanonTo.getObjCGCAttr() |
| 1546 | || CanonFrom.getObjCLifetime() != CanonTo.getObjCLifetime())) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1547 | FromType = ToType; |
| 1548 | CanonFrom = CanonTo; |
| 1549 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1550 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1551 | SCS.setToType(2, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1552 | |
| 1553 | // If we have not converted the argument type to the parameter type, |
| 1554 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1555 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1556 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1557 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1558 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1559 | } |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1560 | |
| 1561 | static bool |
| 1562 | IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 1563 | QualType &ToType, |
| 1564 | bool InOverloadResolution, |
| 1565 | StandardConversionSequence &SCS, |
| 1566 | bool CStyle) { |
| 1567 | |
| 1568 | const RecordType *UT = ToType->getAsUnionType(); |
| 1569 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 1570 | return false; |
| 1571 | // The field to initialize within the transparent union. |
| 1572 | RecordDecl *UD = UT->getDecl(); |
| 1573 | // It's compatible if the expression matches any of the fields. |
| 1574 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1575 | itend = UD->field_end(); |
| 1576 | it != itend; ++it) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1577 | if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS, |
| 1578 | CStyle, /*ObjCWritebackConversion=*/false)) { |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1579 | ToType = it->getType(); |
| 1580 | return true; |
| 1581 | } |
| 1582 | } |
| 1583 | return false; |
| 1584 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1585 | |
| 1586 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1587 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1588 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1589 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1590 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1591 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1592 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1593 | if (!To) { |
| 1594 | return false; |
| 1595 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1596 | |
| 1597 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1598 | // unsigned short int can be converted to an rvalue of type int if |
| 1599 | // int can represent all the values of the source type; otherwise, |
| 1600 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1601 | // int (C++ 4.5p1). |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1602 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1603 | !FromType->isEnumeralType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1604 | if (// We can promote any signed, promotable integer type to an int |
| 1605 | (FromType->isSignedIntegerType() || |
| 1606 | // We can promote any unsigned integer type whose size is |
| 1607 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1608 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1609 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1610 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1613 | return To->getKind() == BuiltinType::UInt; |
| 1614 | } |
| 1615 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1616 | // C++0x [conv.prom]p3: |
| 1617 | // A prvalue of an unscoped enumeration type whose underlying type is not |
| 1618 | // fixed (7.2) can be converted to an rvalue a prvalue of the first of the |
| 1619 | // following types that can represent all the values of the enumeration |
| 1620 | // (i.e., the values in the range bmin to bmax as described in 7.2): int, |
| 1621 | // unsigned int, long int, unsigned long int, long long int, or unsigned |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1622 | // 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] | 1623 | // values of the enumeration, an rvalue a prvalue of an unscoped enumeration |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1624 | // 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] | 1625 | // with lowest integer conversion rank (4.13) greater than the rank of long |
| 1626 | // 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] | 1627 | // there are two such extended types, the signed one is chosen. |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1628 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
| 1629 | // C++0x 7.2p9: Note that this implicit enum to int conversion is not |
| 1630 | // provided for a scoped enumeration. |
| 1631 | if (FromEnumType->getDecl()->isScoped()) |
| 1632 | return false; |
| 1633 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1634 | // We have already pre-calculated the promotion type, so this is trivial. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1635 | if (ToType->isIntegerType() && |
Douglas Gregor | 5dde160 | 2010-09-12 03:38:25 +0000 | [diff] [blame] | 1636 | !RequireCompleteType(From->getLocStart(), FromType, PDiag())) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1637 | return Context.hasSameUnqualifiedType(ToType, |
| 1638 | FromEnumType->getDecl()->getPromotionType()); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1639 | } |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1640 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1641 | // C++0x [conv.prom]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1642 | // A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted |
| 1643 | // to an rvalue a prvalue of the first of the following types that can |
| 1644 | // represent all the values of its underlying type: int, unsigned int, |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1645 | // 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] | 1646 | // 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] | 1647 | // underlying type, an rvalue a prvalue of type char16_t, char32_t, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1648 | // 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] | 1649 | // type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1650 | if (FromType->isAnyCharacterType() && !FromType->isCharType() && |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1651 | ToType->isIntegerType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1652 | // Determine whether the type we're converting from is signed or |
| 1653 | // unsigned. |
David Majnemer | 0ad9231 | 2011-07-22 21:09:04 +0000 | [diff] [blame] | 1654 | bool FromIsSigned = FromType->isSignedIntegerType(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1655 | uint64_t FromSize = Context.getTypeSize(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1656 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1657 | // The types we'll try to promote to, in the appropriate |
| 1658 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1659 | QualType PromoteTypes[6] = { |
| 1660 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1661 | Context.LongTy, Context.UnsignedLongTy , |
| 1662 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1663 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1664 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1665 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1666 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1667 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1668 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1669 | // We found the type that we can promote to. If this is the |
| 1670 | // type we wanted, we have a promotion. Otherwise, no |
| 1671 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1672 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1673 | } |
| 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1678 | // rvalue of type int if int can represent all the values of the |
| 1679 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1680 | // unsigned int can represent all the values of the bit-field. If |
| 1681 | // the bit-field is larger yet, no integral promotion applies to |
| 1682 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1683 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1684 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1685 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1686 | using llvm::APSInt; |
| 1687 | if (From) |
| 1688 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1689 | APSInt BitWidth; |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1690 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1691 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1692 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1693 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1694 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1695 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1696 | if (BitWidth < ToSize || |
| 1697 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1698 | return To->getKind() == BuiltinType::Int; |
| 1699 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1700 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1701 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1702 | // that fits into an unsigned int? |
| 1703 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1704 | return To->getKind() == BuiltinType::UInt; |
| 1705 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1706 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1707 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1708 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1709 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1710 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1711 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1712 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1713 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1714 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1715 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1716 | |
| 1717 | return false; |
| 1718 | } |
| 1719 | |
| 1720 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1721 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1722 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1723 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1724 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1725 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1726 | /// An rvalue of type float can be converted to an rvalue of type |
| 1727 | /// double. (C++ 4.6p1). |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1728 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1729 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1730 | return true; |
| 1731 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1732 | // C99 6.3.1.5p1: |
| 1733 | // When a float is promoted to double or long double, or a |
| 1734 | // double is promoted to long double [...]. |
| 1735 | if (!getLangOptions().CPlusPlus && |
| 1736 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1737 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1738 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1739 | return true; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1740 | |
| 1741 | // Half can be promoted to float. |
| 1742 | if (FromBuiltin->getKind() == BuiltinType::Half && |
| 1743 | ToBuiltin->getKind() == BuiltinType::Float) |
| 1744 | return true; |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1747 | return false; |
| 1748 | } |
| 1749 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1750 | /// \brief Determine if a conversion is a complex promotion. |
| 1751 | /// |
| 1752 | /// A complex promotion is defined as a complex -> complex conversion |
| 1753 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1754 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1755 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1756 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1757 | if (!FromComplex) |
| 1758 | return false; |
| 1759 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1760 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1761 | if (!ToComplex) |
| 1762 | return false; |
| 1763 | |
| 1764 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1765 | ToComplex->getElementType()) || |
| 1766 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1767 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1770 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1771 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1772 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1773 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1774 | /// the right set of qualifiers on its pointee. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1775 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1776 | static QualType |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1777 | BuildSimilarlyQualifiedPointerType(const Type *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1778 | QualType ToPointee, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1779 | ASTContext &Context, |
| 1780 | bool StripObjCLifetime = false) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1781 | assert((FromPtr->getTypeClass() == Type::Pointer || |
| 1782 | FromPtr->getTypeClass() == Type::ObjCObjectPointer) && |
| 1783 | "Invalid similarly-qualified pointer type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1784 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1785 | /// Conversions to 'id' subsume cv-qualifier conversions. |
| 1786 | if (ToType->isObjCIdType() || ToType->isObjCQualifiedIdType()) |
Douglas Gregor | 143c7ac | 2010-12-06 22:09:19 +0000 | [diff] [blame] | 1787 | return ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1788 | |
| 1789 | QualType CanonFromPointee |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1790 | = Context.getCanonicalType(FromPtr->getPointeeType()); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1791 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1792 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1793 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1794 | if (StripObjCLifetime) |
| 1795 | Quals.removeObjCLifetime(); |
| 1796 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1797 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1798 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1799 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1800 | if (!ToType.isNull()) |
Douglas Gregor | af7bea5 | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1801 | return ToType.getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1802 | |
| 1803 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1804 | // already. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1805 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1806 | return Context.getObjCObjectPointerType(ToPointee); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1807 | return Context.getPointerType(ToPointee); |
| 1808 | } |
| 1809 | |
| 1810 | // Just build a canonical type that has the right qualifiers. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1811 | QualType QualifiedCanonToPointee |
| 1812 | = Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1813 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1814 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1815 | return Context.getObjCObjectPointerType(QualifiedCanonToPointee); |
| 1816 | return Context.getPointerType(QualifiedCanonToPointee); |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1817 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1818 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1819 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1820 | bool InOverloadResolution, |
| 1821 | ASTContext &Context) { |
| 1822 | // Handle value-dependent integral null pointer constants correctly. |
| 1823 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1824 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1825 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1826 | return !InOverloadResolution; |
| 1827 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1828 | return Expr->isNullPointerConstant(Context, |
| 1829 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1830 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1831 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1832 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1833 | /// IsPointerConversion - Determines whether the conversion of the |
| 1834 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1835 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1836 | /// 4.10). If so, returns true and places the converted type (that |
| 1837 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1838 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1839 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1840 | /// This routine also supports conversions to and from block pointers |
| 1841 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1842 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1843 | /// appropriate overloading rules for Objective-C, we may want to |
| 1844 | /// split the Objective-C checks into a different routine; however, |
| 1845 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1846 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1847 | /// set if the conversion is an allowed Objective-C conversion that |
| 1848 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1849 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1850 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1851 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1852 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1853 | IncompatibleObjC = false; |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1854 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, |
| 1855 | IncompatibleObjC)) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1856 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1857 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1858 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1859 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1860 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1861 | ConvertedType = ToType; |
| 1862 | return true; |
| 1863 | } |
| 1864 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1865 | // Blocks: Block pointers can be converted to void*. |
| 1866 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1867 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1868 | ConvertedType = ToType; |
| 1869 | return true; |
| 1870 | } |
| 1871 | // Blocks: A null pointer constant can be converted to a block |
| 1872 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1873 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1874 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1875 | ConvertedType = ToType; |
| 1876 | return true; |
| 1877 | } |
| 1878 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1879 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1880 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1881 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1882 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1883 | ConvertedType = ToType; |
| 1884 | return true; |
| 1885 | } |
| 1886 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1887 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1888 | if (!ToTypePtr) |
| 1889 | return false; |
| 1890 | |
| 1891 | // 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] | 1892 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1893 | ConvertedType = ToType; |
| 1894 | return true; |
| 1895 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1896 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1897 | // Beyond this point, both types need to be pointers |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1898 | // , including objective-c pointers. |
| 1899 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1900 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType() && |
| 1901 | !getLangOptions().ObjCAutoRefCount) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1902 | ConvertedType = BuildSimilarlyQualifiedPointerType( |
| 1903 | FromType->getAs<ObjCObjectPointerType>(), |
| 1904 | ToPointeeType, |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1905 | ToType, Context); |
| 1906 | return true; |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1907 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1908 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1909 | if (!FromTypePtr) |
| 1910 | return false; |
| 1911 | |
| 1912 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1913 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1914 | // 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] | 1915 | // pointer conversion, so don't do all of the work below. |
| 1916 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
| 1917 | return false; |
| 1918 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1919 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1920 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1921 | // 4.10p2). |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1922 | if (FromPointeeType->isIncompleteOrObjectType() && |
| 1923 | ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1924 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1925 | ToPointeeType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1926 | ToType, Context, |
| 1927 | /*StripObjCLifetime=*/true); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1928 | return true; |
| 1929 | } |
| 1930 | |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 1931 | // MSVC allows implicit function to void* type conversion. |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 1932 | if (getLangOptions().MicrosoftExt && FromPointeeType->isFunctionType() && |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 1933 | ToPointeeType->isVoidType()) { |
| 1934 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 1935 | ToPointeeType, |
| 1936 | ToType, Context); |
| 1937 | return true; |
| 1938 | } |
| 1939 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1940 | // When we're overloading in C, we allow a special kind of pointer |
| 1941 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1942 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1943 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1944 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1945 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1946 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1947 | return true; |
| 1948 | } |
| 1949 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1950 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1951 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1952 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1953 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1954 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1955 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1956 | // necessitates this conversion is ill-formed. The result of the |
| 1957 | // conversion is a pointer to the base class sub-object of the |
| 1958 | // derived class object. The null pointer value is converted to |
| 1959 | // the null pointer value of the destination type. |
| 1960 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1961 | // Note that we do not check for ambiguity or inaccessibility |
| 1962 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1963 | if (getLangOptions().CPlusPlus && |
| 1964 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | bf1764c | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 1965 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | 2685eab | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1966 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1967 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1968 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1969 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1970 | ToType, Context); |
| 1971 | return true; |
| 1972 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1973 | |
Fariborz Jahanian | 5da3c08 | 2011-04-14 20:33:36 +0000 | [diff] [blame] | 1974 | if (FromPointeeType->isVectorType() && ToPointeeType->isVectorType() && |
| 1975 | Context.areCompatibleVectorTypes(FromPointeeType, ToPointeeType)) { |
| 1976 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 1977 | ToPointeeType, |
| 1978 | ToType, Context); |
| 1979 | return true; |
| 1980 | } |
| 1981 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1982 | return false; |
| 1983 | } |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1984 | |
| 1985 | /// \brief Adopt the given qualifiers for the given type. |
| 1986 | static QualType AdoptQualifiers(ASTContext &Context, QualType T, Qualifiers Qs){ |
| 1987 | Qualifiers TQs = T.getQualifiers(); |
| 1988 | |
| 1989 | // Check whether qualifiers already match. |
| 1990 | if (TQs == Qs) |
| 1991 | return T; |
| 1992 | |
| 1993 | if (Qs.compatiblyIncludes(TQs)) |
| 1994 | return Context.getQualifiedType(T, Qs); |
| 1995 | |
| 1996 | return Context.getQualifiedType(T.getUnqualifiedType(), Qs); |
| 1997 | } |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1998 | |
| 1999 | /// isObjCPointerConversion - Determines whether this is an |
| 2000 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 2001 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2002 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2003 | QualType& ConvertedType, |
| 2004 | bool &IncompatibleObjC) { |
| 2005 | if (!getLangOptions().ObjC1) |
| 2006 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2007 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2008 | // The set of qualifiers on the type we're converting from. |
| 2009 | Qualifiers FromQualifiers = FromType.getQualifiers(); |
| 2010 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2011 | // First, we handle all conversions on ObjC object pointer types. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2012 | const ObjCObjectPointerType* ToObjCPtr = |
| 2013 | ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2014 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2015 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2016 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2017 | if (ToObjCPtr && FromObjCPtr) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2018 | // If the pointee types are the same (ignoring qualifications), |
| 2019 | // then this is not a pointer conversion. |
| 2020 | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
| 2021 | FromObjCPtr->getPointeeType())) |
| 2022 | return false; |
| 2023 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2024 | // Check for compatible |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2025 | // 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] | 2026 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2027 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2028 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2029 | return true; |
| 2030 | } |
| 2031 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2032 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2033 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2034 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 2035 | /*compare=*/false)) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2036 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2037 | return true; |
| 2038 | } |
| 2039 | // Objective C++: We're able to convert from a pointer to an |
| 2040 | // interface to a pointer to a different interface. |
| 2041 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2042 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 2043 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
| 2044 | if (getLangOptions().CPlusPlus && LHS && RHS && |
| 2045 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 2046 | FromObjCPtr->getPointeeType())) |
| 2047 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2048 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2049 | ToObjCPtr->getPointeeType(), |
| 2050 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2051 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2052 | return true; |
| 2053 | } |
| 2054 | |
| 2055 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 2056 | // Okay: this is some kind of implicit downcast of Objective-C |
| 2057 | // interfaces, which is permitted. However, we're going to |
| 2058 | // complain about it. |
| 2059 | IncompatibleObjC = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2060 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2061 | ToObjCPtr->getPointeeType(), |
| 2062 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2063 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2064 | return true; |
| 2065 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2066 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2067 | // 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] | 2068 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2069 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2070 | ToPointeeType = ToCPtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2071 | else if (const BlockPointerType *ToBlockPtr = |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2072 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 2073 | // 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] | 2074 | // to a block pointer type. |
| 2075 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2076 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2077 | return true; |
| 2078 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2079 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2080 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2081 | else if (FromType->getAs<BlockPointerType>() && |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2082 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2083 | // 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] | 2084 | // pointer to any object. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2085 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2086 | return true; |
| 2087 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2088 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2089 | return false; |
| 2090 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2091 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2092 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2093 | FromPointeeType = FromCPtr->getPointeeType(); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2094 | else if (const BlockPointerType *FromBlockPtr = |
| 2095 | FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2096 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2097 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2098 | return false; |
| 2099 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2100 | // If we have pointers to pointers, recursively check whether this |
| 2101 | // is an Objective-C conversion. |
| 2102 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 2103 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2104 | IncompatibleObjC)) { |
| 2105 | // We always complain about this conversion. |
| 2106 | IncompatibleObjC = true; |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2107 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2108 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2109 | return true; |
| 2110 | } |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2111 | // Allow conversion of pointee being objective-c pointer to another one; |
| 2112 | // as in I* to id. |
| 2113 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 2114 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 2115 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2116 | IncompatibleObjC)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2117 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2118 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2119 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2120 | return true; |
| 2121 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2122 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2123 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2124 | // differences in the argument and result types are in Objective-C |
| 2125 | // pointer conversions. If so, we permit the conversion (but |
| 2126 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2127 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2128 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2129 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2130 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2131 | if (FromFunctionType && ToFunctionType) { |
| 2132 | // If the function types are exactly the same, this isn't an |
| 2133 | // Objective-C pointer conversion. |
| 2134 | if (Context.getCanonicalType(FromPointeeType) |
| 2135 | == Context.getCanonicalType(ToPointeeType)) |
| 2136 | return false; |
| 2137 | |
| 2138 | // Perform the quick checks that will tell us whether these |
| 2139 | // function types are obviously different. |
| 2140 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2141 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 2142 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 2143 | return false; |
| 2144 | |
| 2145 | bool HasObjCConversion = false; |
| 2146 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 2147 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 2148 | // Okay, the types match exactly. Nothing to do. |
| 2149 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 2150 | ToFunctionType->getResultType(), |
| 2151 | ConvertedType, IncompatibleObjC)) { |
| 2152 | // Okay, we have an Objective-C pointer conversion. |
| 2153 | HasObjCConversion = true; |
| 2154 | } else { |
| 2155 | // Function types are too different. Abort. |
| 2156 | return false; |
| 2157 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2158 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2159 | // Check argument types. |
| 2160 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2161 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2162 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2163 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2164 | if (Context.getCanonicalType(FromArgType) |
| 2165 | == Context.getCanonicalType(ToArgType)) { |
| 2166 | // Okay, the types match exactly. Nothing to do. |
| 2167 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 2168 | ConvertedType, IncompatibleObjC)) { |
| 2169 | // Okay, we have an Objective-C pointer conversion. |
| 2170 | HasObjCConversion = true; |
| 2171 | } else { |
| 2172 | // Argument types are too different. Abort. |
| 2173 | return false; |
| 2174 | } |
| 2175 | } |
| 2176 | |
| 2177 | if (HasObjCConversion) { |
| 2178 | // We had an Objective-C conversion. Allow this pointer |
| 2179 | // conversion, but complain about it. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2180 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2181 | IncompatibleObjC = true; |
| 2182 | return true; |
| 2183 | } |
| 2184 | } |
| 2185 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2186 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2187 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2188 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2189 | /// \brief Determine whether this is an Objective-C writeback conversion, |
| 2190 | /// used for parameter passing when performing automatic reference counting. |
| 2191 | /// |
| 2192 | /// \param FromType The type we're converting form. |
| 2193 | /// |
| 2194 | /// \param ToType The type we're converting to. |
| 2195 | /// |
| 2196 | /// \param ConvertedType The type that will be produced after applying |
| 2197 | /// this conversion. |
| 2198 | bool Sema::isObjCWritebackConversion(QualType FromType, QualType ToType, |
| 2199 | QualType &ConvertedType) { |
| 2200 | if (!getLangOptions().ObjCAutoRefCount || |
| 2201 | Context.hasSameUnqualifiedType(FromType, ToType)) |
| 2202 | return false; |
| 2203 | |
| 2204 | // Parameter must be a pointer to __autoreleasing (with no other qualifiers). |
| 2205 | QualType ToPointee; |
| 2206 | if (const PointerType *ToPointer = ToType->getAs<PointerType>()) |
| 2207 | ToPointee = ToPointer->getPointeeType(); |
| 2208 | else |
| 2209 | return false; |
| 2210 | |
| 2211 | Qualifiers ToQuals = ToPointee.getQualifiers(); |
| 2212 | if (!ToPointee->isObjCLifetimeType() || |
| 2213 | ToQuals.getObjCLifetime() != Qualifiers::OCL_Autoreleasing || |
| 2214 | !ToQuals.withoutObjCGLifetime().empty()) |
| 2215 | return false; |
| 2216 | |
| 2217 | // Argument must be a pointer to __strong to __weak. |
| 2218 | QualType FromPointee; |
| 2219 | if (const PointerType *FromPointer = FromType->getAs<PointerType>()) |
| 2220 | FromPointee = FromPointer->getPointeeType(); |
| 2221 | else |
| 2222 | return false; |
| 2223 | |
| 2224 | Qualifiers FromQuals = FromPointee.getQualifiers(); |
| 2225 | if (!FromPointee->isObjCLifetimeType() || |
| 2226 | (FromQuals.getObjCLifetime() != Qualifiers::OCL_Strong && |
| 2227 | FromQuals.getObjCLifetime() != Qualifiers::OCL_Weak)) |
| 2228 | return false; |
| 2229 | |
| 2230 | // Make sure that we have compatible qualifiers. |
| 2231 | FromQuals.setObjCLifetime(Qualifiers::OCL_Autoreleasing); |
| 2232 | if (!ToQuals.compatiblyIncludes(FromQuals)) |
| 2233 | return false; |
| 2234 | |
| 2235 | // Remove qualifiers from the pointee type we're converting from; they |
| 2236 | // aren't used in the compatibility check belong, and we'll be adding back |
| 2237 | // qualifiers (with __autoreleasing) if the compatibility check succeeds. |
| 2238 | FromPointee = FromPointee.getUnqualifiedType(); |
| 2239 | |
| 2240 | // The unqualified form of the pointee types must be compatible. |
| 2241 | ToPointee = ToPointee.getUnqualifiedType(); |
| 2242 | bool IncompatibleObjC; |
| 2243 | if (Context.typesAreCompatible(FromPointee, ToPointee)) |
| 2244 | FromPointee = ToPointee; |
| 2245 | else if (!isObjCPointerConversion(FromPointee, ToPointee, FromPointee, |
| 2246 | IncompatibleObjC)) |
| 2247 | return false; |
| 2248 | |
| 2249 | /// \brief Construct the type we're converting to, which is a pointer to |
| 2250 | /// __autoreleasing pointee. |
| 2251 | FromPointee = Context.getQualifiedType(FromPointee, FromQuals); |
| 2252 | ConvertedType = Context.getPointerType(FromPointee); |
| 2253 | return true; |
| 2254 | } |
| 2255 | |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2256 | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
| 2257 | QualType& ConvertedType) { |
| 2258 | QualType ToPointeeType; |
| 2259 | if (const BlockPointerType *ToBlockPtr = |
| 2260 | ToType->getAs<BlockPointerType>()) |
| 2261 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 2262 | else |
| 2263 | return false; |
| 2264 | |
| 2265 | QualType FromPointeeType; |
| 2266 | if (const BlockPointerType *FromBlockPtr = |
| 2267 | FromType->getAs<BlockPointerType>()) |
| 2268 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2269 | else |
| 2270 | return false; |
| 2271 | // We have pointer to blocks, check whether the only |
| 2272 | // differences in the argument and result types are in Objective-C |
| 2273 | // pointer conversions. If so, we permit the conversion. |
| 2274 | |
| 2275 | const FunctionProtoType *FromFunctionType |
| 2276 | = FromPointeeType->getAs<FunctionProtoType>(); |
| 2277 | const FunctionProtoType *ToFunctionType |
| 2278 | = ToPointeeType->getAs<FunctionProtoType>(); |
| 2279 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2280 | if (!FromFunctionType || !ToFunctionType) |
| 2281 | return false; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2282 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2283 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2284 | return true; |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2285 | |
| 2286 | // Perform the quick checks that will tell us whether these |
| 2287 | // function types are obviously different. |
| 2288 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2289 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
| 2290 | return false; |
| 2291 | |
| 2292 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
| 2293 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
| 2294 | if (FromEInfo != ToEInfo) |
| 2295 | return false; |
| 2296 | |
| 2297 | bool IncompatibleObjC = false; |
Fariborz Jahanian | 462dae5 | 2011-02-13 20:11:42 +0000 | [diff] [blame] | 2298 | if (Context.hasSameType(FromFunctionType->getResultType(), |
| 2299 | ToFunctionType->getResultType())) { |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2300 | // Okay, the types match exactly. Nothing to do. |
| 2301 | } else { |
| 2302 | QualType RHS = FromFunctionType->getResultType(); |
| 2303 | QualType LHS = ToFunctionType->getResultType(); |
| 2304 | if ((!getLangOptions().CPlusPlus || !RHS->isRecordType()) && |
| 2305 | !RHS.hasQualifiers() && LHS.hasQualifiers()) |
| 2306 | LHS = LHS.getUnqualifiedType(); |
| 2307 | |
| 2308 | if (Context.hasSameType(RHS,LHS)) { |
| 2309 | // OK exact match. |
| 2310 | } else if (isObjCPointerConversion(RHS, LHS, |
| 2311 | ConvertedType, IncompatibleObjC)) { |
| 2312 | if (IncompatibleObjC) |
| 2313 | return false; |
| 2314 | // Okay, we have an Objective-C pointer conversion. |
| 2315 | } |
| 2316 | else |
| 2317 | return false; |
| 2318 | } |
| 2319 | |
| 2320 | // Check argument types. |
| 2321 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2322 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2323 | IncompatibleObjC = false; |
| 2324 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2325 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2326 | if (Context.hasSameType(FromArgType, ToArgType)) { |
| 2327 | // Okay, the types match exactly. Nothing to do. |
| 2328 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
| 2329 | ConvertedType, IncompatibleObjC)) { |
| 2330 | if (IncompatibleObjC) |
| 2331 | return false; |
| 2332 | // Okay, we have an Objective-C pointer conversion. |
| 2333 | } else |
| 2334 | // Argument types are too different. Abort. |
| 2335 | return false; |
| 2336 | } |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 2337 | if (LangOpts.ObjCAutoRefCount && |
| 2338 | !Context.FunctionTypesMatchOnNSConsumedAttrs(FromFunctionType, |
| 2339 | ToFunctionType)) |
| 2340 | return false; |
Fariborz Jahanian | f9d9527 | 2011-09-28 20:22:05 +0000 | [diff] [blame] | 2341 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2342 | ConvertedType = ToType; |
| 2343 | return true; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2344 | } |
| 2345 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2346 | enum { |
| 2347 | ft_default, |
| 2348 | ft_different_class, |
| 2349 | ft_parameter_arity, |
| 2350 | ft_parameter_mismatch, |
| 2351 | ft_return_type, |
| 2352 | ft_qualifer_mismatch |
| 2353 | }; |
| 2354 | |
| 2355 | /// HandleFunctionTypeMismatch - Gives diagnostic information for differeing |
| 2356 | /// function types. Catches different number of parameter, mismatch in |
| 2357 | /// parameter types, and different return types. |
| 2358 | void Sema::HandleFunctionTypeMismatch(PartialDiagnostic &PDiag, |
| 2359 | QualType FromType, QualType ToType) { |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2360 | // If either type is not valid, include no extra info. |
| 2361 | if (FromType.isNull() || ToType.isNull()) { |
| 2362 | PDiag << ft_default; |
| 2363 | return; |
| 2364 | } |
| 2365 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2366 | // Get the function type from the pointers. |
| 2367 | if (FromType->isMemberPointerType() && ToType->isMemberPointerType()) { |
| 2368 | const MemberPointerType *FromMember = FromType->getAs<MemberPointerType>(), |
| 2369 | *ToMember = ToType->getAs<MemberPointerType>(); |
| 2370 | if (FromMember->getClass() != ToMember->getClass()) { |
| 2371 | PDiag << ft_different_class << QualType(ToMember->getClass(), 0) |
| 2372 | << QualType(FromMember->getClass(), 0); |
| 2373 | return; |
| 2374 | } |
| 2375 | FromType = FromMember->getPointeeType(); |
| 2376 | ToType = ToMember->getPointeeType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2379 | if (FromType->isPointerType()) |
| 2380 | FromType = FromType->getPointeeType(); |
| 2381 | if (ToType->isPointerType()) |
| 2382 | ToType = ToType->getPointeeType(); |
| 2383 | |
| 2384 | // Remove references. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2385 | FromType = FromType.getNonReferenceType(); |
| 2386 | ToType = ToType.getNonReferenceType(); |
| 2387 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2388 | // Don't print extra info for non-specialized template functions. |
| 2389 | if (FromType->isInstantiationDependentType() && |
| 2390 | !FromType->getAs<TemplateSpecializationType>()) { |
| 2391 | PDiag << ft_default; |
| 2392 | return; |
| 2393 | } |
| 2394 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2395 | // No extra info for same types. |
| 2396 | if (Context.hasSameType(FromType, ToType)) { |
| 2397 | PDiag << ft_default; |
| 2398 | return; |
| 2399 | } |
| 2400 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2401 | const FunctionProtoType *FromFunction = FromType->getAs<FunctionProtoType>(), |
| 2402 | *ToFunction = ToType->getAs<FunctionProtoType>(); |
| 2403 | |
| 2404 | // Both types need to be function types. |
| 2405 | if (!FromFunction || !ToFunction) { |
| 2406 | PDiag << ft_default; |
| 2407 | return; |
| 2408 | } |
| 2409 | |
| 2410 | if (FromFunction->getNumArgs() != ToFunction->getNumArgs()) { |
| 2411 | PDiag << ft_parameter_arity << ToFunction->getNumArgs() |
| 2412 | << FromFunction->getNumArgs(); |
| 2413 | return; |
| 2414 | } |
| 2415 | |
| 2416 | // Handle different parameter types. |
| 2417 | unsigned ArgPos; |
| 2418 | if (!FunctionArgTypesAreEqual(FromFunction, ToFunction, &ArgPos)) { |
| 2419 | PDiag << ft_parameter_mismatch << ArgPos + 1 |
| 2420 | << ToFunction->getArgType(ArgPos) |
| 2421 | << FromFunction->getArgType(ArgPos); |
| 2422 | return; |
| 2423 | } |
| 2424 | |
| 2425 | // Handle different return type. |
| 2426 | if (!Context.hasSameType(FromFunction->getResultType(), |
| 2427 | ToFunction->getResultType())) { |
| 2428 | PDiag << ft_return_type << ToFunction->getResultType() |
| 2429 | << FromFunction->getResultType(); |
| 2430 | return; |
| 2431 | } |
| 2432 | |
| 2433 | unsigned FromQuals = FromFunction->getTypeQuals(), |
| 2434 | ToQuals = ToFunction->getTypeQuals(); |
| 2435 | if (FromQuals != ToQuals) { |
| 2436 | PDiag << ft_qualifer_mismatch << ToQuals << FromQuals; |
| 2437 | return; |
| 2438 | } |
| 2439 | |
| 2440 | // Unable to find a difference, so add no extra info. |
| 2441 | PDiag << ft_default; |
| 2442 | } |
| 2443 | |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2444 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
Douglas Gregor | dec1cc4 | 2011-12-15 17:15:07 +0000 | [diff] [blame] | 2445 | /// for equality of their argument types. Caller has already checked that |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2446 | /// they have same number of arguments. This routine assumes that Objective-C |
| 2447 | /// pointer types which only differ in their protocol qualifiers are equal. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2448 | /// If the parameters are different, ArgPos will have the the parameter index |
| 2449 | /// of the first different parameter. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2450 | bool Sema::FunctionArgTypesAreEqual(const FunctionProtoType *OldType, |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2451 | const FunctionProtoType *NewType, |
| 2452 | unsigned *ArgPos) { |
| 2453 | if (!getLangOptions().ObjC1) { |
| 2454 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 2455 | N = NewType->arg_type_begin(), |
| 2456 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
| 2457 | if (!Context.hasSameType(*O, *N)) { |
| 2458 | if (ArgPos) *ArgPos = O - OldType->arg_type_begin(); |
| 2459 | return false; |
| 2460 | } |
| 2461 | } |
| 2462 | return true; |
| 2463 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2464 | |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2465 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 2466 | N = NewType->arg_type_begin(), |
| 2467 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
| 2468 | QualType ToType = (*O); |
| 2469 | QualType FromType = (*N); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2470 | if (!Context.hasSameType(ToType, FromType)) { |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2471 | if (const PointerType *PTTo = ToType->getAs<PointerType>()) { |
| 2472 | if (const PointerType *PTFr = FromType->getAs<PointerType>()) |
Chandler Carruth | 0ee93de | 2010-05-06 00:15:06 +0000 | [diff] [blame] | 2473 | if ((PTTo->getPointeeType()->isObjCQualifiedIdType() && |
| 2474 | PTFr->getPointeeType()->isObjCQualifiedIdType()) || |
| 2475 | (PTTo->getPointeeType()->isObjCQualifiedClassType() && |
| 2476 | PTFr->getPointeeType()->isObjCQualifiedClassType())) |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2477 | continue; |
| 2478 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2479 | else if (const ObjCObjectPointerType *PTTo = |
| 2480 | ToType->getAs<ObjCObjectPointerType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2481 | if (const ObjCObjectPointerType *PTFr = |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2482 | FromType->getAs<ObjCObjectPointerType>()) |
Douglas Gregor | dec1cc4 | 2011-12-15 17:15:07 +0000 | [diff] [blame] | 2483 | if (Context.hasSameUnqualifiedType( |
| 2484 | PTTo->getObjectType()->getBaseType(), |
| 2485 | PTFr->getObjectType()->getBaseType())) |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2486 | continue; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2487 | } |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2488 | if (ArgPos) *ArgPos = O - OldType->arg_type_begin(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2489 | return false; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2490 | } |
| 2491 | } |
| 2492 | return true; |
| 2493 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2494 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2495 | /// CheckPointerConversion - Check the pointer conversion from the |
| 2496 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2497 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2498 | /// conversions for which IsPointerConversion has already returned |
| 2499 | /// true. It returns true and produces a diagnostic if there was an |
| 2500 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2501 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2502 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2503 | CXXCastPath& BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2504 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2505 | QualType FromType = From->getType(); |
Argyrios Kyrtzidis | b335872 | 2010-09-28 14:54:11 +0000 | [diff] [blame] | 2506 | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2507 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2508 | Kind = CK_BitCast; |
| 2509 | |
Chandler Carruth | 88f0aed | 2011-04-09 07:32:05 +0000 | [diff] [blame] | 2510 | if (!IsCStyleOrFunctionalCast && |
| 2511 | Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy) && |
| 2512 | From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 2513 | DiagRuntimeBehavior(From->getExprLoc(), From, |
Chandler Carruth | b600669 | 2011-04-09 07:48:17 +0000 | [diff] [blame] | 2514 | PDiag(diag::warn_impcast_bool_to_null_pointer) |
| 2515 | << ToType << From->getSourceRange()); |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 2516 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2517 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
| 2518 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2519 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 2520 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 2521 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2522 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 2523 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2524 | // We must have a derived-to-base conversion. Check an |
| 2525 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2526 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 2527 | From->getExprLoc(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2528 | From->getSourceRange(), &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2529 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2530 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2531 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2532 | // The conversion was successful. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2533 | Kind = CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2534 | } |
| 2535 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2536 | } else if (const ObjCObjectPointerType *ToPtrType = |
| 2537 | ToType->getAs<ObjCObjectPointerType>()) { |
| 2538 | if (const ObjCObjectPointerType *FromPtrType = |
| 2539 | FromType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2540 | // Objective-C++ conversions are always okay. |
| 2541 | // FIXME: We should have a different class of conversions for the |
| 2542 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2543 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2544 | return false; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2545 | } else if (FromType->isBlockPointerType()) { |
| 2546 | Kind = CK_BlockPointerToObjCPointerCast; |
| 2547 | } else { |
| 2548 | Kind = CK_CPointerToObjCPointerCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2549 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2550 | } else if (ToType->isBlockPointerType()) { |
| 2551 | if (!FromType->isBlockPointerType()) |
| 2552 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2553 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2554 | |
| 2555 | // We shouldn't fall into this case unless it's valid for other |
| 2556 | // reasons. |
| 2557 | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 2558 | Kind = CK_NullToPointer; |
| 2559 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2560 | return false; |
| 2561 | } |
| 2562 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2563 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 2564 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 2565 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 2566 | /// If so, returns true and places the converted type (that might differ from |
| 2567 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 2568 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2569 | QualType ToType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2570 | bool InOverloadResolution, |
| 2571 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2572 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2573 | if (!ToTypePtr) |
| 2574 | return false; |
| 2575 | |
| 2576 | // 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] | 2577 | if (From->isNullPointerConstant(Context, |
| 2578 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 2579 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2580 | ConvertedType = ToType; |
| 2581 | return true; |
| 2582 | } |
| 2583 | |
| 2584 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2585 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2586 | if (!FromTypePtr) |
| 2587 | return false; |
| 2588 | |
| 2589 | // A pointer to member of B can be converted to a pointer to member of D, |
| 2590 | // where D is derived from B (C++ 4.11p2). |
| 2591 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 2592 | QualType ToClass(ToTypePtr->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2593 | |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2594 | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
| 2595 | !RequireCompleteType(From->getLocStart(), ToClass, PDiag()) && |
| 2596 | IsDerivedFrom(ToClass, FromClass)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2597 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 2598 | ToClass.getTypePtr()); |
| 2599 | return true; |
| 2600 | } |
| 2601 | |
| 2602 | return false; |
| 2603 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2604 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2605 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 2606 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2607 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2608 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 2609 | /// true and produces a diagnostic if there was an error, or returns false |
| 2610 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2611 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2612 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2613 | CXXCastPath &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2614 | bool IgnoreBaseAccess) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2615 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2616 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2617 | if (!FromPtrType) { |
| 2618 | // This must be a null pointer to member pointer conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2619 | assert(From->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2620 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2621 | "Expr must be null pointer constant!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2622 | Kind = CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2623 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2624 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2625 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2626 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2627 | assert(ToPtrType && "No member pointer cast has a target type " |
| 2628 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2629 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2630 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 2631 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2632 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2633 | // FIXME: What about dependent types? |
| 2634 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 2635 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2636 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2637 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 2638 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2639 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 2640 | assert(DerivationOkay && |
| 2641 | "Should not have been called if derivation isn't OK."); |
| 2642 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2643 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2644 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 2645 | getUnqualifiedType())) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2646 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 2647 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 2648 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 2649 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2650 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2651 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2652 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2653 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 2654 | << FromClass << ToClass << QualType(VBase, 0) |
| 2655 | << From->getSourceRange(); |
| 2656 | return true; |
| 2657 | } |
| 2658 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2659 | if (!IgnoreBaseAccess) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2660 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 2661 | Paths.front(), |
| 2662 | diag::err_downcast_from_inaccessible_base); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2663 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2664 | // Must be a base to derived member conversion. |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2665 | BuildBasePathArray(Paths, BasePath); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2666 | Kind = CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2667 | return false; |
| 2668 | } |
| 2669 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2670 | /// IsQualificationConversion - Determines whether the conversion from |
| 2671 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 2672 | /// (C++ 4.4). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2673 | /// |
| 2674 | /// \param ObjCLifetimeConversion Output parameter that will be set to indicate |
| 2675 | /// when the qualification conversion involves a change in the Objective-C |
| 2676 | /// object lifetime. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2677 | bool |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2678 | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2679 | bool CStyle, bool &ObjCLifetimeConversion) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2680 | FromType = Context.getCanonicalType(FromType); |
| 2681 | ToType = Context.getCanonicalType(ToType); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2682 | ObjCLifetimeConversion = false; |
| 2683 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2684 | // If FromType and ToType are the same type, this is not a |
| 2685 | // qualification conversion. |
Sebastian Redl | 22c9240 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 2686 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2687 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2688 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2689 | // (C++ 4.4p4): |
| 2690 | // A conversion can add cv-qualifiers at levels other than the first |
| 2691 | // in multi-level pointers, subject to the following rules: [...] |
| 2692 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2693 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2694 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2695 | // Within each iteration of the loop, we check the qualifiers to |
| 2696 | // determine if this still looks like a qualification |
| 2697 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2698 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2699 | // until there are no more pointers or pointers-to-members left to |
| 2700 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2701 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2702 | |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2703 | Qualifiers FromQuals = FromType.getQualifiers(); |
| 2704 | Qualifiers ToQuals = ToType.getQualifiers(); |
| 2705 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2706 | // Objective-C ARC: |
| 2707 | // Check Objective-C lifetime conversions. |
| 2708 | if (FromQuals.getObjCLifetime() != ToQuals.getObjCLifetime() && |
| 2709 | UnwrappedAnyPointer) { |
| 2710 | if (ToQuals.compatiblyIncludesObjCLifetime(FromQuals)) { |
| 2711 | ObjCLifetimeConversion = true; |
| 2712 | FromQuals.removeObjCLifetime(); |
| 2713 | ToQuals.removeObjCLifetime(); |
| 2714 | } else { |
| 2715 | // Qualification conversions cannot cast between different |
| 2716 | // Objective-C lifetime qualifiers. |
| 2717 | return false; |
| 2718 | } |
| 2719 | } |
| 2720 | |
Douglas Gregor | 377e1bd | 2011-05-08 06:09:53 +0000 | [diff] [blame] | 2721 | // Allow addition/removal of GC attributes but not changing GC attributes. |
| 2722 | if (FromQuals.getObjCGCAttr() != ToQuals.getObjCGCAttr() && |
| 2723 | (!FromQuals.hasObjCGCAttr() || !ToQuals.hasObjCGCAttr())) { |
| 2724 | FromQuals.removeObjCGCAttr(); |
| 2725 | ToQuals.removeObjCGCAttr(); |
| 2726 | } |
| 2727 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2728 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 2729 | // 2,j, and similarly for volatile. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2730 | if (!CStyle && !ToQuals.compatiblyIncludes(FromQuals)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2731 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2732 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2733 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 2734 | // every cv for 0 < k < j. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2735 | if (!CStyle && FromQuals.getCVRQualifiers() != ToQuals.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2736 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2737 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2738 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2739 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 2740 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2741 | PreviousToQualsIncludeConst |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2742 | = PreviousToQualsIncludeConst && ToQuals.hasConst(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2743 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2744 | |
| 2745 | // We are left with FromType and ToType being the pointee types |
| 2746 | // after unwrapping the original FromType and ToType the same number |
| 2747 | // of types. If we unwrapped any pointers, and if FromType and |
| 2748 | // ToType have the same unqualified type (since we checked |
| 2749 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2750 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2753 | /// Determines whether there is a user-defined conversion sequence |
| 2754 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 2755 | /// ToType. If such a conversion exists, User will contain the |
| 2756 | /// user-defined conversion sequence that performs such a conversion |
| 2757 | /// and this routine will return true. Otherwise, this routine returns |
| 2758 | /// false and User is unspecified. |
| 2759 | /// |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2760 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 2761 | /// "explicit" conversion functions as well as non-explicit conversion |
| 2762 | /// functions (C++0x [class.conv.fct]p2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2763 | static OverloadingResult |
| 2764 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 2765 | UserDefinedConversionSequence& User, |
| 2766 | OverloadCandidateSet& CandidateSet, |
| 2767 | bool AllowExplicit) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2768 | // Whether we will only visit constructors. |
| 2769 | bool ConstructorsOnly = false; |
| 2770 | |
| 2771 | // If the type we are conversion to is a class type, enumerate its |
| 2772 | // constructors. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2773 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2774 | // C++ [over.match.ctor]p1: |
| 2775 | // When objects of class type are direct-initialized (8.5), or |
| 2776 | // copy-initialized from an expression of the same or a |
| 2777 | // derived class type (8.5), overload resolution selects the |
| 2778 | // constructor. [...] For copy-initialization, the candidate |
| 2779 | // functions are all the converting constructors (12.3.1) of |
| 2780 | // that class. The argument list is the expression-list within |
| 2781 | // the parentheses of the initializer. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2782 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2783 | (From->getType()->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2784 | S.IsDerivedFrom(From->getType(), ToType))) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2785 | ConstructorsOnly = true; |
| 2786 | |
Argyrios Kyrtzidis | e36bca6 | 2011-04-22 17:45:37 +0000 | [diff] [blame] | 2787 | S.RequireCompleteType(From->getLocStart(), ToType, S.PDiag()); |
| 2788 | // RequireCompleteType may have returned true due to some invalid decl |
| 2789 | // during template instantiation, but ToType may be complete enough now |
| 2790 | // to try to recover. |
| 2791 | if (ToType->isIncompleteType()) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 2792 | // We're not going to find any constructors. |
| 2793 | } else if (CXXRecordDecl *ToRecordDecl |
| 2794 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2795 | |
| 2796 | Expr **Args = &From; |
| 2797 | unsigned NumArgs = 1; |
| 2798 | bool ListInitializing = false; |
| 2799 | // If we're list-initializing, we pass the individual elements as |
| 2800 | // arguments, not the entire list. |
| 2801 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(From)) { |
| 2802 | Args = InitList->getInits(); |
| 2803 | NumArgs = InitList->getNumInits(); |
| 2804 | ListInitializing = true; |
| 2805 | } |
| 2806 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2807 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2808 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(ToRecordDecl); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2809 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2810 | NamedDecl *D = *Con; |
| 2811 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2812 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2813 | // Find the constructor (which may be a template). |
| 2814 | CXXConstructorDecl *Constructor = 0; |
| 2815 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2816 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2817 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2818 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2819 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 2820 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2821 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2822 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2823 | bool Usable = !Constructor->isInvalidDecl(); |
| 2824 | if (ListInitializing) |
| 2825 | Usable = Usable && (AllowExplicit || !Constructor->isExplicit()); |
| 2826 | else |
| 2827 | Usable = Usable &&Constructor->isConvertingConstructor(AllowExplicit); |
| 2828 | if (Usable) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2829 | if (ConstructorTmpl) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2830 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2831 | /*ExplicitArgs*/ 0, |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2832 | Args, NumArgs, CandidateSet, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2833 | /*SuppressUserConversions=*/ |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2834 | !ConstructorsOnly && |
| 2835 | !ListInitializing); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2836 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 2837 | // Allow one user-defined conversion when user specifies a |
| 2838 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2839 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2840 | Args, NumArgs, CandidateSet, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2841 | /*SuppressUserConversions=*/ |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2842 | !ConstructorsOnly && !ListInitializing); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2843 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2844 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2845 | } |
| 2846 | } |
| 2847 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2848 | // Enumerate conversion functions, if we're allowed to. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2849 | if (ConstructorsOnly || isa<InitListExpr>(From)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2850 | } else if (S.RequireCompleteType(From->getLocStart(), From->getType(), |
| 2851 | S.PDiag(0) << From->getSourceRange())) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2852 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2853 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2854 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2855 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2856 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 2857 | // Add all of the conversion functions as candidates. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2858 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 2859 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2860 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2861 | E = Conversions->end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2862 | DeclAccessPair FoundDecl = I.getPair(); |
| 2863 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2864 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2865 | if (isa<UsingShadowDecl>(D)) |
| 2866 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2867 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2868 | CXXConversionDecl *Conv; |
| 2869 | FunctionTemplateDecl *ConvTemplate; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2870 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 2871 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2872 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2873 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2874 | |
| 2875 | if (AllowExplicit || !Conv->isExplicit()) { |
| 2876 | if (ConvTemplate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2877 | S.AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
| 2878 | ActingContext, From, ToType, |
| 2879 | CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2880 | else |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2881 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, |
| 2882 | From, ToType, CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2883 | } |
| 2884 | } |
| 2885 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2886 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2887 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2888 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 2889 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2890 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 2891 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2892 | case OR_Success: |
| 2893 | // Record the standard conversion we used and the conversion function. |
| 2894 | if (CXXConstructorDecl *Constructor |
| 2895 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2896 | S.MarkDeclarationReferenced(From->getLocStart(), Constructor); |
| 2897 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2898 | // C++ [over.ics.user]p1: |
| 2899 | // If the user-defined conversion is specified by a |
| 2900 | // constructor (12.3.1), the initial standard conversion |
| 2901 | // sequence converts the source type to the type required by |
| 2902 | // the argument of the constructor. |
| 2903 | // |
| 2904 | QualType ThisType = Constructor->getThisType(S.Context); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2905 | if (isa<InitListExpr>(From)) { |
| 2906 | // Initializer lists don't have conversions as such. |
| 2907 | User.Before.setAsIdentityConversion(); |
| 2908 | } else { |
| 2909 | if (Best->Conversions[0].isEllipsis()) |
| 2910 | User.EllipsisConversion = true; |
| 2911 | else { |
| 2912 | User.Before = Best->Conversions[0].Standard; |
| 2913 | User.EllipsisConversion = false; |
| 2914 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2915 | } |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2916 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2917 | User.ConversionFunction = Constructor; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 2918 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2919 | User.After.setAsIdentityConversion(); |
| 2920 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 2921 | User.After.setAllToTypes(ToType); |
| 2922 | return OR_Success; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2923 | } |
| 2924 | if (CXXConversionDecl *Conversion |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2925 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2926 | S.MarkDeclarationReferenced(From->getLocStart(), Conversion); |
| 2927 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2928 | // C++ [over.ics.user]p1: |
| 2929 | // |
| 2930 | // [...] If the user-defined conversion is specified by a |
| 2931 | // conversion function (12.3.2), the initial standard |
| 2932 | // conversion sequence converts the source type to the |
| 2933 | // implicit object parameter of the conversion function. |
| 2934 | User.Before = Best->Conversions[0].Standard; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2935 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2936 | User.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 2937 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2938 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2939 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2940 | // C++ [over.ics.user]p2: |
| 2941 | // The second standard conversion sequence converts the |
| 2942 | // result of the user-defined conversion to the target type |
| 2943 | // for the sequence. Since an implicit conversion sequence |
| 2944 | // is an initialization, the special rules for |
| 2945 | // initialization by user-defined conversion apply when |
| 2946 | // selecting the best user-defined conversion for a |
| 2947 | // user-defined conversion sequence (see 13.3.3 and |
| 2948 | // 13.3.3.1). |
| 2949 | User.After = Best->FinalConversion; |
| 2950 | return OR_Success; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2951 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2952 | llvm_unreachable("Not a constructor or conversion function?"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2953 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2954 | case OR_No_Viable_Function: |
| 2955 | return OR_No_Viable_Function; |
| 2956 | case OR_Deleted: |
| 2957 | // No conversion here! We're done. |
| 2958 | return OR_Deleted; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2959 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2960 | case OR_Ambiguous: |
| 2961 | return OR_Ambiguous; |
| 2962 | } |
| 2963 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2964 | llvm_unreachable("Invalid OverloadResult!"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2965 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2966 | |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2967 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2968 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2969 | ImplicitConversionSequence ICS; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2970 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2971 | OverloadingResult OvResult = |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2972 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2973 | CandidateSet, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2974 | if (OvResult == OR_Ambiguous) |
| 2975 | Diag(From->getSourceRange().getBegin(), |
| 2976 | diag::err_typecheck_ambiguous_condition) |
| 2977 | << From->getType() << ToType << From->getSourceRange(); |
| 2978 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 2979 | Diag(From->getSourceRange().getBegin(), |
| 2980 | diag::err_typecheck_nonviable_condition) |
| 2981 | << From->getType() << ToType << From->getSourceRange(); |
| 2982 | else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2983 | return false; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2984 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &From, 1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2985 | return true; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2986 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2987 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2988 | /// CompareImplicitConversionSequences - Compare two implicit |
| 2989 | /// conversion sequences to determine whether one is better than the |
| 2990 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2991 | static ImplicitConversionSequence::CompareKind |
| 2992 | CompareImplicitConversionSequences(Sema &S, |
| 2993 | const ImplicitConversionSequence& ICS1, |
| 2994 | const ImplicitConversionSequence& ICS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2995 | { |
| 2996 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 2997 | // conversion sequences (as defined in 13.3.3.1) |
| 2998 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 2999 | // conversion sequence than a user-defined conversion sequence or |
| 3000 | // an ellipsis conversion sequence, and |
| 3001 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 3002 | // conversion sequence than an ellipsis conversion sequence |
| 3003 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3004 | // |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3005 | // C++0x [over.best.ics]p10: |
| 3006 | // For the purpose of ranking implicit conversion sequences as |
| 3007 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 3008 | // treated as a user-defined sequence that is indistinguishable |
| 3009 | // from any other user-defined conversion sequence. |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3010 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 3011 | return ImplicitConversionSequence::Better; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3012 | if (ICS2.getKindRank() < ICS1.getKindRank()) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3013 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3014 | |
Benjamin Kramer | b6eee07 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 3015 | // The following checks require both conversion sequences to be of |
| 3016 | // the same kind. |
| 3017 | if (ICS1.getKind() != ICS2.getKind()) |
| 3018 | return ImplicitConversionSequence::Indistinguishable; |
| 3019 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3020 | ImplicitConversionSequence::CompareKind Result = |
| 3021 | ImplicitConversionSequence::Indistinguishable; |
| 3022 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3023 | // Two implicit conversion sequences of the same form are |
| 3024 | // indistinguishable conversion sequences unless one of the |
| 3025 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3026 | if (ICS1.isStandard()) |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3027 | Result = CompareStandardConversionSequences(S, |
| 3028 | ICS1.Standard, ICS2.Standard); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3029 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3030 | // User-defined conversion sequence U1 is a better conversion |
| 3031 | // sequence than another user-defined conversion sequence U2 if |
| 3032 | // they contain the same user-defined conversion function or |
| 3033 | // constructor and if the second standard conversion sequence of |
| 3034 | // U1 is better than the second standard conversion sequence of |
| 3035 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3036 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3037 | ICS2.UserDefined.ConversionFunction) |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3038 | Result = CompareStandardConversionSequences(S, |
| 3039 | ICS1.UserDefined.After, |
| 3040 | ICS2.UserDefined.After); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3041 | } |
| 3042 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3043 | // List-initialization sequence L1 is a better conversion sequence than |
| 3044 | // list-initialization sequence L2 if L1 converts to std::initializer_list<X> |
| 3045 | // for some X and L2 does not. |
| 3046 | if (Result == ImplicitConversionSequence::Indistinguishable && |
| 3047 | ICS1.isListInitializationSequence() && |
| 3048 | ICS2.isListInitializationSequence()) { |
| 3049 | // FIXME: Find out if ICS1 converts to initializer_list and ICS2 doesn't. |
| 3050 | } |
| 3051 | |
| 3052 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3053 | } |
| 3054 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3055 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 3056 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 3057 | Qualifiers Quals; |
| 3058 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3059 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3060 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3061 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3062 | return Context.hasSameUnqualifiedType(T1, T2); |
| 3063 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3064 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3065 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 3066 | // determine if one is a proper subset of the other. |
| 3067 | static ImplicitConversionSequence::CompareKind |
| 3068 | compareStandardConversionSubsets(ASTContext &Context, |
| 3069 | const StandardConversionSequence& SCS1, |
| 3070 | const StandardConversionSequence& SCS2) { |
| 3071 | ImplicitConversionSequence::CompareKind Result |
| 3072 | = ImplicitConversionSequence::Indistinguishable; |
| 3073 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3074 | // the identity conversion sequence is considered to be a subsequence of |
Douglas Gregor | ae65f4b | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 3075 | // any non-identity conversion sequence |
Douglas Gregor | 4ae5b72 | 2011-06-05 06:15:20 +0000 | [diff] [blame] | 3076 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 3077 | return ImplicitConversionSequence::Better; |
| 3078 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 3079 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3080 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3081 | if (SCS1.Second != SCS2.Second) { |
| 3082 | if (SCS1.Second == ICK_Identity) |
| 3083 | Result = ImplicitConversionSequence::Better; |
| 3084 | else if (SCS2.Second == ICK_Identity) |
| 3085 | Result = ImplicitConversionSequence::Worse; |
| 3086 | else |
| 3087 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3088 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3089 | return ImplicitConversionSequence::Indistinguishable; |
| 3090 | |
| 3091 | if (SCS1.Third == SCS2.Third) { |
| 3092 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 3093 | : ImplicitConversionSequence::Indistinguishable; |
| 3094 | } |
| 3095 | |
| 3096 | if (SCS1.Third == ICK_Identity) |
| 3097 | return Result == ImplicitConversionSequence::Worse |
| 3098 | ? ImplicitConversionSequence::Indistinguishable |
| 3099 | : ImplicitConversionSequence::Better; |
| 3100 | |
| 3101 | if (SCS2.Third == ICK_Identity) |
| 3102 | return Result == ImplicitConversionSequence::Better |
| 3103 | ? ImplicitConversionSequence::Indistinguishable |
| 3104 | : ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3105 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3106 | return ImplicitConversionSequence::Indistinguishable; |
| 3107 | } |
| 3108 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3109 | /// \brief Determine whether one of the given reference bindings is better |
| 3110 | /// than the other based on what kind of bindings they are. |
| 3111 | static bool isBetterReferenceBindingKind(const StandardConversionSequence &SCS1, |
| 3112 | const StandardConversionSequence &SCS2) { |
| 3113 | // C++0x [over.ics.rank]p3b4: |
| 3114 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 3115 | // implicit object parameter of a non-static member function declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3116 | // without a ref-qualifier, and *either* S1 binds an rvalue reference |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3117 | // 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] | 3118 | // lvalue reference to a function lvalue and S2 binds an rvalue |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3119 | // reference*. |
| 3120 | // |
| 3121 | // FIXME: Rvalue references. We're going rogue with the above edits, |
| 3122 | // because the semantics in the current C++0x working paper (N3225 at the |
| 3123 | // time of this writing) break the standard definition of std::forward |
| 3124 | // and std::reference_wrapper when dealing with references to functions. |
| 3125 | // Proposed wording changes submitted to CWG for consideration. |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3126 | if (SCS1.BindsImplicitObjectArgumentWithoutRefQualifier || |
| 3127 | SCS2.BindsImplicitObjectArgumentWithoutRefQualifier) |
| 3128 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3129 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3130 | return (!SCS1.IsLvalueReference && SCS1.BindsToRvalue && |
| 3131 | SCS2.IsLvalueReference) || |
| 3132 | (SCS1.IsLvalueReference && SCS1.BindsToFunctionLvalue && |
| 3133 | !SCS2.IsLvalueReference); |
| 3134 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3135 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3136 | /// CompareStandardConversionSequences - Compare two standard |
| 3137 | /// conversion sequences to determine whether one is better than the |
| 3138 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3139 | static ImplicitConversionSequence::CompareKind |
| 3140 | CompareStandardConversionSequences(Sema &S, |
| 3141 | const StandardConversionSequence& SCS1, |
| 3142 | const StandardConversionSequence& SCS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3143 | { |
| 3144 | // Standard conversion sequence S1 is a better conversion sequence |
| 3145 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 3146 | |
| 3147 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 3148 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 3149 | // excluding any Lvalue Transformation; the identity conversion |
| 3150 | // sequence is considered to be a subsequence of any |
| 3151 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3152 | if (ImplicitConversionSequence::CompareKind CK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3153 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3154 | return CK; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3155 | |
| 3156 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 3157 | // defined below), or, if not that, |
| 3158 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 3159 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 3160 | if (Rank1 < Rank2) |
| 3161 | return ImplicitConversionSequence::Better; |
| 3162 | else if (Rank2 < Rank1) |
| 3163 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3164 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3165 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 3166 | // are indistinguishable unless one of the following rules |
| 3167 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3168 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3169 | // A conversion that is not a conversion of a pointer, or |
| 3170 | // pointer to member, to bool is better than another conversion |
| 3171 | // that is such a conversion. |
| 3172 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 3173 | return SCS2.isPointerConversionToBool() |
| 3174 | ? ImplicitConversionSequence::Better |
| 3175 | : ImplicitConversionSequence::Worse; |
| 3176 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3177 | // C++ [over.ics.rank]p4b2: |
| 3178 | // |
| 3179 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3180 | // conversion of B* to A* is better than conversion of B* to |
| 3181 | // void*, and conversion of A* to void* is better than conversion |
| 3182 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3183 | bool SCS1ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3184 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3185 | bool SCS2ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3186 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3187 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 3188 | // Exactly one of the conversion sequences is a conversion to |
| 3189 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3190 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 3191 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3192 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 3193 | // Neither conversion sequence converts to a void pointer; compare |
| 3194 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3195 | if (ImplicitConversionSequence::CompareKind DerivedCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3196 | = CompareDerivedToBaseConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3197 | return DerivedCK; |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3198 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid && |
| 3199 | !S.Context.hasSameType(SCS1.getFromType(), SCS2.getFromType())) { |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3200 | // Both conversion sequences are conversions to void |
| 3201 | // pointers. Compare the source types to determine if there's an |
| 3202 | // inheritance relationship in their sources. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3203 | QualType FromType1 = SCS1.getFromType(); |
| 3204 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3205 | |
| 3206 | // Adjust the types we're converting from via the array-to-pointer |
| 3207 | // conversion, if we need to. |
| 3208 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3209 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3210 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3211 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3212 | |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3213 | QualType FromPointee1 = FromType1->getPointeeType().getUnqualifiedType(); |
| 3214 | QualType FromPointee2 = FromType2->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3215 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3216 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3217 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3218 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3219 | return ImplicitConversionSequence::Worse; |
| 3220 | |
| 3221 | // Objective-C++: If one interface is more specific than the |
| 3222 | // other, it is the better one. |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3223 | const ObjCObjectPointerType* FromObjCPtr1 |
| 3224 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3225 | const ObjCObjectPointerType* FromObjCPtr2 |
| 3226 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3227 | if (FromObjCPtr1 && FromObjCPtr2) { |
| 3228 | bool AssignLeft = S.Context.canAssignObjCInterfaces(FromObjCPtr1, |
| 3229 | FromObjCPtr2); |
| 3230 | bool AssignRight = S.Context.canAssignObjCInterfaces(FromObjCPtr2, |
| 3231 | FromObjCPtr1); |
| 3232 | if (AssignLeft != AssignRight) { |
| 3233 | return AssignLeft? ImplicitConversionSequence::Better |
| 3234 | : ImplicitConversionSequence::Worse; |
| 3235 | } |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3236 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3237 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3238 | |
| 3239 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 3240 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3241 | if (ImplicitConversionSequence::CompareKind QualCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3242 | = CompareQualificationConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3243 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3244 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3245 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3246 | // Check for a better reference binding based on the kind of bindings. |
| 3247 | if (isBetterReferenceBindingKind(SCS1, SCS2)) |
| 3248 | return ImplicitConversionSequence::Better; |
| 3249 | else if (isBetterReferenceBindingKind(SCS2, SCS1)) |
| 3250 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3251 | |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 3252 | // C++ [over.ics.rank]p3b4: |
| 3253 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 3254 | // which the references refer are the same type except for |
| 3255 | // top-level cv-qualifiers, and the type to which the reference |
| 3256 | // initialized by S2 refers is more cv-qualified than the type |
| 3257 | // to which the reference initialized by S1 refers. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3258 | QualType T1 = SCS1.getToType(2); |
| 3259 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3260 | T1 = S.Context.getCanonicalType(T1); |
| 3261 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3262 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3263 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3264 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3265 | if (UnqualT1 == UnqualT2) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3266 | // Objective-C++ ARC: If the references refer to objects with different |
| 3267 | // lifetimes, prefer bindings that don't change lifetime. |
| 3268 | if (SCS1.ObjCLifetimeConversionBinding != |
| 3269 | SCS2.ObjCLifetimeConversionBinding) { |
| 3270 | return SCS1.ObjCLifetimeConversionBinding |
| 3271 | ? ImplicitConversionSequence::Worse |
| 3272 | : ImplicitConversionSequence::Better; |
| 3273 | } |
| 3274 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3275 | // If the type is an array type, promote the element qualifiers to the |
| 3276 | // type for comparison. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3277 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3278 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3279 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3280 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3281 | if (T2.isMoreQualifiedThan(T1)) |
| 3282 | return ImplicitConversionSequence::Better; |
| 3283 | else if (T1.isMoreQualifiedThan(T2)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3284 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3285 | } |
| 3286 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3287 | |
Francois Pichet | 1c98d62 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3288 | // In Microsoft mode, prefer an integral conversion to a |
| 3289 | // floating-to-integral conversion if the integral conversion |
| 3290 | // is between types of the same size. |
| 3291 | // For example: |
| 3292 | // void f(float); |
| 3293 | // void f(int); |
| 3294 | // int main { |
| 3295 | // long a; |
| 3296 | // f(a); |
| 3297 | // } |
| 3298 | // Here, MSVC will call f(int) instead of generating a compile error |
| 3299 | // as clang will do in standard mode. |
| 3300 | if (S.getLangOptions().MicrosoftMode && |
| 3301 | SCS1.Second == ICK_Integral_Conversion && |
| 3302 | SCS2.Second == ICK_Floating_Integral && |
| 3303 | S.Context.getTypeSize(SCS1.getFromType()) == |
| 3304 | S.Context.getTypeSize(SCS1.getToType(2))) |
| 3305 | return ImplicitConversionSequence::Better; |
| 3306 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3307 | return ImplicitConversionSequence::Indistinguishable; |
| 3308 | } |
| 3309 | |
| 3310 | /// CompareQualificationConversions - Compares two standard conversion |
| 3311 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3312 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 3313 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3314 | CompareQualificationConversions(Sema &S, |
| 3315 | const StandardConversionSequence& SCS1, |
| 3316 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 3317 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3318 | // -- S1 and S2 differ only in their qualification conversion and |
| 3319 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 3320 | // cv-qualification signature of type T1 is a proper subset of |
| 3321 | // the cv-qualification signature of type T2, and S1 is not the |
| 3322 | // deprecated string literal array-to-pointer conversion (4.2). |
| 3323 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 3324 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 3325 | return ImplicitConversionSequence::Indistinguishable; |
| 3326 | |
| 3327 | // FIXME: the example in the standard doesn't use a qualification |
| 3328 | // conversion (!) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3329 | QualType T1 = SCS1.getToType(2); |
| 3330 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3331 | T1 = S.Context.getCanonicalType(T1); |
| 3332 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3333 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3334 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3335 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3336 | |
| 3337 | // If the types are the same, we won't learn anything by unwrapped |
| 3338 | // them. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3339 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3340 | return ImplicitConversionSequence::Indistinguishable; |
| 3341 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3342 | // If the type is an array type, promote the element qualifiers to the type |
| 3343 | // for comparison. |
| 3344 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3345 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3346 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3347 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3348 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3349 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3350 | = ImplicitConversionSequence::Indistinguishable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3351 | |
| 3352 | // Objective-C++ ARC: |
| 3353 | // Prefer qualification conversions not involving a change in lifetime |
| 3354 | // to qualification conversions that do not change lifetime. |
| 3355 | if (SCS1.QualificationIncludesObjCLifetime != |
| 3356 | SCS2.QualificationIncludesObjCLifetime) { |
| 3357 | Result = SCS1.QualificationIncludesObjCLifetime |
| 3358 | ? ImplicitConversionSequence::Worse |
| 3359 | : ImplicitConversionSequence::Better; |
| 3360 | } |
| 3361 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3362 | while (S.Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3363 | // Within each iteration of the loop, we check the qualifiers to |
| 3364 | // determine if this still looks like a qualification |
| 3365 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 3366 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3367 | // until there are no more pointers or pointers-to-members left |
| 3368 | // to unwrap. This essentially mimics what |
| 3369 | // IsQualificationConversion does, but here we're checking for a |
| 3370 | // strict subset of qualifiers. |
| 3371 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 3372 | // The qualifiers are the same, so this doesn't tell us anything |
| 3373 | // about how the sequences rank. |
| 3374 | ; |
| 3375 | else if (T2.isMoreQualifiedThan(T1)) { |
| 3376 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 3377 | if (Result == ImplicitConversionSequence::Worse) |
| 3378 | // Neither has qualifiers that are a subset of the other's |
| 3379 | // qualifiers. |
| 3380 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3381 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3382 | Result = ImplicitConversionSequence::Better; |
| 3383 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 3384 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 3385 | if (Result == ImplicitConversionSequence::Better) |
| 3386 | // Neither has qualifiers that are a subset of the other's |
| 3387 | // qualifiers. |
| 3388 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3389 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3390 | Result = ImplicitConversionSequence::Worse; |
| 3391 | } else { |
| 3392 | // Qualifiers are disjoint. |
| 3393 | return ImplicitConversionSequence::Indistinguishable; |
| 3394 | } |
| 3395 | |
| 3396 | // If the types after this point are equivalent, we're done. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3397 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3398 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3401 | // Check that the winning standard conversion sequence isn't using |
| 3402 | // the deprecated string literal array to pointer conversion. |
| 3403 | switch (Result) { |
| 3404 | case ImplicitConversionSequence::Better: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3405 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3406 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3407 | break; |
| 3408 | |
| 3409 | case ImplicitConversionSequence::Indistinguishable: |
| 3410 | break; |
| 3411 | |
| 3412 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3413 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3414 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3415 | break; |
| 3416 | } |
| 3417 | |
| 3418 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3419 | } |
| 3420 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3421 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 3422 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3423 | /// various kinds of derived-to-base conversions (C++ |
| 3424 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 3425 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3426 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3427 | CompareDerivedToBaseConversions(Sema &S, |
| 3428 | const StandardConversionSequence& SCS1, |
| 3429 | const StandardConversionSequence& SCS2) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3430 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3431 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3432 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3433 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3434 | |
| 3435 | // Adjust the types we're converting from via the array-to-pointer |
| 3436 | // conversion, if we need to. |
| 3437 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3438 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3439 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3440 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3441 | |
| 3442 | // Canonicalize all of the types. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3443 | FromType1 = S.Context.getCanonicalType(FromType1); |
| 3444 | ToType1 = S.Context.getCanonicalType(ToType1); |
| 3445 | FromType2 = S.Context.getCanonicalType(FromType2); |
| 3446 | ToType2 = S.Context.getCanonicalType(ToType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3447 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3448 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3449 | // |
| 3450 | // If class B is derived directly or indirectly from class A and |
| 3451 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3452 | // |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3453 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3454 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 3455 | SCS2.Second == ICK_Pointer_Conversion && |
| 3456 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 3457 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 3458 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3459 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3460 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3461 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3462 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3463 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3464 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3465 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3466 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3467 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3468 | // -- 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] | 3469 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3470 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3471 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3472 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3473 | return ImplicitConversionSequence::Worse; |
| 3474 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3475 | |
| 3476 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 3477 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3478 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3479 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3480 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3481 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3482 | } |
| 3483 | } else if (SCS1.Second == ICK_Pointer_Conversion && |
| 3484 | SCS2.Second == ICK_Pointer_Conversion) { |
| 3485 | const ObjCObjectPointerType *FromPtr1 |
| 3486 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3487 | const ObjCObjectPointerType *FromPtr2 |
| 3488 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3489 | const ObjCObjectPointerType *ToPtr1 |
| 3490 | = ToType1->getAs<ObjCObjectPointerType>(); |
| 3491 | const ObjCObjectPointerType *ToPtr2 |
| 3492 | = ToType2->getAs<ObjCObjectPointerType>(); |
| 3493 | |
| 3494 | if (FromPtr1 && FromPtr2 && ToPtr1 && ToPtr2) { |
| 3495 | // Apply the same conversion ranking rules for Objective-C pointer types |
| 3496 | // that we do for C++ pointers to class types. However, we employ the |
| 3497 | // Objective-C pseudo-subtyping relationship used for assignment of |
| 3498 | // Objective-C pointer types. |
| 3499 | bool FromAssignLeft |
| 3500 | = S.Context.canAssignObjCInterfaces(FromPtr1, FromPtr2); |
| 3501 | bool FromAssignRight |
| 3502 | = S.Context.canAssignObjCInterfaces(FromPtr2, FromPtr1); |
| 3503 | bool ToAssignLeft |
| 3504 | = S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2); |
| 3505 | bool ToAssignRight |
| 3506 | = S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1); |
| 3507 | |
| 3508 | // A conversion to an a non-id object pointer type or qualified 'id' |
| 3509 | // type is better than a conversion to 'id'. |
| 3510 | if (ToPtr1->isObjCIdType() && |
| 3511 | (ToPtr2->isObjCQualifiedIdType() || ToPtr2->getInterfaceDecl())) |
| 3512 | return ImplicitConversionSequence::Worse; |
| 3513 | if (ToPtr2->isObjCIdType() && |
| 3514 | (ToPtr1->isObjCQualifiedIdType() || ToPtr1->getInterfaceDecl())) |
| 3515 | return ImplicitConversionSequence::Better; |
| 3516 | |
| 3517 | // A conversion to a non-id object pointer type is better than a |
| 3518 | // conversion to a qualified 'id' type |
| 3519 | if (ToPtr1->isObjCQualifiedIdType() && ToPtr2->getInterfaceDecl()) |
| 3520 | return ImplicitConversionSequence::Worse; |
| 3521 | if (ToPtr2->isObjCQualifiedIdType() && ToPtr1->getInterfaceDecl()) |
| 3522 | return ImplicitConversionSequence::Better; |
| 3523 | |
| 3524 | // A conversion to an a non-Class object pointer type or qualified 'Class' |
| 3525 | // type is better than a conversion to 'Class'. |
| 3526 | if (ToPtr1->isObjCClassType() && |
| 3527 | (ToPtr2->isObjCQualifiedClassType() || ToPtr2->getInterfaceDecl())) |
| 3528 | return ImplicitConversionSequence::Worse; |
| 3529 | if (ToPtr2->isObjCClassType() && |
| 3530 | (ToPtr1->isObjCQualifiedClassType() || ToPtr1->getInterfaceDecl())) |
| 3531 | return ImplicitConversionSequence::Better; |
| 3532 | |
| 3533 | // A conversion to a non-Class object pointer type is better than a |
| 3534 | // conversion to a qualified 'Class' type. |
| 3535 | if (ToPtr1->isObjCQualifiedClassType() && ToPtr2->getInterfaceDecl()) |
| 3536 | return ImplicitConversionSequence::Worse; |
| 3537 | if (ToPtr2->isObjCQualifiedClassType() && ToPtr1->getInterfaceDecl()) |
| 3538 | return ImplicitConversionSequence::Better; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3539 | |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3540 | // -- "conversion of C* to B* is better than conversion of C* to A*," |
| 3541 | if (S.Context.hasSameType(FromType1, FromType2) && |
| 3542 | !FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType() && |
| 3543 | (ToAssignLeft != ToAssignRight)) |
| 3544 | return ToAssignLeft? ImplicitConversionSequence::Worse |
| 3545 | : ImplicitConversionSequence::Better; |
| 3546 | |
| 3547 | // -- "conversion of B* to A* is better than conversion of C* to A*," |
| 3548 | if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) && |
| 3549 | (FromAssignLeft != FromAssignRight)) |
| 3550 | return FromAssignLeft? ImplicitConversionSequence::Better |
| 3551 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3552 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3553 | } |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3554 | |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3555 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3556 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 3557 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 3558 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3559 | const MemberPointerType * FromMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3560 | FromType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3561 | const MemberPointerType * ToMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3562 | ToType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3563 | const MemberPointerType * FromMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3564 | FromType2->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3565 | const MemberPointerType * ToMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3566 | ToType2->getAs<MemberPointerType>(); |
| 3567 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 3568 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 3569 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 3570 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 3571 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 3572 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 3573 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 3574 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3575 | // 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] | 3576 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3577 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3578 | return ImplicitConversionSequence::Worse; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3579 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3580 | return ImplicitConversionSequence::Better; |
| 3581 | } |
| 3582 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 3583 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3584 | if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3585 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3586 | else if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3587 | return ImplicitConversionSequence::Worse; |
| 3588 | } |
| 3589 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3590 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3591 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3592 | // -- 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] | 3593 | // -- binding of an expression of type C to a reference of type |
| 3594 | // B& is better than binding an expression of type C to a |
| 3595 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3596 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3597 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3598 | if (S.IsDerivedFrom(ToType1, ToType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3599 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3600 | else if (S.IsDerivedFrom(ToType2, ToType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3601 | return ImplicitConversionSequence::Worse; |
| 3602 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3603 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3604 | // -- 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] | 3605 | // -- binding of an expression of type B to a reference of type |
| 3606 | // A& is better than binding an expression of type C to a |
| 3607 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3608 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3609 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3610 | if (S.IsDerivedFrom(FromType2, FromType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3611 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3612 | else if (S.IsDerivedFrom(FromType1, FromType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3613 | return ImplicitConversionSequence::Worse; |
| 3614 | } |
| 3615 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3616 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3617 | return ImplicitConversionSequence::Indistinguishable; |
| 3618 | } |
| 3619 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3620 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 3621 | /// determine whether they are reference-related, |
| 3622 | /// reference-compatible, reference-compatible with added |
| 3623 | /// qualification, or incompatible, for use in C++ initialization by |
| 3624 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 3625 | /// type, and the first type (T1) is the pointee type of the reference |
| 3626 | /// type being initialized. |
| 3627 | Sema::ReferenceCompareResult |
| 3628 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 3629 | QualType OrigT1, QualType OrigT2, |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3630 | bool &DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3631 | bool &ObjCConversion, |
| 3632 | bool &ObjCLifetimeConversion) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3633 | assert(!OrigT1->isReferenceType() && |
| 3634 | "T1 must be the pointee type of the reference type"); |
| 3635 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 3636 | |
| 3637 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 3638 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 3639 | Qualifiers T1Quals, T2Quals; |
| 3640 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3641 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 3642 | |
| 3643 | // C++ [dcl.init.ref]p4: |
| 3644 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 3645 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 3646 | // T1 is a base class of T2. |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3647 | DerivedToBase = false; |
| 3648 | ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3649 | ObjCLifetimeConversion = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3650 | if (UnqualT1 == UnqualT2) { |
| 3651 | // Nothing to do. |
| 3652 | } else if (!RequireCompleteType(Loc, OrigT2, PDiag()) && |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3653 | IsDerivedFrom(UnqualT2, UnqualT1)) |
| 3654 | DerivedToBase = true; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3655 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
| 3656 | UnqualT2->isObjCObjectOrInterfaceType() && |
| 3657 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
| 3658 | ObjCConversion = true; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3659 | else |
| 3660 | return Ref_Incompatible; |
| 3661 | |
| 3662 | // At this point, we know that T1 and T2 are reference-related (at |
| 3663 | // least). |
| 3664 | |
| 3665 | // If the type is an array type, promote the element qualifiers to the type |
| 3666 | // for comparison. |
| 3667 | if (isa<ArrayType>(T1) && T1Quals) |
| 3668 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 3669 | if (isa<ArrayType>(T2) && T2Quals) |
| 3670 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 3671 | |
| 3672 | // C++ [dcl.init.ref]p4: |
| 3673 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 3674 | // reference-related to T2 and cv1 is the same cv-qualification |
| 3675 | // as, or greater cv-qualification than, cv2. For purposes of |
| 3676 | // overload resolution, cases for which cv1 is greater |
| 3677 | // cv-qualification than cv2 are identified as |
| 3678 | // reference-compatible with added qualification (see 13.3.3.2). |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 3679 | // |
| 3680 | // Note that we also require equivalence of Objective-C GC and address-space |
| 3681 | // qualifiers when performing these computations, so that e.g., an int in |
| 3682 | // address space 1 is not reference-compatible with an int in address |
| 3683 | // space 2. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3684 | if (T1Quals.getObjCLifetime() != T2Quals.getObjCLifetime() && |
| 3685 | T1Quals.compatiblyIncludesObjCLifetime(T2Quals)) { |
| 3686 | T1Quals.removeObjCLifetime(); |
| 3687 | T2Quals.removeObjCLifetime(); |
| 3688 | ObjCLifetimeConversion = true; |
| 3689 | } |
| 3690 | |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 3691 | if (T1Quals == T2Quals) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3692 | return Ref_Compatible; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3693 | else if (T1Quals.compatiblyIncludes(T2Quals)) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3694 | return Ref_Compatible_With_Added_Qualification; |
| 3695 | else |
| 3696 | return Ref_Related; |
| 3697 | } |
| 3698 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3699 | /// \brief Look for a user-defined conversion to an value reference-compatible |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3700 | /// with DeclType. Return true if something definite is found. |
| 3701 | static bool |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3702 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
| 3703 | QualType DeclType, SourceLocation DeclLoc, |
| 3704 | Expr *Init, QualType T2, bool AllowRvalues, |
| 3705 | bool AllowExplicit) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3706 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 3707 | CXXRecordDecl *T2RecordDecl |
| 3708 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 3709 | |
| 3710 | OverloadCandidateSet CandidateSet(DeclLoc); |
| 3711 | const UnresolvedSetImpl *Conversions |
| 3712 | = T2RecordDecl->getVisibleConversionFunctions(); |
| 3713 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 3714 | E = Conversions->end(); I != E; ++I) { |
| 3715 | NamedDecl *D = *I; |
| 3716 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3717 | if (isa<UsingShadowDecl>(D)) |
| 3718 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3719 | |
| 3720 | FunctionTemplateDecl *ConvTemplate |
| 3721 | = dyn_cast<FunctionTemplateDecl>(D); |
| 3722 | CXXConversionDecl *Conv; |
| 3723 | if (ConvTemplate) |
| 3724 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3725 | else |
| 3726 | Conv = cast<CXXConversionDecl>(D); |
| 3727 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3728 | // 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] | 3729 | // explicit conversions, skip it. |
| 3730 | if (!AllowExplicit && Conv->isExplicit()) |
| 3731 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3732 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3733 | if (AllowRvalues) { |
| 3734 | bool DerivedToBase = false; |
| 3735 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3736 | bool ObjCLifetimeConversion = false; |
Douglas Gregor | 203050c | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 3737 | |
| 3738 | // If we are initializing an rvalue reference, don't permit conversion |
| 3739 | // functions that return lvalues. |
| 3740 | if (!ConvTemplate && DeclType->isRValueReferenceType()) { |
| 3741 | const ReferenceType *RefType |
| 3742 | = Conv->getConversionType()->getAs<LValueReferenceType>(); |
| 3743 | if (RefType && !RefType->getPointeeType()->isFunctionType()) |
| 3744 | continue; |
| 3745 | } |
| 3746 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3747 | if (!ConvTemplate && |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3748 | S.CompareReferenceRelationship( |
| 3749 | DeclLoc, |
| 3750 | Conv->getConversionType().getNonReferenceType() |
| 3751 | .getUnqualifiedType(), |
| 3752 | DeclType.getNonReferenceType().getUnqualifiedType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3753 | DerivedToBase, ObjCConversion, ObjCLifetimeConversion) == |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3754 | Sema::Ref_Incompatible) |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3755 | continue; |
| 3756 | } else { |
| 3757 | // If the conversion function doesn't return a reference type, |
| 3758 | // it can't be considered for this conversion. An rvalue reference |
| 3759 | // is only acceptable if its referencee is a function type. |
| 3760 | |
| 3761 | const ReferenceType *RefType = |
| 3762 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 3763 | if (!RefType || |
| 3764 | (!RefType->isLValueReferenceType() && |
| 3765 | !RefType->getPointeeType()->isFunctionType())) |
| 3766 | continue; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3767 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3768 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3769 | if (ConvTemplate) |
| 3770 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3771 | Init, DeclType, CandidateSet); |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3772 | else |
| 3773 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3774 | DeclType, CandidateSet); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3777 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3778 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3779 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3780 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3781 | case OR_Success: |
| 3782 | // C++ [over.ics.ref]p1: |
| 3783 | // |
| 3784 | // [...] If the parameter binds directly to the result of |
| 3785 | // applying a conversion function to the argument |
| 3786 | // expression, the implicit conversion sequence is a |
| 3787 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 3788 | // second standard conversion sequence either an identity |
| 3789 | // conversion or, if the conversion function returns an |
| 3790 | // entity of a type that is a derived class of the parameter |
| 3791 | // type, a derived-to-base Conversion. |
| 3792 | if (!Best->FinalConversion.DirectBinding) |
| 3793 | return false; |
| 3794 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3795 | if (Best->Function) |
| 3796 | S.MarkDeclarationReferenced(DeclLoc, Best->Function); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3797 | ICS.setUserDefined(); |
| 3798 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 3799 | ICS.UserDefined.After = Best->FinalConversion; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3800 | ICS.UserDefined.HadMultipleCandidates = HadMultipleCandidates; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3801 | ICS.UserDefined.ConversionFunction = Best->Function; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3802 | ICS.UserDefined.FoundConversionFunction = Best->FoundDecl; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3803 | ICS.UserDefined.EllipsisConversion = false; |
| 3804 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 3805 | ICS.UserDefined.After.DirectBinding && |
| 3806 | "Expected a direct reference binding!"); |
| 3807 | return true; |
| 3808 | |
| 3809 | case OR_Ambiguous: |
| 3810 | ICS.setAmbiguous(); |
| 3811 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3812 | Cand != CandidateSet.end(); ++Cand) |
| 3813 | if (Cand->Viable) |
| 3814 | ICS.Ambiguous.addConversion(Cand->Function); |
| 3815 | return true; |
| 3816 | |
| 3817 | case OR_No_Viable_Function: |
| 3818 | case OR_Deleted: |
| 3819 | // There was no suitable conversion, or we found a deleted |
| 3820 | // conversion; continue with other checks. |
| 3821 | return false; |
| 3822 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3823 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3824 | llvm_unreachable("Invalid OverloadResult!"); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3825 | } |
| 3826 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3827 | /// \brief Compute an implicit conversion sequence for reference |
| 3828 | /// initialization. |
| 3829 | static ImplicitConversionSequence |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 3830 | TryReferenceInit(Sema &S, Expr *Init, QualType DeclType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3831 | SourceLocation DeclLoc, |
| 3832 | bool SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 3833 | bool AllowExplicit) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3834 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 3835 | |
| 3836 | // Most paths end in a failed conversion. |
| 3837 | ImplicitConversionSequence ICS; |
| 3838 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 3839 | |
| 3840 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 3841 | QualType T2 = Init->getType(); |
| 3842 | |
| 3843 | // If the initializer is the address of an overloaded function, try |
| 3844 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3845 | // type of the resulting function. |
| 3846 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 3847 | DeclAccessPair Found; |
| 3848 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 3849 | false, Found)) |
| 3850 | T2 = Fn->getType(); |
| 3851 | } |
| 3852 | |
| 3853 | // Compute some basic properties of the types and the initializer. |
| 3854 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 3855 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3856 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3857 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3858 | Expr::Classification InitCategory = Init->Classify(S.Context); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3859 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3860 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3861 | ObjCConversion, ObjCLifetimeConversion); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3862 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3863 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3864 | // C++0x [dcl.init.ref]p5: |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 3865 | // A reference to type "cv1 T1" is initialized by an expression |
| 3866 | // of type "cv2 T2" as follows: |
| 3867 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3868 | // -- If reference is an lvalue reference and the initializer expression |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3869 | if (!isRValRef) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3870 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 3871 | // reference-compatible with "cv2 T2," or |
| 3872 | // |
| 3873 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 3874 | if (InitCategory.isLValue() && |
| 3875 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3876 | // C++ [over.ics.ref]p1: |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3877 | // When a parameter of reference type binds directly (8.5.3) |
| 3878 | // to an argument expression, the implicit conversion sequence |
| 3879 | // is the identity conversion, unless the argument expression |
| 3880 | // has a type that is a derived class of the parameter type, |
| 3881 | // in which case the implicit conversion sequence is a |
| 3882 | // derived-to-base Conversion (13.3.3.1). |
| 3883 | ICS.setStandard(); |
| 3884 | ICS.Standard.First = ICK_Identity; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3885 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 3886 | : ObjCConversion? ICK_Compatible_Conversion |
| 3887 | : ICK_Identity; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3888 | ICS.Standard.Third = ICK_Identity; |
| 3889 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 3890 | ICS.Standard.setToType(0, T2); |
| 3891 | ICS.Standard.setToType(1, T1); |
| 3892 | ICS.Standard.setToType(2, T1); |
| 3893 | ICS.Standard.ReferenceBinding = true; |
| 3894 | ICS.Standard.DirectBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3895 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3896 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 3897 | ICS.Standard.BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3898 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3899 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3900 | ICS.Standard.CopyConstructor = 0; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3901 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3902 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 3903 | // derived-to-base conversions is suppressed when we're |
| 3904 | // computing the implicit conversion sequence (C++ |
| 3905 | // [over.best.ics]p2). |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3906 | return ICS; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3907 | } |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3908 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3909 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 3910 | // not reference-related to T2, and can be implicitly |
| 3911 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 3912 | // is reference-compatible with "cv3 T3" 92) (this |
| 3913 | // conversion is selected by enumerating the applicable |
| 3914 | // conversion functions (13.3.1.6) and choosing the best |
| 3915 | // one through overload resolution (13.3)), |
| 3916 | if (!SuppressUserConversions && T2->isRecordType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3917 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3918 | RefRelationship == Sema::Ref_Incompatible) { |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3919 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 3920 | Init, T2, /*AllowRvalues=*/false, |
| 3921 | AllowExplicit)) |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3922 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3923 | } |
| 3924 | } |
| 3925 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3926 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 3927 | // 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] | 3928 | // shall be an rvalue reference. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3929 | // |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 3930 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 3931 | // point, which is that, due to p2 (which short-circuits reference |
| 3932 | // binding by only attempting a simple conversion for non-direct |
| 3933 | // bindings) and p3's strange wording, we allow a const volatile |
| 3934 | // reference to bind to an rvalue. Hence the check for the presence |
| 3935 | // of "const" rather than checking for "const" being the only |
| 3936 | // qualifier. |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3937 | // This is also the point where rvalue references and lvalue inits no longer |
| 3938 | // go together. |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 3939 | if (!isRValRef && !T1.isConstQualified()) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3940 | return ICS; |
| 3941 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3942 | // -- If the initializer expression |
| 3943 | // |
| 3944 | // -- is an xvalue, class prvalue, array prvalue or function |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3945 | // lvalue and "cv1 T1" is reference-compatible with "cv2 T2", or |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3946 | if (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification && |
| 3947 | (InitCategory.isXValue() || |
| 3948 | (InitCategory.isPRValue() && (T2->isRecordType() || T2->isArrayType())) || |
| 3949 | (InitCategory.isLValue() && T2->isFunctionType()))) { |
| 3950 | ICS.setStandard(); |
| 3951 | ICS.Standard.First = ICK_Identity; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3952 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3953 | : ObjCConversion? ICK_Compatible_Conversion |
| 3954 | : ICK_Identity; |
| 3955 | ICS.Standard.Third = ICK_Identity; |
| 3956 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 3957 | ICS.Standard.setToType(0, T2); |
| 3958 | ICS.Standard.setToType(1, T1); |
| 3959 | ICS.Standard.setToType(2, T1); |
| 3960 | ICS.Standard.ReferenceBinding = true; |
| 3961 | // In C++0x, this is always a direct binding. In C++98/03, it's a direct |
| 3962 | // binding unless we're binding to a class prvalue. |
| 3963 | // Note: Although xvalues wouldn't normally show up in C++98/03 code, we |
| 3964 | // allow the use of rvalue references in C++98/03 for the benefit of |
| 3965 | // standard library implementors; therefore, we need the xvalue check here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3966 | ICS.Standard.DirectBinding = |
| 3967 | S.getLangOptions().CPlusPlus0x || |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3968 | (InitCategory.isPRValue() && !T2->isRecordType()); |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3969 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3970 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3971 | ICS.Standard.BindsToRvalue = InitCategory.isRValue(); |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3972 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3973 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3974 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3975 | return ICS; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3976 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3977 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3978 | // -- has a class type (i.e., T2 is a class type), where T1 is not |
| 3979 | // reference-related to T2, and can be implicitly converted to |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3980 | // an xvalue, class prvalue, or function lvalue of type |
| 3981 | // "cv3 T3", where "cv1 T1" is reference-compatible with |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3982 | // "cv3 T3", |
| 3983 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3984 | // then the reference is bound to the value of the initializer |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3985 | // expression in the first case and to the result of the conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3986 | // in the second case (or, in either case, to an appropriate base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3987 | // class subobject). |
| 3988 | if (!SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3989 | T2->isRecordType() && !S.RequireCompleteType(DeclLoc, T2, 0) && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3990 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 3991 | Init, T2, /*AllowRvalues=*/true, |
| 3992 | AllowExplicit)) { |
| 3993 | // In the second case, if the reference is an rvalue reference |
| 3994 | // and the second standard conversion sequence of the |
| 3995 | // user-defined conversion sequence includes an lvalue-to-rvalue |
| 3996 | // conversion, the program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3997 | if (ICS.isUserDefined() && isRValRef && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3998 | ICS.UserDefined.After.First == ICK_Lvalue_To_Rvalue) |
| 3999 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4000 | |
Douglas Gregor | 68ed68b | 2011-01-21 16:36:05 +0000 | [diff] [blame] | 4001 | return ICS; |
Rafael Espindola | aa5952c | 2011-01-22 15:32:35 +0000 | [diff] [blame] | 4002 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4003 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4004 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 4005 | // initialized from the initializer expression using the |
| 4006 | // rules for a non-reference copy initialization (8.5). The |
| 4007 | // reference is then bound to the temporary. If T1 is |
| 4008 | // reference-related to T2, cv1 must be the same |
| 4009 | // cv-qualification as, or greater cv-qualification than, |
| 4010 | // cv2; otherwise, the program is ill-formed. |
| 4011 | if (RefRelationship == Sema::Ref_Related) { |
| 4012 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 4013 | // we would be reference-compatible or reference-compatible with |
| 4014 | // added qualification. But that wasn't the case, so the reference |
| 4015 | // initialization fails. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4016 | // |
| 4017 | // Note that we only want to check address spaces and cvr-qualifiers here. |
| 4018 | // ObjC GC and lifetime qualifiers aren't important. |
| 4019 | Qualifiers T1Quals = T1.getQualifiers(); |
| 4020 | Qualifiers T2Quals = T2.getQualifiers(); |
| 4021 | T1Quals.removeObjCGCAttr(); |
| 4022 | T1Quals.removeObjCLifetime(); |
| 4023 | T2Quals.removeObjCGCAttr(); |
| 4024 | T2Quals.removeObjCLifetime(); |
| 4025 | if (!T1Quals.compatiblyIncludes(T2Quals)) |
| 4026 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4027 | } |
| 4028 | |
| 4029 | // If at least one of the types is a class type, the types are not |
| 4030 | // related, and we aren't allowed any user conversions, the |
| 4031 | // reference binding fails. This case is important for breaking |
| 4032 | // recursion, since TryImplicitConversion below will attempt to |
| 4033 | // create a temporary through the use of a copy constructor. |
| 4034 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 4035 | (T1->isRecordType() || T2->isRecordType())) |
| 4036 | return ICS; |
| 4037 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4038 | // If T1 is reference-related to T2 and the reference is an rvalue |
| 4039 | // reference, the initializer expression shall not be an lvalue. |
| 4040 | if (RefRelationship >= Sema::Ref_Related && |
| 4041 | isRValRef && Init->Classify(S.Context).isLValue()) |
| 4042 | return ICS; |
| 4043 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4044 | // C++ [over.ics.ref]p2: |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4045 | // When a parameter of reference type is not bound directly to |
| 4046 | // an argument expression, the conversion sequence is the one |
| 4047 | // required to convert the argument expression to the |
| 4048 | // underlying type of the reference according to |
| 4049 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 4050 | // to copy-initializing a temporary of the underlying type with |
| 4051 | // the argument expression. Any difference in top-level |
| 4052 | // cv-qualification is subsumed by the initialization itself |
| 4053 | // and does not constitute a conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4054 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
| 4055 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4056 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4057 | /*CStyle=*/false, |
| 4058 | /*AllowObjCWritebackConversion=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4059 | |
| 4060 | // Of course, that's still a reference binding. |
| 4061 | if (ICS.isStandard()) { |
| 4062 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4063 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4064 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4065 | ICS.Standard.BindsToRvalue = true; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4066 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4067 | ICS.Standard.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4068 | } else if (ICS.isUserDefined()) { |
Douglas Gregor | 203050c | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4069 | // Don't allow rvalue references to bind to lvalues. |
| 4070 | if (DeclType->isRValueReferenceType()) { |
| 4071 | if (const ReferenceType *RefType |
| 4072 | = ICS.UserDefined.ConversionFunction->getResultType() |
| 4073 | ->getAs<LValueReferenceType>()) { |
| 4074 | if (!RefType->getPointeeType()->isFunctionType()) { |
| 4075 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, Init, |
| 4076 | DeclType); |
| 4077 | return ICS; |
| 4078 | } |
| 4079 | } |
| 4080 | } |
| 4081 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4082 | ICS.UserDefined.After.ReferenceBinding = true; |
Douglas Gregor | f20d272 | 2011-08-15 13:59:46 +0000 | [diff] [blame] | 4083 | ICS.UserDefined.After.IsLvalueReference = !isRValRef; |
| 4084 | ICS.UserDefined.After.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4085 | ICS.UserDefined.After.BindsToRvalue = true; |
| 4086 | ICS.UserDefined.After.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4087 | ICS.UserDefined.After.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4088 | } |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4089 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4090 | return ICS; |
| 4091 | } |
| 4092 | |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4093 | static ImplicitConversionSequence |
| 4094 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
| 4095 | bool SuppressUserConversions, |
| 4096 | bool InOverloadResolution, |
| 4097 | bool AllowObjCWritebackConversion); |
| 4098 | |
| 4099 | /// TryListConversion - Try to copy-initialize a value of type ToType from the |
| 4100 | /// initializer list From. |
| 4101 | static ImplicitConversionSequence |
| 4102 | TryListConversion(Sema &S, InitListExpr *From, QualType ToType, |
| 4103 | bool SuppressUserConversions, |
| 4104 | bool InOverloadResolution, |
| 4105 | bool AllowObjCWritebackConversion) { |
| 4106 | // C++11 [over.ics.list]p1: |
| 4107 | // When an argument is an initializer list, it is not an expression and |
| 4108 | // special rules apply for converting it to a parameter type. |
| 4109 | |
| 4110 | ImplicitConversionSequence Result; |
| 4111 | Result.setBad(BadConversionSequence::no_conversion, From, ToType); |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4112 | Result.setListInitializationSequence(); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4113 | |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4114 | // 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] | 4115 | // initialized from init lists. |
| 4116 | if (S.RequireCompleteType(From->getLocStart(), ToType, S.PDiag())) |
| 4117 | return Result; |
| 4118 | |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4119 | // C++11 [over.ics.list]p2: |
| 4120 | // If the parameter type is std::initializer_list<X> or "array of X" and |
| 4121 | // all the elements can be implicitly converted to X, the implicit |
| 4122 | // conversion sequence is the worst conversion necessary to convert an |
| 4123 | // element of the list to X. |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4124 | QualType X; |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4125 | if (ToType->isArrayType()) |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4126 | X = S.Context.getBaseElementType(ToType); |
| 4127 | else |
| 4128 | (void)S.isStdInitializerList(ToType, &X); |
| 4129 | if (!X.isNull()) { |
| 4130 | for (unsigned i = 0, e = From->getNumInits(); i < e; ++i) { |
| 4131 | Expr *Init = From->getInit(i); |
| 4132 | ImplicitConversionSequence ICS = |
| 4133 | TryCopyInitialization(S, Init, X, SuppressUserConversions, |
| 4134 | InOverloadResolution, |
| 4135 | AllowObjCWritebackConversion); |
| 4136 | // If a single element isn't convertible, fail. |
| 4137 | if (ICS.isBad()) { |
| 4138 | Result = ICS; |
| 4139 | break; |
| 4140 | } |
| 4141 | // Otherwise, look for the worst conversion. |
| 4142 | if (Result.isBad() || |
| 4143 | CompareImplicitConversionSequences(S, ICS, Result) == |
| 4144 | ImplicitConversionSequence::Worse) |
| 4145 | Result = ICS; |
| 4146 | } |
| 4147 | Result.setListInitializationSequence(); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4148 | return Result; |
Sebastian Redl | fe59228 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4149 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4150 | |
| 4151 | // C++11 [over.ics.list]p3: |
| 4152 | // Otherwise, if the parameter is a non-aggregate class X and overload |
| 4153 | // resolution chooses a single best constructor [...] the implicit |
| 4154 | // conversion sequence is a user-defined conversion sequence. If multiple |
| 4155 | // constructors are viable but none is better than the others, the |
| 4156 | // implicit conversion sequence is a user-defined conversion sequence. |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4157 | if (ToType->isRecordType() && !ToType->isAggregateType()) { |
| 4158 | // This function can deal with initializer lists. |
| 4159 | Result = TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 4160 | /*AllowExplicit=*/false, |
| 4161 | InOverloadResolution, /*CStyle=*/false, |
| 4162 | AllowObjCWritebackConversion); |
| 4163 | Result.setListInitializationSequence(); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4164 | return Result; |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4165 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4166 | |
| 4167 | // C++11 [over.ics.list]p4: |
| 4168 | // Otherwise, if the parameter has an aggregate type which can be |
| 4169 | // initialized from the initializer list [...] the implicit conversion |
| 4170 | // sequence is a user-defined conversion sequence. |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4171 | if (ToType->isAggregateType()) { |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4172 | // Type is an aggregate, argument is an init list. At this point it comes |
| 4173 | // down to checking whether the initialization works. |
| 4174 | // FIXME: Find out whether this parameter is consumed or not. |
| 4175 | InitializedEntity Entity = |
| 4176 | InitializedEntity::InitializeParameter(S.Context, ToType, |
| 4177 | /*Consumed=*/false); |
| 4178 | if (S.CanPerformCopyInitialization(Entity, S.Owned(From))) { |
| 4179 | Result.setUserDefined(); |
| 4180 | Result.UserDefined.Before.setAsIdentityConversion(); |
| 4181 | // Initializer lists don't have a type. |
| 4182 | Result.UserDefined.Before.setFromType(QualType()); |
| 4183 | Result.UserDefined.Before.setAllToTypes(QualType()); |
| 4184 | |
| 4185 | Result.UserDefined.After.setAsIdentityConversion(); |
| 4186 | Result.UserDefined.After.setFromType(ToType); |
| 4187 | Result.UserDefined.After.setAllToTypes(ToType); |
| 4188 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4189 | return Result; |
| 4190 | } |
| 4191 | |
| 4192 | // C++11 [over.ics.list]p5: |
| 4193 | // 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] | 4194 | if (ToType->isReferenceType()) { |
| 4195 | // The standard is notoriously unclear here, since 13.3.3.1.4 doesn't |
| 4196 | // mention initializer lists in any way. So we go by what list- |
| 4197 | // initialization would do and try to extrapolate from that. |
| 4198 | |
| 4199 | QualType T1 = ToType->getAs<ReferenceType>()->getPointeeType(); |
| 4200 | |
| 4201 | // If the initializer list has a single element that is reference-related |
| 4202 | // to the parameter type, we initialize the reference from that. |
| 4203 | if (From->getNumInits() == 1) { |
| 4204 | Expr *Init = From->getInit(0); |
| 4205 | |
| 4206 | QualType T2 = Init->getType(); |
| 4207 | |
| 4208 | // If the initializer is the address of an overloaded function, try |
| 4209 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4210 | // type of the resulting function. |
| 4211 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4212 | DeclAccessPair Found; |
| 4213 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction( |
| 4214 | Init, ToType, false, Found)) |
| 4215 | T2 = Fn->getType(); |
| 4216 | } |
| 4217 | |
| 4218 | // Compute some basic properties of the types and the initializer. |
| 4219 | bool dummy1 = false; |
| 4220 | bool dummy2 = false; |
| 4221 | bool dummy3 = false; |
| 4222 | Sema::ReferenceCompareResult RefRelationship |
| 4223 | = S.CompareReferenceRelationship(From->getLocStart(), T1, T2, dummy1, |
| 4224 | dummy2, dummy3); |
| 4225 | |
| 4226 | if (RefRelationship >= Sema::Ref_Related) |
| 4227 | return TryReferenceInit(S, Init, ToType, |
| 4228 | /*FIXME:*/From->getLocStart(), |
| 4229 | SuppressUserConversions, |
| 4230 | /*AllowExplicit=*/false); |
| 4231 | } |
| 4232 | |
| 4233 | // Otherwise, we bind the reference to a temporary created from the |
| 4234 | // initializer list. |
| 4235 | Result = TryListConversion(S, From, T1, SuppressUserConversions, |
| 4236 | InOverloadResolution, |
| 4237 | AllowObjCWritebackConversion); |
| 4238 | if (Result.isFailure()) |
| 4239 | return Result; |
| 4240 | assert(!Result.isEllipsis() && |
| 4241 | "Sub-initialization cannot result in ellipsis conversion."); |
| 4242 | |
| 4243 | // Can we even bind to a temporary? |
| 4244 | if (ToType->isRValueReferenceType() || |
| 4245 | (T1.isConstQualified() && !T1.isVolatileQualified())) { |
| 4246 | StandardConversionSequence &SCS = Result.isStandard() ? Result.Standard : |
| 4247 | Result.UserDefined.After; |
| 4248 | SCS.ReferenceBinding = true; |
| 4249 | SCS.IsLvalueReference = ToType->isLValueReferenceType(); |
| 4250 | SCS.BindsToRvalue = true; |
| 4251 | SCS.BindsToFunctionLvalue = false; |
| 4252 | SCS.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4253 | SCS.ObjCLifetimeConversionBinding = false; |
| 4254 | } else |
| 4255 | Result.setBad(BadConversionSequence::lvalue_ref_to_rvalue, |
| 4256 | From, ToType); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4257 | return Result; |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4258 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4259 | |
| 4260 | // C++11 [over.ics.list]p6: |
| 4261 | // Otherwise, if the parameter type is not a class: |
| 4262 | if (!ToType->isRecordType()) { |
| 4263 | // - if the initializer list has one element, the implicit conversion |
| 4264 | // sequence is the one required to convert the element to the |
| 4265 | // parameter type. |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4266 | unsigned NumInits = From->getNumInits(); |
| 4267 | if (NumInits == 1) |
| 4268 | Result = TryCopyInitialization(S, From->getInit(0), ToType, |
| 4269 | SuppressUserConversions, |
| 4270 | InOverloadResolution, |
| 4271 | AllowObjCWritebackConversion); |
| 4272 | // - if the initializer list has no elements, the implicit conversion |
| 4273 | // sequence is the identity conversion. |
| 4274 | else if (NumInits == 0) { |
| 4275 | Result.setStandard(); |
| 4276 | Result.Standard.setAsIdentityConversion(); |
| 4277 | } |
| 4278 | return Result; |
| 4279 | } |
| 4280 | |
| 4281 | // C++11 [over.ics.list]p7: |
| 4282 | // In all cases other than those enumerated above, no conversion is possible |
| 4283 | return Result; |
| 4284 | } |
| 4285 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4286 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 4287 | /// ToType from the expression From. Return the implicit conversion |
| 4288 | /// sequence required to pass this argument, which may be a bad |
| 4289 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4290 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | 74e386e | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 4291 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4292 | static ImplicitConversionSequence |
| 4293 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4294 | bool SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4295 | bool InOverloadResolution, |
| 4296 | bool AllowObjCWritebackConversion) { |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4297 | if (InitListExpr *FromInitList = dyn_cast<InitListExpr>(From)) |
| 4298 | return TryListConversion(S, FromInitList, ToType, SuppressUserConversions, |
| 4299 | InOverloadResolution,AllowObjCWritebackConversion); |
| 4300 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4301 | if (ToType->isReferenceType()) |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4302 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4303 | /*FIXME:*/From->getLocStart(), |
| 4304 | SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 4305 | /*AllowExplicit=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4306 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4307 | return TryImplicitConversion(S, From, ToType, |
| 4308 | SuppressUserConversions, |
| 4309 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4310 | InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4311 | /*CStyle=*/false, |
| 4312 | AllowObjCWritebackConversion); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4313 | } |
| 4314 | |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 4315 | static bool TryCopyInitialization(const CanQualType FromQTy, |
| 4316 | const CanQualType ToQTy, |
| 4317 | Sema &S, |
| 4318 | SourceLocation Loc, |
| 4319 | ExprValueKind FromVK) { |
| 4320 | OpaqueValueExpr TmpExpr(Loc, FromQTy, FromVK); |
| 4321 | ImplicitConversionSequence ICS = |
| 4322 | TryCopyInitialization(S, &TmpExpr, ToQTy, true, true, false); |
| 4323 | |
| 4324 | return !ICS.isBad(); |
| 4325 | } |
| 4326 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4327 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 4328 | /// parameter of the given member function (@c Method) from the |
| 4329 | /// expression @p From. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4330 | static ImplicitConversionSequence |
| 4331 | TryObjectArgumentInitialization(Sema &S, QualType OrigFromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4332 | Expr::Classification FromClassification, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4333 | CXXMethodDecl *Method, |
| 4334 | CXXRecordDecl *ActingContext) { |
| 4335 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 4336 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 4337 | // const volatile object. |
| 4338 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 4339 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4340 | QualType ImplicitParamType = S.Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4341 | |
| 4342 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 4343 | // to exit early. |
| 4344 | ImplicitConversionSequence ICS; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4345 | |
| 4346 | // We need to have an object of class type. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 4347 | QualType FromType = OrigFromType; |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4348 | if (const PointerType *PT = FromType->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4349 | FromType = PT->getPointeeType(); |
| 4350 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4351 | // When we had a pointer, it's implicitly dereferenced, so we |
| 4352 | // better have an lvalue. |
| 4353 | assert(FromClassification.isLValue()); |
| 4354 | } |
| 4355 | |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4356 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4357 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4358 | // C++0x [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4359 | // For non-static member functions, the type of the implicit object |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4360 | // parameter is |
| 4361 | // |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4362 | // - "lvalue reference to cv X" for functions declared without a |
| 4363 | // ref-qualifier or with the & ref-qualifier |
| 4364 | // - "rvalue reference to cv X" for functions declared with the && |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4365 | // ref-qualifier |
| 4366 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4367 | // 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] | 4368 | // cv-qualification on the member function declaration. |
| 4369 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4370 | // However, when finding an implicit conversion sequence for the argument, we |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4371 | // are not allowed to create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4372 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 4373 | // reference binding here, that allows class rvalues to bind to |
| 4374 | // non-constant references. |
| 4375 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4376 | // First check the qualifiers. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4377 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4378 | if (ImplicitParamType.getCVRQualifiers() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4379 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4380 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4381 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
| 4382 | OrigFromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4383 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4384 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4385 | |
| 4386 | // Check that we have either the same type or a derived type. It |
| 4387 | // affects the conversion rank. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4388 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4389 | ImplicitConversionKind SecondKind; |
| 4390 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 4391 | SecondKind = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4392 | } else if (S.IsDerivedFrom(FromType, ClassType)) |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4393 | SecondKind = ICK_Derived_To_Base; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4394 | else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4395 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 4396 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4397 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4398 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4399 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4400 | // Check the ref-qualifier. |
| 4401 | switch (Method->getRefQualifier()) { |
| 4402 | case RQ_None: |
| 4403 | // Do nothing; we don't care about lvalueness or rvalueness. |
| 4404 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4405 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4406 | case RQ_LValue: |
| 4407 | if (!FromClassification.isLValue() && Quals != Qualifiers::Const) { |
| 4408 | // non-const lvalue reference cannot bind to an rvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4409 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4410 | ImplicitParamType); |
| 4411 | return ICS; |
| 4412 | } |
| 4413 | break; |
| 4414 | |
| 4415 | case RQ_RValue: |
| 4416 | if (!FromClassification.isRValue()) { |
| 4417 | // rvalue reference cannot bind to an lvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4418 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4419 | ImplicitParamType); |
| 4420 | return ICS; |
| 4421 | } |
| 4422 | break; |
| 4423 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4424 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4425 | // Success. Mark this as a reference binding. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4426 | ICS.setStandard(); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4427 | ICS.Standard.setAsIdentityConversion(); |
| 4428 | ICS.Standard.Second = SecondKind; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4429 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 4430 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4431 | ICS.Standard.ReferenceBinding = true; |
| 4432 | ICS.Standard.DirectBinding = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4433 | ICS.Standard.IsLvalueReference = Method->getRefQualifier() != RQ_RValue; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4434 | ICS.Standard.BindsToFunctionLvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4435 | ICS.Standard.BindsToRvalue = FromClassification.isRValue(); |
| 4436 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier |
| 4437 | = (Method->getRefQualifier() == RQ_None); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4438 | return ICS; |
| 4439 | } |
| 4440 | |
| 4441 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 4442 | /// the implicit object parameter for the given Method with the given |
| 4443 | /// expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4444 | ExprResult |
| 4445 | Sema::PerformObjectArgumentInitialization(Expr *From, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4446 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4447 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4448 | CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4449 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4450 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4451 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4452 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4453 | Expr::Classification FromClassification; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4454 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4455 | FromRecordType = PT->getPointeeType(); |
| 4456 | DestType = Method->getThisType(Context); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4457 | FromClassification = Expr::Classification::makeSimpleLValue(); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4458 | } else { |
| 4459 | FromRecordType = From->getType(); |
| 4460 | DestType = ImplicitParamRecordType; |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4461 | FromClassification = From->Classify(Context); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4462 | } |
| 4463 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4464 | // Note that we always use the true parent context when performing |
| 4465 | // the actual argument initialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4466 | ImplicitConversionSequence ICS |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4467 | = TryObjectArgumentInitialization(*this, From->getType(), FromClassification, |
| 4468 | Method, Method->getParent()); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4469 | if (ICS.isBad()) { |
| 4470 | if (ICS.Bad.Kind == BadConversionSequence::bad_qualifiers) { |
| 4471 | Qualifiers FromQs = FromRecordType.getQualifiers(); |
| 4472 | Qualifiers ToQs = DestType.getQualifiers(); |
| 4473 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 4474 | if (CVR) { |
| 4475 | Diag(From->getSourceRange().getBegin(), |
| 4476 | diag::err_member_function_call_bad_cvr) |
| 4477 | << Method->getDeclName() << FromRecordType << (CVR - 1) |
| 4478 | << From->getSourceRange(); |
| 4479 | Diag(Method->getLocation(), diag::note_previous_decl) |
| 4480 | << Method->getDeclName(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4481 | return ExprError(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4482 | } |
| 4483 | } |
| 4484 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4485 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4486 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4487 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4488 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4489 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4490 | if (ICS.Standard.Second == ICK_Derived_To_Base) { |
| 4491 | ExprResult FromRes = |
| 4492 | PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
| 4493 | if (FromRes.isInvalid()) |
| 4494 | return ExprError(); |
| 4495 | From = FromRes.take(); |
| 4496 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4497 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4498 | if (!Context.hasSameType(From->getType(), DestType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4499 | From = ImpCastExprToType(From, DestType, CK_NoOp, |
Richard Smith | acdfa4d | 2011-11-10 23:32:36 +0000 | [diff] [blame] | 4500 | From->getValueKind()).take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4501 | return Owned(From); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4502 | } |
| 4503 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4504 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 4505 | /// expression From to bool (C++0x [conv]p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4506 | static ImplicitConversionSequence |
| 4507 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
Douglas Gregor | c6dfe19 | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 4508 | // FIXME: This is pretty broken. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4509 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 4510 | // FIXME: Are these flags correct? |
| 4511 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4512 | /*AllowExplicit=*/true, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4513 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4514 | /*CStyle=*/false, |
| 4515 | /*AllowObjCWritebackConversion=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4516 | } |
| 4517 | |
| 4518 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 4519 | /// of the expression From to bool (C++0x [conv]p3). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4520 | ExprResult Sema::PerformContextuallyConvertToBool(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4521 | if (checkPlaceholderForOverload(*this, From)) |
| 4522 | return ExprError(); |
| 4523 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4524 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4525 | if (!ICS.isBad()) |
| 4526 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4527 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 4528 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4529 | return Diag(From->getSourceRange().getBegin(), |
| 4530 | diag::err_typecheck_bool_condition) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 4531 | << From->getType() << From->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4532 | return ExprError(); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4533 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4534 | |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4535 | /// Check that the specified conversion is permitted in a converted constant |
| 4536 | /// expression, according to C++11 [expr.const]p3. Return true if the conversion |
| 4537 | /// is acceptable. |
| 4538 | static bool CheckConvertedConstantConversions(Sema &S, |
| 4539 | StandardConversionSequence &SCS) { |
| 4540 | // Since we know that the target type is an integral or unscoped enumeration |
| 4541 | // type, most conversion kinds are impossible. All possible First and Third |
| 4542 | // conversions are fine. |
| 4543 | switch (SCS.Second) { |
| 4544 | case ICK_Identity: |
| 4545 | case ICK_Integral_Promotion: |
| 4546 | case ICK_Integral_Conversion: |
| 4547 | return true; |
| 4548 | |
| 4549 | case ICK_Boolean_Conversion: |
| 4550 | // Conversion from an integral or unscoped enumeration type to bool is |
| 4551 | // classified as ICK_Boolean_Conversion, but it's also an integral |
| 4552 | // conversion, so it's permitted in a converted constant expression. |
| 4553 | return SCS.getFromType()->isIntegralOrUnscopedEnumerationType() && |
| 4554 | SCS.getToType(2)->isBooleanType(); |
| 4555 | |
| 4556 | case ICK_Floating_Integral: |
| 4557 | case ICK_Complex_Real: |
| 4558 | return false; |
| 4559 | |
| 4560 | case ICK_Lvalue_To_Rvalue: |
| 4561 | case ICK_Array_To_Pointer: |
| 4562 | case ICK_Function_To_Pointer: |
| 4563 | case ICK_NoReturn_Adjustment: |
| 4564 | case ICK_Qualification: |
| 4565 | case ICK_Compatible_Conversion: |
| 4566 | case ICK_Vector_Conversion: |
| 4567 | case ICK_Vector_Splat: |
| 4568 | case ICK_Derived_To_Base: |
| 4569 | case ICK_Pointer_Conversion: |
| 4570 | case ICK_Pointer_Member: |
| 4571 | case ICK_Block_Pointer_Conversion: |
| 4572 | case ICK_Writeback_Conversion: |
| 4573 | case ICK_Floating_Promotion: |
| 4574 | case ICK_Complex_Promotion: |
| 4575 | case ICK_Complex_Conversion: |
| 4576 | case ICK_Floating_Conversion: |
| 4577 | case ICK_TransparentUnionConversion: |
| 4578 | llvm_unreachable("unexpected second conversion kind"); |
| 4579 | |
| 4580 | case ICK_Num_Conversion_Kinds: |
| 4581 | break; |
| 4582 | } |
| 4583 | |
| 4584 | llvm_unreachable("unknown conversion kind"); |
| 4585 | } |
| 4586 | |
| 4587 | /// CheckConvertedConstantExpression - Check that the expression From is a |
| 4588 | /// converted constant expression of type T, perform the conversion and produce |
| 4589 | /// the converted expression, per C++11 [expr.const]p3. |
| 4590 | ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, |
| 4591 | llvm::APSInt &Value, |
| 4592 | CCEKind CCE) { |
| 4593 | assert(LangOpts.CPlusPlus0x && "converted constant expression outside C++11"); |
| 4594 | assert(T->isIntegralOrEnumerationType() && "unexpected converted const type"); |
| 4595 | |
| 4596 | if (checkPlaceholderForOverload(*this, From)) |
| 4597 | return ExprError(); |
| 4598 | |
| 4599 | // C++11 [expr.const]p3 with proposed wording fixes: |
| 4600 | // A converted constant expression of type T is a core constant expression, |
| 4601 | // implicitly converted to a prvalue of type T, where the converted |
| 4602 | // expression is a literal constant expression and the implicit conversion |
| 4603 | // sequence contains only user-defined conversions, lvalue-to-rvalue |
| 4604 | // conversions, integral promotions, and integral conversions other than |
| 4605 | // narrowing conversions. |
| 4606 | ImplicitConversionSequence ICS = |
| 4607 | TryImplicitConversion(From, T, |
| 4608 | /*SuppressUserConversions=*/false, |
| 4609 | /*AllowExplicit=*/false, |
| 4610 | /*InOverloadResolution=*/false, |
| 4611 | /*CStyle=*/false, |
| 4612 | /*AllowObjcWritebackConversion=*/false); |
| 4613 | StandardConversionSequence *SCS = 0; |
| 4614 | switch (ICS.getKind()) { |
| 4615 | case ImplicitConversionSequence::StandardConversion: |
| 4616 | if (!CheckConvertedConstantConversions(*this, ICS.Standard)) |
| 4617 | return Diag(From->getSourceRange().getBegin(), |
| 4618 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 4619 | << From->getType() << From->getSourceRange() << T; |
| 4620 | SCS = &ICS.Standard; |
| 4621 | break; |
| 4622 | case ImplicitConversionSequence::UserDefinedConversion: |
| 4623 | // We are converting from class type to an integral or enumeration type, so |
| 4624 | // the Before sequence must be trivial. |
| 4625 | if (!CheckConvertedConstantConversions(*this, ICS.UserDefined.After)) |
| 4626 | return Diag(From->getSourceRange().getBegin(), |
| 4627 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 4628 | << From->getType() << From->getSourceRange() << T; |
| 4629 | SCS = &ICS.UserDefined.After; |
| 4630 | break; |
| 4631 | case ImplicitConversionSequence::AmbiguousConversion: |
| 4632 | case ImplicitConversionSequence::BadConversion: |
| 4633 | if (!DiagnoseMultipleUserDefinedConversion(From, T)) |
| 4634 | return Diag(From->getSourceRange().getBegin(), |
| 4635 | diag::err_typecheck_converted_constant_expression) |
| 4636 | << From->getType() << From->getSourceRange() << T; |
| 4637 | return ExprError(); |
| 4638 | |
| 4639 | case ImplicitConversionSequence::EllipsisConversion: |
| 4640 | llvm_unreachable("ellipsis conversion in converted constant expression"); |
| 4641 | } |
| 4642 | |
| 4643 | ExprResult Result = PerformImplicitConversion(From, T, ICS, AA_Converting); |
| 4644 | if (Result.isInvalid()) |
| 4645 | return Result; |
| 4646 | |
| 4647 | // Check for a narrowing implicit conversion. |
| 4648 | APValue PreNarrowingValue; |
| 4649 | switch (SCS->getNarrowingKind(Context, Result.get(), PreNarrowingValue)) { |
| 4650 | case NK_Variable_Narrowing: |
| 4651 | // Implicit conversion to a narrower type, and the value is not a constant |
| 4652 | // expression. We'll diagnose this in a moment. |
| 4653 | case NK_Not_Narrowing: |
| 4654 | break; |
| 4655 | |
| 4656 | case NK_Constant_Narrowing: |
| 4657 | Diag(From->getSourceRange().getBegin(), diag::err_cce_narrowing) |
| 4658 | << CCE << /*Constant*/1 |
| 4659 | << PreNarrowingValue.getAsString(Context, QualType()) << T; |
| 4660 | break; |
| 4661 | |
| 4662 | case NK_Type_Narrowing: |
| 4663 | Diag(From->getSourceRange().getBegin(), diag::err_cce_narrowing) |
| 4664 | << CCE << /*Constant*/0 << From->getType() << T; |
| 4665 | break; |
| 4666 | } |
| 4667 | |
| 4668 | // Check the expression is a constant expression. |
| 4669 | llvm::SmallVector<PartialDiagnosticAt, 8> Notes; |
| 4670 | Expr::EvalResult Eval; |
| 4671 | Eval.Diag = &Notes; |
| 4672 | |
| 4673 | if (!Result.get()->EvaluateAsRValue(Eval, Context)) { |
| 4674 | // The expression can't be folded, so we can't keep it at this position in |
| 4675 | // the AST. |
| 4676 | Result = ExprError(); |
| 4677 | } else if (Notes.empty()) { |
| 4678 | // It's a constant expression. |
| 4679 | Value = Eval.Val.getInt(); |
| 4680 | return Result; |
| 4681 | } |
| 4682 | |
| 4683 | // It's not a constant expression. Produce an appropriate diagnostic. |
| 4684 | if (Notes.size() == 1 && |
| 4685 | Notes[0].second.getDiagID() == diag::note_invalid_subexpr_in_const_expr) |
| 4686 | Diag(Notes[0].first, diag::err_expr_not_cce) << CCE; |
| 4687 | else { |
| 4688 | Diag(From->getSourceRange().getBegin(), diag::err_expr_not_cce) |
| 4689 | << CCE << From->getSourceRange(); |
| 4690 | for (unsigned I = 0; I < Notes.size(); ++I) |
| 4691 | Diag(Notes[I].first, Notes[I].second); |
| 4692 | } |
| 4693 | return ExprError(); |
| 4694 | } |
| 4695 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 4696 | /// dropPointerConversions - If the given standard conversion sequence |
| 4697 | /// involves any pointer conversions, remove them. This may change |
| 4698 | /// the result type of the conversion sequence. |
| 4699 | static void dropPointerConversion(StandardConversionSequence &SCS) { |
| 4700 | if (SCS.Second == ICK_Pointer_Conversion) { |
| 4701 | SCS.Second = ICK_Identity; |
| 4702 | SCS.Third = ICK_Identity; |
| 4703 | SCS.ToTypePtrs[2] = SCS.ToTypePtrs[1] = SCS.ToTypePtrs[0]; |
| 4704 | } |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 4705 | } |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4706 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 4707 | /// TryContextuallyConvertToObjCPointer - Attempt to contextually |
| 4708 | /// convert the expression From to an Objective-C pointer type. |
| 4709 | static ImplicitConversionSequence |
| 4710 | TryContextuallyConvertToObjCPointer(Sema &S, Expr *From) { |
| 4711 | // Do an implicit conversion to 'id'. |
| 4712 | QualType Ty = S.Context.getObjCIdType(); |
| 4713 | ImplicitConversionSequence ICS |
| 4714 | = TryImplicitConversion(S, From, Ty, |
| 4715 | // FIXME: Are these flags correct? |
| 4716 | /*SuppressUserConversions=*/false, |
| 4717 | /*AllowExplicit=*/true, |
| 4718 | /*InOverloadResolution=*/false, |
| 4719 | /*CStyle=*/false, |
| 4720 | /*AllowObjCWritebackConversion=*/false); |
| 4721 | |
| 4722 | // Strip off any final conversions to 'id'. |
| 4723 | switch (ICS.getKind()) { |
| 4724 | case ImplicitConversionSequence::BadConversion: |
| 4725 | case ImplicitConversionSequence::AmbiguousConversion: |
| 4726 | case ImplicitConversionSequence::EllipsisConversion: |
| 4727 | break; |
| 4728 | |
| 4729 | case ImplicitConversionSequence::UserDefinedConversion: |
| 4730 | dropPointerConversion(ICS.UserDefined.After); |
| 4731 | break; |
| 4732 | |
| 4733 | case ImplicitConversionSequence::StandardConversion: |
| 4734 | dropPointerConversion(ICS.Standard); |
| 4735 | break; |
| 4736 | } |
| 4737 | |
| 4738 | return ICS; |
| 4739 | } |
| 4740 | |
| 4741 | /// PerformContextuallyConvertToObjCPointer - Perform a contextual |
| 4742 | /// conversion of the expression From to an Objective-C pointer type. |
| 4743 | ExprResult Sema::PerformContextuallyConvertToObjCPointer(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4744 | if (checkPlaceholderForOverload(*this, From)) |
| 4745 | return ExprError(); |
| 4746 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4747 | QualType Ty = Context.getObjCIdType(); |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 4748 | ImplicitConversionSequence ICS = |
| 4749 | TryContextuallyConvertToObjCPointer(*this, From); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 4750 | if (!ICS.isBad()) |
| 4751 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4752 | return ExprError(); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 4753 | } |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4754 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4755 | /// \brief Attempt to convert the given expression to an integral or |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4756 | /// enumeration type. |
| 4757 | /// |
| 4758 | /// This routine will attempt to convert an expression of class type to an |
| 4759 | /// integral or enumeration type, if that class type only has a single |
| 4760 | /// conversion to an integral or enumeration type. |
| 4761 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 4762 | /// \param Loc The source location of the construct that requires the |
| 4763 | /// conversion. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4764 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 4765 | /// \param FromE The expression we're converting from. |
| 4766 | /// |
| 4767 | /// \param NotIntDiag The diagnostic to be emitted if the expression does not |
| 4768 | /// have integral or enumeration type. |
| 4769 | /// |
| 4770 | /// \param IncompleteDiag The diagnostic to be emitted if the expression has |
| 4771 | /// incomplete class type. |
| 4772 | /// |
| 4773 | /// \param ExplicitConvDiag The diagnostic to be emitted if we're calling an |
| 4774 | /// explicit conversion function (because no implicit conversion functions |
| 4775 | /// were available). This is a recovery mode. |
| 4776 | /// |
| 4777 | /// \param ExplicitConvNote The note to be emitted with \p ExplicitConvDiag, |
| 4778 | /// showing which conversion was picked. |
| 4779 | /// |
| 4780 | /// \param AmbigDiag The diagnostic to be emitted if there is more than one |
| 4781 | /// conversion function that could convert to integral or enumeration type. |
| 4782 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4783 | /// \param AmbigNote The note to be emitted with \p AmbigDiag for each |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 4784 | /// usable conversion function. |
| 4785 | /// |
| 4786 | /// \param ConvDiag The diagnostic to be emitted if we are calling a conversion |
| 4787 | /// function, which may be an extension in this case. |
| 4788 | /// |
| 4789 | /// \returns The expression, converted to an integral or enumeration type if |
| 4790 | /// successful. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4791 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4792 | Sema::ConvertToIntegralOrEnumerationType(SourceLocation Loc, Expr *From, |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4793 | const PartialDiagnostic &NotIntDiag, |
| 4794 | const PartialDiagnostic &IncompleteDiag, |
| 4795 | const PartialDiagnostic &ExplicitConvDiag, |
| 4796 | const PartialDiagnostic &ExplicitConvNote, |
| 4797 | const PartialDiagnostic &AmbigDiag, |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 4798 | const PartialDiagnostic &AmbigNote, |
| 4799 | const PartialDiagnostic &ConvDiag) { |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4800 | // We can't perform any more checking for type-dependent expressions. |
| 4801 | if (From->isTypeDependent()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4802 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4803 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4804 | // If the expression already has integral or enumeration type, we're golden. |
| 4805 | QualType T = From->getType(); |
| 4806 | if (T->isIntegralOrEnumerationType()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4807 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4808 | |
| 4809 | // FIXME: Check for missing '()' if T is a function type? |
| 4810 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4811 | // If we don't have a class type in C++, there's no way we can get an |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4812 | // expression of integral or enumeration type. |
| 4813 | const RecordType *RecordTy = T->getAs<RecordType>(); |
| 4814 | if (!RecordTy || !getLangOptions().CPlusPlus) { |
| 4815 | Diag(Loc, NotIntDiag) |
| 4816 | << T << From->getSourceRange(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4817 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4818 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4819 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4820 | // We must have a complete class type. |
| 4821 | if (RequireCompleteType(Loc, T, IncompleteDiag)) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4822 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4823 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4824 | // Look for a conversion to an integral or enumeration type. |
| 4825 | UnresolvedSet<4> ViableConversions; |
| 4826 | UnresolvedSet<4> ExplicitConversions; |
| 4827 | const UnresolvedSetImpl *Conversions |
| 4828 | = cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4829 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4830 | bool HadMultipleCandidates = (Conversions->size() > 1); |
| 4831 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4832 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4833 | E = Conversions->end(); |
| 4834 | I != E; |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4835 | ++I) { |
| 4836 | if (CXXConversionDecl *Conversion |
| 4837 | = dyn_cast<CXXConversionDecl>((*I)->getUnderlyingDecl())) |
| 4838 | if (Conversion->getConversionType().getNonReferenceType() |
| 4839 | ->isIntegralOrEnumerationType()) { |
| 4840 | if (Conversion->isExplicit()) |
| 4841 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
| 4842 | else |
| 4843 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
| 4844 | } |
| 4845 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4846 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4847 | switch (ViableConversions.size()) { |
| 4848 | case 0: |
| 4849 | if (ExplicitConversions.size() == 1) { |
| 4850 | DeclAccessPair Found = ExplicitConversions[0]; |
| 4851 | CXXConversionDecl *Conversion |
| 4852 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4853 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4854 | // The user probably meant to invoke the given explicit |
| 4855 | // conversion; use it. |
| 4856 | QualType ConvTy |
| 4857 | = Conversion->getConversionType().getNonReferenceType(); |
| 4858 | std::string TypeStr; |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 4859 | ConvTy.getAsStringInternal(TypeStr, getPrintingPolicy()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4860 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4861 | Diag(Loc, ExplicitConvDiag) |
| 4862 | << T << ConvTy |
| 4863 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 4864 | "static_cast<" + TypeStr + ">(") |
| 4865 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(From->getLocEnd()), |
| 4866 | ")"); |
| 4867 | Diag(Conversion->getLocation(), ExplicitConvNote) |
| 4868 | << ConvTy->isEnumeralType() << ConvTy; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4869 | |
| 4870 | // If we aren't in a SFINAE context, build a call to the |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4871 | // explicit conversion function. |
| 4872 | if (isSFINAEContext()) |
| 4873 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4874 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4875 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4876 | ExprResult Result = BuildCXXMemberCallExpr(From, Found, Conversion, |
| 4877 | HadMultipleCandidates); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 4878 | if (Result.isInvalid()) |
| 4879 | return ExprError(); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 4880 | // Record usage of conversion in an implicit cast. |
| 4881 | From = ImplicitCastExpr::Create(Context, Result.get()->getType(), |
| 4882 | CK_UserDefinedConversion, |
| 4883 | Result.get(), 0, |
| 4884 | Result.get()->getValueKind()); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4885 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4886 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4887 | // We'll complain below about a non-integral condition type. |
| 4888 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4889 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4890 | case 1: { |
| 4891 | // Apply this conversion. |
| 4892 | DeclAccessPair Found = ViableConversions[0]; |
| 4893 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4894 | |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 4895 | CXXConversionDecl *Conversion |
| 4896 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 4897 | QualType ConvTy |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4898 | = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 4899 | if (ConvDiag.getDiagID()) { |
| 4900 | if (isSFINAEContext()) |
| 4901 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4902 | |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 4903 | Diag(Loc, ConvDiag) |
| 4904 | << T << ConvTy->isEnumeralType() << ConvTy << From->getSourceRange(); |
| 4905 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4906 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4907 | ExprResult Result = BuildCXXMemberCallExpr(From, Found, Conversion, |
| 4908 | HadMultipleCandidates); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 4909 | if (Result.isInvalid()) |
| 4910 | return ExprError(); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 4911 | // Record usage of conversion in an implicit cast. |
| 4912 | From = ImplicitCastExpr::Create(Context, Result.get()->getType(), |
| 4913 | CK_UserDefinedConversion, |
| 4914 | Result.get(), 0, |
| 4915 | Result.get()->getValueKind()); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4916 | break; |
| 4917 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4918 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4919 | default: |
| 4920 | Diag(Loc, AmbigDiag) |
| 4921 | << T << From->getSourceRange(); |
| 4922 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 4923 | CXXConversionDecl *Conv |
| 4924 | = cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 4925 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 4926 | Diag(Conv->getLocation(), AmbigNote) |
| 4927 | << ConvTy->isEnumeralType() << ConvTy; |
| 4928 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4929 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4930 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4931 | |
Douglas Gregor | acb0bd8 | 2010-06-29 23:25:20 +0000 | [diff] [blame] | 4932 | if (!From->getType()->isIntegralOrEnumerationType()) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4933 | Diag(Loc, NotIntDiag) |
| 4934 | << From->getType() << From->getSourceRange(); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4935 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4936 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4937 | } |
| 4938 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4939 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4940 | /// candidate functions, using the given function call arguments. If |
| 4941 | /// @p SuppressUserConversions, then don't allow user-defined |
| 4942 | /// conversions via constructors or conversion operators. |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4943 | /// |
| 4944 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 4945 | /// based on an incomplete set of function arguments. This feature is used by |
| 4946 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4947 | void |
| 4948 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4949 | DeclAccessPair FoundDecl, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4950 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4951 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 4952 | bool SuppressUserConversions, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4953 | bool PartialOverloading) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4954 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4955 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4956 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4957 | assert(!Function->getDescribedFunctionTemplate() && |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4958 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4959 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4960 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4961 | if (!isa<CXXConstructorDecl>(Method)) { |
| 4962 | // If we get here, it's because we're calling a member function |
| 4963 | // that is named without a member access expression (e.g., |
| 4964 | // "this->f") that was either written explicitly or created |
| 4965 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4966 | // function, e.g., X::f(). We use an empty type for the implied |
| 4967 | // object argument (C++ [over.call.func]p3), and the acting context |
| 4968 | // is irrelevant. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4969 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4970 | QualType(), Expr::Classification::makeSimpleLValue(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4971 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4972 | SuppressUserConversions); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4973 | return; |
| 4974 | } |
| 4975 | // We treat a constructor like a non-member function, since its object |
| 4976 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4977 | } |
| 4978 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 4979 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4980 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 4981 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4982 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4983 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4984 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 4985 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 4986 | // C++ [class.copy]p3: |
| 4987 | // A member function template is never instantiated to perform the copy |
| 4988 | // of a class object to an object of its class type. |
| 4989 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4990 | if (NumArgs == 1 && |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4991 | Constructor->isSpecializationCopyingObject() && |
Douglas Gregor | 1211606 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 4992 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 4993 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 4994 | return; |
| 4995 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4996 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4997 | // Add this candidate |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 4998 | OverloadCandidate &Candidate = CandidateSet.addCandidate(NumArgs); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4999 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5000 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5001 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5002 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5003 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5004 | Candidate.ExplicitCallArguments = NumArgs; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5005 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5006 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5007 | |
| 5008 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5009 | // parameters is viable only if it has an ellipsis in its parameter |
| 5010 | // list (8.3.5). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5011 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 5012 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5013 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5014 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5015 | return; |
| 5016 | } |
| 5017 | |
| 5018 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5019 | // is viable only if the (m+1)st parameter has a default argument |
| 5020 | // (8.3.6). For the purposes of overload resolution, the |
| 5021 | // parameter list is truncated on the right, so that there are |
| 5022 | // exactly m parameters. |
| 5023 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5024 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5025 | // Not enough arguments. |
| 5026 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5027 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5028 | return; |
| 5029 | } |
| 5030 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5031 | // (CUDA B.1): Check for invalid calls between targets. |
| 5032 | if (getLangOptions().CUDA) |
| 5033 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
| 5034 | if (CheckCUDATarget(Caller, Function)) { |
| 5035 | Candidate.Viable = false; |
| 5036 | Candidate.FailureKind = ovl_fail_bad_target; |
| 5037 | return; |
| 5038 | } |
| 5039 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5040 | // Determine the implicit conversion sequences for each of the |
| 5041 | // arguments. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5042 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 5043 | if (ArgIdx < NumArgsInProto) { |
| 5044 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5045 | // exist for each argument an implicit conversion sequence |
| 5046 | // (13.3.3.1) that converts that argument to the corresponding |
| 5047 | // parameter of F. |
| 5048 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5049 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5050 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5051 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5052 | /*InOverloadResolution=*/true, |
| 5053 | /*AllowObjCWritebackConversion=*/ |
| 5054 | getLangOptions().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5055 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 5056 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5057 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5058 | break; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5059 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5060 | } else { |
| 5061 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5062 | // argument for which there is no corresponding parameter is |
| 5063 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5064 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5065 | } |
| 5066 | } |
| 5067 | } |
| 5068 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5069 | /// \brief Add all of the function declarations in the given function set to |
| 5070 | /// the overload canddiate set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5071 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5072 | Expr **Args, unsigned NumArgs, |
| 5073 | OverloadCandidateSet& CandidateSet, |
| 5074 | bool SuppressUserConversions) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5075 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5076 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 5077 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5078 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5079 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5080 | cast<CXXMethodDecl>(FD)->getParent(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5081 | Args[0]->getType(), Args[0]->Classify(Context), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5082 | Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5083 | CandidateSet, SuppressUserConversions); |
| 5084 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5085 | AddOverloadCandidate(FD, F.getPair(), Args, NumArgs, CandidateSet, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5086 | SuppressUserConversions); |
| 5087 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5088 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5089 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 5090 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5091 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5092 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5093 | /*FIXME: explicit args */ 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5094 | Args[0]->getType(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5095 | Args[0]->Classify(Context), |
| 5096 | Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5097 | CandidateSet, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5098 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5099 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5100 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5101 | /*FIXME: explicit args */ 0, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5102 | Args, NumArgs, CandidateSet, |
| 5103 | SuppressUserConversions); |
| 5104 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5105 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5106 | } |
| 5107 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5108 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 5109 | /// method) as a method candidate to the given overload set. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5110 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5111 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5112 | Expr::Classification ObjectClassification, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5113 | Expr **Args, unsigned NumArgs, |
| 5114 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5115 | bool SuppressUserConversions) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5116 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5117 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5118 | |
| 5119 | if (isa<UsingShadowDecl>(Decl)) |
| 5120 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5121 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5122 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 5123 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 5124 | "Expected a member function template"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5125 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 5126 | /*ExplicitArgs*/ 0, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5127 | ObjectType, ObjectClassification, Args, NumArgs, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5128 | CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5129 | SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5130 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5131 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5132 | ObjectType, ObjectClassification, Args, NumArgs, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5133 | CandidateSet, SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5134 | } |
| 5135 | } |
| 5136 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5137 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 5138 | /// of candidate functions, using the given function call arguments |
| 5139 | /// and the object argument (@c Object). For example, in a call |
| 5140 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 5141 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 5142 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5143 | /// operators. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5144 | void |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5145 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5146 | CXXRecordDecl *ActingContext, QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5147 | Expr::Classification ObjectClassification, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5148 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5149 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5150 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5151 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5152 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5153 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5154 | assert(!isa<CXXConstructorDecl>(Method) && |
| 5155 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5156 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5157 | if (!CandidateSet.isNewCandidate(Method)) |
| 5158 | return; |
| 5159 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5160 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5161 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5162 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5163 | // Add this candidate |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5164 | OverloadCandidate &Candidate = CandidateSet.addCandidate(NumArgs + 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5165 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5166 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5167 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5168 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5169 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5170 | |
| 5171 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5172 | |
| 5173 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5174 | // parameters is viable only if it has an ellipsis in its parameter |
| 5175 | // list (8.3.5). |
| 5176 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 5177 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5178 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5179 | return; |
| 5180 | } |
| 5181 | |
| 5182 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5183 | // is viable only if the (m+1)st parameter has a default argument |
| 5184 | // (8.3.6). For the purposes of overload resolution, the |
| 5185 | // parameter list is truncated on the right, so that there are |
| 5186 | // exactly m parameters. |
| 5187 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 5188 | if (NumArgs < MinRequiredArgs) { |
| 5189 | // Not enough arguments. |
| 5190 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5191 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5192 | return; |
| 5193 | } |
| 5194 | |
| 5195 | Candidate.Viable = true; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5196 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5197 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5198 | // The implicit object argument is ignored. |
| 5199 | Candidate.IgnoreObjectArgument = true; |
| 5200 | else { |
| 5201 | // Determine the implicit conversion sequence for the object |
| 5202 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5203 | Candidate.Conversions[0] |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5204 | = TryObjectArgumentInitialization(*this, ObjectType, ObjectClassification, |
| 5205 | Method, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5206 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5207 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5208 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5209 | return; |
| 5210 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5211 | } |
| 5212 | |
| 5213 | // Determine the implicit conversion sequences for each of the |
| 5214 | // arguments. |
| 5215 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 5216 | if (ArgIdx < NumArgsInProto) { |
| 5217 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5218 | // exist for each argument an implicit conversion sequence |
| 5219 | // (13.3.3.1) that converts that argument to the corresponding |
| 5220 | // parameter of F. |
| 5221 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5222 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5223 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5224 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5225 | /*InOverloadResolution=*/true, |
| 5226 | /*AllowObjCWritebackConversion=*/ |
| 5227 | getLangOptions().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5228 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5229 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5230 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5231 | break; |
| 5232 | } |
| 5233 | } else { |
| 5234 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5235 | // argument for which there is no corresponding parameter is |
| 5236 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5237 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5238 | } |
| 5239 | } |
| 5240 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5241 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5242 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 5243 | /// set, using template argument deduction to produce an appropriate member |
| 5244 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5245 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5246 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5247 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5248 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5249 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5250 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5251 | Expr::Classification ObjectClassification, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5252 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5253 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5254 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5255 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 5256 | return; |
| 5257 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5258 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5259 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5260 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5261 | // 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] | 5262 | // candidate functions in the usual way.113) A given name can refer to one |
| 5263 | // or more function templates and also to a set of overloaded non-template |
| 5264 | // functions. In such a case, the candidate functions generated from each |
| 5265 | // function template are combined with the set of non-template candidate |
| 5266 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5267 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5268 | FunctionDecl *Specialization = 0; |
| 5269 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5270 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5271 | Args, NumArgs, Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5272 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5273 | Candidate.FoundDecl = FoundDecl; |
| 5274 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 5275 | Candidate.Viable = false; |
| 5276 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 5277 | Candidate.IsSurrogate = false; |
| 5278 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5279 | Candidate.ExplicitCallArguments = NumArgs; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5280 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5281 | Info); |
| 5282 | return; |
| 5283 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5284 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5285 | // Add the function template specialization produced by template argument |
| 5286 | // deduction as a candidate. |
| 5287 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5288 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5289 | "Specialization is not a member function?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5290 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5291 | ActingContext, ObjectType, ObjectClassification, |
| 5292 | Args, NumArgs, CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5293 | } |
| 5294 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5295 | /// \brief Add a C++ function template specialization as a candidate |
| 5296 | /// in the candidate set, using template argument deduction to produce |
| 5297 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5298 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5299 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5300 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5301 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5302 | Expr **Args, unsigned NumArgs, |
| 5303 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5304 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5305 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 5306 | return; |
| 5307 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5308 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5309 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5310 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5311 | // 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] | 5312 | // candidate functions in the usual way.113) A given name can refer to one |
| 5313 | // or more function templates and also to a set of overloaded non-template |
| 5314 | // functions. In such a case, the candidate functions generated from each |
| 5315 | // function template are combined with the set of non-template candidate |
| 5316 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5317 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5318 | FunctionDecl *Specialization = 0; |
| 5319 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5320 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 5321 | Args, NumArgs, Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5322 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5323 | Candidate.FoundDecl = FoundDecl; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5324 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 5325 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5326 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5327 | Candidate.IsSurrogate = false; |
| 5328 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5329 | Candidate.ExplicitCallArguments = NumArgs; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5330 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5331 | Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5332 | return; |
| 5333 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5334 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5335 | // Add the function template specialization produced by template argument |
| 5336 | // deduction as a candidate. |
| 5337 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5338 | AddOverloadCandidate(Specialization, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5339 | SuppressUserConversions); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5340 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5341 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5342 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5343 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5344 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5345 | /// 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] | 5346 | /// (which may or may not be the same type as the type that the |
| 5347 | /// conversion function produces). |
| 5348 | void |
| 5349 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5350 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5351 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5352 | Expr *From, QualType ToType, |
| 5353 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5354 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 5355 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5356 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5357 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 5358 | return; |
| 5359 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5360 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5361 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5362 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5363 | // Add this candidate |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5364 | OverloadCandidate &Candidate = CandidateSet.addCandidate(1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5365 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5366 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5367 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5368 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5369 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5370 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 5371 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5372 | Candidate.Viable = true; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5373 | Candidate.ExplicitCallArguments = 1; |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5374 | |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 5375 | // C++ [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5376 | // For conversion functions, the function is considered to be a member of |
| 5377 | // the class of the implicit implied object argument for the purpose of |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 5378 | // defining the type of the implicit object parameter. |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5379 | // |
| 5380 | // Determine the implicit conversion sequence for the implicit |
| 5381 | // object parameter. |
| 5382 | QualType ImplicitParamType = From->getType(); |
| 5383 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
| 5384 | ImplicitParamType = FromPtrType->getPointeeType(); |
| 5385 | CXXRecordDecl *ConversionContext |
| 5386 | = cast<CXXRecordDecl>(ImplicitParamType->getAs<RecordType>()->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5387 | |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5388 | Candidate.Conversions[0] |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5389 | = TryObjectArgumentInitialization(*this, From->getType(), |
| 5390 | From->Classify(Context), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5391 | Conversion, ConversionContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5392 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5393 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5394 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5395 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5396 | return; |
| 5397 | } |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5398 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5399 | // 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] | 5400 | // derived to base as such conversions are given Conversion Rank. They only |
| 5401 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 5402 | QualType FromCanon |
| 5403 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 5404 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 5405 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 5406 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5407 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 5408 | return; |
| 5409 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5410 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5411 | // To determine what the conversion from the result of calling the |
| 5412 | // conversion function to the type we're eventually trying to |
| 5413 | // convert to (ToType), we need to synthesize a call to the |
| 5414 | // conversion function and attempt copy initialization from it. This |
| 5415 | // makes sure that we get the right semantics with respect to |
| 5416 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 5417 | // call on the stack and we don't need its arguments to be |
| 5418 | // well-formed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5419 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5420 | VK_LValue, From->getLocStart()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5421 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 5422 | Context.getPointerType(Conversion->getType()), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5423 | CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5424 | &ConversionRef, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5425 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5426 | QualType ConversionType = Conversion->getConversionType(); |
| 5427 | if (RequireCompleteType(From->getLocStart(), ConversionType, 0)) { |
Douglas Gregor | 7d14d38 | 2010-11-13 19:36:57 +0000 | [diff] [blame] | 5428 | Candidate.Viable = false; |
| 5429 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 5430 | return; |
| 5431 | } |
| 5432 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5433 | ExprValueKind VK = Expr::getValueKindForType(ConversionType); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5434 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5435 | // 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] | 5436 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 5437 | // allocator). |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5438 | QualType CallResultType = ConversionType.getNonLValueExprType(Context); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5439 | CallExpr Call(Context, &ConversionFn, 0, 0, CallResultType, VK, |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 5440 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5441 | ImplicitConversionSequence ICS = |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5442 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 5443 | /*SuppressUserConversions=*/true, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5444 | /*InOverloadResolution=*/false, |
| 5445 | /*AllowObjCWritebackConversion=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5446 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5447 | switch (ICS.getKind()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5448 | case ImplicitConversionSequence::StandardConversion: |
| 5449 | Candidate.FinalConversion = ICS.Standard; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5450 | |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5451 | // C++ [over.ics.user]p3: |
| 5452 | // If the user-defined conversion is specified by a specialization of a |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5453 | // conversion function template, the second standard conversion sequence |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5454 | // shall have exact match rank. |
| 5455 | if (Conversion->getPrimaryTemplate() && |
| 5456 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 5457 | Candidate.Viable = false; |
| 5458 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 5459 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5460 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 5461 | // C++0x [dcl.init.ref]p5: |
| 5462 | // In the second case, if the reference is an rvalue reference and |
| 5463 | // the second standard conversion sequence of the user-defined |
| 5464 | // conversion sequence includes an lvalue-to-rvalue conversion, the |
| 5465 | // program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5466 | if (ToType->isRValueReferenceType() && |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 5467 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 5468 | Candidate.Viable = false; |
| 5469 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 5470 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5471 | break; |
| 5472 | |
| 5473 | case ImplicitConversionSequence::BadConversion: |
| 5474 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5475 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5476 | break; |
| 5477 | |
| 5478 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5479 | llvm_unreachable( |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5480 | "Can only end up with a standard conversion sequence or failure"); |
| 5481 | } |
| 5482 | } |
| 5483 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5484 | /// \brief Adds a conversion function template specialization |
| 5485 | /// candidate to the overload set, using template argument deduction |
| 5486 | /// to deduce the template arguments of the conversion function |
| 5487 | /// template from the type that we are converting to (C++ |
| 5488 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5489 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5490 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5491 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5492 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5493 | Expr *From, QualType ToType, |
| 5494 | OverloadCandidateSet &CandidateSet) { |
| 5495 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 5496 | "Only conversion function templates permitted here"); |
| 5497 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5498 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 5499 | return; |
| 5500 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5501 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5502 | CXXConversionDecl *Specialization = 0; |
| 5503 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5504 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5505 | Specialization, Info)) { |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5506 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5507 | Candidate.FoundDecl = FoundDecl; |
| 5508 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 5509 | Candidate.Viable = false; |
| 5510 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 5511 | Candidate.IsSurrogate = false; |
| 5512 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5513 | Candidate.ExplicitCallArguments = 1; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5514 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5515 | Info); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5516 | return; |
| 5517 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5518 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5519 | // Add the conversion function template specialization produced by |
| 5520 | // template argument deduction as a candidate. |
| 5521 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5522 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5523 | CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5524 | } |
| 5525 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5526 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 5527 | /// converts the given @c Object to a function pointer via the |
| 5528 | /// conversion function @c Conversion, and then attempts to call it |
| 5529 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 5530 | /// the type of function that we'll eventually be calling. |
| 5531 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5532 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5533 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5534 | const FunctionProtoType *Proto, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5535 | Expr *Object, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5536 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5537 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5538 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 5539 | return; |
| 5540 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5541 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5542 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5543 | |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5544 | OverloadCandidate &Candidate = CandidateSet.addCandidate(NumArgs + 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5545 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5546 | Candidate.Function = 0; |
| 5547 | Candidate.Surrogate = Conversion; |
| 5548 | Candidate.Viable = true; |
| 5549 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5550 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5551 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5552 | |
| 5553 | // Determine the implicit conversion sequence for the implicit |
| 5554 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5555 | ImplicitConversionSequence ObjectInit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5556 | = TryObjectArgumentInitialization(*this, Object->getType(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5557 | Object->Classify(Context), |
| 5558 | Conversion, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5559 | if (ObjectInit.isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5560 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5561 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5562 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5563 | return; |
| 5564 | } |
| 5565 | |
| 5566 | // The first conversion is actually a user-defined conversion whose |
| 5567 | // first conversion is ObjectInit's standard conversion (which is |
| 5568 | // effectively a reference binding). Record it as such. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5569 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5570 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 5571 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5572 | Candidate.Conversions[0].UserDefined.HadMultipleCandidates = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5573 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 5574 | Candidate.Conversions[0].UserDefined.FoundConversionFunction = FoundDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5575 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5576 | = Candidate.Conversions[0].UserDefined.Before; |
| 5577 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 5578 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5579 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5580 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5581 | |
| 5582 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5583 | // parameters is viable only if it has an ellipsis in its parameter |
| 5584 | // list (8.3.5). |
| 5585 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 5586 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5587 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5588 | return; |
| 5589 | } |
| 5590 | |
| 5591 | // Function types don't have any default arguments, so just check if |
| 5592 | // we have enough arguments. |
| 5593 | if (NumArgs < NumArgsInProto) { |
| 5594 | // Not enough arguments. |
| 5595 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5596 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5597 | return; |
| 5598 | } |
| 5599 | |
| 5600 | // Determine the implicit conversion sequences for each of the |
| 5601 | // arguments. |
| 5602 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 5603 | if (ArgIdx < NumArgsInProto) { |
| 5604 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5605 | // exist for each argument an implicit conversion sequence |
| 5606 | // (13.3.3.1) that converts that argument to the corresponding |
| 5607 | // parameter of F. |
| 5608 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5609 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5610 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 5611 | /*SuppressUserConversions=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5612 | /*InOverloadResolution=*/false, |
| 5613 | /*AllowObjCWritebackConversion=*/ |
| 5614 | getLangOptions().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5615 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5616 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5617 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5618 | break; |
| 5619 | } |
| 5620 | } else { |
| 5621 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5622 | // argument for which there is no corresponding parameter is |
| 5623 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5624 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5625 | } |
| 5626 | } |
| 5627 | } |
| 5628 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5629 | /// \brief Add overload candidates for overloaded operators that are |
| 5630 | /// member functions. |
| 5631 | /// |
| 5632 | /// Add the overloaded operator candidates that are member functions |
| 5633 | /// for the operator Op that was used in an operator expression such |
| 5634 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 5635 | /// CandidateSet will store the added overload candidates. (C++ |
| 5636 | /// [over.match.oper]). |
| 5637 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 5638 | SourceLocation OpLoc, |
| 5639 | Expr **Args, unsigned NumArgs, |
| 5640 | OverloadCandidateSet& CandidateSet, |
| 5641 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5642 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5643 | |
| 5644 | // C++ [over.match.oper]p3: |
| 5645 | // For a unary operator @ with an operand of a type whose |
| 5646 | // cv-unqualified version is T1, and for a binary operator @ with |
| 5647 | // a left operand of a type whose cv-unqualified version is T1 and |
| 5648 | // a right operand of a type whose cv-unqualified version is T2, |
| 5649 | // three sets of candidate functions, designated member |
| 5650 | // candidates, non-member candidates and built-in candidates, are |
| 5651 | // constructed as follows: |
| 5652 | QualType T1 = Args[0]->getType(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5653 | |
| 5654 | // -- If T1 is a class type, the set of member candidates is the |
| 5655 | // result of the qualified lookup of T1::operator@ |
| 5656 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 5657 | // empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5658 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 5659 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 5660 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 5661 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5662 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5663 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 5664 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 5665 | Operators.suppressDiagnostics(); |
| 5666 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5667 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 5668 | OperEnd = Operators.end(); |
| 5669 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5670 | ++Oper) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5671 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5672 | Args[0]->Classify(Context), Args + 1, NumArgs - 1, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5673 | CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5674 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5675 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5676 | } |
| 5677 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5678 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 5679 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 5680 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 5681 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 5682 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5683 | /// operator. NumContextualBoolArguments is the number of arguments |
| 5684 | /// (at the beginning of the argument list) that will be contextually |
| 5685 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5686 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5687 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 5688 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5689 | bool IsAssignmentOperator, |
| 5690 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5691 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5692 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5693 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5694 | // Add this candidate |
Benjamin Kramer | 0e6a16f | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5695 | OverloadCandidate &Candidate = CandidateSet.addCandidate(NumArgs); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5696 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5697 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 5698 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5699 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5700 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 5701 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 5702 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 5703 | |
| 5704 | // Determine the implicit conversion sequences for each of the |
| 5705 | // arguments. |
| 5706 | Candidate.Viable = true; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5707 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5708 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 5709 | // C++ [over.match.oper]p4: |
| 5710 | // For the built-in assignment operators, conversions of the |
| 5711 | // left operand are restricted as follows: |
| 5712 | // -- no temporaries are introduced to hold the left operand, and |
| 5713 | // -- no user-defined conversions are applied to the left |
| 5714 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5715 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 5716 | // |
| 5717 | // We block these conversions by turning off user-defined |
| 5718 | // conversions, since that is the only way that initialization of |
| 5719 | // a reference to a non-class type can occur from something that |
| 5720 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5721 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5722 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5723 | "Contextual conversion to bool requires bool type"); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5724 | Candidate.Conversions[ArgIdx] |
| 5725 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5726 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5727 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5728 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 5729 | ArgIdx == 0 && IsAssignmentOperator, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5730 | /*InOverloadResolution=*/false, |
| 5731 | /*AllowObjCWritebackConversion=*/ |
| 5732 | getLangOptions().ObjCAutoRefCount); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5733 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5734 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5735 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5736 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5737 | break; |
| 5738 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5739 | } |
| 5740 | } |
| 5741 | |
| 5742 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 5743 | /// candidate operator functions for built-in operators (C++ |
| 5744 | /// [over.built]). The types are separated into pointer types and |
| 5745 | /// enumeration types. |
| 5746 | class BuiltinCandidateTypeSet { |
| 5747 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 5748 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5749 | |
| 5750 | /// PointerTypes - The set of pointer types that will be used in the |
| 5751 | /// built-in candidates. |
| 5752 | TypeSet PointerTypes; |
| 5753 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5754 | /// MemberPointerTypes - The set of member pointer types that will be |
| 5755 | /// used in the built-in candidates. |
| 5756 | TypeSet MemberPointerTypes; |
| 5757 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5758 | /// EnumerationTypes - The set of enumeration types that will be |
| 5759 | /// used in the built-in candidates. |
| 5760 | TypeSet EnumerationTypes; |
| 5761 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5762 | /// \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] | 5763 | /// candidates. |
| 5764 | TypeSet VectorTypes; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5765 | |
| 5766 | /// \brief A flag indicating non-record types are viable candidates |
| 5767 | bool HasNonRecordTypes; |
| 5768 | |
| 5769 | /// \brief A flag indicating whether either arithmetic or enumeration types |
| 5770 | /// were present in the candidate set. |
| 5771 | bool HasArithmeticOrEnumeralTypes; |
| 5772 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 5773 | /// \brief A flag indicating whether the nullptr type was present in the |
| 5774 | /// candidate set. |
| 5775 | bool HasNullPtrType; |
| 5776 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 5777 | /// Sema - The semantic analysis instance where we are building the |
| 5778 | /// candidate type set. |
| 5779 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5780 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5781 | /// Context - The AST context in which we will build the type sets. |
| 5782 | ASTContext &Context; |
| 5783 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 5784 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 5785 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5786 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5787 | |
| 5788 | public: |
| 5789 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 5790 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5791 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5792 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5793 | : HasNonRecordTypes(false), |
| 5794 | HasArithmeticOrEnumeralTypes(false), |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 5795 | HasNullPtrType(false), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5796 | SemaRef(SemaRef), |
| 5797 | Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5798 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5799 | void AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5800 | SourceLocation Loc, |
| 5801 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5802 | bool AllowExplicitConversions, |
| 5803 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5804 | |
| 5805 | /// pointer_begin - First pointer type found; |
| 5806 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 5807 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5808 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5809 | iterator pointer_end() { return PointerTypes.end(); } |
| 5810 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5811 | /// member_pointer_begin - First member pointer type found; |
| 5812 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 5813 | |
| 5814 | /// member_pointer_end - Past the last member pointer type found; |
| 5815 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 5816 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5817 | /// enumeration_begin - First enumeration type found; |
| 5818 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 5819 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5820 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5821 | iterator enumeration_end() { return EnumerationTypes.end(); } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5822 | |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 5823 | iterator vector_begin() { return VectorTypes.begin(); } |
| 5824 | iterator vector_end() { return VectorTypes.end(); } |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5825 | |
| 5826 | bool hasNonRecordTypes() { return HasNonRecordTypes; } |
| 5827 | bool hasArithmeticOrEnumeralTypes() { return HasArithmeticOrEnumeralTypes; } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 5828 | bool hasNullPtrType() const { return HasNullPtrType; } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5829 | }; |
| 5830 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5831 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5832 | /// the set of pointer types along with any more-qualified variants of |
| 5833 | /// that type. For example, if @p Ty is "int const *", this routine |
| 5834 | /// will add "int const *", "int const volatile *", "int const |
| 5835 | /// restrict *", and "int const volatile restrict *" to the set of |
| 5836 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 5837 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5838 | /// |
| 5839 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5840 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5841 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 5842 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5843 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5844 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 5845 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5846 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5847 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 5848 | QualType PointeeTy; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5849 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 5850 | bool buildObjCPtr = false; |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 5851 | if (!PointerTy) { |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 5852 | if (const ObjCObjectPointerType *PTy = Ty->getAs<ObjCObjectPointerType>()) { |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 5853 | PointeeTy = PTy->getPointeeType(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 5854 | buildObjCPtr = true; |
| 5855 | } |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 5856 | else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5857 | llvm_unreachable("type was not a pointer type!"); |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 5858 | } |
| 5859 | else |
| 5860 | PointeeTy = PointerTy->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5861 | |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 5862 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 5863 | // (the qualifier would sink to the element type), and for another, the |
| 5864 | // only overload situation where it matters is subscript or pointer +- int, |
| 5865 | // and those shouldn't have qualifier variants anyway. |
| 5866 | if (PointeeTy->isArrayType()) |
| 5867 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5868 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 5869 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | d411b3f | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 5870 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 5871 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 5872 | bool hasRestrict = VisibleQuals.hasRestrict(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5873 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5874 | // Iterate through all strict supersets of BaseCVR. |
| 5875 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 5876 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 5877 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 5878 | // in the types. |
| 5879 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 5880 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5881 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 5882 | if (!buildObjCPtr) |
| 5883 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
| 5884 | else |
| 5885 | PointerTypes.insert(Context.getObjCObjectPointerType(QPointeeTy)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5886 | } |
| 5887 | |
| 5888 | return true; |
| 5889 | } |
| 5890 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5891 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 5892 | /// to the set of pointer types along with any more-qualified variants of |
| 5893 | /// that type. For example, if @p Ty is "int const *", this routine |
| 5894 | /// will add "int const *", "int const volatile *", "int const |
| 5895 | /// restrict *", and "int const volatile restrict *" to the set of |
| 5896 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 5897 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5898 | /// |
| 5899 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5900 | bool |
| 5901 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 5902 | QualType Ty) { |
| 5903 | // Insert this type. |
| 5904 | if (!MemberPointerTypes.insert(Ty)) |
| 5905 | return false; |
| 5906 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5907 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 5908 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5909 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5910 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 5911 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 5912 | // (the qualifier would sink to the element type), and for another, the |
| 5913 | // only overload situation where it matters is subscript or pointer +- int, |
| 5914 | // and those shouldn't have qualifier variants anyway. |
| 5915 | if (PointeeTy->isArrayType()) |
| 5916 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5917 | const Type *ClassTy = PointerTy->getClass(); |
| 5918 | |
| 5919 | // Iterate through all strict supersets of the pointee type's CVR |
| 5920 | // qualifiers. |
| 5921 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 5922 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 5923 | if ((CVR | BaseCVR) != CVR) continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5924 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5925 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 5926 | MemberPointerTypes.insert( |
| 5927 | Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5928 | } |
| 5929 | |
| 5930 | return true; |
| 5931 | } |
| 5932 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5933 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 5934 | /// 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] | 5935 | /// primarily interested in pointer types and enumeration types. We also |
| 5936 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5937 | /// AllowUserConversions is true if we should look at the conversion |
| 5938 | /// functions of a class type, and AllowExplicitConversions if we |
| 5939 | /// should also include the explicit conversion functions of a class |
| 5940 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5941 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5942 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5943 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5944 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5945 | bool AllowExplicitConversions, |
| 5946 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5947 | // Only deal with canonical types. |
| 5948 | Ty = Context.getCanonicalType(Ty); |
| 5949 | |
| 5950 | // Look through reference types; they aren't part of the type of an |
| 5951 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5952 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5953 | Ty = RefTy->getPointeeType(); |
| 5954 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5955 | // If we're dealing with an array type, decay to the pointer. |
| 5956 | if (Ty->isArrayType()) |
| 5957 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 5958 | |
| 5959 | // Otherwise, we don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5960 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5961 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5962 | // Flag if we ever add a non-record type. |
| 5963 | const RecordType *TyRec = Ty->getAs<RecordType>(); |
| 5964 | HasNonRecordTypes = HasNonRecordTypes || !TyRec; |
| 5965 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5966 | // Flag if we encounter an arithmetic type. |
| 5967 | HasArithmeticOrEnumeralTypes = |
| 5968 | HasArithmeticOrEnumeralTypes || Ty->isArithmeticType(); |
| 5969 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 5970 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
| 5971 | PointerTypes.insert(Ty); |
| 5972 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5973 | // Insert our type, and its more-qualified variants, into the set |
| 5974 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 5975 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5976 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5977 | } else if (Ty->isMemberPointerType()) { |
| 5978 | // Member pointers are far easier, since the pointee can't be converted. |
| 5979 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 5980 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5981 | } else if (Ty->isEnumeralType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5982 | HasArithmeticOrEnumeralTypes = true; |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 5983 | EnumerationTypes.insert(Ty); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 5984 | } else if (Ty->isVectorType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5985 | // We treat vector types as arithmetic types in many contexts as an |
| 5986 | // extension. |
| 5987 | HasArithmeticOrEnumeralTypes = true; |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 5988 | VectorTypes.insert(Ty); |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 5989 | } else if (Ty->isNullPtrType()) { |
| 5990 | HasNullPtrType = true; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5991 | } else if (AllowUserConversions && TyRec) { |
| 5992 | // No conversion functions in incomplete types. |
| 5993 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) |
| 5994 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5995 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5996 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| 5997 | const UnresolvedSetImpl *Conversions |
| 5998 | = ClassDecl->getVisibleConversionFunctions(); |
| 5999 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 6000 | E = Conversions->end(); I != E; ++I) { |
| 6001 | NamedDecl *D = I.getDecl(); |
| 6002 | if (isa<UsingShadowDecl>(D)) |
| 6003 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6004 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6005 | // Skip conversion function templates; they don't tell us anything |
| 6006 | // about which builtin types we can convert to. |
| 6007 | if (isa<FunctionTemplateDecl>(D)) |
| 6008 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6009 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6010 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
| 6011 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
| 6012 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
| 6013 | VisibleQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6014 | } |
| 6015 | } |
| 6016 | } |
| 6017 | } |
| 6018 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6019 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 6020 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 6021 | /// given type to the candidate set. |
| 6022 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 6023 | QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6024 | Expr **Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6025 | unsigned NumArgs, |
| 6026 | OverloadCandidateSet &CandidateSet) { |
| 6027 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6028 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6029 | // T& operator=(T&, T) |
| 6030 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 6031 | ParamTypes[1] = T; |
| 6032 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 6033 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6034 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6035 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 6036 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6037 | ParamTypes[0] |
| 6038 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6039 | ParamTypes[1] = T; |
| 6040 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6041 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6042 | } |
| 6043 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6044 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 6045 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 6046 | /// 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] | 6047 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 6048 | Qualifiers VRQuals; |
| 6049 | const RecordType *TyRec; |
| 6050 | if (const MemberPointerType *RHSMPType = |
| 6051 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6052 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6053 | else |
| 6054 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 6055 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6056 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6057 | VRQuals.addVolatile(); |
| 6058 | VRQuals.addRestrict(); |
| 6059 | return VRQuals; |
| 6060 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6061 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6062 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 6063 | if (!ClassDecl->hasDefinition()) |
| 6064 | return VRQuals; |
| 6065 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6066 | const UnresolvedSetImpl *Conversions = |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 6067 | ClassDecl->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6068 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6069 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6070 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 6071 | NamedDecl *D = I.getDecl(); |
| 6072 | if (isa<UsingShadowDecl>(D)) |
| 6073 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6074 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6075 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 6076 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 6077 | CanTy = ResTypeRef->getPointeeType(); |
| 6078 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 6079 | // as see them. |
| 6080 | bool done = false; |
| 6081 | while (!done) { |
| 6082 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 6083 | CanTy = ResTypePtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6084 | else if (const MemberPointerType *ResTypeMPtr = |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6085 | CanTy->getAs<MemberPointerType>()) |
| 6086 | CanTy = ResTypeMPtr->getPointeeType(); |
| 6087 | else |
| 6088 | done = true; |
| 6089 | if (CanTy.isVolatileQualified()) |
| 6090 | VRQuals.addVolatile(); |
| 6091 | if (CanTy.isRestrictQualified()) |
| 6092 | VRQuals.addRestrict(); |
| 6093 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 6094 | return VRQuals; |
| 6095 | } |
| 6096 | } |
| 6097 | } |
| 6098 | return VRQuals; |
| 6099 | } |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6100 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6101 | namespace { |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6102 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6103 | /// \brief Helper class to manage the addition of builtin operator overload |
| 6104 | /// candidates. It provides shared state and utility methods used throughout |
| 6105 | /// the process, as well as a helper method to add each group of builtin |
| 6106 | /// operator overloads from the standard to a candidate set. |
| 6107 | class BuiltinOperatorOverloadBuilder { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6108 | // Common instance state available to all overload candidate addition methods. |
| 6109 | Sema &S; |
| 6110 | Expr **Args; |
| 6111 | unsigned NumArgs; |
| 6112 | Qualifiers VisibleTypeConversionsQuals; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6113 | bool HasArithmeticOrEnumeralCandidateType; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6114 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6115 | OverloadCandidateSet &CandidateSet; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6116 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6117 | // Define some constants used to index and iterate over the arithemetic types |
| 6118 | // provided via the getArithmeticType() method below. |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6119 | // The "promoted arithmetic types" are the arithmetic |
| 6120 | // types are that preserved by promotion (C++ [over.built]p2). |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6121 | static const unsigned FirstIntegralType = 3; |
| 6122 | static const unsigned LastIntegralType = 18; |
| 6123 | static const unsigned FirstPromotedIntegralType = 3, |
| 6124 | LastPromotedIntegralType = 9; |
| 6125 | static const unsigned FirstPromotedArithmeticType = 0, |
| 6126 | LastPromotedArithmeticType = 9; |
| 6127 | static const unsigned NumArithmeticTypes = 18; |
| 6128 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6129 | /// \brief Get the canonical type for a given arithmetic type index. |
| 6130 | CanQualType getArithmeticType(unsigned index) { |
| 6131 | assert(index < NumArithmeticTypes); |
| 6132 | static CanQualType ASTContext::* const |
| 6133 | ArithmeticTypes[NumArithmeticTypes] = { |
| 6134 | // Start of promoted types. |
| 6135 | &ASTContext::FloatTy, |
| 6136 | &ASTContext::DoubleTy, |
| 6137 | &ASTContext::LongDoubleTy, |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6138 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6139 | // Start of integral types. |
| 6140 | &ASTContext::IntTy, |
| 6141 | &ASTContext::LongTy, |
| 6142 | &ASTContext::LongLongTy, |
| 6143 | &ASTContext::UnsignedIntTy, |
| 6144 | &ASTContext::UnsignedLongTy, |
| 6145 | &ASTContext::UnsignedLongLongTy, |
| 6146 | // End of promoted types. |
| 6147 | |
| 6148 | &ASTContext::BoolTy, |
| 6149 | &ASTContext::CharTy, |
| 6150 | &ASTContext::WCharTy, |
| 6151 | &ASTContext::Char16Ty, |
| 6152 | &ASTContext::Char32Ty, |
| 6153 | &ASTContext::SignedCharTy, |
| 6154 | &ASTContext::ShortTy, |
| 6155 | &ASTContext::UnsignedCharTy, |
| 6156 | &ASTContext::UnsignedShortTy, |
| 6157 | // End of integral types. |
| 6158 | // FIXME: What about complex? |
| 6159 | }; |
| 6160 | return S.Context.*ArithmeticTypes[index]; |
| 6161 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6162 | |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6163 | /// \brief Gets the canonical type resulting from the usual arithemetic |
| 6164 | /// converions for the given arithmetic types. |
| 6165 | CanQualType getUsualArithmeticConversions(unsigned L, unsigned R) { |
| 6166 | // Accelerator table for performing the usual arithmetic conversions. |
| 6167 | // The rules are basically: |
| 6168 | // - if either is floating-point, use the wider floating-point |
| 6169 | // - if same signedness, use the higher rank |
| 6170 | // - if same size, use unsigned of the higher rank |
| 6171 | // - use the larger type |
| 6172 | // These rules, together with the axiom that higher ranks are |
| 6173 | // never smaller, are sufficient to precompute all of these results |
| 6174 | // *except* when dealing with signed types of higher rank. |
| 6175 | // (we could precompute SLL x UI for all known platforms, but it's |
| 6176 | // better not to make any assumptions). |
| 6177 | enum PromotedType { |
| 6178 | Flt, Dbl, LDbl, SI, SL, SLL, UI, UL, ULL, Dep=-1 |
| 6179 | }; |
| 6180 | static PromotedType ConversionsTable[LastPromotedArithmeticType] |
| 6181 | [LastPromotedArithmeticType] = { |
| 6182 | /* Flt*/ { Flt, Dbl, LDbl, Flt, Flt, Flt, Flt, Flt, Flt }, |
| 6183 | /* Dbl*/ { Dbl, Dbl, LDbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl }, |
| 6184 | /*LDbl*/ { LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl }, |
| 6185 | /* SI*/ { Flt, Dbl, LDbl, SI, SL, SLL, UI, UL, ULL }, |
| 6186 | /* SL*/ { Flt, Dbl, LDbl, SL, SL, SLL, Dep, UL, ULL }, |
| 6187 | /* SLL*/ { Flt, Dbl, LDbl, SLL, SLL, SLL, Dep, Dep, ULL }, |
| 6188 | /* UI*/ { Flt, Dbl, LDbl, UI, Dep, Dep, UI, UL, ULL }, |
| 6189 | /* UL*/ { Flt, Dbl, LDbl, UL, UL, Dep, UL, UL, ULL }, |
| 6190 | /* ULL*/ { Flt, Dbl, LDbl, ULL, ULL, ULL, ULL, ULL, ULL }, |
| 6191 | }; |
| 6192 | |
| 6193 | assert(L < LastPromotedArithmeticType); |
| 6194 | assert(R < LastPromotedArithmeticType); |
| 6195 | int Idx = ConversionsTable[L][R]; |
| 6196 | |
| 6197 | // Fast path: the table gives us a concrete answer. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6198 | if (Idx != Dep) return getArithmeticType(Idx); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6199 | |
| 6200 | // Slow path: we need to compare widths. |
| 6201 | // An invariant is that the signed type has higher rank. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6202 | CanQualType LT = getArithmeticType(L), |
| 6203 | RT = getArithmeticType(R); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6204 | unsigned LW = S.Context.getIntWidth(LT), |
| 6205 | RW = S.Context.getIntWidth(RT); |
| 6206 | |
| 6207 | // If they're different widths, use the signed type. |
| 6208 | if (LW > RW) return LT; |
| 6209 | else if (LW < RW) return RT; |
| 6210 | |
| 6211 | // Otherwise, use the unsigned type of the signed type's rank. |
| 6212 | if (L == SL || R == SL) return S.Context.UnsignedLongTy; |
| 6213 | assert(L == SLL || R == SLL); |
| 6214 | return S.Context.UnsignedLongLongTy; |
| 6215 | } |
| 6216 | |
Chandler Carruth | 3c69dc4 | 2010-12-12 09:22:45 +0000 | [diff] [blame] | 6217 | /// \brief Helper method to factor out the common pattern of adding overloads |
| 6218 | /// for '++' and '--' builtin operators. |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6219 | void addPlusPlusMinusMinusStyleOverloads(QualType CandidateTy, |
| 6220 | bool HasVolatile) { |
| 6221 | QualType ParamTypes[2] = { |
| 6222 | S.Context.getLValueReferenceType(CandidateTy), |
| 6223 | S.Context.IntTy |
| 6224 | }; |
| 6225 | |
| 6226 | // Non-volatile version. |
| 6227 | if (NumArgs == 1) |
| 6228 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 6229 | else |
| 6230 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 6231 | |
| 6232 | // Use a heuristic to reduce number of builtin candidates in the set: |
| 6233 | // add volatile version only if there are conversions to a volatile type. |
| 6234 | if (HasVolatile) { |
| 6235 | ParamTypes[0] = |
| 6236 | S.Context.getLValueReferenceType( |
| 6237 | S.Context.getVolatileType(CandidateTy)); |
| 6238 | if (NumArgs == 1) |
| 6239 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 6240 | else |
| 6241 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 6242 | } |
| 6243 | } |
| 6244 | |
| 6245 | public: |
| 6246 | BuiltinOperatorOverloadBuilder( |
| 6247 | Sema &S, Expr **Args, unsigned NumArgs, |
| 6248 | Qualifiers VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6249 | bool HasArithmeticOrEnumeralCandidateType, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6250 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6251 | OverloadCandidateSet &CandidateSet) |
| 6252 | : S(S), Args(Args), NumArgs(NumArgs), |
| 6253 | VisibleTypeConversionsQuals(VisibleTypeConversionsQuals), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6254 | HasArithmeticOrEnumeralCandidateType( |
| 6255 | HasArithmeticOrEnumeralCandidateType), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6256 | CandidateTypes(CandidateTypes), |
| 6257 | CandidateSet(CandidateSet) { |
| 6258 | // Validate some of our static helper constants in debug builds. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6259 | assert(getArithmeticType(FirstPromotedIntegralType) == S.Context.IntTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6260 | "Invalid first promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6261 | assert(getArithmeticType(LastPromotedIntegralType - 1) |
| 6262 | == S.Context.UnsignedLongLongTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6263 | "Invalid last promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6264 | assert(getArithmeticType(FirstPromotedArithmeticType) |
| 6265 | == S.Context.FloatTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6266 | "Invalid first promoted arithmetic type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6267 | assert(getArithmeticType(LastPromotedArithmeticType - 1) |
| 6268 | == S.Context.UnsignedLongLongTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6269 | "Invalid last promoted arithmetic type"); |
| 6270 | } |
| 6271 | |
| 6272 | // C++ [over.built]p3: |
| 6273 | // |
| 6274 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 6275 | // is either volatile or empty, there exist candidate operator |
| 6276 | // functions of the form |
| 6277 | // |
| 6278 | // VQ T& operator++(VQ T&); |
| 6279 | // T operator++(VQ T&, int); |
| 6280 | // |
| 6281 | // C++ [over.built]p4: |
| 6282 | // |
| 6283 | // For every pair (T, VQ), where T is an arithmetic type other |
| 6284 | // than bool, and VQ is either volatile or empty, there exist |
| 6285 | // candidate operator functions of the form |
| 6286 | // |
| 6287 | // VQ T& operator--(VQ T&); |
| 6288 | // T operator--(VQ T&, int); |
| 6289 | void addPlusPlusMinusMinusArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6290 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6291 | return; |
| 6292 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6293 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 6294 | Arith < NumArithmeticTypes; ++Arith) { |
| 6295 | addPlusPlusMinusMinusStyleOverloads( |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6296 | getArithmeticType(Arith), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6297 | VisibleTypeConversionsQuals.hasVolatile()); |
| 6298 | } |
| 6299 | } |
| 6300 | |
| 6301 | // C++ [over.built]p5: |
| 6302 | // |
| 6303 | // For every pair (T, VQ), where T is a cv-qualified or |
| 6304 | // cv-unqualified object type, and VQ is either volatile or |
| 6305 | // empty, there exist candidate operator functions of the form |
| 6306 | // |
| 6307 | // T*VQ& operator++(T*VQ&); |
| 6308 | // T*VQ& operator--(T*VQ&); |
| 6309 | // T* operator++(T*VQ&, int); |
| 6310 | // T* operator--(T*VQ&, int); |
| 6311 | void addPlusPlusMinusMinusPointerOverloads() { |
| 6312 | for (BuiltinCandidateTypeSet::iterator |
| 6313 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6314 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6315 | Ptr != PtrEnd; ++Ptr) { |
| 6316 | // Skip pointer types that aren't pointers to object types. |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6317 | if (!(*Ptr)->getPointeeType()->isObjectType()) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6318 | continue; |
| 6319 | |
| 6320 | addPlusPlusMinusMinusStyleOverloads(*Ptr, |
| 6321 | (!S.Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 6322 | VisibleTypeConversionsQuals.hasVolatile())); |
| 6323 | } |
| 6324 | } |
| 6325 | |
| 6326 | // C++ [over.built]p6: |
| 6327 | // For every cv-qualified or cv-unqualified object type T, there |
| 6328 | // exist candidate operator functions of the form |
| 6329 | // |
| 6330 | // T& operator*(T*); |
| 6331 | // |
| 6332 | // C++ [over.built]p7: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6333 | // 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] | 6334 | // ref-qualifier, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6335 | // T& operator*(T*); |
| 6336 | void addUnaryStarPointerOverloads() { |
| 6337 | for (BuiltinCandidateTypeSet::iterator |
| 6338 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6339 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6340 | Ptr != PtrEnd; ++Ptr) { |
| 6341 | QualType ParamTy = *Ptr; |
| 6342 | QualType PointeeTy = ParamTy->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6343 | if (!PointeeTy->isObjectType() && !PointeeTy->isFunctionType()) |
| 6344 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6345 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6346 | if (const FunctionProtoType *Proto =PointeeTy->getAs<FunctionProtoType>()) |
| 6347 | if (Proto->getTypeQuals() || Proto->getRefQualifier()) |
| 6348 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6349 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6350 | S.AddBuiltinCandidate(S.Context.getLValueReferenceType(PointeeTy), |
| 6351 | &ParamTy, Args, 1, CandidateSet); |
| 6352 | } |
| 6353 | } |
| 6354 | |
| 6355 | // C++ [over.built]p9: |
| 6356 | // For every promoted arithmetic type T, there exist candidate |
| 6357 | // operator functions of the form |
| 6358 | // |
| 6359 | // T operator+(T); |
| 6360 | // T operator-(T); |
| 6361 | void addUnaryPlusOrMinusArithmeticOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6362 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6363 | return; |
| 6364 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6365 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 6366 | Arith < LastPromotedArithmeticType; ++Arith) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6367 | QualType ArithTy = getArithmeticType(Arith); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6368 | S.AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 6369 | } |
| 6370 | |
| 6371 | // Extension: We also add these operators for vector types. |
| 6372 | for (BuiltinCandidateTypeSet::iterator |
| 6373 | Vec = CandidateTypes[0].vector_begin(), |
| 6374 | VecEnd = CandidateTypes[0].vector_end(); |
| 6375 | Vec != VecEnd; ++Vec) { |
| 6376 | QualType VecTy = *Vec; |
| 6377 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 6378 | } |
| 6379 | } |
| 6380 | |
| 6381 | // C++ [over.built]p8: |
| 6382 | // For every type T, there exist candidate operator functions of |
| 6383 | // the form |
| 6384 | // |
| 6385 | // T* operator+(T*); |
| 6386 | void addUnaryPlusPointerOverloads() { |
| 6387 | for (BuiltinCandidateTypeSet::iterator |
| 6388 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6389 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6390 | Ptr != PtrEnd; ++Ptr) { |
| 6391 | QualType ParamTy = *Ptr; |
| 6392 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 6393 | } |
| 6394 | } |
| 6395 | |
| 6396 | // C++ [over.built]p10: |
| 6397 | // For every promoted integral type T, there exist candidate |
| 6398 | // operator functions of the form |
| 6399 | // |
| 6400 | // T operator~(T); |
| 6401 | void addUnaryTildePromotedIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6402 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6403 | return; |
| 6404 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6405 | for (unsigned Int = FirstPromotedIntegralType; |
| 6406 | Int < LastPromotedIntegralType; ++Int) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6407 | QualType IntTy = getArithmeticType(Int); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6408 | S.AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 6409 | } |
| 6410 | |
| 6411 | // Extension: We also add this operator for vector types. |
| 6412 | for (BuiltinCandidateTypeSet::iterator |
| 6413 | Vec = CandidateTypes[0].vector_begin(), |
| 6414 | VecEnd = CandidateTypes[0].vector_end(); |
| 6415 | Vec != VecEnd; ++Vec) { |
| 6416 | QualType VecTy = *Vec; |
| 6417 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 6418 | } |
| 6419 | } |
| 6420 | |
| 6421 | // C++ [over.match.oper]p16: |
| 6422 | // For every pointer to member type T, there exist candidate operator |
| 6423 | // functions of the form |
| 6424 | // |
| 6425 | // bool operator==(T,T); |
| 6426 | // bool operator!=(T,T); |
| 6427 | void addEqualEqualOrNotEqualMemberPointerOverloads() { |
| 6428 | /// Set of (canonical) types that we've already handled. |
| 6429 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6430 | |
| 6431 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 6432 | for (BuiltinCandidateTypeSet::iterator |
| 6433 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 6434 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 6435 | MemPtr != MemPtrEnd; |
| 6436 | ++MemPtr) { |
| 6437 | // Don't add the same builtin candidate twice. |
| 6438 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 6439 | continue; |
| 6440 | |
| 6441 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 6442 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 6443 | CandidateSet); |
| 6444 | } |
| 6445 | } |
| 6446 | } |
| 6447 | |
| 6448 | // C++ [over.built]p15: |
| 6449 | // |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6450 | // For every T, where T is an enumeration type, a pointer type, or |
| 6451 | // std::nullptr_t, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6452 | // |
| 6453 | // bool operator<(T, T); |
| 6454 | // bool operator>(T, T); |
| 6455 | // bool operator<=(T, T); |
| 6456 | // bool operator>=(T, T); |
| 6457 | // bool operator==(T, T); |
| 6458 | // bool operator!=(T, T); |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6459 | void addRelationalPointerOrEnumeralOverloads() { |
| 6460 | // C++ [over.built]p1: |
| 6461 | // If there is a user-written candidate with the same name and parameter |
| 6462 | // types as a built-in candidate operator function, the built-in operator |
| 6463 | // function is hidden and is not included in the set of candidate |
| 6464 | // functions. |
| 6465 | // |
| 6466 | // The text is actually in a note, but if we don't implement it then we end |
| 6467 | // up with ambiguities when the user provides an overloaded operator for |
| 6468 | // an enumeration type. Note that only enumeration types have this problem, |
| 6469 | // so we track which enumeration types we've seen operators for. Also, the |
| 6470 | // only other overloaded operator with enumeration argumenst, operator=, |
| 6471 | // cannot be overloaded for enumeration types, so this is the only place |
| 6472 | // where we must suppress candidates like this. |
| 6473 | llvm::DenseSet<std::pair<CanQualType, CanQualType> > |
| 6474 | UserDefinedBinaryOperators; |
| 6475 | |
| 6476 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 6477 | if (CandidateTypes[ArgIdx].enumeration_begin() != |
| 6478 | CandidateTypes[ArgIdx].enumeration_end()) { |
| 6479 | for (OverloadCandidateSet::iterator C = CandidateSet.begin(), |
| 6480 | CEnd = CandidateSet.end(); |
| 6481 | C != CEnd; ++C) { |
| 6482 | if (!C->Viable || !C->Function || C->Function->getNumParams() != 2) |
| 6483 | continue; |
| 6484 | |
| 6485 | QualType FirstParamType = |
| 6486 | C->Function->getParamDecl(0)->getType().getUnqualifiedType(); |
| 6487 | QualType SecondParamType = |
| 6488 | C->Function->getParamDecl(1)->getType().getUnqualifiedType(); |
| 6489 | |
| 6490 | // Skip if either parameter isn't of enumeral type. |
| 6491 | if (!FirstParamType->isEnumeralType() || |
| 6492 | !SecondParamType->isEnumeralType()) |
| 6493 | continue; |
| 6494 | |
| 6495 | // Add this operator to the set of known user-defined operators. |
| 6496 | UserDefinedBinaryOperators.insert( |
| 6497 | std::make_pair(S.Context.getCanonicalType(FirstParamType), |
| 6498 | S.Context.getCanonicalType(SecondParamType))); |
| 6499 | } |
| 6500 | } |
| 6501 | } |
| 6502 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6503 | /// Set of (canonical) types that we've already handled. |
| 6504 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6505 | |
| 6506 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 6507 | for (BuiltinCandidateTypeSet::iterator |
| 6508 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 6509 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 6510 | Ptr != PtrEnd; ++Ptr) { |
| 6511 | // Don't add the same builtin candidate twice. |
| 6512 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 6513 | continue; |
| 6514 | |
| 6515 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 6516 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 6517 | CandidateSet); |
| 6518 | } |
| 6519 | for (BuiltinCandidateTypeSet::iterator |
| 6520 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 6521 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 6522 | Enum != EnumEnd; ++Enum) { |
| 6523 | CanQualType CanonType = S.Context.getCanonicalType(*Enum); |
| 6524 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6525 | // Don't add the same builtin candidate twice, or if a user defined |
| 6526 | // candidate exists. |
| 6527 | if (!AddedTypes.insert(CanonType) || |
| 6528 | UserDefinedBinaryOperators.count(std::make_pair(CanonType, |
| 6529 | CanonType))) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6530 | continue; |
| 6531 | |
| 6532 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6533 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 6534 | CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6535 | } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6536 | |
| 6537 | if (CandidateTypes[ArgIdx].hasNullPtrType()) { |
| 6538 | CanQualType NullPtrTy = S.Context.getCanonicalType(S.Context.NullPtrTy); |
| 6539 | if (AddedTypes.insert(NullPtrTy) && |
| 6540 | !UserDefinedBinaryOperators.count(std::make_pair(NullPtrTy, |
| 6541 | NullPtrTy))) { |
| 6542 | QualType ParamTypes[2] = { NullPtrTy, NullPtrTy }; |
| 6543 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 6544 | CandidateSet); |
| 6545 | } |
| 6546 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6547 | } |
| 6548 | } |
| 6549 | |
| 6550 | // C++ [over.built]p13: |
| 6551 | // |
| 6552 | // For every cv-qualified or cv-unqualified object type T |
| 6553 | // there exist candidate operator functions of the form |
| 6554 | // |
| 6555 | // T* operator+(T*, ptrdiff_t); |
| 6556 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 6557 | // T* operator-(T*, ptrdiff_t); |
| 6558 | // T* operator+(ptrdiff_t, T*); |
| 6559 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 6560 | // |
| 6561 | // C++ [over.built]p14: |
| 6562 | // |
| 6563 | // For every T, where T is a pointer to object type, there |
| 6564 | // exist candidate operator functions of the form |
| 6565 | // |
| 6566 | // ptrdiff_t operator-(T, T); |
| 6567 | void addBinaryPlusOrMinusPointerOverloads(OverloadedOperatorKind Op) { |
| 6568 | /// Set of (canonical) types that we've already handled. |
| 6569 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6570 | |
| 6571 | for (int Arg = 0; Arg < 2; ++Arg) { |
| 6572 | QualType AsymetricParamTypes[2] = { |
| 6573 | S.Context.getPointerDiffType(), |
| 6574 | S.Context.getPointerDiffType(), |
| 6575 | }; |
| 6576 | for (BuiltinCandidateTypeSet::iterator |
| 6577 | Ptr = CandidateTypes[Arg].pointer_begin(), |
| 6578 | PtrEnd = CandidateTypes[Arg].pointer_end(); |
| 6579 | Ptr != PtrEnd; ++Ptr) { |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6580 | QualType PointeeTy = (*Ptr)->getPointeeType(); |
| 6581 | if (!PointeeTy->isObjectType()) |
| 6582 | continue; |
| 6583 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6584 | AsymetricParamTypes[Arg] = *Ptr; |
| 6585 | if (Arg == 0 || Op == OO_Plus) { |
| 6586 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 6587 | // T* operator+(ptrdiff_t, T*); |
| 6588 | S.AddBuiltinCandidate(*Ptr, AsymetricParamTypes, Args, 2, |
| 6589 | CandidateSet); |
| 6590 | } |
| 6591 | if (Op == OO_Minus) { |
| 6592 | // ptrdiff_t operator-(T, T); |
| 6593 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 6594 | continue; |
| 6595 | |
| 6596 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 6597 | S.AddBuiltinCandidate(S.Context.getPointerDiffType(), ParamTypes, |
| 6598 | Args, 2, CandidateSet); |
| 6599 | } |
| 6600 | } |
| 6601 | } |
| 6602 | } |
| 6603 | |
| 6604 | // C++ [over.built]p12: |
| 6605 | // |
| 6606 | // For every pair of promoted arithmetic types L and R, there |
| 6607 | // exist candidate operator functions of the form |
| 6608 | // |
| 6609 | // LR operator*(L, R); |
| 6610 | // LR operator/(L, R); |
| 6611 | // LR operator+(L, R); |
| 6612 | // LR operator-(L, R); |
| 6613 | // bool operator<(L, R); |
| 6614 | // bool operator>(L, R); |
| 6615 | // bool operator<=(L, R); |
| 6616 | // bool operator>=(L, R); |
| 6617 | // bool operator==(L, R); |
| 6618 | // bool operator!=(L, R); |
| 6619 | // |
| 6620 | // where LR is the result of the usual arithmetic conversions |
| 6621 | // between types L and R. |
| 6622 | // |
| 6623 | // C++ [over.built]p24: |
| 6624 | // |
| 6625 | // For every pair of promoted arithmetic types L and R, there exist |
| 6626 | // candidate operator functions of the form |
| 6627 | // |
| 6628 | // LR operator?(bool, L, R); |
| 6629 | // |
| 6630 | // where LR is the result of the usual arithmetic conversions |
| 6631 | // between types L and R. |
| 6632 | // Our candidates ignore the first parameter. |
| 6633 | void addGenericBinaryArithmeticOverloads(bool isComparison) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6634 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6635 | return; |
| 6636 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6637 | for (unsigned Left = FirstPromotedArithmeticType; |
| 6638 | Left < LastPromotedArithmeticType; ++Left) { |
| 6639 | for (unsigned Right = FirstPromotedArithmeticType; |
| 6640 | Right < LastPromotedArithmeticType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6641 | QualType LandR[2] = { getArithmeticType(Left), |
| 6642 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6643 | QualType Result = |
| 6644 | isComparison ? S.Context.BoolTy |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6645 | : getUsualArithmeticConversions(Left, Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6646 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 6647 | } |
| 6648 | } |
| 6649 | |
| 6650 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 6651 | // conditional operator for vector types. |
| 6652 | for (BuiltinCandidateTypeSet::iterator |
| 6653 | Vec1 = CandidateTypes[0].vector_begin(), |
| 6654 | Vec1End = CandidateTypes[0].vector_end(); |
| 6655 | Vec1 != Vec1End; ++Vec1) { |
| 6656 | for (BuiltinCandidateTypeSet::iterator |
| 6657 | Vec2 = CandidateTypes[1].vector_begin(), |
| 6658 | Vec2End = CandidateTypes[1].vector_end(); |
| 6659 | Vec2 != Vec2End; ++Vec2) { |
| 6660 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 6661 | QualType Result = S.Context.BoolTy; |
| 6662 | if (!isComparison) { |
| 6663 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 6664 | Result = *Vec1; |
| 6665 | else |
| 6666 | Result = *Vec2; |
| 6667 | } |
| 6668 | |
| 6669 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 6670 | } |
| 6671 | } |
| 6672 | } |
| 6673 | |
| 6674 | // C++ [over.built]p17: |
| 6675 | // |
| 6676 | // For every pair of promoted integral types L and R, there |
| 6677 | // exist candidate operator functions of the form |
| 6678 | // |
| 6679 | // LR operator%(L, R); |
| 6680 | // LR operator&(L, R); |
| 6681 | // LR operator^(L, R); |
| 6682 | // LR operator|(L, R); |
| 6683 | // L operator<<(L, R); |
| 6684 | // L operator>>(L, R); |
| 6685 | // |
| 6686 | // where LR is the result of the usual arithmetic conversions |
| 6687 | // between types L and R. |
| 6688 | void addBinaryBitwiseArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6689 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6690 | return; |
| 6691 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6692 | for (unsigned Left = FirstPromotedIntegralType; |
| 6693 | Left < LastPromotedIntegralType; ++Left) { |
| 6694 | for (unsigned Right = FirstPromotedIntegralType; |
| 6695 | Right < LastPromotedIntegralType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6696 | QualType LandR[2] = { getArithmeticType(Left), |
| 6697 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6698 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 6699 | ? LandR[0] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6700 | : getUsualArithmeticConversions(Left, Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6701 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 6702 | } |
| 6703 | } |
| 6704 | } |
| 6705 | |
| 6706 | // C++ [over.built]p20: |
| 6707 | // |
| 6708 | // For every pair (T, VQ), where T is an enumeration or |
| 6709 | // pointer to member type and VQ is either volatile or |
| 6710 | // empty, there exist candidate operator functions of the form |
| 6711 | // |
| 6712 | // VQ T& operator=(VQ T&, T); |
| 6713 | void addAssignmentMemberPointerOrEnumeralOverloads() { |
| 6714 | /// Set of (canonical) types that we've already handled. |
| 6715 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6716 | |
| 6717 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 6718 | for (BuiltinCandidateTypeSet::iterator |
| 6719 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 6720 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 6721 | Enum != EnumEnd; ++Enum) { |
| 6722 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 6723 | continue; |
| 6724 | |
| 6725 | AddBuiltinAssignmentOperatorCandidates(S, *Enum, Args, 2, |
| 6726 | CandidateSet); |
| 6727 | } |
| 6728 | |
| 6729 | for (BuiltinCandidateTypeSet::iterator |
| 6730 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 6731 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 6732 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 6733 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 6734 | continue; |
| 6735 | |
| 6736 | AddBuiltinAssignmentOperatorCandidates(S, *MemPtr, Args, 2, |
| 6737 | CandidateSet); |
| 6738 | } |
| 6739 | } |
| 6740 | } |
| 6741 | |
| 6742 | // C++ [over.built]p19: |
| 6743 | // |
| 6744 | // For every pair (T, VQ), where T is any type and VQ is either |
| 6745 | // volatile or empty, there exist candidate operator functions |
| 6746 | // of the form |
| 6747 | // |
| 6748 | // T*VQ& operator=(T*VQ&, T*); |
| 6749 | // |
| 6750 | // C++ [over.built]p21: |
| 6751 | // |
| 6752 | // For every pair (T, VQ), where T is a cv-qualified or |
| 6753 | // cv-unqualified object type and VQ is either volatile or |
| 6754 | // empty, there exist candidate operator functions of the form |
| 6755 | // |
| 6756 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 6757 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 6758 | void addAssignmentPointerOverloads(bool isEqualOp) { |
| 6759 | /// Set of (canonical) types that we've already handled. |
| 6760 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6761 | |
| 6762 | for (BuiltinCandidateTypeSet::iterator |
| 6763 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6764 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6765 | Ptr != PtrEnd; ++Ptr) { |
| 6766 | // If this is operator=, keep track of the builtin candidates we added. |
| 6767 | if (isEqualOp) |
| 6768 | AddedTypes.insert(S.Context.getCanonicalType(*Ptr)); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6769 | else if (!(*Ptr)->getPointeeType()->isObjectType()) |
| 6770 | continue; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6771 | |
| 6772 | // non-volatile version |
| 6773 | QualType ParamTypes[2] = { |
| 6774 | S.Context.getLValueReferenceType(*Ptr), |
| 6775 | isEqualOp ? *Ptr : S.Context.getPointerDiffType(), |
| 6776 | }; |
| 6777 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 6778 | /*IsAssigmentOperator=*/ isEqualOp); |
| 6779 | |
| 6780 | if (!S.Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 6781 | VisibleTypeConversionsQuals.hasVolatile()) { |
| 6782 | // volatile version |
| 6783 | ParamTypes[0] = |
| 6784 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
| 6785 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 6786 | /*IsAssigmentOperator=*/isEqualOp); |
| 6787 | } |
| 6788 | } |
| 6789 | |
| 6790 | if (isEqualOp) { |
| 6791 | for (BuiltinCandidateTypeSet::iterator |
| 6792 | Ptr = CandidateTypes[1].pointer_begin(), |
| 6793 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 6794 | Ptr != PtrEnd; ++Ptr) { |
| 6795 | // Make sure we don't add the same candidate twice. |
| 6796 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 6797 | continue; |
| 6798 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6799 | QualType ParamTypes[2] = { |
| 6800 | S.Context.getLValueReferenceType(*Ptr), |
| 6801 | *Ptr, |
| 6802 | }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6803 | |
| 6804 | // non-volatile version |
| 6805 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 6806 | /*IsAssigmentOperator=*/true); |
| 6807 | |
| 6808 | if (!S.Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 6809 | VisibleTypeConversionsQuals.hasVolatile()) { |
| 6810 | // volatile version |
| 6811 | ParamTypes[0] = |
| 6812 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6813 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 6814 | CandidateSet, /*IsAssigmentOperator=*/true); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6815 | } |
| 6816 | } |
| 6817 | } |
| 6818 | } |
| 6819 | |
| 6820 | // C++ [over.built]p18: |
| 6821 | // |
| 6822 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 6823 | // VQ is either volatile or empty, and R is a promoted |
| 6824 | // arithmetic type, there exist candidate operator functions of |
| 6825 | // the form |
| 6826 | // |
| 6827 | // VQ L& operator=(VQ L&, R); |
| 6828 | // VQ L& operator*=(VQ L&, R); |
| 6829 | // VQ L& operator/=(VQ L&, R); |
| 6830 | // VQ L& operator+=(VQ L&, R); |
| 6831 | // VQ L& operator-=(VQ L&, R); |
| 6832 | void addAssignmentArithmeticOverloads(bool isEqualOp) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6833 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6834 | return; |
| 6835 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6836 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 6837 | for (unsigned Right = FirstPromotedArithmeticType; |
| 6838 | Right < LastPromotedArithmeticType; ++Right) { |
| 6839 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6840 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6841 | |
| 6842 | // Add this built-in operator as a candidate (VQ is empty). |
| 6843 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6844 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6845 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 6846 | /*IsAssigmentOperator=*/isEqualOp); |
| 6847 | |
| 6848 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 6849 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 6850 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6851 | S.Context.getVolatileType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6852 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6853 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 6854 | CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6855 | /*IsAssigmentOperator=*/isEqualOp); |
| 6856 | } |
| 6857 | } |
| 6858 | } |
| 6859 | |
| 6860 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 6861 | for (BuiltinCandidateTypeSet::iterator |
| 6862 | Vec1 = CandidateTypes[0].vector_begin(), |
| 6863 | Vec1End = CandidateTypes[0].vector_end(); |
| 6864 | Vec1 != Vec1End; ++Vec1) { |
| 6865 | for (BuiltinCandidateTypeSet::iterator |
| 6866 | Vec2 = CandidateTypes[1].vector_begin(), |
| 6867 | Vec2End = CandidateTypes[1].vector_end(); |
| 6868 | Vec2 != Vec2End; ++Vec2) { |
| 6869 | QualType ParamTypes[2]; |
| 6870 | ParamTypes[1] = *Vec2; |
| 6871 | // Add this built-in operator as a candidate (VQ is empty). |
| 6872 | ParamTypes[0] = S.Context.getLValueReferenceType(*Vec1); |
| 6873 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 6874 | /*IsAssigmentOperator=*/isEqualOp); |
| 6875 | |
| 6876 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 6877 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 6878 | ParamTypes[0] = S.Context.getVolatileType(*Vec1); |
| 6879 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6880 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 6881 | CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6882 | /*IsAssigmentOperator=*/isEqualOp); |
| 6883 | } |
| 6884 | } |
| 6885 | } |
| 6886 | } |
| 6887 | |
| 6888 | // C++ [over.built]p22: |
| 6889 | // |
| 6890 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 6891 | // is either volatile or empty, and R is a promoted integral |
| 6892 | // type, there exist candidate operator functions of the form |
| 6893 | // |
| 6894 | // VQ L& operator%=(VQ L&, R); |
| 6895 | // VQ L& operator<<=(VQ L&, R); |
| 6896 | // VQ L& operator>>=(VQ L&, R); |
| 6897 | // VQ L& operator&=(VQ L&, R); |
| 6898 | // VQ L& operator^=(VQ L&, R); |
| 6899 | // VQ L& operator|=(VQ L&, R); |
| 6900 | void addAssignmentIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6901 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6902 | return; |
| 6903 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6904 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 6905 | for (unsigned Right = FirstPromotedIntegralType; |
| 6906 | Right < LastPromotedIntegralType; ++Right) { |
| 6907 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6908 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6909 | |
| 6910 | // Add this built-in operator as a candidate (VQ is empty). |
| 6911 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6912 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6913 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 6914 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 6915 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6916 | ParamTypes[0] = getArithmeticType(Left); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6917 | ParamTypes[0] = S.Context.getVolatileType(ParamTypes[0]); |
| 6918 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
| 6919 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 6920 | CandidateSet); |
| 6921 | } |
| 6922 | } |
| 6923 | } |
| 6924 | } |
| 6925 | |
| 6926 | // C++ [over.operator]p23: |
| 6927 | // |
| 6928 | // There also exist candidate operator functions of the form |
| 6929 | // |
| 6930 | // bool operator!(bool); |
| 6931 | // bool operator&&(bool, bool); |
| 6932 | // bool operator||(bool, bool); |
| 6933 | void addExclaimOverload() { |
| 6934 | QualType ParamTy = S.Context.BoolTy; |
| 6935 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 6936 | /*IsAssignmentOperator=*/false, |
| 6937 | /*NumContextualBoolArguments=*/1); |
| 6938 | } |
| 6939 | void addAmpAmpOrPipePipeOverload() { |
| 6940 | QualType ParamTypes[2] = { S.Context.BoolTy, S.Context.BoolTy }; |
| 6941 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 6942 | /*IsAssignmentOperator=*/false, |
| 6943 | /*NumContextualBoolArguments=*/2); |
| 6944 | } |
| 6945 | |
| 6946 | // C++ [over.built]p13: |
| 6947 | // |
| 6948 | // For every cv-qualified or cv-unqualified object type T there |
| 6949 | // exist candidate operator functions of the form |
| 6950 | // |
| 6951 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 6952 | // T& operator[](T*, ptrdiff_t); |
| 6953 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 6954 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 6955 | // T& operator[](ptrdiff_t, T*); |
| 6956 | void addSubscriptOverloads() { |
| 6957 | for (BuiltinCandidateTypeSet::iterator |
| 6958 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6959 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6960 | Ptr != PtrEnd; ++Ptr) { |
| 6961 | QualType ParamTypes[2] = { *Ptr, S.Context.getPointerDiffType() }; |
| 6962 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6963 | if (!PointeeType->isObjectType()) |
| 6964 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6965 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6966 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 6967 | |
| 6968 | // T& operator[](T*, ptrdiff_t) |
| 6969 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 6970 | } |
| 6971 | |
| 6972 | for (BuiltinCandidateTypeSet::iterator |
| 6973 | Ptr = CandidateTypes[1].pointer_begin(), |
| 6974 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 6975 | Ptr != PtrEnd; ++Ptr) { |
| 6976 | QualType ParamTypes[2] = { S.Context.getPointerDiffType(), *Ptr }; |
| 6977 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6978 | if (!PointeeType->isObjectType()) |
| 6979 | continue; |
| 6980 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6981 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 6982 | |
| 6983 | // T& operator[](ptrdiff_t, T*) |
| 6984 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 6985 | } |
| 6986 | } |
| 6987 | |
| 6988 | // C++ [over.built]p11: |
| 6989 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 6990 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 6991 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 6992 | // there exist candidate operator functions of the form |
| 6993 | // |
| 6994 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 6995 | // |
| 6996 | // where CV12 is the union of CV1 and CV2. |
| 6997 | void addArrowStarOverloads() { |
| 6998 | for (BuiltinCandidateTypeSet::iterator |
| 6999 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7000 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7001 | Ptr != PtrEnd; ++Ptr) { |
| 7002 | QualType C1Ty = (*Ptr); |
| 7003 | QualType C1; |
| 7004 | QualifierCollector Q1; |
| 7005 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
| 7006 | if (!isa<RecordType>(C1)) |
| 7007 | continue; |
| 7008 | // heuristic to reduce number of builtin candidates in the set. |
| 7009 | // Add volatile/restrict version only if there are conversions to a |
| 7010 | // volatile/restrict type. |
| 7011 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 7012 | continue; |
| 7013 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 7014 | continue; |
| 7015 | for (BuiltinCandidateTypeSet::iterator |
| 7016 | MemPtr = CandidateTypes[1].member_pointer_begin(), |
| 7017 | MemPtrEnd = CandidateTypes[1].member_pointer_end(); |
| 7018 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7019 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 7020 | QualType C2 = QualType(mptr->getClass(), 0); |
| 7021 | C2 = C2.getUnqualifiedType(); |
| 7022 | if (C1 != C2 && !S.IsDerivedFrom(C1, C2)) |
| 7023 | break; |
| 7024 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 7025 | // build CV12 T& |
| 7026 | QualType T = mptr->getPointeeType(); |
| 7027 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 7028 | T.isVolatileQualified()) |
| 7029 | continue; |
| 7030 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 7031 | T.isRestrictQualified()) |
| 7032 | continue; |
| 7033 | T = Q1.apply(S.Context, T); |
| 7034 | QualType ResultTy = S.Context.getLValueReferenceType(T); |
| 7035 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 7036 | } |
| 7037 | } |
| 7038 | } |
| 7039 | |
| 7040 | // Note that we don't consider the first argument, since it has been |
| 7041 | // contextually converted to bool long ago. The candidates below are |
| 7042 | // therefore added as binary. |
| 7043 | // |
| 7044 | // C++ [over.built]p25: |
| 7045 | // For every type T, where T is a pointer, pointer-to-member, or scoped |
| 7046 | // enumeration type, there exist candidate operator functions of the form |
| 7047 | // |
| 7048 | // T operator?(bool, T, T); |
| 7049 | // |
| 7050 | void addConditionalOperatorOverloads() { |
| 7051 | /// Set of (canonical) types that we've already handled. |
| 7052 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7053 | |
| 7054 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7055 | for (BuiltinCandidateTypeSet::iterator |
| 7056 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 7057 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 7058 | Ptr != PtrEnd; ++Ptr) { |
| 7059 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7060 | continue; |
| 7061 | |
| 7062 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 7063 | S.AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 7064 | } |
| 7065 | |
| 7066 | for (BuiltinCandidateTypeSet::iterator |
| 7067 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7068 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7069 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7070 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7071 | continue; |
| 7072 | |
| 7073 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 7074 | S.AddBuiltinCandidate(*MemPtr, ParamTypes, Args, 2, CandidateSet); |
| 7075 | } |
| 7076 | |
| 7077 | if (S.getLangOptions().CPlusPlus0x) { |
| 7078 | for (BuiltinCandidateTypeSet::iterator |
| 7079 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7080 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7081 | Enum != EnumEnd; ++Enum) { |
| 7082 | if (!(*Enum)->getAs<EnumType>()->getDecl()->isScoped()) |
| 7083 | continue; |
| 7084 | |
| 7085 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 7086 | continue; |
| 7087 | |
| 7088 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 7089 | S.AddBuiltinCandidate(*Enum, ParamTypes, Args, 2, CandidateSet); |
| 7090 | } |
| 7091 | } |
| 7092 | } |
| 7093 | } |
| 7094 | }; |
| 7095 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7096 | } // end anonymous namespace |
| 7097 | |
| 7098 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 7099 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 7100 | /// on the operator @p Op and the arguments given. For example, if the |
| 7101 | /// operator is a binary '+', this routine might add "int |
| 7102 | /// operator+(int, int)" to cover integer addition. |
| 7103 | void |
| 7104 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 7105 | SourceLocation OpLoc, |
| 7106 | Expr **Args, unsigned NumArgs, |
| 7107 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7108 | // Find all of the types that the arguments can convert to, but only |
| 7109 | // if the operator we're looking at has built-in operator candidates |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7110 | // that make use of these types. Also record whether we encounter non-record |
| 7111 | // candidate types or either arithmetic or enumeral candidate types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7112 | Qualifiers VisibleTypeConversionsQuals; |
| 7113 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 7114 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 7115 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7116 | |
| 7117 | bool HasNonRecordCandidateType = false; |
| 7118 | bool HasArithmeticOrEnumeralCandidateType = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7119 | SmallVector<BuiltinCandidateTypeSet, 2> CandidateTypes; |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7120 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 7121 | CandidateTypes.push_back(BuiltinCandidateTypeSet(*this)); |
| 7122 | CandidateTypes[ArgIdx].AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 7123 | OpLoc, |
| 7124 | true, |
| 7125 | (Op == OO_Exclaim || |
| 7126 | Op == OO_AmpAmp || |
| 7127 | Op == OO_PipePipe), |
| 7128 | VisibleTypeConversionsQuals); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7129 | HasNonRecordCandidateType = HasNonRecordCandidateType || |
| 7130 | CandidateTypes[ArgIdx].hasNonRecordTypes(); |
| 7131 | HasArithmeticOrEnumeralCandidateType = |
| 7132 | HasArithmeticOrEnumeralCandidateType || |
| 7133 | CandidateTypes[ArgIdx].hasArithmeticOrEnumeralTypes(); |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7134 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7135 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7136 | // Exit early when no non-record types have been added to the candidate set |
| 7137 | // for any of the arguments to the operator. |
Douglas Gregor | 25aaff9 | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 7138 | // |
| 7139 | // We can't exit early for !, ||, or &&, since there we have always have |
| 7140 | // 'bool' overloads. |
| 7141 | if (!HasNonRecordCandidateType && |
| 7142 | !(Op == OO_Exclaim || Op == OO_AmpAmp || Op == OO_PipePipe)) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7143 | return; |
| 7144 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7145 | // Setup an object to manage the common state for building overloads. |
| 7146 | BuiltinOperatorOverloadBuilder OpBuilder(*this, Args, NumArgs, |
| 7147 | VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7148 | HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7149 | CandidateTypes, CandidateSet); |
| 7150 | |
| 7151 | // Dispatch over the operation to add in only those overloads which apply. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7152 | switch (Op) { |
| 7153 | case OO_None: |
| 7154 | case NUM_OVERLOADED_OPERATORS: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7155 | llvm_unreachable("Expected an overloaded operator"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7156 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7157 | case OO_New: |
| 7158 | case OO_Delete: |
| 7159 | case OO_Array_New: |
| 7160 | case OO_Array_Delete: |
| 7161 | case OO_Call: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7162 | llvm_unreachable( |
| 7163 | "Special operators don't use AddBuiltinOperatorCandidates"); |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7164 | |
| 7165 | case OO_Comma: |
| 7166 | case OO_Arrow: |
| 7167 | // C++ [over.match.oper]p3: |
| 7168 | // -- For the operator ',', the unary operator '&', or the |
| 7169 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7170 | break; |
| 7171 | |
| 7172 | case OO_Plus: // '+' is either unary or binary |
Chandler Carruth | 32fe0d0 | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 7173 | if (NumArgs == 1) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7174 | OpBuilder.addUnaryPlusPointerOverloads(); |
Chandler Carruth | 32fe0d0 | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 7175 | // Fall through. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7176 | |
| 7177 | case OO_Minus: // '-' is either unary or binary |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7178 | if (NumArgs == 1) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7179 | OpBuilder.addUnaryPlusOrMinusArithmeticOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7180 | } else { |
| 7181 | OpBuilder.addBinaryPlusOrMinusPointerOverloads(Op); |
| 7182 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7183 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7184 | break; |
| 7185 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7186 | case OO_Star: // '*' is either unary or binary |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7187 | if (NumArgs == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7188 | OpBuilder.addUnaryStarPointerOverloads(); |
| 7189 | else |
| 7190 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7191 | break; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7192 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7193 | case OO_Slash: |
| 7194 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 7195 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7196 | |
| 7197 | case OO_PlusPlus: |
| 7198 | case OO_MinusMinus: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7199 | OpBuilder.addPlusPlusMinusMinusArithmeticOverloads(Op); |
| 7200 | OpBuilder.addPlusPlusMinusMinusPointerOverloads(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7201 | break; |
| 7202 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 7203 | case OO_EqualEqual: |
| 7204 | case OO_ExclaimEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7205 | OpBuilder.addEqualEqualOrNotEqualMemberPointerOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 7206 | // Fall through. |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 7207 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7208 | case OO_Less: |
| 7209 | case OO_Greater: |
| 7210 | case OO_LessEqual: |
| 7211 | case OO_GreaterEqual: |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7212 | OpBuilder.addRelationalPointerOrEnumeralOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 7213 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/true); |
| 7214 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7215 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7216 | case OO_Percent: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7217 | case OO_Caret: |
| 7218 | case OO_Pipe: |
| 7219 | case OO_LessLess: |
| 7220 | case OO_GreaterGreater: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7221 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7222 | break; |
| 7223 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7224 | case OO_Amp: // '&' is either unary or binary |
| 7225 | if (NumArgs == 1) |
| 7226 | // C++ [over.match.oper]p3: |
| 7227 | // -- For the operator ',', the unary operator '&', or the |
| 7228 | // operator '->', the built-in candidates set is empty. |
| 7229 | break; |
| 7230 | |
| 7231 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
| 7232 | break; |
| 7233 | |
| 7234 | case OO_Tilde: |
| 7235 | OpBuilder.addUnaryTildePromotedIntegralOverloads(); |
| 7236 | break; |
| 7237 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7238 | case OO_Equal: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7239 | OpBuilder.addAssignmentMemberPointerOrEnumeralOverloads(); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7240 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7241 | |
| 7242 | case OO_PlusEqual: |
| 7243 | case OO_MinusEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7244 | OpBuilder.addAssignmentPointerOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7245 | // Fall through. |
| 7246 | |
| 7247 | case OO_StarEqual: |
| 7248 | case OO_SlashEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7249 | OpBuilder.addAssignmentArithmeticOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7250 | break; |
| 7251 | |
| 7252 | case OO_PercentEqual: |
| 7253 | case OO_LessLessEqual: |
| 7254 | case OO_GreaterGreaterEqual: |
| 7255 | case OO_AmpEqual: |
| 7256 | case OO_CaretEqual: |
| 7257 | case OO_PipeEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7258 | OpBuilder.addAssignmentIntegralOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7259 | break; |
| 7260 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7261 | case OO_Exclaim: |
| 7262 | OpBuilder.addExclaimOverload(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7263 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7264 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7265 | case OO_AmpAmp: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7266 | case OO_PipePipe: |
| 7267 | OpBuilder.addAmpAmpOrPipePipeOverload(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7268 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7269 | |
| 7270 | case OO_Subscript: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7271 | OpBuilder.addSubscriptOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7272 | break; |
| 7273 | |
| 7274 | case OO_ArrowStar: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7275 | OpBuilder.addArrowStarOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7276 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 7277 | |
| 7278 | case OO_Conditional: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7279 | OpBuilder.addConditionalOperatorOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7280 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7281 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7282 | } |
| 7283 | } |
| 7284 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7285 | /// \brief Add function candidates found via argument-dependent lookup |
| 7286 | /// to the set of overloading candidates. |
| 7287 | /// |
| 7288 | /// This routine performs argument-dependent name lookup based on the |
| 7289 | /// given function name (which may also be an operator name) and adds |
| 7290 | /// all of the overload candidates found by ADL to the overload |
| 7291 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7292 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7293 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7294 | bool Operator, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7295 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 7296 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7297 | OverloadCandidateSet& CandidateSet, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 7298 | bool PartialOverloading, |
| 7299 | bool StdNamespaceIsAssociated) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7300 | ADLResult Fns; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7301 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 7302 | // FIXME: This approach for uniquing ADL results (and removing |
| 7303 | // redundant candidates from the set) relies on pointer-equality, |
| 7304 | // which means we need to key off the canonical decl. However, |
| 7305 | // always going back to the canonical decl might not get us the |
| 7306 | // right set of default arguments. What default arguments are |
| 7307 | // we supposed to consider on ADL candidates, anyway? |
| 7308 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7309 | // FIXME: Pass in the explicit template arguments? |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 7310 | ArgumentDependentLookup(Name, Operator, Args, NumArgs, Fns, |
| 7311 | StdNamespaceIsAssociated); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7312 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7313 | // Erase all of the candidates we already knew about. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7314 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 7315 | CandEnd = CandidateSet.end(); |
| 7316 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7317 | if (Cand->Function) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7318 | Fns.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7319 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7320 | Fns.erase(FunTmpl); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7321 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7322 | |
| 7323 | // For each of the ADL candidates we found, add it to the overload |
| 7324 | // set. |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7325 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7326 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7327 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7328 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7329 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7330 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7331 | AddOverloadCandidate(FD, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 7332 | false, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7333 | } else |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7334 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7335 | FoundDecl, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 7336 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7337 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7338 | } |
| 7339 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7340 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 7341 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7342 | bool |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7343 | isBetterOverloadCandidate(Sema &S, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 7344 | const OverloadCandidate &Cand1, |
| 7345 | const OverloadCandidate &Cand2, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7346 | SourceLocation Loc, |
| 7347 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7348 | // Define viable functions to be better candidates than non-viable |
| 7349 | // functions. |
| 7350 | if (!Cand2.Viable) |
| 7351 | return Cand1.Viable; |
| 7352 | else if (!Cand1.Viable) |
| 7353 | return false; |
| 7354 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7355 | // C++ [over.match.best]p1: |
| 7356 | // |
| 7357 | // -- if F is a static member function, ICS1(F) is defined such |
| 7358 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 7359 | // any function G, and, symmetrically, ICS1(G) is neither |
| 7360 | // better nor worse than ICS1(F). |
| 7361 | unsigned StartArg = 0; |
| 7362 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 7363 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7364 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7365 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7366 | // A viable function F1 is defined to be a better function than another |
| 7367 | // 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] | 7368 | // conversion sequence than ICSi(F2), and then... |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 7369 | unsigned NumArgs = Cand1.NumConversions; |
| 7370 | assert(Cand2.NumConversions == NumArgs && "Overload candidate mismatch"); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7371 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7372 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7373 | switch (CompareImplicitConversionSequences(S, |
| 7374 | Cand1.Conversions[ArgIdx], |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7375 | Cand2.Conversions[ArgIdx])) { |
| 7376 | case ImplicitConversionSequence::Better: |
| 7377 | // Cand1 has a better conversion sequence. |
| 7378 | HasBetterConversion = true; |
| 7379 | break; |
| 7380 | |
| 7381 | case ImplicitConversionSequence::Worse: |
| 7382 | // Cand1 can't be better than Cand2. |
| 7383 | return false; |
| 7384 | |
| 7385 | case ImplicitConversionSequence::Indistinguishable: |
| 7386 | // Do nothing. |
| 7387 | break; |
| 7388 | } |
| 7389 | } |
| 7390 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7391 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7392 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7393 | if (HasBetterConversion) |
| 7394 | return true; |
| 7395 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7396 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7397 | // specialization, or, if not that, |
Douglas Gregor | ccd4713 | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 7398 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7399 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 7400 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7401 | |
| 7402 | // -- F1 and F2 are function template specializations, and the function |
| 7403 | // template for F1 is more specialized than the template for F2 |
| 7404 | // 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] | 7405 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 7406 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7407 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7408 | if (FunctionTemplateDecl *BetterTemplate |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7409 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 7410 | Cand2.Function->getPrimaryTemplate(), |
| 7411 | Loc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7412 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 7413 | : TPOC_Call, |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7414 | Cand1.ExplicitCallArguments)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7415 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7416 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7417 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7418 | // -- the context is an initialization by user-defined conversion |
| 7419 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 7420 | // from the return type of F1 to the destination type (i.e., |
| 7421 | // the type of the entity being initialized) is a better |
| 7422 | // conversion sequence than the standard conversion sequence |
| 7423 | // from the return type of F2 to the destination type. |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7424 | if (UserDefinedConversion && Cand1.Function && Cand2.Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7425 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7426 | isa<CXXConversionDecl>(Cand2.Function)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7427 | switch (CompareStandardConversionSequences(S, |
| 7428 | Cand1.FinalConversion, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7429 | Cand2.FinalConversion)) { |
| 7430 | case ImplicitConversionSequence::Better: |
| 7431 | // Cand1 has a better conversion sequence. |
| 7432 | return true; |
| 7433 | |
| 7434 | case ImplicitConversionSequence::Worse: |
| 7435 | // Cand1 can't be better than Cand2. |
| 7436 | return false; |
| 7437 | |
| 7438 | case ImplicitConversionSequence::Indistinguishable: |
| 7439 | // Do nothing |
| 7440 | break; |
| 7441 | } |
| 7442 | } |
| 7443 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7444 | return false; |
| 7445 | } |
| 7446 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7447 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7448 | /// within an overload candidate set. |
| 7449 | /// |
| 7450 | /// \param CandidateSet the set of candidate functions. |
| 7451 | /// |
| 7452 | /// \param Loc the location of the function name (or operator symbol) for |
| 7453 | /// which overload resolution occurs. |
| 7454 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7455 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7456 | /// function, Best points to the candidate function found. |
| 7457 | /// |
| 7458 | /// \returns The result of overload resolution. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7459 | OverloadingResult |
| 7460 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 7461 | iterator &Best, |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 7462 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7463 | // Find the best viable function. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7464 | Best = end(); |
| 7465 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
| 7466 | if (Cand->Viable) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7467 | if (Best == end() || isBetterOverloadCandidate(S, *Cand, *Best, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7468 | UserDefinedConversion)) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7469 | Best = Cand; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7470 | } |
| 7471 | |
| 7472 | // If we didn't find any viable functions, abort. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7473 | if (Best == end()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7474 | return OR_No_Viable_Function; |
| 7475 | |
| 7476 | // Make sure that this function is better than every other viable |
| 7477 | // function. If not, we have an ambiguity. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7478 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7479 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7480 | Cand != Best && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7481 | !isBetterOverloadCandidate(S, *Best, *Cand, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7482 | UserDefinedConversion)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7483 | Best = end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7484 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7485 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7486 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7487 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7488 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7489 | if (Best->Function && |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 7490 | (Best->Function->isDeleted() || |
| 7491 | S.isFunctionConsideredUnavailable(Best->Function))) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7492 | return OR_Deleted; |
| 7493 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7494 | return OR_Success; |
| 7495 | } |
| 7496 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7497 | namespace { |
| 7498 | |
| 7499 | enum OverloadCandidateKind { |
| 7500 | oc_function, |
| 7501 | oc_method, |
| 7502 | oc_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7503 | oc_function_template, |
| 7504 | oc_method_template, |
| 7505 | oc_constructor_template, |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7506 | oc_implicit_default_constructor, |
| 7507 | oc_implicit_copy_constructor, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 7508 | oc_implicit_move_constructor, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7509 | oc_implicit_copy_assignment, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 7510 | oc_implicit_move_assignment, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7511 | oc_implicit_inherited_constructor |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7512 | }; |
| 7513 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7514 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 7515 | FunctionDecl *Fn, |
| 7516 | std::string &Description) { |
| 7517 | bool isTemplate = false; |
| 7518 | |
| 7519 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 7520 | isTemplate = true; |
| 7521 | Description = S.getTemplateArgumentBindingsText( |
| 7522 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 7523 | } |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7524 | |
| 7525 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7526 | if (!Ctor->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7527 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7528 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7529 | if (Ctor->getInheritedConstructor()) |
| 7530 | return oc_implicit_inherited_constructor; |
| 7531 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 7532 | if (Ctor->isDefaultConstructor()) |
| 7533 | return oc_implicit_default_constructor; |
| 7534 | |
| 7535 | if (Ctor->isMoveConstructor()) |
| 7536 | return oc_implicit_move_constructor; |
| 7537 | |
| 7538 | assert(Ctor->isCopyConstructor() && |
| 7539 | "unexpected sort of implicit constructor"); |
| 7540 | return oc_implicit_copy_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7541 | } |
| 7542 | |
| 7543 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 7544 | // This actually gets spelled 'candidate function' for now, but |
| 7545 | // it doesn't hurt to split it out. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7546 | if (!Meth->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7547 | return isTemplate ? oc_method_template : oc_method; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7548 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 7549 | if (Meth->isMoveAssignmentOperator()) |
| 7550 | return oc_implicit_move_assignment; |
| 7551 | |
Douglas Gregor | 3e9438b | 2010-09-27 22:37:28 +0000 | [diff] [blame] | 7552 | assert(Meth->isCopyAssignmentOperator() |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7553 | && "implicit method is not copy assignment operator?"); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7554 | return oc_implicit_copy_assignment; |
| 7555 | } |
| 7556 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7557 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7558 | } |
| 7559 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7560 | void MaybeEmitInheritedConstructorNote(Sema &S, FunctionDecl *Fn) { |
| 7561 | const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn); |
| 7562 | if (!Ctor) return; |
| 7563 | |
| 7564 | Ctor = Ctor->getInheritedConstructor(); |
| 7565 | if (!Ctor) return; |
| 7566 | |
| 7567 | S.Diag(Ctor->getLocation(), diag::note_ovl_candidate_inherited_constructor); |
| 7568 | } |
| 7569 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7570 | } // end anonymous namespace |
| 7571 | |
| 7572 | // Notes the location of an overload candidate. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 7573 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn, QualType DestType) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7574 | std::string FnDesc; |
| 7575 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 7576 | PartialDiagnostic PD = PDiag(diag::note_ovl_candidate) |
| 7577 | << (unsigned) K << FnDesc; |
| 7578 | HandleFunctionTypeMismatch(PD, Fn->getType(), DestType); |
| 7579 | Diag(Fn->getLocation(), PD); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7580 | MaybeEmitInheritedConstructorNote(*this, Fn); |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7581 | } |
| 7582 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7583 | //Notes the location of all overload candidates designated through |
| 7584 | // OverloadedExpr |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 7585 | void Sema::NoteAllOverloadCandidates(Expr* OverloadedExpr, QualType DestType) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7586 | assert(OverloadedExpr->getType() == Context.OverloadTy); |
| 7587 | |
| 7588 | OverloadExpr::FindResult Ovl = OverloadExpr::find(OverloadedExpr); |
| 7589 | OverloadExpr *OvlExpr = Ovl.Expression; |
| 7590 | |
| 7591 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 7592 | IEnd = OvlExpr->decls_end(); |
| 7593 | I != IEnd; ++I) { |
| 7594 | if (FunctionTemplateDecl *FunTmpl = |
| 7595 | dyn_cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 7596 | NoteOverloadCandidate(FunTmpl->getTemplatedDecl(), DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7597 | } else if (FunctionDecl *Fun |
| 7598 | = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 7599 | NoteOverloadCandidate(Fun, DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7600 | } |
| 7601 | } |
| 7602 | } |
| 7603 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7604 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 7605 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 7606 | /// target types of the conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7607 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
| 7608 | Sema &S, |
| 7609 | SourceLocation CaretLoc, |
| 7610 | const PartialDiagnostic &PDiag) const { |
| 7611 | S.Diag(CaretLoc, PDiag) |
| 7612 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7613 | for (AmbiguousConversionSequence::const_iterator |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7614 | I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
| 7615 | S.NoteOverloadCandidate(*I); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7616 | } |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7617 | } |
| 7618 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7619 | namespace { |
| 7620 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7621 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 7622 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 7623 | assert(Conv.isBad()); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7624 | assert(Cand->Function && "for now, candidate must be a function"); |
| 7625 | FunctionDecl *Fn = Cand->Function; |
| 7626 | |
| 7627 | // There's a conversion slot for the object argument if this is a |
| 7628 | // non-constructor method. Note that 'I' corresponds the |
| 7629 | // conversion-slot index. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7630 | bool isObjectArgument = false; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7631 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7632 | if (I == 0) |
| 7633 | isObjectArgument = true; |
| 7634 | else |
| 7635 | I--; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7636 | } |
| 7637 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7638 | std::string FnDesc; |
| 7639 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 7640 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7641 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 7642 | QualType FromTy = Conv.Bad.getFromType(); |
| 7643 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7644 | |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 7645 | if (FromTy == S.Context.OverloadTy) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 7646 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 7647 | Expr *E = FromExpr->IgnoreParens(); |
| 7648 | if (isa<UnaryOperator>(E)) |
| 7649 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 7650 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 7651 | |
| 7652 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 7653 | << (unsigned) FnKind << FnDesc |
| 7654 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 7655 | << ToTy << Name << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7656 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 7657 | return; |
| 7658 | } |
| 7659 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 7660 | // Do some hand-waving analysis to see if the non-viability is due |
| 7661 | // to a qualifier mismatch. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 7662 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 7663 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 7664 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 7665 | CToTy = RT->getPointeeType(); |
| 7666 | else { |
| 7667 | // TODO: detect and diagnose the full richness of const mismatches. |
| 7668 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 7669 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 7670 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 7671 | } |
| 7672 | |
| 7673 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 7674 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
| 7675 | // It is dumb that we have to do this here. |
| 7676 | while (isa<ArrayType>(CFromTy)) |
| 7677 | CFromTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 7678 | while (isa<ArrayType>(CToTy)) |
| 7679 | CToTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 7680 | |
| 7681 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 7682 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 7683 | |
| 7684 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 7685 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 7686 | << (unsigned) FnKind << FnDesc |
| 7687 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 7688 | << FromTy |
| 7689 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 7690 | << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7691 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 7692 | return; |
| 7693 | } |
| 7694 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7695 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 7696 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7697 | << (unsigned) FnKind << FnDesc |
| 7698 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 7699 | << FromTy |
| 7700 | << FromQs.getObjCLifetime() << ToQs.getObjCLifetime() |
| 7701 | << (unsigned) isObjectArgument << I+1; |
| 7702 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 7703 | return; |
| 7704 | } |
| 7705 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 7706 | if (FromQs.getObjCGCAttr() != ToQs.getObjCGCAttr()) { |
| 7707 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_gc) |
| 7708 | << (unsigned) FnKind << FnDesc |
| 7709 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 7710 | << FromTy |
| 7711 | << FromQs.getObjCGCAttr() << ToQs.getObjCGCAttr() |
| 7712 | << (unsigned) isObjectArgument << I+1; |
| 7713 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 7714 | return; |
| 7715 | } |
| 7716 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 7717 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 7718 | assert(CVR && "unexpected qualifiers mismatch"); |
| 7719 | |
| 7720 | if (isObjectArgument) { |
| 7721 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 7722 | << (unsigned) FnKind << FnDesc |
| 7723 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 7724 | << FromTy << (CVR - 1); |
| 7725 | } else { |
| 7726 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 7727 | << (unsigned) FnKind << FnDesc |
| 7728 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 7729 | << FromTy << (CVR - 1) << I+1; |
| 7730 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7731 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 7732 | return; |
| 7733 | } |
| 7734 | |
Sebastian Redl | fd2a00a | 2011-09-24 17:48:32 +0000 | [diff] [blame] | 7735 | // Special diagnostic for failure to convert an initializer list, since |
| 7736 | // telling the user that it has type void is not useful. |
| 7737 | if (FromExpr && isa<InitListExpr>(FromExpr)) { |
| 7738 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_list_argument) |
| 7739 | << (unsigned) FnKind << FnDesc |
| 7740 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 7741 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 7742 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 7743 | return; |
| 7744 | } |
| 7745 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 7746 | // Diagnose references or pointers to incomplete types differently, |
| 7747 | // since it's far from impossible that the incompleteness triggered |
| 7748 | // the failure. |
| 7749 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 7750 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 7751 | TempFromTy = PTy->getPointeeType(); |
| 7752 | if (TempFromTy->isIncompleteType()) { |
| 7753 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 7754 | << (unsigned) FnKind << FnDesc |
| 7755 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 7756 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7757 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 7758 | return; |
| 7759 | } |
| 7760 | |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 7761 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 7762 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 7763 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 7764 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 7765 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 7766 | FromPtrTy->getPointeeType()) && |
| 7767 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 7768 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7769 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 7770 | FromPtrTy->getPointeeType())) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 7771 | BaseToDerivedConversion = 1; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 7772 | } |
| 7773 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 7774 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 7775 | if (const ObjCObjectPointerType *ToPtrTy |
| 7776 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 7777 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 7778 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 7779 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 7780 | FromPtrTy->getPointeeType()) && |
| 7781 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 7782 | BaseToDerivedConversion = 2; |
| 7783 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
| 7784 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 7785 | !FromTy->isIncompleteType() && |
| 7786 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 7787 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) |
| 7788 | BaseToDerivedConversion = 3; |
| 7789 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7790 | |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 7791 | if (BaseToDerivedConversion) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7792 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 7793 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 7794 | << (unsigned) FnKind << FnDesc |
| 7795 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 7796 | << (BaseToDerivedConversion - 1) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7797 | << FromTy << ToTy << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7798 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 7799 | return; |
| 7800 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7801 | |
Fariborz Jahanian | 909bcb3 | 2011-07-20 17:14:09 +0000 | [diff] [blame] | 7802 | if (isa<ObjCObjectPointerType>(CFromTy) && |
| 7803 | isa<PointerType>(CToTy)) { |
| 7804 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 7805 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 7806 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
| 7807 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_arc_conv) |
| 7808 | << (unsigned) FnKind << FnDesc |
| 7809 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 7810 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 7811 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 7812 | return; |
| 7813 | } |
| 7814 | } |
| 7815 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7816 | // Emit the generic diagnostic and, optionally, add the hints to it. |
| 7817 | PartialDiagnostic FDiag = S.PDiag(diag::note_ovl_candidate_bad_conv); |
| 7818 | FDiag << (unsigned) FnKind << FnDesc |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7819 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7820 | << FromTy << ToTy << (unsigned) isObjectArgument << I + 1 |
| 7821 | << (unsigned) (Cand->Fix.Kind); |
| 7822 | |
| 7823 | // If we can fix the conversion, suggest the FixIts. |
Benjamin Kramer | 1136ef0 | 2012-01-14 21:05:10 +0000 | [diff] [blame] | 7824 | for (std::vector<FixItHint>::iterator HI = Cand->Fix.Hints.begin(), |
| 7825 | HE = Cand->Fix.Hints.end(); HI != HE; ++HI) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7826 | FDiag << *HI; |
| 7827 | S.Diag(Fn->getLocation(), FDiag); |
| 7828 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7829 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7830 | } |
| 7831 | |
| 7832 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 7833 | unsigned NumFormalArgs) { |
| 7834 | // TODO: treat calls to a missing default constructor as a special case |
| 7835 | |
| 7836 | FunctionDecl *Fn = Cand->Function; |
| 7837 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 7838 | |
| 7839 | unsigned MinParams = Fn->getMinRequiredArguments(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7840 | |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 7841 | // With invalid overloaded operators, it's possible that we think we |
| 7842 | // have an arity mismatch when it fact it looks like we have the |
| 7843 | // right number of arguments, because only overloaded operators have |
| 7844 | // the weird behavior of overloading member and non-member functions. |
| 7845 | // Just don't report anything. |
| 7846 | if (Fn->isInvalidDecl() && |
| 7847 | Fn->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 7848 | return; |
| 7849 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7850 | // at least / at most / exactly |
| 7851 | unsigned mode, modeCount; |
| 7852 | if (NumFormalArgs < MinParams) { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 7853 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 7854 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 7855 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7856 | if (MinParams != FnTy->getNumArgs() || |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 7857 | FnTy->isVariadic() || FnTy->isTemplateVariadic()) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7858 | mode = 0; // "at least" |
| 7859 | else |
| 7860 | mode = 2; // "exactly" |
| 7861 | modeCount = MinParams; |
| 7862 | } else { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 7863 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 7864 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 7865 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7866 | if (MinParams != FnTy->getNumArgs()) |
| 7867 | mode = 1; // "at most" |
| 7868 | else |
| 7869 | mode = 2; // "exactly" |
| 7870 | modeCount = FnTy->getNumArgs(); |
| 7871 | } |
| 7872 | |
| 7873 | std::string Description; |
| 7874 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 7875 | |
| 7876 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7877 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 7878 | << modeCount << NumFormalArgs; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7879 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7880 | } |
| 7881 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7882 | /// Diagnose a failed template-argument deduction. |
| 7883 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
| 7884 | Expr **Args, unsigned NumArgs) { |
| 7885 | FunctionDecl *Fn = Cand->Function; // pattern |
| 7886 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7887 | TemplateParameter Param = Cand->DeductionFailure.getTemplateParameter(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7888 | NamedDecl *ParamD; |
| 7889 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 7890 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 7891 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7892 | switch (Cand->DeductionFailure.Result) { |
| 7893 | case Sema::TDK_Success: |
| 7894 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 7895 | |
| 7896 | case Sema::TDK_Incomplete: { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7897 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| 7898 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_incomplete_deduction) |
| 7899 | << ParamD->getDeclName(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7900 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7901 | return; |
| 7902 | } |
| 7903 | |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 7904 | case Sema::TDK_Underqualified: { |
| 7905 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 7906 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 7907 | |
| 7908 | QualType Param = Cand->DeductionFailure.getFirstArg()->getAsType(); |
| 7909 | |
| 7910 | // Param will have been canonicalized, but it should just be a |
| 7911 | // qualified version of ParamD, so move the qualifiers to that. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 7912 | QualifierCollector Qs; |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 7913 | Qs.strip(Param); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 7914 | QualType NonCanonParam = Qs.apply(S.Context, TParam->getTypeForDecl()); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 7915 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 7916 | |
| 7917 | // Arg has also been canonicalized, but there's nothing we can do |
| 7918 | // about that. It also doesn't matter as much, because it won't |
| 7919 | // have any template parameters in it (because deduction isn't |
| 7920 | // done on dependent types). |
| 7921 | QualType Arg = Cand->DeductionFailure.getSecondArg()->getAsType(); |
| 7922 | |
| 7923 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_underqualified) |
| 7924 | << ParamD->getDeclName() << Arg << NonCanonParam; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7925 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 7926 | return; |
| 7927 | } |
| 7928 | |
| 7929 | case Sema::TDK_Inconsistent: { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7930 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7931 | int which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7932 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7933 | which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7934 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7935 | which = 1; |
| 7936 | else { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7937 | which = 2; |
| 7938 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7939 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7940 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_inconsistent_deduction) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7941 | << which << ParamD->getDeclName() |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7942 | << *Cand->DeductionFailure.getFirstArg() |
| 7943 | << *Cand->DeductionFailure.getSecondArg(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7944 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7945 | return; |
| 7946 | } |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 7947 | |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7948 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7949 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7950 | if (ParamD->getDeclName()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7951 | S.Diag(Fn->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7952 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
| 7953 | << ParamD->getDeclName(); |
| 7954 | else { |
| 7955 | int index = 0; |
| 7956 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 7957 | index = TTP->getIndex(); |
| 7958 | else if (NonTypeTemplateParmDecl *NTTP |
| 7959 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 7960 | index = NTTP->getIndex(); |
| 7961 | else |
| 7962 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7963 | S.Diag(Fn->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7964 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
| 7965 | << (index + 1); |
| 7966 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7967 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7968 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7969 | |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 7970 | case Sema::TDK_TooManyArguments: |
| 7971 | case Sema::TDK_TooFewArguments: |
| 7972 | DiagnoseArityMismatch(S, Cand, NumArgs); |
| 7973 | return; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 7974 | |
| 7975 | case Sema::TDK_InstantiationDepth: |
| 7976 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_instantiation_depth); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7977 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 7978 | return; |
| 7979 | |
| 7980 | case Sema::TDK_SubstitutionFailure: { |
| 7981 | std::string ArgString; |
| 7982 | if (TemplateArgumentList *Args |
| 7983 | = Cand->DeductionFailure.getTemplateArgumentList()) |
| 7984 | ArgString = S.getTemplateArgumentBindingsText( |
| 7985 | Fn->getDescribedFunctionTemplate()->getTemplateParameters(), |
| 7986 | *Args); |
| 7987 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_substitution_failure) |
| 7988 | << ArgString; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7989 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 7990 | return; |
| 7991 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7992 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7993 | // TODO: diagnose these individually, then kill off |
| 7994 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7995 | case Sema::TDK_NonDeducedMismatch: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7996 | case Sema::TDK_FailedOverloadResolution: |
| 7997 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7998 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7999 | return; |
| 8000 | } |
| 8001 | } |
| 8002 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8003 | /// CUDA: diagnose an invalid call across targets. |
| 8004 | void DiagnoseBadTarget(Sema &S, OverloadCandidate *Cand) { |
| 8005 | FunctionDecl *Caller = cast<FunctionDecl>(S.CurContext); |
| 8006 | FunctionDecl *Callee = Cand->Function; |
| 8007 | |
| 8008 | Sema::CUDAFunctionTarget CallerTarget = S.IdentifyCUDATarget(Caller), |
| 8009 | CalleeTarget = S.IdentifyCUDATarget(Callee); |
| 8010 | |
| 8011 | std::string FnDesc; |
| 8012 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Callee, FnDesc); |
| 8013 | |
| 8014 | S.Diag(Callee->getLocation(), diag::note_ovl_candidate_bad_target) |
| 8015 | << (unsigned) FnKind << CalleeTarget << CallerTarget; |
| 8016 | } |
| 8017 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8018 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 8019 | /// already generated a primary error at the call site. |
| 8020 | /// |
| 8021 | /// It really does need to be a single diagnostic with its caret |
| 8022 | /// pointed at the candidate declaration. Yes, this creates some |
| 8023 | /// major challenges of technical writing. Yes, this makes pointing |
| 8024 | /// out problems with specific arguments quite awkward. It's still |
| 8025 | /// better than generating twenty screens of text for every failed |
| 8026 | /// overload. |
| 8027 | /// |
| 8028 | /// It would be great to be able to express per-candidate problems |
| 8029 | /// more richly for those diagnostic clients that cared, but we'd |
| 8030 | /// still have to be just as careful with the default diagnostics. |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8031 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| 8032 | Expr **Args, unsigned NumArgs) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8033 | FunctionDecl *Fn = Cand->Function; |
| 8034 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8035 | // Note deleted candidates, but only if they're viable. |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 8036 | if (Cand->Viable && (Fn->isDeleted() || |
| 8037 | S.isFunctionConsideredUnavailable(Fn))) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8038 | std::string FnDesc; |
| 8039 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8040 | |
| 8041 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8042 | << FnKind << FnDesc << Fn->isDeleted(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8043 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8044 | return; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8045 | } |
| 8046 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8047 | // We don't really have anything else to say about viable candidates. |
| 8048 | if (Cand->Viable) { |
| 8049 | S.NoteOverloadCandidate(Fn); |
| 8050 | return; |
| 8051 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8052 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8053 | switch (Cand->FailureKind) { |
| 8054 | case ovl_fail_too_many_arguments: |
| 8055 | case ovl_fail_too_few_arguments: |
| 8056 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8057 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8058 | case ovl_fail_bad_deduction: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8059 | return DiagnoseBadDeduction(S, Cand, Args, NumArgs); |
| 8060 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8061 | case ovl_fail_trivial_conversion: |
| 8062 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 8063 | case ovl_fail_final_conversion_not_exact: |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8064 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8065 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8066 | case ovl_fail_bad_conversion: { |
| 8067 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8068 | for (unsigned N = Cand->NumConversions; I != N; ++I) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8069 | if (Cand->Conversions[I].isBad()) |
| 8070 | return DiagnoseBadConversion(S, Cand, I); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8071 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8072 | // FIXME: this currently happens when we're called from SemaInit |
| 8073 | // when user-conversion overload fails. Figure out how to handle |
| 8074 | // those conditions and diagnose them well. |
| 8075 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8076 | } |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8077 | |
| 8078 | case ovl_fail_bad_target: |
| 8079 | return DiagnoseBadTarget(S, Cand); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8080 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8081 | } |
| 8082 | |
| 8083 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 8084 | // Desugar the type of the surrogate down to a function type, |
| 8085 | // retaining as many typedefs as possible while still showing |
| 8086 | // the function type (and, therefore, its parameter types). |
| 8087 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 8088 | bool isLValueReference = false; |
| 8089 | bool isRValueReference = false; |
| 8090 | bool isPointer = false; |
| 8091 | if (const LValueReferenceType *FnTypeRef = |
| 8092 | FnType->getAs<LValueReferenceType>()) { |
| 8093 | FnType = FnTypeRef->getPointeeType(); |
| 8094 | isLValueReference = true; |
| 8095 | } else if (const RValueReferenceType *FnTypeRef = |
| 8096 | FnType->getAs<RValueReferenceType>()) { |
| 8097 | FnType = FnTypeRef->getPointeeType(); |
| 8098 | isRValueReference = true; |
| 8099 | } |
| 8100 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 8101 | FnType = FnTypePtr->getPointeeType(); |
| 8102 | isPointer = true; |
| 8103 | } |
| 8104 | // Desugar down to a function type. |
| 8105 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 8106 | // Reconstruct the pointer/reference as appropriate. |
| 8107 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 8108 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 8109 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 8110 | |
| 8111 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 8112 | << FnType; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8113 | MaybeEmitInheritedConstructorNote(S, Cand->Surrogate); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8114 | } |
| 8115 | |
| 8116 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 8117 | const char *Opc, |
| 8118 | SourceLocation OpLoc, |
| 8119 | OverloadCandidate *Cand) { |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8120 | assert(Cand->NumConversions <= 2 && "builtin operator is not binary"); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8121 | std::string TypeStr("operator"); |
| 8122 | TypeStr += Opc; |
| 8123 | TypeStr += "("; |
| 8124 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8125 | if (Cand->NumConversions == 1) { |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8126 | TypeStr += ")"; |
| 8127 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 8128 | } else { |
| 8129 | TypeStr += ", "; |
| 8130 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 8131 | TypeStr += ")"; |
| 8132 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 8133 | } |
| 8134 | } |
| 8135 | |
| 8136 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 8137 | OverloadCandidate *Cand) { |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8138 | unsigned NoOperands = Cand->NumConversions; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8139 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 8140 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8141 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 8142 | if (!ICS.isAmbiguous()) continue; |
| 8143 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8144 | ICS.DiagnoseAmbiguousConversion(S, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8145 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8146 | } |
| 8147 | } |
| 8148 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8149 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 8150 | if (Cand->Function) |
| 8151 | return Cand->Function->getLocation(); |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 8152 | if (Cand->IsSurrogate) |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8153 | return Cand->Surrogate->getLocation(); |
| 8154 | return SourceLocation(); |
| 8155 | } |
| 8156 | |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8157 | static unsigned |
| 8158 | RankDeductionFailure(const OverloadCandidate::DeductionFailureInfo &DFI) { |
Chandler Carruth | 78bf680 | 2011-09-10 00:51:24 +0000 | [diff] [blame] | 8159 | switch ((Sema::TemplateDeductionResult)DFI.Result) { |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8160 | case Sema::TDK_Success: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8161 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8162 | |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8163 | case Sema::TDK_Incomplete: |
| 8164 | return 1; |
| 8165 | |
| 8166 | case Sema::TDK_Underqualified: |
| 8167 | case Sema::TDK_Inconsistent: |
| 8168 | return 2; |
| 8169 | |
| 8170 | case Sema::TDK_SubstitutionFailure: |
| 8171 | case Sema::TDK_NonDeducedMismatch: |
| 8172 | return 3; |
| 8173 | |
| 8174 | case Sema::TDK_InstantiationDepth: |
| 8175 | case Sema::TDK_FailedOverloadResolution: |
| 8176 | return 4; |
| 8177 | |
| 8178 | case Sema::TDK_InvalidExplicitArguments: |
| 8179 | return 5; |
| 8180 | |
| 8181 | case Sema::TDK_TooManyArguments: |
| 8182 | case Sema::TDK_TooFewArguments: |
| 8183 | return 6; |
| 8184 | } |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8185 | llvm_unreachable("Unhandled deduction result"); |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8186 | } |
| 8187 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8188 | struct CompareOverloadCandidatesForDisplay { |
| 8189 | Sema &S; |
| 8190 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8191 | |
| 8192 | bool operator()(const OverloadCandidate *L, |
| 8193 | const OverloadCandidate *R) { |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 8194 | // Fast-path this check. |
| 8195 | if (L == R) return false; |
| 8196 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8197 | // Order first by viability. |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8198 | if (L->Viable) { |
| 8199 | if (!R->Viable) return true; |
| 8200 | |
| 8201 | // TODO: introduce a tri-valued comparison for overload |
| 8202 | // candidates. Would be more worthwhile if we had a sort |
| 8203 | // that could exploit it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8204 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation())) return true; |
| 8205 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation())) return false; |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8206 | } else if (R->Viable) |
| 8207 | return false; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8208 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8209 | assert(L->Viable == R->Viable); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8210 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8211 | // Criteria by which we can sort non-viable candidates: |
| 8212 | if (!L->Viable) { |
| 8213 | // 1. Arity mismatches come after other candidates. |
| 8214 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 8215 | L->FailureKind == ovl_fail_too_few_arguments) |
| 8216 | return false; |
| 8217 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 8218 | R->FailureKind == ovl_fail_too_few_arguments) |
| 8219 | return true; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8220 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8221 | // 2. Bad conversions come first and are ordered by the number |
| 8222 | // of bad conversions and quality of good conversions. |
| 8223 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 8224 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 8225 | return true; |
| 8226 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8227 | // The conversion that can be fixed with a smaller number of changes, |
| 8228 | // comes first. |
| 8229 | unsigned numLFixes = L->Fix.NumConversionsFixed; |
| 8230 | unsigned numRFixes = R->Fix.NumConversionsFixed; |
| 8231 | numLFixes = (numLFixes == 0) ? UINT_MAX : numLFixes; |
| 8232 | numRFixes = (numRFixes == 0) ? UINT_MAX : numRFixes; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 8233 | if (numLFixes != numRFixes) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8234 | if (numLFixes < numRFixes) |
| 8235 | return true; |
| 8236 | else |
| 8237 | return false; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 8238 | } |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8239 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8240 | // If there's any ordering between the defined conversions... |
| 8241 | // FIXME: this might not be transitive. |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8242 | assert(L->NumConversions == R->NumConversions); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8243 | |
| 8244 | int leftBetter = 0; |
John McCall | 3a81337 | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 8245 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8246 | for (unsigned E = L->NumConversions; I != E; ++I) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8247 | switch (CompareImplicitConversionSequences(S, |
| 8248 | L->Conversions[I], |
| 8249 | R->Conversions[I])) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8250 | case ImplicitConversionSequence::Better: |
| 8251 | leftBetter++; |
| 8252 | break; |
| 8253 | |
| 8254 | case ImplicitConversionSequence::Worse: |
| 8255 | leftBetter--; |
| 8256 | break; |
| 8257 | |
| 8258 | case ImplicitConversionSequence::Indistinguishable: |
| 8259 | break; |
| 8260 | } |
| 8261 | } |
| 8262 | if (leftBetter > 0) return true; |
| 8263 | if (leftBetter < 0) return false; |
| 8264 | |
| 8265 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 8266 | return false; |
| 8267 | |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8268 | if (L->FailureKind == ovl_fail_bad_deduction) { |
| 8269 | if (R->FailureKind != ovl_fail_bad_deduction) |
| 8270 | return true; |
| 8271 | |
| 8272 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 8273 | return RankDeductionFailure(L->DeductionFailure) |
Eli Friedman | ce1846e | 2011-10-14 23:10:30 +0000 | [diff] [blame] | 8274 | < RankDeductionFailure(R->DeductionFailure); |
Eli Friedman | 1c522f7 | 2011-10-14 21:52:24 +0000 | [diff] [blame] | 8275 | } else if (R->FailureKind == ovl_fail_bad_deduction) |
| 8276 | return false; |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8277 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8278 | // TODO: others? |
| 8279 | } |
| 8280 | |
| 8281 | // Sort everything else by location. |
| 8282 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 8283 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 8284 | |
| 8285 | // Put candidates without locations (e.g. builtins) at the end. |
| 8286 | if (LLoc.isInvalid()) return false; |
| 8287 | if (RLoc.isInvalid()) return true; |
| 8288 | |
| 8289 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8290 | } |
| 8291 | }; |
| 8292 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8293 | /// CompleteNonViableCandidate - Normally, overload resolution only |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8294 | /// computes up to the first. Produces the FixIt set if possible. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8295 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
| 8296 | Expr **Args, unsigned NumArgs) { |
| 8297 | assert(!Cand->Viable); |
| 8298 | |
| 8299 | // Don't do anything on failures other than bad conversion. |
| 8300 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 8301 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8302 | // We only want the FixIts if all the arguments can be corrected. |
| 8303 | bool Unfixable = false; |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 8304 | // Use a implicit copy initialization to check conversion fixes. |
| 8305 | Cand->Fix.setConversionChecker(TryCopyInitialization); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8306 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8307 | // Skip forward to the first bad conversion. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8308 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | 09dd379 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8309 | unsigned ConvCount = Cand->NumConversions; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8310 | while (true) { |
| 8311 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 8312 | ConvIdx++; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8313 | if (Cand->Conversions[ConvIdx - 1].isBad()) { |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 8314 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx - 1, S); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8315 | break; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8316 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8317 | } |
| 8318 | |
| 8319 | if (ConvIdx == ConvCount) |
| 8320 | return; |
| 8321 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8322 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 8323 | "remaining conversion is initialized?"); |
| 8324 | |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 8325 | // FIXME: this should probably be preserved from the overload |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8326 | // operation somehow. |
| 8327 | bool SuppressUserConversions = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8328 | |
| 8329 | const FunctionProtoType* Proto; |
| 8330 | unsigned ArgIdx = ConvIdx; |
| 8331 | |
| 8332 | if (Cand->IsSurrogate) { |
| 8333 | QualType ConvType |
| 8334 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 8335 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 8336 | ConvType = ConvPtrType->getPointeeType(); |
| 8337 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 8338 | ArgIdx--; |
| 8339 | } else if (Cand->Function) { |
| 8340 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 8341 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 8342 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 8343 | ArgIdx--; |
| 8344 | } else { |
| 8345 | // Builtin binary operator with a bad first conversion. |
| 8346 | assert(ConvCount <= 3); |
| 8347 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 8348 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 8349 | = TryCopyInitialization(S, Args[ConvIdx], |
| 8350 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8351 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8352 | /*InOverloadResolution*/ true, |
| 8353 | /*AllowObjCWritebackConversion=*/ |
| 8354 | S.getLangOptions().ObjCAutoRefCount); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8355 | return; |
| 8356 | } |
| 8357 | |
| 8358 | // Fill in the rest of the conversions. |
| 8359 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 8360 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8361 | if (ArgIdx < NumArgsInProto) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8362 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 8363 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8364 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8365 | /*InOverloadResolution=*/true, |
| 8366 | /*AllowObjCWritebackConversion=*/ |
| 8367 | S.getLangOptions().ObjCAutoRefCount); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8368 | // Store the FixIt in the candidate if it exists. |
| 8369 | if (!Unfixable && Cand->Conversions[ConvIdx].isBad()) |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 8370 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx, S); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8371 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8372 | else |
| 8373 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 8374 | } |
| 8375 | } |
| 8376 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8377 | } // end anonymous namespace |
| 8378 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8379 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 8380 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8381 | /// set. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8382 | void OverloadCandidateSet::NoteCandidates(Sema &S, |
| 8383 | OverloadCandidateDisplayKind OCD, |
| 8384 | Expr **Args, unsigned NumArgs, |
| 8385 | const char *Opc, |
| 8386 | SourceLocation OpLoc) { |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8387 | // Sort the candidates by viability and position. Sorting directly would |
| 8388 | // be prohibitive, so we make a set of pointers and sort those. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8389 | SmallVector<OverloadCandidate*, 32> Cands; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8390 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
| 8391 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8392 | if (Cand->Viable) |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8393 | Cands.push_back(Cand); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8394 | else if (OCD == OCD_AllCandidates) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8395 | CompleteNonViableCandidate(S, Cand, Args, NumArgs); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8396 | if (Cand->Function || Cand->IsSurrogate) |
| 8397 | Cands.push_back(Cand); |
| 8398 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 8399 | // want to list every possible builtin candidate. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8400 | } |
| 8401 | } |
| 8402 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8403 | std::sort(Cands.begin(), Cands.end(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8404 | CompareOverloadCandidatesForDisplay(S)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8405 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8406 | bool ReportedAmbiguousConversions = false; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8407 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8408 | SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 8409 | const DiagnosticsEngine::OverloadsShown ShowOverloads = |
| 8410 | S.Diags.getShowOverloads(); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8411 | unsigned CandsShown = 0; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8412 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 8413 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 8414 | |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8415 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 8416 | // the user with. FIXME: This limit should depend on details of the |
| 8417 | // candidate list. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 8418 | if (CandsShown >= 4 && ShowOverloads == DiagnosticsEngine::Ovl_Best) { |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8419 | break; |
| 8420 | } |
| 8421 | ++CandsShown; |
| 8422 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8423 | if (Cand->Function) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8424 | NoteFunctionCandidate(S, Cand, Args, NumArgs); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8425 | else if (Cand->IsSurrogate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8426 | NoteSurrogateCandidate(S, Cand); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8427 | else { |
| 8428 | assert(Cand->Viable && |
| 8429 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8430 | // Generally we only see ambiguities including viable builtin |
| 8431 | // operators if overload resolution got screwed up by an |
| 8432 | // ambiguous user-defined conversion. |
| 8433 | // |
| 8434 | // FIXME: It's quite possible for different conversions to see |
| 8435 | // different ambiguities, though. |
| 8436 | if (!ReportedAmbiguousConversions) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8437 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8438 | ReportedAmbiguousConversions = true; |
| 8439 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8440 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8441 | // If this is a viable builtin, print it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8442 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8443 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8444 | } |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8445 | |
| 8446 | if (I != E) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8447 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8448 | } |
| 8449 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8450 | // [PossiblyAFunctionType] --> [Return] |
| 8451 | // NonFunctionType --> NonFunctionType |
| 8452 | // R (A) --> R(A) |
| 8453 | // R (*)(A) --> R (A) |
| 8454 | // R (&)(A) --> R (A) |
| 8455 | // R (S::*)(A) --> R (A) |
| 8456 | QualType Sema::ExtractUnqualifiedFunctionType(QualType PossiblyAFunctionType) { |
| 8457 | QualType Ret = PossiblyAFunctionType; |
| 8458 | if (const PointerType *ToTypePtr = |
| 8459 | PossiblyAFunctionType->getAs<PointerType>()) |
| 8460 | Ret = ToTypePtr->getPointeeType(); |
| 8461 | else if (const ReferenceType *ToTypeRef = |
| 8462 | PossiblyAFunctionType->getAs<ReferenceType>()) |
| 8463 | Ret = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 8464 | else if (const MemberPointerType *MemTypePtr = |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8465 | PossiblyAFunctionType->getAs<MemberPointerType>()) |
| 8466 | Ret = MemTypePtr->getPointeeType(); |
| 8467 | Ret = |
| 8468 | Context.getCanonicalType(Ret).getUnqualifiedType(); |
| 8469 | return Ret; |
| 8470 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 8471 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8472 | // A helper class to help with address of function resolution |
| 8473 | // - allows us to avoid passing around all those ugly parameters |
| 8474 | class AddressOfFunctionResolver |
| 8475 | { |
| 8476 | Sema& S; |
| 8477 | Expr* SourceExpr; |
| 8478 | const QualType& TargetType; |
| 8479 | QualType TargetFunctionType; // Extracted function type from target type |
| 8480 | |
| 8481 | bool Complain; |
| 8482 | //DeclAccessPair& ResultFunctionAccessPair; |
| 8483 | ASTContext& Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8484 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8485 | bool TargetTypeIsNonStaticMemberFunction; |
| 8486 | bool FoundNonTemplateFunction; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8487 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8488 | OverloadExpr::FindResult OvlExprInfo; |
| 8489 | OverloadExpr *OvlExpr; |
| 8490 | TemplateArgumentListInfo OvlExplicitTemplateArgs; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8491 | SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8492 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8493 | public: |
| 8494 | AddressOfFunctionResolver(Sema &S, Expr* SourceExpr, |
| 8495 | const QualType& TargetType, bool Complain) |
| 8496 | : S(S), SourceExpr(SourceExpr), TargetType(TargetType), |
| 8497 | Complain(Complain), Context(S.getASTContext()), |
| 8498 | TargetTypeIsNonStaticMemberFunction( |
| 8499 | !!TargetType->getAs<MemberPointerType>()), |
| 8500 | FoundNonTemplateFunction(false), |
| 8501 | OvlExprInfo(OverloadExpr::find(SourceExpr)), |
| 8502 | OvlExpr(OvlExprInfo.Expression) |
| 8503 | { |
| 8504 | ExtractUnqualifiedFunctionTypeFromTargetType(); |
| 8505 | |
| 8506 | if (!TargetFunctionType->isFunctionType()) { |
| 8507 | if (OvlExpr->hasExplicitTemplateArgs()) { |
| 8508 | DeclAccessPair dap; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8509 | if (FunctionDecl* Fn = S.ResolveSingleFunctionTemplateSpecialization( |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8510 | OvlExpr, false, &dap) ) { |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 8511 | |
| 8512 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 8513 | if (!Method->isStatic()) { |
| 8514 | // If the target type is a non-function type and the function |
| 8515 | // found is a non-static member function, pretend as if that was |
| 8516 | // the target, it's the only possible type to end up with. |
| 8517 | TargetTypeIsNonStaticMemberFunction = true; |
| 8518 | |
| 8519 | // And skip adding the function if its not in the proper form. |
| 8520 | // We'll diagnose this due to an empty set of functions. |
| 8521 | if (!OvlExprInfo.HasFormOfMemberPointer) |
| 8522 | return; |
| 8523 | } |
| 8524 | } |
| 8525 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8526 | Matches.push_back(std::make_pair(dap,Fn)); |
| 8527 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 8528 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8529 | return; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 8530 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8531 | |
| 8532 | if (OvlExpr->hasExplicitTemplateArgs()) |
| 8533 | OvlExpr->getExplicitTemplateArgs().copyInto(OvlExplicitTemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8534 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8535 | if (FindAllFunctionsThatMatchTargetTypeExactly()) { |
| 8536 | // C++ [over.over]p4: |
| 8537 | // If more than one function is selected, [...] |
| 8538 | if (Matches.size() > 1) { |
| 8539 | if (FoundNonTemplateFunction) |
| 8540 | EliminateAllTemplateMatches(); |
| 8541 | else |
| 8542 | EliminateAllExceptMostSpecializedTemplate(); |
| 8543 | } |
| 8544 | } |
| 8545 | } |
| 8546 | |
| 8547 | private: |
| 8548 | bool isTargetTypeAFunction() const { |
| 8549 | return TargetFunctionType->isFunctionType(); |
| 8550 | } |
| 8551 | |
| 8552 | // [ToType] [Return] |
| 8553 | |
| 8554 | // R (*)(A) --> R (A), IsNonStaticMemberFunction = false |
| 8555 | // R (&)(A) --> R (A), IsNonStaticMemberFunction = false |
| 8556 | // R (S::*)(A) --> R (A), IsNonStaticMemberFunction = true |
| 8557 | void inline ExtractUnqualifiedFunctionTypeFromTargetType() { |
| 8558 | TargetFunctionType = S.ExtractUnqualifiedFunctionType(TargetType); |
| 8559 | } |
| 8560 | |
| 8561 | // return true if any matching specializations were found |
| 8562 | bool AddMatchingTemplateFunction(FunctionTemplateDecl* FunctionTemplate, |
| 8563 | const DeclAccessPair& CurAccessFunPair) { |
| 8564 | if (CXXMethodDecl *Method |
| 8565 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
| 8566 | // Skip non-static function templates when converting to pointer, and |
| 8567 | // static when converting to member pointer. |
| 8568 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 8569 | return false; |
| 8570 | } |
| 8571 | else if (TargetTypeIsNonStaticMemberFunction) |
| 8572 | return false; |
| 8573 | |
| 8574 | // C++ [over.over]p2: |
| 8575 | // If the name is a function template, template argument deduction is |
| 8576 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 8577 | // resulting template argument list is used to generate a single |
| 8578 | // function template specialization, which is added to the set of |
| 8579 | // overloaded functions considered. |
| 8580 | FunctionDecl *Specialization = 0; |
| 8581 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
| 8582 | if (Sema::TemplateDeductionResult Result |
| 8583 | = S.DeduceTemplateArguments(FunctionTemplate, |
| 8584 | &OvlExplicitTemplateArgs, |
| 8585 | TargetFunctionType, Specialization, |
| 8586 | Info)) { |
| 8587 | // FIXME: make a note of the failed deduction for diagnostics. |
| 8588 | (void)Result; |
| 8589 | return false; |
| 8590 | } |
| 8591 | |
| 8592 | // Template argument deduction ensures that we have an exact match. |
| 8593 | // This function template specicalization works. |
| 8594 | Specialization = cast<FunctionDecl>(Specialization->getCanonicalDecl()); |
| 8595 | assert(TargetFunctionType |
| 8596 | == Context.getCanonicalType(Specialization->getType())); |
| 8597 | Matches.push_back(std::make_pair(CurAccessFunPair, Specialization)); |
| 8598 | return true; |
| 8599 | } |
| 8600 | |
| 8601 | bool AddMatchingNonTemplateFunction(NamedDecl* Fn, |
| 8602 | const DeclAccessPair& CurAccessFunPair) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 8603 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 8604 | // Skip non-static functions when converting to pointer, and static |
| 8605 | // when converting to member pointer. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8606 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 8607 | return false; |
| 8608 | } |
| 8609 | else if (TargetTypeIsNonStaticMemberFunction) |
| 8610 | return false; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 8611 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 8612 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8613 | if (S.getLangOptions().CUDA) |
| 8614 | if (FunctionDecl *Caller = dyn_cast<FunctionDecl>(S.CurContext)) |
| 8615 | if (S.CheckCUDATarget(Caller, FunDecl)) |
| 8616 | return false; |
| 8617 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 8618 | QualType ResultTy; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8619 | if (Context.hasSameUnqualifiedType(TargetFunctionType, |
| 8620 | FunDecl->getType()) || |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 8621 | S.IsNoReturnConversion(FunDecl->getType(), TargetFunctionType, |
| 8622 | ResultTy)) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8623 | Matches.push_back(std::make_pair(CurAccessFunPair, |
| 8624 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 8625 | FoundNonTemplateFunction = true; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8626 | return true; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 8627 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8628 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8629 | |
| 8630 | return false; |
| 8631 | } |
| 8632 | |
| 8633 | bool FindAllFunctionsThatMatchTargetTypeExactly() { |
| 8634 | bool Ret = false; |
| 8635 | |
| 8636 | // If the overload expression doesn't have the form of a pointer to |
| 8637 | // member, don't try to convert it to a pointer-to-member type. |
| 8638 | if (IsInvalidFormOfPointerToMemberFunction()) |
| 8639 | return false; |
| 8640 | |
| 8641 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 8642 | E = OvlExpr->decls_end(); |
| 8643 | I != E; ++I) { |
| 8644 | // Look through any using declarations to find the underlying function. |
| 8645 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 8646 | |
| 8647 | // C++ [over.over]p3: |
| 8648 | // Non-member functions and static member functions match |
| 8649 | // targets of type "pointer-to-function" or "reference-to-function." |
| 8650 | // Nonstatic member functions match targets of |
| 8651 | // type "pointer-to-member-function." |
| 8652 | // Note that according to DR 247, the containing class does not matter. |
| 8653 | if (FunctionTemplateDecl *FunctionTemplate |
| 8654 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
| 8655 | if (AddMatchingTemplateFunction(FunctionTemplate, I.getPair())) |
| 8656 | Ret = true; |
| 8657 | } |
| 8658 | // If we have explicit template arguments supplied, skip non-templates. |
| 8659 | else if (!OvlExpr->hasExplicitTemplateArgs() && |
| 8660 | AddMatchingNonTemplateFunction(Fn, I.getPair())) |
| 8661 | Ret = true; |
| 8662 | } |
| 8663 | assert(Ret || Matches.empty()); |
| 8664 | return Ret; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 8665 | } |
| 8666 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8667 | void EliminateAllExceptMostSpecializedTemplate() { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 8668 | // [...] and any given function template specialization F1 is |
| 8669 | // eliminated if the set contains a second function template |
| 8670 | // specialization whose function template is more specialized |
| 8671 | // than the function template of F1 according to the partial |
| 8672 | // ordering rules of 14.5.5.2. |
| 8673 | |
| 8674 | // The algorithm specified above is quadratic. We instead use a |
| 8675 | // two-pass algorithm (similar to the one used to identify the |
| 8676 | // best viable function in an overload set) that identifies the |
| 8677 | // best function template (if it exists). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8678 | |
| 8679 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 8680 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 8681 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8682 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8683 | UnresolvedSetIterator Result = |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8684 | S.getMostSpecialized(MatchesCopy.begin(), MatchesCopy.end(), |
| 8685 | TPOC_Other, 0, SourceExpr->getLocStart(), |
| 8686 | S.PDiag(), |
| 8687 | S.PDiag(diag::err_addr_ovl_ambiguous) |
| 8688 | << Matches[0].second->getDeclName(), |
| 8689 | S.PDiag(diag::note_ovl_candidate) |
| 8690 | << (unsigned) oc_function_template, |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8691 | Complain, TargetFunctionType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8692 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8693 | if (Result != MatchesCopy.end()) { |
| 8694 | // Make it the first and only element |
| 8695 | Matches[0].first = Matches[Result - MatchesCopy.begin()].first; |
| 8696 | Matches[0].second = cast<FunctionDecl>(*Result); |
| 8697 | Matches.resize(1); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8698 | } |
| 8699 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8700 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8701 | void EliminateAllTemplateMatches() { |
| 8702 | // [...] any function template specializations in the set are |
| 8703 | // eliminated if the set also contains a non-template function, [...] |
| 8704 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
| 8705 | if (Matches[I].second->getPrimaryTemplate() == 0) |
| 8706 | ++I; |
| 8707 | else { |
| 8708 | Matches[I] = Matches[--N]; |
| 8709 | Matches.set_size(N); |
| 8710 | } |
| 8711 | } |
| 8712 | } |
| 8713 | |
| 8714 | public: |
| 8715 | void ComplainNoMatchesFound() const { |
| 8716 | assert(Matches.empty()); |
| 8717 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_no_viable) |
| 8718 | << OvlExpr->getName() << TargetFunctionType |
| 8719 | << OvlExpr->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8720 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8721 | } |
| 8722 | |
| 8723 | bool IsInvalidFormOfPointerToMemberFunction() const { |
| 8724 | return TargetTypeIsNonStaticMemberFunction && |
| 8725 | !OvlExprInfo.HasFormOfMemberPointer; |
| 8726 | } |
| 8727 | |
| 8728 | void ComplainIsInvalidFormOfPointerToMemberFunction() const { |
| 8729 | // TODO: Should we condition this on whether any functions might |
| 8730 | // have matched, or is it more appropriate to do that in callers? |
| 8731 | // TODO: a fixit wouldn't hurt. |
| 8732 | S.Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
| 8733 | << TargetType << OvlExpr->getSourceRange(); |
| 8734 | } |
| 8735 | |
| 8736 | void ComplainOfInvalidConversion() const { |
| 8737 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 8738 | << OvlExpr->getName() << TargetType; |
| 8739 | } |
| 8740 | |
| 8741 | void ComplainMultipleMatchesFound() const { |
| 8742 | assert(Matches.size() > 1); |
| 8743 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 8744 | << OvlExpr->getName() |
| 8745 | << OvlExpr->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8746 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8747 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 8748 | |
| 8749 | bool hadMultipleCandidates() const { return (OvlExpr->getNumDecls() > 1); } |
| 8750 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8751 | int getNumMatches() const { return Matches.size(); } |
| 8752 | |
| 8753 | FunctionDecl* getMatchingFunctionDecl() const { |
| 8754 | if (Matches.size() != 1) return 0; |
| 8755 | return Matches[0].second; |
| 8756 | } |
| 8757 | |
| 8758 | const DeclAccessPair* getMatchingFunctionAccessPair() const { |
| 8759 | if (Matches.size() != 1) return 0; |
| 8760 | return &Matches[0].first; |
| 8761 | } |
| 8762 | }; |
| 8763 | |
| 8764 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 8765 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 8766 | /// expression with overloaded function type and @p ToType is the type |
| 8767 | /// we're trying to resolve to. For example: |
| 8768 | /// |
| 8769 | /// @code |
| 8770 | /// int f(double); |
| 8771 | /// int f(int); |
| 8772 | /// |
| 8773 | /// int (*pfd)(double) = f; // selects f(double) |
| 8774 | /// @endcode |
| 8775 | /// |
| 8776 | /// This routine returns the resulting FunctionDecl if it could be |
| 8777 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 8778 | /// routine will emit diagnostics if there is an error. |
| 8779 | FunctionDecl * |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 8780 | Sema::ResolveAddressOfOverloadedFunction(Expr *AddressOfExpr, |
| 8781 | QualType TargetType, |
| 8782 | bool Complain, |
| 8783 | DeclAccessPair &FoundResult, |
| 8784 | bool *pHadMultipleCandidates) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8785 | assert(AddressOfExpr->getType() == Context.OverloadTy); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 8786 | |
| 8787 | AddressOfFunctionResolver Resolver(*this, AddressOfExpr, TargetType, |
| 8788 | Complain); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8789 | int NumMatches = Resolver.getNumMatches(); |
| 8790 | FunctionDecl* Fn = 0; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 8791 | if (NumMatches == 0 && Complain) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8792 | if (Resolver.IsInvalidFormOfPointerToMemberFunction()) |
| 8793 | Resolver.ComplainIsInvalidFormOfPointerToMemberFunction(); |
| 8794 | else |
| 8795 | Resolver.ComplainNoMatchesFound(); |
| 8796 | } |
| 8797 | else if (NumMatches > 1 && Complain) |
| 8798 | Resolver.ComplainMultipleMatchesFound(); |
| 8799 | else if (NumMatches == 1) { |
| 8800 | Fn = Resolver.getMatchingFunctionDecl(); |
| 8801 | assert(Fn); |
| 8802 | FoundResult = *Resolver.getMatchingFunctionAccessPair(); |
| 8803 | MarkDeclarationReferenced(AddressOfExpr->getLocStart(), Fn); |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 8804 | if (Complain) |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8805 | CheckAddressOfMemberAccess(AddressOfExpr, FoundResult); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 8806 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 8807 | |
| 8808 | if (pHadMultipleCandidates) |
| 8809 | *pHadMultipleCandidates = Resolver.hadMultipleCandidates(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8810 | return Fn; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 8811 | } |
| 8812 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8813 | /// \brief Given an expression that refers to an overloaded function, try to |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8814 | /// resolve that overloaded function expression down to a single function. |
| 8815 | /// |
| 8816 | /// This routine can only resolve template-ids that refer to a single function |
| 8817 | /// template, where that template-id refers to a single template whose template |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8818 | /// arguments are either provided by the template-id or have defaults, |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8819 | /// as described in C++0x [temp.arg.explicit]p3. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8820 | FunctionDecl * |
| 8821 | Sema::ResolveSingleFunctionTemplateSpecialization(OverloadExpr *ovl, |
| 8822 | bool Complain, |
| 8823 | DeclAccessPair *FoundResult) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8824 | // C++ [over.over]p1: |
| 8825 | // [...] [Note: any redundant set of parentheses surrounding the |
| 8826 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8827 | // C++ [over.over]p1: |
| 8828 | // [...] The overloaded function name can be preceded by the & |
| 8829 | // operator. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8830 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8831 | // If we didn't actually find any template-ids, we're done. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8832 | if (!ovl->hasExplicitTemplateArgs()) |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8833 | return 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 8834 | |
| 8835 | TemplateArgumentListInfo ExplicitTemplateArgs; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8836 | ovl->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8837 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8838 | // Look through all of the overloaded functions, searching for one |
| 8839 | // whose type matches exactly. |
| 8840 | FunctionDecl *Matched = 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8841 | for (UnresolvedSetIterator I = ovl->decls_begin(), |
| 8842 | E = ovl->decls_end(); I != E; ++I) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8843 | // C++0x [temp.arg.explicit]p3: |
| 8844 | // [...] In contexts where deduction is done and fails, or in contexts |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8845 | // where deduction is not done, if a template argument list is |
| 8846 | // specified and it, along with any default template arguments, |
| 8847 | // identifies a single function template specialization, then the |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8848 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | 66a8c9a | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 8849 | FunctionTemplateDecl *FunctionTemplate |
| 8850 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8851 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8852 | // C++ [over.over]p2: |
| 8853 | // If the name is a function template, template argument deduction is |
| 8854 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 8855 | // resulting template argument list is used to generate a single |
| 8856 | // function template specialization, which is added to the set of |
| 8857 | // overloaded functions considered. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8858 | FunctionDecl *Specialization = 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8859 | TemplateDeductionInfo Info(Context, ovl->getNameLoc()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8860 | if (TemplateDeductionResult Result |
| 8861 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 8862 | Specialization, Info)) { |
| 8863 | // FIXME: make a note of the failed deduction for diagnostics. |
| 8864 | (void)Result; |
| 8865 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8866 | } |
| 8867 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8868 | assert(Specialization && "no specialization and no error?"); |
| 8869 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8870 | // Multiple matches; we can't resolve to a single declaration. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8871 | if (Matched) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8872 | if (Complain) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8873 | Diag(ovl->getExprLoc(), diag::err_addr_ovl_ambiguous) |
| 8874 | << ovl->getName(); |
| 8875 | NoteAllOverloadCandidates(ovl); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8876 | } |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8877 | return 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8878 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8879 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8880 | Matched = Specialization; |
| 8881 | if (FoundResult) *FoundResult = I.getPair(); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8882 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8883 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8884 | return Matched; |
| 8885 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8886 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8887 | |
| 8888 | |
| 8889 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8890 | // Resolve and fix an overloaded expression that can be resolved |
| 8891 | // because it identifies a single function template specialization. |
| 8892 | // |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8893 | // Last three arguments should only be supplied if Complain = true |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8894 | // |
| 8895 | // Return true if it was logically possible to so resolve the |
| 8896 | // expression, regardless of whether or not it succeeded. Always |
| 8897 | // returns true if 'complain' is set. |
| 8898 | bool Sema::ResolveAndFixSingleFunctionTemplateSpecialization( |
| 8899 | ExprResult &SrcExpr, bool doFunctionPointerConverion, |
| 8900 | bool complain, const SourceRange& OpRangeForComplaining, |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8901 | QualType DestTypeForComplaining, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8902 | unsigned DiagIDForComplaining) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8903 | assert(SrcExpr.get()->getType() == Context.OverloadTy); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8904 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8905 | OverloadExpr::FindResult ovl = OverloadExpr::find(SrcExpr.get()); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8906 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8907 | DeclAccessPair found; |
| 8908 | ExprResult SingleFunctionExpression; |
| 8909 | if (FunctionDecl *fn = ResolveSingleFunctionTemplateSpecialization( |
| 8910 | ovl.Expression, /*complain*/ false, &found)) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8911 | if (DiagnoseUseOfDecl(fn, SrcExpr.get()->getSourceRange().getBegin())) { |
| 8912 | SrcExpr = ExprError(); |
| 8913 | return true; |
| 8914 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8915 | |
| 8916 | // It is only correct to resolve to an instance method if we're |
| 8917 | // resolving a form that's permitted to be a pointer to member. |
| 8918 | // Otherwise we'll end up making a bound member expression, which |
| 8919 | // is illegal in all the contexts we resolve like this. |
| 8920 | if (!ovl.HasFormOfMemberPointer && |
| 8921 | isa<CXXMethodDecl>(fn) && |
| 8922 | cast<CXXMethodDecl>(fn)->isInstance()) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8923 | if (!complain) return false; |
| 8924 | |
| 8925 | Diag(ovl.Expression->getExprLoc(), |
| 8926 | diag::err_bound_member_function) |
| 8927 | << 0 << ovl.Expression->getSourceRange(); |
| 8928 | |
| 8929 | // TODO: I believe we only end up here if there's a mix of |
| 8930 | // static and non-static candidates (otherwise the expression |
| 8931 | // would have 'bound member' type, not 'overload' type). |
| 8932 | // Ideally we would note which candidate was chosen and why |
| 8933 | // the static candidates were rejected. |
| 8934 | SrcExpr = ExprError(); |
| 8935 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8936 | } |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 8937 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8938 | // Fix the expresion to refer to 'fn'. |
| 8939 | SingleFunctionExpression = |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8940 | Owned(FixOverloadedFunctionReference(SrcExpr.take(), found, fn)); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8941 | |
| 8942 | // If desired, do function-to-pointer decay. |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8943 | if (doFunctionPointerConverion) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8944 | SingleFunctionExpression = |
| 8945 | DefaultFunctionArrayLvalueConversion(SingleFunctionExpression.take()); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8946 | if (SingleFunctionExpression.isInvalid()) { |
| 8947 | SrcExpr = ExprError(); |
| 8948 | return true; |
| 8949 | } |
| 8950 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8951 | } |
| 8952 | |
| 8953 | if (!SingleFunctionExpression.isUsable()) { |
| 8954 | if (complain) { |
| 8955 | Diag(OpRangeForComplaining.getBegin(), DiagIDForComplaining) |
| 8956 | << ovl.Expression->getName() |
| 8957 | << DestTypeForComplaining |
| 8958 | << OpRangeForComplaining |
| 8959 | << ovl.Expression->getQualifierLoc().getSourceRange(); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8960 | NoteAllOverloadCandidates(SrcExpr.get()); |
| 8961 | |
| 8962 | SrcExpr = ExprError(); |
| 8963 | return true; |
| 8964 | } |
| 8965 | |
| 8966 | return false; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8967 | } |
| 8968 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8969 | SrcExpr = SingleFunctionExpression; |
| 8970 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8971 | } |
| 8972 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8973 | /// \brief Add a single candidate to the overload set. |
| 8974 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8975 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 8976 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8977 | Expr **Args, unsigned NumArgs, |
| 8978 | OverloadCandidateSet &CandidateSet, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 8979 | bool PartialOverloading, |
| 8980 | bool KnownValid) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8981 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8982 | if (isa<UsingShadowDecl>(Callee)) |
| 8983 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 8984 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8985 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 8986 | if (ExplicitTemplateArgs) { |
| 8987 | assert(!KnownValid && "Explicit template arguments?"); |
| 8988 | return; |
| 8989 | } |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8990 | S.AddOverloadCandidate(Func, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 8991 | false, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8992 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8993 | } |
| 8994 | |
| 8995 | if (FunctionTemplateDecl *FuncTemplate |
| 8996 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8997 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
| 8998 | ExplicitTemplateArgs, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8999 | Args, NumArgs, CandidateSet); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9000 | return; |
| 9001 | } |
| 9002 | |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9003 | assert(!KnownValid && "unhandled case in overloaded call candidate"); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9004 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9005 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9006 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 9007 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9008 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9009 | Expr **Args, unsigned NumArgs, |
| 9010 | OverloadCandidateSet &CandidateSet, |
| 9011 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9012 | |
| 9013 | #ifndef NDEBUG |
| 9014 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 9015 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9016 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9017 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 9018 | // and let Y be the lookup set produced by argument dependent |
| 9019 | // lookup (defined as follows). If X contains |
| 9020 | // |
| 9021 | // -- a declaration of a class member, or |
| 9022 | // |
| 9023 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9024 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9025 | // |
| 9026 | // -- a declaration that is neither a function or a function |
| 9027 | // template |
| 9028 | // |
| 9029 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9030 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9031 | if (ULE->requiresADL()) { |
| 9032 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 9033 | E = ULE->decls_end(); I != E; ++I) { |
| 9034 | assert(!(*I)->getDeclContext()->isRecord()); |
| 9035 | assert(isa<UsingShadowDecl>(*I) || |
| 9036 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 9037 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9038 | } |
| 9039 | } |
| 9040 | #endif |
| 9041 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9042 | // It would be nice to avoid this copy. |
| 9043 | TemplateArgumentListInfo TABuffer; |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 9044 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9045 | if (ULE->hasExplicitTemplateArgs()) { |
| 9046 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 9047 | ExplicitTemplateArgs = &TABuffer; |
| 9048 | } |
| 9049 | |
| 9050 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 9051 | E = ULE->decls_end(); I != E; ++I) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9052 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9053 | Args, NumArgs, CandidateSet, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9054 | PartialOverloading, /*KnownValid*/ true); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9055 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9056 | if (ULE->requiresADL()) |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9057 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
| 9058 | Args, NumArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9059 | ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9060 | CandidateSet, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 9061 | PartialOverloading, |
| 9062 | ULE->isStdAssociatedNamespace()); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9063 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9064 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9065 | /// Attempt to recover from an ill-formed use of a non-dependent name in a |
| 9066 | /// template, where the non-dependent name was declared after the template |
| 9067 | /// was defined. This is common in code written for a compilers which do not |
| 9068 | /// correctly implement two-stage name lookup. |
| 9069 | /// |
| 9070 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 9071 | static bool |
| 9072 | DiagnoseTwoPhaseLookup(Sema &SemaRef, SourceLocation FnLoc, |
| 9073 | const CXXScopeSpec &SS, LookupResult &R, |
| 9074 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 9075 | Expr **Args, unsigned NumArgs) { |
| 9076 | if (SemaRef.ActiveTemplateInstantiations.empty() || !SS.isEmpty()) |
| 9077 | return false; |
| 9078 | |
| 9079 | for (DeclContext *DC = SemaRef.CurContext; DC; DC = DC->getParent()) { |
| 9080 | SemaRef.LookupQualifiedName(R, DC); |
| 9081 | |
| 9082 | if (!R.empty()) { |
| 9083 | R.suppressDiagnostics(); |
| 9084 | |
| 9085 | if (isa<CXXRecordDecl>(DC)) { |
| 9086 | // Don't diagnose names we find in classes; we get much better |
| 9087 | // diagnostics for these from DiagnoseEmptyLookup. |
| 9088 | R.clear(); |
| 9089 | return false; |
| 9090 | } |
| 9091 | |
| 9092 | OverloadCandidateSet Candidates(FnLoc); |
| 9093 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 9094 | AddOverloadedCallCandidate(SemaRef, I.getPair(), |
| 9095 | ExplicitTemplateArgs, Args, NumArgs, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9096 | Candidates, false, /*KnownValid*/ false); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9097 | |
| 9098 | OverloadCandidateSet::iterator Best; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9099 | if (Candidates.BestViableFunction(SemaRef, FnLoc, Best) != OR_Success) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9100 | // No viable functions. Don't bother the user with notes for functions |
| 9101 | // which don't work and shouldn't be found anyway. |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9102 | R.clear(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9103 | return false; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9104 | } |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9105 | |
| 9106 | // Find the namespaces where ADL would have looked, and suggest |
| 9107 | // declaring the function there instead. |
| 9108 | Sema::AssociatedNamespaceSet AssociatedNamespaces; |
| 9109 | Sema::AssociatedClassSet AssociatedClasses; |
| 9110 | SemaRef.FindAssociatedClassesAndNamespaces(Args, NumArgs, |
| 9111 | AssociatedNamespaces, |
| 9112 | AssociatedClasses); |
| 9113 | // Never suggest declaring a function within namespace 'std'. |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9114 | Sema::AssociatedNamespaceSet SuggestedNamespaces; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9115 | if (DeclContext *Std = SemaRef.getStdNamespace()) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9116 | for (Sema::AssociatedNamespaceSet::iterator |
| 9117 | it = AssociatedNamespaces.begin(), |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9118 | end = AssociatedNamespaces.end(); it != end; ++it) { |
| 9119 | if (!Std->Encloses(*it)) |
| 9120 | SuggestedNamespaces.insert(*it); |
| 9121 | } |
Chandler Carruth | 45cad4a | 2011-06-08 10:13:17 +0000 | [diff] [blame] | 9122 | } else { |
| 9123 | // Lacking the 'std::' namespace, use all of the associated namespaces. |
| 9124 | SuggestedNamespaces = AssociatedNamespaces; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9125 | } |
| 9126 | |
| 9127 | SemaRef.Diag(R.getNameLoc(), diag::err_not_found_by_two_phase_lookup) |
| 9128 | << R.getLookupName(); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9129 | if (SuggestedNamespaces.empty()) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9130 | SemaRef.Diag(Best->Function->getLocation(), |
| 9131 | diag::note_not_found_by_two_phase_lookup) |
| 9132 | << R.getLookupName() << 0; |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9133 | } else if (SuggestedNamespaces.size() == 1) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9134 | SemaRef.Diag(Best->Function->getLocation(), |
| 9135 | diag::note_not_found_by_two_phase_lookup) |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9136 | << R.getLookupName() << 1 << *SuggestedNamespaces.begin(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9137 | } else { |
| 9138 | // FIXME: It would be useful to list the associated namespaces here, |
| 9139 | // but the diagnostics infrastructure doesn't provide a way to produce |
| 9140 | // a localized representation of a list of items. |
| 9141 | SemaRef.Diag(Best->Function->getLocation(), |
| 9142 | diag::note_not_found_by_two_phase_lookup) |
| 9143 | << R.getLookupName() << 2; |
| 9144 | } |
| 9145 | |
| 9146 | // Try to recover by calling this function. |
| 9147 | return true; |
| 9148 | } |
| 9149 | |
| 9150 | R.clear(); |
| 9151 | } |
| 9152 | |
| 9153 | return false; |
| 9154 | } |
| 9155 | |
| 9156 | /// Attempt to recover from ill-formed use of a non-dependent operator in a |
| 9157 | /// template, where the non-dependent operator was declared after the template |
| 9158 | /// was defined. |
| 9159 | /// |
| 9160 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 9161 | static bool |
| 9162 | DiagnoseTwoPhaseOperatorLookup(Sema &SemaRef, OverloadedOperatorKind Op, |
| 9163 | SourceLocation OpLoc, |
| 9164 | Expr **Args, unsigned NumArgs) { |
| 9165 | DeclarationName OpName = |
| 9166 | SemaRef.Context.DeclarationNames.getCXXOperatorName(Op); |
| 9167 | LookupResult R(SemaRef, OpName, OpLoc, Sema::LookupOperatorName); |
| 9168 | return DiagnoseTwoPhaseLookup(SemaRef, OpLoc, CXXScopeSpec(), R, |
| 9169 | /*ExplicitTemplateArgs=*/0, Args, NumArgs); |
| 9170 | } |
| 9171 | |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 9172 | namespace { |
| 9173 | // Callback to limit the allowed keywords and to only accept typo corrections |
| 9174 | // that are keywords or whose decls refer to functions (or template functions) |
| 9175 | // that accept the given number of arguments. |
| 9176 | class RecoveryCallCCC : public CorrectionCandidateCallback { |
| 9177 | public: |
| 9178 | RecoveryCallCCC(Sema &SemaRef, unsigned NumArgs, bool HasExplicitTemplateArgs) |
| 9179 | : NumArgs(NumArgs), HasExplicitTemplateArgs(HasExplicitTemplateArgs) { |
| 9180 | WantTypeSpecifiers = SemaRef.getLangOptions().CPlusPlus; |
| 9181 | WantRemainingKeywords = false; |
| 9182 | } |
| 9183 | |
| 9184 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 9185 | if (!candidate.getCorrectionDecl()) |
| 9186 | return candidate.isKeyword(); |
| 9187 | |
| 9188 | for (TypoCorrection::const_decl_iterator DI = candidate.begin(), |
| 9189 | DIEnd = candidate.end(); DI != DIEnd; ++DI) { |
| 9190 | FunctionDecl *FD = 0; |
| 9191 | NamedDecl *ND = (*DI)->getUnderlyingDecl(); |
| 9192 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 9193 | FD = FTD->getTemplatedDecl(); |
| 9194 | if (!HasExplicitTemplateArgs && !FD) { |
| 9195 | if (!(FD = dyn_cast<FunctionDecl>(ND)) && isa<ValueDecl>(ND)) { |
| 9196 | // If the Decl is neither a function nor a template function, |
| 9197 | // determine if it is a pointer or reference to a function. If so, |
| 9198 | // check against the number of arguments expected for the pointee. |
| 9199 | QualType ValType = cast<ValueDecl>(ND)->getType(); |
| 9200 | if (ValType->isAnyPointerType() || ValType->isReferenceType()) |
| 9201 | ValType = ValType->getPointeeType(); |
| 9202 | if (const FunctionProtoType *FPT = ValType->getAs<FunctionProtoType>()) |
| 9203 | if (FPT->getNumArgs() == NumArgs) |
| 9204 | return true; |
| 9205 | } |
| 9206 | } |
| 9207 | if (FD && FD->getNumParams() >= NumArgs && |
| 9208 | FD->getMinRequiredArguments() <= NumArgs) |
| 9209 | return true; |
| 9210 | } |
| 9211 | return false; |
| 9212 | } |
| 9213 | |
| 9214 | private: |
| 9215 | unsigned NumArgs; |
| 9216 | bool HasExplicitTemplateArgs; |
| 9217 | }; |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 9218 | |
| 9219 | // Callback that effectively disabled typo correction |
| 9220 | class NoTypoCorrectionCCC : public CorrectionCandidateCallback { |
| 9221 | public: |
| 9222 | NoTypoCorrectionCCC() { |
| 9223 | WantTypeSpecifiers = false; |
| 9224 | WantExpressionKeywords = false; |
| 9225 | WantCXXNamedCasts = false; |
| 9226 | WantRemainingKeywords = false; |
| 9227 | } |
| 9228 | |
| 9229 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 9230 | return false; |
| 9231 | } |
| 9232 | }; |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 9233 | } |
| 9234 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9235 | /// Attempts to recover from a call where no functions were found. |
| 9236 | /// |
| 9237 | /// Returns true if new candidates were found. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9238 | static ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 9239 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9240 | UnresolvedLookupExpr *ULE, |
| 9241 | SourceLocation LParenLoc, |
| 9242 | Expr **Args, unsigned NumArgs, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9243 | SourceLocation RParenLoc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 9244 | bool EmptyLookup, bool AllowTypoCorrection) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9245 | |
| 9246 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 9247 | SS.Adopt(ULE->getQualifierLoc()); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9248 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9249 | TemplateArgumentListInfo TABuffer; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9250 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9251 | if (ULE->hasExplicitTemplateArgs()) { |
| 9252 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 9253 | ExplicitTemplateArgs = &TABuffer; |
| 9254 | } |
| 9255 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9256 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 9257 | Sema::LookupOrdinaryName); |
Kaelyn Uhrain | 60a09dc | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 9258 | RecoveryCallCCC Validator(SemaRef, NumArgs, ExplicitTemplateArgs != 0); |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 9259 | NoTypoCorrectionCCC RejectAll; |
| 9260 | CorrectionCandidateCallback *CCC = AllowTypoCorrection ? |
| 9261 | (CorrectionCandidateCallback*)&Validator : |
| 9262 | (CorrectionCandidateCallback*)&RejectAll; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9263 | if (!DiagnoseTwoPhaseLookup(SemaRef, Fn->getExprLoc(), SS, R, |
| 9264 | ExplicitTemplateArgs, Args, NumArgs) && |
| 9265 | (!EmptyLookup || |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 9266 | SemaRef.DiagnoseEmptyLookup(S, SS, R, *CCC, |
Kaelyn Uhrain | ace5e76 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 9267 | ExplicitTemplateArgs, Args, NumArgs))) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9268 | return ExprError(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9269 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9270 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 9271 | |
| 9272 | // Build an implicit member call if appropriate. Just drop the |
| 9273 | // casts and such from the call, we don't really care. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9274 | ExprResult NewFn = ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9275 | if ((*R.begin())->isCXXClassMember()) |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 9276 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, |
| 9277 | ExplicitTemplateArgs); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9278 | else if (ExplicitTemplateArgs) |
| 9279 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 9280 | else |
| 9281 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 9282 | |
| 9283 | if (NewFn.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9284 | return ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9285 | |
| 9286 | // This shouldn't cause an infinite loop because we're giving it |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9287 | // an expression with viable lookup results, which should never |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9288 | // end up here. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9289 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, NewFn.take(), LParenLoc, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 9290 | MultiExprArg(Args, NumArgs), RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9291 | } |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 9292 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9293 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 9294 | /// (which eventually refers to the declaration Func) and the call |
| 9295 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 9296 | /// to a specific function. If overload resolution succeeds, returns |
| 9297 | /// the function declaration produced by overload |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 9298 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9299 | /// arguments and Fn, and returns NULL. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9300 | ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 9301 | Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, UnresolvedLookupExpr *ULE, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9302 | SourceLocation LParenLoc, |
| 9303 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 9304 | SourceLocation RParenLoc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 9305 | Expr *ExecConfig, |
| 9306 | bool AllowTypoCorrection) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9307 | #ifndef NDEBUG |
| 9308 | if (ULE->requiresADL()) { |
| 9309 | // To do ADL, we must have found an unqualified name. |
| 9310 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 9311 | |
| 9312 | // We don't perform ADL for implicit declarations of builtins. |
| 9313 | // Verify that this was correctly set up. |
| 9314 | FunctionDecl *F; |
| 9315 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 9316 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 9317 | F->getBuiltinID() && F->isImplicit()) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 9318 | llvm_unreachable("performing ADL for builtin"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9319 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9320 | // We don't perform ADL in C. |
| 9321 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 9322 | } else |
| 9323 | assert(!ULE->isStdAssociatedNamespace() && |
| 9324 | "std is associated namespace but not doing ADL"); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9325 | #endif |
| 9326 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9327 | UnbridgedCastsSet UnbridgedCasts; |
| 9328 | if (checkArgPlaceholdersForOverload(*this, Args, NumArgs, UnbridgedCasts)) |
| 9329 | return ExprError(); |
| 9330 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9331 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 9332 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9333 | // Add the functions denoted by the callee to the set of candidate |
| 9334 | // functions, including those from argument-dependent lookup. |
| 9335 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9336 | |
| 9337 | // If we found nothing, try to recover. |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9338 | // BuildRecoveryCallExpr diagnoses the error itself, so we just bail |
| 9339 | // out if it fails. |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 9340 | if (CandidateSet.empty()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 9341 | // In Microsoft mode, if we are inside a template class member function then |
| 9342 | // create a type dependent CallExpr. The goal is to postpone name lookup |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 9343 | // to instantiation time to be able to search into type dependent base |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 9344 | // classes. |
Francois Pichet | ef04ecf | 2011-11-11 00:12:11 +0000 | [diff] [blame] | 9345 | if (getLangOptions().MicrosoftMode && CurContext->isDependentContext() && |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 9346 | (isa<FunctionDecl>(CurContext) || isa<CXXRecordDecl>(CurContext))) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 9347 | CallExpr *CE = new (Context) CallExpr(Context, Fn, Args, NumArgs, |
| 9348 | Context.DependentTy, VK_RValue, |
| 9349 | RParenLoc); |
| 9350 | CE->setTypeDependent(true); |
| 9351 | return Owned(CE); |
| 9352 | } |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 9353 | return BuildRecoveryCallExpr(*this, S, Fn, ULE, LParenLoc, Args, NumArgs, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 9354 | RParenLoc, /*EmptyLookup=*/true, |
| 9355 | AllowTypoCorrection); |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 9356 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9357 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9358 | UnbridgedCasts.restore(); |
| 9359 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9360 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9361 | switch (CandidateSet.BestViableFunction(*this, Fn->getLocStart(), Best)) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9362 | case OR_Success: { |
| 9363 | FunctionDecl *FDecl = Best->Function; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 9364 | MarkDeclarationReferenced(Fn->getExprLoc(), FDecl); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9365 | CheckUnresolvedLookupAccess(ULE, Best->FoundDecl); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9366 | DiagnoseUseOfDecl(FDecl, ULE->getNameLoc()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9367 | Fn = FixOverloadedFunctionReference(Fn, Best->FoundDecl, FDecl); |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 9368 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 9369 | ExecConfig); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9370 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9371 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9372 | case OR_No_Viable_Function: { |
| 9373 | // Try to recover by looking for viable functions which the user might |
| 9374 | // have meant to call. |
| 9375 | ExprResult Recovery = BuildRecoveryCallExpr(*this, S, Fn, ULE, LParenLoc, |
| 9376 | Args, NumArgs, RParenLoc, |
Kaelyn Uhrain | 3943b1c | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 9377 | /*EmptyLookup=*/false, |
| 9378 | AllowTypoCorrection); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9379 | if (!Recovery.isInvalid()) |
| 9380 | return Recovery; |
| 9381 | |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 9382 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9383 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9384 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9385 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9386 | break; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9387 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9388 | |
| 9389 | case OR_Ambiguous: |
| 9390 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9391 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9392 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9393 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9394 | |
| 9395 | case OR_Deleted: |
Fariborz Jahanian | 2b982b7 | 2011-02-25 18:38:59 +0000 | [diff] [blame] | 9396 | { |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 9397 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 9398 | << Best->Function->isDeleted() |
| 9399 | << ULE->getName() |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 9400 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 9401 | << Fn->getSourceRange(); |
Fariborz Jahanian | 2b982b7 | 2011-02-25 18:38:59 +0000 | [diff] [blame] | 9402 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Argyrios Kyrtzidis | 0d579b6 | 2011-11-04 15:58:13 +0000 | [diff] [blame] | 9403 | |
| 9404 | // We emitted an error for the unvailable/deleted function call but keep |
| 9405 | // the call in the AST. |
| 9406 | FunctionDecl *FDecl = Best->Function; |
| 9407 | Fn = FixOverloadedFunctionReference(Fn, Best->FoundDecl, FDecl); |
| 9408 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, |
| 9409 | RParenLoc, ExecConfig); |
Fariborz Jahanian | 2b982b7 | 2011-02-25 18:38:59 +0000 | [diff] [blame] | 9410 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9411 | } |
| 9412 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 9413 | // Overload resolution failed. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9414 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9415 | } |
| 9416 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9417 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 9418 | return Functions.size() > 1 || |
| 9419 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 9420 | } |
| 9421 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9422 | /// \brief Create a unary operation that may resolve to an overloaded |
| 9423 | /// operator. |
| 9424 | /// |
| 9425 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 9426 | /// |
| 9427 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 9428 | /// operator. |
| 9429 | /// |
| 9430 | /// \param Functions The set of non-member functions that will be |
| 9431 | /// considered by overload resolution. The caller needs to build this |
| 9432 | /// set based on the context using, e.g., |
| 9433 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 9434 | /// set should not contain any member functions; those will be added |
| 9435 | /// by CreateOverloadedUnaryOp(). |
| 9436 | /// |
| 9437 | /// \param input The input argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9438 | ExprResult |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9439 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 9440 | const UnresolvedSetImpl &Fns, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9441 | Expr *Input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9442 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9443 | |
| 9444 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 9445 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 9446 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9447 | // TODO: provide better source location info. |
| 9448 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9449 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9450 | if (checkPlaceholderForOverload(*this, Input)) |
| 9451 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 9452 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9453 | Expr *Args[2] = { Input, 0 }; |
| 9454 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9455 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9456 | // For post-increment and post-decrement, add the implicit '0' as |
| 9457 | // the second argument, so that we know this is a post-increment or |
| 9458 | // post-decrement. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9459 | if (Opc == UO_PostInc || Opc == UO_PostDec) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9460 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 9461 | Args[1] = IntegerLiteral::Create(Context, Zero, Context.IntTy, |
| 9462 | SourceLocation()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9463 | NumArgs = 2; |
| 9464 | } |
| 9465 | |
| 9466 | if (Input->isTypeDependent()) { |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 9467 | if (Fns.empty()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9468 | return Owned(new (Context) UnaryOperator(Input, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9469 | Opc, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 9470 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9471 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 9472 | OpLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9473 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9474 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9475 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 9476 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 9477 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 9478 | /*ADL*/ true, IsOverloaded(Fns), |
| 9479 | Fns.begin(), Fns.end()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9480 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 9481 | &Args[0], NumArgs, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9482 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9483 | VK_RValue, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9484 | OpLoc)); |
| 9485 | } |
| 9486 | |
| 9487 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9488 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9489 | |
| 9490 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9491 | AddFunctionCandidates(Fns, &Args[0], NumArgs, CandidateSet, false); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9492 | |
| 9493 | // Add operator candidates that are member functions. |
| 9494 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 9495 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9496 | // Add candidates from ADL. |
| 9497 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Douglas Gregor | dc81c88 | 2010-02-05 05:15:43 +0000 | [diff] [blame] | 9498 | Args, NumArgs, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9499 | /*ExplicitTemplateArgs*/ 0, |
| 9500 | CandidateSet); |
| 9501 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9502 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 9503 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9504 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9505 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 9506 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9507 | // Perform overload resolution. |
| 9508 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9509 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9510 | case OR_Success: { |
| 9511 | // We found a built-in operator or an overloaded operator. |
| 9512 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9513 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9514 | if (FnDecl) { |
| 9515 | // We matched an overloaded operator. Build a call to that |
| 9516 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9517 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 9518 | MarkDeclarationReferenced(OpLoc, FnDecl); |
| 9519 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9520 | // Convert the arguments. |
| 9521 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9522 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 9523 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9524 | ExprResult InputRes = |
| 9525 | PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 9526 | Best->FoundDecl, Method); |
| 9527 | if (InputRes.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9528 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9529 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9530 | } else { |
| 9531 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9532 | ExprResult InputInit |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 9533 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 9534 | Context, |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 9535 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9536 | SourceLocation(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9537 | Input); |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 9538 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9539 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9540 | Input = InputInit.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9541 | } |
| 9542 | |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 9543 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 9544 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9545 | // Determine the result type. |
| 9546 | QualType ResultTy = FnDecl->getResultType(); |
| 9547 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 9548 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9549 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9550 | // Build the actual expression node. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9551 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
| 9552 | HadMultipleCandidates); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9553 | if (FnExpr.isInvalid()) |
| 9554 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9555 | |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 9556 | Args[0] = Input; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9557 | CallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9558 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9559 | Args, NumArgs, ResultTy, VK, OpLoc); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 9560 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9561 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 9562 | FnDecl)) |
| 9563 | return ExprError(); |
| 9564 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9565 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9566 | } else { |
| 9567 | // We matched a built-in operator. Convert the arguments, then |
| 9568 | // break out so that we will build the appropriate built-in |
| 9569 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9570 | ExprResult InputRes = |
| 9571 | PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 9572 | Best->Conversions[0], AA_Passing); |
| 9573 | if (InputRes.isInvalid()) |
| 9574 | return ExprError(); |
| 9575 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9576 | break; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9577 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9578 | } |
| 9579 | |
| 9580 | case OR_No_Viable_Function: |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9581 | // This is an erroneous use of an operator which can be overloaded by |
| 9582 | // a non-member function. Check for non-member operators which were |
| 9583 | // defined too late to be candidates. |
| 9584 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, Args, NumArgs)) |
| 9585 | // FIXME: Recover by calling the found function. |
| 9586 | return ExprError(); |
| 9587 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9588 | // No viable function; fall through to handling this as a |
| 9589 | // built-in operator, which will produce an error message for us. |
| 9590 | break; |
| 9591 | |
| 9592 | case OR_Ambiguous: |
| 9593 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 9594 | << UnaryOperator::getOpcodeStr(Opc) |
| 9595 | << Input->getType() |
| 9596 | << Input->getSourceRange(); |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 9597 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9598 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
| 9599 | return ExprError(); |
| 9600 | |
| 9601 | case OR_Deleted: |
| 9602 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 9603 | << Best->Function->isDeleted() |
| 9604 | << UnaryOperator::getOpcodeStr(Opc) |
| 9605 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 9606 | << Input->getSourceRange(); |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 9607 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs, |
| 9608 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9609 | return ExprError(); |
| 9610 | } |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9611 | |
| 9612 | // Either we found no viable overloaded operator or we matched a |
| 9613 | // built-in operator. In either case, fall through to trying to |
| 9614 | // build a built-in operation. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9615 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9616 | } |
| 9617 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9618 | /// \brief Create a binary operation that may resolve to an overloaded |
| 9619 | /// operator. |
| 9620 | /// |
| 9621 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 9622 | /// |
| 9623 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 9624 | /// operator. |
| 9625 | /// |
| 9626 | /// \param Functions The set of non-member functions that will be |
| 9627 | /// considered by overload resolution. The caller needs to build this |
| 9628 | /// set based on the context using, e.g., |
| 9629 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 9630 | /// set should not contain any member functions; those will be added |
| 9631 | /// by CreateOverloadedBinOp(). |
| 9632 | /// |
| 9633 | /// \param LHS Left-hand argument. |
| 9634 | /// \param RHS Right-hand argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9635 | ExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9636 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9637 | unsigned OpcIn, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9638 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9639 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9640 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 9641 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9642 | |
| 9643 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 9644 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 9645 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 9646 | |
| 9647 | // If either side is type-dependent, create an appropriate dependent |
| 9648 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 9649 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9650 | if (Fns.empty()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9651 | // If there are no functions to store, just build a dependent |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9652 | // BinaryOperator or CompoundAssignment. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9653 | if (Opc <= BO_Assign || Opc > BO_OrAssign) |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9654 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9655 | Context.DependentTy, |
| 9656 | VK_RValue, OK_Ordinary, |
| 9657 | OpLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9658 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9659 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 9660 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9661 | VK_LValue, |
| 9662 | OK_Ordinary, |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9663 | Context.DependentTy, |
| 9664 | Context.DependentTy, |
| 9665 | OpLoc)); |
| 9666 | } |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9667 | |
| 9668 | // FIXME: save results of ADL from here? |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9669 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9670 | // TODO: provide better source location info in DNLoc component. |
| 9671 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9672 | UnresolvedLookupExpr *Fn |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 9673 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
| 9674 | NestedNameSpecifierLoc(), OpNameInfo, |
| 9675 | /*ADL*/ true, IsOverloaded(Fns), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 9676 | Fns.begin(), Fns.end()); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9677 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9678 | Args, 2, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9679 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9680 | VK_RValue, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9681 | OpLoc)); |
| 9682 | } |
| 9683 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9684 | // Always do placeholder-like conversions on the RHS. |
| 9685 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 9686 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 9687 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 9688 | // Do placeholder-like conversion on the LHS; note that we should |
| 9689 | // not get here with a PseudoObject LHS. |
| 9690 | assert(Args[0]->getObjectKind() != OK_ObjCProperty); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9691 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 9692 | return ExprError(); |
| 9693 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 9694 | // If this is the assignment operator, we only perform overload resolution |
| 9695 | // if the left-hand side is a class or enumeration type. This is actually |
| 9696 | // a hack. The standard requires that we do overload resolution between the |
| 9697 | // various built-in candidates, but as DR507 points out, this can lead to |
| 9698 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 9699 | // Note that we go the traditional code path for compound assignment forms. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9700 | if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 9701 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9702 | |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 9703 | // If this is the .* operator, which is not overloadable, just |
| 9704 | // create a built-in binary operator. |
| 9705 | if (Opc == BO_PtrMemD) |
| 9706 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 9707 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9708 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9709 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9710 | |
| 9711 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9712 | AddFunctionCandidates(Fns, Args, 2, CandidateSet, false); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9713 | |
| 9714 | // Add operator candidates that are member functions. |
| 9715 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 9716 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9717 | // Add candidates from ADL. |
| 9718 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
| 9719 | Args, 2, |
| 9720 | /*ExplicitTemplateArgs*/ 0, |
| 9721 | CandidateSet); |
| 9722 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9723 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 9724 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9725 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9726 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 9727 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9728 | // Perform overload resolution. |
| 9729 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9730 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 9731 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9732 | // We found a built-in operator or an overloaded operator. |
| 9733 | FunctionDecl *FnDecl = Best->Function; |
| 9734 | |
| 9735 | if (FnDecl) { |
| 9736 | // We matched an overloaded operator. Build a call to that |
| 9737 | // operator. |
| 9738 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 9739 | MarkDeclarationReferenced(OpLoc, FnDecl); |
| 9740 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9741 | // Convert the arguments. |
| 9742 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 9743 | // Best->Access is only meaningful for class members. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9744 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 9745 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 9746 | ExprResult Arg1 = |
| 9747 | PerformCopyInitialization( |
| 9748 | InitializedEntity::InitializeParameter(Context, |
| 9749 | FnDecl->getParamDecl(0)), |
| 9750 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 9751 | if (Arg1.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9752 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 9753 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9754 | ExprResult Arg0 = |
| 9755 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 9756 | Best->FoundDecl, Method); |
| 9757 | if (Arg0.isInvalid()) |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 9758 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9759 | Args[0] = Arg0.takeAs<Expr>(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 9760 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9761 | } else { |
| 9762 | // Convert the arguments. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 9763 | ExprResult Arg0 = PerformCopyInitialization( |
| 9764 | InitializedEntity::InitializeParameter(Context, |
| 9765 | FnDecl->getParamDecl(0)), |
| 9766 | SourceLocation(), Owned(Args[0])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 9767 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9768 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 9769 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 9770 | ExprResult Arg1 = |
| 9771 | PerformCopyInitialization( |
| 9772 | InitializedEntity::InitializeParameter(Context, |
| 9773 | FnDecl->getParamDecl(1)), |
| 9774 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 9775 | if (Arg1.isInvalid()) |
| 9776 | return ExprError(); |
| 9777 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 9778 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9779 | } |
| 9780 | |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 9781 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 9782 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9783 | // Determine the result type. |
| 9784 | QualType ResultTy = FnDecl->getResultType(); |
| 9785 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 9786 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9787 | |
| 9788 | // Build the actual expression node. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9789 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
| 9790 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9791 | if (FnExpr.isInvalid()) |
| 9792 | return ExprError(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9793 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9794 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9795 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9796 | Args, 2, ResultTy, VK, OpLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9797 | |
| 9798 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 9799 | FnDecl)) |
| 9800 | return ExprError(); |
| 9801 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9802 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9803 | } else { |
| 9804 | // We matched a built-in operator. Convert the arguments, then |
| 9805 | // break out so that we will build the appropriate built-in |
| 9806 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9807 | ExprResult ArgsRes0 = |
| 9808 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 9809 | Best->Conversions[0], AA_Passing); |
| 9810 | if (ArgsRes0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9811 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9812 | Args[0] = ArgsRes0.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9813 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9814 | ExprResult ArgsRes1 = |
| 9815 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 9816 | Best->Conversions[1], AA_Passing); |
| 9817 | if (ArgsRes1.isInvalid()) |
| 9818 | return ExprError(); |
| 9819 | Args[1] = ArgsRes1.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9820 | break; |
| 9821 | } |
| 9822 | } |
| 9823 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 9824 | case OR_No_Viable_Function: { |
| 9825 | // C++ [over.match.oper]p9: |
| 9826 | // If the operator is the operator , [...] and there are no |
| 9827 | // viable functions, then the operator is assumed to be the |
| 9828 | // built-in operator and interpreted according to clause 5. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9829 | if (Opc == BO_Comma) |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 9830 | break; |
| 9831 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 9832 | // For class as left operand for assignment or compound assigment |
| 9833 | // operator do not fall through to handling in built-in, but report that |
| 9834 | // no overloaded assignment operator found |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9835 | ExprResult Result = ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9836 | if (Args[0]->getType()->isRecordType() && |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9837 | Opc >= BO_Assign && Opc <= BO_OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 9838 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 9839 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 9840 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 9841 | } else { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9842 | // This is an erroneous use of an operator which can be overloaded by |
| 9843 | // a non-member function. Check for non-member operators which were |
| 9844 | // defined too late to be candidates. |
| 9845 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, Args, 2)) |
| 9846 | // FIXME: Recover by calling the found function. |
| 9847 | return ExprError(); |
| 9848 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 9849 | // No viable function; try to create a built-in operation, which will |
| 9850 | // produce an error. Then, show the non-viable candidates. |
| 9851 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 9852 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9853 | assert(Result.isInvalid() && |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 9854 | "C++ binary operator overloading is missing candidates!"); |
| 9855 | if (Result.isInvalid()) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9856 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 9857 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 9858 | return move(Result); |
| 9859 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9860 | |
| 9861 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 9862 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_binary) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9863 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 9864 | << Args[0]->getType() << Args[1]->getType() |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 9865 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9866 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, 2, |
| 9867 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9868 | return ExprError(); |
| 9869 | |
| 9870 | case OR_Deleted: |
| 9871 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 9872 | << Best->Function->isDeleted() |
| 9873 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 9874 | << getDeletedOrUnavailableSuffix(Best->Function) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 9875 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 9876 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 9877 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9878 | return ExprError(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9879 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9880 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 9881 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 9882 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9883 | } |
| 9884 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9885 | ExprResult |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9886 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 9887 | SourceLocation RLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9888 | Expr *Base, Expr *Idx) { |
| 9889 | Expr *Args[2] = { Base, Idx }; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9890 | DeclarationName OpName = |
| 9891 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 9892 | |
| 9893 | // If either side is type-dependent, create an appropriate dependent |
| 9894 | // expression. |
| 9895 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 9896 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9897 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9898 | // CHECKME: no 'operator' keyword? |
| 9899 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
| 9900 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9901 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 9902 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 9903 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 9904 | /*ADL*/ true, /*Overloaded*/ false, |
| 9905 | UnresolvedSetIterator(), |
| 9906 | UnresolvedSetIterator()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 9907 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9908 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9909 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 9910 | Args, 2, |
| 9911 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9912 | VK_RValue, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9913 | RLoc)); |
| 9914 | } |
| 9915 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9916 | // Handle placeholders on both operands. |
| 9917 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 9918 | return ExprError(); |
| 9919 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 9920 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 9921 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9922 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9923 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9924 | |
| 9925 | // Subscript can only be overloaded as a member function. |
| 9926 | |
| 9927 | // Add operator candidates that are member functions. |
| 9928 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 9929 | |
| 9930 | // Add builtin operator candidates. |
| 9931 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 9932 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9933 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 9934 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9935 | // Perform overload resolution. |
| 9936 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9937 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9938 | case OR_Success: { |
| 9939 | // We found a built-in operator or an overloaded operator. |
| 9940 | FunctionDecl *FnDecl = Best->Function; |
| 9941 | |
| 9942 | if (FnDecl) { |
| 9943 | // We matched an overloaded operator. Build a call to that |
| 9944 | // operator. |
| 9945 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 9946 | MarkDeclarationReferenced(LLoc, FnDecl); |
| 9947 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9948 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 9949 | DiagnoseUseOfDecl(Best->FoundDecl, LLoc); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9950 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9951 | // Convert the arguments. |
| 9952 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9953 | ExprResult Arg0 = |
| 9954 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 9955 | Best->FoundDecl, Method); |
| 9956 | if (Arg0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9957 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9958 | Args[0] = Arg0.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9959 | |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 9960 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9961 | ExprResult InputInit |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 9962 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 9963 | Context, |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 9964 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9965 | SourceLocation(), |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 9966 | Owned(Args[1])); |
| 9967 | if (InputInit.isInvalid()) |
| 9968 | return ExprError(); |
| 9969 | |
| 9970 | Args[1] = InputInit.takeAs<Expr>(); |
| 9971 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9972 | // Determine the result type |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9973 | QualType ResultTy = FnDecl->getResultType(); |
| 9974 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 9975 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9976 | |
| 9977 | // Build the actual expression node. |
Douglas Gregor | 5b8968c | 2011-07-15 16:25:15 +0000 | [diff] [blame] | 9978 | DeclarationNameLoc LocInfo; |
| 9979 | LocInfo.CXXOperatorName.BeginOpNameLoc = LLoc.getRawEncoding(); |
| 9980 | LocInfo.CXXOperatorName.EndOpNameLoc = RLoc.getRawEncoding(); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9981 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
| 9982 | HadMultipleCandidates, |
| 9983 | LLoc, LocInfo); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9984 | if (FnExpr.isInvalid()) |
| 9985 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9986 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9987 | CXXOperatorCallExpr *TheCall = |
| 9988 | new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9989 | FnExpr.take(), Args, 2, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9990 | ResultTy, VK, RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9991 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9992 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9993 | FnDecl)) |
| 9994 | return ExprError(); |
| 9995 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9996 | return MaybeBindToTemporary(TheCall); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9997 | } else { |
| 9998 | // We matched a built-in operator. Convert the arguments, then |
| 9999 | // break out so that we will build the appropriate built-in |
| 10000 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10001 | ExprResult ArgsRes0 = |
| 10002 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 10003 | Best->Conversions[0], AA_Passing); |
| 10004 | if (ArgsRes0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10005 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10006 | Args[0] = ArgsRes0.take(); |
| 10007 | |
| 10008 | ExprResult ArgsRes1 = |
| 10009 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 10010 | Best->Conversions[1], AA_Passing); |
| 10011 | if (ArgsRes1.isInvalid()) |
| 10012 | return ExprError(); |
| 10013 | Args[1] = ArgsRes1.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10014 | |
| 10015 | break; |
| 10016 | } |
| 10017 | } |
| 10018 | |
| 10019 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10020 | if (CandidateSet.empty()) |
| 10021 | Diag(LLoc, diag::err_ovl_no_oper) |
| 10022 | << Args[0]->getType() << /*subscript*/ 0 |
| 10023 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10024 | else |
| 10025 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 10026 | << Args[0]->getType() |
| 10027 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10028 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 10029 | "[]", LLoc); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10030 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10031 | } |
| 10032 | |
| 10033 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10034 | Diag(LLoc, diag::err_ovl_ambiguous_oper_binary) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10035 | << "[]" |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10036 | << Args[0]->getType() << Args[1]->getType() |
| 10037 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10038 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, 2, |
| 10039 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10040 | return ExprError(); |
| 10041 | |
| 10042 | case OR_Deleted: |
| 10043 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 10044 | << Best->Function->isDeleted() << "[]" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 10045 | << getDeletedOrUnavailableSuffix(Best->Function) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10046 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10047 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 10048 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10049 | return ExprError(); |
| 10050 | } |
| 10051 | |
| 10052 | // We matched a built-in operator; build it. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10053 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10054 | } |
| 10055 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10056 | /// BuildCallToMemberFunction - Build a call to a member |
| 10057 | /// function. MemExpr is the expression that refers to the member |
| 10058 | /// function (and includes the object parameter), Args/NumArgs are the |
| 10059 | /// arguments to the function call (not including the object |
| 10060 | /// parameter). The caller needs to validate that the member |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10061 | /// expression refers to a non-static member function or an overloaded |
| 10062 | /// member function. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10063 | ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10064 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 10065 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 10066 | unsigned NumArgs, SourceLocation RParenLoc) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10067 | assert(MemExprE->getType() == Context.BoundMemberTy || |
| 10068 | MemExprE->getType() == Context.OverloadTy); |
| 10069 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10070 | // Dig out the member expression. This holds both the object |
| 10071 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10072 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10073 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10074 | // Determine whether this is a call to a pointer-to-member function. |
| 10075 | if (BinaryOperator *op = dyn_cast<BinaryOperator>(NakedMemExpr)) { |
| 10076 | assert(op->getType() == Context.BoundMemberTy); |
| 10077 | assert(op->getOpcode() == BO_PtrMemD || op->getOpcode() == BO_PtrMemI); |
| 10078 | |
| 10079 | QualType fnType = |
| 10080 | op->getRHS()->getType()->castAs<MemberPointerType>()->getPointeeType(); |
| 10081 | |
| 10082 | const FunctionProtoType *proto = fnType->castAs<FunctionProtoType>(); |
| 10083 | QualType resultType = proto->getCallResultType(Context); |
| 10084 | ExprValueKind valueKind = Expr::getValueKindForType(proto->getResultType()); |
| 10085 | |
| 10086 | // Check that the object type isn't more qualified than the |
| 10087 | // member function we're calling. |
| 10088 | Qualifiers funcQuals = Qualifiers::fromCVRMask(proto->getTypeQuals()); |
| 10089 | |
| 10090 | QualType objectType = op->getLHS()->getType(); |
| 10091 | if (op->getOpcode() == BO_PtrMemI) |
| 10092 | objectType = objectType->castAs<PointerType>()->getPointeeType(); |
| 10093 | Qualifiers objectQuals = objectType.getQualifiers(); |
| 10094 | |
| 10095 | Qualifiers difference = objectQuals - funcQuals; |
| 10096 | difference.removeObjCGCAttr(); |
| 10097 | difference.removeAddressSpace(); |
| 10098 | if (difference) { |
| 10099 | std::string qualsString = difference.getAsString(); |
| 10100 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
| 10101 | << fnType.getUnqualifiedType() |
| 10102 | << qualsString |
| 10103 | << (qualsString.find(' ') == std::string::npos ? 1 : 2); |
| 10104 | } |
| 10105 | |
| 10106 | CXXMemberCallExpr *call |
| 10107 | = new (Context) CXXMemberCallExpr(Context, MemExprE, Args, NumArgs, |
| 10108 | resultType, valueKind, RParenLoc); |
| 10109 | |
| 10110 | if (CheckCallReturnType(proto->getResultType(), |
| 10111 | op->getRHS()->getSourceRange().getBegin(), |
| 10112 | call, 0)) |
| 10113 | return ExprError(); |
| 10114 | |
| 10115 | if (ConvertArgumentsForCall(call, op, 0, proto, Args, NumArgs, RParenLoc)) |
| 10116 | return ExprError(); |
| 10117 | |
| 10118 | return MaybeBindToTemporary(call); |
| 10119 | } |
| 10120 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10121 | UnbridgedCastsSet UnbridgedCasts; |
| 10122 | if (checkArgPlaceholdersForOverload(*this, Args, NumArgs, UnbridgedCasts)) |
| 10123 | return ExprError(); |
| 10124 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10125 | MemberExpr *MemExpr; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10126 | CXXMethodDecl *Method = 0; |
John McCall | bb6fb46 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 10127 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 10128 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10129 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 10130 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10131 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10132 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 10133 | Qualifier = MemExpr->getQualifier(); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10134 | UnbridgedCasts.restore(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10135 | } else { |
| 10136 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 10137 | Qualifier = UnresExpr->getQualifier(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10138 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 10139 | QualType ObjectType = UnresExpr->getBaseType(); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 10140 | Expr::Classification ObjectClassification |
| 10141 | = UnresExpr->isArrow()? Expr::Classification::makeSimpleLValue() |
| 10142 | : UnresExpr->getBase()->Classify(Context); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10143 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10144 | // Add overload candidates |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10145 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10146 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10147 | // FIXME: avoid copy. |
| 10148 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 10149 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 10150 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 10151 | TemplateArgs = &TemplateArgsBuffer; |
| 10152 | } |
| 10153 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10154 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 10155 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 10156 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 10157 | NamedDecl *Func = *I; |
| 10158 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 10159 | if (isa<UsingShadowDecl>(Func)) |
| 10160 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 10161 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 10162 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 10163 | // Microsoft supports direct constructor calls. |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 10164 | if (getLangOptions().MicrosoftExt && isa<CXXConstructorDecl>(Func)) { |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 10165 | AddOverloadCandidate(cast<CXXConstructorDecl>(Func), I.getPair(), Args, NumArgs, |
| 10166 | CandidateSet); |
| 10167 | } else if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 10168 | // If explicit template arguments were provided, we can't call a |
| 10169 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10170 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 10171 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10172 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10173 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10174 | ObjectClassification, |
| 10175 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 10176 | /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 10177 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10178 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10179 | I.getPair(), ActingDC, TemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10180 | ObjectType, ObjectClassification, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 10181 | Args, NumArgs, CandidateSet, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 10182 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 10183 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 10184 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10185 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10186 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 10187 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10188 | UnbridgedCasts.restore(); |
| 10189 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10190 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10191 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getLocStart(), |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 10192 | Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10193 | case OR_Success: |
| 10194 | Method = cast<CXXMethodDecl>(Best->Function); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 10195 | MarkDeclarationReferenced(UnresExpr->getMemberLoc(), Method); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10196 | FoundDecl = Best->FoundDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10197 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10198 | DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10199 | break; |
| 10200 | |
| 10201 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10202 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10203 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 10204 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10205 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10206 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10207 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10208 | |
| 10209 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10210 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 10211 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10212 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10213 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10214 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10215 | |
| 10216 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10217 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10218 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 10219 | << DeclName |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 10220 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 10221 | << MemExprE->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10222 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10223 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10224 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10225 | } |
| 10226 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10227 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10228 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10229 | // If overload resolution picked a static member, build a |
| 10230 | // non-member call based on that function. |
| 10231 | if (Method->isStatic()) { |
| 10232 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 10233 | Args, NumArgs, RParenLoc); |
| 10234 | } |
| 10235 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10236 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10237 | } |
| 10238 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10239 | QualType ResultType = Method->getResultType(); |
| 10240 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 10241 | ResultType = ResultType.getNonLValueExprType(Context); |
| 10242 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10243 | assert(Method && "Member call to something that isn't a method?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10244 | CXXMemberCallExpr *TheCall = |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10245 | new (Context) CXXMemberCallExpr(Context, MemExprE, Args, NumArgs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10246 | ResultType, VK, RParenLoc); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10247 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 10248 | // Check for a valid return type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10249 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10250 | TheCall, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10251 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10252 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10253 | // Convert the object argument (for a non-static member function call). |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10254 | // We only need to do this if there was actually an overload; otherwise |
| 10255 | // it was done at lookup. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10256 | if (!Method->isStatic()) { |
| 10257 | ExprResult ObjectArg = |
| 10258 | PerformObjectArgumentInitialization(MemExpr->getBase(), Qualifier, |
| 10259 | FoundDecl, Method); |
| 10260 | if (ObjectArg.isInvalid()) |
| 10261 | return ExprError(); |
| 10262 | MemExpr->setBase(ObjectArg.take()); |
| 10263 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10264 | |
| 10265 | // Convert the rest of the arguments |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10266 | const FunctionProtoType *Proto = |
| 10267 | Method->getType()->getAs<FunctionProtoType>(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10268 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10269 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10270 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10271 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10272 | if (CheckFunctionCall(Method, TheCall)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10273 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 10274 | |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 10275 | if ((isa<CXXConstructorDecl>(CurContext) || |
| 10276 | isa<CXXDestructorDecl>(CurContext)) && |
| 10277 | TheCall->getMethodDecl()->isPure()) { |
| 10278 | const CXXMethodDecl *MD = TheCall->getMethodDecl(); |
| 10279 | |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 10280 | if (isa<CXXThisExpr>(MemExpr->getBase()->IgnoreParenCasts())) { |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 10281 | Diag(MemExpr->getLocStart(), |
| 10282 | diag::warn_call_to_pure_virtual_member_function_from_ctor_dtor) |
| 10283 | << MD->getDeclName() << isa<CXXDestructorDecl>(CurContext) |
| 10284 | << MD->getParent()->getDeclName(); |
| 10285 | |
| 10286 | Diag(MD->getLocStart(), diag::note_previous_decl) << MD->getDeclName(); |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 10287 | } |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 10288 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10289 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10290 | } |
| 10291 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10292 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 10293 | /// type (C++ [over.call.object]), which can end up invoking an |
| 10294 | /// overloaded function call operator (@c operator()) or performing a |
| 10295 | /// user-defined conversion on the object argument. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10296 | ExprResult |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10297 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 10298 | SourceLocation LParenLoc, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10299 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10300 | SourceLocation RParenLoc) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10301 | if (checkPlaceholderForOverload(*this, Obj)) |
| 10302 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10303 | ExprResult Object = Owned(Obj); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10304 | |
| 10305 | UnbridgedCastsSet UnbridgedCasts; |
| 10306 | if (checkArgPlaceholdersForOverload(*this, Args, NumArgs, UnbridgedCasts)) |
| 10307 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10308 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10309 | assert(Object.get()->getType()->isRecordType() && "Requires object type argument"); |
| 10310 | const RecordType *Record = Object.get()->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10311 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10312 | // C++ [over.call.object]p1: |
| 10313 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 10314 | // 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] | 10315 | // candidate functions includes at least the function call |
| 10316 | // operators of T. The function call operators of T are obtained by |
| 10317 | // ordinary lookup of the name operator() in the context of |
| 10318 | // (E).operator(). |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10319 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 10320 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 10321 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10322 | if (RequireCompleteType(LParenLoc, Object.get()->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 10323 | PDiag(diag::err_incomplete_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10324 | << Object.get()->getSourceRange())) |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 10325 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10326 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 10327 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 10328 | LookupQualifiedName(R, Record->getDecl()); |
| 10329 | R.suppressDiagnostics(); |
| 10330 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 10331 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 10332 | Oper != OperEnd; ++Oper) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10333 | AddMethodCandidate(Oper.getPair(), Object.get()->getType(), |
| 10334 | Object.get()->Classify(Context), Args, NumArgs, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 10335 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 10336 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10337 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10338 | // C++ [over.call.object]p2: |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 10339 | // In addition, for each (non-explicit in C++0x) conversion function |
| 10340 | // declared in T of the form |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10341 | // |
| 10342 | // operator conversion-type-id () cv-qualifier; |
| 10343 | // |
| 10344 | // where cv-qualifier is the same cv-qualification as, or a |
| 10345 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 10346 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 10347 | // R", or the type "reference to pointer to function of |
| 10348 | // (P1,...,Pn) returning R", or the type "reference to function |
| 10349 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10350 | // is also considered as a candidate function. Similarly, |
| 10351 | // surrogate call functions are added to the set of candidate |
| 10352 | // functions for each conversion function declared in an |
| 10353 | // accessible base class provided the function is not hidden |
| 10354 | // within T by another intervening declaration. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 10355 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 10356 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 10357 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10358 | E = Conversions->end(); I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 10359 | NamedDecl *D = *I; |
| 10360 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 10361 | if (isa<UsingShadowDecl>(D)) |
| 10362 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10363 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 10364 | // Skip over templated conversion functions; they aren't |
| 10365 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 10366 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 10367 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 10368 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 10369 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 10370 | if (!Conv->isExplicit()) { |
| 10371 | // Strip the reference type (if any) and then the pointer type (if |
| 10372 | // any) to get down to what might be a function type. |
| 10373 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 10374 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 10375 | ConvType = ConvPtrType->getPointeeType(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10376 | |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 10377 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
| 10378 | { |
| 10379 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
| 10380 | Object.get(), Args, NumArgs, CandidateSet); |
| 10381 | } |
| 10382 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10383 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10384 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10385 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10386 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10387 | // Perform overload resolution. |
| 10388 | OverloadCandidateSet::iterator Best; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10389 | switch (CandidateSet.BestViableFunction(*this, Object.get()->getLocStart(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10390 | Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10391 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10392 | // Overload resolution succeeded; we'll build the appropriate call |
| 10393 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10394 | break; |
| 10395 | |
| 10396 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10397 | if (CandidateSet.empty()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10398 | Diag(Object.get()->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 10399 | << Object.get()->getType() << /*call*/ 1 |
| 10400 | << Object.get()->getSourceRange(); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10401 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10402 | Diag(Object.get()->getSourceRange().getBegin(), |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10403 | diag::err_ovl_no_viable_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10404 | << Object.get()->getType() << Object.get()->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10405 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10406 | break; |
| 10407 | |
| 10408 | case OR_Ambiguous: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10409 | Diag(Object.get()->getSourceRange().getBegin(), |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10410 | diag::err_ovl_ambiguous_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10411 | << Object.get()->getType() << Object.get()->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10412 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10413 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10414 | |
| 10415 | case OR_Deleted: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10416 | Diag(Object.get()->getSourceRange().getBegin(), |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10417 | diag::err_ovl_deleted_object_call) |
| 10418 | << Best->Function->isDeleted() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10419 | << Object.get()->getType() |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 10420 | << getDeletedOrUnavailableSuffix(Best->Function) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10421 | << Object.get()->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10422 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10423 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10424 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10425 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 10426 | if (Best == CandidateSet.end()) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10427 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10428 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10429 | UnbridgedCasts.restore(); |
| 10430 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10431 | if (Best->Function == 0) { |
| 10432 | // Since there is no function declaration, this is one of the |
| 10433 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10434 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10435 | = cast<CXXConversionDecl>( |
| 10436 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 10437 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10438 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10439 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 10440 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10441 | // We selected one of the surrogate functions that converts the |
| 10442 | // object parameter to a function pointer. Perform the conversion |
| 10443 | // on the object argument, then let ActOnCallExpr finish the job. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10444 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 10445 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 10446 | // and then call it. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10447 | ExprResult Call = BuildCXXMemberCallExpr(Object.get(), Best->FoundDecl, |
| 10448 | Conv, HadMultipleCandidates); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 10449 | if (Call.isInvalid()) |
| 10450 | return ExprError(); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 10451 | // Record usage of conversion in an implicit cast. |
| 10452 | Call = Owned(ImplicitCastExpr::Create(Context, Call.get()->getType(), |
| 10453 | CK_UserDefinedConversion, |
| 10454 | Call.get(), 0, VK_RValue)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10455 | |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 10456 | return ActOnCallExpr(S, Call.get(), LParenLoc, MultiExprArg(Args, NumArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 10457 | RParenLoc); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10458 | } |
| 10459 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 10460 | MarkDeclarationReferenced(LParenLoc, Best->Function); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10461 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10462 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 10463 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10464 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 10465 | // that calls this method, using Object for the implicit object |
| 10466 | // parameter and passing along the remaining arguments. |
| 10467 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10468 | const FunctionProtoType *Proto = |
| 10469 | Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10470 | |
| 10471 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 10472 | unsigned NumArgsToCheck = NumArgs; |
| 10473 | |
| 10474 | // Build the full argument list for the method call (the |
| 10475 | // implicit object parameter is placed at the beginning of the |
| 10476 | // list). |
| 10477 | Expr **MethodArgs; |
| 10478 | if (NumArgs < NumArgsInProto) { |
| 10479 | NumArgsToCheck = NumArgsInProto; |
| 10480 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 10481 | } else { |
| 10482 | MethodArgs = new Expr*[NumArgs + 1]; |
| 10483 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10484 | MethodArgs[0] = Object.get(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10485 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 10486 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10487 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10488 | ExprResult NewFn = CreateFunctionRefExpr(*this, Method, |
| 10489 | HadMultipleCandidates); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10490 | if (NewFn.isInvalid()) |
| 10491 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10492 | |
| 10493 | // Once we've built TheCall, all of the expressions are properly |
| 10494 | // owned. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10495 | QualType ResultTy = Method->getResultType(); |
| 10496 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10497 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 10498 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10499 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10500 | new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn.take(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10501 | MethodArgs, NumArgs + 1, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10502 | ResultTy, VK, RParenLoc); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10503 | delete [] MethodArgs; |
| 10504 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10505 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall, |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 10506 | Method)) |
| 10507 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10508 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 10509 | // We may have default arguments. If so, we need to allocate more |
| 10510 | // slots in the call for them. |
| 10511 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 10512 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 10513 | else if (NumArgs > NumArgsInProto) |
| 10514 | NumArgsToCheck = NumArgsInProto; |
| 10515 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 10516 | bool IsError = false; |
| 10517 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10518 | // Initialize the implicit object parameter. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10519 | ExprResult ObjRes = |
| 10520 | PerformObjectArgumentInitialization(Object.get(), /*Qualifier=*/0, |
| 10521 | Best->FoundDecl, Method); |
| 10522 | if (ObjRes.isInvalid()) |
| 10523 | IsError = true; |
| 10524 | else |
| 10525 | Object = move(ObjRes); |
| 10526 | TheCall->setArg(0, Object.take()); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 10527 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10528 | // Check the argument types. |
| 10529 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10530 | Expr *Arg; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 10531 | if (i < NumArgs) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10532 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10533 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 10534 | // Pass the argument. |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 10535 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10536 | ExprResult InputInit |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 10537 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10538 | Context, |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 10539 | Method->getParamDecl(i)), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10540 | SourceLocation(), Arg); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10541 | |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 10542 | IsError |= InputInit.isInvalid(); |
| 10543 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 10544 | } else { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10545 | ExprResult DefArg |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 10546 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 10547 | if (DefArg.isInvalid()) { |
| 10548 | IsError = true; |
| 10549 | break; |
| 10550 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10551 | |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 10552 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 10553 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10554 | |
| 10555 | TheCall->setArg(i + 1, Arg); |
| 10556 | } |
| 10557 | |
| 10558 | // If this is a variadic call, handle args passed through "...". |
| 10559 | if (Proto->isVariadic()) { |
| 10560 | // Promote the arguments (C99 6.5.2.2p7). |
| 10561 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10562 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0); |
| 10563 | IsError |= Arg.isInvalid(); |
| 10564 | TheCall->setArg(i + 1, Arg.take()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10565 | } |
| 10566 | } |
| 10567 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 10568 | if (IsError) return true; |
| 10569 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10570 | if (CheckFunctionCall(Method, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 10571 | return true; |
| 10572 | |
John McCall | 182f709 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 10573 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10574 | } |
| 10575 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10576 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10577 | /// (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] | 10578 | /// @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] | 10579 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10580 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc) { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10581 | assert(Base->getType()->isRecordType() && |
| 10582 | "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10583 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10584 | if (checkPlaceholderForOverload(*this, Base)) |
| 10585 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10586 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10587 | SourceLocation Loc = Base->getExprLoc(); |
| 10588 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10589 | // C++ [over.ref]p1: |
| 10590 | // |
| 10591 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 10592 | // for a class object x of type T if T::operator->() exists and if |
| 10593 | // the operator is selected as the best match function by the |
| 10594 | // overload resolution mechanism (13.3). |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10595 | DeclarationName OpName = |
| 10596 | Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10597 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 10598 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 10599 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10600 | if (RequireCompleteType(Loc, Base->getType(), |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 10601 | PDiag(diag::err_typecheck_incomplete_tag) |
| 10602 | << Base->getSourceRange())) |
| 10603 | return ExprError(); |
| 10604 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 10605 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 10606 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 10607 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 10608 | |
| 10609 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 10610 | Oper != OperEnd; ++Oper) { |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 10611 | AddMethodCandidate(Oper.getPair(), Base->getType(), Base->Classify(Context), |
| 10612 | 0, 0, CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 10613 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10614 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10615 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10616 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10617 | // Perform overload resolution. |
| 10618 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10619 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10620 | case OR_Success: |
| 10621 | // Overload resolution succeeded; we'll build the call below. |
| 10622 | break; |
| 10623 | |
| 10624 | case OR_No_Viable_Function: |
| 10625 | if (CandidateSet.empty()) |
| 10626 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 10627 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10628 | else |
| 10629 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 10630 | << "operator->" << Base->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10631 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 10632 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10633 | |
| 10634 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10635 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 10636 | << "->" << Base->getType() << Base->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10637 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 10638 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10639 | |
| 10640 | case OR_Deleted: |
| 10641 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 10642 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 10643 | << "->" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 10644 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 10645 | << Base->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10646 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 10647 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10648 | } |
| 10649 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 10650 | MarkDeclarationReferenced(OpLoc, Best->Function); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10651 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10652 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10653 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10654 | // Convert the object parameter. |
| 10655 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10656 | ExprResult BaseResult = |
| 10657 | PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 10658 | Best->FoundDecl, Method); |
| 10659 | if (BaseResult.isInvalid()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 10660 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10661 | Base = BaseResult.take(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 10662 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10663 | // Build the operator call. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10664 | ExprResult FnExpr = CreateFunctionRefExpr(*this, Method, |
| 10665 | HadMultipleCandidates); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10666 | if (FnExpr.isInvalid()) |
| 10667 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10668 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10669 | QualType ResultTy = Method->getResultType(); |
| 10670 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10671 | ResultTy = ResultTy.getNonLValueExprType(Context); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10672 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10673 | new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr.take(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10674 | &Base, 1, ResultTy, VK, OpLoc); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 10675 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10676 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 10677 | Method)) |
| 10678 | return ExprError(); |
Eli Friedman | d593190 | 2011-04-04 01:18:25 +0000 | [diff] [blame] | 10679 | |
| 10680 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10681 | } |
| 10682 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 10683 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 10684 | /// a C++ overloaded function (possibly with some parentheses and |
| 10685 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 10686 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 10687 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 10688 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10689 | FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 10690 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10691 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 10692 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10693 | if (SubExpr == PE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 10694 | return PE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10695 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10696 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10697 | } |
| 10698 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10699 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10700 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 10701 | Found, Fn); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10702 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10703 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 10704 | "Implicit cast type cannot be determined from overload"); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 10705 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10706 | if (SubExpr == ICE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 10707 | return ICE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10708 | |
| 10709 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 10710 | ICE->getCastKind(), |
| 10711 | SubExpr, 0, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 10712 | ICE->getValueKind()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10713 | } |
| 10714 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10715 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10716 | assert(UnOp->getOpcode() == UO_AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 10717 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 10718 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 10719 | if (Method->isStatic()) { |
| 10720 | // Do nothing: static member functions aren't any different |
| 10721 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10722 | } else { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 10723 | // Fix the sub expression, which really has to be an |
| 10724 | // UnresolvedLookupExpr holding an overloaded member function |
| 10725 | // or template. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10726 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 10727 | Found, Fn); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10728 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 10729 | return UnOp; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10730 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10731 | assert(isa<DeclRefExpr>(SubExpr) |
| 10732 | && "fixed to something other than a decl ref"); |
| 10733 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 10734 | && "fixed to a member ref with no nested name qualifier"); |
| 10735 | |
| 10736 | // We have taken the address of a pointer to member |
| 10737 | // function. Perform the computation here so that we get the |
| 10738 | // appropriate pointer to member type. |
| 10739 | QualType ClassType |
| 10740 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 10741 | QualType MemPtrType |
| 10742 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 10743 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10744 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, MemPtrType, |
| 10745 | VK_RValue, OK_Ordinary, |
| 10746 | UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 10747 | } |
| 10748 | } |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10749 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 10750 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10751 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 10752 | return UnOp; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10753 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10754 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10755 | Context.getPointerType(SubExpr->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10756 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10757 | UnOp->getOperatorLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10758 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10759 | |
| 10760 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10761 | // FIXME: avoid copy. |
| 10762 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 10763 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10764 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 10765 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 10766 | } |
| 10767 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10768 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 10769 | ULE->getQualifierLoc(), |
| 10770 | Fn, |
| 10771 | ULE->getNameLoc(), |
| 10772 | Fn->getType(), |
| 10773 | VK_LValue, |
| 10774 | Found.getDecl(), |
| 10775 | TemplateArgs); |
| 10776 | DRE->setHadMultipleCandidates(ULE->getNumDecls() > 1); |
| 10777 | return DRE; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10778 | } |
| 10779 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10780 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 10781 | // FIXME: avoid copy. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10782 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 10783 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 10784 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 10785 | TemplateArgs = &TemplateArgsBuffer; |
| 10786 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 10787 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10788 | Expr *Base; |
| 10789 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10790 | // If we're filling in a static method where we used to have an |
| 10791 | // implicit member access, rewrite to a simple decl ref. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10792 | if (MemExpr->isImplicitAccess()) { |
| 10793 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10794 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 10795 | MemExpr->getQualifierLoc(), |
| 10796 | Fn, |
| 10797 | MemExpr->getMemberLoc(), |
| 10798 | Fn->getType(), |
| 10799 | VK_LValue, |
| 10800 | Found.getDecl(), |
| 10801 | TemplateArgs); |
| 10802 | DRE->setHadMultipleCandidates(MemExpr->getNumDecls() > 1); |
| 10803 | return DRE; |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 10804 | } else { |
| 10805 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 10806 | if (MemExpr->getQualifier()) |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10807 | Loc = MemExpr->getQualifierLoc().getBeginLoc(); |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 10808 | CheckCXXThisCapture(Loc); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 10809 | Base = new (Context) CXXThisExpr(Loc, |
| 10810 | MemExpr->getBaseType(), |
| 10811 | /*isImplicit=*/true); |
| 10812 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10813 | } else |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 10814 | Base = MemExpr->getBase(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10815 | |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 10816 | ExprValueKind valueKind; |
| 10817 | QualType type; |
| 10818 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 10819 | valueKind = VK_LValue; |
| 10820 | type = Fn->getType(); |
| 10821 | } else { |
| 10822 | valueKind = VK_RValue; |
| 10823 | type = Context.BoundMemberTy; |
| 10824 | } |
| 10825 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10826 | MemberExpr *ME = MemberExpr::Create(Context, Base, |
| 10827 | MemExpr->isArrow(), |
| 10828 | MemExpr->getQualifierLoc(), |
| 10829 | Fn, |
| 10830 | Found, |
| 10831 | MemExpr->getMemberNameInfo(), |
| 10832 | TemplateArgs, |
| 10833 | type, valueKind, OK_Ordinary); |
| 10834 | ME->setHadMultipleCandidates(true); |
| 10835 | return ME; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10836 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10837 | |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 10838 | llvm_unreachable("Invalid reference to overloaded function"); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 10839 | } |
| 10840 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10841 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10842 | DeclAccessPair Found, |
| 10843 | FunctionDecl *Fn) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10844 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 10845 | } |
| 10846 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 10847 | } // end namespace clang |