Douglas Gregor | 5251f1b | 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 | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Lookup.h" |
| 16 | #include "clang/Sema/Initialization.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Template.h" |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 18 | #include "clang/Sema/TemplateDeduction.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 22 | #include "clang/AST/CXXInheritance.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 24 | #include "clang/AST/Expr.h" |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeOrdering.h" |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | 2bbc026 | 2010-09-12 04:28:07 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseSet.h" |
Douglas Gregor | 58e008d | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallPtrSet.h" |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 33 | #include <algorithm> |
| 34 | |
| 35 | namespace clang { |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 36 | using namespace sema; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 37 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 38 | /// A convenience routine for creating a decayed reference to a |
| 39 | /// function. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 40 | static ExprResult |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 41 | CreateFunctionRefExpr(Sema &S, FunctionDecl *Fn, bool HadMultipleCandidates, |
Douglas Gregor | e9d6293 | 2011-07-15 16:25:15 +0000 | [diff] [blame] | 42 | SourceLocation Loc = SourceLocation(), |
| 43 | const DeclarationNameLoc &LocInfo = DeclarationNameLoc()){ |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 44 | DeclRefExpr *DRE = new (S.Context) DeclRefExpr(Fn, false, Fn->getType(), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 45 | VK_LValue, Loc, LocInfo); |
| 46 | if (HadMultipleCandidates) |
| 47 | DRE->setHadMultipleCandidates(true); |
| 48 | ExprResult E = S.Owned(DRE); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 49 | E = S.DefaultFunctionArrayConversion(E.take()); |
| 50 | if (E.isInvalid()) |
| 51 | return ExprError(); |
| 52 | return move(E); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 53 | } |
| 54 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 55 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 56 | bool InOverloadResolution, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 57 | StandardConversionSequence &SCS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 58 | bool CStyle, |
| 59 | bool AllowObjCWritebackConversion); |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 60 | |
| 61 | static bool IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 62 | QualType &ToType, |
| 63 | bool InOverloadResolution, |
| 64 | StandardConversionSequence &SCS, |
| 65 | bool CStyle); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 66 | static OverloadingResult |
| 67 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 68 | UserDefinedConversionSequence& User, |
| 69 | OverloadCandidateSet& Conversions, |
| 70 | bool AllowExplicit); |
| 71 | |
| 72 | |
| 73 | static ImplicitConversionSequence::CompareKind |
| 74 | CompareStandardConversionSequences(Sema &S, |
| 75 | const StandardConversionSequence& SCS1, |
| 76 | const StandardConversionSequence& SCS2); |
| 77 | |
| 78 | static ImplicitConversionSequence::CompareKind |
| 79 | CompareQualificationConversions(Sema &S, |
| 80 | const StandardConversionSequence& SCS1, |
| 81 | const StandardConversionSequence& SCS2); |
| 82 | |
| 83 | static ImplicitConversionSequence::CompareKind |
| 84 | CompareDerivedToBaseConversions(Sema &S, |
| 85 | const StandardConversionSequence& SCS1, |
| 86 | const StandardConversionSequence& SCS2); |
| 87 | |
| 88 | |
| 89 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 90 | /// GetConversionCategory - Retrieve the implicit conversion |
| 91 | /// category corresponding to the given implicit conversion kind. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | ImplicitConversionCategory |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 93 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 94 | static const ImplicitConversionCategory |
| 95 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 96 | ICC_Identity, |
| 97 | ICC_Lvalue_Transformation, |
| 98 | ICC_Lvalue_Transformation, |
| 99 | ICC_Lvalue_Transformation, |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 100 | ICC_Identity, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 101 | ICC_Qualification_Adjustment, |
| 102 | ICC_Promotion, |
| 103 | ICC_Promotion, |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 104 | ICC_Promotion, |
| 105 | ICC_Conversion, |
| 106 | ICC_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 107 | ICC_Conversion, |
| 108 | ICC_Conversion, |
| 109 | ICC_Conversion, |
| 110 | ICC_Conversion, |
| 111 | ICC_Conversion, |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 112 | ICC_Conversion, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 113 | ICC_Conversion, |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 114 | ICC_Conversion, |
| 115 | ICC_Conversion, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 116 | ICC_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 117 | ICC_Conversion |
| 118 | }; |
| 119 | return Category[(int)Kind]; |
| 120 | } |
| 121 | |
| 122 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 123 | /// corresponding to the given implicit conversion kind. |
| 124 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 125 | static const ImplicitConversionRank |
| 126 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 127 | ICR_Exact_Match, |
| 128 | ICR_Exact_Match, |
| 129 | ICR_Exact_Match, |
| 130 | ICR_Exact_Match, |
| 131 | ICR_Exact_Match, |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 132 | ICR_Exact_Match, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 133 | ICR_Promotion, |
| 134 | ICR_Promotion, |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 135 | ICR_Promotion, |
| 136 | ICR_Conversion, |
| 137 | ICR_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 138 | ICR_Conversion, |
| 139 | ICR_Conversion, |
| 140 | ICR_Conversion, |
| 141 | ICR_Conversion, |
| 142 | ICR_Conversion, |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 143 | ICR_Conversion, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 144 | ICR_Conversion, |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 145 | ICR_Conversion, |
| 146 | ICR_Conversion, |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 147 | ICR_Complex_Real_Conversion, |
| 148 | ICR_Conversion, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 149 | ICR_Conversion, |
| 150 | ICR_Writeback_Conversion |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 151 | }; |
| 152 | return Rank[(int)Kind]; |
| 153 | } |
| 154 | |
| 155 | /// GetImplicitConversionName - Return the name of this kind of |
| 156 | /// implicit conversion. |
| 157 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | cfca1f0 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 158 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 159 | "No conversion", |
| 160 | "Lvalue-to-rvalue", |
| 161 | "Array-to-pointer", |
| 162 | "Function-to-pointer", |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 163 | "Noreturn adjustment", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 164 | "Qualification", |
| 165 | "Integral promotion", |
| 166 | "Floating point promotion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 167 | "Complex promotion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 168 | "Integral conversion", |
| 169 | "Floating conversion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 170 | "Complex conversion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 171 | "Floating-integral conversion", |
| 172 | "Pointer conversion", |
| 173 | "Pointer-to-member conversion", |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 174 | "Boolean conversion", |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 175 | "Compatible-types conversion", |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 176 | "Derived-to-base conversion", |
| 177 | "Vector conversion", |
| 178 | "Vector splat", |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 179 | "Complex-real conversion", |
| 180 | "Block Pointer conversion", |
| 181 | "Transparent Union Conversion" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 182 | "Writeback conversion" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 183 | }; |
| 184 | return Name[Kind]; |
| 185 | } |
| 186 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 187 | /// StandardConversionSequence - Set the standard conversion |
| 188 | /// sequence to the identity conversion. |
| 189 | void StandardConversionSequence::setAsIdentityConversion() { |
| 190 | First = ICK_Identity; |
| 191 | Second = ICK_Identity; |
| 192 | Third = ICK_Identity; |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 193 | DeprecatedStringLiteralToCharPtr = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 194 | QualificationIncludesObjCLifetime = false; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 195 | ReferenceBinding = false; |
| 196 | DirectBinding = false; |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 197 | IsLvalueReference = true; |
| 198 | BindsToFunctionLvalue = false; |
| 199 | BindsToRvalue = false; |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 200 | BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 201 | ObjCLifetimeConversionBinding = false; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 202 | CopyConstructor = 0; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 205 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 206 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 207 | /// implicit conversions. |
| 208 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 209 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 210 | if (GetConversionRank(First) > Rank) |
| 211 | Rank = GetConversionRank(First); |
| 212 | if (GetConversionRank(Second) > Rank) |
| 213 | Rank = GetConversionRank(Second); |
| 214 | if (GetConversionRank(Third) > Rank) |
| 215 | Rank = GetConversionRank(Third); |
| 216 | return Rank; |
| 217 | } |
| 218 | |
| 219 | /// isPointerConversionToBool - Determines whether this conversion is |
| 220 | /// a conversion of a pointer or pointer-to-member to bool. This is |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 222 | /// (C++ 13.3.3.2p4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 223 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 224 | // Note that FromType has not necessarily been transformed by the |
| 225 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 226 | // check for their presence as well as checking whether FromType is |
| 227 | // a pointer. |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 228 | if (getToType(1)->isBooleanType() && |
John McCall | 6d1116a | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 229 | (getFromType()->isPointerType() || |
| 230 | getFromType()->isObjCObjectPointerType() || |
| 231 | getFromType()->isBlockPointerType() || |
Anders Carlsson | 7da7cc5 | 2010-11-05 00:12:09 +0000 | [diff] [blame] | 232 | getFromType()->isNullPtrType() || |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 233 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 234 | return true; |
| 235 | |
| 236 | return false; |
| 237 | } |
| 238 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 239 | /// isPointerConversionToVoidPointer - Determines whether this |
| 240 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 241 | /// used as part of the ranking of standard conversion sequences (C++ |
| 242 | /// 13.3.3.2p4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | bool |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 244 | StandardConversionSequence:: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 246 | QualType FromType = getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 247 | QualType ToType = getToType(1); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 248 | |
| 249 | // Note that FromType has not necessarily been transformed by the |
| 250 | // array-to-pointer implicit conversion, so check for its presence |
| 251 | // and redo the conversion to get a pointer. |
| 252 | if (First == ICK_Array_To_Pointer) |
| 253 | FromType = Context.getArrayDecayedType(FromType); |
| 254 | |
Douglas Gregor | 5d3d3fa | 2011-04-15 20:45:44 +0000 | [diff] [blame] | 255 | if (Second == ICK_Pointer_Conversion && FromType->isAnyPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 256 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 257 | return ToPtrType->getPointeeType()->isVoidType(); |
| 258 | |
| 259 | return false; |
| 260 | } |
| 261 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 262 | /// Skip any implicit casts which could be either part of a narrowing conversion |
| 263 | /// or after one in an implicit conversion. |
| 264 | static const Expr *IgnoreNarrowingConversion(const Expr *Converted) { |
| 265 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Converted)) { |
| 266 | switch (ICE->getCastKind()) { |
| 267 | case CK_NoOp: |
| 268 | case CK_IntegralCast: |
| 269 | case CK_IntegralToBoolean: |
| 270 | case CK_IntegralToFloating: |
| 271 | case CK_FloatingToIntegral: |
| 272 | case CK_FloatingToBoolean: |
| 273 | case CK_FloatingCast: |
| 274 | Converted = ICE->getSubExpr(); |
| 275 | continue; |
| 276 | |
| 277 | default: |
| 278 | return Converted; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | return Converted; |
| 283 | } |
| 284 | |
| 285 | /// Check if this standard conversion sequence represents a narrowing |
| 286 | /// conversion, according to C++11 [dcl.init.list]p7. |
| 287 | /// |
| 288 | /// \param Ctx The AST context. |
| 289 | /// \param Converted The result of applying this standard conversion sequence. |
| 290 | /// \param ConstantValue If this is an NK_Constant_Narrowing conversion, the |
| 291 | /// value of the expression prior to the narrowing conversion. |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 292 | /// \param ConstantType If this is an NK_Constant_Narrowing conversion, the |
| 293 | /// type of the expression prior to the narrowing conversion. |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 294 | NarrowingKind |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 295 | StandardConversionSequence::getNarrowingKind(ASTContext &Ctx, |
| 296 | const Expr *Converted, |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 297 | APValue &ConstantValue, |
| 298 | QualType &ConstantType) const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 299 | assert(Ctx.getLangOpts().CPlusPlus && "narrowing check outside C++"); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 300 | |
| 301 | // C++11 [dcl.init.list]p7: |
| 302 | // A narrowing conversion is an implicit conversion ... |
| 303 | QualType FromType = getToType(0); |
| 304 | QualType ToType = getToType(1); |
| 305 | switch (Second) { |
| 306 | // -- from a floating-point type to an integer type, or |
| 307 | // |
| 308 | // -- from an integer type or unscoped enumeration type to a floating-point |
| 309 | // type, except where the source is a constant expression and the actual |
| 310 | // value after conversion will fit into the target type and will produce |
| 311 | // the original value when converted back to the original type, or |
| 312 | case ICK_Floating_Integral: |
| 313 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
| 314 | return NK_Type_Narrowing; |
| 315 | } else if (FromType->isIntegralType(Ctx) && ToType->isRealFloatingType()) { |
| 316 | llvm::APSInt IntConstantValue; |
| 317 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| 318 | if (Initializer && |
| 319 | Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { |
| 320 | // Convert the integer to the floating type. |
| 321 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
| 322 | Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), |
| 323 | llvm::APFloat::rmNearestTiesToEven); |
| 324 | // And back. |
| 325 | llvm::APSInt ConvertedValue = IntConstantValue; |
| 326 | bool ignored; |
| 327 | Result.convertToInteger(ConvertedValue, |
| 328 | llvm::APFloat::rmTowardZero, &ignored); |
| 329 | // If the resulting value is different, this was a narrowing conversion. |
| 330 | if (IntConstantValue != ConvertedValue) { |
| 331 | ConstantValue = APValue(IntConstantValue); |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 332 | ConstantType = Initializer->getType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 333 | return NK_Constant_Narrowing; |
| 334 | } |
| 335 | } else { |
| 336 | // Variables are always narrowings. |
| 337 | return NK_Variable_Narrowing; |
| 338 | } |
| 339 | } |
| 340 | return NK_Not_Narrowing; |
| 341 | |
| 342 | // -- from long double to double or float, or from double to float, except |
| 343 | // where the source is a constant expression and the actual value after |
| 344 | // conversion is within the range of values that can be represented (even |
| 345 | // if it cannot be represented exactly), or |
| 346 | case ICK_Floating_Conversion: |
| 347 | if (FromType->isRealFloatingType() && ToType->isRealFloatingType() && |
| 348 | Ctx.getFloatingTypeOrder(FromType, ToType) == 1) { |
| 349 | // FromType is larger than ToType. |
| 350 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
| 351 | if (Initializer->isCXX11ConstantExpr(Ctx, &ConstantValue)) { |
| 352 | // Constant! |
| 353 | assert(ConstantValue.isFloat()); |
| 354 | llvm::APFloat FloatVal = ConstantValue.getFloat(); |
| 355 | // Convert the source value into the target type. |
| 356 | bool ignored; |
| 357 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
| 358 | Ctx.getFloatTypeSemantics(ToType), |
| 359 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 360 | // If there was no overflow, the source value is within the range of |
| 361 | // values that can be represented. |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 362 | if (ConvertStatus & llvm::APFloat::opOverflow) { |
| 363 | ConstantType = Initializer->getType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 364 | return NK_Constant_Narrowing; |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 365 | } |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 366 | } else { |
| 367 | return NK_Variable_Narrowing; |
| 368 | } |
| 369 | } |
| 370 | return NK_Not_Narrowing; |
| 371 | |
| 372 | // -- from an integer type or unscoped enumeration type to an integer type |
| 373 | // that cannot represent all the values of the original type, except where |
| 374 | // the source is a constant expression and the actual value after |
| 375 | // conversion will fit into the target type and will produce the original |
| 376 | // value when converted back to the original type. |
| 377 | case ICK_Boolean_Conversion: // Bools are integers too. |
| 378 | if (!FromType->isIntegralOrUnscopedEnumerationType()) { |
| 379 | // Boolean conversions can be from pointers and pointers to members |
| 380 | // [conv.bool], and those aren't considered narrowing conversions. |
| 381 | return NK_Not_Narrowing; |
| 382 | } // Otherwise, fall through to the integral case. |
| 383 | case ICK_Integral_Conversion: { |
| 384 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
| 385 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
| 386 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
| 387 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
| 388 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
| 389 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
| 390 | |
| 391 | if (FromWidth > ToWidth || |
Richard Smith | 25a80d4 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 392 | (FromWidth == ToWidth && FromSigned != ToSigned) || |
| 393 | (FromSigned && !ToSigned)) { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 394 | // Not all values of FromType can be represented in ToType. |
| 395 | llvm::APSInt InitializerValue; |
| 396 | const Expr *Initializer = IgnoreNarrowingConversion(Converted); |
Richard Smith | 25a80d4 | 2012-06-13 01:07:41 +0000 | [diff] [blame] | 397 | if (!Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { |
| 398 | // Such conversions on variables are always narrowing. |
| 399 | return NK_Variable_Narrowing; |
| 400 | } else if (FromWidth < ToWidth) { |
| 401 | // Negative -> unsigned is narrowing. Otherwise, more bits is never |
| 402 | // narrowing. |
| 403 | if (InitializerValue.isSigned() && InitializerValue.isNegative()) |
| 404 | return NK_Constant_Narrowing; |
| 405 | } else { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 406 | ConstantValue = APValue(InitializerValue); |
| 407 | |
| 408 | // Add a bit to the InitializerValue so we don't have to worry about |
| 409 | // signed vs. unsigned comparisons. |
| 410 | InitializerValue = InitializerValue.extend( |
| 411 | InitializerValue.getBitWidth() + 1); |
| 412 | // Convert the initializer to and from the target width and signed-ness. |
| 413 | llvm::APSInt ConvertedValue = InitializerValue; |
| 414 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
| 415 | ConvertedValue.setIsSigned(ToSigned); |
| 416 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
| 417 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
| 418 | // If the result is different, this was a narrowing conversion. |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 419 | if (ConvertedValue != InitializerValue) { |
| 420 | ConstantType = Initializer->getType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 421 | return NK_Constant_Narrowing; |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 422 | } |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | return NK_Not_Narrowing; |
| 426 | } |
| 427 | |
| 428 | default: |
| 429 | // Other kinds of conversions are not narrowings. |
| 430 | return NK_Not_Narrowing; |
| 431 | } |
| 432 | } |
| 433 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 434 | /// DebugPrint - Print this standard conversion sequence to standard |
| 435 | /// error. Useful for debugging overloading issues. |
| 436 | void StandardConversionSequence::DebugPrint() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 437 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 438 | bool PrintedSomething = false; |
| 439 | if (First != ICK_Identity) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 440 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 441 | PrintedSomething = true; |
| 442 | } |
| 443 | |
| 444 | if (Second != ICK_Identity) { |
| 445 | if (PrintedSomething) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 446 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 447 | } |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 448 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 449 | |
| 450 | if (CopyConstructor) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 451 | OS << " (by copy constructor)"; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 452 | } else if (DirectBinding) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 453 | OS << " (direct reference binding)"; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 454 | } else if (ReferenceBinding) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 455 | OS << " (reference binding)"; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 456 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 457 | PrintedSomething = true; |
| 458 | } |
| 459 | |
| 460 | if (Third != ICK_Identity) { |
| 461 | if (PrintedSomething) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 462 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 463 | } |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 464 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 465 | PrintedSomething = true; |
| 466 | } |
| 467 | |
| 468 | if (!PrintedSomething) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 469 | OS << "No conversions required"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
| 473 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 474 | /// error. Useful for debugging overloading issues. |
| 475 | void UserDefinedConversionSequence::DebugPrint() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 476 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 477 | if (Before.First || Before.Second || Before.Third) { |
| 478 | Before.DebugPrint(); |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 479 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 480 | } |
Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 481 | if (ConversionFunction) |
| 482 | OS << '\'' << *ConversionFunction << '\''; |
| 483 | else |
| 484 | OS << "aggregate initialization"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 485 | if (After.First || After.Second || After.Third) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 486 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 487 | After.DebugPrint(); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 492 | /// error. Useful for debugging overloading issues. |
| 493 | void ImplicitConversionSequence::DebugPrint() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 494 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 495 | switch (ConversionKind) { |
| 496 | case StandardConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 497 | OS << "Standard conversion: "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 498 | Standard.DebugPrint(); |
| 499 | break; |
| 500 | case UserDefinedConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 501 | OS << "User-defined conversion: "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 502 | UserDefined.DebugPrint(); |
| 503 | break; |
| 504 | case EllipsisConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 505 | OS << "Ellipsis conversion"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 506 | break; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 507 | case AmbiguousConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 508 | OS << "Ambiguous conversion"; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 509 | break; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 510 | case BadConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 511 | OS << "Bad conversion"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 512 | break; |
| 513 | } |
| 514 | |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 515 | OS << "\n"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 516 | } |
| 517 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 518 | void AmbiguousConversionSequence::construct() { |
| 519 | new (&conversions()) ConversionSet(); |
| 520 | } |
| 521 | |
| 522 | void AmbiguousConversionSequence::destruct() { |
| 523 | conversions().~ConversionSet(); |
| 524 | } |
| 525 | |
| 526 | void |
| 527 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 528 | FromTypePtr = O.FromTypePtr; |
| 529 | ToTypePtr = O.ToTypePtr; |
| 530 | new (&conversions()) ConversionSet(O.conversions()); |
| 531 | } |
| 532 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 533 | namespace { |
| 534 | // Structure used by OverloadCandidate::DeductionFailureInfo to store |
| 535 | // template parameter and template argument information. |
| 536 | struct DFIParamWithArguments { |
| 537 | TemplateParameter Param; |
| 538 | TemplateArgument FirstArg; |
| 539 | TemplateArgument SecondArg; |
| 540 | }; |
| 541 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 542 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 543 | /// \brief Convert from Sema's representation of template deduction information |
| 544 | /// to the form used in overload-candidate information. |
| 545 | OverloadCandidate::DeductionFailureInfo |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 546 | static MakeDeductionFailureInfo(ASTContext &Context, |
| 547 | Sema::TemplateDeductionResult TDK, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 548 | TemplateDeductionInfo &Info) { |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 549 | OverloadCandidate::DeductionFailureInfo Result; |
| 550 | Result.Result = static_cast<unsigned>(TDK); |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 551 | Result.HasDiagnostic = false; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 552 | Result.Data = 0; |
| 553 | switch (TDK) { |
| 554 | case Sema::TDK_Success: |
| 555 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 556 | case Sema::TDK_TooManyArguments: |
| 557 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 558 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 559 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 560 | case Sema::TDK_Incomplete: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 561 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 562 | Result.Data = Info.Param.getOpaqueValue(); |
| 563 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 564 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 565 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 566 | case Sema::TDK_Underqualified: { |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 567 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 568 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 569 | Saved->Param = Info.Param; |
| 570 | Saved->FirstArg = Info.FirstArg; |
| 571 | Saved->SecondArg = Info.SecondArg; |
| 572 | Result.Data = Saved; |
| 573 | break; |
| 574 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 575 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 576 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 577 | Result.Data = Info.take(); |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 578 | if (Info.hasSFINAEDiagnostic()) { |
| 579 | PartialDiagnosticAt *Diag = new (Result.Diagnostic) PartialDiagnosticAt( |
| 580 | SourceLocation(), PartialDiagnostic::NullDiagnostic()); |
| 581 | Info.takeSFINAEDiagnostic(*Diag); |
| 582 | Result.HasDiagnostic = true; |
| 583 | } |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 584 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 585 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 586 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 587 | case Sema::TDK_FailedOverloadResolution: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 588 | break; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 589 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 590 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 591 | return Result; |
| 592 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 593 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 594 | void OverloadCandidate::DeductionFailureInfo::Destroy() { |
| 595 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 596 | case Sema::TDK_Success: |
| 597 | case Sema::TDK_InstantiationDepth: |
| 598 | case Sema::TDK_Incomplete: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 599 | case Sema::TDK_TooManyArguments: |
| 600 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 601 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 602 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 603 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 604 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 605 | case Sema::TDK_Underqualified: |
Douglas Gregor | b02d6b3 | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 606 | // FIXME: Destroy the data? |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 607 | Data = 0; |
| 608 | break; |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 609 | |
| 610 | case Sema::TDK_SubstitutionFailure: |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 611 | // FIXME: Destroy the template argument list? |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 612 | Data = 0; |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 613 | if (PartialDiagnosticAt *Diag = getSFINAEDiagnostic()) { |
| 614 | Diag->~PartialDiagnosticAt(); |
| 615 | HasDiagnostic = false; |
| 616 | } |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 617 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 618 | |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 619 | // Unhandled |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 620 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 621 | case Sema::TDK_FailedOverloadResolution: |
| 622 | break; |
| 623 | } |
| 624 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 625 | |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 626 | PartialDiagnosticAt * |
| 627 | OverloadCandidate::DeductionFailureInfo::getSFINAEDiagnostic() { |
| 628 | if (HasDiagnostic) |
| 629 | return static_cast<PartialDiagnosticAt*>(static_cast<void*>(Diagnostic)); |
| 630 | return 0; |
| 631 | } |
| 632 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 633 | TemplateParameter |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 634 | OverloadCandidate::DeductionFailureInfo::getTemplateParameter() { |
| 635 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 636 | case Sema::TDK_Success: |
| 637 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 638 | case Sema::TDK_TooManyArguments: |
| 639 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 640 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 641 | return TemplateParameter(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 642 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 643 | case Sema::TDK_Incomplete: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 644 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 645 | return TemplateParameter::getFromOpaqueValue(Data); |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 646 | |
| 647 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 648 | case Sema::TDK_Underqualified: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 649 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 650 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 651 | // Unhandled |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 652 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 653 | case Sema::TDK_FailedOverloadResolution: |
| 654 | break; |
| 655 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 656 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 657 | return TemplateParameter(); |
| 658 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 659 | |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 660 | TemplateArgumentList * |
| 661 | OverloadCandidate::DeductionFailureInfo::getTemplateArgumentList() { |
| 662 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 663 | case Sema::TDK_Success: |
| 664 | case Sema::TDK_InstantiationDepth: |
| 665 | case Sema::TDK_TooManyArguments: |
| 666 | case Sema::TDK_TooFewArguments: |
| 667 | case Sema::TDK_Incomplete: |
| 668 | case Sema::TDK_InvalidExplicitArguments: |
| 669 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 670 | case Sema::TDK_Underqualified: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 671 | return 0; |
| 672 | |
| 673 | case Sema::TDK_SubstitutionFailure: |
| 674 | return static_cast<TemplateArgumentList*>(Data); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 675 | |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 676 | // Unhandled |
| 677 | case Sema::TDK_NonDeducedMismatch: |
| 678 | case Sema::TDK_FailedOverloadResolution: |
| 679 | break; |
| 680 | } |
| 681 | |
| 682 | return 0; |
| 683 | } |
| 684 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 685 | const TemplateArgument *OverloadCandidate::DeductionFailureInfo::getFirstArg() { |
| 686 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 687 | case Sema::TDK_Success: |
| 688 | case Sema::TDK_InstantiationDepth: |
| 689 | case Sema::TDK_Incomplete: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 690 | case Sema::TDK_TooManyArguments: |
| 691 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 692 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 693 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 694 | return 0; |
| 695 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 696 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 697 | case Sema::TDK_Underqualified: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 698 | return &static_cast<DFIParamWithArguments*>(Data)->FirstArg; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 699 | |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 700 | // Unhandled |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 701 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 702 | case Sema::TDK_FailedOverloadResolution: |
| 703 | break; |
| 704 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 705 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 706 | return 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 707 | } |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 708 | |
| 709 | const TemplateArgument * |
| 710 | OverloadCandidate::DeductionFailureInfo::getSecondArg() { |
| 711 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 712 | case Sema::TDK_Success: |
| 713 | case Sema::TDK_InstantiationDepth: |
| 714 | case Sema::TDK_Incomplete: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 715 | case Sema::TDK_TooManyArguments: |
| 716 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 717 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 718 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 719 | return 0; |
| 720 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 721 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 722 | case Sema::TDK_Underqualified: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 723 | return &static_cast<DFIParamWithArguments*>(Data)->SecondArg; |
| 724 | |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 725 | // Unhandled |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 726 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 727 | case Sema::TDK_FailedOverloadResolution: |
| 728 | break; |
| 729 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 730 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | void OverloadCandidateSet::clear() { |
Benjamin Kramer | 02b0843 | 2012-01-14 20:16:52 +0000 | [diff] [blame] | 735 | for (iterator i = begin(), e = end(); i != e; ++i) |
| 736 | for (unsigned ii = 0, ie = i->NumConversions; ii != ie; ++ii) |
| 737 | i->Conversions[ii].~ImplicitConversionSequence(); |
Benjamin Kramer | 0b9c509 | 2012-01-14 19:31:39 +0000 | [diff] [blame] | 738 | NumInlineSequences = 0; |
Benjamin Kramer | fb761ff | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 739 | Candidates.clear(); |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 740 | Functions.clear(); |
| 741 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 742 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 743 | namespace { |
| 744 | class UnbridgedCastsSet { |
| 745 | struct Entry { |
| 746 | Expr **Addr; |
| 747 | Expr *Saved; |
| 748 | }; |
| 749 | SmallVector<Entry, 2> Entries; |
| 750 | |
| 751 | public: |
| 752 | void save(Sema &S, Expr *&E) { |
| 753 | assert(E->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 754 | Entry entry = { &E, E }; |
| 755 | Entries.push_back(entry); |
| 756 | E = S.stripARCUnbridgedCast(E); |
| 757 | } |
| 758 | |
| 759 | void restore() { |
| 760 | for (SmallVectorImpl<Entry>::iterator |
| 761 | i = Entries.begin(), e = Entries.end(); i != e; ++i) |
| 762 | *i->Addr = i->Saved; |
| 763 | } |
| 764 | }; |
| 765 | } |
| 766 | |
| 767 | /// checkPlaceholderForOverload - Do any interesting placeholder-like |
| 768 | /// preprocessing on the given expression. |
| 769 | /// |
| 770 | /// \param unbridgedCasts a collection to which to add unbridged casts; |
| 771 | /// without this, they will be immediately diagnosed as errors |
| 772 | /// |
| 773 | /// Return true on unrecoverable error. |
| 774 | static bool checkPlaceholderForOverload(Sema &S, Expr *&E, |
| 775 | UnbridgedCastsSet *unbridgedCasts = 0) { |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 776 | if (const BuiltinType *placeholder = E->getType()->getAsPlaceholderType()) { |
| 777 | // We can't handle overloaded expressions here because overload |
| 778 | // resolution might reasonably tweak them. |
| 779 | if (placeholder->getKind() == BuiltinType::Overload) return false; |
| 780 | |
| 781 | // If the context potentially accepts unbridged ARC casts, strip |
| 782 | // the unbridged cast and add it to the collection for later restoration. |
| 783 | if (placeholder->getKind() == BuiltinType::ARCUnbridgedCast && |
| 784 | unbridgedCasts) { |
| 785 | unbridgedCasts->save(S, E); |
| 786 | return false; |
| 787 | } |
| 788 | |
| 789 | // Go ahead and check everything else. |
| 790 | ExprResult result = S.CheckPlaceholderExpr(E); |
| 791 | if (result.isInvalid()) |
| 792 | return true; |
| 793 | |
| 794 | E = result.take(); |
| 795 | return false; |
| 796 | } |
| 797 | |
| 798 | // Nothing to do. |
| 799 | return false; |
| 800 | } |
| 801 | |
| 802 | /// checkArgPlaceholdersForOverload - Check a set of call operands for |
| 803 | /// placeholders. |
| 804 | static bool checkArgPlaceholdersForOverload(Sema &S, Expr **args, |
| 805 | unsigned numArgs, |
| 806 | UnbridgedCastsSet &unbridged) { |
| 807 | for (unsigned i = 0; i != numArgs; ++i) |
| 808 | if (checkPlaceholderForOverload(S, args[i], &unbridged)) |
| 809 | return true; |
| 810 | |
| 811 | return false; |
| 812 | } |
| 813 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 814 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 815 | // overload of the declarations in Old. This routine returns false if |
| 816 | // New and Old cannot be overloaded, e.g., if New has the same |
| 817 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 818 | // declarations aren't functions (or function templates) at all. When |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 819 | // it does return false, MatchedDecl will point to the decl that New |
| 820 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 821 | // top of the underlying declaration. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 822 | // |
| 823 | // Example: Given the following input: |
| 824 | // |
| 825 | // void f(int, float); // #1 |
| 826 | // void f(int, int); // #2 |
| 827 | // int f(int, int); // #3 |
| 828 | // |
| 829 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 830 | // so IsOverload will not be used. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 831 | // |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 832 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 833 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 834 | // (since they have different signatures), so this routine returns |
| 835 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 836 | // |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 837 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 838 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 839 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 840 | // identical (return types of functions are not part of the |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 841 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 842 | // point to the FunctionDecl for #2. |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 843 | // |
| 844 | // 'NewIsUsingShadowDecl' indicates that 'New' is being introduced |
| 845 | // into a class by a using declaration. The rules for whether to hide |
| 846 | // shadow declarations ignore some properties which otherwise figure |
| 847 | // into a function template's signature. |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 848 | Sema::OverloadKind |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 849 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
| 850 | NamedDecl *&Match, bool NewIsUsingDecl) { |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 851 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 852 | I != E; ++I) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 853 | NamedDecl *OldD = *I; |
| 854 | |
| 855 | bool OldIsUsingDecl = false; |
| 856 | if (isa<UsingShadowDecl>(OldD)) { |
| 857 | OldIsUsingDecl = true; |
| 858 | |
| 859 | // We can always introduce two using declarations into the same |
| 860 | // context, even if they have identical signatures. |
| 861 | if (NewIsUsingDecl) continue; |
| 862 | |
| 863 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 864 | } |
| 865 | |
| 866 | // If either declaration was introduced by a using declaration, |
| 867 | // we'll need to use slightly different rules for matching. |
| 868 | // Essentially, these rules are the normal rules, except that |
| 869 | // function templates hide function templates with different |
| 870 | // return types or template parameter lists. |
| 871 | bool UseMemberUsingDeclRules = |
| 872 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord(); |
| 873 | |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 874 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 875 | if (!IsOverload(New, OldT->getTemplatedDecl(), UseMemberUsingDeclRules)) { |
| 876 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 877 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 878 | continue; |
| 879 | } |
| 880 | |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 881 | Match = *I; |
| 882 | return Ovl_Match; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 883 | } |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 884 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 885 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
| 886 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 887 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 888 | continue; |
| 889 | } |
| 890 | |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 891 | Match = *I; |
| 892 | return Ovl_Match; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 893 | } |
John McCall | a8987a294 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 894 | } else if (isa<UsingDecl>(OldD)) { |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 895 | // We can overload with these, which can show up when doing |
| 896 | // redeclaration checks for UsingDecls. |
| 897 | assert(Old.getLookupKind() == LookupUsingDeclName); |
John McCall | a8987a294 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 898 | } else if (isa<TagDecl>(OldD)) { |
| 899 | // We can always overload with tags by hiding them. |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 900 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 901 | // Optimistically assume that an unresolved using decl will |
| 902 | // overload; if it doesn't, we'll have to diagnose during |
| 903 | // template instantiation. |
| 904 | } else { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 905 | // (C++ 13p1): |
| 906 | // Only function declarations can be overloaded; object and type |
| 907 | // declarations cannot be overloaded. |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 908 | Match = *I; |
| 909 | return Ovl_NonFunction; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 910 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 911 | } |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 912 | |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 913 | return Ovl_Overload; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 914 | } |
| 915 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 916 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| 917 | bool UseUsingDeclRules) { |
John McCall | 8246e35 | 2010-08-12 07:09:11 +0000 | [diff] [blame] | 918 | // If both of the functions are extern "C", then they are not |
| 919 | // overloads. |
| 920 | if (Old->isExternC() && New->isExternC()) |
| 921 | return false; |
| 922 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 923 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 924 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 925 | |
| 926 | // C++ [temp.fct]p2: |
| 927 | // A function template can be overloaded with other function templates |
| 928 | // and with normal (non-template) functions. |
| 929 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 930 | return true; |
| 931 | |
| 932 | // Is the function New an overload of the function Old? |
| 933 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 934 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 935 | |
| 936 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 937 | // determine whether they are overloads. If we find any mismatch |
| 938 | // in the signature, they are overloads. |
| 939 | |
| 940 | // If either of these functions is a K&R-style function (no |
| 941 | // prototype), then we consider them to have matching signatures. |
| 942 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 943 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 944 | return false; |
| 945 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 946 | const FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 947 | const FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 948 | |
| 949 | // The signature of a function includes the types of its |
| 950 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 951 | // of the ellipsis; see C++ DR 357). |
| 952 | if (OldQType != NewQType && |
| 953 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 954 | OldType->isVariadic() != NewType->isVariadic() || |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 955 | !FunctionArgTypesAreEqual(OldType, NewType))) |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 956 | return true; |
| 957 | |
| 958 | // C++ [temp.over.link]p4: |
| 959 | // The signature of a function template consists of its function |
| 960 | // signature, its return type and its template parameter list. The names |
| 961 | // of the template parameters are significant only for establishing the |
| 962 | // relationship between the template parameters and the rest of the |
| 963 | // signature. |
| 964 | // |
| 965 | // We check the return type and template parameter lists for function |
| 966 | // templates first; the remaining checks follow. |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 967 | // |
| 968 | // However, we don't consider either of these when deciding whether |
| 969 | // a member introduced by a shadow declaration is hidden. |
| 970 | if (!UseUsingDeclRules && NewTemplate && |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 971 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 972 | OldTemplate->getTemplateParameters(), |
| 973 | false, TPL_TemplateMatch) || |
| 974 | OldType->getResultType() != NewType->getResultType())) |
| 975 | return true; |
| 976 | |
| 977 | // If the function is a class member, its signature includes the |
Douglas Gregor | b2f8aa9 | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 978 | // cv-qualifiers (if any) and ref-qualifier (if any) on the function itself. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 979 | // |
| 980 | // As part of this, also check whether one of the member functions |
| 981 | // is static, in which case they are not overloads (C++ |
| 982 | // 13.1p2). While not part of the definition of the signature, |
| 983 | // this check is important to determine whether these functions |
| 984 | // can be overloaded. |
| 985 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 986 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 987 | if (OldMethod && NewMethod && |
| 988 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
Douglas Gregor | b2f8aa9 | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 989 | (OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers() || |
Douglas Gregor | c83f9865 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 990 | OldMethod->getRefQualifier() != NewMethod->getRefQualifier())) { |
| 991 | if (!UseUsingDeclRules && |
| 992 | OldMethod->getRefQualifier() != NewMethod->getRefQualifier() && |
| 993 | (OldMethod->getRefQualifier() == RQ_None || |
| 994 | NewMethod->getRefQualifier() == RQ_None)) { |
| 995 | // C++0x [over.load]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 996 | // - Member function declarations with the same name and the same |
| 997 | // parameter-type-list as well as member function template |
| 998 | // declarations with the same name, the same parameter-type-list, and |
| 999 | // the same template parameter lists cannot be overloaded if any of |
Douglas Gregor | c83f9865 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1000 | // them, but not all, have a ref-qualifier (8.3.5). |
| 1001 | Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) |
| 1002 | << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); |
| 1003 | Diag(OldMethod->getLocation(), diag::note_previous_declaration); |
| 1004 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1005 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1006 | return true; |
Douglas Gregor | c83f9865 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 1007 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1008 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1009 | // The signatures match; this is not an overload. |
| 1010 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Argyrios Kyrtzidis | ab72b67 | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 1013 | /// \brief Checks availability of the function depending on the current |
| 1014 | /// function context. Inside an unavailable function, unavailability is ignored. |
| 1015 | /// |
| 1016 | /// \returns true if \arg FD is unavailable and current context is inside |
| 1017 | /// an available function, false otherwise. |
| 1018 | bool Sema::isFunctionConsideredUnavailable(FunctionDecl *FD) { |
| 1019 | return FD->isUnavailable() && !cast<Decl>(CurContext)->isUnavailable(); |
| 1020 | } |
| 1021 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1022 | /// \brief Tries a user-defined conversion from From to ToType. |
| 1023 | /// |
| 1024 | /// Produces an implicit conversion sequence for when a standard conversion |
| 1025 | /// is not an option. See TryImplicitConversion for more information. |
| 1026 | static ImplicitConversionSequence |
| 1027 | TryUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 1028 | bool SuppressUserConversions, |
| 1029 | bool AllowExplicit, |
| 1030 | bool InOverloadResolution, |
| 1031 | bool CStyle, |
| 1032 | bool AllowObjCWritebackConversion) { |
| 1033 | ImplicitConversionSequence ICS; |
| 1034 | |
| 1035 | if (SuppressUserConversions) { |
| 1036 | // We're not in the case above, so there is no conversion that |
| 1037 | // we can perform. |
| 1038 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1039 | return ICS; |
| 1040 | } |
| 1041 | |
| 1042 | // Attempt user-defined conversion. |
| 1043 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 1044 | OverloadingResult UserDefResult |
| 1045 | = IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, Conversions, |
| 1046 | AllowExplicit); |
| 1047 | |
| 1048 | if (UserDefResult == OR_Success) { |
| 1049 | ICS.setUserDefined(); |
| 1050 | // C++ [over.ics.user]p4: |
| 1051 | // A conversion of an expression of class type to the same class |
| 1052 | // type is given Exact Match rank, and a conversion of an |
| 1053 | // expression of class type to a base class of that type is |
| 1054 | // given Conversion rank, in spite of the fact that a copy |
| 1055 | // constructor (i.e., a user-defined conversion function) is |
| 1056 | // called for those cases. |
| 1057 | if (CXXConstructorDecl *Constructor |
| 1058 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
| 1059 | QualType FromCanon |
| 1060 | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 1061 | QualType ToCanon |
| 1062 | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 1063 | if (Constructor->isCopyConstructor() && |
| 1064 | (FromCanon == ToCanon || S.IsDerivedFrom(FromCanon, ToCanon))) { |
| 1065 | // Turn this into a "standard" conversion sequence, so that it |
| 1066 | // gets ranked with standard conversion sequences. |
| 1067 | ICS.setStandard(); |
| 1068 | ICS.Standard.setAsIdentityConversion(); |
| 1069 | ICS.Standard.setFromType(From->getType()); |
| 1070 | ICS.Standard.setAllToTypes(ToType); |
| 1071 | ICS.Standard.CopyConstructor = Constructor; |
| 1072 | if (ToCanon != FromCanon) |
| 1073 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | // C++ [over.best.ics]p4: |
| 1078 | // However, when considering the argument of a user-defined |
| 1079 | // conversion function that is a candidate by 13.3.1.3 when |
| 1080 | // invoked for the copying of the temporary in the second step |
| 1081 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 1082 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 1083 | // ellipsis conversion sequences are allowed. |
| 1084 | if (SuppressUserConversions && ICS.isUserDefined()) { |
| 1085 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
| 1086 | } |
| 1087 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
| 1088 | ICS.setAmbiguous(); |
| 1089 | ICS.Ambiguous.setFromType(From->getType()); |
| 1090 | ICS.Ambiguous.setToType(ToType); |
| 1091 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 1092 | Cand != Conversions.end(); ++Cand) |
| 1093 | if (Cand->Viable) |
| 1094 | ICS.Ambiguous.addConversion(Cand->Function); |
| 1095 | } else { |
| 1096 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 1097 | } |
| 1098 | |
| 1099 | return ICS; |
| 1100 | } |
| 1101 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1102 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 1103 | /// from the given expression (Expr) to the given type (ToType). This |
| 1104 | /// function returns an implicit conversion sequence that can be used |
| 1105 | /// to perform the initialization. Given |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1106 | /// |
| 1107 | /// void f(float f); |
| 1108 | /// void g(int i) { f(i); } |
| 1109 | /// |
| 1110 | /// this routine would produce an implicit conversion sequence to |
| 1111 | /// describe the initialization of f from i, which will be a standard |
| 1112 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 1113 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 1114 | // |
| 1115 | /// Note that this routine only determines how the conversion can be |
| 1116 | /// performed; it does not actually perform the conversion. As such, |
| 1117 | /// it will not produce any diagnostics if no conversion is available, |
| 1118 | /// but will instead return an implicit conversion sequence of kind |
| 1119 | /// "BadConversion". |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1120 | /// |
| 1121 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 1122 | /// not permitted. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1123 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 1124 | /// permitted. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1125 | /// |
| 1126 | /// \param AllowObjCWritebackConversion Whether we allow the Objective-C |
| 1127 | /// writeback conversion, which allows __autoreleasing id* parameters to |
| 1128 | /// be initialized with __strong id* or __weak id* arguments. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1129 | static ImplicitConversionSequence |
| 1130 | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
| 1131 | bool SuppressUserConversions, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1132 | bool AllowExplicit, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1133 | bool InOverloadResolution, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1134 | bool CStyle, |
| 1135 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1136 | ImplicitConversionSequence ICS; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1137 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1138 | ICS.Standard, CStyle, AllowObjCWritebackConversion)){ |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1139 | ICS.setStandard(); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1140 | return ICS; |
| 1141 | } |
| 1142 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1143 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 1144 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1145 | return ICS; |
| 1146 | } |
| 1147 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1148 | // C++ [over.ics.user]p4: |
| 1149 | // A conversion of an expression of class type to the same class |
| 1150 | // type is given Exact Match rank, and a conversion of an |
| 1151 | // expression of class type to a base class of that type is |
| 1152 | // given Conversion rank, in spite of the fact that a copy/move |
| 1153 | // constructor (i.e., a user-defined conversion function) is |
| 1154 | // called for those cases. |
| 1155 | QualType FromType = From->getType(); |
| 1156 | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>() && |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1157 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
| 1158 | S.IsDerivedFrom(FromType, ToType))) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1159 | ICS.setStandard(); |
| 1160 | ICS.Standard.setAsIdentityConversion(); |
| 1161 | ICS.Standard.setFromType(FromType); |
| 1162 | ICS.Standard.setAllToTypes(ToType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1163 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1164 | // We don't actually check at this point whether there is a valid |
| 1165 | // copy/move constructor, since overloading just assumes that it |
| 1166 | // exists. When we actually perform initialization, we'll find the |
| 1167 | // appropriate constructor to copy the returned object, if needed. |
| 1168 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1169 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1170 | // Determine whether this is considered a derived-to-base conversion. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1171 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1172 | ICS.Standard.Second = ICK_Derived_To_Base; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1173 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 1174 | return ICS; |
| 1175 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1176 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 1177 | return TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 1178 | AllowExplicit, InOverloadResolution, CStyle, |
| 1179 | AllowObjCWritebackConversion); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1182 | ImplicitConversionSequence |
| 1183 | Sema::TryImplicitConversion(Expr *From, QualType ToType, |
| 1184 | bool SuppressUserConversions, |
| 1185 | bool AllowExplicit, |
| 1186 | bool InOverloadResolution, |
| 1187 | bool CStyle, |
| 1188 | bool AllowObjCWritebackConversion) { |
| 1189 | return clang::TryImplicitConversion(*this, From, ToType, |
| 1190 | SuppressUserConversions, AllowExplicit, |
| 1191 | InOverloadResolution, CStyle, |
| 1192 | AllowObjCWritebackConversion); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1195 | /// PerformImplicitConversion - Perform an implicit conversion of the |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1196 | /// expression From to the type ToType. Returns the |
Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1197 | /// converted expression. Flavor is the kind of conversion we're |
| 1198 | /// performing, used in the error message. If @p AllowExplicit, |
| 1199 | /// explicit user-defined conversions are permitted. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1200 | ExprResult |
| 1201 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Sebastian Redl | cc15264 | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1202 | AssignmentAction Action, bool AllowExplicit) { |
Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1203 | ImplicitConversionSequence ICS; |
Sebastian Redl | cc15264 | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1204 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1207 | ExprResult |
| 1208 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1209 | AssignmentAction Action, bool AllowExplicit, |
Sebastian Redl | cc15264 | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 1210 | ImplicitConversionSequence& ICS) { |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1211 | if (checkPlaceholderForOverload(*this, From)) |
| 1212 | return ExprError(); |
| 1213 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1214 | // Objective-C ARC: Determine whether we will allow the writeback conversion. |
| 1215 | bool AllowObjCWritebackConversion |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1216 | = getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1217 | (Action == AA_Passing || Action == AA_Sending); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1218 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1219 | ICS = clang::TryImplicitConversion(*this, From, ToType, |
| 1220 | /*SuppressUserConversions=*/false, |
| 1221 | AllowExplicit, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1222 | /*InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1223 | /*CStyle=*/false, |
| 1224 | AllowObjCWritebackConversion); |
Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1225 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 1226 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1227 | |
| 1228 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1229 | /// conversion that strips "noreturn" off the nested function type. |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1230 | bool Sema::IsNoReturnConversion(QualType FromType, QualType ToType, |
| 1231 | QualType &ResultTy) { |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1232 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1233 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1234 | |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1235 | // Permit the conversion F(t __attribute__((noreturn))) -> F(t) |
| 1236 | // where F adds one of the following at most once: |
| 1237 | // - a pointer |
| 1238 | // - a member pointer |
| 1239 | // - a block pointer |
| 1240 | CanQualType CanTo = Context.getCanonicalType(ToType); |
| 1241 | CanQualType CanFrom = Context.getCanonicalType(FromType); |
| 1242 | Type::TypeClass TyClass = CanTo->getTypeClass(); |
| 1243 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1244 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) { |
| 1245 | if (TyClass == Type::Pointer) { |
| 1246 | CanTo = CanTo.getAs<PointerType>()->getPointeeType(); |
| 1247 | CanFrom = CanFrom.getAs<PointerType>()->getPointeeType(); |
| 1248 | } else if (TyClass == Type::BlockPointer) { |
| 1249 | CanTo = CanTo.getAs<BlockPointerType>()->getPointeeType(); |
| 1250 | CanFrom = CanFrom.getAs<BlockPointerType>()->getPointeeType(); |
| 1251 | } else if (TyClass == Type::MemberPointer) { |
| 1252 | CanTo = CanTo.getAs<MemberPointerType>()->getPointeeType(); |
| 1253 | CanFrom = CanFrom.getAs<MemberPointerType>()->getPointeeType(); |
| 1254 | } else { |
| 1255 | return false; |
| 1256 | } |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1257 | |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1258 | TyClass = CanTo->getTypeClass(); |
| 1259 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1260 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) |
| 1261 | return false; |
| 1262 | } |
| 1263 | |
| 1264 | const FunctionType *FromFn = cast<FunctionType>(CanFrom); |
| 1265 | FunctionType::ExtInfo EInfo = FromFn->getExtInfo(); |
| 1266 | if (!EInfo.getNoReturn()) return false; |
| 1267 | |
| 1268 | FromFn = Context.adjustFunctionType(FromFn, EInfo.withNoReturn(false)); |
| 1269 | assert(QualType(FromFn, 0).isCanonical()); |
| 1270 | if (QualType(FromFn, 0) != CanTo) return false; |
| 1271 | |
| 1272 | ResultTy = ToType; |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1273 | return true; |
| 1274 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1275 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1276 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 1277 | /// vector conversion. |
| 1278 | /// |
| 1279 | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
| 1280 | /// conversion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1281 | static bool IsVectorConversion(ASTContext &Context, QualType FromType, |
| 1282 | QualType ToType, ImplicitConversionKind &ICK) { |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1283 | // We need at least one of these types to be a vector type to have a vector |
| 1284 | // conversion. |
| 1285 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
| 1286 | return false; |
| 1287 | |
| 1288 | // Identical types require no conversions. |
| 1289 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1290 | return false; |
| 1291 | |
| 1292 | // There are no conversions between extended vector types, only identity. |
| 1293 | if (ToType->isExtVectorType()) { |
| 1294 | // There are no conversions between extended vector types other than the |
| 1295 | // identity conversion. |
| 1296 | if (FromType->isExtVectorType()) |
| 1297 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1298 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1299 | // Vector splat from any arithmetic type to a vector. |
Douglas Gregor | a3208f9 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 1300 | if (FromType->isArithmeticType()) { |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1301 | ICK = ICK_Vector_Splat; |
| 1302 | return true; |
| 1303 | } |
| 1304 | } |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1305 | |
| 1306 | // We can perform the conversion between vector types in the following cases: |
| 1307 | // 1)vector types are equivalent AltiVec and GCC vector types |
| 1308 | // 2)lax vector conversions are permitted and the vector types are of the |
| 1309 | // same size |
| 1310 | if (ToType->isVectorType() && FromType->isVectorType()) { |
| 1311 | if (Context.areCompatibleVectorTypes(FromType, ToType) || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1312 | (Context.getLangOpts().LaxVectorConversions && |
Chandler Carruth | 9c524c1 | 2010-08-08 05:02:51 +0000 | [diff] [blame] | 1313 | (Context.getTypeSize(FromType) == Context.getTypeSize(ToType)))) { |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1314 | ICK = ICK_Vector_Conversion; |
| 1315 | return true; |
| 1316 | } |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1317 | } |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1318 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1319 | return false; |
| 1320 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1321 | |
Douglas Gregor | f9e36cc | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1322 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 1323 | bool InOverloadResolution, |
| 1324 | StandardConversionSequence &SCS, |
| 1325 | bool CStyle); |
Douglas Gregor | c79862f | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1326 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1327 | /// IsStandardConversion - Determines whether there is a standard |
| 1328 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 1329 | /// expression From to the type ToType. Standard conversion sequences |
| 1330 | /// only consider non-class types; for conversions that involve class |
| 1331 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 1332 | /// contain the standard conversion sequence required to perform this |
| 1333 | /// conversion and this routine will return true. Otherwise, this |
| 1334 | /// routine will return false and the value of SCS is unspecified. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1335 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 1336 | bool InOverloadResolution, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1337 | StandardConversionSequence &SCS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1338 | bool CStyle, |
| 1339 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1340 | QualType FromType = From->getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1341 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1342 | // Standard conversions (C++ [conv]) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1343 | SCS.setAsIdentityConversion(); |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1344 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1345 | SCS.IncompatibleObjC = false; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1346 | SCS.setFromType(FromType); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1347 | SCS.CopyConstructor = 0; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1348 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1349 | // There are no standard conversions for class types in C++, so |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1350 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1351 | if (FromType->isRecordType() || ToType->isRecordType()) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1352 | if (S.getLangOpts().CPlusPlus) |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1353 | return false; |
| 1354 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1355 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1358 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 1359 | // array-to-pointer conversion, or function-to-pointer conversion |
| 1360 | // (C++ 4p1). |
| 1361 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1362 | if (FromType == S.Context.OverloadTy) { |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1363 | DeclAccessPair AccessPair; |
| 1364 | if (FunctionDecl *Fn |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1365 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1366 | AccessPair)) { |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1367 | // We were able to resolve the address of the overloaded function, |
| 1368 | // so we can convert to the type of that function. |
| 1369 | FromType = Fn->getType(); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1370 | |
| 1371 | // we can sometimes resolve &foo<int> regardless of ToType, so check |
| 1372 | // if the type matches (identity) or we are converting to bool |
| 1373 | if (!S.Context.hasSameUnqualifiedType( |
| 1374 | S.ExtractUnqualifiedFunctionType(ToType), FromType)) { |
| 1375 | QualType resultTy; |
| 1376 | // if the function type matches except for [[noreturn]], it's ok |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1377 | if (!S.IsNoReturnConversion(FromType, |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1378 | S.ExtractUnqualifiedFunctionType(ToType), resultTy)) |
| 1379 | // otherwise, only a boolean conversion is standard |
| 1380 | if (!ToType->isBooleanType()) |
| 1381 | return false; |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1382 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1383 | |
Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1384 | // Check if the "from" expression is taking the address of an overloaded |
| 1385 | // function and recompute the FromType accordingly. Take advantage of the |
| 1386 | // fact that non-static member functions *must* have such an address-of |
| 1387 | // expression. |
| 1388 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn); |
| 1389 | if (Method && !Method->isStatic()) { |
| 1390 | assert(isa<UnaryOperator>(From->IgnoreParens()) && |
| 1391 | "Non-unary operator on non-static member address"); |
| 1392 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() |
| 1393 | == UO_AddrOf && |
| 1394 | "Non-address-of operator on non-static member address"); |
| 1395 | const Type *ClassType |
| 1396 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 1397 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
Chandler Carruth | 7750f76 | 2011-03-29 18:38:10 +0000 | [diff] [blame] | 1398 | } else if (isa<UnaryOperator>(From->IgnoreParens())) { |
| 1399 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() == |
| 1400 | UO_AddrOf && |
Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1401 | "Non-address-of operator for overloaded function expression"); |
| 1402 | FromType = S.Context.getPointerType(FromType); |
| 1403 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1404 | |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1405 | // Check that we've computed the proper type after overload resolution. |
Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1406 | assert(S.Context.hasSameType( |
| 1407 | FromType, |
| 1408 | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1409 | } else { |
| 1410 | return false; |
| 1411 | } |
Anders Carlsson | ba37e1e | 2010-11-04 05:28:09 +0000 | [diff] [blame] | 1412 | } |
John McCall | 154a2fd | 2011-08-30 00:57:29 +0000 | [diff] [blame] | 1413 | // Lvalue-to-rvalue conversion (C++11 4.1): |
| 1414 | // A glvalue (3.10) of a non-function, non-array type T can |
| 1415 | // be converted to a prvalue. |
| 1416 | bool argIsLValue = From->isGLValue(); |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1417 | if (argIsLValue && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1418 | !FromType->isFunctionType() && !FromType->isArrayType() && |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1419 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1420 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1421 | |
Douglas Gregor | c79862f | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1422 | // C11 6.3.2.1p2: |
| 1423 | // ... if the lvalue has atomic type, the value has the non-atomic version |
| 1424 | // of the type of the lvalue ... |
| 1425 | if (const AtomicType *Atomic = FromType->getAs<AtomicType>()) |
| 1426 | FromType = Atomic->getValueType(); |
| 1427 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1428 | // If T is a non-class type, the type of the rvalue is the |
| 1429 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1430 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 1431 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1432 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1433 | } else if (FromType->isArrayType()) { |
| 1434 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1435 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1436 | |
| 1437 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 1438 | // bound of T" can be converted to an rvalue of type "pointer to |
| 1439 | // T" (C++ 4.2p1). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1440 | FromType = S.Context.getArrayDecayedType(FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1441 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1442 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1443 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1444 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1445 | |
| 1446 | // For the purpose of ranking in overload resolution |
| 1447 | // (13.3.3.1.1), this conversion is considered an |
| 1448 | // array-to-pointer conversion followed by a qualification |
| 1449 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1450 | SCS.Second = ICK_Identity; |
| 1451 | SCS.Third = ICK_Qualification; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1452 | SCS.QualificationIncludesObjCLifetime = false; |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1453 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1454 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1455 | } |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1456 | } else if (FromType->isFunctionType() && argIsLValue) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1457 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1458 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1459 | |
| 1460 | // An lvalue of function type T can be converted to an rvalue of |
| 1461 | // type "pointer to T." The result is a pointer to the |
| 1462 | // function. (C++ 4.3p1). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1463 | FromType = S.Context.getPointerType(FromType); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1464 | } else { |
| 1465 | // We don't require any conversions for the first step. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1466 | SCS.First = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1467 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1468 | SCS.setToType(0, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1469 | |
| 1470 | // The second conversion can be an integral promotion, floating |
| 1471 | // point promotion, integral conversion, floating point conversion, |
| 1472 | // floating-integral conversion, pointer conversion, |
| 1473 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1474 | // For overloading in C, this can also be a "compatible-type" |
| 1475 | // conversion. |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1476 | bool IncompatibleObjC = false; |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1477 | ImplicitConversionKind SecondICK = ICK_Identity; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1478 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1479 | // The unqualified versions of the types are the same: there's no |
| 1480 | // conversion to do. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1481 | SCS.Second = ICK_Identity; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1482 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1483 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1484 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1485 | FromType = ToType.getUnqualifiedType(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1486 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1487 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1488 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1489 | FromType = ToType.getUnqualifiedType(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1490 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1491 | // Complex promotion (Clang extension) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1492 | SCS.Second = ICK_Complex_Promotion; |
| 1493 | FromType = ToType.getUnqualifiedType(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1494 | } else if (ToType->isBooleanType() && |
| 1495 | (FromType->isArithmeticType() || |
| 1496 | FromType->isAnyPointerType() || |
| 1497 | FromType->isBlockPointerType() || |
| 1498 | FromType->isMemberPointerType() || |
| 1499 | FromType->isNullPtrType())) { |
| 1500 | // Boolean conversions (C++ 4.12). |
| 1501 | SCS.Second = ICK_Boolean_Conversion; |
| 1502 | FromType = S.Context.BoolTy; |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1503 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1504 | ToType->isIntegralType(S.Context)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1505 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1506 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1507 | FromType = ToType.getUnqualifiedType(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1508 | } else if (FromType->isAnyComplexType() && ToType->isComplexType()) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1509 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1510 | SCS.Second = ICK_Complex_Conversion; |
| 1511 | FromType = ToType.getUnqualifiedType(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1512 | } else if ((FromType->isAnyComplexType() && ToType->isArithmeticType()) || |
| 1513 | (ToType->isAnyComplexType() && FromType->isArithmeticType())) { |
Chandler Carruth | 8fa1e7e | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1514 | // Complex-real conversions (C99 6.3.1.7) |
| 1515 | SCS.Second = ICK_Complex_Real; |
| 1516 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 49b4d73 | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1517 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
Chandler Carruth | 8fa1e7e | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1518 | // Floating point conversions (C++ 4.8). |
| 1519 | SCS.Second = ICK_Floating_Conversion; |
| 1520 | FromType = ToType.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1521 | } else if ((FromType->isRealFloatingType() && |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1522 | ToType->isIntegralType(S.Context)) || |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1523 | (FromType->isIntegralOrUnscopedEnumerationType() && |
Douglas Gregor | 49b4d73 | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1524 | ToType->isRealFloatingType())) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1525 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1526 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1527 | FromType = ToType.getUnqualifiedType(); |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1528 | } else if (S.IsBlockPointerConversion(FromType, ToType, FromType)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1529 | SCS.Second = ICK_Block_Pointer_Conversion; |
| 1530 | } else if (AllowObjCWritebackConversion && |
| 1531 | S.isObjCWritebackConversion(FromType, ToType, FromType)) { |
| 1532 | SCS.Second = ICK_Writeback_Conversion; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1533 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1534 | FromType, IncompatibleObjC)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1535 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1536 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1537 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1538 | FromType = FromType.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1539 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1540 | InOverloadResolution, FromType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1541 | // Pointer to member conversions (4.11). |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1542 | SCS.Second = ICK_Pointer_Member; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1543 | } else if (IsVectorConversion(S.Context, FromType, ToType, SecondICK)) { |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1544 | SCS.Second = SecondICK; |
| 1545 | FromType = ToType.getUnqualifiedType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1546 | } else if (!S.getLangOpts().CPlusPlus && |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1547 | S.Context.typesAreCompatible(ToType, FromType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1548 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1549 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1550 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1551 | } else if (S.IsNoReturnConversion(FromType, ToType, FromType)) { |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1552 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1553 | SCS.Second = ICK_NoReturn_Adjustment; |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1554 | } else if (IsTransparentUnionStandardConversion(S, From, ToType, |
| 1555 | InOverloadResolution, |
| 1556 | SCS, CStyle)) { |
| 1557 | SCS.Second = ICK_TransparentUnionConversion; |
| 1558 | FromType = ToType; |
Douglas Gregor | f9e36cc | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 1559 | } else if (tryAtomicConversion(S, From, ToType, InOverloadResolution, SCS, |
| 1560 | CStyle)) { |
| 1561 | // tryAtomicConversion has updated the standard conversion sequence |
Douglas Gregor | c79862f | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 1562 | // appropriately. |
| 1563 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1564 | } else { |
| 1565 | // No second conversion required. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1566 | SCS.Second = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1567 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1568 | SCS.setToType(1, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1569 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1570 | QualType CanonFrom; |
| 1571 | QualType CanonTo; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1572 | // The third conversion can be a qualification conversion (C++ 4p1). |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1573 | bool ObjCLifetimeConversion; |
| 1574 | if (S.IsQualificationConversion(FromType, ToType, CStyle, |
| 1575 | ObjCLifetimeConversion)) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1576 | SCS.Third = ICK_Qualification; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1577 | SCS.QualificationIncludesObjCLifetime = ObjCLifetimeConversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1578 | FromType = ToType; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1579 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1580 | CanonTo = S.Context.getCanonicalType(ToType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1581 | } else { |
| 1582 | // No conversion required |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1583 | SCS.Third = ICK_Identity; |
| 1584 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1585 | // C++ [over.best.ics]p6: |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1586 | // [...] Any difference in top-level cv-qualification is |
| 1587 | // subsumed by the initialization itself and does not constitute |
| 1588 | // a conversion. [...] |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1589 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1590 | CanonTo = S.Context.getCanonicalType(ToType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1591 | if (CanonFrom.getLocalUnqualifiedType() |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1592 | == CanonTo.getLocalUnqualifiedType() && |
Fariborz Jahanian | 9f963c2 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 1593 | (CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers() |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1594 | || CanonFrom.getObjCGCAttr() != CanonTo.getObjCGCAttr() |
| 1595 | || CanonFrom.getObjCLifetime() != CanonTo.getObjCLifetime())) { |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1596 | FromType = ToType; |
| 1597 | CanonFrom = CanonTo; |
| 1598 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1599 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1600 | SCS.setToType(2, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1601 | |
| 1602 | // If we have not converted the argument type to the parameter type, |
| 1603 | // this is a bad conversion sequence. |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1604 | if (CanonFrom != CanonTo) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1605 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1606 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1607 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1608 | } |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1609 | |
| 1610 | static bool |
| 1611 | IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 1612 | QualType &ToType, |
| 1613 | bool InOverloadResolution, |
| 1614 | StandardConversionSequence &SCS, |
| 1615 | bool CStyle) { |
| 1616 | |
| 1617 | const RecordType *UT = ToType->getAsUnionType(); |
| 1618 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 1619 | return false; |
| 1620 | // The field to initialize within the transparent union. |
| 1621 | RecordDecl *UD = UT->getDecl(); |
| 1622 | // It's compatible if the expression matches any of the fields. |
| 1623 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1624 | itend = UD->field_end(); |
| 1625 | it != itend; ++it) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1626 | if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS, |
| 1627 | CStyle, /*ObjCWritebackConversion=*/false)) { |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1628 | ToType = it->getType(); |
| 1629 | return true; |
| 1630 | } |
| 1631 | } |
| 1632 | return false; |
| 1633 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1634 | |
| 1635 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1636 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1637 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1638 | /// sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1639 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1640 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | ee54797 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1641 | // All integers are built-in. |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1642 | if (!To) { |
| 1643 | return false; |
| 1644 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1645 | |
| 1646 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1647 | // unsigned short int can be converted to an rvalue of type int if |
| 1648 | // int can represent all the values of the source type; otherwise, |
| 1649 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1650 | // int (C++ 4.5p1). |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1651 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1652 | !FromType->isEnumeralType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1653 | if (// We can promote any signed, promotable integer type to an int |
| 1654 | (FromType->isSignedIntegerType() || |
| 1655 | // We can promote any unsigned integer type whose size is |
| 1656 | // less than int to an int. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1657 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1658 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1659 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1662 | return To->getKind() == BuiltinType::UInt; |
| 1663 | } |
| 1664 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1665 | // C++0x [conv.prom]p3: |
| 1666 | // A prvalue of an unscoped enumeration type whose underlying type is not |
| 1667 | // fixed (7.2) can be converted to an rvalue a prvalue of the first of the |
| 1668 | // following types that can represent all the values of the enumeration |
| 1669 | // (i.e., the values in the range bmin to bmax as described in 7.2): int, |
| 1670 | // unsigned int, long int, unsigned long int, long long int, or unsigned |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1671 | // long long int. If none of the types in that list can represent all the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1672 | // values of the enumeration, an rvalue a prvalue of an unscoped enumeration |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1673 | // type can be converted to an rvalue a prvalue of the extended integer type |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1674 | // with lowest integer conversion rank (4.13) greater than the rank of long |
| 1675 | // long in which all the values of the enumeration can be represented. If |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1676 | // there are two such extended types, the signed one is chosen. |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1677 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
| 1678 | // C++0x 7.2p9: Note that this implicit enum to int conversion is not |
| 1679 | // provided for a scoped enumeration. |
| 1680 | if (FromEnumType->getDecl()->isScoped()) |
| 1681 | return false; |
| 1682 | |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1683 | // We have already pre-calculated the promotion type, so this is trivial. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1684 | if (ToType->isIntegerType() && |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1685 | !RequireCompleteType(From->getLocStart(), FromType, 0)) |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1686 | return Context.hasSameUnqualifiedType(ToType, |
| 1687 | FromEnumType->getDecl()->getPromotionType()); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1688 | } |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1689 | |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1690 | // C++0x [conv.prom]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1691 | // A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted |
| 1692 | // to an rvalue a prvalue of the first of the following types that can |
| 1693 | // represent all the values of its underlying type: int, unsigned int, |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1694 | // long int, unsigned long int, long long int, or unsigned long long int. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1695 | // If none of the types in that list can represent all the values of its |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1696 | // underlying type, an rvalue a prvalue of type char16_t, char32_t, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1697 | // or wchar_t can be converted to an rvalue a prvalue of its underlying |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1698 | // type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1699 | if (FromType->isAnyCharacterType() && !FromType->isCharType() && |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1700 | ToType->isIntegerType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1701 | // Determine whether the type we're converting from is signed or |
| 1702 | // unsigned. |
David Majnemer | fa01a58 | 2011-07-22 21:09:04 +0000 | [diff] [blame] | 1703 | bool FromIsSigned = FromType->isSignedIntegerType(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1704 | uint64_t FromSize = Context.getTypeSize(FromType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1705 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1706 | // The types we'll try to promote to, in the appropriate |
| 1707 | // order. Try each of these types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1708 | QualType PromoteTypes[6] = { |
| 1709 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1710 | Context.LongTy, Context.UnsignedLongTy , |
| 1711 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1712 | }; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1713 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1714 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1715 | if (FromSize < ToSize || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1716 | (FromSize == ToSize && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1717 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1718 | // We found the type that we can promote to. If this is the |
| 1719 | // type we wanted, we have a promotion. Otherwise, no |
| 1720 | // promotion. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1721 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1722 | } |
| 1723 | } |
| 1724 | } |
| 1725 | |
| 1726 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1727 | // rvalue of type int if int can represent all the values of the |
| 1728 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1729 | // unsigned int can represent all the values of the bit-field. If |
| 1730 | // the bit-field is larger yet, no integral promotion applies to |
| 1731 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1732 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1733 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1734 | // conversion. |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1735 | using llvm::APSInt; |
| 1736 | if (From) |
| 1737 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1738 | APSInt BitWidth; |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1739 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1740 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1741 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1742 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1743 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1744 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1745 | if (BitWidth < ToSize || |
| 1746 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1747 | return To->getKind() == BuiltinType::Int; |
| 1748 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1749 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1750 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1751 | // that fits into an unsigned int? |
| 1752 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1753 | return To->getKind() == BuiltinType::UInt; |
| 1754 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1755 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1756 | return false; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1757 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1758 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1759 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1760 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1761 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1762 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1763 | return true; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1764 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1765 | |
| 1766 | return false; |
| 1767 | } |
| 1768 | |
| 1769 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1770 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1771 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1772 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1773 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1774 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1775 | /// An rvalue of type float can be converted to an rvalue of type |
| 1776 | /// double. (C++ 4.6p1). |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1777 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1778 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1779 | return true; |
| 1780 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1781 | // C99 6.3.1.5p1: |
| 1782 | // When a float is promoted to double or long double, or a |
| 1783 | // double is promoted to long double [...]. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1784 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1785 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1786 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1787 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1788 | return true; |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1789 | |
| 1790 | // Half can be promoted to float. |
| 1791 | if (FromBuiltin->getKind() == BuiltinType::Half && |
| 1792 | ToBuiltin->getKind() == BuiltinType::Float) |
| 1793 | return true; |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1796 | return false; |
| 1797 | } |
| 1798 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1799 | /// \brief Determine if a conversion is a complex promotion. |
| 1800 | /// |
| 1801 | /// A complex promotion is defined as a complex -> complex conversion |
| 1802 | /// where the conversion between the underlying real types is a |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1803 | /// floating-point or integral promotion. |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1804 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1805 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1806 | if (!FromComplex) |
| 1807 | return false; |
| 1808 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1809 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1810 | if (!ToComplex) |
| 1811 | return false; |
| 1812 | |
| 1813 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1814 | ToComplex->getElementType()) || |
| 1815 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1816 | ToComplex->getElementType()); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1819 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1820 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1821 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1822 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1823 | /// the right set of qualifiers on its pointee. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1824 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1825 | static QualType |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1826 | BuildSimilarlyQualifiedPointerType(const Type *FromPtr, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1827 | QualType ToPointee, QualType ToType, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1828 | ASTContext &Context, |
| 1829 | bool StripObjCLifetime = false) { |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1830 | assert((FromPtr->getTypeClass() == Type::Pointer || |
| 1831 | FromPtr->getTypeClass() == Type::ObjCObjectPointer) && |
| 1832 | "Invalid similarly-qualified pointer type"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1833 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1834 | /// Conversions to 'id' subsume cv-qualifier conversions. |
| 1835 | if (ToType->isObjCIdType() || ToType->isObjCQualifiedIdType()) |
Douglas Gregor | c6bd1d3 | 2010-12-06 22:09:19 +0000 | [diff] [blame] | 1836 | return ToType.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1837 | |
| 1838 | QualType CanonFromPointee |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1839 | = Context.getCanonicalType(FromPtr->getPointeeType()); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1840 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1841 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1842 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1843 | if (StripObjCLifetime) |
| 1844 | Quals.removeObjCLifetime(); |
| 1845 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1846 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1847 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1848 | // ToType is exactly what we need. Return it. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1849 | if (!ToType.isNull()) |
Douglas Gregor | b9f907b | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1850 | return ToType.getUnqualifiedType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1851 | |
| 1852 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1853 | // already. |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1854 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1855 | return Context.getObjCObjectPointerType(ToPointee); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1856 | return Context.getPointerType(ToPointee); |
| 1857 | } |
| 1858 | |
| 1859 | // Just build a canonical type that has the right qualifiers. |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1860 | QualType QualifiedCanonToPointee |
| 1861 | = Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), Quals); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1862 | |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1863 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1864 | return Context.getObjCObjectPointerType(QualifiedCanonToPointee); |
| 1865 | return Context.getPointerType(QualifiedCanonToPointee); |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1866 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1867 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1868 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1869 | bool InOverloadResolution, |
| 1870 | ASTContext &Context) { |
| 1871 | // Handle value-dependent integral null pointer constants correctly. |
| 1872 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1873 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1874 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1875 | return !InOverloadResolution; |
| 1876 | |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1877 | return Expr->isNullPointerConstant(Context, |
| 1878 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1879 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1880 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1881 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1882 | /// IsPointerConversion - Determines whether the conversion of the |
| 1883 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1884 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1885 | /// 4.10). If so, returns true and places the converted type (that |
| 1886 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1887 | /// ConvertedType. |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1888 | /// |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1889 | /// This routine also supports conversions to and from block pointers |
| 1890 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1891 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1892 | /// appropriate overloading rules for Objective-C, we may want to |
| 1893 | /// split the Objective-C checks into a different routine; however, |
| 1894 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1895 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1896 | /// set if the conversion is an allowed Objective-C conversion that |
| 1897 | /// should result in a warning. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1898 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1899 | bool InOverloadResolution, |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1900 | QualType& ConvertedType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1901 | bool &IncompatibleObjC) { |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1902 | IncompatibleObjC = false; |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1903 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, |
| 1904 | IncompatibleObjC)) |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1905 | return true; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1906 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1907 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1908 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1909 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 79a6b01 | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1910 | ConvertedType = ToType; |
| 1911 | return true; |
| 1912 | } |
| 1913 | |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1914 | // Blocks: Block pointers can be converted to void*. |
| 1915 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1916 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1917 | ConvertedType = ToType; |
| 1918 | return true; |
| 1919 | } |
| 1920 | // Blocks: A null pointer constant can be converted to a block |
| 1921 | // pointer type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1922 | if (ToType->isBlockPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1923 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1924 | ConvertedType = ToType; |
| 1925 | return true; |
| 1926 | } |
| 1927 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1928 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1929 | // pointer constant. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1930 | if (ToType->isNullPtrType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1931 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1932 | ConvertedType = ToType; |
| 1933 | return true; |
| 1934 | } |
| 1935 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1936 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1937 | if (!ToTypePtr) |
| 1938 | return false; |
| 1939 | |
| 1940 | // A null pointer constant can be converted to a pointer type (C++ 4.10p1). |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1941 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1942 | ConvertedType = ToType; |
| 1943 | return true; |
| 1944 | } |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1945 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1946 | // Beyond this point, both types need to be pointers |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1947 | // , including objective-c pointers. |
| 1948 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1949 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType() && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1950 | !getLangOpts().ObjCAutoRefCount) { |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1951 | ConvertedType = BuildSimilarlyQualifiedPointerType( |
| 1952 | FromType->getAs<ObjCObjectPointerType>(), |
| 1953 | ToPointeeType, |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1954 | ToType, Context); |
| 1955 | return true; |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1956 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1957 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1958 | if (!FromTypePtr) |
| 1959 | return false; |
| 1960 | |
| 1961 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1962 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1963 | // If the unqualified pointee types are the same, this can't be a |
Douglas Gregor | fb64086 | 2010-08-18 21:25:30 +0000 | [diff] [blame] | 1964 | // pointer conversion, so don't do all of the work below. |
| 1965 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
| 1966 | return false; |
| 1967 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1968 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1969 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1970 | // 4.10p2). |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1971 | if (FromPointeeType->isIncompleteOrObjectType() && |
| 1972 | ToPointeeType->isVoidType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1973 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1974 | ToPointeeType, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1975 | ToType, Context, |
| 1976 | /*StripObjCLifetime=*/true); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1977 | return true; |
| 1978 | } |
| 1979 | |
Francois Pichet | bc6ebb5 | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 1980 | // MSVC allows implicit function to void* type conversion. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1981 | if (getLangOpts().MicrosoftExt && FromPointeeType->isFunctionType() && |
Francois Pichet | bc6ebb5 | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 1982 | ToPointeeType->isVoidType()) { |
| 1983 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 1984 | ToPointeeType, |
| 1985 | ToType, Context); |
| 1986 | return true; |
| 1987 | } |
| 1988 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1989 | // When we're overloading in C, we allow a special kind of pointer |
| 1990 | // conversion for compatible-but-not-identical pointee types. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1991 | if (!getLangOpts().CPlusPlus && |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1992 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1993 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1994 | ToPointeeType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1995 | ToType, Context); |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1996 | return true; |
| 1997 | } |
| 1998 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1999 | // C++ [conv.ptr]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2000 | // |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2001 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 2002 | // can be converted to an rvalue of type "pointer to cv B," where |
| 2003 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 2004 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 2005 | // necessitates this conversion is ill-formed. The result of the |
| 2006 | // conversion is a pointer to the base class sub-object of the |
| 2007 | // derived class object. The null pointer value is converted to |
| 2008 | // the null pointer value of the destination type. |
| 2009 | // |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2010 | // Note that we do not check for ambiguity or inaccessibility |
| 2011 | // here. That is handled by CheckPointerConversion. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2012 | if (getLangOpts().CPlusPlus && |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2013 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | d28f041 | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 2014 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2015 | !RequireCompleteType(From->getLocStart(), FromPointeeType, 0) && |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2016 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2017 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 2018 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2019 | ToType, Context); |
| 2020 | return true; |
| 2021 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2022 | |
Fariborz Jahanian | bc2ee93 | 2011-04-14 20:33:36 +0000 | [diff] [blame] | 2023 | if (FromPointeeType->isVectorType() && ToPointeeType->isVectorType() && |
| 2024 | Context.areCompatibleVectorTypes(FromPointeeType, ToPointeeType)) { |
| 2025 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 2026 | ToPointeeType, |
| 2027 | ToType, Context); |
| 2028 | return true; |
| 2029 | } |
| 2030 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2031 | return false; |
| 2032 | } |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2033 | |
| 2034 | /// \brief Adopt the given qualifiers for the given type. |
| 2035 | static QualType AdoptQualifiers(ASTContext &Context, QualType T, Qualifiers Qs){ |
| 2036 | Qualifiers TQs = T.getQualifiers(); |
| 2037 | |
| 2038 | // Check whether qualifiers already match. |
| 2039 | if (TQs == Qs) |
| 2040 | return T; |
| 2041 | |
| 2042 | if (Qs.compatiblyIncludes(TQs)) |
| 2043 | return Context.getQualifiedType(T, Qs); |
| 2044 | |
| 2045 | return Context.getQualifiedType(T.getUnqualifiedType(), Qs); |
| 2046 | } |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2047 | |
| 2048 | /// isObjCPointerConversion - Determines whether this is an |
| 2049 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 2050 | /// with the same arguments and return values. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2051 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2052 | QualType& ConvertedType, |
| 2053 | bool &IncompatibleObjC) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2054 | if (!getLangOpts().ObjC1) |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2055 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2056 | |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2057 | // The set of qualifiers on the type we're converting from. |
| 2058 | Qualifiers FromQualifiers = FromType.getQualifiers(); |
| 2059 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2060 | // First, we handle all conversions on ObjC object pointer types. |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2061 | const ObjCObjectPointerType* ToObjCPtr = |
| 2062 | ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2063 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2064 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2065 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2066 | if (ToObjCPtr && FromObjCPtr) { |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2067 | // If the pointee types are the same (ignoring qualifications), |
| 2068 | // then this is not a pointer conversion. |
| 2069 | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
| 2070 | FromObjCPtr->getPointeeType())) |
| 2071 | return false; |
| 2072 | |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2073 | // Check for compatible |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2074 | // Objective C++: We're able to convert between "id" or "Class" and a |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2075 | // pointer to any interface (in both directions). |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2076 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2077 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2078 | return true; |
| 2079 | } |
| 2080 | // Conversions with Objective-C's id<...>. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2081 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2082 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2083 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 2084 | /*compare=*/false)) { |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2085 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2086 | return true; |
| 2087 | } |
| 2088 | // Objective C++: We're able to convert from a pointer to an |
| 2089 | // interface to a pointer to a different interface. |
| 2090 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | b397e43 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2091 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 2092 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2093 | if (getLangOpts().CPlusPlus && LHS && RHS && |
Fariborz Jahanian | b397e43 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 2094 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 2095 | FromObjCPtr->getPointeeType())) |
| 2096 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2097 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2098 | ToObjCPtr->getPointeeType(), |
| 2099 | ToType, Context); |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2100 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2101 | return true; |
| 2102 | } |
| 2103 | |
| 2104 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 2105 | // Okay: this is some kind of implicit downcast of Objective-C |
| 2106 | // interfaces, which is permitted. However, we're going to |
| 2107 | // complain about it. |
| 2108 | IncompatibleObjC = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2109 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2110 | ToObjCPtr->getPointeeType(), |
| 2111 | ToType, Context); |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2112 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2113 | return true; |
| 2114 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2115 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2116 | // Beyond this point, both types need to be C pointers or block pointers. |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2117 | QualType ToPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2118 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2119 | ToPointeeType = ToCPtr->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2120 | else if (const BlockPointerType *ToBlockPtr = |
Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2121 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 879cc73 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 2122 | // Objective C++: We're able to convert from a pointer to any object |
Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2123 | // to a block pointer type. |
| 2124 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2125 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2126 | return true; |
| 2127 | } |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2128 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 2129 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2130 | else if (FromType->getAs<BlockPointerType>() && |
Fariborz Jahanian | e4951fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2131 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2132 | // Objective C++: We're able to convert from a block pointer type to a |
Fariborz Jahanian | 879cc73 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 2133 | // pointer to any object. |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2134 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | e4951fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 2135 | return true; |
| 2136 | } |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2137 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2138 | return false; |
| 2139 | |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2140 | QualType FromPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2141 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2142 | FromPointeeType = FromCPtr->getPointeeType(); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2143 | else if (const BlockPointerType *FromBlockPtr = |
| 2144 | FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2145 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2146 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2147 | return false; |
| 2148 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2149 | // If we have pointers to pointers, recursively check whether this |
| 2150 | // is an Objective-C conversion. |
| 2151 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 2152 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2153 | IncompatibleObjC)) { |
| 2154 | // We always complain about this conversion. |
| 2155 | IncompatibleObjC = true; |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2156 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2157 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2158 | return true; |
| 2159 | } |
Fariborz Jahanian | 42ffdb3 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2160 | // Allow conversion of pointee being objective-c pointer to another one; |
| 2161 | // as in I* to id. |
| 2162 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 2163 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 2164 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 2165 | IncompatibleObjC)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2166 | |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 2167 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2168 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Fariborz Jahanian | 42ffdb3 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 2169 | return true; |
| 2170 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2171 | |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2172 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2173 | // differences in the argument and result types are in Objective-C |
| 2174 | // pointer conversions. If so, we permit the conversion (but |
| 2175 | // complain about it). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2176 | const FunctionProtoType *FromFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2177 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2178 | const FunctionProtoType *ToFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2179 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2180 | if (FromFunctionType && ToFunctionType) { |
| 2181 | // If the function types are exactly the same, this isn't an |
| 2182 | // Objective-C pointer conversion. |
| 2183 | if (Context.getCanonicalType(FromPointeeType) |
| 2184 | == Context.getCanonicalType(ToPointeeType)) |
| 2185 | return false; |
| 2186 | |
| 2187 | // Perform the quick checks that will tell us whether these |
| 2188 | // function types are obviously different. |
| 2189 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2190 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 2191 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 2192 | return false; |
| 2193 | |
| 2194 | bool HasObjCConversion = false; |
| 2195 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 2196 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 2197 | // Okay, the types match exactly. Nothing to do. |
| 2198 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 2199 | ToFunctionType->getResultType(), |
| 2200 | ConvertedType, IncompatibleObjC)) { |
| 2201 | // Okay, we have an Objective-C pointer conversion. |
| 2202 | HasObjCConversion = true; |
| 2203 | } else { |
| 2204 | // Function types are too different. Abort. |
| 2205 | return false; |
| 2206 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2207 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2208 | // Check argument types. |
| 2209 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2210 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2211 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2212 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2213 | if (Context.getCanonicalType(FromArgType) |
| 2214 | == Context.getCanonicalType(ToArgType)) { |
| 2215 | // Okay, the types match exactly. Nothing to do. |
| 2216 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 2217 | ConvertedType, IncompatibleObjC)) { |
| 2218 | // Okay, we have an Objective-C pointer conversion. |
| 2219 | HasObjCConversion = true; |
| 2220 | } else { |
| 2221 | // Argument types are too different. Abort. |
| 2222 | return false; |
| 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | if (HasObjCConversion) { |
| 2227 | // We had an Objective-C conversion. Allow this pointer |
| 2228 | // conversion, but complain about it. |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2229 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2230 | IncompatibleObjC = true; |
| 2231 | return true; |
| 2232 | } |
| 2233 | } |
| 2234 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2235 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2236 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2237 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2238 | /// \brief Determine whether this is an Objective-C writeback conversion, |
| 2239 | /// used for parameter passing when performing automatic reference counting. |
| 2240 | /// |
| 2241 | /// \param FromType The type we're converting form. |
| 2242 | /// |
| 2243 | /// \param ToType The type we're converting to. |
| 2244 | /// |
| 2245 | /// \param ConvertedType The type that will be produced after applying |
| 2246 | /// this conversion. |
| 2247 | bool Sema::isObjCWritebackConversion(QualType FromType, QualType ToType, |
| 2248 | QualType &ConvertedType) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2249 | if (!getLangOpts().ObjCAutoRefCount || |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2250 | Context.hasSameUnqualifiedType(FromType, ToType)) |
| 2251 | return false; |
| 2252 | |
| 2253 | // Parameter must be a pointer to __autoreleasing (with no other qualifiers). |
| 2254 | QualType ToPointee; |
| 2255 | if (const PointerType *ToPointer = ToType->getAs<PointerType>()) |
| 2256 | ToPointee = ToPointer->getPointeeType(); |
| 2257 | else |
| 2258 | return false; |
| 2259 | |
| 2260 | Qualifiers ToQuals = ToPointee.getQualifiers(); |
| 2261 | if (!ToPointee->isObjCLifetimeType() || |
| 2262 | ToQuals.getObjCLifetime() != Qualifiers::OCL_Autoreleasing || |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2263 | !ToQuals.withoutObjCLifetime().empty()) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2264 | return false; |
| 2265 | |
| 2266 | // Argument must be a pointer to __strong to __weak. |
| 2267 | QualType FromPointee; |
| 2268 | if (const PointerType *FromPointer = FromType->getAs<PointerType>()) |
| 2269 | FromPointee = FromPointer->getPointeeType(); |
| 2270 | else |
| 2271 | return false; |
| 2272 | |
| 2273 | Qualifiers FromQuals = FromPointee.getQualifiers(); |
| 2274 | if (!FromPointee->isObjCLifetimeType() || |
| 2275 | (FromQuals.getObjCLifetime() != Qualifiers::OCL_Strong && |
| 2276 | FromQuals.getObjCLifetime() != Qualifiers::OCL_Weak)) |
| 2277 | return false; |
| 2278 | |
| 2279 | // Make sure that we have compatible qualifiers. |
| 2280 | FromQuals.setObjCLifetime(Qualifiers::OCL_Autoreleasing); |
| 2281 | if (!ToQuals.compatiblyIncludes(FromQuals)) |
| 2282 | return false; |
| 2283 | |
| 2284 | // Remove qualifiers from the pointee type we're converting from; they |
| 2285 | // aren't used in the compatibility check belong, and we'll be adding back |
| 2286 | // qualifiers (with __autoreleasing) if the compatibility check succeeds. |
| 2287 | FromPointee = FromPointee.getUnqualifiedType(); |
| 2288 | |
| 2289 | // The unqualified form of the pointee types must be compatible. |
| 2290 | ToPointee = ToPointee.getUnqualifiedType(); |
| 2291 | bool IncompatibleObjC; |
| 2292 | if (Context.typesAreCompatible(FromPointee, ToPointee)) |
| 2293 | FromPointee = ToPointee; |
| 2294 | else if (!isObjCPointerConversion(FromPointee, ToPointee, FromPointee, |
| 2295 | IncompatibleObjC)) |
| 2296 | return false; |
| 2297 | |
| 2298 | /// \brief Construct the type we're converting to, which is a pointer to |
| 2299 | /// __autoreleasing pointee. |
| 2300 | FromPointee = Context.getQualifiedType(FromPointee, FromQuals); |
| 2301 | ConvertedType = Context.getPointerType(FromPointee); |
| 2302 | return true; |
| 2303 | } |
| 2304 | |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2305 | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
| 2306 | QualType& ConvertedType) { |
| 2307 | QualType ToPointeeType; |
| 2308 | if (const BlockPointerType *ToBlockPtr = |
| 2309 | ToType->getAs<BlockPointerType>()) |
| 2310 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 2311 | else |
| 2312 | return false; |
| 2313 | |
| 2314 | QualType FromPointeeType; |
| 2315 | if (const BlockPointerType *FromBlockPtr = |
| 2316 | FromType->getAs<BlockPointerType>()) |
| 2317 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2318 | else |
| 2319 | return false; |
| 2320 | // We have pointer to blocks, check whether the only |
| 2321 | // differences in the argument and result types are in Objective-C |
| 2322 | // pointer conversions. If so, we permit the conversion. |
| 2323 | |
| 2324 | const FunctionProtoType *FromFunctionType |
| 2325 | = FromPointeeType->getAs<FunctionProtoType>(); |
| 2326 | const FunctionProtoType *ToFunctionType |
| 2327 | = ToPointeeType->getAs<FunctionProtoType>(); |
| 2328 | |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2329 | if (!FromFunctionType || !ToFunctionType) |
| 2330 | return false; |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2331 | |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2332 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2333 | return true; |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2334 | |
| 2335 | // Perform the quick checks that will tell us whether these |
| 2336 | // function types are obviously different. |
| 2337 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2338 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
| 2339 | return false; |
| 2340 | |
| 2341 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
| 2342 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
| 2343 | if (FromEInfo != ToEInfo) |
| 2344 | return false; |
| 2345 | |
| 2346 | bool IncompatibleObjC = false; |
Fariborz Jahanian | 12834e1 | 2011-02-13 20:11:42 +0000 | [diff] [blame] | 2347 | if (Context.hasSameType(FromFunctionType->getResultType(), |
| 2348 | ToFunctionType->getResultType())) { |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2349 | // Okay, the types match exactly. Nothing to do. |
| 2350 | } else { |
| 2351 | QualType RHS = FromFunctionType->getResultType(); |
| 2352 | QualType LHS = ToFunctionType->getResultType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2353 | if ((!getLangOpts().CPlusPlus || !RHS->isRecordType()) && |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2354 | !RHS.hasQualifiers() && LHS.hasQualifiers()) |
| 2355 | LHS = LHS.getUnqualifiedType(); |
| 2356 | |
| 2357 | if (Context.hasSameType(RHS,LHS)) { |
| 2358 | // OK exact match. |
| 2359 | } else if (isObjCPointerConversion(RHS, LHS, |
| 2360 | ConvertedType, IncompatibleObjC)) { |
| 2361 | if (IncompatibleObjC) |
| 2362 | return false; |
| 2363 | // Okay, we have an Objective-C pointer conversion. |
| 2364 | } |
| 2365 | else |
| 2366 | return false; |
| 2367 | } |
| 2368 | |
| 2369 | // Check argument types. |
| 2370 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2371 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2372 | IncompatibleObjC = false; |
| 2373 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2374 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2375 | if (Context.hasSameType(FromArgType, ToArgType)) { |
| 2376 | // Okay, the types match exactly. Nothing to do. |
| 2377 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
| 2378 | ConvertedType, IncompatibleObjC)) { |
| 2379 | if (IncompatibleObjC) |
| 2380 | return false; |
| 2381 | // Okay, we have an Objective-C pointer conversion. |
| 2382 | } else |
| 2383 | // Argument types are too different. Abort. |
| 2384 | return false; |
| 2385 | } |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 2386 | if (LangOpts.ObjCAutoRefCount && |
| 2387 | !Context.FunctionTypesMatchOnNSConsumedAttrs(FromFunctionType, |
| 2388 | ToFunctionType)) |
| 2389 | return false; |
Fariborz Jahanian | 600ba20 | 2011-09-28 20:22:05 +0000 | [diff] [blame] | 2390 | |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2391 | ConvertedType = ToType; |
| 2392 | return true; |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2393 | } |
| 2394 | |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2395 | enum { |
| 2396 | ft_default, |
| 2397 | ft_different_class, |
| 2398 | ft_parameter_arity, |
| 2399 | ft_parameter_mismatch, |
| 2400 | ft_return_type, |
| 2401 | ft_qualifer_mismatch |
| 2402 | }; |
| 2403 | |
| 2404 | /// HandleFunctionTypeMismatch - Gives diagnostic information for differeing |
| 2405 | /// function types. Catches different number of parameter, mismatch in |
| 2406 | /// parameter types, and different return types. |
| 2407 | void Sema::HandleFunctionTypeMismatch(PartialDiagnostic &PDiag, |
| 2408 | QualType FromType, QualType ToType) { |
Richard Trieu | 96ed5b6 | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2409 | // If either type is not valid, include no extra info. |
| 2410 | if (FromType.isNull() || ToType.isNull()) { |
| 2411 | PDiag << ft_default; |
| 2412 | return; |
| 2413 | } |
| 2414 | |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2415 | // Get the function type from the pointers. |
| 2416 | if (FromType->isMemberPointerType() && ToType->isMemberPointerType()) { |
| 2417 | const MemberPointerType *FromMember = FromType->getAs<MemberPointerType>(), |
| 2418 | *ToMember = ToType->getAs<MemberPointerType>(); |
| 2419 | if (FromMember->getClass() != ToMember->getClass()) { |
| 2420 | PDiag << ft_different_class << QualType(ToMember->getClass(), 0) |
| 2421 | << QualType(FromMember->getClass(), 0); |
| 2422 | return; |
| 2423 | } |
| 2424 | FromType = FromMember->getPointeeType(); |
| 2425 | ToType = ToMember->getPointeeType(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
Richard Trieu | 96ed5b6 | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2428 | if (FromType->isPointerType()) |
| 2429 | FromType = FromType->getPointeeType(); |
| 2430 | if (ToType->isPointerType()) |
| 2431 | ToType = ToType->getPointeeType(); |
| 2432 | |
| 2433 | // Remove references. |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2434 | FromType = FromType.getNonReferenceType(); |
| 2435 | ToType = ToType.getNonReferenceType(); |
| 2436 | |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2437 | // Don't print extra info for non-specialized template functions. |
| 2438 | if (FromType->isInstantiationDependentType() && |
| 2439 | !FromType->getAs<TemplateSpecializationType>()) { |
| 2440 | PDiag << ft_default; |
| 2441 | return; |
| 2442 | } |
| 2443 | |
Richard Trieu | 96ed5b6 | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2444 | // No extra info for same types. |
| 2445 | if (Context.hasSameType(FromType, ToType)) { |
| 2446 | PDiag << ft_default; |
| 2447 | return; |
| 2448 | } |
| 2449 | |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2450 | const FunctionProtoType *FromFunction = FromType->getAs<FunctionProtoType>(), |
| 2451 | *ToFunction = ToType->getAs<FunctionProtoType>(); |
| 2452 | |
| 2453 | // Both types need to be function types. |
| 2454 | if (!FromFunction || !ToFunction) { |
| 2455 | PDiag << ft_default; |
| 2456 | return; |
| 2457 | } |
| 2458 | |
| 2459 | if (FromFunction->getNumArgs() != ToFunction->getNumArgs()) { |
| 2460 | PDiag << ft_parameter_arity << ToFunction->getNumArgs() |
| 2461 | << FromFunction->getNumArgs(); |
| 2462 | return; |
| 2463 | } |
| 2464 | |
| 2465 | // Handle different parameter types. |
| 2466 | unsigned ArgPos; |
| 2467 | if (!FunctionArgTypesAreEqual(FromFunction, ToFunction, &ArgPos)) { |
| 2468 | PDiag << ft_parameter_mismatch << ArgPos + 1 |
| 2469 | << ToFunction->getArgType(ArgPos) |
| 2470 | << FromFunction->getArgType(ArgPos); |
| 2471 | return; |
| 2472 | } |
| 2473 | |
| 2474 | // Handle different return type. |
| 2475 | if (!Context.hasSameType(FromFunction->getResultType(), |
| 2476 | ToFunction->getResultType())) { |
| 2477 | PDiag << ft_return_type << ToFunction->getResultType() |
| 2478 | << FromFunction->getResultType(); |
| 2479 | return; |
| 2480 | } |
| 2481 | |
| 2482 | unsigned FromQuals = FromFunction->getTypeQuals(), |
| 2483 | ToQuals = ToFunction->getTypeQuals(); |
| 2484 | if (FromQuals != ToQuals) { |
| 2485 | PDiag << ft_qualifer_mismatch << ToQuals << FromQuals; |
| 2486 | return; |
| 2487 | } |
| 2488 | |
| 2489 | // Unable to find a difference, so add no extra info. |
| 2490 | PDiag << ft_default; |
| 2491 | } |
| 2492 | |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2493 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
Douglas Gregor | 2039ca0 | 2011-12-15 17:15:07 +0000 | [diff] [blame] | 2494 | /// for equality of their argument types. Caller has already checked that |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2495 | /// they have same number of arguments. This routine assumes that Objective-C |
| 2496 | /// pointer types which only differ in their protocol qualifiers are equal. |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2497 | /// If the parameters are different, ArgPos will have the the parameter index |
| 2498 | /// of the first different parameter. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2499 | bool Sema::FunctionArgTypesAreEqual(const FunctionProtoType *OldType, |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2500 | const FunctionProtoType *NewType, |
| 2501 | unsigned *ArgPos) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2502 | if (!getLangOpts().ObjC1) { |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2503 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 2504 | N = NewType->arg_type_begin(), |
| 2505 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
| 2506 | if (!Context.hasSameType(*O, *N)) { |
| 2507 | if (ArgPos) *ArgPos = O - OldType->arg_type_begin(); |
| 2508 | return false; |
| 2509 | } |
| 2510 | } |
| 2511 | return true; |
| 2512 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2513 | |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2514 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 2515 | N = NewType->arg_type_begin(), |
| 2516 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
| 2517 | QualType ToType = (*O); |
| 2518 | QualType FromType = (*N); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2519 | if (!Context.hasSameType(ToType, FromType)) { |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2520 | if (const PointerType *PTTo = ToType->getAs<PointerType>()) { |
| 2521 | if (const PointerType *PTFr = FromType->getAs<PointerType>()) |
Chandler Carruth | 27c9fe9 | 2010-05-06 00:15:06 +0000 | [diff] [blame] | 2522 | if ((PTTo->getPointeeType()->isObjCQualifiedIdType() && |
| 2523 | PTFr->getPointeeType()->isObjCQualifiedIdType()) || |
| 2524 | (PTTo->getPointeeType()->isObjCQualifiedClassType() && |
| 2525 | PTFr->getPointeeType()->isObjCQualifiedClassType())) |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2526 | continue; |
| 2527 | } |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2528 | else if (const ObjCObjectPointerType *PTTo = |
| 2529 | ToType->getAs<ObjCObjectPointerType>()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2530 | if (const ObjCObjectPointerType *PTFr = |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2531 | FromType->getAs<ObjCObjectPointerType>()) |
Douglas Gregor | 2039ca0 | 2011-12-15 17:15:07 +0000 | [diff] [blame] | 2532 | if (Context.hasSameUnqualifiedType( |
| 2533 | PTTo->getObjectType()->getBaseType(), |
| 2534 | PTFr->getObjectType()->getBaseType())) |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2535 | continue; |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2536 | } |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2537 | if (ArgPos) *ArgPos = O - OldType->arg_type_begin(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2538 | return false; |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2539 | } |
| 2540 | } |
| 2541 | return true; |
| 2542 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2543 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2544 | /// CheckPointerConversion - Check the pointer conversion from the |
| 2545 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2546 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2547 | /// conversions for which IsPointerConversion has already returned |
| 2548 | /// true. It returns true and produces a diagnostic if there was an |
| 2549 | /// error, or returns false otherwise. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2550 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2551 | CastKind &Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2552 | CXXCastPath& BasePath, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2553 | bool IgnoreBaseAccess) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2554 | QualType FromType = From->getType(); |
Argyrios Kyrtzidis | d6ea6bd | 2010-09-28 14:54:11 +0000 | [diff] [blame] | 2555 | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2556 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2557 | Kind = CK_BitCast; |
| 2558 | |
Chandler Carruth | ffab873 | 2011-04-09 07:32:05 +0000 | [diff] [blame] | 2559 | if (!IsCStyleOrFunctionalCast && |
| 2560 | Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy) && |
| 2561 | From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 2562 | DiagRuntimeBehavior(From->getExprLoc(), From, |
Chandler Carruth | 66a7b04 | 2011-04-09 07:48:17 +0000 | [diff] [blame] | 2563 | PDiag(diag::warn_impcast_bool_to_null_pointer) |
| 2564 | << ToType << From->getSourceRange()); |
Douglas Gregor | 4038cf4 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 2565 | |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2566 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
| 2567 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2568 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 2569 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | 1e57a3f | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 2570 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2571 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 2572 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2573 | // We must have a derived-to-base conversion. Check an |
| 2574 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2575 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 2576 | From->getExprLoc(), |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2577 | From->getSourceRange(), &BasePath, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2578 | IgnoreBaseAccess)) |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2579 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2580 | |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2581 | // The conversion was successful. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2582 | Kind = CK_DerivedToBase; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2583 | } |
| 2584 | } |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2585 | } else if (const ObjCObjectPointerType *ToPtrType = |
| 2586 | ToType->getAs<ObjCObjectPointerType>()) { |
| 2587 | if (const ObjCObjectPointerType *FromPtrType = |
| 2588 | FromType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2589 | // Objective-C++ conversions are always okay. |
| 2590 | // FIXME: We should have a different class of conversions for the |
| 2591 | // Objective-C++ implicit conversions. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2592 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2593 | return false; |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2594 | } else if (FromType->isBlockPointerType()) { |
| 2595 | Kind = CK_BlockPointerToObjCPointerCast; |
| 2596 | } else { |
| 2597 | Kind = CK_CPointerToObjCPointerCast; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2598 | } |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2599 | } else if (ToType->isBlockPointerType()) { |
| 2600 | if (!FromType->isBlockPointerType()) |
| 2601 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2602 | } |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2603 | |
| 2604 | // We shouldn't fall into this case unless it's valid for other |
| 2605 | // reasons. |
| 2606 | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 2607 | Kind = CK_NullToPointer; |
| 2608 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2609 | return false; |
| 2610 | } |
| 2611 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2612 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 2613 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 2614 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 2615 | /// If so, returns true and places the converted type (that might differ from |
| 2616 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 2617 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2618 | QualType ToType, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2619 | bool InOverloadResolution, |
| 2620 | QualType &ConvertedType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2621 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2622 | if (!ToTypePtr) |
| 2623 | return false; |
| 2624 | |
| 2625 | // A null pointer constant can be converted to a member pointer (C++ 4.11p1) |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2626 | if (From->isNullPointerConstant(Context, |
| 2627 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 2628 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2629 | ConvertedType = ToType; |
| 2630 | return true; |
| 2631 | } |
| 2632 | |
| 2633 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2634 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2635 | if (!FromTypePtr) |
| 2636 | return false; |
| 2637 | |
| 2638 | // A pointer to member of B can be converted to a pointer to member of D, |
| 2639 | // where D is derived from B (C++ 4.11p2). |
| 2640 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 2641 | QualType ToClass(ToTypePtr->getClass(), 0); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2642 | |
Douglas Gregor | 7f6ae69 | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2643 | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2644 | !RequireCompleteType(From->getLocStart(), ToClass, 0) && |
Douglas Gregor | 7f6ae69 | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2645 | IsDerivedFrom(ToClass, FromClass)) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2646 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 2647 | ToClass.getTypePtr()); |
| 2648 | return true; |
| 2649 | } |
| 2650 | |
| 2651 | return false; |
| 2652 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2653 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2654 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 2655 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2656 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2657 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 2658 | /// true and produces a diagnostic if there was an error, or returns false |
| 2659 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2660 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2661 | CastKind &Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2662 | CXXCastPath &BasePath, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2663 | bool IgnoreBaseAccess) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2664 | QualType FromType = From->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2665 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2666 | if (!FromPtrType) { |
| 2667 | // This must be a null pointer to member pointer conversion |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2668 | assert(From->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2669 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2670 | "Expr must be null pointer constant!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2671 | Kind = CK_NullToMemberPointer; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2672 | return false; |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2673 | } |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2674 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2675 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2676 | assert(ToPtrType && "No member pointer cast has a target type " |
| 2677 | "that is not a member pointer."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2678 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2679 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 2680 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2681 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2682 | // FIXME: What about dependent types? |
| 2683 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 2684 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2685 | |
Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2686 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 2687 | /*DetectVirtual=*/true); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2688 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 2689 | assert(DerivationOkay && |
| 2690 | "Should not have been called if derivation isn't OK."); |
| 2691 | (void)DerivationOkay; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2692 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2693 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 2694 | getUnqualifiedType())) { |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2695 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 2696 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 2697 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 2698 | return true; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2699 | } |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2700 | |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2701 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2702 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 2703 | << FromClass << ToClass << QualType(VBase, 0) |
| 2704 | << From->getSourceRange(); |
| 2705 | return true; |
| 2706 | } |
| 2707 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2708 | if (!IgnoreBaseAccess) |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2709 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 2710 | Paths.front(), |
| 2711 | diag::err_downcast_from_inaccessible_base); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2712 | |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2713 | // Must be a base to derived member conversion. |
Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2714 | BuildBasePathArray(Paths, BasePath); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2715 | Kind = CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2716 | return false; |
| 2717 | } |
| 2718 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2719 | /// IsQualificationConversion - Determines whether the conversion from |
| 2720 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 2721 | /// (C++ 4.4). |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2722 | /// |
| 2723 | /// \param ObjCLifetimeConversion Output parameter that will be set to indicate |
| 2724 | /// when the qualification conversion involves a change in the Objective-C |
| 2725 | /// object lifetime. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2726 | bool |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2727 | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2728 | bool CStyle, bool &ObjCLifetimeConversion) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2729 | FromType = Context.getCanonicalType(FromType); |
| 2730 | ToType = Context.getCanonicalType(ToType); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2731 | ObjCLifetimeConversion = false; |
| 2732 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2733 | // If FromType and ToType are the same type, this is not a |
| 2734 | // qualification conversion. |
Sebastian Redl | cbdffb1 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 2735 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2736 | return false; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2737 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2738 | // (C++ 4.4p4): |
| 2739 | // A conversion can add cv-qualifiers at levels other than the first |
| 2740 | // in multi-level pointers, subject to the following rules: [...] |
| 2741 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2742 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2743 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2744 | // Within each iteration of the loop, we check the qualifiers to |
| 2745 | // determine if this still looks like a qualification |
| 2746 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2747 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2748 | // until there are no more pointers or pointers-to-members left to |
| 2749 | // unwrap. |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2750 | UnwrappedAnyPointer = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2751 | |
Douglas Gregor | 90609aa | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2752 | Qualifiers FromQuals = FromType.getQualifiers(); |
| 2753 | Qualifiers ToQuals = ToType.getQualifiers(); |
| 2754 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2755 | // Objective-C ARC: |
| 2756 | // Check Objective-C lifetime conversions. |
| 2757 | if (FromQuals.getObjCLifetime() != ToQuals.getObjCLifetime() && |
| 2758 | UnwrappedAnyPointer) { |
| 2759 | if (ToQuals.compatiblyIncludesObjCLifetime(FromQuals)) { |
| 2760 | ObjCLifetimeConversion = true; |
| 2761 | FromQuals.removeObjCLifetime(); |
| 2762 | ToQuals.removeObjCLifetime(); |
| 2763 | } else { |
| 2764 | // Qualification conversions cannot cast between different |
| 2765 | // Objective-C lifetime qualifiers. |
| 2766 | return false; |
| 2767 | } |
| 2768 | } |
| 2769 | |
Douglas Gregor | f30053d | 2011-05-08 06:09:53 +0000 | [diff] [blame] | 2770 | // Allow addition/removal of GC attributes but not changing GC attributes. |
| 2771 | if (FromQuals.getObjCGCAttr() != ToQuals.getObjCGCAttr() && |
| 2772 | (!FromQuals.hasObjCGCAttr() || !ToQuals.hasObjCGCAttr())) { |
| 2773 | FromQuals.removeObjCGCAttr(); |
| 2774 | ToQuals.removeObjCGCAttr(); |
| 2775 | } |
| 2776 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2777 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 2778 | // 2,j, and similarly for volatile. |
Douglas Gregor | 90609aa | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2779 | if (!CStyle && !ToQuals.compatiblyIncludes(FromQuals)) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2780 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2781 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2782 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 2783 | // every cv for 0 < k < j. |
Douglas Gregor | 90609aa | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2784 | if (!CStyle && FromQuals.getCVRQualifiers() != ToQuals.getCVRQualifiers() |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2785 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2786 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2787 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2788 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 2789 | // include const. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2790 | PreviousToQualsIncludeConst |
Douglas Gregor | 90609aa | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2791 | = PreviousToQualsIncludeConst && ToQuals.hasConst(); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2792 | } |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2793 | |
| 2794 | // We are left with FromType and ToType being the pointee types |
| 2795 | // after unwrapping the original FromType and ToType the same number |
| 2796 | // of types. If we unwrapped any pointers, and if FromType and |
| 2797 | // ToType have the same unqualified type (since we checked |
| 2798 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2799 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
Douglas Gregor | c79862f | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2802 | /// \brief - Determine whether this is a conversion from a scalar type to an |
| 2803 | /// atomic type. |
| 2804 | /// |
| 2805 | /// If successful, updates \c SCS's second and third steps in the conversion |
| 2806 | /// sequence to finish the conversion. |
Douglas Gregor | f9e36cc | 2012-04-12 20:48:09 +0000 | [diff] [blame] | 2807 | static bool tryAtomicConversion(Sema &S, Expr *From, QualType ToType, |
| 2808 | bool InOverloadResolution, |
| 2809 | StandardConversionSequence &SCS, |
| 2810 | bool CStyle) { |
Douglas Gregor | c79862f | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 2811 | const AtomicType *ToAtomic = ToType->getAs<AtomicType>(); |
| 2812 | if (!ToAtomic) |
| 2813 | return false; |
| 2814 | |
| 2815 | StandardConversionSequence InnerSCS; |
| 2816 | if (!IsStandardConversion(S, From, ToAtomic->getValueType(), |
| 2817 | InOverloadResolution, InnerSCS, |
| 2818 | CStyle, /*AllowObjCWritebackConversion=*/false)) |
| 2819 | return false; |
| 2820 | |
| 2821 | SCS.Second = InnerSCS.Second; |
| 2822 | SCS.setToType(1, InnerSCS.getToType(1)); |
| 2823 | SCS.Third = InnerSCS.Third; |
| 2824 | SCS.QualificationIncludesObjCLifetime |
| 2825 | = InnerSCS.QualificationIncludesObjCLifetime; |
| 2826 | SCS.setToType(2, InnerSCS.getToType(2)); |
| 2827 | return true; |
| 2828 | } |
| 2829 | |
Sebastian Redl | e541716 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2830 | static bool isFirstArgumentCompatibleWithType(ASTContext &Context, |
| 2831 | CXXConstructorDecl *Constructor, |
| 2832 | QualType Type) { |
| 2833 | const FunctionProtoType *CtorType = |
| 2834 | Constructor->getType()->getAs<FunctionProtoType>(); |
| 2835 | if (CtorType->getNumArgs() > 0) { |
| 2836 | QualType FirstArg = CtorType->getArgType(0); |
| 2837 | if (Context.hasSameUnqualifiedType(Type, FirstArg.getNonReferenceType())) |
| 2838 | return true; |
| 2839 | } |
| 2840 | return false; |
| 2841 | } |
| 2842 | |
Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2843 | static OverloadingResult |
| 2844 | IsInitializerListConstructorConversion(Sema &S, Expr *From, QualType ToType, |
| 2845 | CXXRecordDecl *To, |
| 2846 | UserDefinedConversionSequence &User, |
| 2847 | OverloadCandidateSet &CandidateSet, |
| 2848 | bool AllowExplicit) { |
| 2849 | DeclContext::lookup_iterator Con, ConEnd; |
| 2850 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(To); |
| 2851 | Con != ConEnd; ++Con) { |
| 2852 | NamedDecl *D = *Con; |
| 2853 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2854 | |
| 2855 | // Find the constructor (which may be a template). |
| 2856 | CXXConstructorDecl *Constructor = 0; |
| 2857 | FunctionTemplateDecl *ConstructorTmpl |
| 2858 | = dyn_cast<FunctionTemplateDecl>(D); |
| 2859 | if (ConstructorTmpl) |
| 2860 | Constructor |
| 2861 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 2862 | else |
| 2863 | Constructor = cast<CXXConstructorDecl>(D); |
| 2864 | |
| 2865 | bool Usable = !Constructor->isInvalidDecl() && |
| 2866 | S.isInitListConstructor(Constructor) && |
| 2867 | (AllowExplicit || !Constructor->isExplicit()); |
| 2868 | if (Usable) { |
Sebastian Redl | e541716 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2869 | // If the first argument is (a reference to) the target type, |
| 2870 | // suppress conversions. |
| 2871 | bool SuppressUserConversions = |
| 2872 | isFirstArgumentCompatibleWithType(S.Context, Constructor, ToType); |
Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2873 | if (ConstructorTmpl) |
| 2874 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2875 | /*ExplicitArgs*/ 0, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2876 | From, CandidateSet, |
Sebastian Redl | e541716 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2877 | SuppressUserConversions); |
Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2878 | else |
| 2879 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2880 | From, CandidateSet, |
Sebastian Redl | e541716 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 2881 | SuppressUserConversions); |
Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2882 | } |
| 2883 | } |
| 2884 | |
| 2885 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 2886 | |
| 2887 | OverloadCandidateSet::iterator Best; |
| 2888 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
| 2889 | case OR_Success: { |
| 2890 | // Record the standard conversion we used and the conversion function. |
| 2891 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
| 2892 | S.MarkFunctionReferenced(From->getLocStart(), Constructor); |
| 2893 | |
| 2894 | QualType ThisType = Constructor->getThisType(S.Context); |
| 2895 | // Initializer lists don't have conversions as such. |
| 2896 | User.Before.setAsIdentityConversion(); |
| 2897 | User.HadMultipleCandidates = HadMultipleCandidates; |
| 2898 | User.ConversionFunction = Constructor; |
| 2899 | User.FoundConversionFunction = Best->FoundDecl; |
| 2900 | User.After.setAsIdentityConversion(); |
| 2901 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 2902 | User.After.setAllToTypes(ToType); |
| 2903 | return OR_Success; |
| 2904 | } |
| 2905 | |
| 2906 | case OR_No_Viable_Function: |
| 2907 | return OR_No_Viable_Function; |
| 2908 | case OR_Deleted: |
| 2909 | return OR_Deleted; |
| 2910 | case OR_Ambiguous: |
| 2911 | return OR_Ambiguous; |
| 2912 | } |
| 2913 | |
| 2914 | llvm_unreachable("Invalid OverloadResult!"); |
| 2915 | } |
| 2916 | |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2917 | /// Determines whether there is a user-defined conversion sequence |
| 2918 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 2919 | /// ToType. If such a conversion exists, User will contain the |
| 2920 | /// user-defined conversion sequence that performs such a conversion |
| 2921 | /// and this routine will return true. Otherwise, this routine returns |
| 2922 | /// false and User is unspecified. |
| 2923 | /// |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2924 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 2925 | /// "explicit" conversion functions as well as non-explicit conversion |
| 2926 | /// functions (C++0x [class.conv.fct]p2). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2927 | static OverloadingResult |
| 2928 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2929 | UserDefinedConversionSequence &User, |
| 2930 | OverloadCandidateSet &CandidateSet, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2931 | bool AllowExplicit) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2932 | // Whether we will only visit constructors. |
| 2933 | bool ConstructorsOnly = false; |
| 2934 | |
| 2935 | // If the type we are conversion to is a class type, enumerate its |
| 2936 | // constructors. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2937 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2938 | // C++ [over.match.ctor]p1: |
| 2939 | // When objects of class type are direct-initialized (8.5), or |
| 2940 | // copy-initialized from an expression of the same or a |
| 2941 | // derived class type (8.5), overload resolution selects the |
| 2942 | // constructor. [...] For copy-initialization, the candidate |
| 2943 | // functions are all the converting constructors (12.3.1) of |
| 2944 | // that class. The argument list is the expression-list within |
| 2945 | // the parentheses of the initializer. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2946 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2947 | (From->getType()->getAs<RecordType>() && |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2948 | S.IsDerivedFrom(From->getType(), ToType))) |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2949 | ConstructorsOnly = true; |
| 2950 | |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2951 | S.RequireCompleteType(From->getLocStart(), ToType, 0); |
Argyrios Kyrtzidis | 7a6f2a3 | 2011-04-22 17:45:37 +0000 | [diff] [blame] | 2952 | // RequireCompleteType may have returned true due to some invalid decl |
| 2953 | // during template instantiation, but ToType may be complete enough now |
| 2954 | // to try to recover. |
| 2955 | if (ToType->isIncompleteType()) { |
Douglas Gregor | 3ec1bf2 | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 2956 | // We're not going to find any constructors. |
| 2957 | } else if (CXXRecordDecl *ToRecordDecl |
| 2958 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2959 | |
| 2960 | Expr **Args = &From; |
| 2961 | unsigned NumArgs = 1; |
| 2962 | bool ListInitializing = false; |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2963 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(From)) { |
Sebastian Redl | 82ace98 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 2964 | // But first, see if there is an init-list-contructor that will work. |
| 2965 | OverloadingResult Result = IsInitializerListConstructorConversion( |
| 2966 | S, From, ToType, ToRecordDecl, User, CandidateSet, AllowExplicit); |
| 2967 | if (Result != OR_No_Viable_Function) |
| 2968 | return Result; |
| 2969 | // Never mind. |
| 2970 | CandidateSet.clear(); |
| 2971 | |
| 2972 | // If we're list-initializing, we pass the individual elements as |
| 2973 | // arguments, not the entire list. |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2974 | Args = InitList->getInits(); |
| 2975 | NumArgs = InitList->getNumInits(); |
| 2976 | ListInitializing = true; |
| 2977 | } |
| 2978 | |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2979 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2980 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(ToRecordDecl); |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2981 | Con != ConEnd; ++Con) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2982 | NamedDecl *D = *Con; |
| 2983 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2984 | |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2985 | // Find the constructor (which may be a template). |
| 2986 | CXXConstructorDecl *Constructor = 0; |
| 2987 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2988 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2989 | if (ConstructorTmpl) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2990 | Constructor |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2991 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 2992 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2993 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2994 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2995 | bool Usable = !Constructor->isInvalidDecl(); |
| 2996 | if (ListInitializing) |
| 2997 | Usable = Usable && (AllowExplicit || !Constructor->isExplicit()); |
| 2998 | else |
| 2999 | Usable = Usable &&Constructor->isConvertingConstructor(AllowExplicit); |
| 3000 | if (Usable) { |
Sebastian Redl | d9170b0 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3001 | bool SuppressUserConversions = !ConstructorsOnly; |
| 3002 | if (SuppressUserConversions && ListInitializing) { |
| 3003 | SuppressUserConversions = false; |
| 3004 | if (NumArgs == 1) { |
| 3005 | // If the first argument is (a reference to) the target type, |
| 3006 | // suppress conversions. |
Sebastian Redl | e541716 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 3007 | SuppressUserConversions = isFirstArgumentCompatibleWithType( |
| 3008 | S.Context, Constructor, ToType); |
Sebastian Redl | d9170b0 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3009 | } |
| 3010 | } |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3011 | if (ConstructorTmpl) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3012 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3013 | /*ExplicitArgs*/ 0, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3014 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | d9170b0 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3015 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3016 | else |
Fariborz Jahanian | b3c44f9 | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 3017 | // Allow one user-defined conversion when user specifies a |
| 3018 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3019 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3020 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | d9170b0 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 3021 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 3022 | } |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 3023 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3024 | } |
| 3025 | } |
| 3026 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3027 | // Enumerate conversion functions, if we're allowed to. |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3028 | if (ConstructorsOnly || isa<InitListExpr>(From)) { |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3029 | } else if (S.RequireCompleteType(From->getLocStart(), From->getType(), 0)) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3030 | // No conversion functions from incomplete types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3031 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3032 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3033 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3034 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 3035 | // Add all of the conversion functions as candidates. |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3036 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | f4061e3 | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 3037 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3038 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3039 | E = Conversions->end(); I != E; ++I) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3040 | DeclAccessPair FoundDecl = I.getPair(); |
| 3041 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3042 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3043 | if (isa<UsingShadowDecl>(D)) |
| 3044 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3045 | |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3046 | CXXConversionDecl *Conv; |
| 3047 | FunctionTemplateDecl *ConvTemplate; |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3048 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 3049 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3050 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3051 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3052 | |
| 3053 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3054 | if (ConvTemplate) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3055 | S.AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
| 3056 | ActingContext, From, ToType, |
| 3057 | CandidateSet); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3058 | else |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3059 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, |
| 3060 | From, ToType, CandidateSet); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 3061 | } |
| 3062 | } |
| 3063 | } |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3064 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3065 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3066 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3067 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3068 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3069 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3070 | case OR_Success: |
| 3071 | // Record the standard conversion we used and the conversion function. |
| 3072 | if (CXXConstructorDecl *Constructor |
| 3073 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3074 | S.MarkFunctionReferenced(From->getLocStart(), Constructor); |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3075 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3076 | // C++ [over.ics.user]p1: |
| 3077 | // If the user-defined conversion is specified by a |
| 3078 | // constructor (12.3.1), the initial standard conversion |
| 3079 | // sequence converts the source type to the type required by |
| 3080 | // the argument of the constructor. |
| 3081 | // |
| 3082 | QualType ThisType = Constructor->getThisType(S.Context); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3083 | if (isa<InitListExpr>(From)) { |
| 3084 | // Initializer lists don't have conversions as such. |
| 3085 | User.Before.setAsIdentityConversion(); |
| 3086 | } else { |
| 3087 | if (Best->Conversions[0].isEllipsis()) |
| 3088 | User.EllipsisConversion = true; |
| 3089 | else { |
| 3090 | User.Before = Best->Conversions[0].Standard; |
| 3091 | User.EllipsisConversion = false; |
| 3092 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3093 | } |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3094 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3095 | User.ConversionFunction = Constructor; |
John McCall | 3090903 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3096 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3097 | User.After.setAsIdentityConversion(); |
| 3098 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 3099 | User.After.setAllToTypes(ToType); |
| 3100 | return OR_Success; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3101 | } |
| 3102 | if (CXXConversionDecl *Conversion |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3103 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3104 | S.MarkFunctionReferenced(From->getLocStart(), Conversion); |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3105 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3106 | // C++ [over.ics.user]p1: |
| 3107 | // |
| 3108 | // [...] If the user-defined conversion is specified by a |
| 3109 | // conversion function (12.3.2), the initial standard |
| 3110 | // conversion sequence converts the source type to the |
| 3111 | // implicit object parameter of the conversion function. |
| 3112 | User.Before = Best->Conversions[0].Standard; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3113 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3114 | User.ConversionFunction = Conversion; |
John McCall | 3090903 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3115 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3116 | User.EllipsisConversion = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3117 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3118 | // C++ [over.ics.user]p2: |
| 3119 | // The second standard conversion sequence converts the |
| 3120 | // result of the user-defined conversion to the target type |
| 3121 | // for the sequence. Since an implicit conversion sequence |
| 3122 | // is an initialization, the special rules for |
| 3123 | // initialization by user-defined conversion apply when |
| 3124 | // selecting the best user-defined conversion for a |
| 3125 | // user-defined conversion sequence (see 13.3.3 and |
| 3126 | // 13.3.3.1). |
| 3127 | User.After = Best->FinalConversion; |
| 3128 | return OR_Success; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3129 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3130 | llvm_unreachable("Not a constructor or conversion function?"); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3131 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3132 | case OR_No_Viable_Function: |
| 3133 | return OR_No_Viable_Function; |
| 3134 | case OR_Deleted: |
| 3135 | // No conversion here! We're done. |
| 3136 | return OR_Deleted; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3137 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3138 | case OR_Ambiguous: |
| 3139 | return OR_Ambiguous; |
| 3140 | } |
| 3141 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3142 | llvm_unreachable("Invalid OverloadResult!"); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3143 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3144 | |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3145 | bool |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3146 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3147 | ImplicitConversionSequence ICS; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3148 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3149 | OverloadingResult OvResult = |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3150 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3151 | CandidateSet, false); |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3152 | if (OvResult == OR_Ambiguous) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3153 | Diag(From->getLocStart(), |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3154 | diag::err_typecheck_ambiguous_condition) |
| 3155 | << From->getType() << ToType << From->getSourceRange(); |
| 3156 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3157 | Diag(From->getLocStart(), |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3158 | diag::err_typecheck_nonviable_condition) |
| 3159 | << From->getType() << ToType << From->getSourceRange(); |
| 3160 | else |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3161 | return false; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3162 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, From); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3163 | return true; |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3164 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 3165 | |
Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3166 | /// \brief Compare the user-defined conversion functions or constructors |
| 3167 | /// of two user-defined conversion sequences to determine whether any ordering |
| 3168 | /// is possible. |
| 3169 | static ImplicitConversionSequence::CompareKind |
| 3170 | compareConversionFunctions(Sema &S, |
| 3171 | FunctionDecl *Function1, |
| 3172 | FunctionDecl *Function2) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3173 | if (!S.getLangOpts().ObjC1 || !S.getLangOpts().CPlusPlus0x) |
Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3174 | return ImplicitConversionSequence::Indistinguishable; |
| 3175 | |
| 3176 | // Objective-C++: |
| 3177 | // If both conversion functions are implicitly-declared conversions from |
| 3178 | // a lambda closure type to a function pointer and a block pointer, |
| 3179 | // respectively, always prefer the conversion to a function pointer, |
| 3180 | // because the function pointer is more lightweight and is more likely |
| 3181 | // to keep code working. |
| 3182 | CXXConversionDecl *Conv1 = dyn_cast<CXXConversionDecl>(Function1); |
| 3183 | if (!Conv1) |
| 3184 | return ImplicitConversionSequence::Indistinguishable; |
| 3185 | |
| 3186 | CXXConversionDecl *Conv2 = dyn_cast<CXXConversionDecl>(Function2); |
| 3187 | if (!Conv2) |
| 3188 | return ImplicitConversionSequence::Indistinguishable; |
| 3189 | |
| 3190 | if (Conv1->getParent()->isLambda() && Conv2->getParent()->isLambda()) { |
| 3191 | bool Block1 = Conv1->getConversionType()->isBlockPointerType(); |
| 3192 | bool Block2 = Conv2->getConversionType()->isBlockPointerType(); |
| 3193 | if (Block1 != Block2) |
| 3194 | return Block1? ImplicitConversionSequence::Worse |
| 3195 | : ImplicitConversionSequence::Better; |
| 3196 | } |
| 3197 | |
| 3198 | return ImplicitConversionSequence::Indistinguishable; |
| 3199 | } |
| 3200 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3201 | /// CompareImplicitConversionSequences - Compare two implicit |
| 3202 | /// conversion sequences to determine whether one is better than the |
| 3203 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3204 | static ImplicitConversionSequence::CompareKind |
| 3205 | CompareImplicitConversionSequences(Sema &S, |
| 3206 | const ImplicitConversionSequence& ICS1, |
| 3207 | const ImplicitConversionSequence& ICS2) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3208 | { |
| 3209 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 3210 | // conversion sequences (as defined in 13.3.3.1) |
| 3211 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 3212 | // conversion sequence than a user-defined conversion sequence or |
| 3213 | // an ellipsis conversion sequence, and |
| 3214 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 3215 | // conversion sequence than an ellipsis conversion sequence |
| 3216 | // (13.3.3.1.3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3217 | // |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3218 | // C++0x [over.best.ics]p10: |
| 3219 | // For the purpose of ranking implicit conversion sequences as |
| 3220 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 3221 | // treated as a user-defined sequence that is indistinguishable |
| 3222 | // from any other user-defined conversion sequence. |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3223 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 3224 | return ImplicitConversionSequence::Better; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3225 | if (ICS2.getKindRank() < ICS1.getKindRank()) |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3226 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3227 | |
Benjamin Kramer | 98ff7f8 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 3228 | // The following checks require both conversion sequences to be of |
| 3229 | // the same kind. |
| 3230 | if (ICS1.getKind() != ICS2.getKind()) |
| 3231 | return ImplicitConversionSequence::Indistinguishable; |
| 3232 | |
Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3233 | ImplicitConversionSequence::CompareKind Result = |
| 3234 | ImplicitConversionSequence::Indistinguishable; |
| 3235 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3236 | // Two implicit conversion sequences of the same form are |
| 3237 | // indistinguishable conversion sequences unless one of the |
| 3238 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3239 | if (ICS1.isStandard()) |
Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3240 | Result = CompareStandardConversionSequences(S, |
| 3241 | ICS1.Standard, ICS2.Standard); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3242 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3243 | // User-defined conversion sequence U1 is a better conversion |
| 3244 | // sequence than another user-defined conversion sequence U2 if |
| 3245 | // they contain the same user-defined conversion function or |
| 3246 | // constructor and if the second standard conversion sequence of |
| 3247 | // U1 is better than the second standard conversion sequence of |
| 3248 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3249 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3250 | ICS2.UserDefined.ConversionFunction) |
Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3251 | Result = CompareStandardConversionSequences(S, |
| 3252 | ICS1.UserDefined.After, |
| 3253 | ICS2.UserDefined.After); |
Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 3254 | else |
| 3255 | Result = compareConversionFunctions(S, |
| 3256 | ICS1.UserDefined.ConversionFunction, |
| 3257 | ICS2.UserDefined.ConversionFunction); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3258 | } |
| 3259 | |
Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3260 | // List-initialization sequence L1 is a better conversion sequence than |
| 3261 | // list-initialization sequence L2 if L1 converts to std::initializer_list<X> |
| 3262 | // for some X and L2 does not. |
| 3263 | if (Result == ImplicitConversionSequence::Indistinguishable && |
Sebastian Redl | aa6feaa | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 3264 | !ICS1.isBad() && |
Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3265 | ICS1.isListInitializationSequence() && |
| 3266 | ICS2.isListInitializationSequence()) { |
Sebastian Redl | aa6feaa | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 3267 | if (ICS1.isStdInitializerListElement() && |
| 3268 | !ICS2.isStdInitializerListElement()) |
| 3269 | return ImplicitConversionSequence::Better; |
| 3270 | if (!ICS1.isStdInitializerListElement() && |
| 3271 | ICS2.isStdInitializerListElement()) |
| 3272 | return ImplicitConversionSequence::Worse; |
Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | return Result; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3276 | } |
| 3277 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3278 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 3279 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 3280 | Qualifiers Quals; |
| 3281 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3282 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3283 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3284 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3285 | return Context.hasSameUnqualifiedType(T1, T2); |
| 3286 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3287 | |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3288 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 3289 | // determine if one is a proper subset of the other. |
| 3290 | static ImplicitConversionSequence::CompareKind |
| 3291 | compareStandardConversionSubsets(ASTContext &Context, |
| 3292 | const StandardConversionSequence& SCS1, |
| 3293 | const StandardConversionSequence& SCS2) { |
| 3294 | ImplicitConversionSequence::CompareKind Result |
| 3295 | = ImplicitConversionSequence::Indistinguishable; |
| 3296 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3297 | // the identity conversion sequence is considered to be a subsequence of |
Douglas Gregor | e87561a | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 3298 | // any non-identity conversion sequence |
Douglas Gregor | 377c109 | 2011-06-05 06:15:20 +0000 | [diff] [blame] | 3299 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 3300 | return ImplicitConversionSequence::Better; |
| 3301 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 3302 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3303 | |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3304 | if (SCS1.Second != SCS2.Second) { |
| 3305 | if (SCS1.Second == ICK_Identity) |
| 3306 | Result = ImplicitConversionSequence::Better; |
| 3307 | else if (SCS2.Second == ICK_Identity) |
| 3308 | Result = ImplicitConversionSequence::Worse; |
| 3309 | else |
| 3310 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3311 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3312 | return ImplicitConversionSequence::Indistinguishable; |
| 3313 | |
| 3314 | if (SCS1.Third == SCS2.Third) { |
| 3315 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 3316 | : ImplicitConversionSequence::Indistinguishable; |
| 3317 | } |
| 3318 | |
| 3319 | if (SCS1.Third == ICK_Identity) |
| 3320 | return Result == ImplicitConversionSequence::Worse |
| 3321 | ? ImplicitConversionSequence::Indistinguishable |
| 3322 | : ImplicitConversionSequence::Better; |
| 3323 | |
| 3324 | if (SCS2.Third == ICK_Identity) |
| 3325 | return Result == ImplicitConversionSequence::Better |
| 3326 | ? ImplicitConversionSequence::Indistinguishable |
| 3327 | : ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3328 | |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3329 | return ImplicitConversionSequence::Indistinguishable; |
| 3330 | } |
| 3331 | |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3332 | /// \brief Determine whether one of the given reference bindings is better |
| 3333 | /// than the other based on what kind of bindings they are. |
| 3334 | static bool isBetterReferenceBindingKind(const StandardConversionSequence &SCS1, |
| 3335 | const StandardConversionSequence &SCS2) { |
| 3336 | // C++0x [over.ics.rank]p3b4: |
| 3337 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 3338 | // implicit object parameter of a non-static member function declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3339 | // without a ref-qualifier, and *either* S1 binds an rvalue reference |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3340 | // to an rvalue and S2 binds an lvalue reference *or S1 binds an |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3341 | // lvalue reference to a function lvalue and S2 binds an rvalue |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3342 | // reference*. |
| 3343 | // |
| 3344 | // FIXME: Rvalue references. We're going rogue with the above edits, |
| 3345 | // because the semantics in the current C++0x working paper (N3225 at the |
| 3346 | // time of this writing) break the standard definition of std::forward |
| 3347 | // and std::reference_wrapper when dealing with references to functions. |
| 3348 | // Proposed wording changes submitted to CWG for consideration. |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3349 | if (SCS1.BindsImplicitObjectArgumentWithoutRefQualifier || |
| 3350 | SCS2.BindsImplicitObjectArgumentWithoutRefQualifier) |
| 3351 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3352 | |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3353 | return (!SCS1.IsLvalueReference && SCS1.BindsToRvalue && |
| 3354 | SCS2.IsLvalueReference) || |
| 3355 | (SCS1.IsLvalueReference && SCS1.BindsToFunctionLvalue && |
| 3356 | !SCS2.IsLvalueReference); |
| 3357 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3358 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3359 | /// CompareStandardConversionSequences - Compare two standard |
| 3360 | /// conversion sequences to determine whether one is better than the |
| 3361 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3362 | static ImplicitConversionSequence::CompareKind |
| 3363 | CompareStandardConversionSequences(Sema &S, |
| 3364 | const StandardConversionSequence& SCS1, |
| 3365 | const StandardConversionSequence& SCS2) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3366 | { |
| 3367 | // Standard conversion sequence S1 is a better conversion sequence |
| 3368 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 3369 | |
| 3370 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 3371 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 3372 | // excluding any Lvalue Transformation; the identity conversion |
| 3373 | // sequence is considered to be a subsequence of any |
| 3374 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3375 | if (ImplicitConversionSequence::CompareKind CK |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3376 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3377 | return CK; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3378 | |
| 3379 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 3380 | // defined below), or, if not that, |
| 3381 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 3382 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 3383 | if (Rank1 < Rank2) |
| 3384 | return ImplicitConversionSequence::Better; |
| 3385 | else if (Rank2 < Rank1) |
| 3386 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3387 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3388 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 3389 | // are indistinguishable unless one of the following rules |
| 3390 | // applies: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3391 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3392 | // A conversion that is not a conversion of a pointer, or |
| 3393 | // pointer to member, to bool is better than another conversion |
| 3394 | // that is such a conversion. |
| 3395 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 3396 | return SCS2.isPointerConversionToBool() |
| 3397 | ? ImplicitConversionSequence::Better |
| 3398 | : ImplicitConversionSequence::Worse; |
| 3399 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3400 | // C++ [over.ics.rank]p4b2: |
| 3401 | // |
| 3402 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3403 | // conversion of B* to A* is better than conversion of B* to |
| 3404 | // void*, and conversion of A* to void* is better than conversion |
| 3405 | // of B* to void*. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3406 | bool SCS1ConvertsToVoid |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3407 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3408 | bool SCS2ConvertsToVoid |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3409 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3410 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 3411 | // Exactly one of the conversion sequences is a conversion to |
| 3412 | // a void pointer; it's the worse conversion. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3413 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 3414 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3415 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 3416 | // Neither conversion sequence converts to a void pointer; compare |
| 3417 | // their derived-to-base conversions. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3418 | if (ImplicitConversionSequence::CompareKind DerivedCK |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3419 | = CompareDerivedToBaseConversions(S, SCS1, SCS2)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3420 | return DerivedCK; |
Douglas Gregor | 30ee16f | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3421 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid && |
| 3422 | !S.Context.hasSameType(SCS1.getFromType(), SCS2.getFromType())) { |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3423 | // Both conversion sequences are conversions to void |
| 3424 | // pointers. Compare the source types to determine if there's an |
| 3425 | // inheritance relationship in their sources. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3426 | QualType FromType1 = SCS1.getFromType(); |
| 3427 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3428 | |
| 3429 | // Adjust the types we're converting from via the array-to-pointer |
| 3430 | // conversion, if we need to. |
| 3431 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3432 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3433 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3434 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3435 | |
Douglas Gregor | 30ee16f | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3436 | QualType FromPointee1 = FromType1->getPointeeType().getUnqualifiedType(); |
| 3437 | QualType FromPointee2 = FromType2->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3438 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3439 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3440 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3441 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3442 | return ImplicitConversionSequence::Worse; |
| 3443 | |
| 3444 | // Objective-C++: If one interface is more specific than the |
| 3445 | // other, it is the better one. |
Douglas Gregor | 30ee16f | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3446 | const ObjCObjectPointerType* FromObjCPtr1 |
| 3447 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3448 | const ObjCObjectPointerType* FromObjCPtr2 |
| 3449 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3450 | if (FromObjCPtr1 && FromObjCPtr2) { |
| 3451 | bool AssignLeft = S.Context.canAssignObjCInterfaces(FromObjCPtr1, |
| 3452 | FromObjCPtr2); |
| 3453 | bool AssignRight = S.Context.canAssignObjCInterfaces(FromObjCPtr2, |
| 3454 | FromObjCPtr1); |
| 3455 | if (AssignLeft != AssignRight) { |
| 3456 | return AssignLeft? ImplicitConversionSequence::Better |
| 3457 | : ImplicitConversionSequence::Worse; |
| 3458 | } |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3459 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3460 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3461 | |
| 3462 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 3463 | // bullet 3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3464 | if (ImplicitConversionSequence::CompareKind QualCK |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3465 | = CompareQualificationConversions(S, SCS1, SCS2)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3466 | return QualCK; |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3467 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3468 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3469 | // Check for a better reference binding based on the kind of bindings. |
| 3470 | if (isBetterReferenceBindingKind(SCS1, SCS2)) |
| 3471 | return ImplicitConversionSequence::Better; |
| 3472 | else if (isBetterReferenceBindingKind(SCS2, SCS1)) |
| 3473 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3474 | |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 3475 | // C++ [over.ics.rank]p3b4: |
| 3476 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 3477 | // which the references refer are the same type except for |
| 3478 | // top-level cv-qualifiers, and the type to which the reference |
| 3479 | // initialized by S2 refers is more cv-qualified than the type |
| 3480 | // to which the reference initialized by S1 refers. |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3481 | QualType T1 = SCS1.getToType(2); |
| 3482 | QualType T2 = SCS2.getToType(2); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3483 | T1 = S.Context.getCanonicalType(T1); |
| 3484 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3485 | Qualifiers T1Quals, T2Quals; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3486 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3487 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3488 | if (UnqualT1 == UnqualT2) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3489 | // Objective-C++ ARC: If the references refer to objects with different |
| 3490 | // lifetimes, prefer bindings that don't change lifetime. |
| 3491 | if (SCS1.ObjCLifetimeConversionBinding != |
| 3492 | SCS2.ObjCLifetimeConversionBinding) { |
| 3493 | return SCS1.ObjCLifetimeConversionBinding |
| 3494 | ? ImplicitConversionSequence::Worse |
| 3495 | : ImplicitConversionSequence::Better; |
| 3496 | } |
| 3497 | |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3498 | // If the type is an array type, promote the element qualifiers to the |
| 3499 | // type for comparison. |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3500 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3501 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3502 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3503 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3504 | if (T2.isMoreQualifiedThan(T1)) |
| 3505 | return ImplicitConversionSequence::Better; |
| 3506 | else if (T1.isMoreQualifiedThan(T2)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3507 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3508 | } |
| 3509 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3510 | |
Francois Pichet | 08d2fa0 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3511 | // In Microsoft mode, prefer an integral conversion to a |
| 3512 | // floating-to-integral conversion if the integral conversion |
| 3513 | // is between types of the same size. |
| 3514 | // For example: |
| 3515 | // void f(float); |
| 3516 | // void f(int); |
| 3517 | // int main { |
| 3518 | // long a; |
| 3519 | // f(a); |
| 3520 | // } |
| 3521 | // Here, MSVC will call f(int) instead of generating a compile error |
| 3522 | // as clang will do in standard mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3523 | if (S.getLangOpts().MicrosoftMode && |
Francois Pichet | 08d2fa0 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3524 | SCS1.Second == ICK_Integral_Conversion && |
| 3525 | SCS2.Second == ICK_Floating_Integral && |
| 3526 | S.Context.getTypeSize(SCS1.getFromType()) == |
| 3527 | S.Context.getTypeSize(SCS1.getToType(2))) |
| 3528 | return ImplicitConversionSequence::Better; |
| 3529 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3530 | return ImplicitConversionSequence::Indistinguishable; |
| 3531 | } |
| 3532 | |
| 3533 | /// CompareQualificationConversions - Compares two standard conversion |
| 3534 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3535 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 3536 | ImplicitConversionSequence::CompareKind |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3537 | CompareQualificationConversions(Sema &S, |
| 3538 | const StandardConversionSequence& SCS1, |
| 3539 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | 4b62ec6 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 3540 | // C++ 13.3.3.2p3: |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3541 | // -- S1 and S2 differ only in their qualification conversion and |
| 3542 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 3543 | // cv-qualification signature of type T1 is a proper subset of |
| 3544 | // the cv-qualification signature of type T2, and S1 is not the |
| 3545 | // deprecated string literal array-to-pointer conversion (4.2). |
| 3546 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 3547 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 3548 | return ImplicitConversionSequence::Indistinguishable; |
| 3549 | |
| 3550 | // FIXME: the example in the standard doesn't use a qualification |
| 3551 | // conversion (!) |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3552 | QualType T1 = SCS1.getToType(2); |
| 3553 | QualType T2 = SCS2.getToType(2); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3554 | T1 = S.Context.getCanonicalType(T1); |
| 3555 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3556 | Qualifiers T1Quals, T2Quals; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3557 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3558 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3559 | |
| 3560 | // If the types are the same, we won't learn anything by unwrapped |
| 3561 | // them. |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3562 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3563 | return ImplicitConversionSequence::Indistinguishable; |
| 3564 | |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3565 | // If the type is an array type, promote the element qualifiers to the type |
| 3566 | // for comparison. |
| 3567 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3568 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3569 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3570 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3571 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3572 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3573 | = ImplicitConversionSequence::Indistinguishable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3574 | |
| 3575 | // Objective-C++ ARC: |
| 3576 | // Prefer qualification conversions not involving a change in lifetime |
| 3577 | // to qualification conversions that do not change lifetime. |
| 3578 | if (SCS1.QualificationIncludesObjCLifetime != |
| 3579 | SCS2.QualificationIncludesObjCLifetime) { |
| 3580 | Result = SCS1.QualificationIncludesObjCLifetime |
| 3581 | ? ImplicitConversionSequence::Worse |
| 3582 | : ImplicitConversionSequence::Better; |
| 3583 | } |
| 3584 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3585 | while (S.Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3586 | // Within each iteration of the loop, we check the qualifiers to |
| 3587 | // determine if this still looks like a qualification |
| 3588 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 3589 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3590 | // until there are no more pointers or pointers-to-members left |
| 3591 | // to unwrap. This essentially mimics what |
| 3592 | // IsQualificationConversion does, but here we're checking for a |
| 3593 | // strict subset of qualifiers. |
| 3594 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 3595 | // The qualifiers are the same, so this doesn't tell us anything |
| 3596 | // about how the sequences rank. |
| 3597 | ; |
| 3598 | else if (T2.isMoreQualifiedThan(T1)) { |
| 3599 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 3600 | if (Result == ImplicitConversionSequence::Worse) |
| 3601 | // Neither has qualifiers that are a subset of the other's |
| 3602 | // qualifiers. |
| 3603 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3604 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3605 | Result = ImplicitConversionSequence::Better; |
| 3606 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 3607 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 3608 | if (Result == ImplicitConversionSequence::Better) |
| 3609 | // Neither has qualifiers that are a subset of the other's |
| 3610 | // qualifiers. |
| 3611 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3612 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3613 | Result = ImplicitConversionSequence::Worse; |
| 3614 | } else { |
| 3615 | // Qualifiers are disjoint. |
| 3616 | return ImplicitConversionSequence::Indistinguishable; |
| 3617 | } |
| 3618 | |
| 3619 | // If the types after this point are equivalent, we're done. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3620 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3621 | break; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3622 | } |
| 3623 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3624 | // Check that the winning standard conversion sequence isn't using |
| 3625 | // the deprecated string literal array to pointer conversion. |
| 3626 | switch (Result) { |
| 3627 | case ImplicitConversionSequence::Better: |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3628 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3629 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3630 | break; |
| 3631 | |
| 3632 | case ImplicitConversionSequence::Indistinguishable: |
| 3633 | break; |
| 3634 | |
| 3635 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3636 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3637 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3638 | break; |
| 3639 | } |
| 3640 | |
| 3641 | return Result; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3642 | } |
| 3643 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3644 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 3645 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3646 | /// various kinds of derived-to-base conversions (C++ |
| 3647 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 3648 | /// conversions between Objective-C interface types. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3649 | ImplicitConversionSequence::CompareKind |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3650 | CompareDerivedToBaseConversions(Sema &S, |
| 3651 | const StandardConversionSequence& SCS1, |
| 3652 | const StandardConversionSequence& SCS2) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3653 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3654 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3655 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3656 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3657 | |
| 3658 | // Adjust the types we're converting from via the array-to-pointer |
| 3659 | // conversion, if we need to. |
| 3660 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3661 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3662 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3663 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3664 | |
| 3665 | // Canonicalize all of the types. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3666 | FromType1 = S.Context.getCanonicalType(FromType1); |
| 3667 | ToType1 = S.Context.getCanonicalType(ToType1); |
| 3668 | FromType2 = S.Context.getCanonicalType(FromType2); |
| 3669 | ToType2 = S.Context.getCanonicalType(ToType2); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3670 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3671 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3672 | // |
| 3673 | // If class B is derived directly or indirectly from class A and |
| 3674 | // class C is derived directly or indirectly from B, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3675 | // |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3676 | // Compare based on pointer conversions. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3677 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 3678 | SCS2.Second == ICK_Pointer_Conversion && |
| 3679 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 3680 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 3681 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3682 | QualType FromPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3683 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3684 | QualType ToPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3685 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3686 | QualType FromPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3687 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3688 | QualType ToPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3689 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3690 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3691 | // -- conversion of C* to B* is better than conversion of C* to A*, |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3692 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3693 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3694 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3695 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3696 | return ImplicitConversionSequence::Worse; |
| 3697 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3698 | |
| 3699 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 3700 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3701 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3702 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3703 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3704 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3705 | } |
| 3706 | } else if (SCS1.Second == ICK_Pointer_Conversion && |
| 3707 | SCS2.Second == ICK_Pointer_Conversion) { |
| 3708 | const ObjCObjectPointerType *FromPtr1 |
| 3709 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3710 | const ObjCObjectPointerType *FromPtr2 |
| 3711 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3712 | const ObjCObjectPointerType *ToPtr1 |
| 3713 | = ToType1->getAs<ObjCObjectPointerType>(); |
| 3714 | const ObjCObjectPointerType *ToPtr2 |
| 3715 | = ToType2->getAs<ObjCObjectPointerType>(); |
| 3716 | |
| 3717 | if (FromPtr1 && FromPtr2 && ToPtr1 && ToPtr2) { |
| 3718 | // Apply the same conversion ranking rules for Objective-C pointer types |
| 3719 | // that we do for C++ pointers to class types. However, we employ the |
| 3720 | // Objective-C pseudo-subtyping relationship used for assignment of |
| 3721 | // Objective-C pointer types. |
| 3722 | bool FromAssignLeft |
| 3723 | = S.Context.canAssignObjCInterfaces(FromPtr1, FromPtr2); |
| 3724 | bool FromAssignRight |
| 3725 | = S.Context.canAssignObjCInterfaces(FromPtr2, FromPtr1); |
| 3726 | bool ToAssignLeft |
| 3727 | = S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2); |
| 3728 | bool ToAssignRight |
| 3729 | = S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1); |
| 3730 | |
| 3731 | // A conversion to an a non-id object pointer type or qualified 'id' |
| 3732 | // type is better than a conversion to 'id'. |
| 3733 | if (ToPtr1->isObjCIdType() && |
| 3734 | (ToPtr2->isObjCQualifiedIdType() || ToPtr2->getInterfaceDecl())) |
| 3735 | return ImplicitConversionSequence::Worse; |
| 3736 | if (ToPtr2->isObjCIdType() && |
| 3737 | (ToPtr1->isObjCQualifiedIdType() || ToPtr1->getInterfaceDecl())) |
| 3738 | return ImplicitConversionSequence::Better; |
| 3739 | |
| 3740 | // A conversion to a non-id object pointer type is better than a |
| 3741 | // conversion to a qualified 'id' type |
| 3742 | if (ToPtr1->isObjCQualifiedIdType() && ToPtr2->getInterfaceDecl()) |
| 3743 | return ImplicitConversionSequence::Worse; |
| 3744 | if (ToPtr2->isObjCQualifiedIdType() && ToPtr1->getInterfaceDecl()) |
| 3745 | return ImplicitConversionSequence::Better; |
| 3746 | |
| 3747 | // A conversion to an a non-Class object pointer type or qualified 'Class' |
| 3748 | // type is better than a conversion to 'Class'. |
| 3749 | if (ToPtr1->isObjCClassType() && |
| 3750 | (ToPtr2->isObjCQualifiedClassType() || ToPtr2->getInterfaceDecl())) |
| 3751 | return ImplicitConversionSequence::Worse; |
| 3752 | if (ToPtr2->isObjCClassType() && |
| 3753 | (ToPtr1->isObjCQualifiedClassType() || ToPtr1->getInterfaceDecl())) |
| 3754 | return ImplicitConversionSequence::Better; |
| 3755 | |
| 3756 | // A conversion to a non-Class object pointer type is better than a |
| 3757 | // conversion to a qualified 'Class' type. |
| 3758 | if (ToPtr1->isObjCQualifiedClassType() && ToPtr2->getInterfaceDecl()) |
| 3759 | return ImplicitConversionSequence::Worse; |
| 3760 | if (ToPtr2->isObjCQualifiedClassType() && ToPtr1->getInterfaceDecl()) |
| 3761 | return ImplicitConversionSequence::Better; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3762 | |
Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3763 | // -- "conversion of C* to B* is better than conversion of C* to A*," |
| 3764 | if (S.Context.hasSameType(FromType1, FromType2) && |
| 3765 | !FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType() && |
| 3766 | (ToAssignLeft != ToAssignRight)) |
| 3767 | return ToAssignLeft? ImplicitConversionSequence::Worse |
| 3768 | : ImplicitConversionSequence::Better; |
| 3769 | |
| 3770 | // -- "conversion of B* to A* is better than conversion of C* to A*," |
| 3771 | if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) && |
| 3772 | (FromAssignLeft != FromAssignRight)) |
| 3773 | return FromAssignLeft? ImplicitConversionSequence::Better |
| 3774 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3775 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3776 | } |
Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3777 | |
Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3778 | // Ranking of member-pointer types. |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3779 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 3780 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 3781 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3782 | const MemberPointerType * FromMemPointer1 = |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3783 | FromType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3784 | const MemberPointerType * ToMemPointer1 = |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3785 | ToType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3786 | const MemberPointerType * FromMemPointer2 = |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3787 | FromType2->getAs<MemberPointerType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3788 | const MemberPointerType * ToMemPointer2 = |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3789 | ToType2->getAs<MemberPointerType>(); |
| 3790 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 3791 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 3792 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 3793 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 3794 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 3795 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 3796 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 3797 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3798 | // conversion of A::* to B::* is better than conversion of A::* to C::*, |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3799 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3800 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3801 | return ImplicitConversionSequence::Worse; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3802 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3803 | return ImplicitConversionSequence::Better; |
| 3804 | } |
| 3805 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 3806 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3807 | if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3808 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3809 | else if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3810 | return ImplicitConversionSequence::Worse; |
| 3811 | } |
| 3812 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3813 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3814 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3815 | // -- conversion of C to B is better than conversion of C to A, |
Douglas Gregor | 83af86a | 2010-02-25 19:01:05 +0000 | [diff] [blame] | 3816 | // -- binding of an expression of type C to a reference of type |
| 3817 | // B& is better than binding an expression of type C to a |
| 3818 | // reference of type A&, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3819 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3820 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3821 | if (S.IsDerivedFrom(ToType1, ToType2)) |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3822 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3823 | else if (S.IsDerivedFrom(ToType2, ToType1)) |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3824 | return ImplicitConversionSequence::Worse; |
| 3825 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3826 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3827 | // -- conversion of B to A is better than conversion of C to A. |
Douglas Gregor | 83af86a | 2010-02-25 19:01:05 +0000 | [diff] [blame] | 3828 | // -- binding of an expression of type B to a reference of type |
| 3829 | // A& is better than binding an expression of type C to a |
| 3830 | // reference of type A&, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3831 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3832 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3833 | if (S.IsDerivedFrom(FromType2, FromType1)) |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3834 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3835 | else if (S.IsDerivedFrom(FromType1, FromType2)) |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3836 | return ImplicitConversionSequence::Worse; |
| 3837 | } |
| 3838 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3839 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3840 | return ImplicitConversionSequence::Indistinguishable; |
| 3841 | } |
| 3842 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3843 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 3844 | /// determine whether they are reference-related, |
| 3845 | /// reference-compatible, reference-compatible with added |
| 3846 | /// qualification, or incompatible, for use in C++ initialization by |
| 3847 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 3848 | /// type, and the first type (T1) is the pointee type of the reference |
| 3849 | /// type being initialized. |
| 3850 | Sema::ReferenceCompareResult |
| 3851 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 3852 | QualType OrigT1, QualType OrigT2, |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3853 | bool &DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3854 | bool &ObjCConversion, |
| 3855 | bool &ObjCLifetimeConversion) { |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3856 | assert(!OrigT1->isReferenceType() && |
| 3857 | "T1 must be the pointee type of the reference type"); |
| 3858 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 3859 | |
| 3860 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 3861 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 3862 | Qualifiers T1Quals, T2Quals; |
| 3863 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3864 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 3865 | |
| 3866 | // C++ [dcl.init.ref]p4: |
| 3867 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 3868 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 3869 | // T1 is a base class of T2. |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3870 | DerivedToBase = false; |
| 3871 | ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3872 | ObjCLifetimeConversion = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3873 | if (UnqualT1 == UnqualT2) { |
| 3874 | // Nothing to do. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3875 | } else if (!RequireCompleteType(Loc, OrigT2, 0) && |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3876 | IsDerivedFrom(UnqualT2, UnqualT1)) |
| 3877 | DerivedToBase = true; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3878 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
| 3879 | UnqualT2->isObjCObjectOrInterfaceType() && |
| 3880 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
| 3881 | ObjCConversion = true; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3882 | else |
| 3883 | return Ref_Incompatible; |
| 3884 | |
| 3885 | // At this point, we know that T1 and T2 are reference-related (at |
| 3886 | // least). |
| 3887 | |
| 3888 | // If the type is an array type, promote the element qualifiers to the type |
| 3889 | // for comparison. |
| 3890 | if (isa<ArrayType>(T1) && T1Quals) |
| 3891 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 3892 | if (isa<ArrayType>(T2) && T2Quals) |
| 3893 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 3894 | |
| 3895 | // C++ [dcl.init.ref]p4: |
| 3896 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 3897 | // reference-related to T2 and cv1 is the same cv-qualification |
| 3898 | // as, or greater cv-qualification than, cv2. For purposes of |
| 3899 | // overload resolution, cases for which cv1 is greater |
| 3900 | // cv-qualification than cv2 are identified as |
| 3901 | // reference-compatible with added qualification (see 13.3.3.2). |
Douglas Gregor | d517d55 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 3902 | // |
| 3903 | // Note that we also require equivalence of Objective-C GC and address-space |
| 3904 | // qualifiers when performing these computations, so that e.g., an int in |
| 3905 | // address space 1 is not reference-compatible with an int in address |
| 3906 | // space 2. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3907 | if (T1Quals.getObjCLifetime() != T2Quals.getObjCLifetime() && |
| 3908 | T1Quals.compatiblyIncludesObjCLifetime(T2Quals)) { |
| 3909 | T1Quals.removeObjCLifetime(); |
| 3910 | T2Quals.removeObjCLifetime(); |
| 3911 | ObjCLifetimeConversion = true; |
| 3912 | } |
| 3913 | |
Douglas Gregor | d517d55 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 3914 | if (T1Quals == T2Quals) |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3915 | return Ref_Compatible; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3916 | else if (T1Quals.compatiblyIncludes(T2Quals)) |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3917 | return Ref_Compatible_With_Added_Qualification; |
| 3918 | else |
| 3919 | return Ref_Related; |
| 3920 | } |
| 3921 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3922 | /// \brief Look for a user-defined conversion to an value reference-compatible |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3923 | /// with DeclType. Return true if something definite is found. |
| 3924 | static bool |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3925 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
| 3926 | QualType DeclType, SourceLocation DeclLoc, |
| 3927 | Expr *Init, QualType T2, bool AllowRvalues, |
| 3928 | bool AllowExplicit) { |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3929 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 3930 | CXXRecordDecl *T2RecordDecl |
| 3931 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 3932 | |
| 3933 | OverloadCandidateSet CandidateSet(DeclLoc); |
| 3934 | const UnresolvedSetImpl *Conversions |
| 3935 | = T2RecordDecl->getVisibleConversionFunctions(); |
| 3936 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 3937 | E = Conversions->end(); I != E; ++I) { |
| 3938 | NamedDecl *D = *I; |
| 3939 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3940 | if (isa<UsingShadowDecl>(D)) |
| 3941 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3942 | |
| 3943 | FunctionTemplateDecl *ConvTemplate |
| 3944 | = dyn_cast<FunctionTemplateDecl>(D); |
| 3945 | CXXConversionDecl *Conv; |
| 3946 | if (ConvTemplate) |
| 3947 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3948 | else |
| 3949 | Conv = cast<CXXConversionDecl>(D); |
| 3950 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3951 | // If this is an explicit conversion, and we're not allowed to consider |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3952 | // explicit conversions, skip it. |
| 3953 | if (!AllowExplicit && Conv->isExplicit()) |
| 3954 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3955 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3956 | if (AllowRvalues) { |
| 3957 | bool DerivedToBase = false; |
| 3958 | bool ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3959 | bool ObjCLifetimeConversion = false; |
Douglas Gregor | b0e6c8a | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 3960 | |
| 3961 | // If we are initializing an rvalue reference, don't permit conversion |
| 3962 | // functions that return lvalues. |
| 3963 | if (!ConvTemplate && DeclType->isRValueReferenceType()) { |
| 3964 | const ReferenceType *RefType |
| 3965 | = Conv->getConversionType()->getAs<LValueReferenceType>(); |
| 3966 | if (RefType && !RefType->getPointeeType()->isFunctionType()) |
| 3967 | continue; |
| 3968 | } |
| 3969 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3970 | if (!ConvTemplate && |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3971 | S.CompareReferenceRelationship( |
| 3972 | DeclLoc, |
| 3973 | Conv->getConversionType().getNonReferenceType() |
| 3974 | .getUnqualifiedType(), |
| 3975 | DeclType.getNonReferenceType().getUnqualifiedType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3976 | DerivedToBase, ObjCConversion, ObjCLifetimeConversion) == |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3977 | Sema::Ref_Incompatible) |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3978 | continue; |
| 3979 | } else { |
| 3980 | // If the conversion function doesn't return a reference type, |
| 3981 | // it can't be considered for this conversion. An rvalue reference |
| 3982 | // is only acceptable if its referencee is a function type. |
| 3983 | |
| 3984 | const ReferenceType *RefType = |
| 3985 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 3986 | if (!RefType || |
| 3987 | (!RefType->isLValueReferenceType() && |
| 3988 | !RefType->getPointeeType()->isFunctionType())) |
| 3989 | continue; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3990 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3991 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3992 | if (ConvTemplate) |
| 3993 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3994 | Init, DeclType, CandidateSet); |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3995 | else |
| 3996 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3997 | DeclType, CandidateSet); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3998 | } |
| 3999 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4000 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4001 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4002 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4003 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4004 | case OR_Success: |
| 4005 | // C++ [over.ics.ref]p1: |
| 4006 | // |
| 4007 | // [...] If the parameter binds directly to the result of |
| 4008 | // applying a conversion function to the argument |
| 4009 | // expression, the implicit conversion sequence is a |
| 4010 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 4011 | // second standard conversion sequence either an identity |
| 4012 | // conversion or, if the conversion function returns an |
| 4013 | // entity of a type that is a derived class of the parameter |
| 4014 | // type, a derived-to-base Conversion. |
| 4015 | if (!Best->FinalConversion.DirectBinding) |
| 4016 | return false; |
| 4017 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4018 | if (Best->Function) |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 4019 | S.MarkFunctionReferenced(DeclLoc, Best->Function); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4020 | ICS.setUserDefined(); |
| 4021 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 4022 | ICS.UserDefined.After = Best->FinalConversion; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4023 | ICS.UserDefined.HadMultipleCandidates = HadMultipleCandidates; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4024 | ICS.UserDefined.ConversionFunction = Best->Function; |
John McCall | 3090903 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 4025 | ICS.UserDefined.FoundConversionFunction = Best->FoundDecl; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4026 | ICS.UserDefined.EllipsisConversion = false; |
| 4027 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 4028 | ICS.UserDefined.After.DirectBinding && |
| 4029 | "Expected a direct reference binding!"); |
| 4030 | return true; |
| 4031 | |
| 4032 | case OR_Ambiguous: |
| 4033 | ICS.setAmbiguous(); |
| 4034 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4035 | Cand != CandidateSet.end(); ++Cand) |
| 4036 | if (Cand->Viable) |
| 4037 | ICS.Ambiguous.addConversion(Cand->Function); |
| 4038 | return true; |
| 4039 | |
| 4040 | case OR_No_Viable_Function: |
| 4041 | case OR_Deleted: |
| 4042 | // There was no suitable conversion, or we found a deleted |
| 4043 | // conversion; continue with other checks. |
| 4044 | return false; |
| 4045 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4046 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4047 | llvm_unreachable("Invalid OverloadResult!"); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4050 | /// \brief Compute an implicit conversion sequence for reference |
| 4051 | /// initialization. |
| 4052 | static ImplicitConversionSequence |
Sebastian Redl | df88864 | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4053 | TryReferenceInit(Sema &S, Expr *Init, QualType DeclType, |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4054 | SourceLocation DeclLoc, |
| 4055 | bool SuppressUserConversions, |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 4056 | bool AllowExplicit) { |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4057 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 4058 | |
| 4059 | // Most paths end in a failed conversion. |
| 4060 | ImplicitConversionSequence ICS; |
| 4061 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4062 | |
| 4063 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 4064 | QualType T2 = Init->getType(); |
| 4065 | |
| 4066 | // If the initializer is the address of an overloaded function, try |
| 4067 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4068 | // type of the resulting function. |
| 4069 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4070 | DeclAccessPair Found; |
| 4071 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 4072 | false, Found)) |
| 4073 | T2 = Fn->getType(); |
| 4074 | } |
| 4075 | |
| 4076 | // Compute some basic properties of the types and the initializer. |
| 4077 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 4078 | bool DerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4079 | bool ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4080 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4081 | Expr::Classification InitCategory = Init->Classify(S.Context); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4082 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4083 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4084 | ObjCConversion, ObjCLifetimeConversion); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4085 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4086 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4087 | // C++0x [dcl.init.ref]p5: |
Douglas Gregor | 870f374 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 4088 | // A reference to type "cv1 T1" is initialized by an expression |
| 4089 | // of type "cv2 T2" as follows: |
| 4090 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4091 | // -- If reference is an lvalue reference and the initializer expression |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4092 | if (!isRValRef) { |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4093 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 4094 | // reference-compatible with "cv2 T2," or |
| 4095 | // |
| 4096 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 4097 | if (InitCategory.isLValue() && |
| 4098 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4099 | // C++ [over.ics.ref]p1: |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4100 | // When a parameter of reference type binds directly (8.5.3) |
| 4101 | // to an argument expression, the implicit conversion sequence |
| 4102 | // is the identity conversion, unless the argument expression |
| 4103 | // has a type that is a derived class of the parameter type, |
| 4104 | // in which case the implicit conversion sequence is a |
| 4105 | // derived-to-base Conversion (13.3.3.1). |
| 4106 | ICS.setStandard(); |
| 4107 | ICS.Standard.First = ICK_Identity; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4108 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 4109 | : ObjCConversion? ICK_Compatible_Conversion |
| 4110 | : ICK_Identity; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4111 | ICS.Standard.Third = ICK_Identity; |
| 4112 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4113 | ICS.Standard.setToType(0, T2); |
| 4114 | ICS.Standard.setToType(1, T1); |
| 4115 | ICS.Standard.setToType(2, T1); |
| 4116 | ICS.Standard.ReferenceBinding = true; |
| 4117 | ICS.Standard.DirectBinding = true; |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4118 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4119 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4120 | ICS.Standard.BindsToRvalue = false; |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4121 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4122 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4123 | ICS.Standard.CopyConstructor = 0; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4124 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4125 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 4126 | // derived-to-base conversions is suppressed when we're |
| 4127 | // computing the implicit conversion sequence (C++ |
| 4128 | // [over.best.ics]p2). |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4129 | return ICS; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4130 | } |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4131 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4132 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 4133 | // not reference-related to T2, and can be implicitly |
| 4134 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 4135 | // is reference-compatible with "cv3 T3" 92) (this |
| 4136 | // conversion is selected by enumerating the applicable |
| 4137 | // conversion functions (13.3.1.6) and choosing the best |
| 4138 | // one through overload resolution (13.3)), |
| 4139 | if (!SuppressUserConversions && T2->isRecordType() && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4140 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4141 | RefRelationship == Sema::Ref_Incompatible) { |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 4142 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4143 | Init, T2, /*AllowRvalues=*/false, |
| 4144 | AllowExplicit)) |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4145 | return ICS; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4146 | } |
| 4147 | } |
| 4148 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4149 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 4150 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4151 | // shall be an rvalue reference. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4152 | // |
Douglas Gregor | 870f374 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 4153 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 4154 | // point, which is that, due to p2 (which short-circuits reference |
| 4155 | // binding by only attempting a simple conversion for non-direct |
| 4156 | // bindings) and p3's strange wording, we allow a const volatile |
| 4157 | // reference to bind to an rvalue. Hence the check for the presence |
| 4158 | // of "const" rather than checking for "const" being the only |
| 4159 | // qualifier. |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 4160 | // This is also the point where rvalue references and lvalue inits no longer |
| 4161 | // go together. |
Richard Smith | ce4f608 | 2012-05-24 04:29:20 +0000 | [diff] [blame] | 4162 | if (!isRValRef && (!T1.isConstQualified() || T1.isVolatileQualified())) |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4163 | return ICS; |
| 4164 | |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4165 | // -- If the initializer expression |
| 4166 | // |
| 4167 | // -- is an xvalue, class prvalue, array prvalue or function |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4168 | // lvalue and "cv1 T1" is reference-compatible with "cv2 T2", or |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4169 | if (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification && |
| 4170 | (InitCategory.isXValue() || |
| 4171 | (InitCategory.isPRValue() && (T2->isRecordType() || T2->isArrayType())) || |
| 4172 | (InitCategory.isLValue() && T2->isFunctionType()))) { |
| 4173 | ICS.setStandard(); |
| 4174 | ICS.Standard.First = ICK_Identity; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4175 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4176 | : ObjCConversion? ICK_Compatible_Conversion |
| 4177 | : ICK_Identity; |
| 4178 | ICS.Standard.Third = ICK_Identity; |
| 4179 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4180 | ICS.Standard.setToType(0, T2); |
| 4181 | ICS.Standard.setToType(1, T1); |
| 4182 | ICS.Standard.setToType(2, T1); |
| 4183 | ICS.Standard.ReferenceBinding = true; |
| 4184 | // In C++0x, this is always a direct binding. In C++98/03, it's a direct |
| 4185 | // binding unless we're binding to a class prvalue. |
| 4186 | // Note: Although xvalues wouldn't normally show up in C++98/03 code, we |
| 4187 | // allow the use of rvalue references in C++98/03 for the benefit of |
| 4188 | // standard library implementors; therefore, we need the xvalue check here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4189 | ICS.Standard.DirectBinding = |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4190 | S.getLangOpts().CPlusPlus0x || |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4191 | (InitCategory.isPRValue() && !T2->isRecordType()); |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4192 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4193 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4194 | ICS.Standard.BindsToRvalue = InitCategory.isRValue(); |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4195 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4196 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4197 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4198 | return ICS; |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4199 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4200 | |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4201 | // -- has a class type (i.e., T2 is a class type), where T1 is not |
| 4202 | // reference-related to T2, and can be implicitly converted to |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4203 | // an xvalue, class prvalue, or function lvalue of type |
| 4204 | // "cv3 T3", where "cv1 T1" is reference-compatible with |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4205 | // "cv3 T3", |
| 4206 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4207 | // then the reference is bound to the value of the initializer |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4208 | // expression in the first case and to the result of the conversion |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4209 | // in the second case (or, in either case, to an appropriate base |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4210 | // class subobject). |
| 4211 | if (!SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4212 | T2->isRecordType() && !S.RequireCompleteType(DeclLoc, T2, 0) && |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4213 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 4214 | Init, T2, /*AllowRvalues=*/true, |
| 4215 | AllowExplicit)) { |
| 4216 | // In the second case, if the reference is an rvalue reference |
| 4217 | // and the second standard conversion sequence of the |
| 4218 | // user-defined conversion sequence includes an lvalue-to-rvalue |
| 4219 | // conversion, the program is ill-formed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4220 | if (ICS.isUserDefined() && isRValRef && |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 4221 | ICS.UserDefined.After.First == ICK_Lvalue_To_Rvalue) |
| 4222 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 4223 | |
Douglas Gregor | 95273c3 | 2011-01-21 16:36:05 +0000 | [diff] [blame] | 4224 | return ICS; |
Rafael Espindola | be468d9 | 2011-01-22 15:32:35 +0000 | [diff] [blame] | 4225 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4226 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4227 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 4228 | // initialized from the initializer expression using the |
| 4229 | // rules for a non-reference copy initialization (8.5). The |
| 4230 | // reference is then bound to the temporary. If T1 is |
| 4231 | // reference-related to T2, cv1 must be the same |
| 4232 | // cv-qualification as, or greater cv-qualification than, |
| 4233 | // cv2; otherwise, the program is ill-formed. |
| 4234 | if (RefRelationship == Sema::Ref_Related) { |
| 4235 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 4236 | // we would be reference-compatible or reference-compatible with |
| 4237 | // added qualification. But that wasn't the case, so the reference |
| 4238 | // initialization fails. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4239 | // |
| 4240 | // Note that we only want to check address spaces and cvr-qualifiers here. |
| 4241 | // ObjC GC and lifetime qualifiers aren't important. |
| 4242 | Qualifiers T1Quals = T1.getQualifiers(); |
| 4243 | Qualifiers T2Quals = T2.getQualifiers(); |
| 4244 | T1Quals.removeObjCGCAttr(); |
| 4245 | T1Quals.removeObjCLifetime(); |
| 4246 | T2Quals.removeObjCGCAttr(); |
| 4247 | T2Quals.removeObjCLifetime(); |
| 4248 | if (!T1Quals.compatiblyIncludes(T2Quals)) |
| 4249 | return ICS; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4250 | } |
| 4251 | |
| 4252 | // If at least one of the types is a class type, the types are not |
| 4253 | // related, and we aren't allowed any user conversions, the |
| 4254 | // reference binding fails. This case is important for breaking |
| 4255 | // recursion, since TryImplicitConversion below will attempt to |
| 4256 | // create a temporary through the use of a copy constructor. |
| 4257 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 4258 | (T1->isRecordType() || T2->isRecordType())) |
| 4259 | return ICS; |
| 4260 | |
Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4261 | // If T1 is reference-related to T2 and the reference is an rvalue |
| 4262 | // reference, the initializer expression shall not be an lvalue. |
| 4263 | if (RefRelationship >= Sema::Ref_Related && |
| 4264 | isRValRef && Init->Classify(S.Context).isLValue()) |
| 4265 | return ICS; |
| 4266 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4267 | // C++ [over.ics.ref]p2: |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4268 | // When a parameter of reference type is not bound directly to |
| 4269 | // an argument expression, the conversion sequence is the one |
| 4270 | // required to convert the argument expression to the |
| 4271 | // underlying type of the reference according to |
| 4272 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 4273 | // to copy-initializing a temporary of the underlying type with |
| 4274 | // the argument expression. Any difference in top-level |
| 4275 | // cv-qualification is subsumed by the initialization itself |
| 4276 | // and does not constitute a conversion. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4277 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
| 4278 | /*AllowExplicit=*/false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4279 | /*InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4280 | /*CStyle=*/false, |
| 4281 | /*AllowObjCWritebackConversion=*/false); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4282 | |
| 4283 | // Of course, that's still a reference binding. |
| 4284 | if (ICS.isStandard()) { |
| 4285 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4286 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 4287 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4288 | ICS.Standard.BindsToRvalue = true; |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4289 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4290 | ICS.Standard.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4291 | } else if (ICS.isUserDefined()) { |
Douglas Gregor | b0e6c8a | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 4292 | // Don't allow rvalue references to bind to lvalues. |
| 4293 | if (DeclType->isRValueReferenceType()) { |
| 4294 | if (const ReferenceType *RefType |
| 4295 | = ICS.UserDefined.ConversionFunction->getResultType() |
| 4296 | ->getAs<LValueReferenceType>()) { |
| 4297 | if (!RefType->getPointeeType()->isFunctionType()) { |
| 4298 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, Init, |
| 4299 | DeclType); |
| 4300 | return ICS; |
| 4301 | } |
| 4302 | } |
| 4303 | } |
| 4304 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4305 | ICS.UserDefined.After.ReferenceBinding = true; |
Douglas Gregor | 3ec7910 | 2011-08-15 13:59:46 +0000 | [diff] [blame] | 4306 | ICS.UserDefined.After.IsLvalueReference = !isRValRef; |
| 4307 | ICS.UserDefined.After.BindsToFunctionLvalue = T2->isFunctionType(); |
| 4308 | ICS.UserDefined.After.BindsToRvalue = true; |
| 4309 | ICS.UserDefined.After.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4310 | ICS.UserDefined.After.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4311 | } |
Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4312 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4313 | return ICS; |
| 4314 | } |
| 4315 | |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4316 | static ImplicitConversionSequence |
| 4317 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
| 4318 | bool SuppressUserConversions, |
| 4319 | bool InOverloadResolution, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4320 | bool AllowObjCWritebackConversion, |
| 4321 | bool AllowExplicit = false); |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4322 | |
| 4323 | /// TryListConversion - Try to copy-initialize a value of type ToType from the |
| 4324 | /// initializer list From. |
| 4325 | static ImplicitConversionSequence |
| 4326 | TryListConversion(Sema &S, InitListExpr *From, QualType ToType, |
| 4327 | bool SuppressUserConversions, |
| 4328 | bool InOverloadResolution, |
| 4329 | bool AllowObjCWritebackConversion) { |
| 4330 | // C++11 [over.ics.list]p1: |
| 4331 | // When an argument is an initializer list, it is not an expression and |
| 4332 | // special rules apply for converting it to a parameter type. |
| 4333 | |
| 4334 | ImplicitConversionSequence Result; |
| 4335 | Result.setBad(BadConversionSequence::no_conversion, From, ToType); |
Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4336 | Result.setListInitializationSequence(); |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4337 | |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4338 | // We need a complete type for what follows. Incomplete types can never be |
Sebastian Redl | 10f0fc0 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4339 | // initialized from init lists. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4340 | if (S.RequireCompleteType(From->getLocStart(), ToType, 0)) |
Sebastian Redl | 10f0fc0 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4341 | return Result; |
| 4342 | |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4343 | // C++11 [over.ics.list]p2: |
| 4344 | // If the parameter type is std::initializer_list<X> or "array of X" and |
| 4345 | // all the elements can be implicitly converted to X, the implicit |
| 4346 | // conversion sequence is the worst conversion necessary to convert an |
| 4347 | // element of the list to X. |
Sebastian Redl | aa6feaa | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4348 | bool toStdInitializerList = false; |
Sebastian Redl | 10f0fc0 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4349 | QualType X; |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4350 | if (ToType->isArrayType()) |
Sebastian Redl | 10f0fc0 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4351 | X = S.Context.getBaseElementType(ToType); |
| 4352 | else |
Sebastian Redl | aa6feaa | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4353 | toStdInitializerList = S.isStdInitializerList(ToType, &X); |
Sebastian Redl | 10f0fc0 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4354 | if (!X.isNull()) { |
| 4355 | for (unsigned i = 0, e = From->getNumInits(); i < e; ++i) { |
| 4356 | Expr *Init = From->getInit(i); |
| 4357 | ImplicitConversionSequence ICS = |
| 4358 | TryCopyInitialization(S, Init, X, SuppressUserConversions, |
| 4359 | InOverloadResolution, |
| 4360 | AllowObjCWritebackConversion); |
| 4361 | // If a single element isn't convertible, fail. |
| 4362 | if (ICS.isBad()) { |
| 4363 | Result = ICS; |
| 4364 | break; |
| 4365 | } |
| 4366 | // Otherwise, look for the worst conversion. |
| 4367 | if (Result.isBad() || |
| 4368 | CompareImplicitConversionSequences(S, ICS, Result) == |
| 4369 | ImplicitConversionSequence::Worse) |
| 4370 | Result = ICS; |
| 4371 | } |
Douglas Gregor | 0f5c1c0 | 2012-04-04 23:09:20 +0000 | [diff] [blame] | 4372 | |
| 4373 | // For an empty list, we won't have computed any conversion sequence. |
| 4374 | // Introduce the identity conversion sequence. |
| 4375 | if (From->getNumInits() == 0) { |
| 4376 | Result.setStandard(); |
| 4377 | Result.Standard.setAsIdentityConversion(); |
| 4378 | Result.Standard.setFromType(ToType); |
| 4379 | Result.Standard.setAllToTypes(ToType); |
| 4380 | } |
| 4381 | |
Sebastian Redl | 10f0fc0 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4382 | Result.setListInitializationSequence(); |
Sebastian Redl | aa6feaa | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 4383 | Result.setStdInitializerListElement(toStdInitializerList); |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4384 | return Result; |
Sebastian Redl | 10f0fc0 | 2012-01-17 22:49:48 +0000 | [diff] [blame] | 4385 | } |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4386 | |
| 4387 | // C++11 [over.ics.list]p3: |
| 4388 | // Otherwise, if the parameter is a non-aggregate class X and overload |
| 4389 | // resolution chooses a single best constructor [...] the implicit |
| 4390 | // conversion sequence is a user-defined conversion sequence. If multiple |
| 4391 | // constructors are viable but none is better than the others, the |
| 4392 | // implicit conversion sequence is a user-defined conversion sequence. |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4393 | if (ToType->isRecordType() && !ToType->isAggregateType()) { |
| 4394 | // This function can deal with initializer lists. |
| 4395 | Result = TryUserDefinedConversion(S, From, ToType, SuppressUserConversions, |
| 4396 | /*AllowExplicit=*/false, |
| 4397 | InOverloadResolution, /*CStyle=*/false, |
| 4398 | AllowObjCWritebackConversion); |
| 4399 | Result.setListInitializationSequence(); |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4400 | return Result; |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 4401 | } |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4402 | |
| 4403 | // C++11 [over.ics.list]p4: |
| 4404 | // Otherwise, if the parameter has an aggregate type which can be |
| 4405 | // initialized from the initializer list [...] the implicit conversion |
| 4406 | // sequence is a user-defined conversion sequence. |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4407 | if (ToType->isAggregateType()) { |
Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4408 | // Type is an aggregate, argument is an init list. At this point it comes |
| 4409 | // down to checking whether the initialization works. |
| 4410 | // FIXME: Find out whether this parameter is consumed or not. |
| 4411 | InitializedEntity Entity = |
| 4412 | InitializedEntity::InitializeParameter(S.Context, ToType, |
| 4413 | /*Consumed=*/false); |
| 4414 | if (S.CanPerformCopyInitialization(Entity, S.Owned(From))) { |
| 4415 | Result.setUserDefined(); |
| 4416 | Result.UserDefined.Before.setAsIdentityConversion(); |
| 4417 | // Initializer lists don't have a type. |
| 4418 | Result.UserDefined.Before.setFromType(QualType()); |
| 4419 | Result.UserDefined.Before.setAllToTypes(QualType()); |
| 4420 | |
| 4421 | Result.UserDefined.After.setAsIdentityConversion(); |
| 4422 | Result.UserDefined.After.setFromType(ToType); |
| 4423 | Result.UserDefined.After.setAllToTypes(ToType); |
Benjamin Kramer | b6d6508 | 2012-02-02 19:35:29 +0000 | [diff] [blame] | 4424 | Result.UserDefined.ConversionFunction = 0; |
Sebastian Redl | 72ef7bc | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 4425 | } |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4426 | return Result; |
| 4427 | } |
| 4428 | |
| 4429 | // C++11 [over.ics.list]p5: |
| 4430 | // Otherwise, if the parameter is a reference, see 13.3.3.1.4. |
Sebastian Redl | df88864 | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4431 | if (ToType->isReferenceType()) { |
| 4432 | // The standard is notoriously unclear here, since 13.3.3.1.4 doesn't |
| 4433 | // mention initializer lists in any way. So we go by what list- |
| 4434 | // initialization would do and try to extrapolate from that. |
| 4435 | |
| 4436 | QualType T1 = ToType->getAs<ReferenceType>()->getPointeeType(); |
| 4437 | |
| 4438 | // If the initializer list has a single element that is reference-related |
| 4439 | // to the parameter type, we initialize the reference from that. |
| 4440 | if (From->getNumInits() == 1) { |
| 4441 | Expr *Init = From->getInit(0); |
| 4442 | |
| 4443 | QualType T2 = Init->getType(); |
| 4444 | |
| 4445 | // If the initializer is the address of an overloaded function, try |
| 4446 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4447 | // type of the resulting function. |
| 4448 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 4449 | DeclAccessPair Found; |
| 4450 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction( |
| 4451 | Init, ToType, false, Found)) |
| 4452 | T2 = Fn->getType(); |
| 4453 | } |
| 4454 | |
| 4455 | // Compute some basic properties of the types and the initializer. |
| 4456 | bool dummy1 = false; |
| 4457 | bool dummy2 = false; |
| 4458 | bool dummy3 = false; |
| 4459 | Sema::ReferenceCompareResult RefRelationship |
| 4460 | = S.CompareReferenceRelationship(From->getLocStart(), T1, T2, dummy1, |
| 4461 | dummy2, dummy3); |
| 4462 | |
| 4463 | if (RefRelationship >= Sema::Ref_Related) |
| 4464 | return TryReferenceInit(S, Init, ToType, |
| 4465 | /*FIXME:*/From->getLocStart(), |
| 4466 | SuppressUserConversions, |
| 4467 | /*AllowExplicit=*/false); |
| 4468 | } |
| 4469 | |
| 4470 | // Otherwise, we bind the reference to a temporary created from the |
| 4471 | // initializer list. |
| 4472 | Result = TryListConversion(S, From, T1, SuppressUserConversions, |
| 4473 | InOverloadResolution, |
| 4474 | AllowObjCWritebackConversion); |
| 4475 | if (Result.isFailure()) |
| 4476 | return Result; |
| 4477 | assert(!Result.isEllipsis() && |
| 4478 | "Sub-initialization cannot result in ellipsis conversion."); |
| 4479 | |
| 4480 | // Can we even bind to a temporary? |
| 4481 | if (ToType->isRValueReferenceType() || |
| 4482 | (T1.isConstQualified() && !T1.isVolatileQualified())) { |
| 4483 | StandardConversionSequence &SCS = Result.isStandard() ? Result.Standard : |
| 4484 | Result.UserDefined.After; |
| 4485 | SCS.ReferenceBinding = true; |
| 4486 | SCS.IsLvalueReference = ToType->isLValueReferenceType(); |
| 4487 | SCS.BindsToRvalue = true; |
| 4488 | SCS.BindsToFunctionLvalue = false; |
| 4489 | SCS.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4490 | SCS.ObjCLifetimeConversionBinding = false; |
| 4491 | } else |
| 4492 | Result.setBad(BadConversionSequence::lvalue_ref_to_rvalue, |
| 4493 | From, ToType); |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4494 | return Result; |
Sebastian Redl | df88864 | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4495 | } |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4496 | |
| 4497 | // C++11 [over.ics.list]p6: |
| 4498 | // Otherwise, if the parameter type is not a class: |
| 4499 | if (!ToType->isRecordType()) { |
| 4500 | // - if the initializer list has one element, the implicit conversion |
| 4501 | // sequence is the one required to convert the element to the |
| 4502 | // parameter type. |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4503 | unsigned NumInits = From->getNumInits(); |
| 4504 | if (NumInits == 1) |
| 4505 | Result = TryCopyInitialization(S, From->getInit(0), ToType, |
| 4506 | SuppressUserConversions, |
| 4507 | InOverloadResolution, |
| 4508 | AllowObjCWritebackConversion); |
| 4509 | // - if the initializer list has no elements, the implicit conversion |
| 4510 | // sequence is the identity conversion. |
| 4511 | else if (NumInits == 0) { |
| 4512 | Result.setStandard(); |
| 4513 | Result.Standard.setAsIdentityConversion(); |
John McCall | b73bc9a | 2012-04-04 02:40:27 +0000 | [diff] [blame] | 4514 | Result.Standard.setFromType(ToType); |
| 4515 | Result.Standard.setAllToTypes(ToType); |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4516 | } |
Sebastian Redl | 12edeb0 | 2012-02-28 23:36:38 +0000 | [diff] [blame] | 4517 | Result.setListInitializationSequence(); |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4518 | return Result; |
| 4519 | } |
| 4520 | |
| 4521 | // C++11 [over.ics.list]p7: |
| 4522 | // In all cases other than those enumerated above, no conversion is possible |
| 4523 | return Result; |
| 4524 | } |
| 4525 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4526 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 4527 | /// ToType from the expression From. Return the implicit conversion |
| 4528 | /// sequence required to pass this argument, which may be a bad |
| 4529 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4530 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | e81335c | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 4531 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4532 | static ImplicitConversionSequence |
| 4533 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4534 | bool SuppressUserConversions, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4535 | bool InOverloadResolution, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4536 | bool AllowObjCWritebackConversion, |
| 4537 | bool AllowExplicit) { |
Sebastian Redl | b17be8d | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4538 | if (InitListExpr *FromInitList = dyn_cast<InitListExpr>(From)) |
| 4539 | return TryListConversion(S, FromInitList, ToType, SuppressUserConversions, |
| 4540 | InOverloadResolution,AllowObjCWritebackConversion); |
| 4541 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4542 | if (ToType->isReferenceType()) |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4543 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4544 | /*FIXME:*/From->getLocStart(), |
| 4545 | SuppressUserConversions, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4546 | AllowExplicit); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4547 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4548 | return TryImplicitConversion(S, From, ToType, |
| 4549 | SuppressUserConversions, |
| 4550 | /*AllowExplicit=*/false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4551 | InOverloadResolution, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4552 | /*CStyle=*/false, |
| 4553 | AllowObjCWritebackConversion); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4554 | } |
| 4555 | |
Anna Zaks | 1b06812 | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 4556 | static bool TryCopyInitialization(const CanQualType FromQTy, |
| 4557 | const CanQualType ToQTy, |
| 4558 | Sema &S, |
| 4559 | SourceLocation Loc, |
| 4560 | ExprValueKind FromVK) { |
| 4561 | OpaqueValueExpr TmpExpr(Loc, FromQTy, FromVK); |
| 4562 | ImplicitConversionSequence ICS = |
| 4563 | TryCopyInitialization(S, &TmpExpr, ToQTy, true, true, false); |
| 4564 | |
| 4565 | return !ICS.isBad(); |
| 4566 | } |
| 4567 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4568 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 4569 | /// parameter of the given member function (@c Method) from the |
| 4570 | /// expression @p From. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4571 | static ImplicitConversionSequence |
| 4572 | TryObjectArgumentInitialization(Sema &S, QualType OrigFromType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4573 | Expr::Classification FromClassification, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4574 | CXXMethodDecl *Method, |
| 4575 | CXXRecordDecl *ActingContext) { |
| 4576 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 931e0bd | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 4577 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 4578 | // const volatile object. |
| 4579 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 4580 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4581 | QualType ImplicitParamType = S.Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4582 | |
| 4583 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 4584 | // to exit early. |
| 4585 | ImplicitConversionSequence ICS; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4586 | |
| 4587 | // We need to have an object of class type. |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 4588 | QualType FromType = OrigFromType; |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4589 | if (const PointerType *PT = FromType->getAs<PointerType>()) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4590 | FromType = PT->getPointeeType(); |
| 4591 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4592 | // When we had a pointer, it's implicitly dereferenced, so we |
| 4593 | // better have an lvalue. |
| 4594 | assert(FromClassification.isLValue()); |
| 4595 | } |
| 4596 | |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4597 | assert(FromType->isRecordType()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4598 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4599 | // C++0x [over.match.funcs]p4: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4600 | // For non-static member functions, the type of the implicit object |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4601 | // parameter is |
| 4602 | // |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4603 | // - "lvalue reference to cv X" for functions declared without a |
| 4604 | // ref-qualifier or with the & ref-qualifier |
| 4605 | // - "rvalue reference to cv X" for functions declared with the && |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4606 | // ref-qualifier |
| 4607 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4608 | // where X is the class of which the function is a member and cv is the |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4609 | // cv-qualification on the member function declaration. |
| 4610 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4611 | // However, when finding an implicit conversion sequence for the argument, we |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4612 | // are not allowed to create temporaries or perform user-defined conversions |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4613 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 4614 | // reference binding here, that allows class rvalues to bind to |
| 4615 | // non-constant references. |
| 4616 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4617 | // First check the qualifiers. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4618 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4619 | if (ImplicitParamType.getCVRQualifiers() |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4620 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4621 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4622 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
| 4623 | OrigFromType, ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4624 | return ICS; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4625 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4626 | |
| 4627 | // Check that we have either the same type or a derived type. It |
| 4628 | // affects the conversion rank. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4629 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4630 | ImplicitConversionKind SecondKind; |
| 4631 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 4632 | SecondKind = ICK_Identity; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4633 | } else if (S.IsDerivedFrom(FromType, ClassType)) |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4634 | SecondKind = ICK_Derived_To_Base; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4635 | else { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4636 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 4637 | FromType, ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4638 | return ICS; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4639 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4640 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4641 | // Check the ref-qualifier. |
| 4642 | switch (Method->getRefQualifier()) { |
| 4643 | case RQ_None: |
| 4644 | // Do nothing; we don't care about lvalueness or rvalueness. |
| 4645 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4646 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4647 | case RQ_LValue: |
| 4648 | if (!FromClassification.isLValue() && Quals != Qualifiers::Const) { |
| 4649 | // non-const lvalue reference cannot bind to an rvalue |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4650 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, FromType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4651 | ImplicitParamType); |
| 4652 | return ICS; |
| 4653 | } |
| 4654 | break; |
| 4655 | |
| 4656 | case RQ_RValue: |
| 4657 | if (!FromClassification.isRValue()) { |
| 4658 | // rvalue reference cannot bind to an lvalue |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4659 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, FromType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4660 | ImplicitParamType); |
| 4661 | return ICS; |
| 4662 | } |
| 4663 | break; |
| 4664 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4665 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4666 | // Success. Mark this as a reference binding. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4667 | ICS.setStandard(); |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4668 | ICS.Standard.setAsIdentityConversion(); |
| 4669 | ICS.Standard.Second = SecondKind; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4670 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 4671 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4672 | ICS.Standard.ReferenceBinding = true; |
| 4673 | ICS.Standard.DirectBinding = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4674 | ICS.Standard.IsLvalueReference = Method->getRefQualifier() != RQ_RValue; |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4675 | ICS.Standard.BindsToFunctionLvalue = false; |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4676 | ICS.Standard.BindsToRvalue = FromClassification.isRValue(); |
| 4677 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier |
| 4678 | = (Method->getRefQualifier() == RQ_None); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4679 | return ICS; |
| 4680 | } |
| 4681 | |
| 4682 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 4683 | /// the implicit object parameter for the given Method with the given |
| 4684 | /// expression. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4685 | ExprResult |
| 4686 | Sema::PerformObjectArgumentInitialization(Expr *From, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4687 | NestedNameSpecifier *Qualifier, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4688 | NamedDecl *FoundDecl, |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4689 | CXXMethodDecl *Method) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4690 | QualType FromRecordType, DestType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4691 | QualType ImplicitParamRecordType = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4692 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4693 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4694 | Expr::Classification FromClassification; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4695 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4696 | FromRecordType = PT->getPointeeType(); |
| 4697 | DestType = Method->getThisType(Context); |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4698 | FromClassification = Expr::Classification::makeSimpleLValue(); |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4699 | } else { |
| 4700 | FromRecordType = From->getType(); |
| 4701 | DestType = ImplicitParamRecordType; |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4702 | FromClassification = From->Classify(Context); |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4703 | } |
| 4704 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4705 | // Note that we always use the true parent context when performing |
| 4706 | // the actual argument initialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4707 | ImplicitConversionSequence ICS |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4708 | = TryObjectArgumentInitialization(*this, From->getType(), FromClassification, |
| 4709 | Method, Method->getParent()); |
Argyrios Kyrtzidis | 9813d32 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4710 | if (ICS.isBad()) { |
| 4711 | if (ICS.Bad.Kind == BadConversionSequence::bad_qualifiers) { |
| 4712 | Qualifiers FromQs = FromRecordType.getQualifiers(); |
| 4713 | Qualifiers ToQs = DestType.getQualifiers(); |
| 4714 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 4715 | if (CVR) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4716 | Diag(From->getLocStart(), |
Argyrios Kyrtzidis | 9813d32 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4717 | diag::err_member_function_call_bad_cvr) |
| 4718 | << Method->getDeclName() << FromRecordType << (CVR - 1) |
| 4719 | << From->getSourceRange(); |
| 4720 | Diag(Method->getLocation(), diag::note_previous_decl) |
| 4721 | << Method->getDeclName(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4722 | return ExprError(); |
Argyrios Kyrtzidis | 9813d32 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4723 | } |
| 4724 | } |
| 4725 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4726 | return Diag(From->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4727 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4728 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Argyrios Kyrtzidis | 9813d32 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4729 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4730 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4731 | if (ICS.Standard.Second == ICK_Derived_To_Base) { |
| 4732 | ExprResult FromRes = |
| 4733 | PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
| 4734 | if (FromRes.isInvalid()) |
| 4735 | return ExprError(); |
| 4736 | From = FromRes.take(); |
| 4737 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4738 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4739 | if (!Context.hasSameType(From->getType(), DestType)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4740 | From = ImpCastExprToType(From, DestType, CK_NoOp, |
Richard Smith | 4a905b6 | 2011-11-10 23:32:36 +0000 | [diff] [blame] | 4741 | From->getValueKind()).take(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4742 | return Owned(From); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4743 | } |
| 4744 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4745 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 4746 | /// expression From to bool (C++0x [conv]p3). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4747 | static ImplicitConversionSequence |
| 4748 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
Douglas Gregor | 0bbe94d | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 4749 | // FIXME: This is pretty broken. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4750 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
Anders Carlsson | ef4c721 | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 4751 | // FIXME: Are these flags correct? |
| 4752 | /*SuppressUserConversions=*/false, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4753 | /*AllowExplicit=*/true, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4754 | /*InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4755 | /*CStyle=*/false, |
| 4756 | /*AllowObjCWritebackConversion=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4757 | } |
| 4758 | |
| 4759 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 4760 | /// of the expression From to bool (C++0x [conv]p3). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4761 | ExprResult Sema::PerformContextuallyConvertToBool(Expr *From) { |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4762 | if (checkPlaceholderForOverload(*this, From)) |
| 4763 | return ExprError(); |
| 4764 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4765 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4766 | if (!ICS.isBad()) |
| 4767 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4768 | |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 4769 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4770 | return Diag(From->getLocStart(), |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4771 | diag::err_typecheck_bool_condition) |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 4772 | << From->getType() << From->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4773 | return ExprError(); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4774 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4775 | |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4776 | /// Check that the specified conversion is permitted in a converted constant |
| 4777 | /// expression, according to C++11 [expr.const]p3. Return true if the conversion |
| 4778 | /// is acceptable. |
| 4779 | static bool CheckConvertedConstantConversions(Sema &S, |
| 4780 | StandardConversionSequence &SCS) { |
| 4781 | // Since we know that the target type is an integral or unscoped enumeration |
| 4782 | // type, most conversion kinds are impossible. All possible First and Third |
| 4783 | // conversions are fine. |
| 4784 | switch (SCS.Second) { |
| 4785 | case ICK_Identity: |
| 4786 | case ICK_Integral_Promotion: |
| 4787 | case ICK_Integral_Conversion: |
| 4788 | return true; |
| 4789 | |
| 4790 | case ICK_Boolean_Conversion: |
| 4791 | // Conversion from an integral or unscoped enumeration type to bool is |
| 4792 | // classified as ICK_Boolean_Conversion, but it's also an integral |
| 4793 | // conversion, so it's permitted in a converted constant expression. |
| 4794 | return SCS.getFromType()->isIntegralOrUnscopedEnumerationType() && |
| 4795 | SCS.getToType(2)->isBooleanType(); |
| 4796 | |
| 4797 | case ICK_Floating_Integral: |
| 4798 | case ICK_Complex_Real: |
| 4799 | return false; |
| 4800 | |
| 4801 | case ICK_Lvalue_To_Rvalue: |
| 4802 | case ICK_Array_To_Pointer: |
| 4803 | case ICK_Function_To_Pointer: |
| 4804 | case ICK_NoReturn_Adjustment: |
| 4805 | case ICK_Qualification: |
| 4806 | case ICK_Compatible_Conversion: |
| 4807 | case ICK_Vector_Conversion: |
| 4808 | case ICK_Vector_Splat: |
| 4809 | case ICK_Derived_To_Base: |
| 4810 | case ICK_Pointer_Conversion: |
| 4811 | case ICK_Pointer_Member: |
| 4812 | case ICK_Block_Pointer_Conversion: |
| 4813 | case ICK_Writeback_Conversion: |
| 4814 | case ICK_Floating_Promotion: |
| 4815 | case ICK_Complex_Promotion: |
| 4816 | case ICK_Complex_Conversion: |
| 4817 | case ICK_Floating_Conversion: |
| 4818 | case ICK_TransparentUnionConversion: |
| 4819 | llvm_unreachable("unexpected second conversion kind"); |
| 4820 | |
| 4821 | case ICK_Num_Conversion_Kinds: |
| 4822 | break; |
| 4823 | } |
| 4824 | |
| 4825 | llvm_unreachable("unknown conversion kind"); |
| 4826 | } |
| 4827 | |
| 4828 | /// CheckConvertedConstantExpression - Check that the expression From is a |
| 4829 | /// converted constant expression of type T, perform the conversion and produce |
| 4830 | /// the converted expression, per C++11 [expr.const]p3. |
| 4831 | ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, |
| 4832 | llvm::APSInt &Value, |
| 4833 | CCEKind CCE) { |
| 4834 | assert(LangOpts.CPlusPlus0x && "converted constant expression outside C++11"); |
| 4835 | assert(T->isIntegralOrEnumerationType() && "unexpected converted const type"); |
| 4836 | |
| 4837 | if (checkPlaceholderForOverload(*this, From)) |
| 4838 | return ExprError(); |
| 4839 | |
| 4840 | // C++11 [expr.const]p3 with proposed wording fixes: |
| 4841 | // A converted constant expression of type T is a core constant expression, |
| 4842 | // implicitly converted to a prvalue of type T, where the converted |
| 4843 | // expression is a literal constant expression and the implicit conversion |
| 4844 | // sequence contains only user-defined conversions, lvalue-to-rvalue |
| 4845 | // conversions, integral promotions, and integral conversions other than |
| 4846 | // narrowing conversions. |
| 4847 | ImplicitConversionSequence ICS = |
| 4848 | TryImplicitConversion(From, T, |
| 4849 | /*SuppressUserConversions=*/false, |
| 4850 | /*AllowExplicit=*/false, |
| 4851 | /*InOverloadResolution=*/false, |
| 4852 | /*CStyle=*/false, |
| 4853 | /*AllowObjcWritebackConversion=*/false); |
| 4854 | StandardConversionSequence *SCS = 0; |
| 4855 | switch (ICS.getKind()) { |
| 4856 | case ImplicitConversionSequence::StandardConversion: |
| 4857 | if (!CheckConvertedConstantConversions(*this, ICS.Standard)) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4858 | return Diag(From->getLocStart(), |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4859 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 4860 | << From->getType() << From->getSourceRange() << T; |
| 4861 | SCS = &ICS.Standard; |
| 4862 | break; |
| 4863 | case ImplicitConversionSequence::UserDefinedConversion: |
| 4864 | // We are converting from class type to an integral or enumeration type, so |
| 4865 | // the Before sequence must be trivial. |
| 4866 | if (!CheckConvertedConstantConversions(*this, ICS.UserDefined.After)) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4867 | return Diag(From->getLocStart(), |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4868 | diag::err_typecheck_converted_constant_expression_disallowed) |
| 4869 | << From->getType() << From->getSourceRange() << T; |
| 4870 | SCS = &ICS.UserDefined.After; |
| 4871 | break; |
| 4872 | case ImplicitConversionSequence::AmbiguousConversion: |
| 4873 | case ImplicitConversionSequence::BadConversion: |
| 4874 | if (!DiagnoseMultipleUserDefinedConversion(From, T)) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4875 | return Diag(From->getLocStart(), |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4876 | diag::err_typecheck_converted_constant_expression) |
| 4877 | << From->getType() << From->getSourceRange() << T; |
| 4878 | return ExprError(); |
| 4879 | |
| 4880 | case ImplicitConversionSequence::EllipsisConversion: |
| 4881 | llvm_unreachable("ellipsis conversion in converted constant expression"); |
| 4882 | } |
| 4883 | |
| 4884 | ExprResult Result = PerformImplicitConversion(From, T, ICS, AA_Converting); |
| 4885 | if (Result.isInvalid()) |
| 4886 | return Result; |
| 4887 | |
| 4888 | // Check for a narrowing implicit conversion. |
| 4889 | APValue PreNarrowingValue; |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4890 | QualType PreNarrowingType; |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4891 | switch (SCS->getNarrowingKind(Context, Result.get(), PreNarrowingValue, |
| 4892 | PreNarrowingType)) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4893 | case NK_Variable_Narrowing: |
| 4894 | // Implicit conversion to a narrower type, and the value is not a constant |
| 4895 | // expression. We'll diagnose this in a moment. |
| 4896 | case NK_Not_Narrowing: |
| 4897 | break; |
| 4898 | |
| 4899 | case NK_Constant_Narrowing: |
Eli Friedman | 2b22a6e | 2012-03-29 23:39:39 +0000 | [diff] [blame] | 4900 | Diag(From->getLocStart(), |
| 4901 | isSFINAEContext() ? diag::err_cce_narrowing_sfinae : |
| 4902 | diag::err_cce_narrowing) |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4903 | << CCE << /*Constant*/1 |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 4904 | << PreNarrowingValue.getAsString(Context, PreNarrowingType) << T; |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4905 | break; |
| 4906 | |
| 4907 | case NK_Type_Narrowing: |
Eli Friedman | 2b22a6e | 2012-03-29 23:39:39 +0000 | [diff] [blame] | 4908 | Diag(From->getLocStart(), |
| 4909 | isSFINAEContext() ? diag::err_cce_narrowing_sfinae : |
| 4910 | diag::err_cce_narrowing) |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4911 | << CCE << /*Constant*/0 << From->getType() << T; |
| 4912 | break; |
| 4913 | } |
| 4914 | |
| 4915 | // Check the expression is a constant expression. |
| 4916 | llvm::SmallVector<PartialDiagnosticAt, 8> Notes; |
| 4917 | Expr::EvalResult Eval; |
| 4918 | Eval.Diag = &Notes; |
| 4919 | |
| 4920 | if (!Result.get()->EvaluateAsRValue(Eval, Context)) { |
| 4921 | // The expression can't be folded, so we can't keep it at this position in |
| 4922 | // the AST. |
| 4923 | Result = ExprError(); |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 4924 | } else { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4925 | Value = Eval.Val.getInt(); |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 4926 | |
| 4927 | if (Notes.empty()) { |
| 4928 | // It's a constant expression. |
| 4929 | return Result; |
| 4930 | } |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4931 | } |
| 4932 | |
| 4933 | // It's not a constant expression. Produce an appropriate diagnostic. |
| 4934 | if (Notes.size() == 1 && |
| 4935 | Notes[0].second.getDiagID() == diag::note_invalid_subexpr_in_const_expr) |
| 4936 | Diag(Notes[0].first, diag::err_expr_not_cce) << CCE; |
| 4937 | else { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4938 | Diag(From->getLocStart(), diag::err_expr_not_cce) |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4939 | << CCE << From->getSourceRange(); |
| 4940 | for (unsigned I = 0; I < Notes.size(); ++I) |
| 4941 | Diag(Notes[I].first, Notes[I].second); |
| 4942 | } |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 4943 | return Result; |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4944 | } |
| 4945 | |
John McCall | fec112d | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 4946 | /// dropPointerConversions - If the given standard conversion sequence |
| 4947 | /// involves any pointer conversions, remove them. This may change |
| 4948 | /// the result type of the conversion sequence. |
| 4949 | static void dropPointerConversion(StandardConversionSequence &SCS) { |
| 4950 | if (SCS.Second == ICK_Pointer_Conversion) { |
| 4951 | SCS.Second = ICK_Identity; |
| 4952 | SCS.Third = ICK_Identity; |
| 4953 | SCS.ToTypePtrs[2] = SCS.ToTypePtrs[1] = SCS.ToTypePtrs[0]; |
| 4954 | } |
Fariborz Jahanian | cac49a8 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 4955 | } |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4956 | |
John McCall | fec112d | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 4957 | /// TryContextuallyConvertToObjCPointer - Attempt to contextually |
| 4958 | /// convert the expression From to an Objective-C pointer type. |
| 4959 | static ImplicitConversionSequence |
| 4960 | TryContextuallyConvertToObjCPointer(Sema &S, Expr *From) { |
| 4961 | // Do an implicit conversion to 'id'. |
| 4962 | QualType Ty = S.Context.getObjCIdType(); |
| 4963 | ImplicitConversionSequence ICS |
| 4964 | = TryImplicitConversion(S, From, Ty, |
| 4965 | // FIXME: Are these flags correct? |
| 4966 | /*SuppressUserConversions=*/false, |
| 4967 | /*AllowExplicit=*/true, |
| 4968 | /*InOverloadResolution=*/false, |
| 4969 | /*CStyle=*/false, |
| 4970 | /*AllowObjCWritebackConversion=*/false); |
| 4971 | |
| 4972 | // Strip off any final conversions to 'id'. |
| 4973 | switch (ICS.getKind()) { |
| 4974 | case ImplicitConversionSequence::BadConversion: |
| 4975 | case ImplicitConversionSequence::AmbiguousConversion: |
| 4976 | case ImplicitConversionSequence::EllipsisConversion: |
| 4977 | break; |
| 4978 | |
| 4979 | case ImplicitConversionSequence::UserDefinedConversion: |
| 4980 | dropPointerConversion(ICS.UserDefined.After); |
| 4981 | break; |
| 4982 | |
| 4983 | case ImplicitConversionSequence::StandardConversion: |
| 4984 | dropPointerConversion(ICS.Standard); |
| 4985 | break; |
| 4986 | } |
| 4987 | |
| 4988 | return ICS; |
| 4989 | } |
| 4990 | |
| 4991 | /// PerformContextuallyConvertToObjCPointer - Perform a contextual |
| 4992 | /// conversion of the expression From to an Objective-C pointer type. |
| 4993 | ExprResult Sema::PerformContextuallyConvertToObjCPointer(Expr *From) { |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4994 | if (checkPlaceholderForOverload(*this, From)) |
| 4995 | return ExprError(); |
| 4996 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4997 | QualType Ty = Context.getObjCIdType(); |
John McCall | fec112d | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 4998 | ImplicitConversionSequence ICS = |
| 4999 | TryContextuallyConvertToObjCPointer(*this, From); |
Fariborz Jahanian | cac49a8 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5000 | if (!ICS.isBad()) |
| 5001 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5002 | return ExprError(); |
Fariborz Jahanian | cac49a8 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 5003 | } |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5004 | |
Richard Smith | 8dd3425 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5005 | /// Determine whether the provided type is an integral type, or an enumeration |
| 5006 | /// type of a permitted flavor. |
| 5007 | static bool isIntegralOrEnumerationType(QualType T, bool AllowScopedEnum) { |
| 5008 | return AllowScopedEnum ? T->isIntegralOrEnumerationType() |
| 5009 | : T->isIntegralOrUnscopedEnumerationType(); |
| 5010 | } |
| 5011 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5012 | /// \brief Attempt to convert the given expression to an integral or |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5013 | /// enumeration type. |
| 5014 | /// |
| 5015 | /// This routine will attempt to convert an expression of class type to an |
| 5016 | /// integral or enumeration type, if that class type only has a single |
| 5017 | /// conversion to an integral or enumeration type. |
| 5018 | /// |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5019 | /// \param Loc The source location of the construct that requires the |
| 5020 | /// conversion. |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5021 | /// |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5022 | /// \param FromE The expression we're converting from. |
| 5023 | /// |
| 5024 | /// \param NotIntDiag The diagnostic to be emitted if the expression does not |
| 5025 | /// have integral or enumeration type. |
| 5026 | /// |
| 5027 | /// \param IncompleteDiag The diagnostic to be emitted if the expression has |
| 5028 | /// incomplete class type. |
| 5029 | /// |
| 5030 | /// \param ExplicitConvDiag The diagnostic to be emitted if we're calling an |
| 5031 | /// explicit conversion function (because no implicit conversion functions |
| 5032 | /// were available). This is a recovery mode. |
| 5033 | /// |
| 5034 | /// \param ExplicitConvNote The note to be emitted with \p ExplicitConvDiag, |
| 5035 | /// showing which conversion was picked. |
| 5036 | /// |
| 5037 | /// \param AmbigDiag The diagnostic to be emitted if there is more than one |
| 5038 | /// conversion function that could convert to integral or enumeration type. |
| 5039 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5040 | /// \param AmbigNote The note to be emitted with \p AmbigDiag for each |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5041 | /// usable conversion function. |
| 5042 | /// |
| 5043 | /// \param ConvDiag The diagnostic to be emitted if we are calling a conversion |
| 5044 | /// function, which may be an extension in this case. |
| 5045 | /// |
Richard Smith | 8dd3425 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5046 | /// \param AllowScopedEnumerations Specifies whether conversions to scoped |
| 5047 | /// enumerations should be considered. |
| 5048 | /// |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5049 | /// \returns The expression, converted to an integral or enumeration type if |
| 5050 | /// successful. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5051 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5052 | Sema::ConvertToIntegralOrEnumerationType(SourceLocation Loc, Expr *From, |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5053 | ICEConvertDiagnoser &Diagnoser, |
Richard Smith | 8dd3425 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5054 | bool AllowScopedEnumerations) { |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5055 | // We can't perform any more checking for type-dependent expressions. |
| 5056 | if (From->isTypeDependent()) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5057 | return Owned(From); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5058 | |
Eli Friedman | 1da7039 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5059 | // Process placeholders immediately. |
| 5060 | if (From->hasPlaceholderType()) { |
| 5061 | ExprResult result = CheckPlaceholderExpr(From); |
| 5062 | if (result.isInvalid()) return result; |
| 5063 | From = result.take(); |
| 5064 | } |
| 5065 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5066 | // If the expression already has integral or enumeration type, we're golden. |
| 5067 | QualType T = From->getType(); |
Richard Smith | 8dd3425 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5068 | if (isIntegralOrEnumerationType(T, AllowScopedEnumerations)) |
Eli Friedman | 1da7039 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5069 | return DefaultLvalueConversion(From); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5070 | |
| 5071 | // FIXME: Check for missing '()' if T is a function type? |
| 5072 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5073 | // If we don't have a class type in C++, there's no way we can get an |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5074 | // expression of integral or enumeration type. |
| 5075 | const RecordType *RecordTy = T->getAs<RecordType>(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5076 | if (!RecordTy || !getLangOpts().CPlusPlus) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5077 | if (!Diagnoser.Suppress) |
| 5078 | Diagnoser.diagnoseNotInt(*this, Loc, T) << From->getSourceRange(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5079 | return Owned(From); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5080 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5081 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5082 | // We must have a complete class type. |
Douglas Gregor | a6c5abb | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 5083 | struct TypeDiagnoserPartialDiag : TypeDiagnoser { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5084 | ICEConvertDiagnoser &Diagnoser; |
| 5085 | Expr *From; |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5086 | |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5087 | TypeDiagnoserPartialDiag(ICEConvertDiagnoser &Diagnoser, Expr *From) |
| 5088 | : TypeDiagnoser(Diagnoser.Suppress), Diagnoser(Diagnoser), From(From) {} |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5089 | |
| 5090 | virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5091 | Diagnoser.diagnoseIncomplete(S, Loc, T) << From->getSourceRange(); |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5092 | } |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5093 | } IncompleteDiagnoser(Diagnoser, From); |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5094 | |
| 5095 | if (RequireCompleteType(Loc, T, IncompleteDiagnoser)) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5096 | return Owned(From); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5097 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5098 | // Look for a conversion to an integral or enumeration type. |
| 5099 | UnresolvedSet<4> ViableConversions; |
| 5100 | UnresolvedSet<4> ExplicitConversions; |
| 5101 | const UnresolvedSetImpl *Conversions |
| 5102 | = cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5103 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5104 | bool HadMultipleCandidates = (Conversions->size() > 1); |
| 5105 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5106 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5107 | E = Conversions->end(); |
| 5108 | I != E; |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5109 | ++I) { |
| 5110 | if (CXXConversionDecl *Conversion |
Richard Smith | 8dd3425 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5111 | = dyn_cast<CXXConversionDecl>((*I)->getUnderlyingDecl())) { |
| 5112 | if (isIntegralOrEnumerationType( |
| 5113 | Conversion->getConversionType().getNonReferenceType(), |
| 5114 | AllowScopedEnumerations)) { |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5115 | if (Conversion->isExplicit()) |
| 5116 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
| 5117 | else |
| 5118 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
| 5119 | } |
Richard Smith | 8dd3425 | 2012-02-04 07:07:42 +0000 | [diff] [blame] | 5120 | } |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5121 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5122 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5123 | switch (ViableConversions.size()) { |
| 5124 | case 0: |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5125 | if (ExplicitConversions.size() == 1 && !Diagnoser.Suppress) { |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5126 | DeclAccessPair Found = ExplicitConversions[0]; |
| 5127 | CXXConversionDecl *Conversion |
| 5128 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5129 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5130 | // The user probably meant to invoke the given explicit |
| 5131 | // conversion; use it. |
| 5132 | QualType ConvTy |
| 5133 | = Conversion->getConversionType().getNonReferenceType(); |
| 5134 | std::string TypeStr; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 5135 | ConvTy.getAsStringInternal(TypeStr, getPrintingPolicy()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5136 | |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5137 | Diagnoser.diagnoseExplicitConv(*this, Loc, T, ConvTy) |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5138 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 5139 | "static_cast<" + TypeStr + ">(") |
| 5140 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(From->getLocEnd()), |
| 5141 | ")"); |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5142 | Diagnoser.noteExplicitConv(*this, Conversion, ConvTy); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5143 | |
| 5144 | // If we aren't in a SFINAE context, build a call to the |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5145 | // explicit conversion function. |
| 5146 | if (isSFINAEContext()) |
| 5147 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5148 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5149 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5150 | ExprResult Result = BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5151 | HadMultipleCandidates); |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 5152 | if (Result.isInvalid()) |
| 5153 | return ExprError(); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5154 | // Record usage of conversion in an implicit cast. |
| 5155 | From = ImplicitCastExpr::Create(Context, Result.get()->getType(), |
| 5156 | CK_UserDefinedConversion, |
| 5157 | Result.get(), 0, |
| 5158 | Result.get()->getValueKind()); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5159 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5160 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5161 | // We'll complain below about a non-integral condition type. |
| 5162 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5163 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5164 | case 1: { |
| 5165 | // Apply this conversion. |
| 5166 | DeclAccessPair Found = ViableConversions[0]; |
| 5167 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5168 | |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5169 | CXXConversionDecl *Conversion |
| 5170 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 5171 | QualType ConvTy |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5172 | = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5173 | if (!Diagnoser.SuppressConversion) { |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5174 | if (isSFINAEContext()) |
| 5175 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5176 | |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5177 | Diagnoser.diagnoseConversion(*this, Loc, T, ConvTy) |
| 5178 | << From->getSourceRange(); |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 5179 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5180 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5181 | ExprResult Result = BuildCXXMemberCallExpr(From, Found, Conversion, |
| 5182 | HadMultipleCandidates); |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 5183 | if (Result.isInvalid()) |
| 5184 | return ExprError(); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5185 | // Record usage of conversion in an implicit cast. |
| 5186 | From = ImplicitCastExpr::Create(Context, Result.get()->getType(), |
| 5187 | CK_UserDefinedConversion, |
| 5188 | Result.get(), 0, |
| 5189 | Result.get()->getValueKind()); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5190 | break; |
| 5191 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5192 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5193 | default: |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5194 | if (Diagnoser.Suppress) |
| 5195 | return ExprError(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5196 | |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5197 | Diagnoser.diagnoseAmbiguous(*this, Loc, T) << From->getSourceRange(); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5198 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 5199 | CXXConversionDecl *Conv |
| 5200 | = cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 5201 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5202 | Diagnoser.noteAmbiguous(*this, Conv, ConvTy); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5203 | } |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5204 | return Owned(From); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5205 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5206 | |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5207 | if (!isIntegralOrEnumerationType(From->getType(), AllowScopedEnumerations) && |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5208 | !Diagnoser.Suppress) { |
| 5209 | Diagnoser.diagnoseNotInt(*this, Loc, From->getType()) |
| 5210 | << From->getSourceRange(); |
| 5211 | } |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5212 | |
Eli Friedman | 1da7039 | 2012-01-26 00:26:18 +0000 | [diff] [blame] | 5213 | return DefaultLvalueConversion(From); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 5214 | } |
| 5215 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5216 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 5217 | /// candidate functions, using the given function call arguments. If |
| 5218 | /// @p SuppressUserConversions, then don't allow user-defined |
| 5219 | /// conversions via constructors or conversion operators. |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5220 | /// |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 5221 | /// \param PartialOverloading true if we are performing "partial" overloading |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5222 | /// based on an incomplete set of function arguments. This feature is used by |
| 5223 | /// code completion. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5224 | void |
| 5225 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5226 | DeclAccessPair FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5227 | llvm::ArrayRef<Expr *> Args, |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 5228 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 5229 | bool SuppressUserConversions, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5230 | bool PartialOverloading, |
| 5231 | bool AllowExplicit) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5232 | const FunctionProtoType* Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5233 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5234 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5235 | assert(!Function->getDescribedFunctionTemplate() && |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5236 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5237 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5238 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5239 | if (!isa<CXXConstructorDecl>(Method)) { |
| 5240 | // If we get here, it's because we're calling a member function |
| 5241 | // that is named without a member access expression (e.g., |
| 5242 | // "this->f") that was either written explicitly or created |
| 5243 | // implicitly. This can happen with a qualified call to a member |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5244 | // function, e.g., X::f(). We use an empty type for the implied |
| 5245 | // object argument (C++ [over.call.func]p3), and the acting context |
| 5246 | // is irrelevant. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5247 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5248 | QualType(), Expr::Classification::makeSimpleLValue(), |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5249 | Args, CandidateSet, SuppressUserConversions); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5250 | return; |
| 5251 | } |
| 5252 | // We treat a constructor like a non-member function, since its object |
| 5253 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5254 | } |
| 5255 | |
Douglas Gregor | ff7028a | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 5256 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5257 | return; |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5258 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5259 | // Overload resolution is always an unevaluated context. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5260 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5261 | |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5262 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 5263 | // C++ [class.copy]p3: |
| 5264 | // A member function template is never instantiated to perform the copy |
| 5265 | // of a class object to an object of its class type. |
| 5266 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5267 | if (Args.size() == 1 && |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 5268 | Constructor->isSpecializationCopyingObject() && |
Douglas Gregor | 901e717 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 5269 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 5270 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5271 | return; |
| 5272 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5273 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5274 | // Add this candidate |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5275 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size()); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5276 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5277 | Candidate.Function = Function; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5278 | Candidate.Viable = true; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5279 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5280 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5281 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5282 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5283 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5284 | |
| 5285 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5286 | // parameters is viable only if it has an ellipsis in its parameter |
| 5287 | // list (8.3.5). |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5288 | if ((Args.size() + (PartialOverloading && Args.size())) > NumArgsInProto && |
Douglas Gregor | 2a92001 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 5289 | !Proto->isVariadic()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5290 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5291 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5292 | return; |
| 5293 | } |
| 5294 | |
| 5295 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5296 | // is viable only if the (m+1)st parameter has a default argument |
| 5297 | // (8.3.6). For the purposes of overload resolution, the |
| 5298 | // parameter list is truncated on the right, so that there are |
| 5299 | // exactly m parameters. |
| 5300 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5301 | if (Args.size() < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5302 | // Not enough arguments. |
| 5303 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5304 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5305 | return; |
| 5306 | } |
| 5307 | |
Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5308 | // (CUDA B.1): Check for invalid calls between targets. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5309 | if (getLangOpts().CUDA) |
Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 5310 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
| 5311 | if (CheckCUDATarget(Caller, Function)) { |
| 5312 | Candidate.Viable = false; |
| 5313 | Candidate.FailureKind = ovl_fail_bad_target; |
| 5314 | return; |
| 5315 | } |
| 5316 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5317 | // Determine the implicit conversion sequences for each of the |
| 5318 | // arguments. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5319 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5320 | if (ArgIdx < NumArgsInProto) { |
| 5321 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5322 | // exist for each argument an implicit conversion sequence |
| 5323 | // (13.3.3.1) that converts that argument to the corresponding |
| 5324 | // parameter of F. |
| 5325 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5326 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5327 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5328 | SuppressUserConversions, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5329 | /*InOverloadResolution=*/true, |
| 5330 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5331 | getLangOpts().ObjCAutoRefCount, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5332 | AllowExplicit); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5333 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 5334 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5335 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5336 | break; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5337 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5338 | } else { |
| 5339 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5340 | // argument for which there is no corresponding parameter is |
| 5341 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5342 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5343 | } |
| 5344 | } |
| 5345 | } |
| 5346 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5347 | /// \brief Add all of the function declarations in the given function set to |
| 5348 | /// the overload canddiate set. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5349 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5350 | llvm::ArrayRef<Expr *> Args, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5351 | OverloadCandidateSet& CandidateSet, |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5352 | bool SuppressUserConversions, |
| 5353 | TemplateArgumentListInfo *ExplicitTemplateArgs) { |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5354 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5355 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 5356 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5357 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5358 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5359 | cast<CXXMethodDecl>(FD)->getParent(), |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5360 | Args[0]->getType(), Args[0]->Classify(Context), |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5361 | Args.slice(1), CandidateSet, |
| 5362 | SuppressUserConversions); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5363 | else |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5364 | AddOverloadCandidate(FD, F.getPair(), Args, CandidateSet, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5365 | SuppressUserConversions); |
| 5366 | } else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5367 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5368 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 5369 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5370 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5371 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5372 | ExplicitTemplateArgs, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5373 | Args[0]->getType(), |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5374 | Args[0]->Classify(Context), Args.slice(1), |
| 5375 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5376 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5377 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5378 | ExplicitTemplateArgs, Args, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5379 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5380 | } |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5381 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5382 | } |
| 5383 | |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5384 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 5385 | /// method) as a method candidate to the given overload set. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5386 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5387 | QualType ObjectType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5388 | Expr::Classification ObjectClassification, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5389 | Expr **Args, unsigned NumArgs, |
| 5390 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5391 | bool SuppressUserConversions) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5392 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5393 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5394 | |
| 5395 | if (isa<UsingShadowDecl>(Decl)) |
| 5396 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5397 | |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5398 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 5399 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 5400 | "Expected a member function template"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5401 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 5402 | /*ExplicitArgs*/ 0, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5403 | ObjectType, ObjectClassification, |
| 5404 | llvm::makeArrayRef(Args, NumArgs), CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5405 | SuppressUserConversions); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5406 | } else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5407 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5408 | ObjectType, ObjectClassification, |
| 5409 | llvm::makeArrayRef(Args, NumArgs), |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5410 | CandidateSet, SuppressUserConversions); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5411 | } |
| 5412 | } |
| 5413 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5414 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 5415 | /// of candidate functions, using the given function call arguments |
| 5416 | /// and the object argument (@c Object). For example, in a call |
| 5417 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 5418 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 5419 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5420 | /// operators. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5421 | void |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5422 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5423 | CXXRecordDecl *ActingContext, QualType ObjectType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5424 | Expr::Classification ObjectClassification, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5425 | llvm::ArrayRef<Expr *> Args, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5426 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5427 | bool SuppressUserConversions) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5428 | const FunctionProtoType* Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5429 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5430 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5431 | assert(!isa<CXXConstructorDecl>(Method) && |
| 5432 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5433 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5434 | if (!CandidateSet.isNewCandidate(Method)) |
| 5435 | return; |
| 5436 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5437 | // Overload resolution is always an unevaluated context. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5438 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5439 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5440 | // Add this candidate |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5441 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5442 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5443 | Candidate.Function = Method; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5444 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5445 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5446 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5447 | |
| 5448 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5449 | |
| 5450 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5451 | // parameters is viable only if it has an ellipsis in its parameter |
| 5452 | // list (8.3.5). |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5453 | if (Args.size() > NumArgsInProto && !Proto->isVariadic()) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5454 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5455 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5456 | return; |
| 5457 | } |
| 5458 | |
| 5459 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 5460 | // is viable only if the (m+1)st parameter has a default argument |
| 5461 | // (8.3.6). For the purposes of overload resolution, the |
| 5462 | // parameter list is truncated on the right, so that there are |
| 5463 | // exactly m parameters. |
| 5464 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5465 | if (Args.size() < MinRequiredArgs) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5466 | // Not enough arguments. |
| 5467 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5468 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5469 | return; |
| 5470 | } |
| 5471 | |
| 5472 | Candidate.Viable = true; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5473 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5474 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5475 | // The implicit object argument is ignored. |
| 5476 | Candidate.IgnoreObjectArgument = true; |
| 5477 | else { |
| 5478 | // Determine the implicit conversion sequence for the object |
| 5479 | // parameter. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5480 | Candidate.Conversions[0] |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5481 | = TryObjectArgumentInitialization(*this, ObjectType, ObjectClassification, |
| 5482 | Method, ActingContext); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5483 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5484 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5485 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5486 | return; |
| 5487 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5488 | } |
| 5489 | |
| 5490 | // Determine the implicit conversion sequences for each of the |
| 5491 | // arguments. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5492 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5493 | if (ArgIdx < NumArgsInProto) { |
| 5494 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5495 | // exist for each argument an implicit conversion sequence |
| 5496 | // (13.3.3.1) that converts that argument to the corresponding |
| 5497 | // parameter of F. |
| 5498 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5499 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5500 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5501 | SuppressUserConversions, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5502 | /*InOverloadResolution=*/true, |
| 5503 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5504 | getLangOpts().ObjCAutoRefCount); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5505 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5506 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5507 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5508 | break; |
| 5509 | } |
| 5510 | } else { |
| 5511 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5512 | // argument for which there is no corresponding parameter is |
| 5513 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5514 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5515 | } |
| 5516 | } |
| 5517 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5518 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5519 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 5520 | /// set, using template argument deduction to produce an appropriate member |
| 5521 | /// function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5522 | void |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5523 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5524 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5525 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5526 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5527 | QualType ObjectType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5528 | Expr::Classification ObjectClassification, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5529 | llvm::ArrayRef<Expr *> Args, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5530 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5531 | bool SuppressUserConversions) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5532 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 5533 | return; |
| 5534 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5535 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5536 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5537 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5538 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5539 | // candidate functions in the usual way.113) A given name can refer to one |
| 5540 | // or more function templates and also to a set of overloaded non-template |
| 5541 | // functions. In such a case, the candidate functions generated from each |
| 5542 | // function template are combined with the set of non-template candidate |
| 5543 | // functions. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5544 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5545 | FunctionDecl *Specialization = 0; |
| 5546 | if (TemplateDeductionResult Result |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5547 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, Args, |
| 5548 | Specialization, Info)) { |
Benjamin Kramer | fb761ff | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5549 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5550 | Candidate.FoundDecl = FoundDecl; |
| 5551 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 5552 | Candidate.Viable = false; |
| 5553 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 5554 | Candidate.IsSurrogate = false; |
| 5555 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5556 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5557 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5558 | Info); |
| 5559 | return; |
| 5560 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5561 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5562 | // Add the function template specialization produced by template argument |
| 5563 | // deduction as a candidate. |
| 5564 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5565 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5566 | "Specialization is not a member function?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5567 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5568 | ActingContext, ObjectType, ObjectClassification, Args, |
| 5569 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5570 | } |
| 5571 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5572 | /// \brief Add a C++ function template specialization as a candidate |
| 5573 | /// in the candidate set, using template argument deduction to produce |
| 5574 | /// an appropriate function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5575 | void |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5576 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5577 | DeclAccessPair FoundDecl, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5578 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5579 | llvm::ArrayRef<Expr *> Args, |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5580 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5581 | bool SuppressUserConversions) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5582 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 5583 | return; |
| 5584 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5585 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5586 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5587 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5588 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5589 | // candidate functions in the usual way.113) A given name can refer to one |
| 5590 | // or more function templates and also to a set of overloaded non-template |
| 5591 | // functions. In such a case, the candidate functions generated from each |
| 5592 | // function template are combined with the set of non-template candidate |
| 5593 | // functions. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5594 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5595 | FunctionDecl *Specialization = 0; |
| 5596 | if (TemplateDeductionResult Result |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5597 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, Args, |
| 5598 | Specialization, Info)) { |
Benjamin Kramer | fb761ff | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5599 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5600 | Candidate.FoundDecl = FoundDecl; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5601 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 5602 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5603 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5604 | Candidate.IsSurrogate = false; |
| 5605 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5606 | Candidate.ExplicitCallArguments = Args.size(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5607 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5608 | Info); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5609 | return; |
| 5610 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5611 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5612 | // Add the function template specialization produced by template argument |
| 5613 | // deduction as a candidate. |
| 5614 | assert(Specialization && "Missing function template specialization?"); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5615 | AddOverloadCandidate(Specialization, FoundDecl, Args, CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 5616 | SuppressUserConversions); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5617 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5618 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5619 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5620 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5621 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5622 | /// and ToType is the type that we're eventually trying to convert to |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5623 | /// (which may or may not be the same type as the type that the |
| 5624 | /// conversion function produces). |
| 5625 | void |
| 5626 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5627 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5628 | CXXRecordDecl *ActingContext, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5629 | Expr *From, QualType ToType, |
| 5630 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5631 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 5632 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5633 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5634 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 5635 | return; |
| 5636 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5637 | // Overload resolution is always an unevaluated context. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5638 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5639 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5640 | // Add this candidate |
Benjamin Kramer | fb761ff | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5641 | OverloadCandidate &Candidate = CandidateSet.addCandidate(1); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5642 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5643 | Candidate.Function = Conversion; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5644 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5645 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5646 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5647 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 5648 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5649 | Candidate.Viable = true; |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5650 | Candidate.ExplicitCallArguments = 1; |
Douglas Gregor | c9ed468 | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5651 | |
Douglas Gregor | 6affc78 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 5652 | // C++ [over.match.funcs]p4: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5653 | // For conversion functions, the function is considered to be a member of |
| 5654 | // the class of the implicit implied object argument for the purpose of |
Douglas Gregor | 6affc78 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 5655 | // defining the type of the implicit object parameter. |
Douglas Gregor | c9ed468 | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5656 | // |
| 5657 | // Determine the implicit conversion sequence for the implicit |
| 5658 | // object parameter. |
| 5659 | QualType ImplicitParamType = From->getType(); |
| 5660 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
| 5661 | ImplicitParamType = FromPtrType->getPointeeType(); |
| 5662 | CXXRecordDecl *ConversionContext |
| 5663 | = cast<CXXRecordDecl>(ImplicitParamType->getAs<RecordType>()->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5664 | |
Douglas Gregor | c9ed468 | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5665 | Candidate.Conversions[0] |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5666 | = TryObjectArgumentInitialization(*this, From->getType(), |
| 5667 | From->Classify(Context), |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5668 | Conversion, ConversionContext); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5669 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5670 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5671 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5672 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5673 | return; |
| 5674 | } |
Douglas Gregor | c9ed468 | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5675 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5676 | // We won't go through a user-define type conversion function to convert a |
Fariborz Jahanian | 996a6aa | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 5677 | // derived to base as such conversions are given Conversion Rank. They only |
| 5678 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 5679 | QualType FromCanon |
| 5680 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 5681 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 5682 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 5683 | Candidate.Viable = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5684 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 996a6aa | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 5685 | return; |
| 5686 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5687 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5688 | // To determine what the conversion from the result of calling the |
| 5689 | // conversion function to the type we're eventually trying to |
| 5690 | // convert to (ToType), we need to synthesize a call to the |
| 5691 | // conversion function and attempt copy initialization from it. This |
| 5692 | // makes sure that we get the right semantics with respect to |
| 5693 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 5694 | // call on the stack and we don't need its arguments to be |
| 5695 | // well-formed. |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 5696 | DeclRefExpr ConversionRef(Conversion, false, Conversion->getType(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5697 | VK_LValue, From->getLocStart()); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5698 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 5699 | Context.getPointerType(Conversion->getType()), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5700 | CK_FunctionToPointerDecay, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5701 | &ConversionRef, VK_RValue); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5702 | |
Richard Smith | 48d2464 | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5703 | QualType ConversionType = Conversion->getConversionType(); |
| 5704 | if (RequireCompleteType(From->getLocStart(), ConversionType, 0)) { |
Douglas Gregor | 72ebdab | 2010-11-13 19:36:57 +0000 | [diff] [blame] | 5705 | Candidate.Viable = false; |
| 5706 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 5707 | return; |
| 5708 | } |
| 5709 | |
Richard Smith | 48d2464 | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5710 | ExprValueKind VK = Expr::getValueKindForType(ConversionType); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5711 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5712 | // Note that it is safe to allocate CallExpr on the stack here because |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 5713 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 5714 | // allocator). |
Richard Smith | 48d2464 | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5715 | QualType CallResultType = ConversionType.getNonLValueExprType(Context); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5716 | CallExpr Call(Context, &ConversionFn, 0, 0, CallResultType, VK, |
Douglas Gregor | e8f08012 | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 5717 | From->getLocStart()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5718 | ImplicitConversionSequence ICS = |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5719 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 5720 | /*SuppressUserConversions=*/true, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5721 | /*InOverloadResolution=*/false, |
| 5722 | /*AllowObjCWritebackConversion=*/false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5723 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5724 | switch (ICS.getKind()) { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5725 | case ImplicitConversionSequence::StandardConversion: |
| 5726 | Candidate.FinalConversion = ICS.Standard; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5727 | |
Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5728 | // C++ [over.ics.user]p3: |
| 5729 | // If the user-defined conversion is specified by a specialization of a |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5730 | // conversion function template, the second standard conversion sequence |
Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5731 | // shall have exact match rank. |
| 5732 | if (Conversion->getPrimaryTemplate() && |
| 5733 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 5734 | Candidate.Viable = false; |
| 5735 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 5736 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5737 | |
Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 5738 | // C++0x [dcl.init.ref]p5: |
| 5739 | // In the second case, if the reference is an rvalue reference and |
| 5740 | // the second standard conversion sequence of the user-defined |
| 5741 | // conversion sequence includes an lvalue-to-rvalue conversion, the |
| 5742 | // program is ill-formed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5743 | if (ToType->isRValueReferenceType() && |
Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 5744 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 5745 | Candidate.Viable = false; |
| 5746 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 5747 | } |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5748 | break; |
| 5749 | |
| 5750 | case ImplicitConversionSequence::BadConversion: |
| 5751 | Candidate.Viable = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5752 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5753 | break; |
| 5754 | |
| 5755 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5756 | llvm_unreachable( |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5757 | "Can only end up with a standard conversion sequence or failure"); |
| 5758 | } |
| 5759 | } |
| 5760 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5761 | /// \brief Adds a conversion function template specialization |
| 5762 | /// candidate to the overload set, using template argument deduction |
| 5763 | /// to deduce the template arguments of the conversion function |
| 5764 | /// template from the type that we are converting to (C++ |
| 5765 | /// [temp.deduct.conv]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5766 | void |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5767 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5768 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5769 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5770 | Expr *From, QualType ToType, |
| 5771 | OverloadCandidateSet &CandidateSet) { |
| 5772 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 5773 | "Only conversion function templates permitted here"); |
| 5774 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5775 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 5776 | return; |
| 5777 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5778 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5779 | CXXConversionDecl *Specialization = 0; |
| 5780 | if (TemplateDeductionResult Result |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5781 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5782 | Specialization, Info)) { |
Benjamin Kramer | fb761ff | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5783 | OverloadCandidate &Candidate = CandidateSet.addCandidate(); |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5784 | Candidate.FoundDecl = FoundDecl; |
| 5785 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 5786 | Candidate.Viable = false; |
| 5787 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 5788 | Candidate.IsSurrogate = false; |
| 5789 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5790 | Candidate.ExplicitCallArguments = 1; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5791 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5792 | Info); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5793 | return; |
| 5794 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5795 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5796 | // Add the conversion function template specialization produced by |
| 5797 | // template argument deduction as a candidate. |
| 5798 | assert(Specialization && "Missing function template specialization?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5799 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5800 | CandidateSet); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5801 | } |
| 5802 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5803 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 5804 | /// converts the given @c Object to a function pointer via the |
| 5805 | /// conversion function @c Conversion, and then attempts to call it |
| 5806 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 5807 | /// the type of function that we'll eventually be calling. |
| 5808 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5809 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5810 | CXXRecordDecl *ActingContext, |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5811 | const FunctionProtoType *Proto, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5812 | Expr *Object, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5813 | llvm::ArrayRef<Expr *> Args, |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5814 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5815 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 5816 | return; |
| 5817 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5818 | // Overload resolution is always an unevaluated context. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5819 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5820 | |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5821 | OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size() + 1); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5822 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5823 | Candidate.Function = 0; |
| 5824 | Candidate.Surrogate = Conversion; |
| 5825 | Candidate.Viable = true; |
| 5826 | Candidate.IsSurrogate = true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5827 | Candidate.IgnoreObjectArgument = false; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5828 | Candidate.ExplicitCallArguments = Args.size(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5829 | |
| 5830 | // Determine the implicit conversion sequence for the implicit |
| 5831 | // object parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5832 | ImplicitConversionSequence ObjectInit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5833 | = TryObjectArgumentInitialization(*this, Object->getType(), |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5834 | Object->Classify(Context), |
| 5835 | Conversion, ActingContext); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5836 | if (ObjectInit.isBad()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5837 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5838 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5839 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5840 | return; |
| 5841 | } |
| 5842 | |
| 5843 | // The first conversion is actually a user-defined conversion whose |
| 5844 | // first conversion is ObjectInit's standard conversion (which is |
| 5845 | // effectively a reference binding). Record it as such. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5846 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5847 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 5848 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5849 | Candidate.Conversions[0].UserDefined.HadMultipleCandidates = false; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5850 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
John McCall | 3090903 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 5851 | Candidate.Conversions[0].UserDefined.FoundConversionFunction = FoundDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5852 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5853 | = Candidate.Conversions[0].UserDefined.Before; |
| 5854 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 5855 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5856 | // Find the |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5857 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5858 | |
| 5859 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5860 | // parameters is viable only if it has an ellipsis in its parameter |
| 5861 | // list (8.3.5). |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5862 | if (Args.size() > NumArgsInProto && !Proto->isVariadic()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5863 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5864 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5865 | return; |
| 5866 | } |
| 5867 | |
| 5868 | // Function types don't have any default arguments, so just check if |
| 5869 | // we have enough arguments. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5870 | if (Args.size() < NumArgsInProto) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5871 | // Not enough arguments. |
| 5872 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5873 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5874 | return; |
| 5875 | } |
| 5876 | |
| 5877 | // Determine the implicit conversion sequences for each of the |
| 5878 | // arguments. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5879 | for (unsigned ArgIdx = 0; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5880 | if (ArgIdx < NumArgsInProto) { |
| 5881 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5882 | // exist for each argument an implicit conversion sequence |
| 5883 | // (13.3.3.1) that converts that argument to the corresponding |
| 5884 | // parameter of F. |
| 5885 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5886 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5887 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 5888 | /*SuppressUserConversions=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5889 | /*InOverloadResolution=*/false, |
| 5890 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5891 | getLangOpts().ObjCAutoRefCount); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5892 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5893 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5894 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5895 | break; |
| 5896 | } |
| 5897 | } else { |
| 5898 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5899 | // argument for which there is no corresponding parameter is |
| 5900 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5901 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5902 | } |
| 5903 | } |
| 5904 | } |
| 5905 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5906 | /// \brief Add overload candidates for overloaded operators that are |
| 5907 | /// member functions. |
| 5908 | /// |
| 5909 | /// Add the overloaded operator candidates that are member functions |
| 5910 | /// for the operator Op that was used in an operator expression such |
| 5911 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 5912 | /// CandidateSet will store the added overload candidates. (C++ |
| 5913 | /// [over.match.oper]). |
| 5914 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 5915 | SourceLocation OpLoc, |
| 5916 | Expr **Args, unsigned NumArgs, |
| 5917 | OverloadCandidateSet& CandidateSet, |
| 5918 | SourceRange OpRange) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5919 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5920 | |
| 5921 | // C++ [over.match.oper]p3: |
| 5922 | // For a unary operator @ with an operand of a type whose |
| 5923 | // cv-unqualified version is T1, and for a binary operator @ with |
| 5924 | // a left operand of a type whose cv-unqualified version is T1 and |
| 5925 | // a right operand of a type whose cv-unqualified version is T2, |
| 5926 | // three sets of candidate functions, designated member |
| 5927 | // candidates, non-member candidates and built-in candidates, are |
| 5928 | // constructed as follows: |
| 5929 | QualType T1 = Args[0]->getType(); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5930 | |
| 5931 | // -- If T1 is a class type, the set of member candidates is the |
| 5932 | // result of the qualified lookup of T1::operator@ |
| 5933 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 5934 | // empty. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5935 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 5936 | // Complete the type if it can be completed. Otherwise, we're done. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5937 | if (RequireCompleteType(OpLoc, T1, 0)) |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 5938 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5939 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5940 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 5941 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 5942 | Operators.suppressDiagnostics(); |
| 5943 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5944 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 5945 | OperEnd = Operators.end(); |
| 5946 | Oper != OperEnd; |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5947 | ++Oper) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5948 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5949 | Args[0]->Classify(Context), Args + 1, NumArgs - 1, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5950 | CandidateSet, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5951 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5952 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5953 | } |
| 5954 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5955 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 5956 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 5957 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 5958 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 5959 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5960 | /// operator. NumContextualBoolArguments is the number of arguments |
| 5961 | /// (at the beginning of the argument list) that will be contextually |
| 5962 | /// converted to bool. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5963 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5964 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 5965 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5966 | bool IsAssignmentOperator, |
| 5967 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5968 | // Overload resolution is always an unevaluated context. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5969 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5970 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5971 | // Add this candidate |
Benjamin Kramer | fb761ff | 2012-01-14 16:31:55 +0000 | [diff] [blame] | 5972 | OverloadCandidate &Candidate = CandidateSet.addCandidate(NumArgs); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5973 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5974 | Candidate.Function = 0; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 5975 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5976 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5977 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 5978 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 5979 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 5980 | |
| 5981 | // Determine the implicit conversion sequences for each of the |
| 5982 | // arguments. |
| 5983 | Candidate.Viable = true; |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5984 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5985 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 5986 | // C++ [over.match.oper]p4: |
| 5987 | // For the built-in assignment operators, conversions of the |
| 5988 | // left operand are restricted as follows: |
| 5989 | // -- no temporaries are introduced to hold the left operand, and |
| 5990 | // -- no user-defined conversions are applied to the left |
| 5991 | // operand to achieve a type match with the left-most |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5992 | // parameter of a built-in candidate. |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 5993 | // |
| 5994 | // We block these conversions by turning off user-defined |
| 5995 | // conversions, since that is the only way that initialization of |
| 5996 | // a reference to a non-class type can occur from something that |
| 5997 | // is not of the same type. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5998 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5999 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6000 | "Contextual conversion to bool requires bool type"); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6001 | Candidate.Conversions[ArgIdx] |
| 6002 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6003 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6004 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6005 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 6006 | ArgIdx == 0 && IsAssignmentOperator, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6007 | /*InOverloadResolution=*/false, |
| 6008 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6009 | getLangOpts().ObjCAutoRefCount); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6010 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6011 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6012 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6013 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 6014 | break; |
| 6015 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6016 | } |
| 6017 | } |
| 6018 | |
| 6019 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 6020 | /// candidate operator functions for built-in operators (C++ |
| 6021 | /// [over.built]). The types are separated into pointer types and |
| 6022 | /// enumeration types. |
| 6023 | class BuiltinCandidateTypeSet { |
| 6024 | /// TypeSet - A set of types. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6025 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6026 | |
| 6027 | /// PointerTypes - The set of pointer types that will be used in the |
| 6028 | /// built-in candidates. |
| 6029 | TypeSet PointerTypes; |
| 6030 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6031 | /// MemberPointerTypes - The set of member pointer types that will be |
| 6032 | /// used in the built-in candidates. |
| 6033 | TypeSet MemberPointerTypes; |
| 6034 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6035 | /// EnumerationTypes - The set of enumeration types that will be |
| 6036 | /// used in the built-in candidates. |
| 6037 | TypeSet EnumerationTypes; |
| 6038 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6039 | /// \brief The set of vector types that will be used in the built-in |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6040 | /// candidates. |
| 6041 | TypeSet VectorTypes; |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6042 | |
| 6043 | /// \brief A flag indicating non-record types are viable candidates |
| 6044 | bool HasNonRecordTypes; |
| 6045 | |
| 6046 | /// \brief A flag indicating whether either arithmetic or enumeration types |
| 6047 | /// were present in the candidate set. |
| 6048 | bool HasArithmeticOrEnumeralTypes; |
| 6049 | |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6050 | /// \brief A flag indicating whether the nullptr type was present in the |
| 6051 | /// candidate set. |
| 6052 | bool HasNullPtrType; |
| 6053 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 6054 | /// Sema - The semantic analysis instance where we are building the |
| 6055 | /// candidate type set. |
| 6056 | Sema &SemaRef; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6057 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6058 | /// Context - The AST context in which we will build the type sets. |
| 6059 | ASTContext &Context; |
| 6060 | |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6061 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6062 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6063 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6064 | |
| 6065 | public: |
| 6066 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6067 | typedef TypeSet::iterator iterator; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6068 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6069 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6070 | : HasNonRecordTypes(false), |
| 6071 | HasArithmeticOrEnumeralTypes(false), |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6072 | HasNullPtrType(false), |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6073 | SemaRef(SemaRef), |
| 6074 | Context(SemaRef.Context) { } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6075 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6076 | void AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6077 | SourceLocation Loc, |
| 6078 | bool AllowUserConversions, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6079 | bool AllowExplicitConversions, |
| 6080 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6081 | |
| 6082 | /// pointer_begin - First pointer type found; |
| 6083 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 6084 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6085 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6086 | iterator pointer_end() { return PointerTypes.end(); } |
| 6087 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6088 | /// member_pointer_begin - First member pointer type found; |
| 6089 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 6090 | |
| 6091 | /// member_pointer_end - Past the last member pointer type found; |
| 6092 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 6093 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6094 | /// enumeration_begin - First enumeration type found; |
| 6095 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 6096 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6097 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6098 | iterator enumeration_end() { return EnumerationTypes.end(); } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6099 | |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6100 | iterator vector_begin() { return VectorTypes.begin(); } |
| 6101 | iterator vector_end() { return VectorTypes.end(); } |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6102 | |
| 6103 | bool hasNonRecordTypes() { return HasNonRecordTypes; } |
| 6104 | bool hasArithmeticOrEnumeralTypes() { return HasArithmeticOrEnumeralTypes; } |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6105 | bool hasNullPtrType() const { return HasNullPtrType; } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6106 | }; |
| 6107 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6108 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6109 | /// the set of pointer types along with any more-qualified variants of |
| 6110 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6111 | /// will add "int const *", "int const volatile *", "int const |
| 6112 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6113 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6114 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6115 | /// |
| 6116 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6117 | bool |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6118 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 6119 | const Qualifiers &VisibleQuals) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6120 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6121 | // Insert this type. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6122 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6123 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6124 | |
Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6125 | QualType PointeeTy; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6126 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
Fariborz Jahanian | f2afc80 | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6127 | bool buildObjCPtr = false; |
Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6128 | if (!PointerTy) { |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6129 | const ObjCObjectPointerType *PTy = Ty->castAs<ObjCObjectPointerType>(); |
| 6130 | PointeeTy = PTy->getPointeeType(); |
| 6131 | buildObjCPtr = true; |
| 6132 | } else { |
Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6133 | PointeeTy = PointerTy->getPointeeType(); |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6134 | } |
| 6135 | |
Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6136 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6137 | // (the qualifier would sink to the element type), and for another, the |
| 6138 | // only overload situation where it matters is subscript or pointer +- int, |
| 6139 | // and those shouldn't have qualifier variants anyway. |
| 6140 | if (PointeeTy->isArrayType()) |
| 6141 | return true; |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6142 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6143 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6144 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 6145 | bool hasRestrict = VisibleQuals.hasRestrict(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6146 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6147 | // Iterate through all strict supersets of BaseCVR. |
| 6148 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6149 | if ((CVR | BaseCVR) != CVR) continue; |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6150 | // Skip over volatile if no volatile found anywhere in the types. |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6151 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6152 | |
| 6153 | // Skip over restrict if no restrict found anywhere in the types, or if |
| 6154 | // the type cannot be restrict-qualified. |
| 6155 | if ((CVR & Qualifiers::Restrict) && |
| 6156 | (!hasRestrict || |
| 6157 | (!(PointeeTy->isAnyPointerType() || PointeeTy->isReferenceType())))) |
| 6158 | continue; |
| 6159 | |
| 6160 | // Build qualified pointee type. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6161 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6162 | |
| 6163 | // Build qualified pointer type. |
| 6164 | QualType QPointerTy; |
Fariborz Jahanian | f2afc80 | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6165 | if (!buildObjCPtr) |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6166 | QPointerTy = Context.getPointerType(QPointeeTy); |
Fariborz Jahanian | f2afc80 | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 6167 | else |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6168 | QPointerTy = Context.getObjCObjectPointerType(QPointeeTy); |
| 6169 | |
| 6170 | // Insert qualified pointer type. |
| 6171 | PointerTypes.insert(QPointerTy); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6172 | } |
| 6173 | |
| 6174 | return true; |
| 6175 | } |
| 6176 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6177 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 6178 | /// to the set of pointer types along with any more-qualified variants of |
| 6179 | /// that type. For example, if @p Ty is "int const *", this routine |
| 6180 | /// will add "int const *", "int const volatile *", "int const |
| 6181 | /// restrict *", and "int const volatile restrict *" to the set of |
| 6182 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 6183 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6184 | /// |
| 6185 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6186 | bool |
| 6187 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 6188 | QualType Ty) { |
| 6189 | // Insert this type. |
| 6190 | if (!MemberPointerTypes.insert(Ty)) |
| 6191 | return false; |
| 6192 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6193 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 6194 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6195 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6196 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 6197 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 6198 | // (the qualifier would sink to the element type), and for another, the |
| 6199 | // only overload situation where it matters is subscript or pointer +- int, |
| 6200 | // and those shouldn't have qualifier variants anyway. |
| 6201 | if (PointeeTy->isArrayType()) |
| 6202 | return true; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6203 | const Type *ClassTy = PointerTy->getClass(); |
| 6204 | |
| 6205 | // Iterate through all strict supersets of the pointee type's CVR |
| 6206 | // qualifiers. |
| 6207 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 6208 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 6209 | if ((CVR | BaseCVR) != CVR) continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6210 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6211 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6212 | MemberPointerTypes.insert( |
| 6213 | Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6214 | } |
| 6215 | |
| 6216 | return true; |
| 6217 | } |
| 6218 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6219 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 6220 | /// Ty can be implicit converted to the given set of @p Types. We're |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6221 | /// primarily interested in pointer types and enumeration types. We also |
| 6222 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6223 | /// AllowUserConversions is true if we should look at the conversion |
| 6224 | /// functions of a class type, and AllowExplicitConversions if we |
| 6225 | /// should also include the explicit conversion functions of a class |
| 6226 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6227 | void |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6228 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6229 | SourceLocation Loc, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6230 | bool AllowUserConversions, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6231 | bool AllowExplicitConversions, |
| 6232 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6233 | // Only deal with canonical types. |
| 6234 | Ty = Context.getCanonicalType(Ty); |
| 6235 | |
| 6236 | // Look through reference types; they aren't part of the type of an |
| 6237 | // expression for the purposes of conversions. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6238 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6239 | Ty = RefTy->getPointeeType(); |
| 6240 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 6241 | // If we're dealing with an array type, decay to the pointer. |
| 6242 | if (Ty->isArrayType()) |
| 6243 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 6244 | |
| 6245 | // Otherwise, we don't care about qualifiers on the type. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 6246 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6247 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6248 | // Flag if we ever add a non-record type. |
| 6249 | const RecordType *TyRec = Ty->getAs<RecordType>(); |
| 6250 | HasNonRecordTypes = HasNonRecordTypes || !TyRec; |
| 6251 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6252 | // Flag if we encounter an arithmetic type. |
| 6253 | HasArithmeticOrEnumeralTypes = |
| 6254 | HasArithmeticOrEnumeralTypes || Ty->isArithmeticType(); |
| 6255 | |
Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 6256 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
| 6257 | PointerTypes.insert(Ty); |
| 6258 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6259 | // Insert our type, and its more-qualified variants, into the set |
| 6260 | // of types. |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6261 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6262 | return; |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 6263 | } else if (Ty->isMemberPointerType()) { |
| 6264 | // Member pointers are far easier, since the pointee can't be converted. |
| 6265 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 6266 | return; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6267 | } else if (Ty->isEnumeralType()) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6268 | HasArithmeticOrEnumeralTypes = true; |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 6269 | EnumerationTypes.insert(Ty); |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6270 | } else if (Ty->isVectorType()) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6271 | // We treat vector types as arithmetic types in many contexts as an |
| 6272 | // extension. |
| 6273 | HasArithmeticOrEnumeralTypes = true; |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6274 | VectorTypes.insert(Ty); |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6275 | } else if (Ty->isNullPtrType()) { |
| 6276 | HasNullPtrType = true; |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6277 | } else if (AllowUserConversions && TyRec) { |
| 6278 | // No conversion functions in incomplete types. |
| 6279 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) |
| 6280 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6281 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6282 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| 6283 | const UnresolvedSetImpl *Conversions |
| 6284 | = ClassDecl->getVisibleConversionFunctions(); |
| 6285 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 6286 | E = Conversions->end(); I != E; ++I) { |
| 6287 | NamedDecl *D = I.getDecl(); |
| 6288 | if (isa<UsingShadowDecl>(D)) |
| 6289 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6290 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6291 | // Skip conversion function templates; they don't tell us anything |
| 6292 | // about which builtin types we can convert to. |
| 6293 | if (isa<FunctionTemplateDecl>(D)) |
| 6294 | continue; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6295 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6296 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
| 6297 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
| 6298 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
| 6299 | VisibleQuals); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6300 | } |
| 6301 | } |
| 6302 | } |
| 6303 | } |
| 6304 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6305 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 6306 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 6307 | /// given type to the candidate set. |
| 6308 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 6309 | QualType T, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6310 | Expr **Args, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6311 | unsigned NumArgs, |
| 6312 | OverloadCandidateSet &CandidateSet) { |
| 6313 | QualType ParamTypes[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6314 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6315 | // T& operator=(T&, T) |
| 6316 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 6317 | ParamTypes[1] = T; |
| 6318 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 6319 | /*IsAssignmentOperator=*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6320 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6321 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 6322 | // volatile T& operator=(volatile T&, T) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6323 | ParamTypes[0] |
| 6324 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6325 | ParamTypes[1] = T; |
| 6326 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6327 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6328 | } |
| 6329 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6330 | |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 6331 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 6332 | /// if any, found in visible type conversion functions found in ArgExpr's type. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6333 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 6334 | Qualifiers VRQuals; |
| 6335 | const RecordType *TyRec; |
| 6336 | if (const MemberPointerType *RHSMPType = |
| 6337 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 6338 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6339 | else |
| 6340 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 6341 | if (!TyRec) { |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 6342 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6343 | VRQuals.addVolatile(); |
| 6344 | VRQuals.addRestrict(); |
| 6345 | return VRQuals; |
| 6346 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6347 | |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6348 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 6349 | if (!ClassDecl->hasDefinition()) |
| 6350 | return VRQuals; |
| 6351 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6352 | const UnresolvedSetImpl *Conversions = |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 6353 | ClassDecl->getVisibleConversionFunctions(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6354 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6355 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6356 | E = Conversions->end(); I != E; ++I) { |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 6357 | NamedDecl *D = I.getDecl(); |
| 6358 | if (isa<UsingShadowDecl>(D)) |
| 6359 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6360 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6361 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 6362 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 6363 | CanTy = ResTypeRef->getPointeeType(); |
| 6364 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 6365 | // as see them. |
| 6366 | bool done = false; |
| 6367 | while (!done) { |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6368 | if (CanTy.isRestrictQualified()) |
| 6369 | VRQuals.addRestrict(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6370 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 6371 | CanTy = ResTypePtr->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6372 | else if (const MemberPointerType *ResTypeMPtr = |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6373 | CanTy->getAs<MemberPointerType>()) |
| 6374 | CanTy = ResTypeMPtr->getPointeeType(); |
| 6375 | else |
| 6376 | done = true; |
| 6377 | if (CanTy.isVolatileQualified()) |
| 6378 | VRQuals.addVolatile(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6379 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 6380 | return VRQuals; |
| 6381 | } |
| 6382 | } |
| 6383 | } |
| 6384 | return VRQuals; |
| 6385 | } |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6386 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6387 | namespace { |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6388 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6389 | /// \brief Helper class to manage the addition of builtin operator overload |
| 6390 | /// candidates. It provides shared state and utility methods used throughout |
| 6391 | /// the process, as well as a helper method to add each group of builtin |
| 6392 | /// operator overloads from the standard to a candidate set. |
| 6393 | class BuiltinOperatorOverloadBuilder { |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6394 | // Common instance state available to all overload candidate addition methods. |
| 6395 | Sema &S; |
| 6396 | Expr **Args; |
| 6397 | unsigned NumArgs; |
| 6398 | Qualifiers VisibleTypeConversionsQuals; |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6399 | bool HasArithmeticOrEnumeralCandidateType; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6400 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes; |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6401 | OverloadCandidateSet &CandidateSet; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6402 | |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6403 | // Define some constants used to index and iterate over the arithemetic types |
| 6404 | // provided via the getArithmeticType() method below. |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6405 | // The "promoted arithmetic types" are the arithmetic |
| 6406 | // types are that preserved by promotion (C++ [over.built]p2). |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6407 | static const unsigned FirstIntegralType = 3; |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6408 | static const unsigned LastIntegralType = 20; |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6409 | static const unsigned FirstPromotedIntegralType = 3, |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6410 | LastPromotedIntegralType = 11; |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6411 | static const unsigned FirstPromotedArithmeticType = 0, |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6412 | LastPromotedArithmeticType = 11; |
| 6413 | static const unsigned NumArithmeticTypes = 20; |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6414 | |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6415 | /// \brief Get the canonical type for a given arithmetic type index. |
| 6416 | CanQualType getArithmeticType(unsigned index) { |
| 6417 | assert(index < NumArithmeticTypes); |
| 6418 | static CanQualType ASTContext::* const |
| 6419 | ArithmeticTypes[NumArithmeticTypes] = { |
| 6420 | // Start of promoted types. |
| 6421 | &ASTContext::FloatTy, |
| 6422 | &ASTContext::DoubleTy, |
| 6423 | &ASTContext::LongDoubleTy, |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 6424 | |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6425 | // Start of integral types. |
| 6426 | &ASTContext::IntTy, |
| 6427 | &ASTContext::LongTy, |
| 6428 | &ASTContext::LongLongTy, |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6429 | &ASTContext::Int128Ty, |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6430 | &ASTContext::UnsignedIntTy, |
| 6431 | &ASTContext::UnsignedLongTy, |
| 6432 | &ASTContext::UnsignedLongLongTy, |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6433 | &ASTContext::UnsignedInt128Ty, |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6434 | // End of promoted types. |
| 6435 | |
| 6436 | &ASTContext::BoolTy, |
| 6437 | &ASTContext::CharTy, |
| 6438 | &ASTContext::WCharTy, |
| 6439 | &ASTContext::Char16Ty, |
| 6440 | &ASTContext::Char32Ty, |
| 6441 | &ASTContext::SignedCharTy, |
| 6442 | &ASTContext::ShortTy, |
| 6443 | &ASTContext::UnsignedCharTy, |
| 6444 | &ASTContext::UnsignedShortTy, |
| 6445 | // End of integral types. |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6446 | // FIXME: What about complex? What about half? |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6447 | }; |
| 6448 | return S.Context.*ArithmeticTypes[index]; |
| 6449 | } |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6450 | |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6451 | /// \brief Gets the canonical type resulting from the usual arithemetic |
| 6452 | /// converions for the given arithmetic types. |
| 6453 | CanQualType getUsualArithmeticConversions(unsigned L, unsigned R) { |
| 6454 | // Accelerator table for performing the usual arithmetic conversions. |
| 6455 | // The rules are basically: |
| 6456 | // - if either is floating-point, use the wider floating-point |
| 6457 | // - if same signedness, use the higher rank |
| 6458 | // - if same size, use unsigned of the higher rank |
| 6459 | // - use the larger type |
| 6460 | // These rules, together with the axiom that higher ranks are |
| 6461 | // never smaller, are sufficient to precompute all of these results |
| 6462 | // *except* when dealing with signed types of higher rank. |
| 6463 | // (we could precompute SLL x UI for all known platforms, but it's |
| 6464 | // better not to make any assumptions). |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6465 | // We assume that int128 has a higher rank than long long on all platforms. |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6466 | enum PromotedType { |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6467 | Dep=-1, |
| 6468 | Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6469 | }; |
Nuno Lopes | 9af6b03 | 2012-04-21 14:45:25 +0000 | [diff] [blame] | 6470 | static const PromotedType ConversionsTable[LastPromotedArithmeticType] |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6471 | [LastPromotedArithmeticType] = { |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6472 | /* Flt*/ { Flt, Dbl, LDbl, Flt, Flt, Flt, Flt, Flt, Flt, Flt, Flt }, |
| 6473 | /* Dbl*/ { Dbl, Dbl, LDbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl }, |
| 6474 | /*LDbl*/ { LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl }, |
| 6475 | /* SI*/ { Flt, Dbl, LDbl, SI, SL, SLL, S128, UI, UL, ULL, U128 }, |
| 6476 | /* SL*/ { Flt, Dbl, LDbl, SL, SL, SLL, S128, Dep, UL, ULL, U128 }, |
| 6477 | /* SLL*/ { Flt, Dbl, LDbl, SLL, SLL, SLL, S128, Dep, Dep, ULL, U128 }, |
| 6478 | /*S128*/ { Flt, Dbl, LDbl, S128, S128, S128, S128, S128, S128, S128, U128 }, |
| 6479 | /* UI*/ { Flt, Dbl, LDbl, UI, Dep, Dep, S128, UI, UL, ULL, U128 }, |
| 6480 | /* UL*/ { Flt, Dbl, LDbl, UL, UL, Dep, S128, UL, UL, ULL, U128 }, |
| 6481 | /* ULL*/ { Flt, Dbl, LDbl, ULL, ULL, ULL, S128, ULL, ULL, ULL, U128 }, |
| 6482 | /*U128*/ { Flt, Dbl, LDbl, U128, U128, U128, U128, U128, U128, U128, U128 }, |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6483 | }; |
| 6484 | |
| 6485 | assert(L < LastPromotedArithmeticType); |
| 6486 | assert(R < LastPromotedArithmeticType); |
| 6487 | int Idx = ConversionsTable[L][R]; |
| 6488 | |
| 6489 | // Fast path: the table gives us a concrete answer. |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6490 | if (Idx != Dep) return getArithmeticType(Idx); |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6491 | |
| 6492 | // Slow path: we need to compare widths. |
| 6493 | // An invariant is that the signed type has higher rank. |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6494 | CanQualType LT = getArithmeticType(L), |
| 6495 | RT = getArithmeticType(R); |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6496 | unsigned LW = S.Context.getIntWidth(LT), |
| 6497 | RW = S.Context.getIntWidth(RT); |
| 6498 | |
| 6499 | // If they're different widths, use the signed type. |
| 6500 | if (LW > RW) return LT; |
| 6501 | else if (LW < RW) return RT; |
| 6502 | |
| 6503 | // Otherwise, use the unsigned type of the signed type's rank. |
| 6504 | if (L == SL || R == SL) return S.Context.UnsignedLongTy; |
| 6505 | assert(L == SLL || R == SLL); |
| 6506 | return S.Context.UnsignedLongLongTy; |
| 6507 | } |
| 6508 | |
Chandler Carruth | 5659c0c | 2010-12-12 09:22:45 +0000 | [diff] [blame] | 6509 | /// \brief Helper method to factor out the common pattern of adding overloads |
| 6510 | /// for '++' and '--' builtin operators. |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6511 | void addPlusPlusMinusMinusStyleOverloads(QualType CandidateTy, |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6512 | bool HasVolatile, |
| 6513 | bool HasRestrict) { |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6514 | QualType ParamTypes[2] = { |
| 6515 | S.Context.getLValueReferenceType(CandidateTy), |
| 6516 | S.Context.IntTy |
| 6517 | }; |
| 6518 | |
| 6519 | // Non-volatile version. |
| 6520 | if (NumArgs == 1) |
| 6521 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 6522 | else |
| 6523 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 6524 | |
| 6525 | // Use a heuristic to reduce number of builtin candidates in the set: |
| 6526 | // add volatile version only if there are conversions to a volatile type. |
| 6527 | if (HasVolatile) { |
| 6528 | ParamTypes[0] = |
| 6529 | S.Context.getLValueReferenceType( |
| 6530 | S.Context.getVolatileType(CandidateTy)); |
| 6531 | if (NumArgs == 1) |
| 6532 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 6533 | else |
| 6534 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 6535 | } |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6536 | |
| 6537 | // Add restrict version only if there are conversions to a restrict type |
| 6538 | // and our candidate type is a non-restrict-qualified pointer. |
| 6539 | if (HasRestrict && CandidateTy->isAnyPointerType() && |
| 6540 | !CandidateTy.isRestrictQualified()) { |
| 6541 | ParamTypes[0] |
| 6542 | = S.Context.getLValueReferenceType( |
| 6543 | S.Context.getCVRQualifiedType(CandidateTy, Qualifiers::Restrict)); |
| 6544 | if (NumArgs == 1) |
| 6545 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 6546 | else |
| 6547 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 6548 | |
| 6549 | if (HasVolatile) { |
| 6550 | ParamTypes[0] |
| 6551 | = S.Context.getLValueReferenceType( |
| 6552 | S.Context.getCVRQualifiedType(CandidateTy, |
| 6553 | (Qualifiers::Volatile | |
| 6554 | Qualifiers::Restrict))); |
| 6555 | if (NumArgs == 1) |
| 6556 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, |
| 6557 | CandidateSet); |
| 6558 | else |
| 6559 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 6560 | } |
| 6561 | } |
| 6562 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6563 | } |
| 6564 | |
| 6565 | public: |
| 6566 | BuiltinOperatorOverloadBuilder( |
| 6567 | Sema &S, Expr **Args, unsigned NumArgs, |
| 6568 | Qualifiers VisibleTypeConversionsQuals, |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6569 | bool HasArithmeticOrEnumeralCandidateType, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6570 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6571 | OverloadCandidateSet &CandidateSet) |
| 6572 | : S(S), Args(Args), NumArgs(NumArgs), |
| 6573 | VisibleTypeConversionsQuals(VisibleTypeConversionsQuals), |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6574 | HasArithmeticOrEnumeralCandidateType( |
| 6575 | HasArithmeticOrEnumeralCandidateType), |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6576 | CandidateTypes(CandidateTypes), |
| 6577 | CandidateSet(CandidateSet) { |
| 6578 | // Validate some of our static helper constants in debug builds. |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6579 | assert(getArithmeticType(FirstPromotedIntegralType) == S.Context.IntTy && |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6580 | "Invalid first promoted integral type"); |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6581 | assert(getArithmeticType(LastPromotedIntegralType - 1) |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6582 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6583 | "Invalid last promoted integral type"); |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6584 | assert(getArithmeticType(FirstPromotedArithmeticType) |
| 6585 | == S.Context.FloatTy && |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6586 | "Invalid first promoted arithmetic type"); |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6587 | assert(getArithmeticType(LastPromotedArithmeticType - 1) |
Richard Smith | 521ecc1 | 2012-06-10 08:00:26 +0000 | [diff] [blame] | 6588 | == S.Context.UnsignedInt128Ty && |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6589 | "Invalid last promoted arithmetic type"); |
| 6590 | } |
| 6591 | |
| 6592 | // C++ [over.built]p3: |
| 6593 | // |
| 6594 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 6595 | // is either volatile or empty, there exist candidate operator |
| 6596 | // functions of the form |
| 6597 | // |
| 6598 | // VQ T& operator++(VQ T&); |
| 6599 | // T operator++(VQ T&, int); |
| 6600 | // |
| 6601 | // C++ [over.built]p4: |
| 6602 | // |
| 6603 | // For every pair (T, VQ), where T is an arithmetic type other |
| 6604 | // than bool, and VQ is either volatile or empty, there exist |
| 6605 | // candidate operator functions of the form |
| 6606 | // |
| 6607 | // VQ T& operator--(VQ T&); |
| 6608 | // T operator--(VQ T&, int); |
| 6609 | void addPlusPlusMinusMinusArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6610 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6611 | return; |
| 6612 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6613 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 6614 | Arith < NumArithmeticTypes; ++Arith) { |
| 6615 | addPlusPlusMinusMinusStyleOverloads( |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6616 | getArithmeticType(Arith), |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6617 | VisibleTypeConversionsQuals.hasVolatile(), |
| 6618 | VisibleTypeConversionsQuals.hasRestrict()); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6619 | } |
| 6620 | } |
| 6621 | |
| 6622 | // C++ [over.built]p5: |
| 6623 | // |
| 6624 | // For every pair (T, VQ), where T is a cv-qualified or |
| 6625 | // cv-unqualified object type, and VQ is either volatile or |
| 6626 | // empty, there exist candidate operator functions of the form |
| 6627 | // |
| 6628 | // T*VQ& operator++(T*VQ&); |
| 6629 | // T*VQ& operator--(T*VQ&); |
| 6630 | // T* operator++(T*VQ&, int); |
| 6631 | // T* operator--(T*VQ&, int); |
| 6632 | void addPlusPlusMinusMinusPointerOverloads() { |
| 6633 | for (BuiltinCandidateTypeSet::iterator |
| 6634 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6635 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6636 | Ptr != PtrEnd; ++Ptr) { |
| 6637 | // Skip pointer types that aren't pointers to object types. |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6638 | if (!(*Ptr)->getPointeeType()->isObjectType()) |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6639 | continue; |
| 6640 | |
| 6641 | addPlusPlusMinusMinusStyleOverloads(*Ptr, |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 6642 | (!(*Ptr).isVolatileQualified() && |
| 6643 | VisibleTypeConversionsQuals.hasVolatile()), |
| 6644 | (!(*Ptr).isRestrictQualified() && |
| 6645 | VisibleTypeConversionsQuals.hasRestrict())); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6646 | } |
| 6647 | } |
| 6648 | |
| 6649 | // C++ [over.built]p6: |
| 6650 | // For every cv-qualified or cv-unqualified object type T, there |
| 6651 | // exist candidate operator functions of the form |
| 6652 | // |
| 6653 | // T& operator*(T*); |
| 6654 | // |
| 6655 | // C++ [over.built]p7: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6656 | // For every function type T that does not have cv-qualifiers or a |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6657 | // ref-qualifier, there exist candidate operator functions of the form |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6658 | // T& operator*(T*); |
| 6659 | void addUnaryStarPointerOverloads() { |
| 6660 | for (BuiltinCandidateTypeSet::iterator |
| 6661 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6662 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6663 | Ptr != PtrEnd; ++Ptr) { |
| 6664 | QualType ParamTy = *Ptr; |
| 6665 | QualType PointeeTy = ParamTy->getPointeeType(); |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6666 | if (!PointeeTy->isObjectType() && !PointeeTy->isFunctionType()) |
| 6667 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6668 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 6669 | if (const FunctionProtoType *Proto =PointeeTy->getAs<FunctionProtoType>()) |
| 6670 | if (Proto->getTypeQuals() || Proto->getRefQualifier()) |
| 6671 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6672 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6673 | S.AddBuiltinCandidate(S.Context.getLValueReferenceType(PointeeTy), |
| 6674 | &ParamTy, Args, 1, CandidateSet); |
| 6675 | } |
| 6676 | } |
| 6677 | |
| 6678 | // C++ [over.built]p9: |
| 6679 | // For every promoted arithmetic type T, there exist candidate |
| 6680 | // operator functions of the form |
| 6681 | // |
| 6682 | // T operator+(T); |
| 6683 | // T operator-(T); |
| 6684 | void addUnaryPlusOrMinusArithmeticOverloads() { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6685 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6686 | return; |
| 6687 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6688 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 6689 | Arith < LastPromotedArithmeticType; ++Arith) { |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6690 | QualType ArithTy = getArithmeticType(Arith); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6691 | S.AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 6692 | } |
| 6693 | |
| 6694 | // Extension: We also add these operators for vector types. |
| 6695 | for (BuiltinCandidateTypeSet::iterator |
| 6696 | Vec = CandidateTypes[0].vector_begin(), |
| 6697 | VecEnd = CandidateTypes[0].vector_end(); |
| 6698 | Vec != VecEnd; ++Vec) { |
| 6699 | QualType VecTy = *Vec; |
| 6700 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 6701 | } |
| 6702 | } |
| 6703 | |
| 6704 | // C++ [over.built]p8: |
| 6705 | // For every type T, there exist candidate operator functions of |
| 6706 | // the form |
| 6707 | // |
| 6708 | // T* operator+(T*); |
| 6709 | void addUnaryPlusPointerOverloads() { |
| 6710 | for (BuiltinCandidateTypeSet::iterator |
| 6711 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6712 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6713 | Ptr != PtrEnd; ++Ptr) { |
| 6714 | QualType ParamTy = *Ptr; |
| 6715 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 6716 | } |
| 6717 | } |
| 6718 | |
| 6719 | // C++ [over.built]p10: |
| 6720 | // For every promoted integral type T, there exist candidate |
| 6721 | // operator functions of the form |
| 6722 | // |
| 6723 | // T operator~(T); |
| 6724 | void addUnaryTildePromotedIntegralOverloads() { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6725 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6726 | return; |
| 6727 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6728 | for (unsigned Int = FirstPromotedIntegralType; |
| 6729 | Int < LastPromotedIntegralType; ++Int) { |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6730 | QualType IntTy = getArithmeticType(Int); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6731 | S.AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 6732 | } |
| 6733 | |
| 6734 | // Extension: We also add this operator for vector types. |
| 6735 | for (BuiltinCandidateTypeSet::iterator |
| 6736 | Vec = CandidateTypes[0].vector_begin(), |
| 6737 | VecEnd = CandidateTypes[0].vector_end(); |
| 6738 | Vec != VecEnd; ++Vec) { |
| 6739 | QualType VecTy = *Vec; |
| 6740 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 6741 | } |
| 6742 | } |
| 6743 | |
| 6744 | // C++ [over.match.oper]p16: |
| 6745 | // For every pointer to member type T, there exist candidate operator |
| 6746 | // functions of the form |
| 6747 | // |
| 6748 | // bool operator==(T,T); |
| 6749 | // bool operator!=(T,T); |
| 6750 | void addEqualEqualOrNotEqualMemberPointerOverloads() { |
| 6751 | /// Set of (canonical) types that we've already handled. |
| 6752 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6753 | |
| 6754 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 6755 | for (BuiltinCandidateTypeSet::iterator |
| 6756 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 6757 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 6758 | MemPtr != MemPtrEnd; |
| 6759 | ++MemPtr) { |
| 6760 | // Don't add the same builtin candidate twice. |
| 6761 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 6762 | continue; |
| 6763 | |
| 6764 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 6765 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 6766 | CandidateSet); |
| 6767 | } |
| 6768 | } |
| 6769 | } |
| 6770 | |
| 6771 | // C++ [over.built]p15: |
| 6772 | // |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6773 | // For every T, where T is an enumeration type, a pointer type, or |
| 6774 | // std::nullptr_t, there exist candidate operator functions of the form |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6775 | // |
| 6776 | // bool operator<(T, T); |
| 6777 | // bool operator>(T, T); |
| 6778 | // bool operator<=(T, T); |
| 6779 | // bool operator>=(T, T); |
| 6780 | // bool operator==(T, T); |
| 6781 | // bool operator!=(T, T); |
Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6782 | void addRelationalPointerOrEnumeralOverloads() { |
| 6783 | // C++ [over.built]p1: |
| 6784 | // If there is a user-written candidate with the same name and parameter |
| 6785 | // types as a built-in candidate operator function, the built-in operator |
| 6786 | // function is hidden and is not included in the set of candidate |
| 6787 | // functions. |
| 6788 | // |
| 6789 | // The text is actually in a note, but if we don't implement it then we end |
| 6790 | // up with ambiguities when the user provides an overloaded operator for |
| 6791 | // an enumeration type. Note that only enumeration types have this problem, |
| 6792 | // so we track which enumeration types we've seen operators for. Also, the |
| 6793 | // only other overloaded operator with enumeration argumenst, operator=, |
| 6794 | // cannot be overloaded for enumeration types, so this is the only place |
| 6795 | // where we must suppress candidates like this. |
| 6796 | llvm::DenseSet<std::pair<CanQualType, CanQualType> > |
| 6797 | UserDefinedBinaryOperators; |
| 6798 | |
| 6799 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 6800 | if (CandidateTypes[ArgIdx].enumeration_begin() != |
| 6801 | CandidateTypes[ArgIdx].enumeration_end()) { |
| 6802 | for (OverloadCandidateSet::iterator C = CandidateSet.begin(), |
| 6803 | CEnd = CandidateSet.end(); |
| 6804 | C != CEnd; ++C) { |
| 6805 | if (!C->Viable || !C->Function || C->Function->getNumParams() != 2) |
| 6806 | continue; |
| 6807 | |
| 6808 | QualType FirstParamType = |
| 6809 | C->Function->getParamDecl(0)->getType().getUnqualifiedType(); |
| 6810 | QualType SecondParamType = |
| 6811 | C->Function->getParamDecl(1)->getType().getUnqualifiedType(); |
| 6812 | |
| 6813 | // Skip if either parameter isn't of enumeral type. |
| 6814 | if (!FirstParamType->isEnumeralType() || |
| 6815 | !SecondParamType->isEnumeralType()) |
| 6816 | continue; |
| 6817 | |
| 6818 | // Add this operator to the set of known user-defined operators. |
| 6819 | UserDefinedBinaryOperators.insert( |
| 6820 | std::make_pair(S.Context.getCanonicalType(FirstParamType), |
| 6821 | S.Context.getCanonicalType(SecondParamType))); |
| 6822 | } |
| 6823 | } |
| 6824 | } |
| 6825 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6826 | /// Set of (canonical) types that we've already handled. |
| 6827 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6828 | |
| 6829 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 6830 | for (BuiltinCandidateTypeSet::iterator |
| 6831 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 6832 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 6833 | Ptr != PtrEnd; ++Ptr) { |
| 6834 | // Don't add the same builtin candidate twice. |
| 6835 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 6836 | continue; |
| 6837 | |
| 6838 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 6839 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 6840 | CandidateSet); |
| 6841 | } |
| 6842 | for (BuiltinCandidateTypeSet::iterator |
| 6843 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 6844 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 6845 | Enum != EnumEnd; ++Enum) { |
| 6846 | CanQualType CanonType = S.Context.getCanonicalType(*Enum); |
| 6847 | |
Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6848 | // Don't add the same builtin candidate twice, or if a user defined |
| 6849 | // candidate exists. |
| 6850 | if (!AddedTypes.insert(CanonType) || |
| 6851 | UserDefinedBinaryOperators.count(std::make_pair(CanonType, |
| 6852 | CanonType))) |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6853 | continue; |
| 6854 | |
| 6855 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6856 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 6857 | CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6858 | } |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6859 | |
| 6860 | if (CandidateTypes[ArgIdx].hasNullPtrType()) { |
| 6861 | CanQualType NullPtrTy = S.Context.getCanonicalType(S.Context.NullPtrTy); |
| 6862 | if (AddedTypes.insert(NullPtrTy) && |
| 6863 | !UserDefinedBinaryOperators.count(std::make_pair(NullPtrTy, |
| 6864 | NullPtrTy))) { |
| 6865 | QualType ParamTypes[2] = { NullPtrTy, NullPtrTy }; |
| 6866 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 6867 | CandidateSet); |
| 6868 | } |
| 6869 | } |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6870 | } |
| 6871 | } |
| 6872 | |
| 6873 | // C++ [over.built]p13: |
| 6874 | // |
| 6875 | // For every cv-qualified or cv-unqualified object type T |
| 6876 | // there exist candidate operator functions of the form |
| 6877 | // |
| 6878 | // T* operator+(T*, ptrdiff_t); |
| 6879 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 6880 | // T* operator-(T*, ptrdiff_t); |
| 6881 | // T* operator+(ptrdiff_t, T*); |
| 6882 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 6883 | // |
| 6884 | // C++ [over.built]p14: |
| 6885 | // |
| 6886 | // For every T, where T is a pointer to object type, there |
| 6887 | // exist candidate operator functions of the form |
| 6888 | // |
| 6889 | // ptrdiff_t operator-(T, T); |
| 6890 | void addBinaryPlusOrMinusPointerOverloads(OverloadedOperatorKind Op) { |
| 6891 | /// Set of (canonical) types that we've already handled. |
| 6892 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6893 | |
| 6894 | for (int Arg = 0; Arg < 2; ++Arg) { |
| 6895 | QualType AsymetricParamTypes[2] = { |
| 6896 | S.Context.getPointerDiffType(), |
| 6897 | S.Context.getPointerDiffType(), |
| 6898 | }; |
| 6899 | for (BuiltinCandidateTypeSet::iterator |
| 6900 | Ptr = CandidateTypes[Arg].pointer_begin(), |
| 6901 | PtrEnd = CandidateTypes[Arg].pointer_end(); |
| 6902 | Ptr != PtrEnd; ++Ptr) { |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6903 | QualType PointeeTy = (*Ptr)->getPointeeType(); |
| 6904 | if (!PointeeTy->isObjectType()) |
| 6905 | continue; |
| 6906 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6907 | AsymetricParamTypes[Arg] = *Ptr; |
| 6908 | if (Arg == 0 || Op == OO_Plus) { |
| 6909 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 6910 | // T* operator+(ptrdiff_t, T*); |
| 6911 | S.AddBuiltinCandidate(*Ptr, AsymetricParamTypes, Args, 2, |
| 6912 | CandidateSet); |
| 6913 | } |
| 6914 | if (Op == OO_Minus) { |
| 6915 | // ptrdiff_t operator-(T, T); |
| 6916 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 6917 | continue; |
| 6918 | |
| 6919 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 6920 | S.AddBuiltinCandidate(S.Context.getPointerDiffType(), ParamTypes, |
| 6921 | Args, 2, CandidateSet); |
| 6922 | } |
| 6923 | } |
| 6924 | } |
| 6925 | } |
| 6926 | |
| 6927 | // C++ [over.built]p12: |
| 6928 | // |
| 6929 | // For every pair of promoted arithmetic types L and R, there |
| 6930 | // exist candidate operator functions of the form |
| 6931 | // |
| 6932 | // LR operator*(L, R); |
| 6933 | // LR operator/(L, R); |
| 6934 | // LR operator+(L, R); |
| 6935 | // LR operator-(L, R); |
| 6936 | // bool operator<(L, R); |
| 6937 | // bool operator>(L, R); |
| 6938 | // bool operator<=(L, R); |
| 6939 | // bool operator>=(L, R); |
| 6940 | // bool operator==(L, R); |
| 6941 | // bool operator!=(L, R); |
| 6942 | // |
| 6943 | // where LR is the result of the usual arithmetic conversions |
| 6944 | // between types L and R. |
| 6945 | // |
| 6946 | // C++ [over.built]p24: |
| 6947 | // |
| 6948 | // For every pair of promoted arithmetic types L and R, there exist |
| 6949 | // candidate operator functions of the form |
| 6950 | // |
| 6951 | // LR operator?(bool, L, R); |
| 6952 | // |
| 6953 | // where LR is the result of the usual arithmetic conversions |
| 6954 | // between types L and R. |
| 6955 | // Our candidates ignore the first parameter. |
| 6956 | void addGenericBinaryArithmeticOverloads(bool isComparison) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6957 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6958 | return; |
| 6959 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6960 | for (unsigned Left = FirstPromotedArithmeticType; |
| 6961 | Left < LastPromotedArithmeticType; ++Left) { |
| 6962 | for (unsigned Right = FirstPromotedArithmeticType; |
| 6963 | Right < LastPromotedArithmeticType; ++Right) { |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6964 | QualType LandR[2] = { getArithmeticType(Left), |
| 6965 | getArithmeticType(Right) }; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6966 | QualType Result = |
| 6967 | isComparison ? S.Context.BoolTy |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6968 | : getUsualArithmeticConversions(Left, Right); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6969 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 6970 | } |
| 6971 | } |
| 6972 | |
| 6973 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 6974 | // conditional operator for vector types. |
| 6975 | for (BuiltinCandidateTypeSet::iterator |
| 6976 | Vec1 = CandidateTypes[0].vector_begin(), |
| 6977 | Vec1End = CandidateTypes[0].vector_end(); |
| 6978 | Vec1 != Vec1End; ++Vec1) { |
| 6979 | for (BuiltinCandidateTypeSet::iterator |
| 6980 | Vec2 = CandidateTypes[1].vector_begin(), |
| 6981 | Vec2End = CandidateTypes[1].vector_end(); |
| 6982 | Vec2 != Vec2End; ++Vec2) { |
| 6983 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 6984 | QualType Result = S.Context.BoolTy; |
| 6985 | if (!isComparison) { |
| 6986 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 6987 | Result = *Vec1; |
| 6988 | else |
| 6989 | Result = *Vec2; |
| 6990 | } |
| 6991 | |
| 6992 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 6993 | } |
| 6994 | } |
| 6995 | } |
| 6996 | |
| 6997 | // C++ [over.built]p17: |
| 6998 | // |
| 6999 | // For every pair of promoted integral types L and R, there |
| 7000 | // exist candidate operator functions of the form |
| 7001 | // |
| 7002 | // LR operator%(L, R); |
| 7003 | // LR operator&(L, R); |
| 7004 | // LR operator^(L, R); |
| 7005 | // LR operator|(L, R); |
| 7006 | // L operator<<(L, R); |
| 7007 | // L operator>>(L, R); |
| 7008 | // |
| 7009 | // where LR is the result of the usual arithmetic conversions |
| 7010 | // between types L and R. |
| 7011 | void addBinaryBitwiseArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7012 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7013 | return; |
| 7014 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7015 | for (unsigned Left = FirstPromotedIntegralType; |
| 7016 | Left < LastPromotedIntegralType; ++Left) { |
| 7017 | for (unsigned Right = FirstPromotedIntegralType; |
| 7018 | Right < LastPromotedIntegralType; ++Right) { |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7019 | QualType LandR[2] = { getArithmeticType(Left), |
| 7020 | getArithmeticType(Right) }; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7021 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 7022 | ? LandR[0] |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 7023 | : getUsualArithmeticConversions(Left, Right); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7024 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 7025 | } |
| 7026 | } |
| 7027 | } |
| 7028 | |
| 7029 | // C++ [over.built]p20: |
| 7030 | // |
| 7031 | // For every pair (T, VQ), where T is an enumeration or |
| 7032 | // pointer to member type and VQ is either volatile or |
| 7033 | // empty, there exist candidate operator functions of the form |
| 7034 | // |
| 7035 | // VQ T& operator=(VQ T&, T); |
| 7036 | void addAssignmentMemberPointerOrEnumeralOverloads() { |
| 7037 | /// Set of (canonical) types that we've already handled. |
| 7038 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7039 | |
| 7040 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7041 | for (BuiltinCandidateTypeSet::iterator |
| 7042 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7043 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7044 | Enum != EnumEnd; ++Enum) { |
| 7045 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 7046 | continue; |
| 7047 | |
| 7048 | AddBuiltinAssignmentOperatorCandidates(S, *Enum, Args, 2, |
| 7049 | CandidateSet); |
| 7050 | } |
| 7051 | |
| 7052 | for (BuiltinCandidateTypeSet::iterator |
| 7053 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7054 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7055 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7056 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7057 | continue; |
| 7058 | |
| 7059 | AddBuiltinAssignmentOperatorCandidates(S, *MemPtr, Args, 2, |
| 7060 | CandidateSet); |
| 7061 | } |
| 7062 | } |
| 7063 | } |
| 7064 | |
| 7065 | // C++ [over.built]p19: |
| 7066 | // |
| 7067 | // For every pair (T, VQ), where T is any type and VQ is either |
| 7068 | // volatile or empty, there exist candidate operator functions |
| 7069 | // of the form |
| 7070 | // |
| 7071 | // T*VQ& operator=(T*VQ&, T*); |
| 7072 | // |
| 7073 | // C++ [over.built]p21: |
| 7074 | // |
| 7075 | // For every pair (T, VQ), where T is a cv-qualified or |
| 7076 | // cv-unqualified object type and VQ is either volatile or |
| 7077 | // empty, there exist candidate operator functions of the form |
| 7078 | // |
| 7079 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 7080 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 7081 | void addAssignmentPointerOverloads(bool isEqualOp) { |
| 7082 | /// Set of (canonical) types that we've already handled. |
| 7083 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7084 | |
| 7085 | for (BuiltinCandidateTypeSet::iterator |
| 7086 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7087 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7088 | Ptr != PtrEnd; ++Ptr) { |
| 7089 | // If this is operator=, keep track of the builtin candidates we added. |
| 7090 | if (isEqualOp) |
| 7091 | AddedTypes.insert(S.Context.getCanonicalType(*Ptr)); |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7092 | else if (!(*Ptr)->getPointeeType()->isObjectType()) |
| 7093 | continue; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7094 | |
| 7095 | // non-volatile version |
| 7096 | QualType ParamTypes[2] = { |
| 7097 | S.Context.getLValueReferenceType(*Ptr), |
| 7098 | isEqualOp ? *Ptr : S.Context.getPointerDiffType(), |
| 7099 | }; |
| 7100 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 7101 | /*IsAssigmentOperator=*/ isEqualOp); |
| 7102 | |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7103 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7104 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7105 | if (NeedVolatile) { |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7106 | // volatile version |
| 7107 | ParamTypes[0] = |
| 7108 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
| 7109 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 7110 | /*IsAssigmentOperator=*/isEqualOp); |
| 7111 | } |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7112 | |
| 7113 | if (!(*Ptr).isRestrictQualified() && |
| 7114 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7115 | // restrict version |
| 7116 | ParamTypes[0] |
| 7117 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
| 7118 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 7119 | /*IsAssigmentOperator=*/isEqualOp); |
| 7120 | |
| 7121 | if (NeedVolatile) { |
| 7122 | // volatile restrict version |
| 7123 | ParamTypes[0] |
| 7124 | = S.Context.getLValueReferenceType( |
| 7125 | S.Context.getCVRQualifiedType(*Ptr, |
| 7126 | (Qualifiers::Volatile | |
| 7127 | Qualifiers::Restrict))); |
| 7128 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 7129 | CandidateSet, |
| 7130 | /*IsAssigmentOperator=*/isEqualOp); |
| 7131 | } |
| 7132 | } |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7133 | } |
| 7134 | |
| 7135 | if (isEqualOp) { |
| 7136 | for (BuiltinCandidateTypeSet::iterator |
| 7137 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7138 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7139 | Ptr != PtrEnd; ++Ptr) { |
| 7140 | // Make sure we don't add the same candidate twice. |
| 7141 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7142 | continue; |
| 7143 | |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7144 | QualType ParamTypes[2] = { |
| 7145 | S.Context.getLValueReferenceType(*Ptr), |
| 7146 | *Ptr, |
| 7147 | }; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7148 | |
| 7149 | // non-volatile version |
| 7150 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 7151 | /*IsAssigmentOperator=*/true); |
| 7152 | |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7153 | bool NeedVolatile = !(*Ptr).isVolatileQualified() && |
| 7154 | VisibleTypeConversionsQuals.hasVolatile(); |
| 7155 | if (NeedVolatile) { |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7156 | // volatile version |
| 7157 | ParamTypes[0] = |
| 7158 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7159 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 7160 | CandidateSet, /*IsAssigmentOperator=*/true); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7161 | } |
Douglas Gregor | 5bee258 | 2012-06-04 00:15:09 +0000 | [diff] [blame] | 7162 | |
| 7163 | if (!(*Ptr).isRestrictQualified() && |
| 7164 | VisibleTypeConversionsQuals.hasRestrict()) { |
| 7165 | // restrict version |
| 7166 | ParamTypes[0] |
| 7167 | = S.Context.getLValueReferenceType(S.Context.getRestrictType(*Ptr)); |
| 7168 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 7169 | CandidateSet, /*IsAssigmentOperator=*/true); |
| 7170 | |
| 7171 | if (NeedVolatile) { |
| 7172 | // volatile restrict version |
| 7173 | ParamTypes[0] |
| 7174 | = S.Context.getLValueReferenceType( |
| 7175 | S.Context.getCVRQualifiedType(*Ptr, |
| 7176 | (Qualifiers::Volatile | |
| 7177 | Qualifiers::Restrict))); |
| 7178 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 7179 | CandidateSet, /*IsAssigmentOperator=*/true); |
| 7180 | |
| 7181 | } |
| 7182 | } |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7183 | } |
| 7184 | } |
| 7185 | } |
| 7186 | |
| 7187 | // C++ [over.built]p18: |
| 7188 | // |
| 7189 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 7190 | // VQ is either volatile or empty, and R is a promoted |
| 7191 | // arithmetic type, there exist candidate operator functions of |
| 7192 | // the form |
| 7193 | // |
| 7194 | // VQ L& operator=(VQ L&, R); |
| 7195 | // VQ L& operator*=(VQ L&, R); |
| 7196 | // VQ L& operator/=(VQ L&, R); |
| 7197 | // VQ L& operator+=(VQ L&, R); |
| 7198 | // VQ L& operator-=(VQ L&, R); |
| 7199 | void addAssignmentArithmeticOverloads(bool isEqualOp) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7200 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7201 | return; |
| 7202 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7203 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 7204 | for (unsigned Right = FirstPromotedArithmeticType; |
| 7205 | Right < LastPromotedArithmeticType; ++Right) { |
| 7206 | QualType ParamTypes[2]; |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7207 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7208 | |
| 7209 | // Add this built-in operator as a candidate (VQ is empty). |
| 7210 | ParamTypes[0] = |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7211 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7212 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 7213 | /*IsAssigmentOperator=*/isEqualOp); |
| 7214 | |
| 7215 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7216 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7217 | ParamTypes[0] = |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7218 | S.Context.getVolatileType(getArithmeticType(Left)); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7219 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7220 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 7221 | CandidateSet, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7222 | /*IsAssigmentOperator=*/isEqualOp); |
| 7223 | } |
| 7224 | } |
| 7225 | } |
| 7226 | |
| 7227 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 7228 | for (BuiltinCandidateTypeSet::iterator |
| 7229 | Vec1 = CandidateTypes[0].vector_begin(), |
| 7230 | Vec1End = CandidateTypes[0].vector_end(); |
| 7231 | Vec1 != Vec1End; ++Vec1) { |
| 7232 | for (BuiltinCandidateTypeSet::iterator |
| 7233 | Vec2 = CandidateTypes[1].vector_begin(), |
| 7234 | Vec2End = CandidateTypes[1].vector_end(); |
| 7235 | Vec2 != Vec2End; ++Vec2) { |
| 7236 | QualType ParamTypes[2]; |
| 7237 | ParamTypes[1] = *Vec2; |
| 7238 | // Add this built-in operator as a candidate (VQ is empty). |
| 7239 | ParamTypes[0] = S.Context.getLValueReferenceType(*Vec1); |
| 7240 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 7241 | /*IsAssigmentOperator=*/isEqualOp); |
| 7242 | |
| 7243 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 7244 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7245 | ParamTypes[0] = S.Context.getVolatileType(*Vec1); |
| 7246 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7247 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 7248 | CandidateSet, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7249 | /*IsAssigmentOperator=*/isEqualOp); |
| 7250 | } |
| 7251 | } |
| 7252 | } |
| 7253 | } |
| 7254 | |
| 7255 | // C++ [over.built]p22: |
| 7256 | // |
| 7257 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 7258 | // is either volatile or empty, and R is a promoted integral |
| 7259 | // type, there exist candidate operator functions of the form |
| 7260 | // |
| 7261 | // VQ L& operator%=(VQ L&, R); |
| 7262 | // VQ L& operator<<=(VQ L&, R); |
| 7263 | // VQ L& operator>>=(VQ L&, R); |
| 7264 | // VQ L& operator&=(VQ L&, R); |
| 7265 | // VQ L& operator^=(VQ L&, R); |
| 7266 | // VQ L& operator|=(VQ L&, R); |
| 7267 | void addAssignmentIntegralOverloads() { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7268 | if (!HasArithmeticOrEnumeralCandidateType) |
| 7269 | return; |
| 7270 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7271 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 7272 | for (unsigned Right = FirstPromotedIntegralType; |
| 7273 | Right < LastPromotedIntegralType; ++Right) { |
| 7274 | QualType ParamTypes[2]; |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7275 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7276 | |
| 7277 | // Add this built-in operator as a candidate (VQ is empty). |
| 7278 | ParamTypes[0] = |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7279 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7280 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 7281 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 7282 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 7283 | ParamTypes[0] = getArithmeticType(Left); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7284 | ParamTypes[0] = S.Context.getVolatileType(ParamTypes[0]); |
| 7285 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
| 7286 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 7287 | CandidateSet); |
| 7288 | } |
| 7289 | } |
| 7290 | } |
| 7291 | } |
| 7292 | |
| 7293 | // C++ [over.operator]p23: |
| 7294 | // |
| 7295 | // There also exist candidate operator functions of the form |
| 7296 | // |
| 7297 | // bool operator!(bool); |
| 7298 | // bool operator&&(bool, bool); |
| 7299 | // bool operator||(bool, bool); |
| 7300 | void addExclaimOverload() { |
| 7301 | QualType ParamTy = S.Context.BoolTy; |
| 7302 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 7303 | /*IsAssignmentOperator=*/false, |
| 7304 | /*NumContextualBoolArguments=*/1); |
| 7305 | } |
| 7306 | void addAmpAmpOrPipePipeOverload() { |
| 7307 | QualType ParamTypes[2] = { S.Context.BoolTy, S.Context.BoolTy }; |
| 7308 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 7309 | /*IsAssignmentOperator=*/false, |
| 7310 | /*NumContextualBoolArguments=*/2); |
| 7311 | } |
| 7312 | |
| 7313 | // C++ [over.built]p13: |
| 7314 | // |
| 7315 | // For every cv-qualified or cv-unqualified object type T there |
| 7316 | // exist candidate operator functions of the form |
| 7317 | // |
| 7318 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 7319 | // T& operator[](T*, ptrdiff_t); |
| 7320 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 7321 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 7322 | // T& operator[](ptrdiff_t, T*); |
| 7323 | void addSubscriptOverloads() { |
| 7324 | for (BuiltinCandidateTypeSet::iterator |
| 7325 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7326 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7327 | Ptr != PtrEnd; ++Ptr) { |
| 7328 | QualType ParamTypes[2] = { *Ptr, S.Context.getPointerDiffType() }; |
| 7329 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7330 | if (!PointeeType->isObjectType()) |
| 7331 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7332 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7333 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7334 | |
| 7335 | // T& operator[](T*, ptrdiff_t) |
| 7336 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 7337 | } |
| 7338 | |
| 7339 | for (BuiltinCandidateTypeSet::iterator |
| 7340 | Ptr = CandidateTypes[1].pointer_begin(), |
| 7341 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 7342 | Ptr != PtrEnd; ++Ptr) { |
| 7343 | QualType ParamTypes[2] = { S.Context.getPointerDiffType(), *Ptr }; |
| 7344 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 7345 | if (!PointeeType->isObjectType()) |
| 7346 | continue; |
| 7347 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7348 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 7349 | |
| 7350 | // T& operator[](ptrdiff_t, T*) |
| 7351 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 7352 | } |
| 7353 | } |
| 7354 | |
| 7355 | // C++ [over.built]p11: |
| 7356 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 7357 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 7358 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 7359 | // there exist candidate operator functions of the form |
| 7360 | // |
| 7361 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 7362 | // |
| 7363 | // where CV12 is the union of CV1 and CV2. |
| 7364 | void addArrowStarOverloads() { |
| 7365 | for (BuiltinCandidateTypeSet::iterator |
| 7366 | Ptr = CandidateTypes[0].pointer_begin(), |
| 7367 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 7368 | Ptr != PtrEnd; ++Ptr) { |
| 7369 | QualType C1Ty = (*Ptr); |
| 7370 | QualType C1; |
| 7371 | QualifierCollector Q1; |
| 7372 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
| 7373 | if (!isa<RecordType>(C1)) |
| 7374 | continue; |
| 7375 | // heuristic to reduce number of builtin candidates in the set. |
| 7376 | // Add volatile/restrict version only if there are conversions to a |
| 7377 | // volatile/restrict type. |
| 7378 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 7379 | continue; |
| 7380 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 7381 | continue; |
| 7382 | for (BuiltinCandidateTypeSet::iterator |
| 7383 | MemPtr = CandidateTypes[1].member_pointer_begin(), |
| 7384 | MemPtrEnd = CandidateTypes[1].member_pointer_end(); |
| 7385 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7386 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 7387 | QualType C2 = QualType(mptr->getClass(), 0); |
| 7388 | C2 = C2.getUnqualifiedType(); |
| 7389 | if (C1 != C2 && !S.IsDerivedFrom(C1, C2)) |
| 7390 | break; |
| 7391 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 7392 | // build CV12 T& |
| 7393 | QualType T = mptr->getPointeeType(); |
| 7394 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 7395 | T.isVolatileQualified()) |
| 7396 | continue; |
| 7397 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 7398 | T.isRestrictQualified()) |
| 7399 | continue; |
| 7400 | T = Q1.apply(S.Context, T); |
| 7401 | QualType ResultTy = S.Context.getLValueReferenceType(T); |
| 7402 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 7403 | } |
| 7404 | } |
| 7405 | } |
| 7406 | |
| 7407 | // Note that we don't consider the first argument, since it has been |
| 7408 | // contextually converted to bool long ago. The candidates below are |
| 7409 | // therefore added as binary. |
| 7410 | // |
| 7411 | // C++ [over.built]p25: |
| 7412 | // For every type T, where T is a pointer, pointer-to-member, or scoped |
| 7413 | // enumeration type, there exist candidate operator functions of the form |
| 7414 | // |
| 7415 | // T operator?(bool, T, T); |
| 7416 | // |
| 7417 | void addConditionalOperatorOverloads() { |
| 7418 | /// Set of (canonical) types that we've already handled. |
| 7419 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 7420 | |
| 7421 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 7422 | for (BuiltinCandidateTypeSet::iterator |
| 7423 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 7424 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 7425 | Ptr != PtrEnd; ++Ptr) { |
| 7426 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 7427 | continue; |
| 7428 | |
| 7429 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 7430 | S.AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 7431 | } |
| 7432 | |
| 7433 | for (BuiltinCandidateTypeSet::iterator |
| 7434 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 7435 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 7436 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 7437 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 7438 | continue; |
| 7439 | |
| 7440 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 7441 | S.AddBuiltinCandidate(*MemPtr, ParamTypes, Args, 2, CandidateSet); |
| 7442 | } |
| 7443 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7444 | if (S.getLangOpts().CPlusPlus0x) { |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7445 | for (BuiltinCandidateTypeSet::iterator |
| 7446 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 7447 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 7448 | Enum != EnumEnd; ++Enum) { |
| 7449 | if (!(*Enum)->getAs<EnumType>()->getDecl()->isScoped()) |
| 7450 | continue; |
| 7451 | |
| 7452 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 7453 | continue; |
| 7454 | |
| 7455 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 7456 | S.AddBuiltinCandidate(*Enum, ParamTypes, Args, 2, CandidateSet); |
| 7457 | } |
| 7458 | } |
| 7459 | } |
| 7460 | } |
| 7461 | }; |
| 7462 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7463 | } // end anonymous namespace |
| 7464 | |
| 7465 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 7466 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 7467 | /// on the operator @p Op and the arguments given. For example, if the |
| 7468 | /// operator is a binary '+', this routine might add "int |
| 7469 | /// operator+(int, int)" to cover integer addition. |
| 7470 | void |
| 7471 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 7472 | SourceLocation OpLoc, |
| 7473 | Expr **Args, unsigned NumArgs, |
| 7474 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7475 | // Find all of the types that the arguments can convert to, but only |
| 7476 | // if the operator we're looking at has built-in operator candidates |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7477 | // that make use of these types. Also record whether we encounter non-record |
| 7478 | // candidate types or either arithmetic or enumeral candidate types. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 7479 | Qualifiers VisibleTypeConversionsQuals; |
| 7480 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 7481 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 7482 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7483 | |
| 7484 | bool HasNonRecordCandidateType = false; |
| 7485 | bool HasArithmeticOrEnumeralCandidateType = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7486 | SmallVector<BuiltinCandidateTypeSet, 2> CandidateTypes; |
Douglas Gregor | b37c9af | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7487 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 7488 | CandidateTypes.push_back(BuiltinCandidateTypeSet(*this)); |
| 7489 | CandidateTypes[ArgIdx].AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 7490 | OpLoc, |
| 7491 | true, |
| 7492 | (Op == OO_Exclaim || |
| 7493 | Op == OO_AmpAmp || |
| 7494 | Op == OO_PipePipe), |
| 7495 | VisibleTypeConversionsQuals); |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7496 | HasNonRecordCandidateType = HasNonRecordCandidateType || |
| 7497 | CandidateTypes[ArgIdx].hasNonRecordTypes(); |
| 7498 | HasArithmeticOrEnumeralCandidateType = |
| 7499 | HasArithmeticOrEnumeralCandidateType || |
| 7500 | CandidateTypes[ArgIdx].hasArithmeticOrEnumeralTypes(); |
Douglas Gregor | b37c9af | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 7501 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7502 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7503 | // Exit early when no non-record types have been added to the candidate set |
| 7504 | // for any of the arguments to the operator. |
Douglas Gregor | 877d4eb | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 7505 | // |
| 7506 | // We can't exit early for !, ||, or &&, since there we have always have |
| 7507 | // 'bool' overloads. |
| 7508 | if (!HasNonRecordCandidateType && |
| 7509 | !(Op == OO_Exclaim || Op == OO_AmpAmp || Op == OO_PipePipe)) |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7510 | return; |
| 7511 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7512 | // Setup an object to manage the common state for building overloads. |
| 7513 | BuiltinOperatorOverloadBuilder OpBuilder(*this, Args, NumArgs, |
| 7514 | VisibleTypeConversionsQuals, |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 7515 | HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7516 | CandidateTypes, CandidateSet); |
| 7517 | |
| 7518 | // Dispatch over the operation to add in only those overloads which apply. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7519 | switch (Op) { |
| 7520 | case OO_None: |
| 7521 | case NUM_OVERLOADED_OPERATORS: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7522 | llvm_unreachable("Expected an overloaded operator"); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7523 | |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7524 | case OO_New: |
| 7525 | case OO_Delete: |
| 7526 | case OO_Array_New: |
| 7527 | case OO_Array_Delete: |
| 7528 | case OO_Call: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7529 | llvm_unreachable( |
| 7530 | "Special operators don't use AddBuiltinOperatorCandidates"); |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7531 | |
| 7532 | case OO_Comma: |
| 7533 | case OO_Arrow: |
| 7534 | // C++ [over.match.oper]p3: |
| 7535 | // -- For the operator ',', the unary operator '&', or the |
| 7536 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7537 | break; |
| 7538 | |
| 7539 | case OO_Plus: // '+' is either unary or binary |
Chandler Carruth | 9694b9c | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 7540 | if (NumArgs == 1) |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7541 | OpBuilder.addUnaryPlusPointerOverloads(); |
Chandler Carruth | 9694b9c | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 7542 | // Fall through. |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7543 | |
| 7544 | case OO_Minus: // '-' is either unary or binary |
Chandler Carruth | f980244 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7545 | if (NumArgs == 1) { |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7546 | OpBuilder.addUnaryPlusOrMinusArithmeticOverloads(); |
Chandler Carruth | f980244 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7547 | } else { |
| 7548 | OpBuilder.addBinaryPlusOrMinusPointerOverloads(Op); |
| 7549 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7550 | } |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7551 | break; |
| 7552 | |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7553 | case OO_Star: // '*' is either unary or binary |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7554 | if (NumArgs == 1) |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7555 | OpBuilder.addUnaryStarPointerOverloads(); |
| 7556 | else |
| 7557 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7558 | break; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7559 | |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7560 | case OO_Slash: |
| 7561 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
Chandler Carruth | 9de23cd | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 7562 | break; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7563 | |
| 7564 | case OO_PlusPlus: |
| 7565 | case OO_MinusMinus: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7566 | OpBuilder.addPlusPlusMinusMinusArithmeticOverloads(Op); |
| 7567 | OpBuilder.addPlusPlusMinusMinusPointerOverloads(); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7568 | break; |
| 7569 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 7570 | case OO_EqualEqual: |
| 7571 | case OO_ExclaimEqual: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7572 | OpBuilder.addEqualEqualOrNotEqualMemberPointerOverloads(); |
Chandler Carruth | 0375e95 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 7573 | // Fall through. |
Chandler Carruth | 9de23cd | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 7574 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7575 | case OO_Less: |
| 7576 | case OO_Greater: |
| 7577 | case OO_LessEqual: |
| 7578 | case OO_GreaterEqual: |
Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 7579 | OpBuilder.addRelationalPointerOrEnumeralOverloads(); |
Chandler Carruth | 0375e95 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 7580 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/true); |
| 7581 | break; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7582 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7583 | case OO_Percent: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7584 | case OO_Caret: |
| 7585 | case OO_Pipe: |
| 7586 | case OO_LessLess: |
| 7587 | case OO_GreaterGreater: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7588 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7589 | break; |
| 7590 | |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 7591 | case OO_Amp: // '&' is either unary or binary |
| 7592 | if (NumArgs == 1) |
| 7593 | // C++ [over.match.oper]p3: |
| 7594 | // -- For the operator ',', the unary operator '&', or the |
| 7595 | // operator '->', the built-in candidates set is empty. |
| 7596 | break; |
| 7597 | |
| 7598 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
| 7599 | break; |
| 7600 | |
| 7601 | case OO_Tilde: |
| 7602 | OpBuilder.addUnaryTildePromotedIntegralOverloads(); |
| 7603 | break; |
| 7604 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7605 | case OO_Equal: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7606 | OpBuilder.addAssignmentMemberPointerOrEnumeralOverloads(); |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7607 | // Fall through. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7608 | |
| 7609 | case OO_PlusEqual: |
| 7610 | case OO_MinusEqual: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7611 | OpBuilder.addAssignmentPointerOverloads(Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7612 | // Fall through. |
| 7613 | |
| 7614 | case OO_StarEqual: |
| 7615 | case OO_SlashEqual: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7616 | OpBuilder.addAssignmentArithmeticOverloads(Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7617 | break; |
| 7618 | |
| 7619 | case OO_PercentEqual: |
| 7620 | case OO_LessLessEqual: |
| 7621 | case OO_GreaterGreaterEqual: |
| 7622 | case OO_AmpEqual: |
| 7623 | case OO_CaretEqual: |
| 7624 | case OO_PipeEqual: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7625 | OpBuilder.addAssignmentIntegralOverloads(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7626 | break; |
| 7627 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7628 | case OO_Exclaim: |
| 7629 | OpBuilder.addExclaimOverload(); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7630 | break; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7631 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7632 | case OO_AmpAmp: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7633 | case OO_PipePipe: |
| 7634 | OpBuilder.addAmpAmpOrPipePipeOverload(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7635 | break; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7636 | |
| 7637 | case OO_Subscript: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7638 | OpBuilder.addSubscriptOverloads(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7639 | break; |
| 7640 | |
| 7641 | case OO_ArrowStar: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7642 | OpBuilder.addArrowStarOverloads(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7643 | break; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 7644 | |
| 7645 | case OO_Conditional: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 7646 | OpBuilder.addConditionalOperatorOverloads(); |
Chandler Carruth | f980244 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 7647 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 7648 | break; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7649 | } |
| 7650 | } |
| 7651 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7652 | /// \brief Add function candidates found via argument-dependent lookup |
| 7653 | /// to the set of overloading candidates. |
| 7654 | /// |
| 7655 | /// This routine performs argument-dependent name lookup based on the |
| 7656 | /// given function name (which may also be an operator name) and adds |
| 7657 | /// all of the overload candidates found by ADL to the overload |
| 7658 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7659 | void |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7660 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
Richard Smith | e06a2c1 | 2012-02-25 06:24:24 +0000 | [diff] [blame] | 7661 | bool Operator, SourceLocation Loc, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7662 | llvm::ArrayRef<Expr *> Args, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 7663 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7664 | OverloadCandidateSet& CandidateSet, |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 7665 | bool PartialOverloading, |
| 7666 | bool StdNamespaceIsAssociated) { |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7667 | ADLResult Fns; |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7668 | |
John McCall | 91f61fc | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 7669 | // FIXME: This approach for uniquing ADL results (and removing |
| 7670 | // redundant candidates from the set) relies on pointer-equality, |
| 7671 | // which means we need to key off the canonical decl. However, |
| 7672 | // always going back to the canonical decl might not get us the |
| 7673 | // right set of default arguments. What default arguments are |
| 7674 | // we supposed to consider on ADL candidates, anyway? |
| 7675 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7676 | // FIXME: Pass in the explicit template arguments? |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7677 | ArgumentDependentLookup(Name, Operator, Loc, Args, Fns, |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 7678 | StdNamespaceIsAssociated); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7679 | |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7680 | // Erase all of the candidates we already knew about. |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7681 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 7682 | CandEnd = CandidateSet.end(); |
| 7683 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7684 | if (Cand->Function) { |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7685 | Fns.erase(Cand->Function); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7686 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7687 | Fns.erase(FunTmpl); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7688 | } |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 7689 | |
| 7690 | // For each of the ADL candidates we found, add it to the overload |
| 7691 | // set. |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 7692 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7693 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7694 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7695 | if (ExplicitTemplateArgs) |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7696 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7697 | |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7698 | AddOverloadCandidate(FD, FoundDecl, Args, CandidateSet, false, |
| 7699 | PartialOverloading); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7700 | } else |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7701 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7702 | FoundDecl, ExplicitTemplateArgs, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 7703 | Args, CandidateSet); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 7704 | } |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7705 | } |
| 7706 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7707 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 7708 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7709 | bool |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7710 | isBetterOverloadCandidate(Sema &S, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 7711 | const OverloadCandidate &Cand1, |
| 7712 | const OverloadCandidate &Cand2, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7713 | SourceLocation Loc, |
| 7714 | bool UserDefinedConversion) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7715 | // Define viable functions to be better candidates than non-viable |
| 7716 | // functions. |
| 7717 | if (!Cand2.Viable) |
| 7718 | return Cand1.Viable; |
| 7719 | else if (!Cand1.Viable) |
| 7720 | return false; |
| 7721 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7722 | // C++ [over.match.best]p1: |
| 7723 | // |
| 7724 | // -- if F is a static member function, ICS1(F) is defined such |
| 7725 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 7726 | // any function G, and, symmetrically, ICS1(G) is neither |
| 7727 | // better nor worse than ICS1(F). |
| 7728 | unsigned StartArg = 0; |
| 7729 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 7730 | StartArg = 1; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7731 | |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7732 | // C++ [over.match.best]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7733 | // A viable function F1 is defined to be a better function than another |
| 7734 | // viable function F2 if for all arguments i, ICSi(F1) is not a worse |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7735 | // conversion sequence than ICSi(F2), and then... |
Benjamin Kramer | b009517 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 7736 | unsigned NumArgs = Cand1.NumConversions; |
| 7737 | assert(Cand2.NumConversions == NumArgs && "Overload candidate mismatch"); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7738 | bool HasBetterConversion = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7739 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7740 | switch (CompareImplicitConversionSequences(S, |
| 7741 | Cand1.Conversions[ArgIdx], |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7742 | Cand2.Conversions[ArgIdx])) { |
| 7743 | case ImplicitConversionSequence::Better: |
| 7744 | // Cand1 has a better conversion sequence. |
| 7745 | HasBetterConversion = true; |
| 7746 | break; |
| 7747 | |
| 7748 | case ImplicitConversionSequence::Worse: |
| 7749 | // Cand1 can't be better than Cand2. |
| 7750 | return false; |
| 7751 | |
| 7752 | case ImplicitConversionSequence::Indistinguishable: |
| 7753 | // Do nothing. |
| 7754 | break; |
| 7755 | } |
| 7756 | } |
| 7757 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7758 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7759 | // ICSj(F2), or, if not that, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7760 | if (HasBetterConversion) |
| 7761 | return true; |
| 7762 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7763 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7764 | // specialization, or, if not that, |
Douglas Gregor | ce21919b | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 7765 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7766 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 7767 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7768 | |
| 7769 | // -- F1 and F2 are function template specializations, and the function |
| 7770 | // template for F1 is more specialized than the template for F2 |
| 7771 | // according to the partial ordering rules described in 14.5.5.2, or, |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7772 | // if not that, |
Douglas Gregor | 55137cb | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 7773 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7774 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7775 | if (FunctionTemplateDecl *BetterTemplate |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7776 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 7777 | Cand2.Function->getPrimaryTemplate(), |
| 7778 | Loc, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7779 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 7780 | : TPOC_Call, |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7781 | Cand1.ExplicitCallArguments)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7782 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7783 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7784 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7785 | // -- the context is an initialization by user-defined conversion |
| 7786 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 7787 | // from the return type of F1 to the destination type (i.e., |
| 7788 | // the type of the entity being initialized) is a better |
| 7789 | // conversion sequence than the standard conversion sequence |
| 7790 | // from the return type of F2 to the destination type. |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7791 | if (UserDefinedConversion && Cand1.Function && Cand2.Function && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7792 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7793 | isa<CXXConversionDecl>(Cand2.Function)) { |
Douglas Gregor | 2837aa2 | 2012-02-22 17:32:19 +0000 | [diff] [blame] | 7794 | // First check whether we prefer one of the conversion functions over the |
| 7795 | // other. This only distinguishes the results in non-standard, extension |
| 7796 | // cases such as the conversion from a lambda closure type to a function |
| 7797 | // pointer or block. |
| 7798 | ImplicitConversionSequence::CompareKind FuncResult |
| 7799 | = compareConversionFunctions(S, Cand1.Function, Cand2.Function); |
| 7800 | if (FuncResult != ImplicitConversionSequence::Indistinguishable) |
| 7801 | return FuncResult; |
| 7802 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7803 | switch (CompareStandardConversionSequences(S, |
| 7804 | Cand1.FinalConversion, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7805 | Cand2.FinalConversion)) { |
| 7806 | case ImplicitConversionSequence::Better: |
| 7807 | // Cand1 has a better conversion sequence. |
| 7808 | return true; |
| 7809 | |
| 7810 | case ImplicitConversionSequence::Worse: |
| 7811 | // Cand1 can't be better than Cand2. |
| 7812 | return false; |
| 7813 | |
| 7814 | case ImplicitConversionSequence::Indistinguishable: |
| 7815 | // Do nothing |
| 7816 | break; |
| 7817 | } |
| 7818 | } |
| 7819 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7820 | return false; |
| 7821 | } |
| 7822 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7823 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7824 | /// within an overload candidate set. |
| 7825 | /// |
| 7826 | /// \param CandidateSet the set of candidate functions. |
| 7827 | /// |
| 7828 | /// \param Loc the location of the function name (or operator symbol) for |
| 7829 | /// which overload resolution occurs. |
| 7830 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7831 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7832 | /// function, Best points to the candidate function found. |
| 7833 | /// |
| 7834 | /// \returns The result of overload resolution. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7835 | OverloadingResult |
| 7836 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 7837 | iterator &Best, |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 7838 | bool UserDefinedConversion) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7839 | // Find the best viable function. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7840 | Best = end(); |
| 7841 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
| 7842 | if (Cand->Viable) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7843 | if (Best == end() || isBetterOverloadCandidate(S, *Cand, *Best, Loc, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7844 | UserDefinedConversion)) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7845 | Best = Cand; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7846 | } |
| 7847 | |
| 7848 | // If we didn't find any viable functions, abort. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7849 | if (Best == end()) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7850 | return OR_No_Viable_Function; |
| 7851 | |
| 7852 | // Make sure that this function is better than every other viable |
| 7853 | // function. If not, we have an ambiguity. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7854 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7855 | if (Cand->Viable && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7856 | Cand != Best && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7857 | !isBetterOverloadCandidate(S, *Best, *Cand, Loc, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7858 | UserDefinedConversion)) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7859 | Best = end(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7860 | return OR_Ambiguous; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7861 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7862 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7863 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7864 | // Best is the best viable function. |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7865 | if (Best->Function && |
Argyrios Kyrtzidis | ab72b67 | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 7866 | (Best->Function->isDeleted() || |
| 7867 | S.isFunctionConsideredUnavailable(Best->Function))) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7868 | return OR_Deleted; |
| 7869 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7870 | return OR_Success; |
| 7871 | } |
| 7872 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7873 | namespace { |
| 7874 | |
| 7875 | enum OverloadCandidateKind { |
| 7876 | oc_function, |
| 7877 | oc_method, |
| 7878 | oc_constructor, |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7879 | oc_function_template, |
| 7880 | oc_method_template, |
| 7881 | oc_constructor_template, |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7882 | oc_implicit_default_constructor, |
| 7883 | oc_implicit_copy_constructor, |
Alexis Hunt | 119c10e | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 7884 | oc_implicit_move_constructor, |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7885 | oc_implicit_copy_assignment, |
Alexis Hunt | 119c10e | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 7886 | oc_implicit_move_assignment, |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7887 | oc_implicit_inherited_constructor |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7888 | }; |
| 7889 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7890 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 7891 | FunctionDecl *Fn, |
| 7892 | std::string &Description) { |
| 7893 | bool isTemplate = false; |
| 7894 | |
| 7895 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 7896 | isTemplate = true; |
| 7897 | Description = S.getTemplateArgumentBindingsText( |
| 7898 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 7899 | } |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7900 | |
| 7901 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7902 | if (!Ctor->isImplicit()) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7903 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7904 | |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7905 | if (Ctor->getInheritedConstructor()) |
| 7906 | return oc_implicit_inherited_constructor; |
| 7907 | |
Alexis Hunt | 119c10e | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 7908 | if (Ctor->isDefaultConstructor()) |
| 7909 | return oc_implicit_default_constructor; |
| 7910 | |
| 7911 | if (Ctor->isMoveConstructor()) |
| 7912 | return oc_implicit_move_constructor; |
| 7913 | |
| 7914 | assert(Ctor->isCopyConstructor() && |
| 7915 | "unexpected sort of implicit constructor"); |
| 7916 | return oc_implicit_copy_constructor; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7917 | } |
| 7918 | |
| 7919 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 7920 | // This actually gets spelled 'candidate function' for now, but |
| 7921 | // it doesn't hurt to split it out. |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7922 | if (!Meth->isImplicit()) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7923 | return isTemplate ? oc_method_template : oc_method; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7924 | |
Alexis Hunt | 119c10e | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 7925 | if (Meth->isMoveAssignmentOperator()) |
| 7926 | return oc_implicit_move_assignment; |
| 7927 | |
Douglas Gregor | 1269510 | 2012-02-10 08:36:38 +0000 | [diff] [blame] | 7928 | if (Meth->isCopyAssignmentOperator()) |
| 7929 | return oc_implicit_copy_assignment; |
| 7930 | |
| 7931 | assert(isa<CXXConversionDecl>(Meth) && "expected conversion"); |
| 7932 | return oc_method; |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7933 | } |
| 7934 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7935 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7936 | } |
| 7937 | |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7938 | void MaybeEmitInheritedConstructorNote(Sema &S, FunctionDecl *Fn) { |
| 7939 | const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn); |
| 7940 | if (!Ctor) return; |
| 7941 | |
| 7942 | Ctor = Ctor->getInheritedConstructor(); |
| 7943 | if (!Ctor) return; |
| 7944 | |
| 7945 | S.Diag(Ctor->getLocation(), diag::note_ovl_candidate_inherited_constructor); |
| 7946 | } |
| 7947 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7948 | } // end anonymous namespace |
| 7949 | |
| 7950 | // Notes the location of an overload candidate. |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 7951 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn, QualType DestType) { |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7952 | std::string FnDesc; |
| 7953 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 7954 | PartialDiagnostic PD = PDiag(diag::note_ovl_candidate) |
| 7955 | << (unsigned) K << FnDesc; |
| 7956 | HandleFunctionTypeMismatch(PD, Fn->getType(), DestType); |
| 7957 | Diag(Fn->getLocation(), PD); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7958 | MaybeEmitInheritedConstructorNote(*this, Fn); |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7959 | } |
| 7960 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7961 | //Notes the location of all overload candidates designated through |
| 7962 | // OverloadedExpr |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 7963 | void Sema::NoteAllOverloadCandidates(Expr* OverloadedExpr, QualType DestType) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7964 | assert(OverloadedExpr->getType() == Context.OverloadTy); |
| 7965 | |
| 7966 | OverloadExpr::FindResult Ovl = OverloadExpr::find(OverloadedExpr); |
| 7967 | OverloadExpr *OvlExpr = Ovl.Expression; |
| 7968 | |
| 7969 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 7970 | IEnd = OvlExpr->decls_end(); |
| 7971 | I != IEnd; ++I) { |
| 7972 | if (FunctionTemplateDecl *FunTmpl = |
| 7973 | dyn_cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 7974 | NoteOverloadCandidate(FunTmpl->getTemplatedDecl(), DestType); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7975 | } else if (FunctionDecl *Fun |
| 7976 | = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 7977 | NoteOverloadCandidate(Fun, DestType); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7978 | } |
| 7979 | } |
| 7980 | } |
| 7981 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7982 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 7983 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 7984 | /// target types of the conversion. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7985 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
| 7986 | Sema &S, |
| 7987 | SourceLocation CaretLoc, |
| 7988 | const PartialDiagnostic &PDiag) const { |
| 7989 | S.Diag(CaretLoc, PDiag) |
| 7990 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7991 | for (AmbiguousConversionSequence::const_iterator |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7992 | I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
| 7993 | S.NoteOverloadCandidate(*I); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7994 | } |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7995 | } |
| 7996 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7997 | namespace { |
| 7998 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7999 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 8000 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 8001 | assert(Conv.isBad()); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8002 | assert(Cand->Function && "for now, candidate must be a function"); |
| 8003 | FunctionDecl *Fn = Cand->Function; |
| 8004 | |
| 8005 | // There's a conversion slot for the object argument if this is a |
| 8006 | // non-constructor method. Note that 'I' corresponds the |
| 8007 | // conversion-slot index. |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8008 | bool isObjectArgument = false; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8009 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8010 | if (I == 0) |
| 8011 | isObjectArgument = true; |
| 8012 | else |
| 8013 | I--; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8014 | } |
| 8015 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8016 | std::string FnDesc; |
| 8017 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 8018 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8019 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 8020 | QualType FromTy = Conv.Bad.getFromType(); |
| 8021 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8022 | |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8023 | if (FromTy == S.Context.OverloadTy) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8024 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8025 | Expr *E = FromExpr->IgnoreParens(); |
| 8026 | if (isa<UnaryOperator>(E)) |
| 8027 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 8028 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8029 | |
| 8030 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 8031 | << (unsigned) FnKind << FnDesc |
| 8032 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8033 | << ToTy << Name << I+1; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8034 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 8035 | return; |
| 8036 | } |
| 8037 | |
John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8038 | // Do some hand-waving analysis to see if the non-viability is due |
| 8039 | // to a qualifier mismatch. |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8040 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 8041 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 8042 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 8043 | CToTy = RT->getPointeeType(); |
| 8044 | else { |
| 8045 | // TODO: detect and diagnose the full richness of const mismatches. |
| 8046 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 8047 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 8048 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 8049 | } |
| 8050 | |
| 8051 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 8052 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8053 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8054 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8055 | |
| 8056 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 8057 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 8058 | << (unsigned) FnKind << FnDesc |
| 8059 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8060 | << FromTy |
| 8061 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 8062 | << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8063 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8064 | return; |
| 8065 | } |
| 8066 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8067 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 8068 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_ownership) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8069 | << (unsigned) FnKind << FnDesc |
| 8070 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8071 | << FromTy |
| 8072 | << FromQs.getObjCLifetime() << ToQs.getObjCLifetime() |
| 8073 | << (unsigned) isObjectArgument << I+1; |
| 8074 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8075 | return; |
| 8076 | } |
| 8077 | |
Douglas Gregor | aec2584 | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 8078 | if (FromQs.getObjCGCAttr() != ToQs.getObjCGCAttr()) { |
| 8079 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_gc) |
| 8080 | << (unsigned) FnKind << FnDesc |
| 8081 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8082 | << FromTy |
| 8083 | << FromQs.getObjCGCAttr() << ToQs.getObjCGCAttr() |
| 8084 | << (unsigned) isObjectArgument << I+1; |
| 8085 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8086 | return; |
| 8087 | } |
| 8088 | |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8089 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 8090 | assert(CVR && "unexpected qualifiers mismatch"); |
| 8091 | |
| 8092 | if (isObjectArgument) { |
| 8093 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 8094 | << (unsigned) FnKind << FnDesc |
| 8095 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8096 | << FromTy << (CVR - 1); |
| 8097 | } else { |
| 8098 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 8099 | << (unsigned) FnKind << FnDesc |
| 8100 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8101 | << FromTy << (CVR - 1) << I+1; |
| 8102 | } |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8103 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 8104 | return; |
| 8105 | } |
| 8106 | |
Sebastian Redl | a72462c | 2011-09-24 17:48:32 +0000 | [diff] [blame] | 8107 | // Special diagnostic for failure to convert an initializer list, since |
| 8108 | // telling the user that it has type void is not useful. |
| 8109 | if (FromExpr && isa<InitListExpr>(FromExpr)) { |
| 8110 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_list_argument) |
| 8111 | << (unsigned) FnKind << FnDesc |
| 8112 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8113 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8114 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8115 | return; |
| 8116 | } |
| 8117 | |
John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8118 | // Diagnose references or pointers to incomplete types differently, |
| 8119 | // since it's far from impossible that the incompleteness triggered |
| 8120 | // the failure. |
| 8121 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 8122 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 8123 | TempFromTy = PTy->getPointeeType(); |
| 8124 | if (TempFromTy->isIncompleteType()) { |
| 8125 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 8126 | << (unsigned) FnKind << FnDesc |
| 8127 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8128 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8129 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 8130 | return; |
| 8131 | } |
| 8132 | |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8133 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8134 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8135 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 8136 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 8137 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8138 | FromPtrTy->getPointeeType()) && |
| 8139 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 8140 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8141 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8142 | FromPtrTy->getPointeeType())) |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8143 | BaseToDerivedConversion = 1; |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8144 | } |
| 8145 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 8146 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 8147 | if (const ObjCObjectPointerType *ToPtrTy |
| 8148 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 8149 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 8150 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 8151 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 8152 | FromPtrTy->getPointeeType()) && |
| 8153 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8154 | BaseToDerivedConversion = 2; |
| 8155 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
Kaelyn Uhrain | 9ea8f7e | 2012-06-19 00:37:47 +0000 | [diff] [blame^] | 8156 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 8157 | !FromTy->isIncompleteType() && |
| 8158 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 8159 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) { |
| 8160 | BaseToDerivedConversion = 3; |
| 8161 | } else if (ToTy->isLValueReferenceType() && !FromExpr->isLValue() && |
| 8162 | ToTy.getNonReferenceType().getCanonicalType() == |
| 8163 | FromTy.getNonReferenceType().getCanonicalType()) { |
| 8164 | QualType T1 = ToTy.getCanonicalType(); |
| 8165 | QualType T2 = ToTy.getNonReferenceType(); |
| 8166 | QualType T3 = T2.getUnqualifiedType(); |
| 8167 | QualType T4 = FromTy.getCanonicalType(); |
| 8168 | (void)T1; (void)T2; (void)T3; (void)T4; |
| 8169 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_lvalue) |
| 8170 | << (unsigned) FnKind << FnDesc |
| 8171 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8172 | << (unsigned) isObjectArgument << I + 1; |
| 8173 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8174 | return; |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8175 | } |
Kaelyn Uhrain | 9ea8f7e | 2012-06-19 00:37:47 +0000 | [diff] [blame^] | 8176 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8177 | |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8178 | if (BaseToDerivedConversion) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8179 | S.Diag(Fn->getLocation(), |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8180 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8181 | << (unsigned) FnKind << FnDesc |
| 8182 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 8183 | << (BaseToDerivedConversion - 1) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8184 | << FromTy << ToTy << I+1; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8185 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 8186 | return; |
| 8187 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8188 | |
Fariborz Jahanian | a644f9c | 2011-07-20 17:14:09 +0000 | [diff] [blame] | 8189 | if (isa<ObjCObjectPointerType>(CFromTy) && |
| 8190 | isa<PointerType>(CToTy)) { |
| 8191 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 8192 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 8193 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
| 8194 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_arc_conv) |
| 8195 | << (unsigned) FnKind << FnDesc |
| 8196 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 8197 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 8198 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 8199 | return; |
| 8200 | } |
| 8201 | } |
| 8202 | |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8203 | // Emit the generic diagnostic and, optionally, add the hints to it. |
| 8204 | PartialDiagnostic FDiag = S.PDiag(diag::note_ovl_candidate_bad_conv); |
| 8205 | FDiag << (unsigned) FnKind << FnDesc |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8206 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8207 | << FromTy << ToTy << (unsigned) isObjectArgument << I + 1 |
| 8208 | << (unsigned) (Cand->Fix.Kind); |
| 8209 | |
| 8210 | // If we can fix the conversion, suggest the FixIts. |
Benjamin Kramer | 490afa6 | 2012-01-14 21:05:10 +0000 | [diff] [blame] | 8211 | for (std::vector<FixItHint>::iterator HI = Cand->Fix.Hints.begin(), |
| 8212 | HE = Cand->Fix.Hints.end(); HI != HE; ++HI) |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8213 | FDiag << *HI; |
| 8214 | S.Diag(Fn->getLocation(), FDiag); |
| 8215 | |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8216 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8217 | } |
| 8218 | |
| 8219 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 8220 | unsigned NumFormalArgs) { |
| 8221 | // TODO: treat calls to a missing default constructor as a special case |
| 8222 | |
| 8223 | FunctionDecl *Fn = Cand->Function; |
| 8224 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 8225 | |
| 8226 | unsigned MinParams = Fn->getMinRequiredArguments(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8227 | |
Douglas Gregor | 1d33f8d | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 8228 | // With invalid overloaded operators, it's possible that we think we |
| 8229 | // have an arity mismatch when it fact it looks like we have the |
| 8230 | // right number of arguments, because only overloaded operators have |
| 8231 | // the weird behavior of overloading member and non-member functions. |
| 8232 | // Just don't report anything. |
| 8233 | if (Fn->isInvalidDecl() && |
| 8234 | Fn->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 8235 | return; |
| 8236 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8237 | // at least / at most / exactly |
| 8238 | unsigned mode, modeCount; |
| 8239 | if (NumFormalArgs < MinParams) { |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8240 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 8241 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8242 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8243 | if (MinParams != FnTy->getNumArgs() || |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 8244 | FnTy->isVariadic() || FnTy->isTemplateVariadic()) |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8245 | mode = 0; // "at least" |
| 8246 | else |
| 8247 | mode = 2; // "exactly" |
| 8248 | modeCount = MinParams; |
| 8249 | } else { |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8250 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 8251 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 8252 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8253 | if (MinParams != FnTy->getNumArgs()) |
| 8254 | mode = 1; // "at most" |
| 8255 | else |
| 8256 | mode = 2; // "exactly" |
| 8257 | modeCount = FnTy->getNumArgs(); |
| 8258 | } |
| 8259 | |
| 8260 | std::string Description; |
| 8261 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 8262 | |
Richard Smith | 10ff50d | 2012-05-11 05:16:41 +0000 | [diff] [blame] | 8263 | if (modeCount == 1 && Fn->getParamDecl(0)->getDeclName()) |
| 8264 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity_one) |
| 8265 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 8266 | << Fn->getParamDecl(0) << NumFormalArgs; |
| 8267 | else |
| 8268 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
| 8269 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 8270 | << modeCount << NumFormalArgs; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8271 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8272 | } |
| 8273 | |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8274 | /// Diagnose a failed template-argument deduction. |
| 8275 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8276 | unsigned NumArgs) { |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8277 | FunctionDecl *Fn = Cand->Function; // pattern |
| 8278 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8279 | TemplateParameter Param = Cand->DeductionFailure.getTemplateParameter(); |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8280 | NamedDecl *ParamD; |
| 8281 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 8282 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 8283 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8284 | switch (Cand->DeductionFailure.Result) { |
| 8285 | case Sema::TDK_Success: |
| 8286 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 8287 | |
| 8288 | case Sema::TDK_Incomplete: { |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8289 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| 8290 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_incomplete_deduction) |
| 8291 | << ParamD->getDeclName(); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8292 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8293 | return; |
| 8294 | } |
| 8295 | |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8296 | case Sema::TDK_Underqualified: { |
| 8297 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 8298 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 8299 | |
| 8300 | QualType Param = Cand->DeductionFailure.getFirstArg()->getAsType(); |
| 8301 | |
| 8302 | // Param will have been canonicalized, but it should just be a |
| 8303 | // qualified version of ParamD, so move the qualifiers to that. |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 8304 | QualifierCollector Qs; |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8305 | Qs.strip(Param); |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 8306 | QualType NonCanonParam = Qs.apply(S.Context, TParam->getTypeForDecl()); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8307 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 8308 | |
| 8309 | // Arg has also been canonicalized, but there's nothing we can do |
| 8310 | // about that. It also doesn't matter as much, because it won't |
| 8311 | // have any template parameters in it (because deduction isn't |
| 8312 | // done on dependent types). |
| 8313 | QualType Arg = Cand->DeductionFailure.getSecondArg()->getAsType(); |
| 8314 | |
| 8315 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_underqualified) |
| 8316 | << ParamD->getDeclName() << Arg << NonCanonParam; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8317 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 8318 | return; |
| 8319 | } |
| 8320 | |
| 8321 | case Sema::TDK_Inconsistent: { |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8322 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8323 | int which = 0; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8324 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8325 | which = 0; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8326 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8327 | which = 1; |
| 8328 | else { |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8329 | which = 2; |
| 8330 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8331 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8332 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_inconsistent_deduction) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8333 | << which << ParamD->getDeclName() |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8334 | << *Cand->DeductionFailure.getFirstArg() |
| 8335 | << *Cand->DeductionFailure.getSecondArg(); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8336 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 8337 | return; |
| 8338 | } |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8339 | |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8340 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8341 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8342 | if (ParamD->getDeclName()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8343 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8344 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
| 8345 | << ParamD->getDeclName(); |
| 8346 | else { |
| 8347 | int index = 0; |
| 8348 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 8349 | index = TTP->getIndex(); |
| 8350 | else if (NonTypeTemplateParmDecl *NTTP |
| 8351 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 8352 | index = NTTP->getIndex(); |
| 8353 | else |
| 8354 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8355 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8356 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
| 8357 | << (index + 1); |
| 8358 | } |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8359 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 8360 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8361 | |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 8362 | case Sema::TDK_TooManyArguments: |
| 8363 | case Sema::TDK_TooFewArguments: |
| 8364 | DiagnoseArityMismatch(S, Cand, NumArgs); |
| 8365 | return; |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8366 | |
| 8367 | case Sema::TDK_InstantiationDepth: |
| 8368 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_instantiation_depth); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8369 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8370 | return; |
| 8371 | |
| 8372 | case Sema::TDK_SubstitutionFailure: { |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8373 | // Format the template argument list into the argument string. |
| 8374 | llvm::SmallString<128> TemplateArgString; |
| 8375 | if (TemplateArgumentList *Args = |
| 8376 | Cand->DeductionFailure.getTemplateArgumentList()) { |
| 8377 | TemplateArgString = " "; |
| 8378 | TemplateArgString += S.getTemplateArgumentBindingsText( |
| 8379 | Fn->getDescribedFunctionTemplate()->getTemplateParameters(), *Args); |
| 8380 | } |
| 8381 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8382 | // If this candidate was disabled by enable_if, say so. |
| 8383 | PartialDiagnosticAt *PDiag = Cand->DeductionFailure.getSFINAEDiagnostic(); |
| 8384 | if (PDiag && PDiag->second.getDiagID() == |
| 8385 | diag::err_typename_nested_not_found_enable_if) { |
| 8386 | // FIXME: Use the source range of the condition, and the fully-qualified |
| 8387 | // name of the enable_if template. These are both present in PDiag. |
| 8388 | S.Diag(PDiag->first, diag::note_ovl_candidate_disabled_by_enable_if) |
| 8389 | << "'enable_if'" << TemplateArgString; |
| 8390 | return; |
| 8391 | } |
| 8392 | |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8393 | // Format the SFINAE diagnostic into the argument string. |
| 8394 | // FIXME: Add a general mechanism to include a PartialDiagnostic *'s |
| 8395 | // formatted message in another diagnostic. |
| 8396 | llvm::SmallString<128> SFINAEArgString; |
| 8397 | SourceRange R; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8398 | if (PDiag) { |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8399 | SFINAEArgString = ": "; |
| 8400 | R = SourceRange(PDiag->first, PDiag->first); |
| 8401 | PDiag->second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 8402 | } |
| 8403 | |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8404 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_substitution_failure) |
Richard Smith | 9ca6461 | 2012-05-07 09:03:25 +0000 | [diff] [blame] | 8405 | << TemplateArgString << SFINAEArgString << R; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8406 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 8407 | return; |
| 8408 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8409 | |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8410 | // TODO: diagnose these individually, then kill off |
| 8411 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8412 | case Sema::TDK_NonDeducedMismatch: |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8413 | case Sema::TDK_FailedOverloadResolution: |
| 8414 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8415 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8416 | return; |
| 8417 | } |
| 8418 | } |
| 8419 | |
Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8420 | /// CUDA: diagnose an invalid call across targets. |
| 8421 | void DiagnoseBadTarget(Sema &S, OverloadCandidate *Cand) { |
| 8422 | FunctionDecl *Caller = cast<FunctionDecl>(S.CurContext); |
| 8423 | FunctionDecl *Callee = Cand->Function; |
| 8424 | |
| 8425 | Sema::CUDAFunctionTarget CallerTarget = S.IdentifyCUDATarget(Caller), |
| 8426 | CalleeTarget = S.IdentifyCUDATarget(Callee); |
| 8427 | |
| 8428 | std::string FnDesc; |
| 8429 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Callee, FnDesc); |
| 8430 | |
| 8431 | S.Diag(Callee->getLocation(), diag::note_ovl_candidate_bad_target) |
| 8432 | << (unsigned) FnKind << CalleeTarget << CallerTarget; |
| 8433 | } |
| 8434 | |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8435 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 8436 | /// already generated a primary error at the call site. |
| 8437 | /// |
| 8438 | /// It really does need to be a single diagnostic with its caret |
| 8439 | /// pointed at the candidate declaration. Yes, this creates some |
| 8440 | /// major challenges of technical writing. Yes, this makes pointing |
| 8441 | /// out problems with specific arguments quite awkward. It's still |
| 8442 | /// better than generating twenty screens of text for every failed |
| 8443 | /// overload. |
| 8444 | /// |
| 8445 | /// It would be great to be able to express per-candidate problems |
| 8446 | /// more richly for those diagnostic clients that cared, but we'd |
| 8447 | /// still have to be just as careful with the default diagnostics. |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8448 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8449 | unsigned NumArgs) { |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8450 | FunctionDecl *Fn = Cand->Function; |
| 8451 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8452 | // Note deleted candidates, but only if they're viable. |
Argyrios Kyrtzidis | ab72b67 | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 8453 | if (Cand->Viable && (Fn->isDeleted() || |
| 8454 | S.isFunctionConsideredUnavailable(Fn))) { |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8455 | std::string FnDesc; |
| 8456 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 8457 | |
| 8458 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
Richard Smith | 6f1e2c6 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 8459 | << FnKind << FnDesc |
| 8460 | << (Fn->isDeleted() ? (Fn->isDeletedAsWritten() ? 1 : 2) : 0); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8461 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8462 | return; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8463 | } |
| 8464 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8465 | // We don't really have anything else to say about viable candidates. |
| 8466 | if (Cand->Viable) { |
| 8467 | S.NoteOverloadCandidate(Fn); |
| 8468 | return; |
| 8469 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8470 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8471 | switch (Cand->FailureKind) { |
| 8472 | case ovl_fail_too_many_arguments: |
| 8473 | case ovl_fail_too_few_arguments: |
| 8474 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8475 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8476 | case ovl_fail_bad_deduction: |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8477 | return DiagnoseBadDeduction(S, Cand, NumArgs); |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 8478 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8479 | case ovl_fail_trivial_conversion: |
| 8480 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 8481 | case ovl_fail_final_conversion_not_exact: |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8482 | return S.NoteOverloadCandidate(Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8483 | |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8484 | case ovl_fail_bad_conversion: { |
| 8485 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | b009517 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8486 | for (unsigned N = Cand->NumConversions; I != N; ++I) |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8487 | if (Cand->Conversions[I].isBad()) |
| 8488 | return DiagnoseBadConversion(S, Cand, I); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8489 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 8490 | // FIXME: this currently happens when we're called from SemaInit |
| 8491 | // when user-conversion overload fails. Figure out how to handle |
| 8492 | // those conditions and diagnose them well. |
| 8493 | return S.NoteOverloadCandidate(Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 8494 | } |
Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8495 | |
| 8496 | case ovl_fail_bad_target: |
| 8497 | return DiagnoseBadTarget(S, Cand); |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8498 | } |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8499 | } |
| 8500 | |
| 8501 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 8502 | // Desugar the type of the surrogate down to a function type, |
| 8503 | // retaining as many typedefs as possible while still showing |
| 8504 | // the function type (and, therefore, its parameter types). |
| 8505 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 8506 | bool isLValueReference = false; |
| 8507 | bool isRValueReference = false; |
| 8508 | bool isPointer = false; |
| 8509 | if (const LValueReferenceType *FnTypeRef = |
| 8510 | FnType->getAs<LValueReferenceType>()) { |
| 8511 | FnType = FnTypeRef->getPointeeType(); |
| 8512 | isLValueReference = true; |
| 8513 | } else if (const RValueReferenceType *FnTypeRef = |
| 8514 | FnType->getAs<RValueReferenceType>()) { |
| 8515 | FnType = FnTypeRef->getPointeeType(); |
| 8516 | isRValueReference = true; |
| 8517 | } |
| 8518 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 8519 | FnType = FnTypePtr->getPointeeType(); |
| 8520 | isPointer = true; |
| 8521 | } |
| 8522 | // Desugar down to a function type. |
| 8523 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 8524 | // Reconstruct the pointer/reference as appropriate. |
| 8525 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 8526 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 8527 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 8528 | |
| 8529 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 8530 | << FnType; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8531 | MaybeEmitInheritedConstructorNote(S, Cand->Surrogate); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8532 | } |
| 8533 | |
| 8534 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 8535 | const char *Opc, |
| 8536 | SourceLocation OpLoc, |
| 8537 | OverloadCandidate *Cand) { |
Benjamin Kramer | b009517 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8538 | assert(Cand->NumConversions <= 2 && "builtin operator is not binary"); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8539 | std::string TypeStr("operator"); |
| 8540 | TypeStr += Opc; |
| 8541 | TypeStr += "("; |
| 8542 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
Benjamin Kramer | b009517 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8543 | if (Cand->NumConversions == 1) { |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8544 | TypeStr += ")"; |
| 8545 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 8546 | } else { |
| 8547 | TypeStr += ", "; |
| 8548 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 8549 | TypeStr += ")"; |
| 8550 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 8551 | } |
| 8552 | } |
| 8553 | |
| 8554 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 8555 | OverloadCandidate *Cand) { |
Benjamin Kramer | b009517 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8556 | unsigned NoOperands = Cand->NumConversions; |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8557 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 8558 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8559 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 8560 | if (!ICS.isAmbiguous()) continue; |
| 8561 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8562 | ICS.DiagnoseAmbiguousConversion(S, OpLoc, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8563 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8564 | } |
| 8565 | } |
| 8566 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8567 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 8568 | if (Cand->Function) |
| 8569 | return Cand->Function->getLocation(); |
John McCall | 982adb5 | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 8570 | if (Cand->IsSurrogate) |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8571 | return Cand->Surrogate->getLocation(); |
| 8572 | return SourceLocation(); |
| 8573 | } |
| 8574 | |
Benjamin Kramer | 8a8051f | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8575 | static unsigned |
| 8576 | RankDeductionFailure(const OverloadCandidate::DeductionFailureInfo &DFI) { |
Chandler Carruth | 73fddfe | 2011-09-10 00:51:24 +0000 | [diff] [blame] | 8577 | switch ((Sema::TemplateDeductionResult)DFI.Result) { |
Kaelyn Uhrain | 45e9370 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8578 | case Sema::TDK_Success: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8579 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
Benjamin Kramer | 8a8051f | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8580 | |
Kaelyn Uhrain | 45e9370 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8581 | case Sema::TDK_Incomplete: |
| 8582 | return 1; |
| 8583 | |
| 8584 | case Sema::TDK_Underqualified: |
| 8585 | case Sema::TDK_Inconsistent: |
| 8586 | return 2; |
| 8587 | |
| 8588 | case Sema::TDK_SubstitutionFailure: |
| 8589 | case Sema::TDK_NonDeducedMismatch: |
| 8590 | return 3; |
| 8591 | |
| 8592 | case Sema::TDK_InstantiationDepth: |
| 8593 | case Sema::TDK_FailedOverloadResolution: |
| 8594 | return 4; |
| 8595 | |
| 8596 | case Sema::TDK_InvalidExplicitArguments: |
| 8597 | return 5; |
| 8598 | |
| 8599 | case Sema::TDK_TooManyArguments: |
| 8600 | case Sema::TDK_TooFewArguments: |
| 8601 | return 6; |
| 8602 | } |
Benjamin Kramer | 8a8051f | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 8603 | llvm_unreachable("Unhandled deduction result"); |
Kaelyn Uhrain | 45e9370 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8604 | } |
| 8605 | |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8606 | struct CompareOverloadCandidatesForDisplay { |
| 8607 | Sema &S; |
| 8608 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8609 | |
| 8610 | bool operator()(const OverloadCandidate *L, |
| 8611 | const OverloadCandidate *R) { |
John McCall | 982adb5 | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 8612 | // Fast-path this check. |
| 8613 | if (L == R) return false; |
| 8614 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8615 | // Order first by viability. |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8616 | if (L->Viable) { |
| 8617 | if (!R->Viable) return true; |
| 8618 | |
| 8619 | // TODO: introduce a tri-valued comparison for overload |
| 8620 | // candidates. Would be more worthwhile if we had a sort |
| 8621 | // that could exploit it. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8622 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation())) return true; |
| 8623 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation())) return false; |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8624 | } else if (R->Viable) |
| 8625 | return false; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8626 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8627 | assert(L->Viable == R->Viable); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8628 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8629 | // Criteria by which we can sort non-viable candidates: |
| 8630 | if (!L->Viable) { |
| 8631 | // 1. Arity mismatches come after other candidates. |
| 8632 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 8633 | L->FailureKind == ovl_fail_too_few_arguments) |
| 8634 | return false; |
| 8635 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 8636 | R->FailureKind == ovl_fail_too_few_arguments) |
| 8637 | return true; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8638 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8639 | // 2. Bad conversions come first and are ordered by the number |
| 8640 | // of bad conversions and quality of good conversions. |
| 8641 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 8642 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 8643 | return true; |
| 8644 | |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8645 | // The conversion that can be fixed with a smaller number of changes, |
| 8646 | // comes first. |
| 8647 | unsigned numLFixes = L->Fix.NumConversionsFixed; |
| 8648 | unsigned numRFixes = R->Fix.NumConversionsFixed; |
| 8649 | numLFixes = (numLFixes == 0) ? UINT_MAX : numLFixes; |
| 8650 | numRFixes = (numRFixes == 0) ? UINT_MAX : numRFixes; |
Anna Zaks | 9ccf84e | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 8651 | if (numLFixes != numRFixes) { |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8652 | if (numLFixes < numRFixes) |
| 8653 | return true; |
| 8654 | else |
| 8655 | return false; |
Anna Zaks | 9ccf84e | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 8656 | } |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8657 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8658 | // If there's any ordering between the defined conversions... |
| 8659 | // FIXME: this might not be transitive. |
Benjamin Kramer | b009517 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8660 | assert(L->NumConversions == R->NumConversions); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8661 | |
| 8662 | int leftBetter = 0; |
John McCall | 21b57fa | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 8663 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
Benjamin Kramer | b009517 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8664 | for (unsigned E = L->NumConversions; I != E; ++I) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8665 | switch (CompareImplicitConversionSequences(S, |
| 8666 | L->Conversions[I], |
| 8667 | R->Conversions[I])) { |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8668 | case ImplicitConversionSequence::Better: |
| 8669 | leftBetter++; |
| 8670 | break; |
| 8671 | |
| 8672 | case ImplicitConversionSequence::Worse: |
| 8673 | leftBetter--; |
| 8674 | break; |
| 8675 | |
| 8676 | case ImplicitConversionSequence::Indistinguishable: |
| 8677 | break; |
| 8678 | } |
| 8679 | } |
| 8680 | if (leftBetter > 0) return true; |
| 8681 | if (leftBetter < 0) return false; |
| 8682 | |
| 8683 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 8684 | return false; |
| 8685 | |
Kaelyn Uhrain | 45e9370 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8686 | if (L->FailureKind == ovl_fail_bad_deduction) { |
| 8687 | if (R->FailureKind != ovl_fail_bad_deduction) |
| 8688 | return true; |
| 8689 | |
| 8690 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 8691 | return RankDeductionFailure(L->DeductionFailure) |
Eli Friedman | 1e7a0c6 | 2011-10-14 23:10:30 +0000 | [diff] [blame] | 8692 | < RankDeductionFailure(R->DeductionFailure); |
Eli Friedman | e2c600c | 2011-10-14 21:52:24 +0000 | [diff] [blame] | 8693 | } else if (R->FailureKind == ovl_fail_bad_deduction) |
| 8694 | return false; |
Kaelyn Uhrain | 45e9370 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 8695 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 8696 | // TODO: others? |
| 8697 | } |
| 8698 | |
| 8699 | // Sort everything else by location. |
| 8700 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 8701 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 8702 | |
| 8703 | // Put candidates without locations (e.g. builtins) at the end. |
| 8704 | if (LLoc.isInvalid()) return false; |
| 8705 | if (RLoc.isInvalid()) return true; |
| 8706 | |
| 8707 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8708 | } |
| 8709 | }; |
| 8710 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8711 | /// CompleteNonViableCandidate - Normally, overload resolution only |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8712 | /// computes up to the first. Produces the FixIt set if possible. |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8713 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8714 | llvm::ArrayRef<Expr *> Args) { |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8715 | assert(!Cand->Viable); |
| 8716 | |
| 8717 | // Don't do anything on failures other than bad conversion. |
| 8718 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 8719 | |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8720 | // We only want the FixIts if all the arguments can be corrected. |
| 8721 | bool Unfixable = false; |
Anna Zaks | 1b06812 | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 8722 | // Use a implicit copy initialization to check conversion fixes. |
| 8723 | Cand->Fix.setConversionChecker(TryCopyInitialization); |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8724 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8725 | // Skip forward to the first bad conversion. |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8726 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
Benjamin Kramer | b009517 | 2012-01-14 16:32:05 +0000 | [diff] [blame] | 8727 | unsigned ConvCount = Cand->NumConversions; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8728 | while (true) { |
| 8729 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 8730 | ConvIdx++; |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8731 | if (Cand->Conversions[ConvIdx - 1].isBad()) { |
Anna Zaks | 1b06812 | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 8732 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx - 1, S); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8733 | break; |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8734 | } |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8735 | } |
| 8736 | |
| 8737 | if (ConvIdx == ConvCount) |
| 8738 | return; |
| 8739 | |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 8740 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 8741 | "remaining conversion is initialized?"); |
| 8742 | |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 8743 | // FIXME: this should probably be preserved from the overload |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8744 | // operation somehow. |
| 8745 | bool SuppressUserConversions = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8746 | |
| 8747 | const FunctionProtoType* Proto; |
| 8748 | unsigned ArgIdx = ConvIdx; |
| 8749 | |
| 8750 | if (Cand->IsSurrogate) { |
| 8751 | QualType ConvType |
| 8752 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 8753 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 8754 | ConvType = ConvPtrType->getPointeeType(); |
| 8755 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 8756 | ArgIdx--; |
| 8757 | } else if (Cand->Function) { |
| 8758 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 8759 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 8760 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 8761 | ArgIdx--; |
| 8762 | } else { |
| 8763 | // Builtin binary operator with a bad first conversion. |
| 8764 | assert(ConvCount <= 3); |
| 8765 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 8766 | Cand->Conversions[ConvIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 8767 | = TryCopyInitialization(S, Args[ConvIdx], |
| 8768 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8769 | SuppressUserConversions, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8770 | /*InOverloadResolution*/ true, |
| 8771 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8772 | S.getLangOpts().ObjCAutoRefCount); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8773 | return; |
| 8774 | } |
| 8775 | |
| 8776 | // Fill in the rest of the conversions. |
| 8777 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 8778 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8779 | if (ArgIdx < NumArgsInProto) { |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8780 | Cand->Conversions[ConvIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 8781 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8782 | SuppressUserConversions, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8783 | /*InOverloadResolution=*/true, |
| 8784 | /*AllowObjCWritebackConversion=*/ |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8785 | S.getLangOpts().ObjCAutoRefCount); |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8786 | // Store the FixIt in the candidate if it exists. |
| 8787 | if (!Unfixable && Cand->Conversions[ConvIdx].isBad()) |
Anna Zaks | 1b06812 | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 8788 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx, S); |
Anna Zaks | df92ddf | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 8789 | } |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8790 | else |
| 8791 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 8792 | } |
| 8793 | } |
| 8794 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8795 | } // end anonymous namespace |
| 8796 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8797 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 8798 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8799 | /// set. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8800 | void OverloadCandidateSet::NoteCandidates(Sema &S, |
| 8801 | OverloadCandidateDisplayKind OCD, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8802 | llvm::ArrayRef<Expr *> Args, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8803 | const char *Opc, |
| 8804 | SourceLocation OpLoc) { |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8805 | // Sort the candidates by viability and position. Sorting directly would |
| 8806 | // be prohibitive, so we make a set of pointers and sort those. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8807 | SmallVector<OverloadCandidate*, 32> Cands; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8808 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
| 8809 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8810 | if (Cand->Viable) |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8811 | Cands.push_back(Cand); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8812 | else if (OCD == OCD_AllCandidates) { |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8813 | CompleteNonViableCandidate(S, Cand, Args); |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8814 | if (Cand->Function || Cand->IsSurrogate) |
| 8815 | Cands.push_back(Cand); |
| 8816 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 8817 | // want to list every possible builtin candidate. |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8818 | } |
| 8819 | } |
| 8820 | |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8821 | std::sort(Cands.begin(), Cands.end(), |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8822 | CompareOverloadCandidatesForDisplay(S)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8823 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8824 | bool ReportedAmbiguousConversions = false; |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8825 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8826 | SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 8827 | const DiagnosticsEngine::OverloadsShown ShowOverloads = |
| 8828 | S.Diags.getShowOverloads(); |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8829 | unsigned CandsShown = 0; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8830 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 8831 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 8832 | |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8833 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 8834 | // the user with. FIXME: This limit should depend on details of the |
| 8835 | // candidate list. |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 8836 | if (CandsShown >= 4 && ShowOverloads == DiagnosticsEngine::Ovl_Best) { |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8837 | break; |
| 8838 | } |
| 8839 | ++CandsShown; |
| 8840 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8841 | if (Cand->Function) |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 8842 | NoteFunctionCandidate(S, Cand, Args.size()); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8843 | else if (Cand->IsSurrogate) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8844 | NoteSurrogateCandidate(S, Cand); |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8845 | else { |
| 8846 | assert(Cand->Viable && |
| 8847 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8848 | // Generally we only see ambiguities including viable builtin |
| 8849 | // operators if overload resolution got screwed up by an |
| 8850 | // ambiguous user-defined conversion. |
| 8851 | // |
| 8852 | // FIXME: It's quite possible for different conversions to see |
| 8853 | // different ambiguities, though. |
| 8854 | if (!ReportedAmbiguousConversions) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8855 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8856 | ReportedAmbiguousConversions = true; |
| 8857 | } |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8858 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8859 | // If this is a viable builtin, print it. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8860 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8861 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8862 | } |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8863 | |
| 8864 | if (I != E) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8865 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8866 | } |
| 8867 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8868 | // [PossiblyAFunctionType] --> [Return] |
| 8869 | // NonFunctionType --> NonFunctionType |
| 8870 | // R (A) --> R(A) |
| 8871 | // R (*)(A) --> R (A) |
| 8872 | // R (&)(A) --> R (A) |
| 8873 | // R (S::*)(A) --> R (A) |
| 8874 | QualType Sema::ExtractUnqualifiedFunctionType(QualType PossiblyAFunctionType) { |
| 8875 | QualType Ret = PossiblyAFunctionType; |
| 8876 | if (const PointerType *ToTypePtr = |
| 8877 | PossiblyAFunctionType->getAs<PointerType>()) |
| 8878 | Ret = ToTypePtr->getPointeeType(); |
| 8879 | else if (const ReferenceType *ToTypeRef = |
| 8880 | PossiblyAFunctionType->getAs<ReferenceType>()) |
| 8881 | Ret = ToTypeRef->getPointeeType(); |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 8882 | else if (const MemberPointerType *MemTypePtr = |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8883 | PossiblyAFunctionType->getAs<MemberPointerType>()) |
| 8884 | Ret = MemTypePtr->getPointeeType(); |
| 8885 | Ret = |
| 8886 | Context.getCanonicalType(Ret).getUnqualifiedType(); |
| 8887 | return Ret; |
| 8888 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 8889 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8890 | // A helper class to help with address of function resolution |
| 8891 | // - allows us to avoid passing around all those ugly parameters |
| 8892 | class AddressOfFunctionResolver |
| 8893 | { |
| 8894 | Sema& S; |
| 8895 | Expr* SourceExpr; |
| 8896 | const QualType& TargetType; |
| 8897 | QualType TargetFunctionType; // Extracted function type from target type |
| 8898 | |
| 8899 | bool Complain; |
| 8900 | //DeclAccessPair& ResultFunctionAccessPair; |
| 8901 | ASTContext& Context; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8902 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8903 | bool TargetTypeIsNonStaticMemberFunction; |
| 8904 | bool FoundNonTemplateFunction; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8905 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8906 | OverloadExpr::FindResult OvlExprInfo; |
| 8907 | OverloadExpr *OvlExpr; |
| 8908 | TemplateArgumentListInfo OvlExplicitTemplateArgs; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8909 | SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8910 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8911 | public: |
| 8912 | AddressOfFunctionResolver(Sema &S, Expr* SourceExpr, |
| 8913 | const QualType& TargetType, bool Complain) |
| 8914 | : S(S), SourceExpr(SourceExpr), TargetType(TargetType), |
| 8915 | Complain(Complain), Context(S.getASTContext()), |
| 8916 | TargetTypeIsNonStaticMemberFunction( |
| 8917 | !!TargetType->getAs<MemberPointerType>()), |
| 8918 | FoundNonTemplateFunction(false), |
| 8919 | OvlExprInfo(OverloadExpr::find(SourceExpr)), |
| 8920 | OvlExpr(OvlExprInfo.Expression) |
| 8921 | { |
| 8922 | ExtractUnqualifiedFunctionTypeFromTargetType(); |
| 8923 | |
| 8924 | if (!TargetFunctionType->isFunctionType()) { |
| 8925 | if (OvlExpr->hasExplicitTemplateArgs()) { |
| 8926 | DeclAccessPair dap; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8927 | if (FunctionDecl* Fn = S.ResolveSingleFunctionTemplateSpecialization( |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8928 | OvlExpr, false, &dap) ) { |
Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 8929 | |
| 8930 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 8931 | if (!Method->isStatic()) { |
| 8932 | // If the target type is a non-function type and the function |
| 8933 | // found is a non-static member function, pretend as if that was |
| 8934 | // the target, it's the only possible type to end up with. |
| 8935 | TargetTypeIsNonStaticMemberFunction = true; |
| 8936 | |
| 8937 | // And skip adding the function if its not in the proper form. |
| 8938 | // We'll diagnose this due to an empty set of functions. |
| 8939 | if (!OvlExprInfo.HasFormOfMemberPointer) |
| 8940 | return; |
| 8941 | } |
| 8942 | } |
| 8943 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8944 | Matches.push_back(std::make_pair(dap,Fn)); |
| 8945 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 8946 | } |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8947 | return; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 8948 | } |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8949 | |
| 8950 | if (OvlExpr->hasExplicitTemplateArgs()) |
| 8951 | OvlExpr->getExplicitTemplateArgs().copyInto(OvlExplicitTemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8952 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8953 | if (FindAllFunctionsThatMatchTargetTypeExactly()) { |
| 8954 | // C++ [over.over]p4: |
| 8955 | // If more than one function is selected, [...] |
| 8956 | if (Matches.size() > 1) { |
| 8957 | if (FoundNonTemplateFunction) |
| 8958 | EliminateAllTemplateMatches(); |
| 8959 | else |
| 8960 | EliminateAllExceptMostSpecializedTemplate(); |
| 8961 | } |
| 8962 | } |
| 8963 | } |
| 8964 | |
| 8965 | private: |
| 8966 | bool isTargetTypeAFunction() const { |
| 8967 | return TargetFunctionType->isFunctionType(); |
| 8968 | } |
| 8969 | |
| 8970 | // [ToType] [Return] |
| 8971 | |
| 8972 | // R (*)(A) --> R (A), IsNonStaticMemberFunction = false |
| 8973 | // R (&)(A) --> R (A), IsNonStaticMemberFunction = false |
| 8974 | // R (S::*)(A) --> R (A), IsNonStaticMemberFunction = true |
| 8975 | void inline ExtractUnqualifiedFunctionTypeFromTargetType() { |
| 8976 | TargetFunctionType = S.ExtractUnqualifiedFunctionType(TargetType); |
| 8977 | } |
| 8978 | |
| 8979 | // return true if any matching specializations were found |
| 8980 | bool AddMatchingTemplateFunction(FunctionTemplateDecl* FunctionTemplate, |
| 8981 | const DeclAccessPair& CurAccessFunPair) { |
| 8982 | if (CXXMethodDecl *Method |
| 8983 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
| 8984 | // Skip non-static function templates when converting to pointer, and |
| 8985 | // static when converting to member pointer. |
| 8986 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 8987 | return false; |
| 8988 | } |
| 8989 | else if (TargetTypeIsNonStaticMemberFunction) |
| 8990 | return false; |
| 8991 | |
| 8992 | // C++ [over.over]p2: |
| 8993 | // If the name is a function template, template argument deduction is |
| 8994 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 8995 | // resulting template argument list is used to generate a single |
| 8996 | // function template specialization, which is added to the set of |
| 8997 | // overloaded functions considered. |
| 8998 | FunctionDecl *Specialization = 0; |
| 8999 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
| 9000 | if (Sema::TemplateDeductionResult Result |
| 9001 | = S.DeduceTemplateArguments(FunctionTemplate, |
| 9002 | &OvlExplicitTemplateArgs, |
| 9003 | TargetFunctionType, Specialization, |
| 9004 | Info)) { |
| 9005 | // FIXME: make a note of the failed deduction for diagnostics. |
| 9006 | (void)Result; |
| 9007 | return false; |
| 9008 | } |
| 9009 | |
| 9010 | // Template argument deduction ensures that we have an exact match. |
| 9011 | // This function template specicalization works. |
| 9012 | Specialization = cast<FunctionDecl>(Specialization->getCanonicalDecl()); |
| 9013 | assert(TargetFunctionType |
| 9014 | == Context.getCanonicalType(Specialization->getType())); |
| 9015 | Matches.push_back(std::make_pair(CurAccessFunPair, Specialization)); |
| 9016 | return true; |
| 9017 | } |
| 9018 | |
| 9019 | bool AddMatchingNonTemplateFunction(NamedDecl* Fn, |
| 9020 | const DeclAccessPair& CurAccessFunPair) { |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9021 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 9022 | // Skip non-static functions when converting to pointer, and static |
| 9023 | // when converting to member pointer. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9024 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 9025 | return false; |
| 9026 | } |
| 9027 | else if (TargetTypeIsNonStaticMemberFunction) |
| 9028 | return false; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9029 | |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 9030 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9031 | if (S.getLangOpts().CUDA) |
Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 9032 | if (FunctionDecl *Caller = dyn_cast<FunctionDecl>(S.CurContext)) |
| 9033 | if (S.CheckCUDATarget(Caller, FunDecl)) |
| 9034 | return false; |
| 9035 | |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 9036 | QualType ResultTy; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9037 | if (Context.hasSameUnqualifiedType(TargetFunctionType, |
| 9038 | FunDecl->getType()) || |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 9039 | S.IsNoReturnConversion(FunDecl->getType(), TargetFunctionType, |
| 9040 | ResultTy)) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9041 | Matches.push_back(std::make_pair(CurAccessFunPair, |
| 9042 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 9043 | FoundNonTemplateFunction = true; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9044 | return true; |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 9045 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9046 | } |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9047 | |
| 9048 | return false; |
| 9049 | } |
| 9050 | |
| 9051 | bool FindAllFunctionsThatMatchTargetTypeExactly() { |
| 9052 | bool Ret = false; |
| 9053 | |
| 9054 | // If the overload expression doesn't have the form of a pointer to |
| 9055 | // member, don't try to convert it to a pointer-to-member type. |
| 9056 | if (IsInvalidFormOfPointerToMemberFunction()) |
| 9057 | return false; |
| 9058 | |
| 9059 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 9060 | E = OvlExpr->decls_end(); |
| 9061 | I != E; ++I) { |
| 9062 | // Look through any using declarations to find the underlying function. |
| 9063 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 9064 | |
| 9065 | // C++ [over.over]p3: |
| 9066 | // Non-member functions and static member functions match |
| 9067 | // targets of type "pointer-to-function" or "reference-to-function." |
| 9068 | // Nonstatic member functions match targets of |
| 9069 | // type "pointer-to-member-function." |
| 9070 | // Note that according to DR 247, the containing class does not matter. |
| 9071 | if (FunctionTemplateDecl *FunctionTemplate |
| 9072 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
| 9073 | if (AddMatchingTemplateFunction(FunctionTemplate, I.getPair())) |
| 9074 | Ret = true; |
| 9075 | } |
| 9076 | // If we have explicit template arguments supplied, skip non-templates. |
| 9077 | else if (!OvlExpr->hasExplicitTemplateArgs() && |
| 9078 | AddMatchingNonTemplateFunction(Fn, I.getPair())) |
| 9079 | Ret = true; |
| 9080 | } |
| 9081 | assert(Ret || Matches.empty()); |
| 9082 | return Ret; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9083 | } |
| 9084 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9085 | void EliminateAllExceptMostSpecializedTemplate() { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 9086 | // [...] and any given function template specialization F1 is |
| 9087 | // eliminated if the set contains a second function template |
| 9088 | // specialization whose function template is more specialized |
| 9089 | // than the function template of F1 according to the partial |
| 9090 | // ordering rules of 14.5.5.2. |
| 9091 | |
| 9092 | // The algorithm specified above is quadratic. We instead use a |
| 9093 | // two-pass algorithm (similar to the one used to identify the |
| 9094 | // best viable function in an overload set) that identifies the |
| 9095 | // best function template (if it exists). |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9096 | |
| 9097 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 9098 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 9099 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9100 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9101 | UnresolvedSetIterator Result = |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9102 | S.getMostSpecialized(MatchesCopy.begin(), MatchesCopy.end(), |
| 9103 | TPOC_Other, 0, SourceExpr->getLocStart(), |
| 9104 | S.PDiag(), |
| 9105 | S.PDiag(diag::err_addr_ovl_ambiguous) |
| 9106 | << Matches[0].second->getDeclName(), |
| 9107 | S.PDiag(diag::note_ovl_candidate) |
| 9108 | << (unsigned) oc_function_template, |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9109 | Complain, TargetFunctionType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9110 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9111 | if (Result != MatchesCopy.end()) { |
| 9112 | // Make it the first and only element |
| 9113 | Matches[0].first = Matches[Result - MatchesCopy.begin()].first; |
| 9114 | Matches[0].second = cast<FunctionDecl>(*Result); |
| 9115 | Matches.resize(1); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9116 | } |
| 9117 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9118 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9119 | void EliminateAllTemplateMatches() { |
| 9120 | // [...] any function template specializations in the set are |
| 9121 | // eliminated if the set also contains a non-template function, [...] |
| 9122 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
| 9123 | if (Matches[I].second->getPrimaryTemplate() == 0) |
| 9124 | ++I; |
| 9125 | else { |
| 9126 | Matches[I] = Matches[--N]; |
| 9127 | Matches.set_size(N); |
| 9128 | } |
| 9129 | } |
| 9130 | } |
| 9131 | |
| 9132 | public: |
| 9133 | void ComplainNoMatchesFound() const { |
| 9134 | assert(Matches.empty()); |
| 9135 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_no_viable) |
| 9136 | << OvlExpr->getName() << TargetFunctionType |
| 9137 | << OvlExpr->getSourceRange(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9138 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9139 | } |
| 9140 | |
| 9141 | bool IsInvalidFormOfPointerToMemberFunction() const { |
| 9142 | return TargetTypeIsNonStaticMemberFunction && |
| 9143 | !OvlExprInfo.HasFormOfMemberPointer; |
| 9144 | } |
| 9145 | |
| 9146 | void ComplainIsInvalidFormOfPointerToMemberFunction() const { |
| 9147 | // TODO: Should we condition this on whether any functions might |
| 9148 | // have matched, or is it more appropriate to do that in callers? |
| 9149 | // TODO: a fixit wouldn't hurt. |
| 9150 | S.Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
| 9151 | << TargetType << OvlExpr->getSourceRange(); |
| 9152 | } |
| 9153 | |
| 9154 | void ComplainOfInvalidConversion() const { |
| 9155 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 9156 | << OvlExpr->getName() << TargetType; |
| 9157 | } |
| 9158 | |
| 9159 | void ComplainMultipleMatchesFound() const { |
| 9160 | assert(Matches.size() > 1); |
| 9161 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 9162 | << OvlExpr->getName() |
| 9163 | << OvlExpr->getSourceRange(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9164 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9165 | } |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9166 | |
| 9167 | bool hadMultipleCandidates() const { return (OvlExpr->getNumDecls() > 1); } |
| 9168 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9169 | int getNumMatches() const { return Matches.size(); } |
| 9170 | |
| 9171 | FunctionDecl* getMatchingFunctionDecl() const { |
| 9172 | if (Matches.size() != 1) return 0; |
| 9173 | return Matches[0].second; |
| 9174 | } |
| 9175 | |
| 9176 | const DeclAccessPair* getMatchingFunctionAccessPair() const { |
| 9177 | if (Matches.size() != 1) return 0; |
| 9178 | return &Matches[0].first; |
| 9179 | } |
| 9180 | }; |
| 9181 | |
| 9182 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 9183 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 9184 | /// expression with overloaded function type and @p ToType is the type |
| 9185 | /// we're trying to resolve to. For example: |
| 9186 | /// |
| 9187 | /// @code |
| 9188 | /// int f(double); |
| 9189 | /// int f(int); |
| 9190 | /// |
| 9191 | /// int (*pfd)(double) = f; // selects f(double) |
| 9192 | /// @endcode |
| 9193 | /// |
| 9194 | /// This routine returns the resulting FunctionDecl if it could be |
| 9195 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 9196 | /// routine will emit diagnostics if there is an error. |
| 9197 | FunctionDecl * |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9198 | Sema::ResolveAddressOfOverloadedFunction(Expr *AddressOfExpr, |
| 9199 | QualType TargetType, |
| 9200 | bool Complain, |
| 9201 | DeclAccessPair &FoundResult, |
| 9202 | bool *pHadMultipleCandidates) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9203 | assert(AddressOfExpr->getType() == Context.OverloadTy); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9204 | |
| 9205 | AddressOfFunctionResolver Resolver(*this, AddressOfExpr, TargetType, |
| 9206 | Complain); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9207 | int NumMatches = Resolver.getNumMatches(); |
| 9208 | FunctionDecl* Fn = 0; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9209 | if (NumMatches == 0 && Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9210 | if (Resolver.IsInvalidFormOfPointerToMemberFunction()) |
| 9211 | Resolver.ComplainIsInvalidFormOfPointerToMemberFunction(); |
| 9212 | else |
| 9213 | Resolver.ComplainNoMatchesFound(); |
| 9214 | } |
| 9215 | else if (NumMatches > 1 && Complain) |
| 9216 | Resolver.ComplainMultipleMatchesFound(); |
| 9217 | else if (NumMatches == 1) { |
| 9218 | Fn = Resolver.getMatchingFunctionDecl(); |
| 9219 | assert(Fn); |
| 9220 | FoundResult = *Resolver.getMatchingFunctionAccessPair(); |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9221 | MarkFunctionReferenced(AddressOfExpr->getLocStart(), Fn); |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 9222 | if (Complain) |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9223 | CheckAddressOfMemberAccess(AddressOfExpr, FoundResult); |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 9224 | } |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 9225 | |
| 9226 | if (pHadMultipleCandidates) |
| 9227 | *pHadMultipleCandidates = Resolver.hadMultipleCandidates(); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9228 | return Fn; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9229 | } |
| 9230 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9231 | /// \brief Given an expression that refers to an overloaded function, try to |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9232 | /// resolve that overloaded function expression down to a single function. |
| 9233 | /// |
| 9234 | /// This routine can only resolve template-ids that refer to a single function |
| 9235 | /// template, where that template-id refers to a single template whose template |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9236 | /// arguments are either provided by the template-id or have defaults, |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9237 | /// as described in C++0x [temp.arg.explicit]p3. |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9238 | FunctionDecl * |
| 9239 | Sema::ResolveSingleFunctionTemplateSpecialization(OverloadExpr *ovl, |
| 9240 | bool Complain, |
| 9241 | DeclAccessPair *FoundResult) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9242 | // C++ [over.over]p1: |
| 9243 | // [...] [Note: any redundant set of parentheses surrounding the |
| 9244 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9245 | // C++ [over.over]p1: |
| 9246 | // [...] The overloaded function name can be preceded by the & |
| 9247 | // operator. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9248 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9249 | // If we didn't actually find any template-ids, we're done. |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9250 | if (!ovl->hasExplicitTemplateArgs()) |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9251 | return 0; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 9252 | |
| 9253 | TemplateArgumentListInfo ExplicitTemplateArgs; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9254 | ovl->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9255 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9256 | // Look through all of the overloaded functions, searching for one |
| 9257 | // whose type matches exactly. |
| 9258 | FunctionDecl *Matched = 0; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9259 | for (UnresolvedSetIterator I = ovl->decls_begin(), |
| 9260 | E = ovl->decls_end(); I != E; ++I) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9261 | // C++0x [temp.arg.explicit]p3: |
| 9262 | // [...] In contexts where deduction is done and fails, or in contexts |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9263 | // where deduction is not done, if a template argument list is |
| 9264 | // specified and it, along with any default template arguments, |
| 9265 | // identifies a single function template specialization, then the |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9266 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | eebe721 | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 9267 | FunctionTemplateDecl *FunctionTemplate |
| 9268 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9269 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9270 | // C++ [over.over]p2: |
| 9271 | // If the name is a function template, template argument deduction is |
| 9272 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 9273 | // resulting template argument list is used to generate a single |
| 9274 | // function template specialization, which is added to the set of |
| 9275 | // overloaded functions considered. |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9276 | FunctionDecl *Specialization = 0; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9277 | TemplateDeductionInfo Info(Context, ovl->getNameLoc()); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9278 | if (TemplateDeductionResult Result |
| 9279 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 9280 | Specialization, Info)) { |
| 9281 | // FIXME: make a note of the failed deduction for diagnostics. |
| 9282 | (void)Result; |
| 9283 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9284 | } |
| 9285 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9286 | assert(Specialization && "no specialization and no error?"); |
| 9287 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9288 | // Multiple matches; we can't resolve to a single declaration. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9289 | if (Matched) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9290 | if (Complain) { |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9291 | Diag(ovl->getExprLoc(), diag::err_addr_ovl_ambiguous) |
| 9292 | << ovl->getName(); |
| 9293 | NoteAllOverloadCandidates(ovl); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9294 | } |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9295 | return 0; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9296 | } |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 9297 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9298 | Matched = Specialization; |
| 9299 | if (FoundResult) *FoundResult = I.getPair(); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9300 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9301 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 9302 | return Matched; |
| 9303 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9304 | |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9305 | |
| 9306 | |
| 9307 | |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9308 | // Resolve and fix an overloaded expression that can be resolved |
| 9309 | // because it identifies a single function template specialization. |
| 9310 | // |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9311 | // Last three arguments should only be supplied if Complain = true |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9312 | // |
| 9313 | // Return true if it was logically possible to so resolve the |
| 9314 | // expression, regardless of whether or not it succeeded. Always |
| 9315 | // returns true if 'complain' is set. |
| 9316 | bool Sema::ResolveAndFixSingleFunctionTemplateSpecialization( |
| 9317 | ExprResult &SrcExpr, bool doFunctionPointerConverion, |
| 9318 | bool complain, const SourceRange& OpRangeForComplaining, |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9319 | QualType DestTypeForComplaining, |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9320 | unsigned DiagIDForComplaining) { |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9321 | assert(SrcExpr.get()->getType() == Context.OverloadTy); |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9322 | |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9323 | OverloadExpr::FindResult ovl = OverloadExpr::find(SrcExpr.get()); |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9324 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9325 | DeclAccessPair found; |
| 9326 | ExprResult SingleFunctionExpression; |
| 9327 | if (FunctionDecl *fn = ResolveSingleFunctionTemplateSpecialization( |
| 9328 | ovl.Expression, /*complain*/ false, &found)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9329 | if (DiagnoseUseOfDecl(fn, SrcExpr.get()->getLocStart())) { |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9330 | SrcExpr = ExprError(); |
| 9331 | return true; |
| 9332 | } |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9333 | |
| 9334 | // It is only correct to resolve to an instance method if we're |
| 9335 | // resolving a form that's permitted to be a pointer to member. |
| 9336 | // Otherwise we'll end up making a bound member expression, which |
| 9337 | // is illegal in all the contexts we resolve like this. |
| 9338 | if (!ovl.HasFormOfMemberPointer && |
| 9339 | isa<CXXMethodDecl>(fn) && |
| 9340 | cast<CXXMethodDecl>(fn)->isInstance()) { |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9341 | if (!complain) return false; |
| 9342 | |
| 9343 | Diag(ovl.Expression->getExprLoc(), |
| 9344 | diag::err_bound_member_function) |
| 9345 | << 0 << ovl.Expression->getSourceRange(); |
| 9346 | |
| 9347 | // TODO: I believe we only end up here if there's a mix of |
| 9348 | // static and non-static candidates (otherwise the expression |
| 9349 | // would have 'bound member' type, not 'overload' type). |
| 9350 | // Ideally we would note which candidate was chosen and why |
| 9351 | // the static candidates were rejected. |
| 9352 | SrcExpr = ExprError(); |
| 9353 | return true; |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9354 | } |
Douglas Gregor | 89f3cd5 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 9355 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9356 | // Fix the expresion to refer to 'fn'. |
| 9357 | SingleFunctionExpression = |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9358 | Owned(FixOverloadedFunctionReference(SrcExpr.take(), found, fn)); |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9359 | |
| 9360 | // If desired, do function-to-pointer decay. |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9361 | if (doFunctionPointerConverion) { |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9362 | SingleFunctionExpression = |
| 9363 | DefaultFunctionArrayLvalueConversion(SingleFunctionExpression.take()); |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9364 | if (SingleFunctionExpression.isInvalid()) { |
| 9365 | SrcExpr = ExprError(); |
| 9366 | return true; |
| 9367 | } |
| 9368 | } |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9369 | } |
| 9370 | |
| 9371 | if (!SingleFunctionExpression.isUsable()) { |
| 9372 | if (complain) { |
| 9373 | Diag(OpRangeForComplaining.getBegin(), DiagIDForComplaining) |
| 9374 | << ovl.Expression->getName() |
| 9375 | << DestTypeForComplaining |
| 9376 | << OpRangeForComplaining |
| 9377 | << ovl.Expression->getQualifierLoc().getSourceRange(); |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9378 | NoteAllOverloadCandidates(SrcExpr.get()); |
| 9379 | |
| 9380 | SrcExpr = ExprError(); |
| 9381 | return true; |
| 9382 | } |
| 9383 | |
| 9384 | return false; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9385 | } |
| 9386 | |
John McCall | 50a2c2c | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 9387 | SrcExpr = SingleFunctionExpression; |
| 9388 | return true; |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 9389 | } |
| 9390 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9391 | /// \brief Add a single candidate to the overload set. |
| 9392 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9393 | DeclAccessPair FoundDecl, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 9394 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9395 | llvm::ArrayRef<Expr *> Args, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9396 | OverloadCandidateSet &CandidateSet, |
Richard Smith | 95ce4f6 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9397 | bool PartialOverloading, |
| 9398 | bool KnownValid) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9399 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9400 | if (isa<UsingShadowDecl>(Callee)) |
| 9401 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 9402 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9403 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
Richard Smith | 95ce4f6 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9404 | if (ExplicitTemplateArgs) { |
| 9405 | assert(!KnownValid && "Explicit template arguments?"); |
| 9406 | return; |
| 9407 | } |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9408 | S.AddOverloadCandidate(Func, FoundDecl, Args, CandidateSet, false, |
| 9409 | PartialOverloading); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9410 | return; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9411 | } |
| 9412 | |
| 9413 | if (FunctionTemplateDecl *FuncTemplate |
| 9414 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9415 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9416 | ExplicitTemplateArgs, Args, CandidateSet); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9417 | return; |
| 9418 | } |
| 9419 | |
Richard Smith | 95ce4f6 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9420 | assert(!KnownValid && "unhandled case in overloaded call candidate"); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9421 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9422 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9423 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 9424 | /// dependent lookup to the given overload set. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9425 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9426 | llvm::ArrayRef<Expr *> Args, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9427 | OverloadCandidateSet &CandidateSet, |
| 9428 | bool PartialOverloading) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9429 | |
| 9430 | #ifndef NDEBUG |
| 9431 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 9432 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9433 | // |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9434 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 9435 | // and let Y be the lookup set produced by argument dependent |
| 9436 | // lookup (defined as follows). If X contains |
| 9437 | // |
| 9438 | // -- a declaration of a class member, or |
| 9439 | // |
| 9440 | // -- a block-scope function declaration that is not a |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9441 | // using-declaration, or |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9442 | // |
| 9443 | // -- a declaration that is neither a function or a function |
| 9444 | // template |
| 9445 | // |
| 9446 | // then Y is empty. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9447 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9448 | if (ULE->requiresADL()) { |
| 9449 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 9450 | E = ULE->decls_end(); I != E; ++I) { |
| 9451 | assert(!(*I)->getDeclContext()->isRecord()); |
| 9452 | assert(isa<UsingShadowDecl>(*I) || |
| 9453 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 9454 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9455 | } |
| 9456 | } |
| 9457 | #endif |
| 9458 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9459 | // It would be nice to avoid this copy. |
| 9460 | TemplateArgumentListInfo TABuffer; |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 9461 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9462 | if (ULE->hasExplicitTemplateArgs()) { |
| 9463 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 9464 | ExplicitTemplateArgs = &TABuffer; |
| 9465 | } |
| 9466 | |
| 9467 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 9468 | E = ULE->decls_end(); I != E; ++I) |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9469 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, Args, |
| 9470 | CandidateSet, PartialOverloading, |
| 9471 | /*KnownValid*/ true); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9472 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9473 | if (ULE->requiresADL()) |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9474 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
Richard Smith | e06a2c1 | 2012-02-25 06:24:24 +0000 | [diff] [blame] | 9475 | ULE->getExprLoc(), |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9476 | Args, ExplicitTemplateArgs, |
| 9477 | CandidateSet, PartialOverloading, |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 9478 | ULE->isStdAssociatedNamespace()); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 9479 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9480 | |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9481 | /// Attempt to recover from an ill-formed use of a non-dependent name in a |
| 9482 | /// template, where the non-dependent name was declared after the template |
| 9483 | /// was defined. This is common in code written for a compilers which do not |
| 9484 | /// correctly implement two-stage name lookup. |
| 9485 | /// |
| 9486 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 9487 | static bool |
| 9488 | DiagnoseTwoPhaseLookup(Sema &SemaRef, SourceLocation FnLoc, |
| 9489 | const CXXScopeSpec &SS, LookupResult &R, |
| 9490 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9491 | llvm::ArrayRef<Expr *> Args) { |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9492 | if (SemaRef.ActiveTemplateInstantiations.empty() || !SS.isEmpty()) |
| 9493 | return false; |
| 9494 | |
| 9495 | for (DeclContext *DC = SemaRef.CurContext; DC; DC = DC->getParent()) { |
Nick Lewycky | fcd5e7a | 2012-03-14 20:41:00 +0000 | [diff] [blame] | 9496 | if (DC->isTransparentContext()) |
| 9497 | continue; |
| 9498 | |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9499 | SemaRef.LookupQualifiedName(R, DC); |
| 9500 | |
| 9501 | if (!R.empty()) { |
| 9502 | R.suppressDiagnostics(); |
| 9503 | |
| 9504 | if (isa<CXXRecordDecl>(DC)) { |
| 9505 | // Don't diagnose names we find in classes; we get much better |
| 9506 | // diagnostics for these from DiagnoseEmptyLookup. |
| 9507 | R.clear(); |
| 9508 | return false; |
| 9509 | } |
| 9510 | |
| 9511 | OverloadCandidateSet Candidates(FnLoc); |
| 9512 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 9513 | AddOverloadedCallCandidate(SemaRef, I.getPair(), |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9514 | ExplicitTemplateArgs, Args, |
Richard Smith | 95ce4f6 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9515 | Candidates, false, /*KnownValid*/ false); |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9516 | |
| 9517 | OverloadCandidateSet::iterator Best; |
Richard Smith | 95ce4f6 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9518 | if (Candidates.BestViableFunction(SemaRef, FnLoc, Best) != OR_Success) { |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9519 | // No viable functions. Don't bother the user with notes for functions |
| 9520 | // which don't work and shouldn't be found anyway. |
Richard Smith | 95ce4f6 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9521 | R.clear(); |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9522 | return false; |
Richard Smith | 95ce4f6 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 9523 | } |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9524 | |
| 9525 | // Find the namespaces where ADL would have looked, and suggest |
| 9526 | // declaring the function there instead. |
| 9527 | Sema::AssociatedNamespaceSet AssociatedNamespaces; |
| 9528 | Sema::AssociatedClassSet AssociatedClasses; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9529 | SemaRef.FindAssociatedClassesAndNamespaces(Args, |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9530 | AssociatedNamespaces, |
| 9531 | AssociatedClasses); |
| 9532 | // Never suggest declaring a function within namespace 'std'. |
Chandler Carruth | d50f169 | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9533 | Sema::AssociatedNamespaceSet SuggestedNamespaces; |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9534 | if (DeclContext *Std = SemaRef.getStdNamespace()) { |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9535 | for (Sema::AssociatedNamespaceSet::iterator |
| 9536 | it = AssociatedNamespaces.begin(), |
Chandler Carruth | d50f169 | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9537 | end = AssociatedNamespaces.end(); it != end; ++it) { |
| 9538 | if (!Std->Encloses(*it)) |
| 9539 | SuggestedNamespaces.insert(*it); |
| 9540 | } |
Chandler Carruth | d54186a | 2011-06-08 10:13:17 +0000 | [diff] [blame] | 9541 | } else { |
| 9542 | // Lacking the 'std::' namespace, use all of the associated namespaces. |
| 9543 | SuggestedNamespaces = AssociatedNamespaces; |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9544 | } |
| 9545 | |
| 9546 | SemaRef.Diag(R.getNameLoc(), diag::err_not_found_by_two_phase_lookup) |
| 9547 | << R.getLookupName(); |
Chandler Carruth | d50f169 | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9548 | if (SuggestedNamespaces.empty()) { |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9549 | SemaRef.Diag(Best->Function->getLocation(), |
| 9550 | diag::note_not_found_by_two_phase_lookup) |
| 9551 | << R.getLookupName() << 0; |
Chandler Carruth | d50f169 | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9552 | } else if (SuggestedNamespaces.size() == 1) { |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9553 | SemaRef.Diag(Best->Function->getLocation(), |
| 9554 | diag::note_not_found_by_two_phase_lookup) |
Chandler Carruth | d50f169 | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 9555 | << R.getLookupName() << 1 << *SuggestedNamespaces.begin(); |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9556 | } else { |
| 9557 | // FIXME: It would be useful to list the associated namespaces here, |
| 9558 | // but the diagnostics infrastructure doesn't provide a way to produce |
| 9559 | // a localized representation of a list of items. |
| 9560 | SemaRef.Diag(Best->Function->getLocation(), |
| 9561 | diag::note_not_found_by_two_phase_lookup) |
| 9562 | << R.getLookupName() << 2; |
| 9563 | } |
| 9564 | |
| 9565 | // Try to recover by calling this function. |
| 9566 | return true; |
| 9567 | } |
| 9568 | |
| 9569 | R.clear(); |
| 9570 | } |
| 9571 | |
| 9572 | return false; |
| 9573 | } |
| 9574 | |
| 9575 | /// Attempt to recover from ill-formed use of a non-dependent operator in a |
| 9576 | /// template, where the non-dependent operator was declared after the template |
| 9577 | /// was defined. |
| 9578 | /// |
| 9579 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 9580 | static bool |
| 9581 | DiagnoseTwoPhaseOperatorLookup(Sema &SemaRef, OverloadedOperatorKind Op, |
| 9582 | SourceLocation OpLoc, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9583 | llvm::ArrayRef<Expr *> Args) { |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9584 | DeclarationName OpName = |
| 9585 | SemaRef.Context.DeclarationNames.getCXXOperatorName(Op); |
| 9586 | LookupResult R(SemaRef, OpName, OpLoc, Sema::LookupOperatorName); |
| 9587 | return DiagnoseTwoPhaseLookup(SemaRef, OpLoc, CXXScopeSpec(), R, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9588 | /*ExplicitTemplateArgs=*/0, Args); |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9589 | } |
| 9590 | |
Kaelyn Uhrain | 8edb17d | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 9591 | namespace { |
| 9592 | // Callback to limit the allowed keywords and to only accept typo corrections |
| 9593 | // that are keywords or whose decls refer to functions (or template functions) |
| 9594 | // that accept the given number of arguments. |
| 9595 | class RecoveryCallCCC : public CorrectionCandidateCallback { |
| 9596 | public: |
| 9597 | RecoveryCallCCC(Sema &SemaRef, unsigned NumArgs, bool HasExplicitTemplateArgs) |
| 9598 | : NumArgs(NumArgs), HasExplicitTemplateArgs(HasExplicitTemplateArgs) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9599 | WantTypeSpecifiers = SemaRef.getLangOpts().CPlusPlus; |
Kaelyn Uhrain | 8edb17d | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 9600 | WantRemainingKeywords = false; |
| 9601 | } |
| 9602 | |
| 9603 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 9604 | if (!candidate.getCorrectionDecl()) |
| 9605 | return candidate.isKeyword(); |
| 9606 | |
| 9607 | for (TypoCorrection::const_decl_iterator DI = candidate.begin(), |
| 9608 | DIEnd = candidate.end(); DI != DIEnd; ++DI) { |
| 9609 | FunctionDecl *FD = 0; |
| 9610 | NamedDecl *ND = (*DI)->getUnderlyingDecl(); |
| 9611 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 9612 | FD = FTD->getTemplatedDecl(); |
| 9613 | if (!HasExplicitTemplateArgs && !FD) { |
| 9614 | if (!(FD = dyn_cast<FunctionDecl>(ND)) && isa<ValueDecl>(ND)) { |
| 9615 | // If the Decl is neither a function nor a template function, |
| 9616 | // determine if it is a pointer or reference to a function. If so, |
| 9617 | // check against the number of arguments expected for the pointee. |
| 9618 | QualType ValType = cast<ValueDecl>(ND)->getType(); |
| 9619 | if (ValType->isAnyPointerType() || ValType->isReferenceType()) |
| 9620 | ValType = ValType->getPointeeType(); |
| 9621 | if (const FunctionProtoType *FPT = ValType->getAs<FunctionProtoType>()) |
| 9622 | if (FPT->getNumArgs() == NumArgs) |
| 9623 | return true; |
| 9624 | } |
| 9625 | } |
| 9626 | if (FD && FD->getNumParams() >= NumArgs && |
| 9627 | FD->getMinRequiredArguments() <= NumArgs) |
| 9628 | return true; |
| 9629 | } |
| 9630 | return false; |
| 9631 | } |
| 9632 | |
| 9633 | private: |
| 9634 | unsigned NumArgs; |
| 9635 | bool HasExplicitTemplateArgs; |
| 9636 | }; |
Kaelyn Uhrain | 9afaf79 | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 9637 | |
| 9638 | // Callback that effectively disabled typo correction |
| 9639 | class NoTypoCorrectionCCC : public CorrectionCandidateCallback { |
| 9640 | public: |
| 9641 | NoTypoCorrectionCCC() { |
| 9642 | WantTypeSpecifiers = false; |
| 9643 | WantExpressionKeywords = false; |
| 9644 | WantCXXNamedCasts = false; |
| 9645 | WantRemainingKeywords = false; |
| 9646 | } |
| 9647 | |
| 9648 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 9649 | return false; |
| 9650 | } |
| 9651 | }; |
Kaelyn Uhrain | 8edb17d | 2012-01-25 18:37:44 +0000 | [diff] [blame] | 9652 | } |
| 9653 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9654 | /// Attempts to recover from a call where no functions were found. |
| 9655 | /// |
| 9656 | /// Returns true if new candidates were found. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9657 | static ExprResult |
Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 9658 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9659 | UnresolvedLookupExpr *ULE, |
| 9660 | SourceLocation LParenLoc, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9661 | llvm::MutableArrayRef<Expr *> Args, |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9662 | SourceLocation RParenLoc, |
Kaelyn Uhrain | 9afaf79 | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 9663 | bool EmptyLookup, bool AllowTypoCorrection) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9664 | |
| 9665 | CXXScopeSpec SS; |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 9666 | SS.Adopt(ULE->getQualifierLoc()); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 9667 | SourceLocation TemplateKWLoc = ULE->getTemplateKeywordLoc(); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9668 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9669 | TemplateArgumentListInfo TABuffer; |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9670 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9671 | if (ULE->hasExplicitTemplateArgs()) { |
| 9672 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 9673 | ExplicitTemplateArgs = &TABuffer; |
| 9674 | } |
| 9675 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9676 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 9677 | Sema::LookupOrdinaryName); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9678 | RecoveryCallCCC Validator(SemaRef, Args.size(), ExplicitTemplateArgs != 0); |
Kaelyn Uhrain | 9afaf79 | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 9679 | NoTypoCorrectionCCC RejectAll; |
| 9680 | CorrectionCandidateCallback *CCC = AllowTypoCorrection ? |
| 9681 | (CorrectionCandidateCallback*)&Validator : |
| 9682 | (CorrectionCandidateCallback*)&RejectAll; |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9683 | if (!DiagnoseTwoPhaseLookup(SemaRef, Fn->getExprLoc(), SS, R, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9684 | ExplicitTemplateArgs, Args) && |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9685 | (!EmptyLookup || |
Kaelyn Uhrain | 9afaf79 | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 9686 | SemaRef.DiagnoseEmptyLookup(S, SS, R, *CCC, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9687 | ExplicitTemplateArgs, Args))) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9688 | return ExprError(); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9689 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9690 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 9691 | |
| 9692 | // Build an implicit member call if appropriate. Just drop the |
| 9693 | // casts and such from the call, we don't really care. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9694 | ExprResult NewFn = ExprError(); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9695 | if ((*R.begin())->isCXXClassMember()) |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 9696 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, |
| 9697 | R, ExplicitTemplateArgs); |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 9698 | else if (ExplicitTemplateArgs || TemplateKWLoc.isValid()) |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 9699 | NewFn = SemaRef.BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 9700 | ExplicitTemplateArgs); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9701 | else |
| 9702 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 9703 | |
| 9704 | if (NewFn.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9705 | return ExprError(); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9706 | |
| 9707 | // This shouldn't cause an infinite loop because we're giving it |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9708 | // an expression with viable lookup results, which should never |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9709 | // end up here. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9710 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, NewFn.take(), LParenLoc, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9711 | MultiExprArg(Args.data(), Args.size()), |
| 9712 | RParenLoc); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9713 | } |
Douglas Gregor | 4038cf4 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 9714 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9715 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 9716 | /// (which eventually refers to the declaration Func) and the call |
| 9717 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 9718 | /// to a specific function. If overload resolution succeeds, returns |
| 9719 | /// the function declaration produced by overload |
Douglas Gregor | a60a691 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 9720 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9721 | /// arguments and Fn, and returns NULL. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9722 | ExprResult |
Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 9723 | Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, UnresolvedLookupExpr *ULE, |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9724 | SourceLocation LParenLoc, |
| 9725 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 9726 | SourceLocation RParenLoc, |
Kaelyn Uhrain | 9afaf79 | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 9727 | Expr *ExecConfig, |
| 9728 | bool AllowTypoCorrection) { |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9729 | #ifndef NDEBUG |
| 9730 | if (ULE->requiresADL()) { |
| 9731 | // To do ADL, we must have found an unqualified name. |
| 9732 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 9733 | |
| 9734 | // We don't perform ADL for implicit declarations of builtins. |
| 9735 | // Verify that this was correctly set up. |
| 9736 | FunctionDecl *F; |
| 9737 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 9738 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 9739 | F->getBuiltinID() && F->isImplicit()) |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 9740 | llvm_unreachable("performing ADL for builtin"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9741 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9742 | // We don't perform ADL in C. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9743 | assert(getLangOpts().CPlusPlus && "ADL enabled in C"); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 9744 | } else |
| 9745 | assert(!ULE->isStdAssociatedNamespace() && |
| 9746 | "std is associated namespace but not doing ADL"); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9747 | #endif |
| 9748 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9749 | UnbridgedCastsSet UnbridgedCasts; |
| 9750 | if (checkArgPlaceholdersForOverload(*this, Args, NumArgs, UnbridgedCasts)) |
| 9751 | return ExprError(); |
| 9752 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9753 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 9754 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9755 | // Add the functions denoted by the callee to the set of candidate |
| 9756 | // functions, including those from argument-dependent lookup. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9757 | AddOverloadedCallCandidates(ULE, llvm::makeArrayRef(Args, NumArgs), |
| 9758 | CandidateSet); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9759 | |
| 9760 | // If we found nothing, try to recover. |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9761 | // BuildRecoveryCallExpr diagnoses the error itself, so we just bail |
| 9762 | // out if it fails. |
Francois Pichet | bcf6471 | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 9763 | if (CandidateSet.empty()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 9764 | // In Microsoft mode, if we are inside a template class member function then |
| 9765 | // create a type dependent CallExpr. The goal is to postpone name lookup |
Francois Pichet | bcf6471 | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 9766 | // to instantiation time to be able to search into type dependent base |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 9767 | // classes. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9768 | if (getLangOpts().MicrosoftMode && CurContext->isDependentContext() && |
Francois Pichet | de232cb | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 9769 | (isa<FunctionDecl>(CurContext) || isa<CXXRecordDecl>(CurContext))) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 9770 | CallExpr *CE = new (Context) CallExpr(Context, Fn, Args, NumArgs, |
| 9771 | Context.DependentTy, VK_RValue, |
| 9772 | RParenLoc); |
| 9773 | CE->setTypeDependent(true); |
| 9774 | return Owned(CE); |
| 9775 | } |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9776 | return BuildRecoveryCallExpr(*this, S, Fn, ULE, LParenLoc, |
| 9777 | llvm::MutableArrayRef<Expr *>(Args, NumArgs), |
Kaelyn Uhrain | 9afaf79 | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 9778 | RParenLoc, /*EmptyLookup=*/true, |
| 9779 | AllowTypoCorrection); |
Francois Pichet | bcf6471 | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 9780 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 9781 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9782 | UnbridgedCasts.restore(); |
| 9783 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9784 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9785 | switch (CandidateSet.BestViableFunction(*this, Fn->getLocStart(), Best)) { |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9786 | case OR_Success: { |
| 9787 | FunctionDecl *FDecl = Best->Function; |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9788 | MarkFunctionReferenced(Fn->getExprLoc(), FDecl); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9789 | CheckUnresolvedLookupAccess(ULE, Best->FoundDecl); |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9790 | DiagnoseUseOfDecl(FDecl, ULE->getNameLoc()); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9791 | Fn = FixOverloadedFunctionReference(Fn, Best->FoundDecl, FDecl); |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 9792 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 9793 | ExecConfig); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9794 | } |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9795 | |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9796 | case OR_No_Viable_Function: { |
| 9797 | // Try to recover by looking for viable functions which the user might |
| 9798 | // have meant to call. |
| 9799 | ExprResult Recovery = BuildRecoveryCallExpr(*this, S, Fn, ULE, LParenLoc, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9800 | llvm::MutableArrayRef<Expr *>(Args, NumArgs), |
| 9801 | RParenLoc, |
Kaelyn Uhrain | 9afaf79 | 2012-01-25 21:11:35 +0000 | [diff] [blame] | 9802 | /*EmptyLookup=*/false, |
| 9803 | AllowTypoCorrection); |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9804 | if (!Recovery.isInvalid()) |
| 9805 | return Recovery; |
| 9806 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9807 | Diag(Fn->getLocStart(), |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9808 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9809 | << ULE->getName() << Fn->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9810 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, |
| 9811 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9812 | break; |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9813 | } |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9814 | |
| 9815 | case OR_Ambiguous: |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9816 | Diag(Fn->getLocStart(), diag::err_ovl_ambiguous_call) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9817 | << ULE->getName() << Fn->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9818 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, |
| 9819 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9820 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9821 | |
| 9822 | case OR_Deleted: |
Fariborz Jahanian | bff158d | 2011-02-25 18:38:59 +0000 | [diff] [blame] | 9823 | { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9824 | Diag(Fn->getLocStart(), diag::err_ovl_deleted_call) |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 9825 | << Best->Function->isDeleted() |
| 9826 | << ULE->getName() |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 9827 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 9828 | << Fn->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9829 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, |
| 9830 | llvm::makeArrayRef(Args, NumArgs)); |
Argyrios Kyrtzidis | 3eaa22a | 2011-11-04 15:58:13 +0000 | [diff] [blame] | 9831 | |
| 9832 | // We emitted an error for the unvailable/deleted function call but keep |
| 9833 | // the call in the AST. |
| 9834 | FunctionDecl *FDecl = Best->Function; |
| 9835 | Fn = FixOverloadedFunctionReference(Fn, Best->FoundDecl, FDecl); |
| 9836 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, |
| 9837 | RParenLoc, ExecConfig); |
Fariborz Jahanian | bff158d | 2011-02-25 18:38:59 +0000 | [diff] [blame] | 9838 | } |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9839 | } |
| 9840 | |
Douglas Gregor | b412e17 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 9841 | // Overload resolution failed. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 9842 | return ExprError(); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 9843 | } |
| 9844 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9845 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 9846 | return Functions.size() > 1 || |
| 9847 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 9848 | } |
| 9849 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9850 | /// \brief Create a unary operation that may resolve to an overloaded |
| 9851 | /// operator. |
| 9852 | /// |
| 9853 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 9854 | /// |
| 9855 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 9856 | /// operator. |
| 9857 | /// |
| 9858 | /// \param Functions The set of non-member functions that will be |
| 9859 | /// considered by overload resolution. The caller needs to build this |
| 9860 | /// set based on the context using, e.g., |
| 9861 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 9862 | /// set should not contain any member functions; those will be added |
| 9863 | /// by CreateOverloadedUnaryOp(). |
| 9864 | /// |
| 9865 | /// \param input The input argument. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9866 | ExprResult |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9867 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 9868 | const UnresolvedSetImpl &Fns, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9869 | Expr *Input) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9870 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9871 | |
| 9872 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 9873 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 9874 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9875 | // TODO: provide better source location info. |
| 9876 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9877 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9878 | if (checkPlaceholderForOverload(*this, Input)) |
| 9879 | return ExprError(); |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 9880 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9881 | Expr *Args[2] = { Input, 0 }; |
| 9882 | unsigned NumArgs = 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9883 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9884 | // For post-increment and post-decrement, add the implicit '0' as |
| 9885 | // the second argument, so that we know this is a post-increment or |
| 9886 | // post-decrement. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9887 | if (Opc == UO_PostInc || Opc == UO_PostDec) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9888 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 9889 | Args[1] = IntegerLiteral::Create(Context, Zero, Context.IntTy, |
| 9890 | SourceLocation()); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9891 | NumArgs = 2; |
| 9892 | } |
| 9893 | |
| 9894 | if (Input->isTypeDependent()) { |
Douglas Gregor | 630dec5 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 9895 | if (Fns.empty()) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9896 | return Owned(new (Context) UnaryOperator(Input, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9897 | Opc, |
Douglas Gregor | 630dec5 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 9898 | Context.DependentTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9899 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 630dec5 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 9900 | OpLoc)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9901 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9902 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9903 | UnresolvedLookupExpr *Fn |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 9904 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 9905 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 9906 | /*ADL*/ true, IsOverloaded(Fns), |
| 9907 | Fns.begin(), Fns.end()); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9908 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 9909 | &Args[0], NumArgs, |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9910 | Context.DependentTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9911 | VK_RValue, |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9912 | OpLoc)); |
| 9913 | } |
| 9914 | |
| 9915 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9916 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9917 | |
| 9918 | // Add the candidates from the given function set. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9919 | AddFunctionCandidates(Fns, llvm::makeArrayRef(Args, NumArgs), CandidateSet, |
| 9920 | false); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9921 | |
| 9922 | // Add operator candidates that are member functions. |
| 9923 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 9924 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9925 | // Add candidates from ADL. |
| 9926 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 9927 | OpLoc, llvm::makeArrayRef(Args, NumArgs), |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9928 | /*ExplicitTemplateArgs*/ 0, |
| 9929 | CandidateSet); |
| 9930 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9931 | // Add builtin operator candidates. |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 9932 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9933 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9934 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 9935 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9936 | // Perform overload resolution. |
| 9937 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9938 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9939 | case OR_Success: { |
| 9940 | // We found a built-in operator or an overloaded operator. |
| 9941 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9942 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9943 | if (FnDecl) { |
| 9944 | // We matched an overloaded operator. Build a call to that |
| 9945 | // operator. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9946 | |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9947 | MarkFunctionReferenced(OpLoc, FnDecl); |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 9948 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9949 | // Convert the arguments. |
| 9950 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9951 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 9952 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9953 | ExprResult InputRes = |
| 9954 | PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 9955 | Best->FoundDecl, Method); |
| 9956 | if (InputRes.isInvalid()) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9957 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9958 | Input = InputRes.take(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9959 | } else { |
| 9960 | // Convert the arguments. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9961 | ExprResult InputInit |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 9962 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 9963 | Context, |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 9964 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9965 | SourceLocation(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9966 | Input); |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 9967 | if (InputInit.isInvalid()) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9968 | return ExprError(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9969 | Input = InputInit.take(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9970 | } |
| 9971 | |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 9972 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 9973 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9974 | // Determine the result type. |
| 9975 | QualType ResultTy = FnDecl->getResultType(); |
| 9976 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 9977 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9978 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9979 | // Build the actual expression node. |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9980 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
Argyrios Kyrtzidis | a2a299e | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 9981 | HadMultipleCandidates, OpLoc); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9982 | if (FnExpr.isInvalid()) |
| 9983 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9984 | |
Eli Friedman | 030eee4 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 9985 | Args[0] = Input; |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9986 | CallExpr *TheCall = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9987 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9988 | Args, NumArgs, ResultTy, VK, OpLoc); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 9989 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9990 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 9991 | FnDecl)) |
| 9992 | return ExprError(); |
| 9993 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9994 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9995 | } else { |
| 9996 | // We matched a built-in operator. Convert the arguments, then |
| 9997 | // break out so that we will build the appropriate built-in |
| 9998 | // operator node. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9999 | ExprResult InputRes = |
| 10000 | PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 10001 | Best->Conversions[0], AA_Passing); |
| 10002 | if (InputRes.isInvalid()) |
| 10003 | return ExprError(); |
| 10004 | Input = InputRes.take(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10005 | break; |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10006 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10007 | } |
| 10008 | |
| 10009 | case OR_No_Viable_Function: |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10010 | // This is an erroneous use of an operator which can be overloaded by |
| 10011 | // a non-member function. Check for non-member operators which were |
| 10012 | // defined too late to be candidates. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10013 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, |
| 10014 | llvm::makeArrayRef(Args, NumArgs))) |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10015 | // FIXME: Recover by calling the found function. |
| 10016 | return ExprError(); |
| 10017 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10018 | // No viable function; fall through to handling this as a |
| 10019 | // built-in operator, which will produce an error message for us. |
| 10020 | break; |
| 10021 | |
| 10022 | case OR_Ambiguous: |
| 10023 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 10024 | << UnaryOperator::getOpcodeStr(Opc) |
| 10025 | << Input->getType() |
| 10026 | << Input->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10027 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, |
| 10028 | llvm::makeArrayRef(Args, NumArgs), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10029 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
| 10030 | return ExprError(); |
| 10031 | |
| 10032 | case OR_Deleted: |
| 10033 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 10034 | << Best->Function->isDeleted() |
| 10035 | << UnaryOperator::getOpcodeStr(Opc) |
| 10036 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 10037 | << Input->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10038 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, |
| 10039 | llvm::makeArrayRef(Args, NumArgs), |
Eli Friedman | 79b2d3a | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 10040 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10041 | return ExprError(); |
| 10042 | } |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10043 | |
| 10044 | // Either we found no viable overloaded operator or we matched a |
| 10045 | // built-in operator. In either case, fall through to trying to |
| 10046 | // build a built-in operation. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10047 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10048 | } |
| 10049 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10050 | /// \brief Create a binary operation that may resolve to an overloaded |
| 10051 | /// operator. |
| 10052 | /// |
| 10053 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 10054 | /// |
| 10055 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 10056 | /// operator. |
| 10057 | /// |
| 10058 | /// \param Functions The set of non-member functions that will be |
| 10059 | /// considered by overload resolution. The caller needs to build this |
| 10060 | /// set based on the context using, e.g., |
| 10061 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 10062 | /// set should not contain any member functions; those will be added |
| 10063 | /// by CreateOverloadedBinOp(). |
| 10064 | /// |
| 10065 | /// \param LHS Left-hand argument. |
| 10066 | /// \param RHS Right-hand argument. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10067 | ExprResult |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10068 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10069 | unsigned OpcIn, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10070 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10071 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10072 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10073 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10074 | |
| 10075 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 10076 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 10077 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 10078 | |
| 10079 | // If either side is type-dependent, create an appropriate dependent |
| 10080 | // expression. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10081 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10082 | if (Fns.empty()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10083 | // If there are no functions to store, just build a dependent |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10084 | // BinaryOperator or CompoundAssignment. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10085 | if (Opc <= BO_Assign || Opc > BO_OrAssign) |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10086 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10087 | Context.DependentTy, |
| 10088 | VK_RValue, OK_Ordinary, |
| 10089 | OpLoc)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10090 | |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10091 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 10092 | Context.DependentTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10093 | VK_LValue, |
| 10094 | OK_Ordinary, |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 10095 | Context.DependentTy, |
| 10096 | Context.DependentTy, |
| 10097 | OpLoc)); |
| 10098 | } |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10099 | |
| 10100 | // FIXME: save results of ADL from here? |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10101 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10102 | // TODO: provide better source location info in DNLoc component. |
| 10103 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10104 | UnresolvedLookupExpr *Fn |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10105 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
| 10106 | NestedNameSpecifierLoc(), OpNameInfo, |
| 10107 | /*ADL*/ true, IsOverloaded(Fns), |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10108 | Fns.begin(), Fns.end()); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10109 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10110 | Args, 2, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10111 | Context.DependentTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10112 | VK_RValue, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10113 | OpLoc)); |
| 10114 | } |
| 10115 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10116 | // Always do placeholder-like conversions on the RHS. |
| 10117 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 10118 | return ExprError(); |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10119 | |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 10120 | // Do placeholder-like conversion on the LHS; note that we should |
| 10121 | // not get here with a PseudoObject LHS. |
| 10122 | assert(Args[0]->getObjectKind() != OK_ObjCProperty); |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10123 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 10124 | return ExprError(); |
| 10125 | |
Sebastian Redl | 6a96bf7 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 10126 | // If this is the assignment operator, we only perform overload resolution |
| 10127 | // if the left-hand side is a class or enumeration type. This is actually |
| 10128 | // a hack. The standard requires that we do overload resolution between the |
| 10129 | // various built-in candidates, but as DR507 points out, this can lead to |
| 10130 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 10131 | // Note that we go the traditional code path for compound assignment forms. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10132 | if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10133 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10134 | |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10135 | // If this is the .* operator, which is not overloadable, just |
| 10136 | // create a built-in binary operator. |
| 10137 | if (Opc == BO_PtrMemD) |
| 10138 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 10139 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 10140 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10141 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10142 | |
| 10143 | // Add the candidates from the given function set. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10144 | AddFunctionCandidates(Fns, Args, CandidateSet, false); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10145 | |
| 10146 | // Add operator candidates that are member functions. |
| 10147 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 10148 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10149 | // Add candidates from ADL. |
| 10150 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10151 | OpLoc, Args, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 10152 | /*ExplicitTemplateArgs*/ 0, |
| 10153 | CandidateSet); |
| 10154 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10155 | // Add builtin operator candidates. |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 10156 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10157 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10158 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10159 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10160 | // Perform overload resolution. |
| 10161 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10162 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 10163 | case OR_Success: { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10164 | // We found a built-in operator or an overloaded operator. |
| 10165 | FunctionDecl *FnDecl = Best->Function; |
| 10166 | |
| 10167 | if (FnDecl) { |
| 10168 | // We matched an overloaded operator. Build a call to that |
| 10169 | // operator. |
| 10170 | |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10171 | MarkFunctionReferenced(OpLoc, FnDecl); |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 10172 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10173 | // Convert the arguments. |
| 10174 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10175 | // Best->Access is only meaningful for class members. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10176 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 10177 | |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10178 | ExprResult Arg1 = |
| 10179 | PerformCopyInitialization( |
| 10180 | InitializedEntity::InitializeParameter(Context, |
| 10181 | FnDecl->getParamDecl(0)), |
| 10182 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10183 | if (Arg1.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10184 | return ExprError(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10185 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10186 | ExprResult Arg0 = |
| 10187 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 10188 | Best->FoundDecl, Method); |
| 10189 | if (Arg0.isInvalid()) |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10190 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10191 | Args[0] = Arg0.takeAs<Expr>(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10192 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10193 | } else { |
| 10194 | // Convert the arguments. |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10195 | ExprResult Arg0 = PerformCopyInitialization( |
| 10196 | InitializedEntity::InitializeParameter(Context, |
| 10197 | FnDecl->getParamDecl(0)), |
| 10198 | SourceLocation(), Owned(Args[0])); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10199 | if (Arg0.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10200 | return ExprError(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10201 | |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10202 | ExprResult Arg1 = |
| 10203 | PerformCopyInitialization( |
| 10204 | InitializedEntity::InitializeParameter(Context, |
| 10205 | FnDecl->getParamDecl(1)), |
| 10206 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 10207 | if (Arg1.isInvalid()) |
| 10208 | return ExprError(); |
| 10209 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 10210 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10211 | } |
| 10212 | |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10213 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 10214 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10215 | // Determine the result type. |
| 10216 | QualType ResultTy = FnDecl->getResultType(); |
| 10217 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10218 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10219 | |
| 10220 | // Build the actual expression node. |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10221 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
| 10222 | HadMultipleCandidates, OpLoc); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10223 | if (FnExpr.isInvalid()) |
| 10224 | return ExprError(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10225 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10226 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10227 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10228 | Args, 2, ResultTy, VK, OpLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10229 | |
| 10230 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 10231 | FnDecl)) |
| 10232 | return ExprError(); |
| 10233 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10234 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10235 | } else { |
| 10236 | // We matched a built-in operator. Convert the arguments, then |
| 10237 | // break out so that we will build the appropriate built-in |
| 10238 | // operator node. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10239 | ExprResult ArgsRes0 = |
| 10240 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 10241 | Best->Conversions[0], AA_Passing); |
| 10242 | if (ArgsRes0.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10243 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10244 | Args[0] = ArgsRes0.take(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10245 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10246 | ExprResult ArgsRes1 = |
| 10247 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 10248 | Best->Conversions[1], AA_Passing); |
| 10249 | if (ArgsRes1.isInvalid()) |
| 10250 | return ExprError(); |
| 10251 | Args[1] = ArgsRes1.take(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10252 | break; |
| 10253 | } |
| 10254 | } |
| 10255 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10256 | case OR_No_Viable_Function: { |
| 10257 | // C++ [over.match.oper]p9: |
| 10258 | // If the operator is the operator , [...] and there are no |
| 10259 | // viable functions, then the operator is assumed to be the |
| 10260 | // built-in operator and interpreted according to clause 5. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10261 | if (Opc == BO_Comma) |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10262 | break; |
| 10263 | |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10264 | // For class as left operand for assignment or compound assigment |
| 10265 | // operator do not fall through to handling in built-in, but report that |
| 10266 | // no overloaded assignment operator found |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10267 | ExprResult Result = ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10268 | if (Args[0]->getType()->isRecordType() && |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10269 | Opc >= BO_Assign && Opc <= BO_OrAssign) { |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 10270 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 10271 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10272 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10273 | } else { |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10274 | // This is an erroneous use of an operator which can be overloaded by |
| 10275 | // a non-member function. Check for non-member operators which were |
| 10276 | // defined too late to be candidates. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10277 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, Args)) |
Richard Smith | 998a591 | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 10278 | // FIXME: Recover by calling the found function. |
| 10279 | return ExprError(); |
| 10280 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10281 | // No viable function; try to create a built-in operation, which will |
| 10282 | // produce an error. Then, show the non-viable candidates. |
| 10283 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 10284 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10285 | assert(Result.isInvalid() && |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10286 | "C++ binary operator overloading is missing candidates!"); |
| 10287 | if (Result.isInvalid()) |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10288 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10289 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10290 | return move(Result); |
| 10291 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10292 | |
| 10293 | case OR_Ambiguous: |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10294 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_binary) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10295 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10296 | << Args[0]->getType() << Args[1]->getType() |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10297 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10298 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10299 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10300 | return ExprError(); |
| 10301 | |
| 10302 | case OR_Deleted: |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10303 | if (isImplicitlyDeleted(Best->Function)) { |
| 10304 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
| 10305 | Diag(OpLoc, diag::err_ovl_deleted_special_oper) |
| 10306 | << getSpecialMember(Method) |
| 10307 | << BinaryOperator::getOpcodeStr(Opc) |
| 10308 | << getDeletedOrUnavailableSuffix(Best->Function); |
Richard Smith | 6f1e2c6 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 10309 | |
| 10310 | if (getSpecialMember(Method) != CXXInvalid) { |
| 10311 | // The user probably meant to call this special member. Just |
| 10312 | // explain why it's deleted. |
| 10313 | NoteDeletedFunction(Method); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10314 | return ExprError(); |
| 10315 | } |
| 10316 | } else { |
| 10317 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 10318 | << Best->Function->isDeleted() |
| 10319 | << BinaryOperator::getOpcodeStr(Opc) |
| 10320 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 10321 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10322 | } |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10323 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
Eli Friedman | 79b2d3a | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 10324 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10325 | return ExprError(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 10326 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10327 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 10328 | // We matched a built-in operator; build it. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 10329 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 10330 | } |
| 10331 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10332 | ExprResult |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10333 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 10334 | SourceLocation RLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10335 | Expr *Base, Expr *Idx) { |
| 10336 | Expr *Args[2] = { Base, Idx }; |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10337 | DeclarationName OpName = |
| 10338 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 10339 | |
| 10340 | // If either side is type-dependent, create an appropriate dependent |
| 10341 | // expression. |
| 10342 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 10343 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10344 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10345 | // CHECKME: no 'operator' keyword? |
| 10346 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
| 10347 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10348 | UnresolvedLookupExpr *Fn |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 10349 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10350 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 10351 | /*ADL*/ true, /*Overloaded*/ false, |
| 10352 | UnresolvedSetIterator(), |
| 10353 | UnresolvedSetIterator()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 10354 | // Can't add any actual overloads yet |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10355 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10356 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 10357 | Args, 2, |
| 10358 | Context.DependentTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10359 | VK_RValue, |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10360 | RLoc)); |
| 10361 | } |
| 10362 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10363 | // Handle placeholders on both operands. |
| 10364 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 10365 | return ExprError(); |
| 10366 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 10367 | return ExprError(); |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10368 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10369 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10370 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10371 | |
| 10372 | // Subscript can only be overloaded as a member function. |
| 10373 | |
| 10374 | // Add operator candidates that are member functions. |
| 10375 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 10376 | |
| 10377 | // Add builtin operator candidates. |
| 10378 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 10379 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10380 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10381 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10382 | // Perform overload resolution. |
| 10383 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10384 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10385 | case OR_Success: { |
| 10386 | // We found a built-in operator or an overloaded operator. |
| 10387 | FunctionDecl *FnDecl = Best->Function; |
| 10388 | |
| 10389 | if (FnDecl) { |
| 10390 | // We matched an overloaded operator. Build a call to that |
| 10391 | // operator. |
| 10392 | |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10393 | MarkFunctionReferenced(LLoc, FnDecl); |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 10394 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10395 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10396 | DiagnoseUseOfDecl(Best->FoundDecl, LLoc); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 10397 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10398 | // Convert the arguments. |
| 10399 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10400 | ExprResult Arg0 = |
| 10401 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 10402 | Best->FoundDecl, Method); |
| 10403 | if (Arg0.isInvalid()) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10404 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10405 | Args[0] = Arg0.take(); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10406 | |
Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10407 | // Convert the arguments. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10408 | ExprResult InputInit |
Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10409 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10410 | Context, |
Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10411 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10412 | SourceLocation(), |
Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 10413 | Owned(Args[1])); |
| 10414 | if (InputInit.isInvalid()) |
| 10415 | return ExprError(); |
| 10416 | |
| 10417 | Args[1] = InputInit.takeAs<Expr>(); |
| 10418 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10419 | // Determine the result type |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10420 | QualType ResultTy = FnDecl->getResultType(); |
| 10421 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10422 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10423 | |
| 10424 | // Build the actual expression node. |
Argyrios Kyrtzidis | a2a299e | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10425 | DeclarationNameInfo OpLocInfo(OpName, LLoc); |
| 10426 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10427 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
| 10428 | HadMultipleCandidates, |
Argyrios Kyrtzidis | a2a299e | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10429 | OpLocInfo.getLoc(), |
| 10430 | OpLocInfo.getInfo()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10431 | if (FnExpr.isInvalid()) |
| 10432 | return ExprError(); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10433 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10434 | CXXOperatorCallExpr *TheCall = |
| 10435 | new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10436 | FnExpr.take(), Args, 2, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10437 | ResultTy, VK, RLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10438 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10439 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall, |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10440 | FnDecl)) |
| 10441 | return ExprError(); |
| 10442 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10443 | return MaybeBindToTemporary(TheCall); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10444 | } else { |
| 10445 | // We matched a built-in operator. Convert the arguments, then |
| 10446 | // break out so that we will build the appropriate built-in |
| 10447 | // operator node. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10448 | ExprResult ArgsRes0 = |
| 10449 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 10450 | Best->Conversions[0], AA_Passing); |
| 10451 | if (ArgsRes0.isInvalid()) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10452 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10453 | Args[0] = ArgsRes0.take(); |
| 10454 | |
| 10455 | ExprResult ArgsRes1 = |
| 10456 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 10457 | Best->Conversions[1], AA_Passing); |
| 10458 | if (ArgsRes1.isInvalid()) |
| 10459 | return ExprError(); |
| 10460 | Args[1] = ArgsRes1.take(); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10461 | |
| 10462 | break; |
| 10463 | } |
| 10464 | } |
| 10465 | |
| 10466 | case OR_No_Viable_Function: { |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10467 | if (CandidateSet.empty()) |
| 10468 | Diag(LLoc, diag::err_ovl_no_oper) |
| 10469 | << Args[0]->getType() << /*subscript*/ 0 |
| 10470 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 10471 | else |
| 10472 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 10473 | << Args[0]->getType() |
| 10474 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10475 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10476 | "[]", LLoc); |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10477 | return ExprError(); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10478 | } |
| 10479 | |
| 10480 | case OR_Ambiguous: |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10481 | Diag(LLoc, diag::err_ovl_ambiguous_oper_binary) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10482 | << "[]" |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10483 | << Args[0]->getType() << Args[1]->getType() |
| 10484 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10485 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10486 | "[]", LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10487 | return ExprError(); |
| 10488 | |
| 10489 | case OR_Deleted: |
| 10490 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 10491 | << Best->Function->isDeleted() << "[]" |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 10492 | << getDeletedOrUnavailableSuffix(Best->Function) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10493 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10494 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10495 | "[]", LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10496 | return ExprError(); |
| 10497 | } |
| 10498 | |
| 10499 | // We matched a built-in operator; build it. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10500 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 10501 | } |
| 10502 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10503 | /// BuildCallToMemberFunction - Build a call to a member |
| 10504 | /// function. MemExpr is the expression that refers to the member |
| 10505 | /// function (and includes the object parameter), Args/NumArgs are the |
| 10506 | /// arguments to the function call (not including the object |
| 10507 | /// parameter). The caller needs to validate that the member |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10508 | /// expression refers to a non-static member function or an overloaded |
| 10509 | /// member function. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10510 | ExprResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10511 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 10512 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 10513 | unsigned NumArgs, SourceLocation RParenLoc) { |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10514 | assert(MemExprE->getType() == Context.BoundMemberTy || |
| 10515 | MemExprE->getType() == Context.OverloadTy); |
| 10516 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10517 | // Dig out the member expression. This holds both the object |
| 10518 | // argument and the member function we're referring to. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10519 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10520 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10521 | // Determine whether this is a call to a pointer-to-member function. |
| 10522 | if (BinaryOperator *op = dyn_cast<BinaryOperator>(NakedMemExpr)) { |
| 10523 | assert(op->getType() == Context.BoundMemberTy); |
| 10524 | assert(op->getOpcode() == BO_PtrMemD || op->getOpcode() == BO_PtrMemI); |
| 10525 | |
| 10526 | QualType fnType = |
| 10527 | op->getRHS()->getType()->castAs<MemberPointerType>()->getPointeeType(); |
| 10528 | |
| 10529 | const FunctionProtoType *proto = fnType->castAs<FunctionProtoType>(); |
| 10530 | QualType resultType = proto->getCallResultType(Context); |
| 10531 | ExprValueKind valueKind = Expr::getValueKindForType(proto->getResultType()); |
| 10532 | |
| 10533 | // Check that the object type isn't more qualified than the |
| 10534 | // member function we're calling. |
| 10535 | Qualifiers funcQuals = Qualifiers::fromCVRMask(proto->getTypeQuals()); |
| 10536 | |
| 10537 | QualType objectType = op->getLHS()->getType(); |
| 10538 | if (op->getOpcode() == BO_PtrMemI) |
| 10539 | objectType = objectType->castAs<PointerType>()->getPointeeType(); |
| 10540 | Qualifiers objectQuals = objectType.getQualifiers(); |
| 10541 | |
| 10542 | Qualifiers difference = objectQuals - funcQuals; |
| 10543 | difference.removeObjCGCAttr(); |
| 10544 | difference.removeAddressSpace(); |
| 10545 | if (difference) { |
| 10546 | std::string qualsString = difference.getAsString(); |
| 10547 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
| 10548 | << fnType.getUnqualifiedType() |
| 10549 | << qualsString |
| 10550 | << (qualsString.find(' ') == std::string::npos ? 1 : 2); |
| 10551 | } |
| 10552 | |
| 10553 | CXXMemberCallExpr *call |
| 10554 | = new (Context) CXXMemberCallExpr(Context, MemExprE, Args, NumArgs, |
| 10555 | resultType, valueKind, RParenLoc); |
| 10556 | |
| 10557 | if (CheckCallReturnType(proto->getResultType(), |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10558 | op->getRHS()->getLocStart(), |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10559 | call, 0)) |
| 10560 | return ExprError(); |
| 10561 | |
| 10562 | if (ConvertArgumentsForCall(call, op, 0, proto, Args, NumArgs, RParenLoc)) |
| 10563 | return ExprError(); |
| 10564 | |
| 10565 | return MaybeBindToTemporary(call); |
| 10566 | } |
| 10567 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10568 | UnbridgedCastsSet UnbridgedCasts; |
| 10569 | if (checkArgPlaceholdersForOverload(*this, Args, NumArgs, UnbridgedCasts)) |
| 10570 | return ExprError(); |
| 10571 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10572 | MemberExpr *MemExpr; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10573 | CXXMethodDecl *Method = 0; |
John McCall | 3a65ef4 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 10574 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 10575 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10576 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 10577 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10578 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10579 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 10580 | Qualifier = MemExpr->getQualifier(); |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10581 | UnbridgedCasts.restore(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10582 | } else { |
| 10583 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 10584 | Qualifier = UnresExpr->getQualifier(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10585 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 10586 | QualType ObjectType = UnresExpr->getBaseType(); |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 10587 | Expr::Classification ObjectClassification |
| 10588 | = UnresExpr->isArrow()? Expr::Classification::makeSimpleLValue() |
| 10589 | : UnresExpr->getBase()->Classify(Context); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10590 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10591 | // Add overload candidates |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10592 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10593 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10594 | // FIXME: avoid copy. |
| 10595 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 10596 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 10597 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 10598 | TemplateArgs = &TemplateArgsBuffer; |
| 10599 | } |
| 10600 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10601 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 10602 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 10603 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 10604 | NamedDecl *Func = *I; |
| 10605 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 10606 | if (isa<UsingShadowDecl>(Func)) |
| 10607 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 10608 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 10609 | |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 10610 | // Microsoft supports direct constructor calls. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10611 | if (getLangOpts().MicrosoftExt && isa<CXXConstructorDecl>(Func)) { |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10612 | AddOverloadCandidate(cast<CXXConstructorDecl>(Func), I.getPair(), |
| 10613 | llvm::makeArrayRef(Args, NumArgs), CandidateSet); |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 10614 | } else if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 10615 | // If explicit template arguments were provided, we can't call a |
| 10616 | // non-template member function. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10617 | if (TemplateArgs) |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 10618 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10619 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10620 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10621 | ObjectClassification, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10622 | llvm::makeArrayRef(Args, NumArgs), CandidateSet, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 10623 | /*SuppressUserConversions=*/false); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 10624 | } else { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10625 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10626 | I.getPair(), ActingDC, TemplateArgs, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10627 | ObjectType, ObjectClassification, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10628 | llvm::makeArrayRef(Args, NumArgs), |
| 10629 | CandidateSet, |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 10630 | /*SuppressUsedConversions=*/false); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 10631 | } |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 10632 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10633 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10634 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 10635 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10636 | UnbridgedCasts.restore(); |
| 10637 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10638 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10639 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getLocStart(), |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 10640 | Best)) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10641 | case OR_Success: |
| 10642 | Method = cast<CXXMethodDecl>(Best->Function); |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10643 | MarkFunctionReferenced(UnresExpr->getMemberLoc(), Method); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10644 | FoundDecl = Best->FoundDecl; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10645 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10646 | DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10647 | break; |
| 10648 | |
| 10649 | case OR_No_Viable_Function: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10650 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10651 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 10652 | << DeclName << MemExprE->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10653 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, |
| 10654 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10655 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10656 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10657 | |
| 10658 | case OR_Ambiguous: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10659 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 10660 | << DeclName << MemExprE->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10661 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, |
| 10662 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10663 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10664 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10665 | |
| 10666 | case OR_Deleted: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10667 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10668 | << Best->Function->isDeleted() |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 10669 | << DeclName |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 10670 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 10671 | << MemExprE->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10672 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, |
| 10673 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10674 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10675 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10676 | } |
| 10677 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10678 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10679 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10680 | // If overload resolution picked a static member, build a |
| 10681 | // non-member call based on that function. |
| 10682 | if (Method->isStatic()) { |
| 10683 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 10684 | Args, NumArgs, RParenLoc); |
| 10685 | } |
| 10686 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10687 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10688 | } |
| 10689 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10690 | QualType ResultType = Method->getResultType(); |
| 10691 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 10692 | ResultType = ResultType.getNonLValueExprType(Context); |
| 10693 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10694 | assert(Method && "Member call to something that isn't a method?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10695 | CXXMemberCallExpr *TheCall = |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10696 | new (Context) CXXMemberCallExpr(Context, MemExprE, Args, NumArgs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10697 | ResultType, VK, RParenLoc); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10698 | |
Anders Carlsson | c4859ba | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 10699 | // Check for a valid return type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10700 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10701 | TheCall, Method)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10702 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10703 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10704 | // Convert the object argument (for a non-static member function call). |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10705 | // We only need to do this if there was actually an overload; otherwise |
| 10706 | // it was done at lookup. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10707 | if (!Method->isStatic()) { |
| 10708 | ExprResult ObjectArg = |
| 10709 | PerformObjectArgumentInitialization(MemExpr->getBase(), Qualifier, |
| 10710 | FoundDecl, Method); |
| 10711 | if (ObjectArg.isInvalid()) |
| 10712 | return ExprError(); |
| 10713 | MemExpr->setBase(ObjectArg.take()); |
| 10714 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10715 | |
| 10716 | // Convert the rest of the arguments |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10717 | const FunctionProtoType *Proto = |
| 10718 | Method->getType()->getAs<FunctionProtoType>(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10719 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10720 | RParenLoc)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10721 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10722 | |
Eli Friedman | ff4b407 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 10723 | DiagnoseSentinelCalls(Method, LParenLoc, Args, NumArgs); |
| 10724 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10725 | if (CheckFunctionCall(Method, TheCall)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10726 | return ExprError(); |
Anders Carlsson | 8c84c20 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 10727 | |
Anders Carlsson | 47061ee | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 10728 | if ((isa<CXXConstructorDecl>(CurContext) || |
| 10729 | isa<CXXDestructorDecl>(CurContext)) && |
| 10730 | TheCall->getMethodDecl()->isPure()) { |
| 10731 | const CXXMethodDecl *MD = TheCall->getMethodDecl(); |
| 10732 | |
Chandler Carruth | 5925926 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 10733 | if (isa<CXXThisExpr>(MemExpr->getBase()->IgnoreParenCasts())) { |
Anders Carlsson | 47061ee | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 10734 | Diag(MemExpr->getLocStart(), |
| 10735 | diag::warn_call_to_pure_virtual_member_function_from_ctor_dtor) |
| 10736 | << MD->getDeclName() << isa<CXXDestructorDecl>(CurContext) |
| 10737 | << MD->getParent()->getDeclName(); |
| 10738 | |
| 10739 | Diag(MD->getLocStart(), diag::note_previous_decl) << MD->getDeclName(); |
Chandler Carruth | 5925926 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 10740 | } |
Anders Carlsson | 47061ee | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 10741 | } |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10742 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 10743 | } |
| 10744 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10745 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 10746 | /// type (C++ [over.call.object]), which can end up invoking an |
| 10747 | /// overloaded function call operator (@c operator()) or performing a |
| 10748 | /// user-defined conversion on the object argument. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10749 | ExprResult |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10750 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, |
Douglas Gregor | b0846b0 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 10751 | SourceLocation LParenLoc, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10752 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10753 | SourceLocation RParenLoc) { |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10754 | if (checkPlaceholderForOverload(*this, Obj)) |
| 10755 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10756 | ExprResult Object = Owned(Obj); |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10757 | |
| 10758 | UnbridgedCastsSet UnbridgedCasts; |
| 10759 | if (checkArgPlaceholdersForOverload(*this, Args, NumArgs, UnbridgedCasts)) |
| 10760 | return ExprError(); |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10761 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10762 | assert(Object.get()->getType()->isRecordType() && "Requires object type argument"); |
| 10763 | const RecordType *Record = Object.get()->getType()->getAs<RecordType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10764 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10765 | // C++ [over.call.object]p1: |
| 10766 | // If the primary-expression E in the function call syntax |
Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 10767 | // evaluates to a class object of type "cv T", then the set of |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10768 | // candidate functions includes at least the function call |
| 10769 | // operators of T. The function call operators of T are obtained by |
| 10770 | // ordinary lookup of the name operator() in the context of |
| 10771 | // (E).operator(). |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10772 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 10773 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 10774 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10775 | if (RequireCompleteType(LParenLoc, Object.get()->getType(), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 10776 | diag::err_incomplete_object_call, Object.get())) |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 10777 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10778 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 10779 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 10780 | LookupQualifiedName(R, Record->getDecl()); |
| 10781 | R.suppressDiagnostics(); |
| 10782 | |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 10783 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 10784 | Oper != OperEnd; ++Oper) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10785 | AddMethodCandidate(Oper.getPair(), Object.get()->getType(), |
| 10786 | Object.get()->Classify(Context), Args, NumArgs, CandidateSet, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 10787 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 10788 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10789 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10790 | // C++ [over.call.object]p2: |
Douglas Gregor | 38b2d3f | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 10791 | // In addition, for each (non-explicit in C++0x) conversion function |
| 10792 | // declared in T of the form |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10793 | // |
| 10794 | // operator conversion-type-id () cv-qualifier; |
| 10795 | // |
| 10796 | // where cv-qualifier is the same cv-qualification as, or a |
| 10797 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | f49fdf8 | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 10798 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 10799 | // R", or the type "reference to pointer to function of |
| 10800 | // (P1,...,Pn) returning R", or the type "reference to function |
| 10801 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10802 | // is also considered as a candidate function. Similarly, |
| 10803 | // surrogate call functions are added to the set of candidate |
| 10804 | // functions for each conversion function declared in an |
| 10805 | // accessible base class provided the function is not hidden |
| 10806 | // within T by another intervening declaration. |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 10807 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 2159182 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 10808 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 10809 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10810 | E = Conversions->end(); I != E; ++I) { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 10811 | NamedDecl *D = *I; |
| 10812 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 10813 | if (isa<UsingShadowDecl>(D)) |
| 10814 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10815 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 10816 | // Skip over templated conversion functions; they aren't |
| 10817 | // surrogates. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 10818 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 10819 | continue; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 10820 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 10821 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Douglas Gregor | 38b2d3f | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 10822 | if (!Conv->isExplicit()) { |
| 10823 | // Strip the reference type (if any) and then the pointer type (if |
| 10824 | // any) to get down to what might be a function type. |
| 10825 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 10826 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 10827 | ConvType = ConvPtrType->getPointeeType(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10828 | |
Douglas Gregor | 38b2d3f | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 10829 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
| 10830 | { |
| 10831 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10832 | Object.get(), llvm::makeArrayRef(Args, NumArgs), |
| 10833 | CandidateSet); |
Douglas Gregor | 38b2d3f | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 10834 | } |
| 10835 | } |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10836 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10837 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10838 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10839 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10840 | // Perform overload resolution. |
| 10841 | OverloadCandidateSet::iterator Best; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10842 | switch (CandidateSet.BestViableFunction(*this, Object.get()->getLocStart(), |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10843 | Best)) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10844 | case OR_Success: |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10845 | // Overload resolution succeeded; we'll build the appropriate call |
| 10846 | // below. |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10847 | break; |
| 10848 | |
| 10849 | case OR_No_Viable_Function: |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10850 | if (CandidateSet.empty()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10851 | Diag(Object.get()->getLocStart(), diag::err_ovl_no_oper) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10852 | << Object.get()->getType() << /*call*/ 1 |
| 10853 | << Object.get()->getSourceRange(); |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10854 | else |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10855 | Diag(Object.get()->getLocStart(), |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 10856 | diag::err_ovl_no_viable_object_call) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10857 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10858 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, |
| 10859 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10860 | break; |
| 10861 | |
| 10862 | case OR_Ambiguous: |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10863 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10864 | diag::err_ovl_ambiguous_object_call) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10865 | << Object.get()->getType() << Object.get()->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10866 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, |
| 10867 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10868 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10869 | |
| 10870 | case OR_Deleted: |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10871 | Diag(Object.get()->getLocStart(), |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10872 | diag::err_ovl_deleted_object_call) |
| 10873 | << Best->Function->isDeleted() |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10874 | << Object.get()->getType() |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 10875 | << getDeletedOrUnavailableSuffix(Best->Function) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10876 | << Object.get()->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 10877 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, |
| 10878 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10879 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10880 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10881 | |
Douglas Gregor | b412e17 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 10882 | if (Best == CandidateSet.end()) |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10883 | return true; |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10884 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10885 | UnbridgedCasts.restore(); |
| 10886 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10887 | if (Best->Function == 0) { |
| 10888 | // Since there is no function declaration, this is one of the |
| 10889 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10890 | CXXConversionDecl *Conv |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10891 | = cast<CXXConversionDecl>( |
| 10892 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 10893 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10894 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10895 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 49ec2e6 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 10896 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10897 | // We selected one of the surrogate functions that converts the |
| 10898 | // object parameter to a function pointer. Perform the conversion |
| 10899 | // on the object argument, then let ActOnCallExpr finish the job. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10900 | |
Fariborz Jahanian | 774cf79 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 10901 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 10902 | // and then call it. |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10903 | ExprResult Call = BuildCXXMemberCallExpr(Object.get(), Best->FoundDecl, |
| 10904 | Conv, HadMultipleCandidates); |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 10905 | if (Call.isInvalid()) |
| 10906 | return ExprError(); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 10907 | // Record usage of conversion in an implicit cast. |
| 10908 | Call = Owned(ImplicitCastExpr::Create(Context, Call.get()->getType(), |
| 10909 | CK_UserDefinedConversion, |
| 10910 | Call.get(), 0, VK_RValue)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10911 | |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 10912 | return ActOnCallExpr(S, Call.get(), LParenLoc, MultiExprArg(Args, NumArgs), |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 10913 | RParenLoc); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10914 | } |
| 10915 | |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10916 | MarkFunctionReferenced(LParenLoc, Best->Function); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10917 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10918 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 49ec2e6 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 10919 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10920 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 10921 | // that calls this method, using Object for the implicit object |
| 10922 | // parameter and passing along the remaining arguments. |
| 10923 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10924 | const FunctionProtoType *Proto = |
| 10925 | Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10926 | |
| 10927 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 10928 | unsigned NumArgsToCheck = NumArgs; |
| 10929 | |
| 10930 | // Build the full argument list for the method call (the |
| 10931 | // implicit object parameter is placed at the beginning of the |
| 10932 | // list). |
| 10933 | Expr **MethodArgs; |
| 10934 | if (NumArgs < NumArgsInProto) { |
| 10935 | NumArgsToCheck = NumArgsInProto; |
| 10936 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 10937 | } else { |
| 10938 | MethodArgs = new Expr*[NumArgs + 1]; |
| 10939 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10940 | MethodArgs[0] = Object.get(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10941 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 10942 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10943 | |
Argyrios Kyrtzidis | a2a299e | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10944 | DeclarationNameInfo OpLocInfo( |
| 10945 | Context.DeclarationNames.getCXXOperatorName(OO_Call), LParenLoc); |
| 10946 | OpLocInfo.setCXXOperatorNameRange(SourceRange(LParenLoc, RParenLoc)); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10947 | ExprResult NewFn = CreateFunctionRefExpr(*this, Method, |
Argyrios Kyrtzidis | a2a299e | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 10948 | HadMultipleCandidates, |
| 10949 | OpLocInfo.getLoc(), |
| 10950 | OpLocInfo.getInfo()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10951 | if (NewFn.isInvalid()) |
| 10952 | return true; |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10953 | |
| 10954 | // Once we've built TheCall, all of the expressions are properly |
| 10955 | // owned. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10956 | QualType ResultTy = Method->getResultType(); |
| 10957 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10958 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 10959 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10960 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10961 | new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn.take(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10962 | MethodArgs, NumArgs + 1, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10963 | ResultTy, VK, RParenLoc); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10964 | delete [] MethodArgs; |
| 10965 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10966 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall, |
Anders Carlsson | 3d5829c | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 10967 | Method)) |
| 10968 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10969 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 10970 | // We may have default arguments. If so, we need to allocate more |
| 10971 | // slots in the call for them. |
| 10972 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 10973 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 10974 | else if (NumArgs > NumArgsInProto) |
| 10975 | NumArgsToCheck = NumArgsInProto; |
| 10976 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 10977 | bool IsError = false; |
| 10978 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10979 | // Initialize the implicit object parameter. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10980 | ExprResult ObjRes = |
| 10981 | PerformObjectArgumentInitialization(Object.get(), /*Qualifier=*/0, |
| 10982 | Best->FoundDecl, Method); |
| 10983 | if (ObjRes.isInvalid()) |
| 10984 | IsError = true; |
| 10985 | else |
| 10986 | Object = move(ObjRes); |
| 10987 | TheCall->setArg(0, Object.take()); |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 10988 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10989 | // Check the argument types. |
| 10990 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10991 | Expr *Arg; |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 10992 | if (i < NumArgs) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10993 | Arg = Args[i]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10994 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 10995 | // Pass the argument. |
Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 10996 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10997 | ExprResult InputInit |
Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 10998 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10999 | Context, |
Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11000 | Method->getParamDecl(i)), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11001 | SourceLocation(), Arg); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11002 | |
Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 11003 | IsError |= InputInit.isInvalid(); |
| 11004 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11005 | } else { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11006 | ExprResult DefArg |
Douglas Gregor | 1bc688d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 11007 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 11008 | if (DefArg.isInvalid()) { |
| 11009 | IsError = true; |
| 11010 | break; |
| 11011 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11012 | |
Douglas Gregor | 1bc688d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 11013 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 11014 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11015 | |
| 11016 | TheCall->setArg(i + 1, Arg); |
| 11017 | } |
| 11018 | |
| 11019 | // If this is a variadic call, handle args passed through "...". |
| 11020 | if (Proto->isVariadic()) { |
| 11021 | // Promote the arguments (C99 6.5.2.2p7). |
| 11022 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11023 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0); |
| 11024 | IsError |= Arg.isInvalid(); |
| 11025 | TheCall->setArg(i + 1, Arg.take()); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11026 | } |
| 11027 | } |
| 11028 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 11029 | if (IsError) return true; |
| 11030 | |
Eli Friedman | ff4b407 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11031 | DiagnoseSentinelCalls(Method, LParenLoc, Args, NumArgs); |
| 11032 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11033 | if (CheckFunctionCall(Method, TheCall)) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 11034 | return true; |
| 11035 | |
John McCall | e172be5 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 11036 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 11037 | } |
| 11038 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11039 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11040 | /// (if one exists), where @c Base is an expression of class type and |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11041 | /// @c Member is the name of the member we're trying to find. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11042 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11043 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc) { |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11044 | assert(Base->getType()->isRecordType() && |
| 11045 | "left-hand side must have class type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11046 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11047 | if (checkPlaceholderForOverload(*this, Base)) |
| 11048 | return ExprError(); |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 11049 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11050 | SourceLocation Loc = Base->getExprLoc(); |
| 11051 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11052 | // C++ [over.ref]p1: |
| 11053 | // |
| 11054 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 11055 | // for a class object x of type T if T::operator->() exists and if |
| 11056 | // the operator is selected as the best match function by the |
| 11057 | // overload resolution mechanism (13.3). |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 11058 | DeclarationName OpName = |
| 11059 | Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11060 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 11061 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11062 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 11063 | if (RequireCompleteType(Loc, Base->getType(), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11064 | diag::err_typecheck_incomplete_tag, Base)) |
Eli Friedman | 132e70b | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 11065 | return ExprError(); |
| 11066 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 11067 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 11068 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 11069 | R.suppressDiagnostics(); |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 11070 | |
| 11071 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11072 | Oper != OperEnd; ++Oper) { |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 11073 | AddMethodCandidate(Oper.getPair(), Base->getType(), Base->Classify(Context), |
| 11074 | 0, 0, CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 11075 | } |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11076 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11077 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11078 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11079 | // Perform overload resolution. |
| 11080 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 11081 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11082 | case OR_Success: |
| 11083 | // Overload resolution succeeded; we'll build the call below. |
| 11084 | break; |
| 11085 | |
| 11086 | case OR_No_Viable_Function: |
| 11087 | if (CandidateSet.empty()) |
| 11088 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11089 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11090 | else |
| 11091 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11092 | << "operator->" << Base->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11093 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11094 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11095 | |
| 11096 | case OR_Ambiguous: |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 11097 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 11098 | << "->" << Base->getType() << Base->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11099 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Base); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11100 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 11101 | |
| 11102 | case OR_Deleted: |
| 11103 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 11104 | << Best->Function->isDeleted() |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11105 | << "->" |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 11106 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 11107 | << Base->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 11108 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Base); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11109 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11110 | } |
| 11111 | |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11112 | MarkFunctionReferenced(OpLoc, Best->Function); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11113 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 11114 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 11115 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11116 | // Convert the object parameter. |
| 11117 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11118 | ExprResult BaseResult = |
| 11119 | PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 11120 | Best->FoundDecl, Method); |
| 11121 | if (BaseResult.isInvalid()) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 11122 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11123 | Base = BaseResult.take(); |
Douglas Gregor | 9ecea26 | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 11124 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11125 | // Build the operator call. |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11126 | ExprResult FnExpr = CreateFunctionRefExpr(*this, Method, |
Argyrios Kyrtzidis | a2a299e | 2012-02-08 01:21:13 +0000 | [diff] [blame] | 11127 | HadMultipleCandidates, OpLoc); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11128 | if (FnExpr.isInvalid()) |
| 11129 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11130 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11131 | QualType ResultTy = Method->getResultType(); |
| 11132 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11133 | ResultTy = ResultTy.getNonLValueExprType(Context); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11134 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11135 | new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr.take(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11136 | &Base, 1, ResultTy, VK, OpLoc); |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 11137 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11138 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall, |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 11139 | Method)) |
| 11140 | return ExprError(); |
Eli Friedman | 2d9c47e | 2011-04-04 01:18:25 +0000 | [diff] [blame] | 11141 | |
| 11142 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 11143 | } |
| 11144 | |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11145 | /// BuildLiteralOperatorCall - Build a UserDefinedLiteral by creating a call to |
| 11146 | /// a literal operator described by the provided lookup results. |
| 11147 | ExprResult Sema::BuildLiteralOperatorCall(LookupResult &R, |
| 11148 | DeclarationNameInfo &SuffixInfo, |
| 11149 | ArrayRef<Expr*> Args, |
| 11150 | SourceLocation LitEndLoc, |
| 11151 | TemplateArgumentListInfo *TemplateArgs) { |
| 11152 | SourceLocation UDSuffixLoc = SuffixInfo.getCXXLiteralOperatorNameLoc(); |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11153 | |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11154 | OverloadCandidateSet CandidateSet(UDSuffixLoc); |
| 11155 | AddFunctionCandidates(R.asUnresolvedSet(), Args, CandidateSet, true, |
| 11156 | TemplateArgs); |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11157 | |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11158 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 11159 | |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11160 | // Perform overload resolution. This will usually be trivial, but might need |
| 11161 | // to perform substitutions for a literal operator template. |
| 11162 | OverloadCandidateSet::iterator Best; |
| 11163 | switch (CandidateSet.BestViableFunction(*this, UDSuffixLoc, Best)) { |
| 11164 | case OR_Success: |
| 11165 | case OR_Deleted: |
| 11166 | break; |
| 11167 | |
| 11168 | case OR_No_Viable_Function: |
| 11169 | Diag(UDSuffixLoc, diag::err_ovl_no_viable_function_in_call) |
| 11170 | << R.getLookupName(); |
| 11171 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args); |
| 11172 | return ExprError(); |
| 11173 | |
| 11174 | case OR_Ambiguous: |
| 11175 | Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) << R.getLookupName(); |
| 11176 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args); |
| 11177 | return ExprError(); |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11178 | } |
| 11179 | |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11180 | FunctionDecl *FD = Best->Function; |
| 11181 | MarkFunctionReferenced(UDSuffixLoc, FD); |
| 11182 | DiagnoseUseOfDecl(Best->FoundDecl, UDSuffixLoc); |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11183 | |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11184 | ExprResult Fn = CreateFunctionRefExpr(*this, FD, HadMultipleCandidates, |
| 11185 | SuffixInfo.getLoc(), |
| 11186 | SuffixInfo.getInfo()); |
| 11187 | if (Fn.isInvalid()) |
| 11188 | return true; |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11189 | |
| 11190 | // Check the argument types. This should almost always be a no-op, except |
| 11191 | // that array-to-pointer decay is applied to string literals. |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11192 | Expr *ConvArgs[2]; |
| 11193 | for (unsigned ArgIdx = 0; ArgIdx != Args.size(); ++ArgIdx) { |
| 11194 | ExprResult InputInit = PerformCopyInitialization( |
| 11195 | InitializedEntity::InitializeParameter(Context, FD->getParamDecl(ArgIdx)), |
| 11196 | SourceLocation(), Args[ArgIdx]); |
| 11197 | if (InputInit.isInvalid()) |
| 11198 | return true; |
| 11199 | ConvArgs[ArgIdx] = InputInit.take(); |
| 11200 | } |
| 11201 | |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11202 | QualType ResultTy = FD->getResultType(); |
| 11203 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 11204 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 11205 | |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11206 | UserDefinedLiteral *UDL = |
| 11207 | new (Context) UserDefinedLiteral(Context, Fn.take(), ConvArgs, Args.size(), |
| 11208 | ResultTy, VK, LitEndLoc, UDSuffixLoc); |
| 11209 | |
| 11210 | if (CheckCallReturnType(FD->getResultType(), UDSuffixLoc, UDL, FD)) |
| 11211 | return ExprError(); |
| 11212 | |
| 11213 | if (CheckFunctionCall(FD, UDL)) |
| 11214 | return ExprError(); |
| 11215 | |
| 11216 | return MaybeBindToTemporary(UDL); |
| 11217 | } |
| 11218 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11219 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 11220 | /// a C++ overloaded function (possibly with some parentheses and |
| 11221 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 11222 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | fcb4ab4 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 11223 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 11224 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11225 | FunctionDecl *Fn) { |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11226 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11227 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 11228 | Found, Fn); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11229 | if (SubExpr == PE->getSubExpr()) |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11230 | return PE; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11231 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11232 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11233 | } |
| 11234 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11235 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11236 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 11237 | Found, Fn); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11238 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11239 | SubExpr->getType()) && |
Douglas Gregor | 091f042 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 11240 | "Implicit cast type cannot be determined from overload"); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 11241 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11242 | if (SubExpr == ICE->getSubExpr()) |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11243 | return ICE; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11244 | |
| 11245 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 11246 | ICE->getCastKind(), |
| 11247 | SubExpr, 0, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 11248 | ICE->getValueKind()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11249 | } |
| 11250 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11251 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11252 | assert(UnOp->getOpcode() == UO_AddrOf && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11253 | "Can only take the address of an overloaded function"); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 11254 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 11255 | if (Method->isStatic()) { |
| 11256 | // Do nothing: static member functions aren't any different |
| 11257 | // from non-member functions. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11258 | } else { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11259 | // Fix the sub expression, which really has to be an |
| 11260 | // UnresolvedLookupExpr holding an overloaded member function |
| 11261 | // or template. |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11262 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 11263 | Found, Fn); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11264 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11265 | return UnOp; |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11266 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11267 | assert(isa<DeclRefExpr>(SubExpr) |
| 11268 | && "fixed to something other than a decl ref"); |
| 11269 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 11270 | && "fixed to a member ref with no nested name qualifier"); |
| 11271 | |
| 11272 | // We have taken the address of a pointer to member |
| 11273 | // function. Perform the computation here so that we get the |
| 11274 | // appropriate pointer to member type. |
| 11275 | QualType ClassType |
| 11276 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 11277 | QualType MemPtrType |
| 11278 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 11279 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11280 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, MemPtrType, |
| 11281 | VK_RValue, OK_Ordinary, |
| 11282 | UnOp->getOperatorLoc()); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 11283 | } |
| 11284 | } |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11285 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 11286 | Found, Fn); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11287 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11288 | return UnOp; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11289 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11290 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11291 | Context.getPointerType(SubExpr->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11292 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11293 | UnOp->getOperatorLoc()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11294 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11295 | |
| 11296 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11297 | // FIXME: avoid copy. |
| 11298 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11299 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11300 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11301 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 11302 | } |
| 11303 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11304 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 11305 | ULE->getQualifierLoc(), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11306 | ULE->getTemplateKeywordLoc(), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11307 | Fn, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 11308 | /*enclosing*/ false, // FIXME? |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11309 | ULE->getNameLoc(), |
| 11310 | Fn->getType(), |
| 11311 | VK_LValue, |
| 11312 | Found.getDecl(), |
| 11313 | TemplateArgs); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 11314 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11315 | DRE->setHadMultipleCandidates(ULE->getNumDecls() > 1); |
| 11316 | return DRE; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 11317 | } |
| 11318 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 11319 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11320 | // FIXME: avoid copy. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11321 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 11322 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 11323 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 11324 | TemplateArgs = &TemplateArgsBuffer; |
| 11325 | } |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 11326 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11327 | Expr *Base; |
| 11328 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 11329 | // If we're filling in a static method where we used to have an |
| 11330 | // implicit member access, rewrite to a simple decl ref. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11331 | if (MemExpr->isImplicitAccess()) { |
| 11332 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11333 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 11334 | MemExpr->getQualifierLoc(), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11335 | MemExpr->getTemplateKeywordLoc(), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11336 | Fn, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 11337 | /*enclosing*/ false, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11338 | MemExpr->getMemberLoc(), |
| 11339 | Fn->getType(), |
| 11340 | VK_LValue, |
| 11341 | Found.getDecl(), |
| 11342 | TemplateArgs); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 11343 | MarkDeclRefReferenced(DRE); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11344 | DRE->setHadMultipleCandidates(MemExpr->getNumDecls() > 1); |
| 11345 | return DRE; |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 11346 | } else { |
| 11347 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 11348 | if (MemExpr->getQualifier()) |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 11349 | Loc = MemExpr->getQualifierLoc().getBeginLoc(); |
Eli Friedman | 73a0409 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 11350 | CheckCXXThisCapture(Loc); |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 11351 | Base = new (Context) CXXThisExpr(Loc, |
| 11352 | MemExpr->getBaseType(), |
| 11353 | /*isImplicit=*/true); |
| 11354 | } |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11355 | } else |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11356 | Base = MemExpr->getBase(); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 11357 | |
John McCall | 4adb38c | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11358 | ExprValueKind valueKind; |
| 11359 | QualType type; |
| 11360 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 11361 | valueKind = VK_LValue; |
| 11362 | type = Fn->getType(); |
| 11363 | } else { |
| 11364 | valueKind = VK_RValue; |
| 11365 | type = Context.BoundMemberTy; |
| 11366 | } |
| 11367 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11368 | MemberExpr *ME = MemberExpr::Create(Context, Base, |
| 11369 | MemExpr->isArrow(), |
| 11370 | MemExpr->getQualifierLoc(), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 11371 | MemExpr->getTemplateKeywordLoc(), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11372 | Fn, |
| 11373 | Found, |
| 11374 | MemExpr->getMemberNameInfo(), |
| 11375 | TemplateArgs, |
| 11376 | type, valueKind, OK_Ordinary); |
| 11377 | ME->setHadMultipleCandidates(true); |
| 11378 | return ME; |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 11379 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11380 | |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 11381 | llvm_unreachable("Invalid reference to overloaded function"); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 11382 | } |
| 11383 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 11384 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11385 | DeclAccessPair Found, |
| 11386 | FunctionDecl *Fn) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 11387 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 11388 | } |
| 11389 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 11390 | } // end namespace clang |