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" |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 32 | #include <algorithm> |
| 33 | |
| 34 | namespace clang { |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 35 | using namespace sema; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 36 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 37 | /// A convenience routine for creating a decayed reference to a |
| 38 | /// function. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 39 | static ExprResult |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 40 | CreateFunctionRefExpr(Sema &S, FunctionDecl *Fn, |
| 41 | SourceLocation Loc = SourceLocation()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 42 | ExprResult E = S.Owned(new (S.Context) DeclRefExpr(Fn, Fn->getType(), VK_LValue, Loc)); |
| 43 | E = S.DefaultFunctionArrayConversion(E.take()); |
| 44 | if (E.isInvalid()) |
| 45 | return ExprError(); |
| 46 | return move(E); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 47 | } |
| 48 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 49 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 50 | bool InOverloadResolution, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 51 | StandardConversionSequence &SCS, |
| 52 | bool CStyle); |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 53 | |
| 54 | static bool IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 55 | QualType &ToType, |
| 56 | bool InOverloadResolution, |
| 57 | StandardConversionSequence &SCS, |
| 58 | bool CStyle); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 59 | static OverloadingResult |
| 60 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 61 | UserDefinedConversionSequence& User, |
| 62 | OverloadCandidateSet& Conversions, |
| 63 | bool AllowExplicit); |
| 64 | |
| 65 | |
| 66 | static ImplicitConversionSequence::CompareKind |
| 67 | CompareStandardConversionSequences(Sema &S, |
| 68 | const StandardConversionSequence& SCS1, |
| 69 | const StandardConversionSequence& SCS2); |
| 70 | |
| 71 | static ImplicitConversionSequence::CompareKind |
| 72 | CompareQualificationConversions(Sema &S, |
| 73 | const StandardConversionSequence& SCS1, |
| 74 | const StandardConversionSequence& SCS2); |
| 75 | |
| 76 | static ImplicitConversionSequence::CompareKind |
| 77 | CompareDerivedToBaseConversions(Sema &S, |
| 78 | const StandardConversionSequence& SCS1, |
| 79 | const StandardConversionSequence& SCS2); |
| 80 | |
| 81 | |
| 82 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 83 | /// GetConversionCategory - Retrieve the implicit conversion |
| 84 | /// category corresponding to the given implicit conversion kind. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | ImplicitConversionCategory |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 86 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 87 | static const ImplicitConversionCategory |
| 88 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 89 | ICC_Identity, |
| 90 | ICC_Lvalue_Transformation, |
| 91 | ICC_Lvalue_Transformation, |
| 92 | ICC_Lvalue_Transformation, |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 93 | ICC_Identity, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 94 | ICC_Qualification_Adjustment, |
| 95 | ICC_Promotion, |
| 96 | ICC_Promotion, |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 97 | ICC_Promotion, |
| 98 | ICC_Conversion, |
| 99 | ICC_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 100 | ICC_Conversion, |
| 101 | ICC_Conversion, |
| 102 | ICC_Conversion, |
| 103 | ICC_Conversion, |
| 104 | ICC_Conversion, |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 105 | ICC_Conversion, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 106 | ICC_Conversion, |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 107 | ICC_Conversion, |
| 108 | ICC_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 109 | ICC_Conversion |
| 110 | }; |
| 111 | return Category[(int)Kind]; |
| 112 | } |
| 113 | |
| 114 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 115 | /// corresponding to the given implicit conversion kind. |
| 116 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 117 | static const ImplicitConversionRank |
| 118 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 119 | ICR_Exact_Match, |
| 120 | ICR_Exact_Match, |
| 121 | ICR_Exact_Match, |
| 122 | ICR_Exact_Match, |
| 123 | ICR_Exact_Match, |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 124 | ICR_Exact_Match, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 125 | ICR_Promotion, |
| 126 | ICR_Promotion, |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 127 | ICR_Promotion, |
| 128 | ICR_Conversion, |
| 129 | ICR_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 130 | ICR_Conversion, |
| 131 | ICR_Conversion, |
| 132 | ICR_Conversion, |
| 133 | ICR_Conversion, |
| 134 | ICR_Conversion, |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 135 | ICR_Conversion, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 136 | ICR_Conversion, |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 137 | ICR_Conversion, |
| 138 | ICR_Conversion, |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 139 | ICR_Complex_Real_Conversion, |
| 140 | ICR_Conversion, |
| 141 | ICR_Conversion |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 142 | }; |
| 143 | return Rank[(int)Kind]; |
| 144 | } |
| 145 | |
| 146 | /// GetImplicitConversionName - Return the name of this kind of |
| 147 | /// implicit conversion. |
| 148 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | cfca1f0 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 149 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 150 | "No conversion", |
| 151 | "Lvalue-to-rvalue", |
| 152 | "Array-to-pointer", |
| 153 | "Function-to-pointer", |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 154 | "Noreturn adjustment", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 155 | "Qualification", |
| 156 | "Integral promotion", |
| 157 | "Floating point promotion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 158 | "Complex promotion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 159 | "Integral conversion", |
| 160 | "Floating conversion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 161 | "Complex conversion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 162 | "Floating-integral conversion", |
| 163 | "Pointer conversion", |
| 164 | "Pointer-to-member conversion", |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 165 | "Boolean conversion", |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 166 | "Compatible-types conversion", |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 167 | "Derived-to-base conversion", |
| 168 | "Vector conversion", |
| 169 | "Vector splat", |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 170 | "Complex-real conversion", |
| 171 | "Block Pointer conversion", |
| 172 | "Transparent Union Conversion" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 173 | }; |
| 174 | return Name[Kind]; |
| 175 | } |
| 176 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 177 | /// StandardConversionSequence - Set the standard conversion |
| 178 | /// sequence to the identity conversion. |
| 179 | void StandardConversionSequence::setAsIdentityConversion() { |
| 180 | First = ICK_Identity; |
| 181 | Second = ICK_Identity; |
| 182 | Third = ICK_Identity; |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 183 | DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 184 | ReferenceBinding = false; |
| 185 | DirectBinding = false; |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 186 | IsLvalueReference = true; |
| 187 | BindsToFunctionLvalue = false; |
| 188 | BindsToRvalue = false; |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 189 | BindsImplicitObjectArgumentWithoutRefQualifier = false; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 190 | CopyConstructor = 0; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 193 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 194 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 195 | /// implicit conversions. |
| 196 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 197 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 198 | if (GetConversionRank(First) > Rank) |
| 199 | Rank = GetConversionRank(First); |
| 200 | if (GetConversionRank(Second) > Rank) |
| 201 | Rank = GetConversionRank(Second); |
| 202 | if (GetConversionRank(Third) > Rank) |
| 203 | Rank = GetConversionRank(Third); |
| 204 | return Rank; |
| 205 | } |
| 206 | |
| 207 | /// isPointerConversionToBool - Determines whether this conversion is |
| 208 | /// 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] | 209 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 210 | /// (C++ 13.3.3.2p4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 212 | // Note that FromType has not necessarily been transformed by the |
| 213 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 214 | // check for their presence as well as checking whether FromType is |
| 215 | // a pointer. |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 216 | if (getToType(1)->isBooleanType() && |
John McCall | 6d1116a | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 217 | (getFromType()->isPointerType() || |
| 218 | getFromType()->isObjCObjectPointerType() || |
| 219 | getFromType()->isBlockPointerType() || |
Anders Carlsson | 7da7cc5 | 2010-11-05 00:12:09 +0000 | [diff] [blame] | 220 | getFromType()->isNullPtrType() || |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 221 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 222 | return true; |
| 223 | |
| 224 | return false; |
| 225 | } |
| 226 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 227 | /// isPointerConversionToVoidPointer - Determines whether this |
| 228 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 229 | /// used as part of the ranking of standard conversion sequences (C++ |
| 230 | /// 13.3.3.2p4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | bool |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 232 | StandardConversionSequence:: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 234 | QualType FromType = getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 235 | QualType ToType = getToType(1); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 236 | |
| 237 | // Note that FromType has not necessarily been transformed by the |
| 238 | // array-to-pointer implicit conversion, so check for its presence |
| 239 | // and redo the conversion to get a pointer. |
| 240 | if (First == ICK_Array_To_Pointer) |
| 241 | FromType = Context.getArrayDecayedType(FromType); |
| 242 | |
John McCall | 75851b1 | 2010-10-26 06:40:27 +0000 | [diff] [blame] | 243 | if (Second == ICK_Pointer_Conversion && FromType->isPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 244 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 245 | return ToPtrType->getPointeeType()->isVoidType(); |
| 246 | |
| 247 | return false; |
| 248 | } |
| 249 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 250 | /// DebugPrint - Print this standard conversion sequence to standard |
| 251 | /// error. Useful for debugging overloading issues. |
| 252 | void StandardConversionSequence::DebugPrint() const { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 253 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 254 | bool PrintedSomething = false; |
| 255 | if (First != ICK_Identity) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 256 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 257 | PrintedSomething = true; |
| 258 | } |
| 259 | |
| 260 | if (Second != ICK_Identity) { |
| 261 | if (PrintedSomething) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 262 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 263 | } |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 264 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 265 | |
| 266 | if (CopyConstructor) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 267 | OS << " (by copy constructor)"; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 268 | } else if (DirectBinding) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 269 | OS << " (direct reference binding)"; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 270 | } else if (ReferenceBinding) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 271 | OS << " (reference binding)"; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 272 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 273 | PrintedSomething = true; |
| 274 | } |
| 275 | |
| 276 | if (Third != ICK_Identity) { |
| 277 | if (PrintedSomething) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 278 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 279 | } |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 280 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 281 | PrintedSomething = true; |
| 282 | } |
| 283 | |
| 284 | if (!PrintedSomething) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 285 | OS << "No conversions required"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 290 | /// error. Useful for debugging overloading issues. |
| 291 | void UserDefinedConversionSequence::DebugPrint() const { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 292 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 293 | if (Before.First || Before.Second || Before.Third) { |
| 294 | Before.DebugPrint(); |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 295 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 296 | } |
Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 297 | OS << '\'' << ConversionFunction << '\''; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 298 | if (After.First || After.Second || After.Third) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 299 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 300 | After.DebugPrint(); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 305 | /// error. Useful for debugging overloading issues. |
| 306 | void ImplicitConversionSequence::DebugPrint() const { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 307 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 308 | switch (ConversionKind) { |
| 309 | case StandardConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 310 | OS << "Standard conversion: "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 311 | Standard.DebugPrint(); |
| 312 | break; |
| 313 | case UserDefinedConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 314 | OS << "User-defined conversion: "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 315 | UserDefined.DebugPrint(); |
| 316 | break; |
| 317 | case EllipsisConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 318 | OS << "Ellipsis conversion"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 319 | break; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 320 | case AmbiguousConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 321 | OS << "Ambiguous conversion"; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 322 | break; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 323 | case BadConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 324 | OS << "Bad conversion"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 325 | break; |
| 326 | } |
| 327 | |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 328 | OS << "\n"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 329 | } |
| 330 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 331 | void AmbiguousConversionSequence::construct() { |
| 332 | new (&conversions()) ConversionSet(); |
| 333 | } |
| 334 | |
| 335 | void AmbiguousConversionSequence::destruct() { |
| 336 | conversions().~ConversionSet(); |
| 337 | } |
| 338 | |
| 339 | void |
| 340 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 341 | FromTypePtr = O.FromTypePtr; |
| 342 | ToTypePtr = O.ToTypePtr; |
| 343 | new (&conversions()) ConversionSet(O.conversions()); |
| 344 | } |
| 345 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 346 | namespace { |
| 347 | // Structure used by OverloadCandidate::DeductionFailureInfo to store |
| 348 | // template parameter and template argument information. |
| 349 | struct DFIParamWithArguments { |
| 350 | TemplateParameter Param; |
| 351 | TemplateArgument FirstArg; |
| 352 | TemplateArgument SecondArg; |
| 353 | }; |
| 354 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 355 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 356 | /// \brief Convert from Sema's representation of template deduction information |
| 357 | /// to the form used in overload-candidate information. |
| 358 | OverloadCandidate::DeductionFailureInfo |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 359 | static MakeDeductionFailureInfo(ASTContext &Context, |
| 360 | Sema::TemplateDeductionResult TDK, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 361 | TemplateDeductionInfo &Info) { |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 362 | OverloadCandidate::DeductionFailureInfo Result; |
| 363 | Result.Result = static_cast<unsigned>(TDK); |
| 364 | Result.Data = 0; |
| 365 | switch (TDK) { |
| 366 | case Sema::TDK_Success: |
| 367 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 368 | case Sema::TDK_TooManyArguments: |
| 369 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 370 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 371 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 372 | case Sema::TDK_Incomplete: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 373 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 374 | Result.Data = Info.Param.getOpaqueValue(); |
| 375 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 376 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 377 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 378 | case Sema::TDK_Underqualified: { |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 379 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 380 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 381 | Saved->Param = Info.Param; |
| 382 | Saved->FirstArg = Info.FirstArg; |
| 383 | Saved->SecondArg = Info.SecondArg; |
| 384 | Result.Data = Saved; |
| 385 | break; |
| 386 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 387 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 388 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 389 | Result.Data = Info.take(); |
| 390 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 391 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 392 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 393 | case Sema::TDK_FailedOverloadResolution: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 394 | break; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 395 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 396 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 397 | return Result; |
| 398 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 399 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 400 | void OverloadCandidate::DeductionFailureInfo::Destroy() { |
| 401 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 402 | case Sema::TDK_Success: |
| 403 | case Sema::TDK_InstantiationDepth: |
| 404 | case Sema::TDK_Incomplete: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 405 | case Sema::TDK_TooManyArguments: |
| 406 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 407 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 408 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 409 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 410 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 411 | case Sema::TDK_Underqualified: |
Douglas Gregor | b02d6b3 | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 412 | // FIXME: Destroy the data? |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 413 | Data = 0; |
| 414 | break; |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 415 | |
| 416 | case Sema::TDK_SubstitutionFailure: |
| 417 | // FIXME: Destroy the template arugment list? |
| 418 | Data = 0; |
| 419 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 420 | |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 421 | // Unhandled |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 422 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 423 | case Sema::TDK_FailedOverloadResolution: |
| 424 | break; |
| 425 | } |
| 426 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 427 | |
| 428 | TemplateParameter |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 429 | OverloadCandidate::DeductionFailureInfo::getTemplateParameter() { |
| 430 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 431 | case Sema::TDK_Success: |
| 432 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 433 | case Sema::TDK_TooManyArguments: |
| 434 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 435 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 436 | return TemplateParameter(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 437 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 438 | case Sema::TDK_Incomplete: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 439 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 440 | return TemplateParameter::getFromOpaqueValue(Data); |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 441 | |
| 442 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 443 | case Sema::TDK_Underqualified: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 444 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 445 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 446 | // Unhandled |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 447 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 448 | case Sema::TDK_FailedOverloadResolution: |
| 449 | break; |
| 450 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 451 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 452 | return TemplateParameter(); |
| 453 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 454 | |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 455 | TemplateArgumentList * |
| 456 | OverloadCandidate::DeductionFailureInfo::getTemplateArgumentList() { |
| 457 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 458 | case Sema::TDK_Success: |
| 459 | case Sema::TDK_InstantiationDepth: |
| 460 | case Sema::TDK_TooManyArguments: |
| 461 | case Sema::TDK_TooFewArguments: |
| 462 | case Sema::TDK_Incomplete: |
| 463 | case Sema::TDK_InvalidExplicitArguments: |
| 464 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 465 | case Sema::TDK_Underqualified: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 466 | return 0; |
| 467 | |
| 468 | case Sema::TDK_SubstitutionFailure: |
| 469 | return static_cast<TemplateArgumentList*>(Data); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 470 | |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 471 | // Unhandled |
| 472 | case Sema::TDK_NonDeducedMismatch: |
| 473 | case Sema::TDK_FailedOverloadResolution: |
| 474 | break; |
| 475 | } |
| 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 480 | const TemplateArgument *OverloadCandidate::DeductionFailureInfo::getFirstArg() { |
| 481 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 482 | case Sema::TDK_Success: |
| 483 | case Sema::TDK_InstantiationDepth: |
| 484 | case Sema::TDK_Incomplete: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 485 | case Sema::TDK_TooManyArguments: |
| 486 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 487 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 488 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 489 | return 0; |
| 490 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 491 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 492 | case Sema::TDK_Underqualified: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 493 | return &static_cast<DFIParamWithArguments*>(Data)->FirstArg; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 494 | |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 495 | // Unhandled |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 496 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 497 | case Sema::TDK_FailedOverloadResolution: |
| 498 | break; |
| 499 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 500 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 501 | return 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 502 | } |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 503 | |
| 504 | const TemplateArgument * |
| 505 | OverloadCandidate::DeductionFailureInfo::getSecondArg() { |
| 506 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 507 | case Sema::TDK_Success: |
| 508 | case Sema::TDK_InstantiationDepth: |
| 509 | case Sema::TDK_Incomplete: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 510 | case Sema::TDK_TooManyArguments: |
| 511 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 512 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 513 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 514 | return 0; |
| 515 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 516 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 517 | case Sema::TDK_Underqualified: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 518 | return &static_cast<DFIParamWithArguments*>(Data)->SecondArg; |
| 519 | |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 520 | // Unhandled |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 521 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 522 | case Sema::TDK_FailedOverloadResolution: |
| 523 | break; |
| 524 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 525 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | void OverloadCandidateSet::clear() { |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 530 | inherited::clear(); |
| 531 | Functions.clear(); |
| 532 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 533 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 534 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 535 | // overload of the declarations in Old. This routine returns false if |
| 536 | // New and Old cannot be overloaded, e.g., if New has the same |
| 537 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 538 | // declarations aren't functions (or function templates) at all. When |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 539 | // it does return false, MatchedDecl will point to the decl that New |
| 540 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 541 | // top of the underlying declaration. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 542 | // |
| 543 | // Example: Given the following input: |
| 544 | // |
| 545 | // void f(int, float); // #1 |
| 546 | // void f(int, int); // #2 |
| 547 | // int f(int, int); // #3 |
| 548 | // |
| 549 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 550 | // so IsOverload will not be used. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 551 | // |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 552 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 553 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 554 | // (since they have different signatures), so this routine returns |
| 555 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 556 | // |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 557 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 558 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 559 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 560 | // identical (return types of functions are not part of the |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 561 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 562 | // point to the FunctionDecl for #2. |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 563 | // |
| 564 | // 'NewIsUsingShadowDecl' indicates that 'New' is being introduced |
| 565 | // into a class by a using declaration. The rules for whether to hide |
| 566 | // shadow declarations ignore some properties which otherwise figure |
| 567 | // into a function template's signature. |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 568 | Sema::OverloadKind |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 569 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
| 570 | NamedDecl *&Match, bool NewIsUsingDecl) { |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 571 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 572 | I != E; ++I) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 573 | NamedDecl *OldD = *I; |
| 574 | |
| 575 | bool OldIsUsingDecl = false; |
| 576 | if (isa<UsingShadowDecl>(OldD)) { |
| 577 | OldIsUsingDecl = true; |
| 578 | |
| 579 | // We can always introduce two using declarations into the same |
| 580 | // context, even if they have identical signatures. |
| 581 | if (NewIsUsingDecl) continue; |
| 582 | |
| 583 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 584 | } |
| 585 | |
| 586 | // If either declaration was introduced by a using declaration, |
| 587 | // we'll need to use slightly different rules for matching. |
| 588 | // Essentially, these rules are the normal rules, except that |
| 589 | // function templates hide function templates with different |
| 590 | // return types or template parameter lists. |
| 591 | bool UseMemberUsingDeclRules = |
| 592 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord(); |
| 593 | |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 594 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 595 | if (!IsOverload(New, OldT->getTemplatedDecl(), UseMemberUsingDeclRules)) { |
| 596 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 597 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 598 | continue; |
| 599 | } |
| 600 | |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 601 | Match = *I; |
| 602 | return Ovl_Match; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 603 | } |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 604 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 605 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
| 606 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 607 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 608 | continue; |
| 609 | } |
| 610 | |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 611 | Match = *I; |
| 612 | return Ovl_Match; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 613 | } |
John McCall | a8987a294 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 614 | } else if (isa<UsingDecl>(OldD)) { |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 615 | // We can overload with these, which can show up when doing |
| 616 | // redeclaration checks for UsingDecls. |
| 617 | assert(Old.getLookupKind() == LookupUsingDeclName); |
John McCall | a8987a294 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 618 | } else if (isa<TagDecl>(OldD)) { |
| 619 | // We can always overload with tags by hiding them. |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 620 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 621 | // Optimistically assume that an unresolved using decl will |
| 622 | // overload; if it doesn't, we'll have to diagnose during |
| 623 | // template instantiation. |
| 624 | } else { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 625 | // (C++ 13p1): |
| 626 | // Only function declarations can be overloaded; object and type |
| 627 | // declarations cannot be overloaded. |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 628 | Match = *I; |
| 629 | return Ovl_NonFunction; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 630 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 631 | } |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 632 | |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 633 | return Ovl_Overload; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 634 | } |
| 635 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 636 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| 637 | bool UseUsingDeclRules) { |
John McCall | 8246e35 | 2010-08-12 07:09:11 +0000 | [diff] [blame] | 638 | // If both of the functions are extern "C", then they are not |
| 639 | // overloads. |
| 640 | if (Old->isExternC() && New->isExternC()) |
| 641 | return false; |
| 642 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 643 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 644 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 645 | |
| 646 | // C++ [temp.fct]p2: |
| 647 | // A function template can be overloaded with other function templates |
| 648 | // and with normal (non-template) functions. |
| 649 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 650 | return true; |
| 651 | |
| 652 | // Is the function New an overload of the function Old? |
| 653 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 654 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 655 | |
| 656 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 657 | // determine whether they are overloads. If we find any mismatch |
| 658 | // in the signature, they are overloads. |
| 659 | |
| 660 | // If either of these functions is a K&R-style function (no |
| 661 | // prototype), then we consider them to have matching signatures. |
| 662 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 663 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 664 | return false; |
| 665 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 666 | const FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 667 | const FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 668 | |
| 669 | // The signature of a function includes the types of its |
| 670 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 671 | // of the ellipsis; see C++ DR 357). |
| 672 | if (OldQType != NewQType && |
| 673 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 674 | OldType->isVariadic() != NewType->isVariadic() || |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 675 | !FunctionArgTypesAreEqual(OldType, NewType))) |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 676 | return true; |
| 677 | |
| 678 | // C++ [temp.over.link]p4: |
| 679 | // The signature of a function template consists of its function |
| 680 | // signature, its return type and its template parameter list. The names |
| 681 | // of the template parameters are significant only for establishing the |
| 682 | // relationship between the template parameters and the rest of the |
| 683 | // signature. |
| 684 | // |
| 685 | // We check the return type and template parameter lists for function |
| 686 | // templates first; the remaining checks follow. |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 687 | // |
| 688 | // However, we don't consider either of these when deciding whether |
| 689 | // a member introduced by a shadow declaration is hidden. |
| 690 | if (!UseUsingDeclRules && NewTemplate && |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 691 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 692 | OldTemplate->getTemplateParameters(), |
| 693 | false, TPL_TemplateMatch) || |
| 694 | OldType->getResultType() != NewType->getResultType())) |
| 695 | return true; |
| 696 | |
| 697 | // If the function is a class member, its signature includes the |
Douglas Gregor | b2f8aa9 | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 698 | // 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] | 699 | // |
| 700 | // As part of this, also check whether one of the member functions |
| 701 | // is static, in which case they are not overloads (C++ |
| 702 | // 13.1p2). While not part of the definition of the signature, |
| 703 | // this check is important to determine whether these functions |
| 704 | // can be overloaded. |
| 705 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 706 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 707 | if (OldMethod && NewMethod && |
| 708 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
Douglas Gregor | b2f8aa9 | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 709 | (OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers() || |
Douglas Gregor | c83f9865 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 710 | OldMethod->getRefQualifier() != NewMethod->getRefQualifier())) { |
| 711 | if (!UseUsingDeclRules && |
| 712 | OldMethod->getRefQualifier() != NewMethod->getRefQualifier() && |
| 713 | (OldMethod->getRefQualifier() == RQ_None || |
| 714 | NewMethod->getRefQualifier() == RQ_None)) { |
| 715 | // C++0x [over.load]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 716 | // - Member function declarations with the same name and the same |
| 717 | // parameter-type-list as well as member function template |
| 718 | // declarations with the same name, the same parameter-type-list, and |
| 719 | // the same template parameter lists cannot be overloaded if any of |
Douglas Gregor | c83f9865 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 720 | // them, but not all, have a ref-qualifier (8.3.5). |
| 721 | Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) |
| 722 | << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); |
| 723 | Diag(OldMethod->getLocation(), diag::note_previous_declaration); |
| 724 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 725 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 726 | return true; |
Douglas Gregor | c83f9865 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 727 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 728 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 729 | // The signatures match; this is not an overload. |
| 730 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 733 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 734 | /// from the given expression (Expr) to the given type (ToType). This |
| 735 | /// function returns an implicit conversion sequence that can be used |
| 736 | /// to perform the initialization. Given |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 737 | /// |
| 738 | /// void f(float f); |
| 739 | /// void g(int i) { f(i); } |
| 740 | /// |
| 741 | /// this routine would produce an implicit conversion sequence to |
| 742 | /// describe the initialization of f from i, which will be a standard |
| 743 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 744 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 745 | // |
| 746 | /// Note that this routine only determines how the conversion can be |
| 747 | /// performed; it does not actually perform the conversion. As such, |
| 748 | /// it will not produce any diagnostics if no conversion is available, |
| 749 | /// but will instead return an implicit conversion sequence of kind |
| 750 | /// "BadConversion". |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 751 | /// |
| 752 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 753 | /// not permitted. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 754 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 755 | /// permitted. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 756 | static ImplicitConversionSequence |
| 757 | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
| 758 | bool SuppressUserConversions, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 759 | bool AllowExplicit, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 760 | bool InOverloadResolution, |
| 761 | bool CStyle) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 762 | ImplicitConversionSequence ICS; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 763 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 764 | ICS.Standard, CStyle)) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 765 | ICS.setStandard(); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 766 | return ICS; |
| 767 | } |
| 768 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 769 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 770 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 771 | return ICS; |
| 772 | } |
| 773 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 774 | // C++ [over.ics.user]p4: |
| 775 | // A conversion of an expression of class type to the same class |
| 776 | // type is given Exact Match rank, and a conversion of an |
| 777 | // expression of class type to a base class of that type is |
| 778 | // given Conversion rank, in spite of the fact that a copy/move |
| 779 | // constructor (i.e., a user-defined conversion function) is |
| 780 | // called for those cases. |
| 781 | QualType FromType = From->getType(); |
| 782 | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>() && |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 783 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
| 784 | S.IsDerivedFrom(FromType, ToType))) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 785 | ICS.setStandard(); |
| 786 | ICS.Standard.setAsIdentityConversion(); |
| 787 | ICS.Standard.setFromType(FromType); |
| 788 | ICS.Standard.setAllToTypes(ToType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 789 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 790 | // We don't actually check at this point whether there is a valid |
| 791 | // copy/move constructor, since overloading just assumes that it |
| 792 | // exists. When we actually perform initialization, we'll find the |
| 793 | // appropriate constructor to copy the returned object, if needed. |
| 794 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 795 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 796 | // Determine whether this is considered a derived-to-base conversion. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 797 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 798 | ICS.Standard.Second = ICK_Derived_To_Base; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 799 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 800 | return ICS; |
| 801 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 802 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 803 | if (SuppressUserConversions) { |
| 804 | // We're not in the case above, so there is no conversion that |
| 805 | // we can perform. |
| 806 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 807 | return ICS; |
| 808 | } |
| 809 | |
| 810 | // Attempt user-defined conversion. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 811 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 812 | OverloadingResult UserDefResult |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 813 | = IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, Conversions, |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 814 | AllowExplicit); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 815 | |
| 816 | if (UserDefResult == OR_Success) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 817 | ICS.setUserDefined(); |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 818 | // C++ [over.ics.user]p4: |
| 819 | // A conversion of an expression of class type to the same class |
| 820 | // type is given Exact Match rank, and a conversion of an |
| 821 | // expression of class type to a base class of that type is |
| 822 | // given Conversion rank, in spite of the fact that a copy |
| 823 | // constructor (i.e., a user-defined conversion function) is |
| 824 | // called for those cases. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 826 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 827 | QualType FromCanon |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 828 | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 829 | QualType ToCanon |
| 830 | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
Douglas Gregor | 507eb87 | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 831 | if (Constructor->isCopyConstructor() && |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 832 | (FromCanon == ToCanon || S.IsDerivedFrom(FromCanon, ToCanon))) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 833 | // Turn this into a "standard" conversion sequence, so that it |
| 834 | // gets ranked with standard conversion sequences. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 835 | ICS.setStandard(); |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 836 | ICS.Standard.setAsIdentityConversion(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 837 | ICS.Standard.setFromType(From->getType()); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 838 | ICS.Standard.setAllToTypes(ToType); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 839 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | bb2e6883 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 840 | if (ToCanon != FromCanon) |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 841 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 842 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 843 | } |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 844 | |
| 845 | // C++ [over.best.ics]p4: |
| 846 | // However, when considering the argument of a user-defined |
| 847 | // conversion function that is a candidate by 13.3.1.3 when |
| 848 | // invoked for the copying of the temporary in the second step |
| 849 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 850 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 851 | // ellipsis conversion sequences are allowed. |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 852 | if (SuppressUserConversions && ICS.isUserDefined()) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 853 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 854 | } |
John McCall | e8c8cd2 | 2010-01-13 22:30:33 +0000 | [diff] [blame] | 855 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 856 | ICS.setAmbiguous(); |
| 857 | ICS.Ambiguous.setFromType(From->getType()); |
| 858 | ICS.Ambiguous.setToType(ToType); |
| 859 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 860 | Cand != Conversions.end(); ++Cand) |
| 861 | if (Cand->Viable) |
| 862 | ICS.Ambiguous.addConversion(Cand->Function); |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 863 | } else { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 864 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 865 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 866 | |
| 867 | return ICS; |
| 868 | } |
| 869 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 870 | bool Sema::TryImplicitConversion(InitializationSequence &Sequence, |
| 871 | const InitializedEntity &Entity, |
| 872 | Expr *Initializer, |
| 873 | bool SuppressUserConversions, |
| 874 | bool AllowExplicitConversions, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 875 | bool InOverloadResolution, |
| 876 | bool CStyle) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 877 | ImplicitConversionSequence ICS |
| 878 | = clang::TryImplicitConversion(*this, Initializer, Entity.getType(), |
| 879 | SuppressUserConversions, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 880 | AllowExplicitConversions, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 881 | InOverloadResolution, |
| 882 | CStyle); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 883 | if (ICS.isBad()) return true; |
| 884 | |
| 885 | // Perform the actual conversion. |
| 886 | Sequence.AddConversionSequenceStep(ICS, Entity.getType()); |
| 887 | return false; |
| 888 | } |
| 889 | |
Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 890 | /// PerformImplicitConversion - Perform an implicit conversion of the |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 891 | /// expression From to the type ToType. Returns the |
Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 892 | /// converted expression. Flavor is the kind of conversion we're |
| 893 | /// performing, used in the error message. If @p AllowExplicit, |
| 894 | /// explicit user-defined conversions are permitted. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 895 | ExprResult |
| 896 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 897 | AssignmentAction Action, bool AllowExplicit) { |
| 898 | ImplicitConversionSequence ICS; |
| 899 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
| 900 | } |
| 901 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 902 | ExprResult |
| 903 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 904 | AssignmentAction Action, bool AllowExplicit, |
| 905 | ImplicitConversionSequence& ICS) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 906 | ICS = clang::TryImplicitConversion(*this, From, ToType, |
| 907 | /*SuppressUserConversions=*/false, |
| 908 | AllowExplicit, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 909 | /*InOverloadResolution=*/false, |
| 910 | /*CStyle=*/false); |
Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 911 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 912 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 913 | |
| 914 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 915 | /// conversion that strips "noreturn" off the nested function type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 916 | static bool IsNoReturnConversion(ASTContext &Context, QualType FromType, |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 917 | QualType ToType, QualType &ResultTy) { |
| 918 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 919 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 920 | |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 921 | // Permit the conversion F(t __attribute__((noreturn))) -> F(t) |
| 922 | // where F adds one of the following at most once: |
| 923 | // - a pointer |
| 924 | // - a member pointer |
| 925 | // - a block pointer |
| 926 | CanQualType CanTo = Context.getCanonicalType(ToType); |
| 927 | CanQualType CanFrom = Context.getCanonicalType(FromType); |
| 928 | Type::TypeClass TyClass = CanTo->getTypeClass(); |
| 929 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 930 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) { |
| 931 | if (TyClass == Type::Pointer) { |
| 932 | CanTo = CanTo.getAs<PointerType>()->getPointeeType(); |
| 933 | CanFrom = CanFrom.getAs<PointerType>()->getPointeeType(); |
| 934 | } else if (TyClass == Type::BlockPointer) { |
| 935 | CanTo = CanTo.getAs<BlockPointerType>()->getPointeeType(); |
| 936 | CanFrom = CanFrom.getAs<BlockPointerType>()->getPointeeType(); |
| 937 | } else if (TyClass == Type::MemberPointer) { |
| 938 | CanTo = CanTo.getAs<MemberPointerType>()->getPointeeType(); |
| 939 | CanFrom = CanFrom.getAs<MemberPointerType>()->getPointeeType(); |
| 940 | } else { |
| 941 | return false; |
| 942 | } |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 943 | |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 944 | TyClass = CanTo->getTypeClass(); |
| 945 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 946 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) |
| 947 | return false; |
| 948 | } |
| 949 | |
| 950 | const FunctionType *FromFn = cast<FunctionType>(CanFrom); |
| 951 | FunctionType::ExtInfo EInfo = FromFn->getExtInfo(); |
| 952 | if (!EInfo.getNoReturn()) return false; |
| 953 | |
| 954 | FromFn = Context.adjustFunctionType(FromFn, EInfo.withNoReturn(false)); |
| 955 | assert(QualType(FromFn, 0).isCanonical()); |
| 956 | if (QualType(FromFn, 0) != CanTo) return false; |
| 957 | |
| 958 | ResultTy = ToType; |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 959 | return true; |
| 960 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 961 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 962 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 963 | /// vector conversion. |
| 964 | /// |
| 965 | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
| 966 | /// conversion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 967 | static bool IsVectorConversion(ASTContext &Context, QualType FromType, |
| 968 | QualType ToType, ImplicitConversionKind &ICK) { |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 969 | // We need at least one of these types to be a vector type to have a vector |
| 970 | // conversion. |
| 971 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
| 972 | return false; |
| 973 | |
| 974 | // Identical types require no conversions. |
| 975 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 976 | return false; |
| 977 | |
| 978 | // There are no conversions between extended vector types, only identity. |
| 979 | if (ToType->isExtVectorType()) { |
| 980 | // There are no conversions between extended vector types other than the |
| 981 | // identity conversion. |
| 982 | if (FromType->isExtVectorType()) |
| 983 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 984 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 985 | // Vector splat from any arithmetic type to a vector. |
Douglas Gregor | a3208f9 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 986 | if (FromType->isArithmeticType()) { |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 987 | ICK = ICK_Vector_Splat; |
| 988 | return true; |
| 989 | } |
| 990 | } |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 991 | |
| 992 | // We can perform the conversion between vector types in the following cases: |
| 993 | // 1)vector types are equivalent AltiVec and GCC vector types |
| 994 | // 2)lax vector conversions are permitted and the vector types are of the |
| 995 | // same size |
| 996 | if (ToType->isVectorType() && FromType->isVectorType()) { |
| 997 | if (Context.areCompatibleVectorTypes(FromType, ToType) || |
Chandler Carruth | 9c524c1 | 2010-08-08 05:02:51 +0000 | [diff] [blame] | 998 | (Context.getLangOptions().LaxVectorConversions && |
| 999 | (Context.getTypeSize(FromType) == Context.getTypeSize(ToType)))) { |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1000 | ICK = ICK_Vector_Conversion; |
| 1001 | return true; |
| 1002 | } |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1003 | } |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1004 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1005 | return false; |
| 1006 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1007 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1008 | /// IsStandardConversion - Determines whether there is a standard |
| 1009 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 1010 | /// expression From to the type ToType. Standard conversion sequences |
| 1011 | /// only consider non-class types; for conversions that involve class |
| 1012 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 1013 | /// contain the standard conversion sequence required to perform this |
| 1014 | /// conversion and this routine will return true. Otherwise, this |
| 1015 | /// routine will return false and the value of SCS is unspecified. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1016 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 1017 | bool InOverloadResolution, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1018 | StandardConversionSequence &SCS, |
| 1019 | bool CStyle) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1020 | QualType FromType = From->getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1021 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1022 | // Standard conversions (C++ [conv]) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1023 | SCS.setAsIdentityConversion(); |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1024 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1025 | SCS.IncompatibleObjC = false; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1026 | SCS.setFromType(FromType); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1027 | SCS.CopyConstructor = 0; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1028 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1029 | // There are no standard conversions for class types in C++, so |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1030 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1031 | if (FromType->isRecordType() || ToType->isRecordType()) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1032 | if (S.getLangOptions().CPlusPlus) |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1033 | return false; |
| 1034 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1035 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1038 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 1039 | // array-to-pointer conversion, or function-to-pointer conversion |
| 1040 | // (C++ 4p1). |
| 1041 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1042 | if (FromType == S.Context.OverloadTy) { |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1043 | DeclAccessPair AccessPair; |
| 1044 | if (FunctionDecl *Fn |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1045 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1046 | AccessPair)) { |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1047 | // We were able to resolve the address of the overloaded function, |
| 1048 | // so we can convert to the type of that function. |
| 1049 | FromType = Fn->getType(); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1050 | |
| 1051 | // we can sometimes resolve &foo<int> regardless of ToType, so check |
| 1052 | // if the type matches (identity) or we are converting to bool |
| 1053 | if (!S.Context.hasSameUnqualifiedType( |
| 1054 | S.ExtractUnqualifiedFunctionType(ToType), FromType)) { |
| 1055 | QualType resultTy; |
| 1056 | // if the function type matches except for [[noreturn]], it's ok |
| 1057 | if (!IsNoReturnConversion(S.Context, FromType, |
| 1058 | S.ExtractUnqualifiedFunctionType(ToType), resultTy)) |
| 1059 | // otherwise, only a boolean conversion is standard |
| 1060 | if (!ToType->isBooleanType()) |
| 1061 | return false; |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1062 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1063 | |
Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1064 | // Check if the "from" expression is taking the address of an overloaded |
| 1065 | // function and recompute the FromType accordingly. Take advantage of the |
| 1066 | // fact that non-static member functions *must* have such an address-of |
| 1067 | // expression. |
| 1068 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn); |
| 1069 | if (Method && !Method->isStatic()) { |
| 1070 | assert(isa<UnaryOperator>(From->IgnoreParens()) && |
| 1071 | "Non-unary operator on non-static member address"); |
| 1072 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() |
| 1073 | == UO_AddrOf && |
| 1074 | "Non-address-of operator on non-static member address"); |
| 1075 | const Type *ClassType |
| 1076 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 1077 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
Chandler Carruth | 7750f76 | 2011-03-29 18:38:10 +0000 | [diff] [blame] | 1078 | } else if (isa<UnaryOperator>(From->IgnoreParens())) { |
| 1079 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() == |
| 1080 | UO_AddrOf && |
Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1081 | "Non-address-of operator for overloaded function expression"); |
| 1082 | FromType = S.Context.getPointerType(FromType); |
| 1083 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1084 | |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1085 | // Check that we've computed the proper type after overload resolution. |
Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1086 | assert(S.Context.hasSameType( |
| 1087 | FromType, |
| 1088 | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1089 | } else { |
| 1090 | return false; |
| 1091 | } |
Anders Carlsson | ba37e1e | 2010-11-04 05:28:09 +0000 | [diff] [blame] | 1092 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1093 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1094 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 1095 | // converted to an rvalue. |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1096 | bool argIsLValue = From->isLValue(); |
| 1097 | if (argIsLValue && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1098 | !FromType->isFunctionType() && !FromType->isArrayType() && |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1099 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1100 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1101 | |
| 1102 | // If T is a non-class type, the type of the rvalue is the |
| 1103 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1104 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 1105 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1106 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1107 | } else if (FromType->isArrayType()) { |
| 1108 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1109 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1110 | |
| 1111 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 1112 | // bound of T" can be converted to an rvalue of type "pointer to |
| 1113 | // T" (C++ 4.2p1). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1114 | FromType = S.Context.getArrayDecayedType(FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1115 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1116 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1117 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1118 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1119 | |
| 1120 | // For the purpose of ranking in overload resolution |
| 1121 | // (13.3.3.1.1), this conversion is considered an |
| 1122 | // array-to-pointer conversion followed by a qualification |
| 1123 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1124 | SCS.Second = ICK_Identity; |
| 1125 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1126 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1127 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1128 | } |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1129 | } else if (FromType->isFunctionType() && argIsLValue) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1130 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1131 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1132 | |
| 1133 | // An lvalue of function type T can be converted to an rvalue of |
| 1134 | // type "pointer to T." The result is a pointer to the |
| 1135 | // function. (C++ 4.3p1). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1136 | FromType = S.Context.getPointerType(FromType); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1137 | } else { |
| 1138 | // We don't require any conversions for the first step. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1139 | SCS.First = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1140 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1141 | SCS.setToType(0, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1142 | |
| 1143 | // The second conversion can be an integral promotion, floating |
| 1144 | // point promotion, integral conversion, floating point conversion, |
| 1145 | // floating-integral conversion, pointer conversion, |
| 1146 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1147 | // For overloading in C, this can also be a "compatible-type" |
| 1148 | // conversion. |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1149 | bool IncompatibleObjC = false; |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1150 | ImplicitConversionKind SecondICK = ICK_Identity; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1151 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1152 | // The unqualified versions of the types are the same: there's no |
| 1153 | // conversion to do. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1154 | SCS.Second = ICK_Identity; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1155 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1156 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1157 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1158 | FromType = ToType.getUnqualifiedType(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1159 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1160 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1161 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1162 | FromType = ToType.getUnqualifiedType(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1163 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1164 | // Complex promotion (Clang extension) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1165 | SCS.Second = ICK_Complex_Promotion; |
| 1166 | FromType = ToType.getUnqualifiedType(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1167 | } else if (ToType->isBooleanType() && |
| 1168 | (FromType->isArithmeticType() || |
| 1169 | FromType->isAnyPointerType() || |
| 1170 | FromType->isBlockPointerType() || |
| 1171 | FromType->isMemberPointerType() || |
| 1172 | FromType->isNullPtrType())) { |
| 1173 | // Boolean conversions (C++ 4.12). |
| 1174 | SCS.Second = ICK_Boolean_Conversion; |
| 1175 | FromType = S.Context.BoolTy; |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1176 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1177 | ToType->isIntegralType(S.Context)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1178 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1179 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1180 | FromType = ToType.getUnqualifiedType(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1181 | } else if (FromType->isAnyComplexType() && ToType->isComplexType()) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1182 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1183 | SCS.Second = ICK_Complex_Conversion; |
| 1184 | FromType = ToType.getUnqualifiedType(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1185 | } else if ((FromType->isAnyComplexType() && ToType->isArithmeticType()) || |
| 1186 | (ToType->isAnyComplexType() && FromType->isArithmeticType())) { |
Chandler Carruth | 8fa1e7e | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1187 | // Complex-real conversions (C99 6.3.1.7) |
| 1188 | SCS.Second = ICK_Complex_Real; |
| 1189 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 49b4d73 | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1190 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
Chandler Carruth | 8fa1e7e | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1191 | // Floating point conversions (C++ 4.8). |
| 1192 | SCS.Second = ICK_Floating_Conversion; |
| 1193 | FromType = ToType.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1194 | } else if ((FromType->isRealFloatingType() && |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1195 | ToType->isIntegralType(S.Context)) || |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1196 | (FromType->isIntegralOrUnscopedEnumerationType() && |
Douglas Gregor | 49b4d73 | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1197 | ToType->isRealFloatingType())) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1198 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1199 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1200 | FromType = ToType.getUnqualifiedType(); |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1201 | } else if (S.IsBlockPointerConversion(FromType, ToType, FromType)) { |
| 1202 | SCS.Second = ICK_Block_Pointer_Conversion; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1203 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1204 | FromType, IncompatibleObjC)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1205 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1206 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1207 | SCS.IncompatibleObjC = IncompatibleObjC; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1208 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1209 | InOverloadResolution, FromType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1210 | // Pointer to member conversions (4.11). |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1211 | SCS.Second = ICK_Pointer_Member; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1212 | } else if (IsVectorConversion(S.Context, FromType, ToType, SecondICK)) { |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1213 | SCS.Second = SecondICK; |
| 1214 | FromType = ToType.getUnqualifiedType(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1215 | } else if (!S.getLangOptions().CPlusPlus && |
| 1216 | S.Context.typesAreCompatible(ToType, FromType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1217 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1218 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1219 | FromType = ToType.getUnqualifiedType(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1220 | } else if (IsNoReturnConversion(S.Context, FromType, ToType, FromType)) { |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1221 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1222 | SCS.Second = ICK_NoReturn_Adjustment; |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1223 | } else if (IsTransparentUnionStandardConversion(S, From, ToType, |
| 1224 | InOverloadResolution, |
| 1225 | SCS, CStyle)) { |
| 1226 | SCS.Second = ICK_TransparentUnionConversion; |
| 1227 | FromType = ToType; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1228 | } else { |
| 1229 | // No second conversion required. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1230 | SCS.Second = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1231 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1232 | SCS.setToType(1, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1233 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1234 | QualType CanonFrom; |
| 1235 | QualType CanonTo; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1236 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1237 | if (S.IsQualificationConversion(FromType, ToType, CStyle)) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1238 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1239 | FromType = ToType; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1240 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1241 | CanonTo = S.Context.getCanonicalType(ToType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1242 | } else { |
| 1243 | // No conversion required |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1244 | SCS.Third = ICK_Identity; |
| 1245 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1246 | // C++ [over.best.ics]p6: |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1247 | // [...] Any difference in top-level cv-qualification is |
| 1248 | // subsumed by the initialization itself and does not constitute |
| 1249 | // a conversion. [...] |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1250 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1251 | CanonTo = S.Context.getCanonicalType(ToType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1252 | if (CanonFrom.getLocalUnqualifiedType() |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1253 | == CanonTo.getLocalUnqualifiedType() && |
Fariborz Jahanian | 9f963c2 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 1254 | (CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers() |
| 1255 | || CanonFrom.getObjCGCAttr() != CanonTo.getObjCGCAttr())) { |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1256 | FromType = ToType; |
| 1257 | CanonFrom = CanonTo; |
| 1258 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1259 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1260 | SCS.setToType(2, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1261 | |
| 1262 | // If we have not converted the argument type to the parameter type, |
| 1263 | // this is a bad conversion sequence. |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1264 | if (CanonFrom != CanonTo) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1265 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1266 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1267 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1268 | } |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1269 | |
| 1270 | static bool |
| 1271 | IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 1272 | QualType &ToType, |
| 1273 | bool InOverloadResolution, |
| 1274 | StandardConversionSequence &SCS, |
| 1275 | bool CStyle) { |
| 1276 | |
| 1277 | const RecordType *UT = ToType->getAsUnionType(); |
| 1278 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 1279 | return false; |
| 1280 | // The field to initialize within the transparent union. |
| 1281 | RecordDecl *UD = UT->getDecl(); |
| 1282 | // It's compatible if the expression matches any of the fields. |
| 1283 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1284 | itend = UD->field_end(); |
| 1285 | it != itend; ++it) { |
| 1286 | if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS, CStyle)) { |
| 1287 | ToType = it->getType(); |
| 1288 | return true; |
| 1289 | } |
| 1290 | } |
| 1291 | return false; |
| 1292 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1293 | |
| 1294 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1295 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1296 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1297 | /// sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1298 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1299 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | ee54797 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1300 | // All integers are built-in. |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1301 | if (!To) { |
| 1302 | return false; |
| 1303 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1304 | |
| 1305 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1306 | // unsigned short int can be converted to an rvalue of type int if |
| 1307 | // int can represent all the values of the source type; otherwise, |
| 1308 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1309 | // int (C++ 4.5p1). |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1310 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1311 | !FromType->isEnumeralType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1312 | if (// We can promote any signed, promotable integer type to an int |
| 1313 | (FromType->isSignedIntegerType() || |
| 1314 | // We can promote any unsigned integer type whose size is |
| 1315 | // less than int to an int. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1316 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1317 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1318 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1321 | return To->getKind() == BuiltinType::UInt; |
| 1322 | } |
| 1323 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1324 | // C++0x [conv.prom]p3: |
| 1325 | // A prvalue of an unscoped enumeration type whose underlying type is not |
| 1326 | // fixed (7.2) can be converted to an rvalue a prvalue of the first of the |
| 1327 | // following types that can represent all the values of the enumeration |
| 1328 | // (i.e., the values in the range bmin to bmax as described in 7.2): int, |
| 1329 | // unsigned int, long int, unsigned long int, long long int, or unsigned |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1330 | // 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] | 1331 | // values of the enumeration, an rvalue a prvalue of an unscoped enumeration |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1332 | // 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] | 1333 | // with lowest integer conversion rank (4.13) greater than the rank of long |
| 1334 | // 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] | 1335 | // there are two such extended types, the signed one is chosen. |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1336 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
| 1337 | // C++0x 7.2p9: Note that this implicit enum to int conversion is not |
| 1338 | // provided for a scoped enumeration. |
| 1339 | if (FromEnumType->getDecl()->isScoped()) |
| 1340 | return false; |
| 1341 | |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1342 | // We have already pre-calculated the promotion type, so this is trivial. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1343 | if (ToType->isIntegerType() && |
Douglas Gregor | c87f4d4 | 2010-09-12 03:38:25 +0000 | [diff] [blame] | 1344 | !RequireCompleteType(From->getLocStart(), FromType, PDiag())) |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1345 | return Context.hasSameUnqualifiedType(ToType, |
| 1346 | FromEnumType->getDecl()->getPromotionType()); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1347 | } |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1348 | |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1349 | // C++0x [conv.prom]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1350 | // A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted |
| 1351 | // to an rvalue a prvalue of the first of the following types that can |
| 1352 | // represent all the values of its underlying type: int, unsigned int, |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1353 | // 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] | 1354 | // 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] | 1355 | // underlying type, an rvalue a prvalue of type char16_t, char32_t, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1356 | // 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] | 1357 | // type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1358 | if (FromType->isAnyCharacterType() && !FromType->isCharType() && |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1359 | ToType->isIntegerType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1360 | // Determine whether the type we're converting from is signed or |
| 1361 | // unsigned. |
| 1362 | bool FromIsSigned; |
| 1363 | uint64_t FromSize = Context.getTypeSize(FromType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1364 | |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1365 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 1366 | FromIsSigned = true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1367 | |
| 1368 | // The types we'll try to promote to, in the appropriate |
| 1369 | // order. Try each of these types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1370 | QualType PromoteTypes[6] = { |
| 1371 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1372 | Context.LongTy, Context.UnsignedLongTy , |
| 1373 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1374 | }; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1375 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1376 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1377 | if (FromSize < ToSize || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1378 | (FromSize == ToSize && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1379 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1380 | // We found the type that we can promote to. If this is the |
| 1381 | // type we wanted, we have a promotion. Otherwise, no |
| 1382 | // promotion. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1383 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1384 | } |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1389 | // rvalue of type int if int can represent all the values of the |
| 1390 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1391 | // unsigned int can represent all the values of the bit-field. If |
| 1392 | // the bit-field is larger yet, no integral promotion applies to |
| 1393 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1394 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1395 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1396 | // conversion. |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1397 | using llvm::APSInt; |
| 1398 | if (From) |
| 1399 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1400 | APSInt BitWidth; |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1401 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1402 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1403 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1404 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1405 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1406 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1407 | if (BitWidth < ToSize || |
| 1408 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1409 | return To->getKind() == BuiltinType::Int; |
| 1410 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1411 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1412 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1413 | // that fits into an unsigned int? |
| 1414 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1415 | return To->getKind() == BuiltinType::UInt; |
| 1416 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1417 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1418 | return false; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1419 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1420 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1421 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1422 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1423 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1424 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1425 | return true; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1426 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1427 | |
| 1428 | return false; |
| 1429 | } |
| 1430 | |
| 1431 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1432 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1433 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1434 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1435 | /// An rvalue of type float can be converted to an rvalue of type |
| 1436 | /// double. (C++ 4.6p1). |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1437 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1438 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1439 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1440 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1441 | return true; |
| 1442 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1443 | // C99 6.3.1.5p1: |
| 1444 | // When a float is promoted to double or long double, or a |
| 1445 | // double is promoted to long double [...]. |
| 1446 | if (!getLangOptions().CPlusPlus && |
| 1447 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1448 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1449 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1450 | return true; |
| 1451 | } |
| 1452 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1453 | return false; |
| 1454 | } |
| 1455 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1456 | /// \brief Determine if a conversion is a complex promotion. |
| 1457 | /// |
| 1458 | /// A complex promotion is defined as a complex -> complex conversion |
| 1459 | /// where the conversion between the underlying real types is a |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1460 | /// floating-point or integral promotion. |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1461 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1462 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1463 | if (!FromComplex) |
| 1464 | return false; |
| 1465 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1466 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1467 | if (!ToComplex) |
| 1468 | return false; |
| 1469 | |
| 1470 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1471 | ToComplex->getElementType()) || |
| 1472 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1473 | ToComplex->getElementType()); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1476 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1477 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1478 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1479 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1480 | /// the right set of qualifiers on its pointee. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1481 | static QualType |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1482 | BuildSimilarlyQualifiedPointerType(const Type *FromPtr, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1483 | QualType ToPointee, QualType ToType, |
| 1484 | ASTContext &Context) { |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1485 | assert((FromPtr->getTypeClass() == Type::Pointer || |
| 1486 | FromPtr->getTypeClass() == Type::ObjCObjectPointer) && |
| 1487 | "Invalid similarly-qualified pointer type"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1488 | |
Douglas Gregor | c6bd1d3 | 2010-12-06 22:09:19 +0000 | [diff] [blame] | 1489 | /// \brief Conversions to 'id' subsume cv-qualifier conversions. |
| 1490 | if (ToType->isObjCIdType() || ToType->isObjCQualifiedIdType()) |
| 1491 | return ToType.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1492 | |
| 1493 | QualType CanonFromPointee |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1494 | = Context.getCanonicalType(FromPtr->getPointeeType()); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1495 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1496 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1497 | |
| 1498 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1499 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1500 | // ToType is exactly what we need. Return it. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1501 | if (!ToType.isNull()) |
Douglas Gregor | b9f907b | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1502 | return ToType.getUnqualifiedType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1503 | |
| 1504 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1505 | // already. |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1506 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1507 | return Context.getObjCObjectPointerType(ToPointee); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1508 | return Context.getPointerType(ToPointee); |
| 1509 | } |
| 1510 | |
| 1511 | // Just build a canonical type that has the right qualifiers. |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1512 | QualType QualifiedCanonToPointee |
| 1513 | = Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), Quals); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1514 | |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1515 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1516 | return Context.getObjCObjectPointerType(QualifiedCanonToPointee); |
| 1517 | return Context.getPointerType(QualifiedCanonToPointee); |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1518 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1519 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1520 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1521 | bool InOverloadResolution, |
| 1522 | ASTContext &Context) { |
| 1523 | // Handle value-dependent integral null pointer constants correctly. |
| 1524 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1525 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1526 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1527 | return !InOverloadResolution; |
| 1528 | |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1529 | return Expr->isNullPointerConstant(Context, |
| 1530 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1531 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1532 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1533 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1534 | /// IsPointerConversion - Determines whether the conversion of the |
| 1535 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1536 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1537 | /// 4.10). If so, returns true and places the converted type (that |
| 1538 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1539 | /// ConvertedType. |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1540 | /// |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1541 | /// This routine also supports conversions to and from block pointers |
| 1542 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1543 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1544 | /// appropriate overloading rules for Objective-C, we may want to |
| 1545 | /// split the Objective-C checks into a different routine; however, |
| 1546 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1547 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1548 | /// set if the conversion is an allowed Objective-C conversion that |
| 1549 | /// should result in a warning. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1550 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1551 | bool InOverloadResolution, |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1552 | QualType& ConvertedType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1553 | bool &IncompatibleObjC) { |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1554 | IncompatibleObjC = false; |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1555 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, |
| 1556 | IncompatibleObjC)) |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1557 | return true; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1558 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1559 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1560 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1561 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 79a6b01 | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1562 | ConvertedType = ToType; |
| 1563 | return true; |
| 1564 | } |
| 1565 | |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1566 | // Blocks: Block pointers can be converted to void*. |
| 1567 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1568 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1569 | ConvertedType = ToType; |
| 1570 | return true; |
| 1571 | } |
| 1572 | // Blocks: A null pointer constant can be converted to a block |
| 1573 | // pointer type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1574 | if (ToType->isBlockPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1575 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1576 | ConvertedType = ToType; |
| 1577 | return true; |
| 1578 | } |
| 1579 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1580 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1581 | // pointer constant. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1582 | if (ToType->isNullPtrType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1583 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1584 | ConvertedType = ToType; |
| 1585 | return true; |
| 1586 | } |
| 1587 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1588 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1589 | if (!ToTypePtr) |
| 1590 | return false; |
| 1591 | |
| 1592 | // 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] | 1593 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1594 | ConvertedType = ToType; |
| 1595 | return true; |
| 1596 | } |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1597 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1598 | // Beyond this point, both types need to be pointers |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1599 | // , including objective-c pointers. |
| 1600 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 1601 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType()) { |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1602 | ConvertedType = BuildSimilarlyQualifiedPointerType( |
| 1603 | FromType->getAs<ObjCObjectPointerType>(), |
| 1604 | ToPointeeType, |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1605 | ToType, Context); |
| 1606 | return true; |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1607 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1608 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1609 | if (!FromTypePtr) |
| 1610 | return false; |
| 1611 | |
| 1612 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1613 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1614 | // 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] | 1615 | // pointer conversion, so don't do all of the work below. |
| 1616 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
| 1617 | return false; |
| 1618 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1619 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1620 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1621 | // 4.10p2). |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1622 | if (FromPointeeType->isIncompleteOrObjectType() && |
| 1623 | ToPointeeType->isVoidType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1624 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1625 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1626 | ToType, Context); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1627 | return true; |
| 1628 | } |
| 1629 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1630 | // When we're overloading in C, we allow a special kind of pointer |
| 1631 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1632 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1633 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1634 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1635 | ToPointeeType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1636 | ToType, Context); |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1637 | return true; |
| 1638 | } |
| 1639 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1640 | // C++ [conv.ptr]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1641 | // |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1642 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1643 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1644 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1645 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1646 | // necessitates this conversion is ill-formed. The result of the |
| 1647 | // conversion is a pointer to the base class sub-object of the |
| 1648 | // derived class object. The null pointer value is converted to |
| 1649 | // the null pointer value of the destination type. |
| 1650 | // |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1651 | // Note that we do not check for ambiguity or inaccessibility |
| 1652 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1653 | if (getLangOptions().CPlusPlus && |
| 1654 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | d28f041 | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 1655 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | e6fb91f | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1656 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1657 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1658 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1659 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1660 | ToType, Context); |
| 1661 | return true; |
| 1662 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1663 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1664 | return false; |
| 1665 | } |
| 1666 | |
| 1667 | /// isObjCPointerConversion - Determines whether this is an |
| 1668 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1669 | /// with the same arguments and return values. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1670 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1671 | QualType& ConvertedType, |
| 1672 | bool &IncompatibleObjC) { |
| 1673 | if (!getLangOptions().ObjC1) |
| 1674 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1675 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1676 | // First, we handle all conversions on ObjC object pointer types. |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1677 | const ObjCObjectPointerType* ToObjCPtr = |
| 1678 | ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1679 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1680 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1681 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1682 | if (ToObjCPtr && FromObjCPtr) { |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1683 | // If the pointee types are the same (ignoring qualifications), |
| 1684 | // then this is not a pointer conversion. |
| 1685 | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
| 1686 | FromObjCPtr->getPointeeType())) |
| 1687 | return false; |
| 1688 | |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1689 | // 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] | 1690 | // pointer to any interface (in both directions). |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1691 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1692 | ConvertedType = ToType; |
| 1693 | return true; |
| 1694 | } |
| 1695 | // Conversions with Objective-C's id<...>. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1696 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1697 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1698 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1699 | /*compare=*/false)) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1700 | ConvertedType = ToType; |
| 1701 | return true; |
| 1702 | } |
| 1703 | // Objective C++: We're able to convert from a pointer to an |
| 1704 | // interface to a pointer to a different interface. |
| 1705 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | b397e43 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 1706 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 1707 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
| 1708 | if (getLangOptions().CPlusPlus && LHS && RHS && |
| 1709 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 1710 | FromObjCPtr->getPointeeType())) |
| 1711 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1712 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1713 | ToObjCPtr->getPointeeType(), |
| 1714 | ToType, Context); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1715 | return true; |
| 1716 | } |
| 1717 | |
| 1718 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1719 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1720 | // interfaces, which is permitted. However, we're going to |
| 1721 | // complain about it. |
| 1722 | IncompatibleObjC = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1723 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1724 | ToObjCPtr->getPointeeType(), |
| 1725 | ToType, Context); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1726 | return true; |
| 1727 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1728 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1729 | // 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] | 1730 | QualType ToPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1731 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1732 | ToPointeeType = ToCPtr->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1733 | else if (const BlockPointerType *ToBlockPtr = |
Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1734 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 879cc73 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 1735 | // 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] | 1736 | // to a block pointer type. |
| 1737 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
| 1738 | ConvertedType = ToType; |
| 1739 | return true; |
| 1740 | } |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1741 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1742 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1743 | else if (FromType->getAs<BlockPointerType>() && |
Fariborz Jahanian | e4951fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1744 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1745 | // 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] | 1746 | // pointer to any object. |
Fariborz Jahanian | e4951fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1747 | ConvertedType = ToType; |
| 1748 | return true; |
| 1749 | } |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1750 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1751 | return false; |
| 1752 | |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1753 | QualType FromPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1754 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1755 | FromPointeeType = FromCPtr->getPointeeType(); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1756 | else if (const BlockPointerType *FromBlockPtr = |
| 1757 | FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1758 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1759 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1760 | return false; |
| 1761 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1762 | // If we have pointers to pointers, recursively check whether this |
| 1763 | // is an Objective-C conversion. |
| 1764 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1765 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1766 | IncompatibleObjC)) { |
| 1767 | // We always complain about this conversion. |
| 1768 | IncompatibleObjC = true; |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1769 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1770 | return true; |
| 1771 | } |
Fariborz Jahanian | 42ffdb3 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1772 | // Allow conversion of pointee being objective-c pointer to another one; |
| 1773 | // as in I* to id. |
| 1774 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 1775 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 1776 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1777 | IncompatibleObjC)) { |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1778 | ConvertedType = Context.getPointerType(ConvertedType); |
Fariborz Jahanian | 42ffdb3 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1779 | return true; |
| 1780 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1781 | |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1782 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1783 | // differences in the argument and result types are in Objective-C |
| 1784 | // pointer conversions. If so, we permit the conversion (but |
| 1785 | // complain about it). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1786 | const FunctionProtoType *FromFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1787 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1788 | const FunctionProtoType *ToFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1789 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1790 | if (FromFunctionType && ToFunctionType) { |
| 1791 | // If the function types are exactly the same, this isn't an |
| 1792 | // Objective-C pointer conversion. |
| 1793 | if (Context.getCanonicalType(FromPointeeType) |
| 1794 | == Context.getCanonicalType(ToPointeeType)) |
| 1795 | return false; |
| 1796 | |
| 1797 | // Perform the quick checks that will tell us whether these |
| 1798 | // function types are obviously different. |
| 1799 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1800 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1801 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1802 | return false; |
| 1803 | |
| 1804 | bool HasObjCConversion = false; |
| 1805 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1806 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1807 | // Okay, the types match exactly. Nothing to do. |
| 1808 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1809 | ToFunctionType->getResultType(), |
| 1810 | ConvertedType, IncompatibleObjC)) { |
| 1811 | // Okay, we have an Objective-C pointer conversion. |
| 1812 | HasObjCConversion = true; |
| 1813 | } else { |
| 1814 | // Function types are too different. Abort. |
| 1815 | return false; |
| 1816 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1817 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1818 | // Check argument types. |
| 1819 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1820 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1821 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1822 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1823 | if (Context.getCanonicalType(FromArgType) |
| 1824 | == Context.getCanonicalType(ToArgType)) { |
| 1825 | // Okay, the types match exactly. Nothing to do. |
| 1826 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1827 | ConvertedType, IncompatibleObjC)) { |
| 1828 | // Okay, we have an Objective-C pointer conversion. |
| 1829 | HasObjCConversion = true; |
| 1830 | } else { |
| 1831 | // Argument types are too different. Abort. |
| 1832 | return false; |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | if (HasObjCConversion) { |
| 1837 | // We had an Objective-C conversion. Allow this pointer |
| 1838 | // conversion, but complain about it. |
| 1839 | ConvertedType = ToType; |
| 1840 | IncompatibleObjC = true; |
| 1841 | return true; |
| 1842 | } |
| 1843 | } |
| 1844 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1845 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1846 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1847 | |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1848 | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
| 1849 | QualType& ConvertedType) { |
| 1850 | QualType ToPointeeType; |
| 1851 | if (const BlockPointerType *ToBlockPtr = |
| 1852 | ToType->getAs<BlockPointerType>()) |
| 1853 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 1854 | else |
| 1855 | return false; |
| 1856 | |
| 1857 | QualType FromPointeeType; |
| 1858 | if (const BlockPointerType *FromBlockPtr = |
| 1859 | FromType->getAs<BlockPointerType>()) |
| 1860 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1861 | else |
| 1862 | return false; |
| 1863 | // We have pointer to blocks, check whether the only |
| 1864 | // differences in the argument and result types are in Objective-C |
| 1865 | // pointer conversions. If so, we permit the conversion. |
| 1866 | |
| 1867 | const FunctionProtoType *FromFunctionType |
| 1868 | = FromPointeeType->getAs<FunctionProtoType>(); |
| 1869 | const FunctionProtoType *ToFunctionType |
| 1870 | = ToPointeeType->getAs<FunctionProtoType>(); |
| 1871 | |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 1872 | if (!FromFunctionType || !ToFunctionType) |
| 1873 | return false; |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1874 | |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 1875 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1876 | return true; |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 1877 | |
| 1878 | // Perform the quick checks that will tell us whether these |
| 1879 | // function types are obviously different. |
| 1880 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1881 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
| 1882 | return false; |
| 1883 | |
| 1884 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
| 1885 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
| 1886 | if (FromEInfo != ToEInfo) |
| 1887 | return false; |
| 1888 | |
| 1889 | bool IncompatibleObjC = false; |
Fariborz Jahanian | 12834e1 | 2011-02-13 20:11:42 +0000 | [diff] [blame] | 1890 | if (Context.hasSameType(FromFunctionType->getResultType(), |
| 1891 | ToFunctionType->getResultType())) { |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 1892 | // Okay, the types match exactly. Nothing to do. |
| 1893 | } else { |
| 1894 | QualType RHS = FromFunctionType->getResultType(); |
| 1895 | QualType LHS = ToFunctionType->getResultType(); |
| 1896 | if ((!getLangOptions().CPlusPlus || !RHS->isRecordType()) && |
| 1897 | !RHS.hasQualifiers() && LHS.hasQualifiers()) |
| 1898 | LHS = LHS.getUnqualifiedType(); |
| 1899 | |
| 1900 | if (Context.hasSameType(RHS,LHS)) { |
| 1901 | // OK exact match. |
| 1902 | } else if (isObjCPointerConversion(RHS, LHS, |
| 1903 | ConvertedType, IncompatibleObjC)) { |
| 1904 | if (IncompatibleObjC) |
| 1905 | return false; |
| 1906 | // Okay, we have an Objective-C pointer conversion. |
| 1907 | } |
| 1908 | else |
| 1909 | return false; |
| 1910 | } |
| 1911 | |
| 1912 | // Check argument types. |
| 1913 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1914 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1915 | IncompatibleObjC = false; |
| 1916 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1917 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1918 | if (Context.hasSameType(FromArgType, ToArgType)) { |
| 1919 | // Okay, the types match exactly. Nothing to do. |
| 1920 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
| 1921 | ConvertedType, IncompatibleObjC)) { |
| 1922 | if (IncompatibleObjC) |
| 1923 | return false; |
| 1924 | // Okay, we have an Objective-C pointer conversion. |
| 1925 | } else |
| 1926 | // Argument types are too different. Abort. |
| 1927 | return false; |
| 1928 | } |
| 1929 | ConvertedType = ToType; |
| 1930 | return true; |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1933 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
| 1934 | /// for equlity of their argument types. Caller has already checked that |
| 1935 | /// they have same number of arguments. This routine assumes that Objective-C |
| 1936 | /// pointer types which only differ in their protocol qualifiers are equal. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1937 | bool Sema::FunctionArgTypesAreEqual(const FunctionProtoType *OldType, |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1938 | const FunctionProtoType *NewType) { |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1939 | if (!getLangOptions().ObjC1) |
| 1940 | return std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 1941 | NewType->arg_type_begin()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1942 | |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1943 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 1944 | N = NewType->arg_type_begin(), |
| 1945 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
| 1946 | QualType ToType = (*O); |
| 1947 | QualType FromType = (*N); |
| 1948 | if (ToType != FromType) { |
| 1949 | if (const PointerType *PTTo = ToType->getAs<PointerType>()) { |
| 1950 | if (const PointerType *PTFr = FromType->getAs<PointerType>()) |
Chandler Carruth | 27c9fe9 | 2010-05-06 00:15:06 +0000 | [diff] [blame] | 1951 | if ((PTTo->getPointeeType()->isObjCQualifiedIdType() && |
| 1952 | PTFr->getPointeeType()->isObjCQualifiedIdType()) || |
| 1953 | (PTTo->getPointeeType()->isObjCQualifiedClassType() && |
| 1954 | PTFr->getPointeeType()->isObjCQualifiedClassType())) |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1955 | continue; |
| 1956 | } |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1957 | else if (const ObjCObjectPointerType *PTTo = |
| 1958 | ToType->getAs<ObjCObjectPointerType>()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1959 | if (const ObjCObjectPointerType *PTFr = |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1960 | FromType->getAs<ObjCObjectPointerType>()) |
| 1961 | if (PTTo->getInterfaceDecl() == PTFr->getInterfaceDecl()) |
| 1962 | continue; |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1963 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1964 | return false; |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1965 | } |
| 1966 | } |
| 1967 | return true; |
| 1968 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1969 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1970 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1971 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1972 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1973 | /// conversions for which IsPointerConversion has already returned |
| 1974 | /// true. It returns true and produces a diagnostic if there was an |
| 1975 | /// error, or returns false otherwise. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1976 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1977 | CastKind &Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1978 | CXXCastPath& BasePath, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1979 | bool IgnoreBaseAccess) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1980 | QualType FromType = From->getType(); |
Argyrios Kyrtzidis | d6ea6bd | 2010-09-28 14:54:11 +0000 | [diff] [blame] | 1981 | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1982 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1983 | Kind = CK_BitCast; |
| 1984 | |
Chandler Carruth | ffab873 | 2011-04-09 07:32:05 +0000 | [diff] [blame] | 1985 | if (!IsCStyleOrFunctionalCast && |
| 1986 | Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy) && |
| 1987 | From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 1988 | DiagRuntimeBehavior(From->getExprLoc(), From, |
Chandler Carruth | 66a7b04 | 2011-04-09 07:48:17 +0000 | [diff] [blame] | 1989 | PDiag(diag::warn_impcast_bool_to_null_pointer) |
| 1990 | << ToType << From->getSourceRange()); |
Douglas Gregor | 4038cf4 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 1991 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1992 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1993 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1994 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1995 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | 1e57a3f | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1996 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1997 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 1998 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1999 | // We must have a derived-to-base conversion. Check an |
| 2000 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2001 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 2002 | From->getExprLoc(), |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2003 | From->getSourceRange(), &BasePath, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2004 | IgnoreBaseAccess)) |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2005 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2006 | |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2007 | // The conversion was successful. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2008 | Kind = CK_DerivedToBase; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2009 | } |
| 2010 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2011 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2012 | FromType->getAs<ObjCObjectPointerType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2013 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2014 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2015 | // Objective-C++ conversions are always okay. |
| 2016 | // FIXME: We should have a different class of conversions for the |
| 2017 | // Objective-C++ implicit conversions. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2018 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2019 | return false; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2020 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2021 | } |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2022 | |
| 2023 | // We shouldn't fall into this case unless it's valid for other |
| 2024 | // reasons. |
| 2025 | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 2026 | Kind = CK_NullToPointer; |
| 2027 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2028 | return false; |
| 2029 | } |
| 2030 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2031 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 2032 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 2033 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 2034 | /// If so, returns true and places the converted type (that might differ from |
| 2035 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 2036 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2037 | QualType ToType, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2038 | bool InOverloadResolution, |
| 2039 | QualType &ConvertedType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2040 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2041 | if (!ToTypePtr) |
| 2042 | return false; |
| 2043 | |
| 2044 | // 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] | 2045 | if (From->isNullPointerConstant(Context, |
| 2046 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 2047 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2048 | ConvertedType = ToType; |
| 2049 | return true; |
| 2050 | } |
| 2051 | |
| 2052 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2053 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2054 | if (!FromTypePtr) |
| 2055 | return false; |
| 2056 | |
| 2057 | // A pointer to member of B can be converted to a pointer to member of D, |
| 2058 | // where D is derived from B (C++ 4.11p2). |
| 2059 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 2060 | QualType ToClass(ToTypePtr->getClass(), 0); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2061 | |
Douglas Gregor | 7f6ae69 | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2062 | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
| 2063 | !RequireCompleteType(From->getLocStart(), ToClass, PDiag()) && |
| 2064 | IsDerivedFrom(ToClass, FromClass)) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2065 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 2066 | ToClass.getTypePtr()); |
| 2067 | return true; |
| 2068 | } |
| 2069 | |
| 2070 | return false; |
| 2071 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2072 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2073 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 2074 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2075 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2076 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 2077 | /// true and produces a diagnostic if there was an error, or returns false |
| 2078 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2079 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2080 | CastKind &Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2081 | CXXCastPath &BasePath, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2082 | bool IgnoreBaseAccess) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2083 | QualType FromType = From->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2084 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2085 | if (!FromPtrType) { |
| 2086 | // This must be a null pointer to member pointer conversion |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2087 | assert(From->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2088 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2089 | "Expr must be null pointer constant!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2090 | Kind = CK_NullToMemberPointer; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2091 | return false; |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2092 | } |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2093 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2094 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2095 | assert(ToPtrType && "No member pointer cast has a target type " |
| 2096 | "that is not a member pointer."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2097 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2098 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 2099 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2100 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2101 | // FIXME: What about dependent types? |
| 2102 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 2103 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2104 | |
Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2105 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 2106 | /*DetectVirtual=*/true); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2107 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 2108 | assert(DerivationOkay && |
| 2109 | "Should not have been called if derivation isn't OK."); |
| 2110 | (void)DerivationOkay; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2111 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2112 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 2113 | getUnqualifiedType())) { |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2114 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 2115 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 2116 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 2117 | return true; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2118 | } |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2119 | |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2120 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2121 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 2122 | << FromClass << ToClass << QualType(VBase, 0) |
| 2123 | << From->getSourceRange(); |
| 2124 | return true; |
| 2125 | } |
| 2126 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2127 | if (!IgnoreBaseAccess) |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2128 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 2129 | Paths.front(), |
| 2130 | diag::err_downcast_from_inaccessible_base); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2131 | |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2132 | // Must be a base to derived member conversion. |
Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2133 | BuildBasePathArray(Paths, BasePath); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2134 | Kind = CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2135 | return false; |
| 2136 | } |
| 2137 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2138 | /// IsQualificationConversion - Determines whether the conversion from |
| 2139 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 2140 | /// (C++ 4.4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2141 | bool |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2142 | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2143 | bool CStyle) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2144 | FromType = Context.getCanonicalType(FromType); |
| 2145 | ToType = Context.getCanonicalType(ToType); |
| 2146 | |
| 2147 | // If FromType and ToType are the same type, this is not a |
| 2148 | // qualification conversion. |
Sebastian Redl | cbdffb1 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 2149 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2150 | return false; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2151 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2152 | // (C++ 4.4p4): |
| 2153 | // A conversion can add cv-qualifiers at levels other than the first |
| 2154 | // in multi-level pointers, subject to the following rules: [...] |
| 2155 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2156 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2157 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2158 | // Within each iteration of the loop, we check the qualifiers to |
| 2159 | // determine if this still looks like a qualification |
| 2160 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2161 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2162 | // until there are no more pointers or pointers-to-members left to |
| 2163 | // unwrap. |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2164 | UnwrappedAnyPointer = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2165 | |
| 2166 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 2167 | // 2,j, and similarly for volatile. |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2168 | if (!CStyle && !ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2169 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2170 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2171 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 2172 | // every cv for 0 < k < j. |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2173 | if (!CStyle && FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2174 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2175 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2176 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2177 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 2178 | // include const. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2179 | PreviousToQualsIncludeConst |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2180 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2181 | } |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2182 | |
| 2183 | // We are left with FromType and ToType being the pointee types |
| 2184 | // after unwrapping the original FromType and ToType the same number |
| 2185 | // of types. If we unwrapped any pointers, and if FromType and |
| 2186 | // ToType have the same unqualified type (since we checked |
| 2187 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2188 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2191 | /// Determines whether there is a user-defined conversion sequence |
| 2192 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 2193 | /// ToType. If such a conversion exists, User will contain the |
| 2194 | /// user-defined conversion sequence that performs such a conversion |
| 2195 | /// and this routine will return true. Otherwise, this routine returns |
| 2196 | /// false and User is unspecified. |
| 2197 | /// |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2198 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 2199 | /// "explicit" conversion functions as well as non-explicit conversion |
| 2200 | /// functions (C++0x [class.conv.fct]p2). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2201 | static OverloadingResult |
| 2202 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 2203 | UserDefinedConversionSequence& User, |
| 2204 | OverloadCandidateSet& CandidateSet, |
| 2205 | bool AllowExplicit) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2206 | // Whether we will only visit constructors. |
| 2207 | bool ConstructorsOnly = false; |
| 2208 | |
| 2209 | // If the type we are conversion to is a class type, enumerate its |
| 2210 | // constructors. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2211 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2212 | // C++ [over.match.ctor]p1: |
| 2213 | // When objects of class type are direct-initialized (8.5), or |
| 2214 | // copy-initialized from an expression of the same or a |
| 2215 | // derived class type (8.5), overload resolution selects the |
| 2216 | // constructor. [...] For copy-initialization, the candidate |
| 2217 | // functions are all the converting constructors (12.3.1) of |
| 2218 | // that class. The argument list is the expression-list within |
| 2219 | // the parentheses of the initializer. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2220 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2221 | (From->getType()->getAs<RecordType>() && |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2222 | S.IsDerivedFrom(From->getType(), ToType))) |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2223 | ConstructorsOnly = true; |
| 2224 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2225 | if (S.RequireCompleteType(From->getLocStart(), ToType, S.PDiag())) { |
Douglas Gregor | 3ec1bf2 | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 2226 | // We're not going to find any constructors. |
| 2227 | } else if (CXXRecordDecl *ToRecordDecl |
| 2228 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2229 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2230 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(ToRecordDecl); |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2231 | Con != ConEnd; ++Con) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2232 | NamedDecl *D = *Con; |
| 2233 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2234 | |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2235 | // Find the constructor (which may be a template). |
| 2236 | CXXConstructorDecl *Constructor = 0; |
| 2237 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2238 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2239 | if (ConstructorTmpl) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2240 | Constructor |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2241 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 2242 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2243 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2244 | |
Fariborz Jahanian | 11a8e95 | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 2245 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | d20e795 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 2246 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2247 | if (ConstructorTmpl) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2248 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2249 | /*ExplicitArgs*/ 0, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2250 | &From, 1, CandidateSet, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2251 | /*SuppressUserConversions=*/ |
| 2252 | !ConstructorsOnly); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2253 | else |
Fariborz Jahanian | b3c44f9 | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 2254 | // Allow one user-defined conversion when user specifies a |
| 2255 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2256 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 2257 | &From, 1, CandidateSet, |
| 2258 | /*SuppressUserConversions=*/ |
| 2259 | !ConstructorsOnly); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2260 | } |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2261 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2262 | } |
| 2263 | } |
| 2264 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2265 | // Enumerate conversion functions, if we're allowed to. |
| 2266 | if (ConstructorsOnly) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2267 | } else if (S.RequireCompleteType(From->getLocStart(), From->getType(), |
| 2268 | S.PDiag(0) << From->getSourceRange())) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2269 | // No conversion functions from incomplete types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2270 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2271 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2272 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2273 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 2274 | // Add all of the conversion functions as candidates. |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2275 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | f4061e3 | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 2276 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2277 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2278 | E = Conversions->end(); I != E; ++I) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2279 | DeclAccessPair FoundDecl = I.getPair(); |
| 2280 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2281 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2282 | if (isa<UsingShadowDecl>(D)) |
| 2283 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2284 | |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2285 | CXXConversionDecl *Conv; |
| 2286 | FunctionTemplateDecl *ConvTemplate; |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2287 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 2288 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2289 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2290 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2291 | |
| 2292 | if (AllowExplicit || !Conv->isExplicit()) { |
| 2293 | if (ConvTemplate) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2294 | S.AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
| 2295 | ActingContext, From, ToType, |
| 2296 | CandidateSet); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2297 | else |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2298 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, |
| 2299 | From, ToType, CandidateSet); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2300 | } |
| 2301 | } |
| 2302 | } |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2303 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2304 | |
| 2305 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 2306 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2307 | case OR_Success: |
| 2308 | // Record the standard conversion we used and the conversion function. |
| 2309 | if (CXXConstructorDecl *Constructor |
| 2310 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2311 | S.MarkDeclarationReferenced(From->getLocStart(), Constructor); |
| 2312 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2313 | // C++ [over.ics.user]p1: |
| 2314 | // If the user-defined conversion is specified by a |
| 2315 | // constructor (12.3.1), the initial standard conversion |
| 2316 | // sequence converts the source type to the type required by |
| 2317 | // the argument of the constructor. |
| 2318 | // |
| 2319 | QualType ThisType = Constructor->getThisType(S.Context); |
| 2320 | if (Best->Conversions[0].isEllipsis()) |
| 2321 | User.EllipsisConversion = true; |
| 2322 | else { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2323 | User.Before = Best->Conversions[0].Standard; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2324 | User.EllipsisConversion = false; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2325 | } |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2326 | User.ConversionFunction = Constructor; |
Douglas Gregor | 2bbfba0 | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 2327 | User.FoundConversionFunction = Best->FoundDecl.getDecl(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2328 | User.After.setAsIdentityConversion(); |
| 2329 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 2330 | User.After.setAllToTypes(ToType); |
| 2331 | return OR_Success; |
| 2332 | } else if (CXXConversionDecl *Conversion |
| 2333 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2334 | S.MarkDeclarationReferenced(From->getLocStart(), Conversion); |
| 2335 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2336 | // C++ [over.ics.user]p1: |
| 2337 | // |
| 2338 | // [...] If the user-defined conversion is specified by a |
| 2339 | // conversion function (12.3.2), the initial standard |
| 2340 | // conversion sequence converts the source type to the |
| 2341 | // implicit object parameter of the conversion function. |
| 2342 | User.Before = Best->Conversions[0].Standard; |
| 2343 | User.ConversionFunction = Conversion; |
Douglas Gregor | 2bbfba0 | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 2344 | User.FoundConversionFunction = Best->FoundDecl.getDecl(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2345 | User.EllipsisConversion = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2346 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2347 | // C++ [over.ics.user]p2: |
| 2348 | // The second standard conversion sequence converts the |
| 2349 | // result of the user-defined conversion to the target type |
| 2350 | // for the sequence. Since an implicit conversion sequence |
| 2351 | // is an initialization, the special rules for |
| 2352 | // initialization by user-defined conversion apply when |
| 2353 | // selecting the best user-defined conversion for a |
| 2354 | // user-defined conversion sequence (see 13.3.3 and |
| 2355 | // 13.3.3.1). |
| 2356 | User.After = Best->FinalConversion; |
| 2357 | return OR_Success; |
| 2358 | } else { |
| 2359 | llvm_unreachable("Not a constructor or conversion function?"); |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2360 | return OR_No_Viable_Function; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2363 | case OR_No_Viable_Function: |
| 2364 | return OR_No_Viable_Function; |
| 2365 | case OR_Deleted: |
| 2366 | // No conversion here! We're done. |
| 2367 | return OR_Deleted; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2368 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2369 | case OR_Ambiguous: |
| 2370 | return OR_Ambiguous; |
| 2371 | } |
| 2372 | |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2373 | return OR_No_Viable_Function; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2374 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2375 | |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2376 | bool |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2377 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2378 | ImplicitConversionSequence ICS; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2379 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2380 | OverloadingResult OvResult = |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2381 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2382 | CandidateSet, false); |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2383 | if (OvResult == OR_Ambiguous) |
| 2384 | Diag(From->getSourceRange().getBegin(), |
| 2385 | diag::err_typecheck_ambiguous_condition) |
| 2386 | << From->getType() << ToType << From->getSourceRange(); |
| 2387 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 2388 | Diag(From->getSourceRange().getBegin(), |
| 2389 | diag::err_typecheck_nonviable_condition) |
| 2390 | << From->getType() << ToType << From->getSourceRange(); |
| 2391 | else |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2392 | return false; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2393 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &From, 1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2394 | return true; |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2395 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2396 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2397 | /// CompareImplicitConversionSequences - Compare two implicit |
| 2398 | /// conversion sequences to determine whether one is better than the |
| 2399 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2400 | static ImplicitConversionSequence::CompareKind |
| 2401 | CompareImplicitConversionSequences(Sema &S, |
| 2402 | const ImplicitConversionSequence& ICS1, |
| 2403 | const ImplicitConversionSequence& ICS2) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2404 | { |
| 2405 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 2406 | // conversion sequences (as defined in 13.3.3.1) |
| 2407 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 2408 | // conversion sequence than a user-defined conversion sequence or |
| 2409 | // an ellipsis conversion sequence, and |
| 2410 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 2411 | // conversion sequence than an ellipsis conversion sequence |
| 2412 | // (13.3.3.1.3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2413 | // |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2414 | // C++0x [over.best.ics]p10: |
| 2415 | // For the purpose of ranking implicit conversion sequences as |
| 2416 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 2417 | // treated as a user-defined sequence that is indistinguishable |
| 2418 | // from any other user-defined conversion sequence. |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2419 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 2420 | return ImplicitConversionSequence::Better; |
| 2421 | else if (ICS2.getKindRank() < ICS1.getKindRank()) |
| 2422 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2423 | |
Benjamin Kramer | 98ff7f8 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 2424 | // The following checks require both conversion sequences to be of |
| 2425 | // the same kind. |
| 2426 | if (ICS1.getKind() != ICS2.getKind()) |
| 2427 | return ImplicitConversionSequence::Indistinguishable; |
| 2428 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2429 | // Two implicit conversion sequences of the same form are |
| 2430 | // indistinguishable conversion sequences unless one of the |
| 2431 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2432 | if (ICS1.isStandard()) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2433 | return CompareStandardConversionSequences(S, ICS1.Standard, ICS2.Standard); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2434 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2435 | // User-defined conversion sequence U1 is a better conversion |
| 2436 | // sequence than another user-defined conversion sequence U2 if |
| 2437 | // they contain the same user-defined conversion function or |
| 2438 | // constructor and if the second standard conversion sequence of |
| 2439 | // U1 is better than the second standard conversion sequence of |
| 2440 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2441 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2442 | ICS2.UserDefined.ConversionFunction) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2443 | return CompareStandardConversionSequences(S, |
| 2444 | ICS1.UserDefined.After, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2445 | ICS2.UserDefined.After); |
| 2446 | } |
| 2447 | |
| 2448 | return ImplicitConversionSequence::Indistinguishable; |
| 2449 | } |
| 2450 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2451 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 2452 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 2453 | Qualifiers Quals; |
| 2454 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2455 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2456 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2457 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2458 | return Context.hasSameUnqualifiedType(T1, T2); |
| 2459 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2460 | |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2461 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 2462 | // determine if one is a proper subset of the other. |
| 2463 | static ImplicitConversionSequence::CompareKind |
| 2464 | compareStandardConversionSubsets(ASTContext &Context, |
| 2465 | const StandardConversionSequence& SCS1, |
| 2466 | const StandardConversionSequence& SCS2) { |
| 2467 | ImplicitConversionSequence::CompareKind Result |
| 2468 | = ImplicitConversionSequence::Indistinguishable; |
| 2469 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2470 | // the identity conversion sequence is considered to be a subsequence of |
Douglas Gregor | e87561a | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 2471 | // any non-identity conversion sequence |
| 2472 | if (SCS1.ReferenceBinding == SCS2.ReferenceBinding) { |
| 2473 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 2474 | return ImplicitConversionSequence::Better; |
| 2475 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 2476 | return ImplicitConversionSequence::Worse; |
| 2477 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2478 | |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2479 | if (SCS1.Second != SCS2.Second) { |
| 2480 | if (SCS1.Second == ICK_Identity) |
| 2481 | Result = ImplicitConversionSequence::Better; |
| 2482 | else if (SCS2.Second == ICK_Identity) |
| 2483 | Result = ImplicitConversionSequence::Worse; |
| 2484 | else |
| 2485 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2486 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2487 | return ImplicitConversionSequence::Indistinguishable; |
| 2488 | |
| 2489 | if (SCS1.Third == SCS2.Third) { |
| 2490 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 2491 | : ImplicitConversionSequence::Indistinguishable; |
| 2492 | } |
| 2493 | |
| 2494 | if (SCS1.Third == ICK_Identity) |
| 2495 | return Result == ImplicitConversionSequence::Worse |
| 2496 | ? ImplicitConversionSequence::Indistinguishable |
| 2497 | : ImplicitConversionSequence::Better; |
| 2498 | |
| 2499 | if (SCS2.Third == ICK_Identity) |
| 2500 | return Result == ImplicitConversionSequence::Better |
| 2501 | ? ImplicitConversionSequence::Indistinguishable |
| 2502 | : ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2503 | |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2504 | return ImplicitConversionSequence::Indistinguishable; |
| 2505 | } |
| 2506 | |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2507 | /// \brief Determine whether one of the given reference bindings is better |
| 2508 | /// than the other based on what kind of bindings they are. |
| 2509 | static bool isBetterReferenceBindingKind(const StandardConversionSequence &SCS1, |
| 2510 | const StandardConversionSequence &SCS2) { |
| 2511 | // C++0x [over.ics.rank]p3b4: |
| 2512 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 2513 | // implicit object parameter of a non-static member function declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2514 | // without a ref-qualifier, and *either* S1 binds an rvalue reference |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2515 | // 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] | 2516 | // lvalue reference to a function lvalue and S2 binds an rvalue |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2517 | // reference*. |
| 2518 | // |
| 2519 | // FIXME: Rvalue references. We're going rogue with the above edits, |
| 2520 | // because the semantics in the current C++0x working paper (N3225 at the |
| 2521 | // time of this writing) break the standard definition of std::forward |
| 2522 | // and std::reference_wrapper when dealing with references to functions. |
| 2523 | // Proposed wording changes submitted to CWG for consideration. |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 2524 | if (SCS1.BindsImplicitObjectArgumentWithoutRefQualifier || |
| 2525 | SCS2.BindsImplicitObjectArgumentWithoutRefQualifier) |
| 2526 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2527 | |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2528 | return (!SCS1.IsLvalueReference && SCS1.BindsToRvalue && |
| 2529 | SCS2.IsLvalueReference) || |
| 2530 | (SCS1.IsLvalueReference && SCS1.BindsToFunctionLvalue && |
| 2531 | !SCS2.IsLvalueReference); |
| 2532 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2533 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2534 | /// CompareStandardConversionSequences - Compare two standard |
| 2535 | /// conversion sequences to determine whether one is better than the |
| 2536 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2537 | static ImplicitConversionSequence::CompareKind |
| 2538 | CompareStandardConversionSequences(Sema &S, |
| 2539 | const StandardConversionSequence& SCS1, |
| 2540 | const StandardConversionSequence& SCS2) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2541 | { |
| 2542 | // Standard conversion sequence S1 is a better conversion sequence |
| 2543 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 2544 | |
| 2545 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 2546 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 2547 | // excluding any Lvalue Transformation; the identity conversion |
| 2548 | // sequence is considered to be a subsequence of any |
| 2549 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2550 | if (ImplicitConversionSequence::CompareKind CK |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2551 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2552 | return CK; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2553 | |
| 2554 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 2555 | // defined below), or, if not that, |
| 2556 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 2557 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 2558 | if (Rank1 < Rank2) |
| 2559 | return ImplicitConversionSequence::Better; |
| 2560 | else if (Rank2 < Rank1) |
| 2561 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2562 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2563 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 2564 | // are indistinguishable unless one of the following rules |
| 2565 | // applies: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2566 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2567 | // A conversion that is not a conversion of a pointer, or |
| 2568 | // pointer to member, to bool is better than another conversion |
| 2569 | // that is such a conversion. |
| 2570 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 2571 | return SCS2.isPointerConversionToBool() |
| 2572 | ? ImplicitConversionSequence::Better |
| 2573 | : ImplicitConversionSequence::Worse; |
| 2574 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2575 | // C++ [over.ics.rank]p4b2: |
| 2576 | // |
| 2577 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2578 | // conversion of B* to A* is better than conversion of B* to |
| 2579 | // void*, and conversion of A* to void* is better than conversion |
| 2580 | // of B* to void*. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2581 | bool SCS1ConvertsToVoid |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2582 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2583 | bool SCS2ConvertsToVoid |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2584 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2585 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 2586 | // Exactly one of the conversion sequences is a conversion to |
| 2587 | // a void pointer; it's the worse conversion. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2588 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 2589 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2590 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 2591 | // Neither conversion sequence converts to a void pointer; compare |
| 2592 | // their derived-to-base conversions. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2593 | if (ImplicitConversionSequence::CompareKind DerivedCK |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2594 | = CompareDerivedToBaseConversions(S, SCS1, SCS2)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2595 | return DerivedCK; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2596 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 2597 | // Both conversion sequences are conversions to void |
| 2598 | // pointers. Compare the source types to determine if there's an |
| 2599 | // inheritance relationship in their sources. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2600 | QualType FromType1 = SCS1.getFromType(); |
| 2601 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2602 | |
| 2603 | // Adjust the types we're converting from via the array-to-pointer |
| 2604 | // conversion, if we need to. |
| 2605 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2606 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2607 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2608 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2609 | |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2610 | QualType FromPointee1 |
| 2611 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 2612 | QualType FromPointee2 |
| 2613 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2614 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2615 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2616 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2617 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2618 | return ImplicitConversionSequence::Worse; |
| 2619 | |
| 2620 | // Objective-C++: If one interface is more specific than the |
| 2621 | // other, it is the better one. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2622 | const ObjCObjectType* FromIface1 = FromPointee1->getAs<ObjCObjectType>(); |
| 2623 | const ObjCObjectType* FromIface2 = FromPointee2->getAs<ObjCObjectType>(); |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2624 | if (FromIface1 && FromIface1) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2625 | if (S.Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2626 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2627 | else if (S.Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2628 | return ImplicitConversionSequence::Worse; |
| 2629 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2630 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2631 | |
| 2632 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 2633 | // bullet 3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2634 | if (ImplicitConversionSequence::CompareKind QualCK |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2635 | = CompareQualificationConversions(S, SCS1, SCS2)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2636 | return QualCK; |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2637 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2638 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2639 | // Check for a better reference binding based on the kind of bindings. |
| 2640 | if (isBetterReferenceBindingKind(SCS1, SCS2)) |
| 2641 | return ImplicitConversionSequence::Better; |
| 2642 | else if (isBetterReferenceBindingKind(SCS2, SCS1)) |
| 2643 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2644 | |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 2645 | // C++ [over.ics.rank]p3b4: |
| 2646 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 2647 | // which the references refer are the same type except for |
| 2648 | // top-level cv-qualifiers, and the type to which the reference |
| 2649 | // initialized by S2 refers is more cv-qualified than the type |
| 2650 | // to which the reference initialized by S1 refers. |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2651 | QualType T1 = SCS1.getToType(2); |
| 2652 | QualType T2 = SCS2.getToType(2); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2653 | T1 = S.Context.getCanonicalType(T1); |
| 2654 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2655 | Qualifiers T1Quals, T2Quals; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2656 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2657 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2658 | if (UnqualT1 == UnqualT2) { |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2659 | // If the type is an array type, promote the element qualifiers to the |
| 2660 | // type for comparison. |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2661 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2662 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2663 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2664 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2665 | if (T2.isMoreQualifiedThan(T1)) |
| 2666 | return ImplicitConversionSequence::Better; |
| 2667 | else if (T1.isMoreQualifiedThan(T2)) |
| 2668 | return ImplicitConversionSequence::Worse; |
| 2669 | } |
| 2670 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2671 | |
| 2672 | return ImplicitConversionSequence::Indistinguishable; |
| 2673 | } |
| 2674 | |
| 2675 | /// CompareQualificationConversions - Compares two standard conversion |
| 2676 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2677 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 2678 | ImplicitConversionSequence::CompareKind |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2679 | CompareQualificationConversions(Sema &S, |
| 2680 | const StandardConversionSequence& SCS1, |
| 2681 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | 4b62ec6 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 2682 | // C++ 13.3.3.2p3: |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2683 | // -- S1 and S2 differ only in their qualification conversion and |
| 2684 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 2685 | // cv-qualification signature of type T1 is a proper subset of |
| 2686 | // the cv-qualification signature of type T2, and S1 is not the |
| 2687 | // deprecated string literal array-to-pointer conversion (4.2). |
| 2688 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 2689 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 2690 | return ImplicitConversionSequence::Indistinguishable; |
| 2691 | |
| 2692 | // FIXME: the example in the standard doesn't use a qualification |
| 2693 | // conversion (!) |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2694 | QualType T1 = SCS1.getToType(2); |
| 2695 | QualType T2 = SCS2.getToType(2); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2696 | T1 = S.Context.getCanonicalType(T1); |
| 2697 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2698 | Qualifiers T1Quals, T2Quals; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2699 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2700 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2701 | |
| 2702 | // If the types are the same, we won't learn anything by unwrapped |
| 2703 | // them. |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2704 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2705 | return ImplicitConversionSequence::Indistinguishable; |
| 2706 | |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2707 | // If the type is an array type, promote the element qualifiers to the type |
| 2708 | // for comparison. |
| 2709 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2710 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2711 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2712 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2713 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2714 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2715 | = ImplicitConversionSequence::Indistinguishable; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2716 | while (S.Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2717 | // Within each iteration of the loop, we check the qualifiers to |
| 2718 | // determine if this still looks like a qualification |
| 2719 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2720 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2721 | // until there are no more pointers or pointers-to-members left |
| 2722 | // to unwrap. This essentially mimics what |
| 2723 | // IsQualificationConversion does, but here we're checking for a |
| 2724 | // strict subset of qualifiers. |
| 2725 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 2726 | // The qualifiers are the same, so this doesn't tell us anything |
| 2727 | // about how the sequences rank. |
| 2728 | ; |
| 2729 | else if (T2.isMoreQualifiedThan(T1)) { |
| 2730 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 2731 | if (Result == ImplicitConversionSequence::Worse) |
| 2732 | // Neither has qualifiers that are a subset of the other's |
| 2733 | // qualifiers. |
| 2734 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2735 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2736 | Result = ImplicitConversionSequence::Better; |
| 2737 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 2738 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 2739 | if (Result == ImplicitConversionSequence::Better) |
| 2740 | // Neither has qualifiers that are a subset of the other's |
| 2741 | // qualifiers. |
| 2742 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2743 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2744 | Result = ImplicitConversionSequence::Worse; |
| 2745 | } else { |
| 2746 | // Qualifiers are disjoint. |
| 2747 | return ImplicitConversionSequence::Indistinguishable; |
| 2748 | } |
| 2749 | |
| 2750 | // If the types after this point are equivalent, we're done. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2751 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2752 | break; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2755 | // Check that the winning standard conversion sequence isn't using |
| 2756 | // the deprecated string literal array to pointer conversion. |
| 2757 | switch (Result) { |
| 2758 | case ImplicitConversionSequence::Better: |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2759 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2760 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2761 | break; |
| 2762 | |
| 2763 | case ImplicitConversionSequence::Indistinguishable: |
| 2764 | break; |
| 2765 | |
| 2766 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2767 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2768 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2769 | break; |
| 2770 | } |
| 2771 | |
| 2772 | return Result; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2773 | } |
| 2774 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2775 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 2776 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2777 | /// various kinds of derived-to-base conversions (C++ |
| 2778 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 2779 | /// conversions between Objective-C interface types. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2780 | ImplicitConversionSequence::CompareKind |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2781 | CompareDerivedToBaseConversions(Sema &S, |
| 2782 | const StandardConversionSequence& SCS1, |
| 2783 | const StandardConversionSequence& SCS2) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2784 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2785 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2786 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2787 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2788 | |
| 2789 | // Adjust the types we're converting from via the array-to-pointer |
| 2790 | // conversion, if we need to. |
| 2791 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2792 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2793 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2794 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2795 | |
| 2796 | // Canonicalize all of the types. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2797 | FromType1 = S.Context.getCanonicalType(FromType1); |
| 2798 | ToType1 = S.Context.getCanonicalType(ToType1); |
| 2799 | FromType2 = S.Context.getCanonicalType(FromType2); |
| 2800 | ToType2 = S.Context.getCanonicalType(ToType2); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2801 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2802 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2803 | // |
| 2804 | // If class B is derived directly or indirectly from class A and |
| 2805 | // class C is derived directly or indirectly from B, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2806 | // |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2807 | // Compare based on pointer conversions. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2808 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 2809 | SCS2.Second == ICK_Pointer_Conversion && |
| 2810 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 2811 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 2812 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2813 | QualType FromPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2814 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2815 | QualType ToPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2816 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2817 | QualType FromPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2818 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2819 | QualType ToPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2820 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2821 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2822 | // -- 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] | 2823 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2824 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2825 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2826 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2827 | return ImplicitConversionSequence::Worse; |
| 2828 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2829 | |
| 2830 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 2831 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2832 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2833 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2834 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2835 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 2836 | } |
| 2837 | } else if (SCS1.Second == ICK_Pointer_Conversion && |
| 2838 | SCS2.Second == ICK_Pointer_Conversion) { |
| 2839 | const ObjCObjectPointerType *FromPtr1 |
| 2840 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 2841 | const ObjCObjectPointerType *FromPtr2 |
| 2842 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 2843 | const ObjCObjectPointerType *ToPtr1 |
| 2844 | = ToType1->getAs<ObjCObjectPointerType>(); |
| 2845 | const ObjCObjectPointerType *ToPtr2 |
| 2846 | = ToType2->getAs<ObjCObjectPointerType>(); |
| 2847 | |
| 2848 | if (FromPtr1 && FromPtr2 && ToPtr1 && ToPtr2) { |
| 2849 | // Apply the same conversion ranking rules for Objective-C pointer types |
| 2850 | // that we do for C++ pointers to class types. However, we employ the |
| 2851 | // Objective-C pseudo-subtyping relationship used for assignment of |
| 2852 | // Objective-C pointer types. |
| 2853 | bool FromAssignLeft |
| 2854 | = S.Context.canAssignObjCInterfaces(FromPtr1, FromPtr2); |
| 2855 | bool FromAssignRight |
| 2856 | = S.Context.canAssignObjCInterfaces(FromPtr2, FromPtr1); |
| 2857 | bool ToAssignLeft |
| 2858 | = S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2); |
| 2859 | bool ToAssignRight |
| 2860 | = S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1); |
| 2861 | |
| 2862 | // A conversion to an a non-id object pointer type or qualified 'id' |
| 2863 | // type is better than a conversion to 'id'. |
| 2864 | if (ToPtr1->isObjCIdType() && |
| 2865 | (ToPtr2->isObjCQualifiedIdType() || ToPtr2->getInterfaceDecl())) |
| 2866 | return ImplicitConversionSequence::Worse; |
| 2867 | if (ToPtr2->isObjCIdType() && |
| 2868 | (ToPtr1->isObjCQualifiedIdType() || ToPtr1->getInterfaceDecl())) |
| 2869 | return ImplicitConversionSequence::Better; |
| 2870 | |
| 2871 | // A conversion to a non-id object pointer type is better than a |
| 2872 | // conversion to a qualified 'id' type |
| 2873 | if (ToPtr1->isObjCQualifiedIdType() && ToPtr2->getInterfaceDecl()) |
| 2874 | return ImplicitConversionSequence::Worse; |
| 2875 | if (ToPtr2->isObjCQualifiedIdType() && ToPtr1->getInterfaceDecl()) |
| 2876 | return ImplicitConversionSequence::Better; |
| 2877 | |
| 2878 | // A conversion to an a non-Class object pointer type or qualified 'Class' |
| 2879 | // type is better than a conversion to 'Class'. |
| 2880 | if (ToPtr1->isObjCClassType() && |
| 2881 | (ToPtr2->isObjCQualifiedClassType() || ToPtr2->getInterfaceDecl())) |
| 2882 | return ImplicitConversionSequence::Worse; |
| 2883 | if (ToPtr2->isObjCClassType() && |
| 2884 | (ToPtr1->isObjCQualifiedClassType() || ToPtr1->getInterfaceDecl())) |
| 2885 | return ImplicitConversionSequence::Better; |
| 2886 | |
| 2887 | // A conversion to a non-Class object pointer type is better than a |
| 2888 | // conversion to a qualified 'Class' type. |
| 2889 | if (ToPtr1->isObjCQualifiedClassType() && ToPtr2->getInterfaceDecl()) |
| 2890 | return ImplicitConversionSequence::Worse; |
| 2891 | if (ToPtr2->isObjCQualifiedClassType() && ToPtr1->getInterfaceDecl()) |
| 2892 | return ImplicitConversionSequence::Better; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2893 | |
Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 2894 | // -- "conversion of C* to B* is better than conversion of C* to A*," |
| 2895 | if (S.Context.hasSameType(FromType1, FromType2) && |
| 2896 | !FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType() && |
| 2897 | (ToAssignLeft != ToAssignRight)) |
| 2898 | return ToAssignLeft? ImplicitConversionSequence::Worse |
| 2899 | : ImplicitConversionSequence::Better; |
| 2900 | |
| 2901 | // -- "conversion of B* to A* is better than conversion of C* to A*," |
| 2902 | if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) && |
| 2903 | (FromAssignLeft != FromAssignRight)) |
| 2904 | return FromAssignLeft? ImplicitConversionSequence::Better |
| 2905 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2906 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2907 | } |
Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 2908 | |
Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2909 | // Ranking of member-pointer types. |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2910 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 2911 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 2912 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2913 | const MemberPointerType * FromMemPointer1 = |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2914 | FromType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2915 | const MemberPointerType * ToMemPointer1 = |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2916 | ToType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2917 | const MemberPointerType * FromMemPointer2 = |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2918 | FromType2->getAs<MemberPointerType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2919 | const MemberPointerType * ToMemPointer2 = |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2920 | ToType2->getAs<MemberPointerType>(); |
| 2921 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 2922 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 2923 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 2924 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 2925 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 2926 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 2927 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 2928 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2929 | // 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] | 2930 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2931 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2932 | return ImplicitConversionSequence::Worse; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2933 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2934 | return ImplicitConversionSequence::Better; |
| 2935 | } |
| 2936 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 2937 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2938 | if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2939 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2940 | else if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2941 | return ImplicitConversionSequence::Worse; |
| 2942 | } |
| 2943 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2944 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2945 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2946 | // -- 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] | 2947 | // -- binding of an expression of type C to a reference of type |
| 2948 | // B& is better than binding an expression of type C to a |
| 2949 | // reference of type A&, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2950 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2951 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 2952 | if (S.IsDerivedFrom(ToType1, ToType2)) |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2953 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2954 | else if (S.IsDerivedFrom(ToType2, ToType1)) |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2955 | return ImplicitConversionSequence::Worse; |
| 2956 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2957 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2958 | // -- 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] | 2959 | // -- binding of an expression of type B to a reference of type |
| 2960 | // A& is better than binding an expression of type C to a |
| 2961 | // reference of type A&, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2962 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2963 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 2964 | if (S.IsDerivedFrom(FromType2, FromType1)) |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2965 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2966 | else if (S.IsDerivedFrom(FromType1, FromType2)) |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2967 | return ImplicitConversionSequence::Worse; |
| 2968 | } |
| 2969 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2970 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2971 | return ImplicitConversionSequence::Indistinguishable; |
| 2972 | } |
| 2973 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2974 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 2975 | /// determine whether they are reference-related, |
| 2976 | /// reference-compatible, reference-compatible with added |
| 2977 | /// qualification, or incompatible, for use in C++ initialization by |
| 2978 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 2979 | /// type, and the first type (T1) is the pointee type of the reference |
| 2980 | /// type being initialized. |
| 2981 | Sema::ReferenceCompareResult |
| 2982 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 2983 | QualType OrigT1, QualType OrigT2, |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2984 | bool &DerivedToBase, |
| 2985 | bool &ObjCConversion) { |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2986 | assert(!OrigT1->isReferenceType() && |
| 2987 | "T1 must be the pointee type of the reference type"); |
| 2988 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 2989 | |
| 2990 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 2991 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 2992 | Qualifiers T1Quals, T2Quals; |
| 2993 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2994 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 2995 | |
| 2996 | // C++ [dcl.init.ref]p4: |
| 2997 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 2998 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 2999 | // T1 is a base class of T2. |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3000 | DerivedToBase = false; |
| 3001 | ObjCConversion = false; |
| 3002 | if (UnqualT1 == UnqualT2) { |
| 3003 | // Nothing to do. |
| 3004 | } else if (!RequireCompleteType(Loc, OrigT2, PDiag()) && |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3005 | IsDerivedFrom(UnqualT2, UnqualT1)) |
| 3006 | DerivedToBase = true; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3007 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
| 3008 | UnqualT2->isObjCObjectOrInterfaceType() && |
| 3009 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
| 3010 | ObjCConversion = true; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3011 | else |
| 3012 | return Ref_Incompatible; |
| 3013 | |
| 3014 | // At this point, we know that T1 and T2 are reference-related (at |
| 3015 | // least). |
| 3016 | |
| 3017 | // If the type is an array type, promote the element qualifiers to the type |
| 3018 | // for comparison. |
| 3019 | if (isa<ArrayType>(T1) && T1Quals) |
| 3020 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 3021 | if (isa<ArrayType>(T2) && T2Quals) |
| 3022 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 3023 | |
| 3024 | // C++ [dcl.init.ref]p4: |
| 3025 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 3026 | // reference-related to T2 and cv1 is the same cv-qualification |
| 3027 | // as, or greater cv-qualification than, cv2. For purposes of |
| 3028 | // overload resolution, cases for which cv1 is greater |
| 3029 | // cv-qualification than cv2 are identified as |
| 3030 | // reference-compatible with added qualification (see 13.3.3.2). |
| 3031 | if (T1Quals.getCVRQualifiers() == T2Quals.getCVRQualifiers()) |
| 3032 | return Ref_Compatible; |
| 3033 | else if (T1.isMoreQualifiedThan(T2)) |
| 3034 | return Ref_Compatible_With_Added_Qualification; |
| 3035 | else |
| 3036 | return Ref_Related; |
| 3037 | } |
| 3038 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3039 | /// \brief Look for a user-defined conversion to an value reference-compatible |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3040 | /// with DeclType. Return true if something definite is found. |
| 3041 | static bool |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3042 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
| 3043 | QualType DeclType, SourceLocation DeclLoc, |
| 3044 | Expr *Init, QualType T2, bool AllowRvalues, |
| 3045 | bool AllowExplicit) { |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3046 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 3047 | CXXRecordDecl *T2RecordDecl |
| 3048 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 3049 | |
| 3050 | OverloadCandidateSet CandidateSet(DeclLoc); |
| 3051 | const UnresolvedSetImpl *Conversions |
| 3052 | = T2RecordDecl->getVisibleConversionFunctions(); |
| 3053 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 3054 | E = Conversions->end(); I != E; ++I) { |
| 3055 | NamedDecl *D = *I; |
| 3056 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3057 | if (isa<UsingShadowDecl>(D)) |
| 3058 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3059 | |
| 3060 | FunctionTemplateDecl *ConvTemplate |
| 3061 | = dyn_cast<FunctionTemplateDecl>(D); |
| 3062 | CXXConversionDecl *Conv; |
| 3063 | if (ConvTemplate) |
| 3064 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3065 | else |
| 3066 | Conv = cast<CXXConversionDecl>(D); |
| 3067 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3068 | // 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] | 3069 | // explicit conversions, skip it. |
| 3070 | if (!AllowExplicit && Conv->isExplicit()) |
| 3071 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3072 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3073 | if (AllowRvalues) { |
| 3074 | bool DerivedToBase = false; |
| 3075 | bool ObjCConversion = false; |
| 3076 | if (!ConvTemplate && |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3077 | S.CompareReferenceRelationship( |
| 3078 | DeclLoc, |
| 3079 | Conv->getConversionType().getNonReferenceType() |
| 3080 | .getUnqualifiedType(), |
| 3081 | DeclType.getNonReferenceType().getUnqualifiedType(), |
| 3082 | DerivedToBase, ObjCConversion) == |
| 3083 | Sema::Ref_Incompatible) |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3084 | continue; |
| 3085 | } else { |
| 3086 | // If the conversion function doesn't return a reference type, |
| 3087 | // it can't be considered for this conversion. An rvalue reference |
| 3088 | // is only acceptable if its referencee is a function type. |
| 3089 | |
| 3090 | const ReferenceType *RefType = |
| 3091 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 3092 | if (!RefType || |
| 3093 | (!RefType->isLValueReferenceType() && |
| 3094 | !RefType->getPointeeType()->isFunctionType())) |
| 3095 | continue; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3096 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3097 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3098 | if (ConvTemplate) |
| 3099 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3100 | Init, DeclType, CandidateSet); |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3101 | else |
| 3102 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3103 | DeclType, CandidateSet); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
| 3106 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3107 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3108 | case OR_Success: |
| 3109 | // C++ [over.ics.ref]p1: |
| 3110 | // |
| 3111 | // [...] If the parameter binds directly to the result of |
| 3112 | // applying a conversion function to the argument |
| 3113 | // expression, the implicit conversion sequence is a |
| 3114 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 3115 | // second standard conversion sequence either an identity |
| 3116 | // conversion or, if the conversion function returns an |
| 3117 | // entity of a type that is a derived class of the parameter |
| 3118 | // type, a derived-to-base Conversion. |
| 3119 | if (!Best->FinalConversion.DirectBinding) |
| 3120 | return false; |
| 3121 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3122 | if (Best->Function) |
| 3123 | S.MarkDeclarationReferenced(DeclLoc, Best->Function); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3124 | ICS.setUserDefined(); |
| 3125 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 3126 | ICS.UserDefined.After = Best->FinalConversion; |
| 3127 | ICS.UserDefined.ConversionFunction = Best->Function; |
Douglas Gregor | 2bbfba0 | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 3128 | ICS.UserDefined.FoundConversionFunction = Best->FoundDecl.getDecl(); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3129 | ICS.UserDefined.EllipsisConversion = false; |
| 3130 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 3131 | ICS.UserDefined.After.DirectBinding && |
| 3132 | "Expected a direct reference binding!"); |
| 3133 | return true; |
| 3134 | |
| 3135 | case OR_Ambiguous: |
| 3136 | ICS.setAmbiguous(); |
| 3137 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3138 | Cand != CandidateSet.end(); ++Cand) |
| 3139 | if (Cand->Viable) |
| 3140 | ICS.Ambiguous.addConversion(Cand->Function); |
| 3141 | return true; |
| 3142 | |
| 3143 | case OR_No_Viable_Function: |
| 3144 | case OR_Deleted: |
| 3145 | // There was no suitable conversion, or we found a deleted |
| 3146 | // conversion; continue with other checks. |
| 3147 | return false; |
| 3148 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3149 | |
Eric Christopher | aba9fb2 | 2010-06-30 18:36:32 +0000 | [diff] [blame] | 3150 | return false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3153 | /// \brief Compute an implicit conversion sequence for reference |
| 3154 | /// initialization. |
| 3155 | static ImplicitConversionSequence |
| 3156 | TryReferenceInit(Sema &S, Expr *&Init, QualType DeclType, |
| 3157 | SourceLocation DeclLoc, |
| 3158 | bool SuppressUserConversions, |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 3159 | bool AllowExplicit) { |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3160 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 3161 | |
| 3162 | // Most paths end in a failed conversion. |
| 3163 | ImplicitConversionSequence ICS; |
| 3164 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 3165 | |
| 3166 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 3167 | QualType T2 = Init->getType(); |
| 3168 | |
| 3169 | // If the initializer is the address of an overloaded function, try |
| 3170 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3171 | // type of the resulting function. |
| 3172 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 3173 | DeclAccessPair Found; |
| 3174 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 3175 | false, Found)) |
| 3176 | T2 = Fn->getType(); |
| 3177 | } |
| 3178 | |
| 3179 | // Compute some basic properties of the types and the initializer. |
| 3180 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 3181 | bool DerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3182 | bool ObjCConversion = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3183 | Expr::Classification InitCategory = Init->Classify(S.Context); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3184 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3185 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase, |
| 3186 | ObjCConversion); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3187 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3188 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3189 | // C++0x [dcl.init.ref]p5: |
Douglas Gregor | 870f374 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 3190 | // A reference to type "cv1 T1" is initialized by an expression |
| 3191 | // of type "cv2 T2" as follows: |
| 3192 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3193 | // -- If reference is an lvalue reference and the initializer expression |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3194 | if (!isRValRef) { |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3195 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 3196 | // reference-compatible with "cv2 T2," or |
| 3197 | // |
| 3198 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 3199 | if (InitCategory.isLValue() && |
| 3200 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3201 | // C++ [over.ics.ref]p1: |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3202 | // When a parameter of reference type binds directly (8.5.3) |
| 3203 | // to an argument expression, the implicit conversion sequence |
| 3204 | // is the identity conversion, unless the argument expression |
| 3205 | // has a type that is a derived class of the parameter type, |
| 3206 | // in which case the implicit conversion sequence is a |
| 3207 | // derived-to-base Conversion (13.3.3.1). |
| 3208 | ICS.setStandard(); |
| 3209 | ICS.Standard.First = ICK_Identity; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3210 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 3211 | : ObjCConversion? ICK_Compatible_Conversion |
| 3212 | : ICK_Identity; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3213 | ICS.Standard.Third = ICK_Identity; |
| 3214 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 3215 | ICS.Standard.setToType(0, T2); |
| 3216 | ICS.Standard.setToType(1, T1); |
| 3217 | ICS.Standard.setToType(2, T1); |
| 3218 | ICS.Standard.ReferenceBinding = true; |
| 3219 | ICS.Standard.DirectBinding = true; |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3220 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3221 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 3222 | ICS.Standard.BindsToRvalue = false; |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3223 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3224 | ICS.Standard.CopyConstructor = 0; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3225 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3226 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 3227 | // derived-to-base conversions is suppressed when we're |
| 3228 | // computing the implicit conversion sequence (C++ |
| 3229 | // [over.best.ics]p2). |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3230 | return ICS; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3231 | } |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3232 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3233 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 3234 | // not reference-related to T2, and can be implicitly |
| 3235 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 3236 | // is reference-compatible with "cv3 T3" 92) (this |
| 3237 | // conversion is selected by enumerating the applicable |
| 3238 | // conversion functions (13.3.1.6) and choosing the best |
| 3239 | // one through overload resolution (13.3)), |
| 3240 | if (!SuppressUserConversions && T2->isRecordType() && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3241 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3242 | RefRelationship == Sema::Ref_Incompatible) { |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3243 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 3244 | Init, T2, /*AllowRvalues=*/false, |
| 3245 | AllowExplicit)) |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3246 | return ICS; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3247 | } |
| 3248 | } |
| 3249 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3250 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 3251 | // 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] | 3252 | // shall be an rvalue reference. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3253 | // |
Douglas Gregor | 870f374 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 3254 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 3255 | // point, which is that, due to p2 (which short-circuits reference |
| 3256 | // binding by only attempting a simple conversion for non-direct |
| 3257 | // bindings) and p3's strange wording, we allow a const volatile |
| 3258 | // reference to bind to an rvalue. Hence the check for the presence |
| 3259 | // of "const" rather than checking for "const" being the only |
| 3260 | // qualifier. |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3261 | // This is also the point where rvalue references and lvalue inits no longer |
| 3262 | // go together. |
Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 3263 | if (!isRValRef && !T1.isConstQualified()) |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3264 | return ICS; |
| 3265 | |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3266 | // -- If the initializer expression |
| 3267 | // |
| 3268 | // -- is an xvalue, class prvalue, array prvalue or function |
| 3269 | // lvalue and "cv1T1" is reference-compatible with "cv2 T2", or |
| 3270 | if (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification && |
| 3271 | (InitCategory.isXValue() || |
| 3272 | (InitCategory.isPRValue() && (T2->isRecordType() || T2->isArrayType())) || |
| 3273 | (InitCategory.isLValue() && T2->isFunctionType()))) { |
| 3274 | ICS.setStandard(); |
| 3275 | ICS.Standard.First = ICK_Identity; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3276 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3277 | : ObjCConversion? ICK_Compatible_Conversion |
| 3278 | : ICK_Identity; |
| 3279 | ICS.Standard.Third = ICK_Identity; |
| 3280 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 3281 | ICS.Standard.setToType(0, T2); |
| 3282 | ICS.Standard.setToType(1, T1); |
| 3283 | ICS.Standard.setToType(2, T1); |
| 3284 | ICS.Standard.ReferenceBinding = true; |
| 3285 | // In C++0x, this is always a direct binding. In C++98/03, it's a direct |
| 3286 | // binding unless we're binding to a class prvalue. |
| 3287 | // Note: Although xvalues wouldn't normally show up in C++98/03 code, we |
| 3288 | // allow the use of rvalue references in C++98/03 for the benefit of |
| 3289 | // standard library implementors; therefore, we need the xvalue check here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3290 | ICS.Standard.DirectBinding = |
| 3291 | S.getLangOptions().CPlusPlus0x || |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3292 | (InitCategory.isPRValue() && !T2->isRecordType()); |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3293 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3294 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3295 | ICS.Standard.BindsToRvalue = InitCategory.isRValue(); |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3296 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3297 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3298 | return ICS; |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3299 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3300 | |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3301 | // -- has a class type (i.e., T2 is a class type), where T1 is not |
| 3302 | // reference-related to T2, and can be implicitly converted to |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3303 | // an xvalue, class prvalue, or function lvalue of type |
| 3304 | // "cv3 T3", where "cv1 T1" is reference-compatible with |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3305 | // "cv3 T3", |
| 3306 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3307 | // then the reference is bound to the value of the initializer |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3308 | // expression in the first case and to the result of the conversion |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3309 | // in the second case (or, in either case, to an appropriate base |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3310 | // class subobject). |
| 3311 | if (!SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3312 | T2->isRecordType() && !S.RequireCompleteType(DeclLoc, T2, 0) && |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3313 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 3314 | Init, T2, /*AllowRvalues=*/true, |
| 3315 | AllowExplicit)) { |
| 3316 | // In the second case, if the reference is an rvalue reference |
| 3317 | // and the second standard conversion sequence of the |
| 3318 | // user-defined conversion sequence includes an lvalue-to-rvalue |
| 3319 | // conversion, the program is ill-formed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3320 | if (ICS.isUserDefined() && isRValRef && |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3321 | ICS.UserDefined.After.First == ICK_Lvalue_To_Rvalue) |
| 3322 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 3323 | |
Douglas Gregor | 95273c3 | 2011-01-21 16:36:05 +0000 | [diff] [blame] | 3324 | return ICS; |
Rafael Espindola | be468d9 | 2011-01-22 15:32:35 +0000 | [diff] [blame] | 3325 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3326 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3327 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 3328 | // initialized from the initializer expression using the |
| 3329 | // rules for a non-reference copy initialization (8.5). The |
| 3330 | // reference is then bound to the temporary. If T1 is |
| 3331 | // reference-related to T2, cv1 must be the same |
| 3332 | // cv-qualification as, or greater cv-qualification than, |
| 3333 | // cv2; otherwise, the program is ill-formed. |
| 3334 | if (RefRelationship == Sema::Ref_Related) { |
| 3335 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 3336 | // we would be reference-compatible or reference-compatible with |
| 3337 | // added qualification. But that wasn't the case, so the reference |
| 3338 | // initialization fails. |
| 3339 | return ICS; |
| 3340 | } |
| 3341 | |
| 3342 | // If at least one of the types is a class type, the types are not |
| 3343 | // related, and we aren't allowed any user conversions, the |
| 3344 | // reference binding fails. This case is important for breaking |
| 3345 | // recursion, since TryImplicitConversion below will attempt to |
| 3346 | // create a temporary through the use of a copy constructor. |
| 3347 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 3348 | (T1->isRecordType() || T2->isRecordType())) |
| 3349 | return ICS; |
| 3350 | |
Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 3351 | // If T1 is reference-related to T2 and the reference is an rvalue |
| 3352 | // reference, the initializer expression shall not be an lvalue. |
| 3353 | if (RefRelationship >= Sema::Ref_Related && |
| 3354 | isRValRef && Init->Classify(S.Context).isLValue()) |
| 3355 | return ICS; |
| 3356 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3357 | // C++ [over.ics.ref]p2: |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3358 | // When a parameter of reference type is not bound directly to |
| 3359 | // an argument expression, the conversion sequence is the one |
| 3360 | // required to convert the argument expression to the |
| 3361 | // underlying type of the reference according to |
| 3362 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 3363 | // to copy-initializing a temporary of the underlying type with |
| 3364 | // the argument expression. Any difference in top-level |
| 3365 | // cv-qualification is subsumed by the initialization itself |
| 3366 | // and does not constitute a conversion. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3367 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
| 3368 | /*AllowExplicit=*/false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3369 | /*InOverloadResolution=*/false, |
| 3370 | /*CStyle=*/false); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3371 | |
| 3372 | // Of course, that's still a reference binding. |
| 3373 | if (ICS.isStandard()) { |
| 3374 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3375 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3376 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 3377 | ICS.Standard.BindsToRvalue = true; |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3378 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3379 | } else if (ICS.isUserDefined()) { |
| 3380 | ICS.UserDefined.After.ReferenceBinding = true; |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3381 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3382 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 3383 | ICS.Standard.BindsToRvalue = true; |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3384 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3385 | } |
Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 3386 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3387 | return ICS; |
| 3388 | } |
| 3389 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3390 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 3391 | /// ToType from the expression From. Return the implicit conversion |
| 3392 | /// sequence required to pass this argument, which may be a bad |
| 3393 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3394 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | e81335c | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 3395 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3396 | static ImplicitConversionSequence |
| 3397 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3398 | bool SuppressUserConversions, |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3399 | bool InOverloadResolution) { |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3400 | if (ToType->isReferenceType()) |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3401 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3402 | /*FIXME:*/From->getLocStart(), |
| 3403 | SuppressUserConversions, |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 3404 | /*AllowExplicit=*/false); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3405 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3406 | return TryImplicitConversion(S, From, ToType, |
| 3407 | SuppressUserConversions, |
| 3408 | /*AllowExplicit=*/false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3409 | InOverloadResolution, |
| 3410 | /*CStyle=*/false); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3411 | } |
| 3412 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3413 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 3414 | /// parameter of the given member function (@c Method) from the |
| 3415 | /// expression @p From. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3416 | static ImplicitConversionSequence |
| 3417 | TryObjectArgumentInitialization(Sema &S, QualType OrigFromType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3418 | Expr::Classification FromClassification, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3419 | CXXMethodDecl *Method, |
| 3420 | CXXRecordDecl *ActingContext) { |
| 3421 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 931e0bd | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 3422 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 3423 | // const volatile object. |
| 3424 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 3425 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3426 | QualType ImplicitParamType = S.Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3427 | |
| 3428 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 3429 | // to exit early. |
| 3430 | ImplicitConversionSequence ICS; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3431 | |
| 3432 | // We need to have an object of class type. |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 3433 | QualType FromType = OrigFromType; |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3434 | if (const PointerType *PT = FromType->getAs<PointerType>()) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3435 | FromType = PT->getPointeeType(); |
| 3436 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3437 | // When we had a pointer, it's implicitly dereferenced, so we |
| 3438 | // better have an lvalue. |
| 3439 | assert(FromClassification.isLValue()); |
| 3440 | } |
| 3441 | |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3442 | assert(FromType->isRecordType()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3443 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3444 | // C++0x [over.match.funcs]p4: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3445 | // For non-static member functions, the type of the implicit object |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3446 | // parameter is |
| 3447 | // |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3448 | // - "lvalue reference to cv X" for functions declared without a |
| 3449 | // ref-qualifier or with the & ref-qualifier |
| 3450 | // - "rvalue reference to cv X" for functions declared with the && |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3451 | // ref-qualifier |
| 3452 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3453 | // 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] | 3454 | // cv-qualification on the member function declaration. |
| 3455 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3456 | // However, when finding an implicit conversion sequence for the argument, we |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3457 | // are not allowed to create temporaries or perform user-defined conversions |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3458 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 3459 | // reference binding here, that allows class rvalues to bind to |
| 3460 | // non-constant references. |
| 3461 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3462 | // First check the qualifiers. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3463 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3464 | if (ImplicitParamType.getCVRQualifiers() |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3465 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3466 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3467 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
| 3468 | OrigFromType, ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3469 | return ICS; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3470 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3471 | |
| 3472 | // Check that we have either the same type or a derived type. It |
| 3473 | // affects the conversion rank. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3474 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3475 | ImplicitConversionKind SecondKind; |
| 3476 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 3477 | SecondKind = ICK_Identity; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3478 | } else if (S.IsDerivedFrom(FromType, ClassType)) |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3479 | SecondKind = ICK_Derived_To_Base; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3480 | else { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3481 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 3482 | FromType, ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3483 | return ICS; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3484 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3485 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3486 | // Check the ref-qualifier. |
| 3487 | switch (Method->getRefQualifier()) { |
| 3488 | case RQ_None: |
| 3489 | // Do nothing; we don't care about lvalueness or rvalueness. |
| 3490 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3491 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3492 | case RQ_LValue: |
| 3493 | if (!FromClassification.isLValue() && Quals != Qualifiers::Const) { |
| 3494 | // non-const lvalue reference cannot bind to an rvalue |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3495 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, FromType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3496 | ImplicitParamType); |
| 3497 | return ICS; |
| 3498 | } |
| 3499 | break; |
| 3500 | |
| 3501 | case RQ_RValue: |
| 3502 | if (!FromClassification.isRValue()) { |
| 3503 | // rvalue reference cannot bind to an lvalue |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3504 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, FromType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3505 | ImplicitParamType); |
| 3506 | return ICS; |
| 3507 | } |
| 3508 | break; |
| 3509 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3510 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3511 | // Success. Mark this as a reference binding. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3512 | ICS.setStandard(); |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3513 | ICS.Standard.setAsIdentityConversion(); |
| 3514 | ICS.Standard.Second = SecondKind; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3515 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3516 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3517 | ICS.Standard.ReferenceBinding = true; |
| 3518 | ICS.Standard.DirectBinding = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3519 | ICS.Standard.IsLvalueReference = Method->getRefQualifier() != RQ_RValue; |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3520 | ICS.Standard.BindsToFunctionLvalue = false; |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3521 | ICS.Standard.BindsToRvalue = FromClassification.isRValue(); |
| 3522 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier |
| 3523 | = (Method->getRefQualifier() == RQ_None); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3524 | return ICS; |
| 3525 | } |
| 3526 | |
| 3527 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 3528 | /// the implicit object parameter for the given Method with the given |
| 3529 | /// expression. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3530 | ExprResult |
| 3531 | Sema::PerformObjectArgumentInitialization(Expr *From, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3532 | NestedNameSpecifier *Qualifier, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3533 | NamedDecl *FoundDecl, |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3534 | CXXMethodDecl *Method) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3535 | QualType FromRecordType, DestType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3536 | QualType ImplicitParamRecordType = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3537 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3538 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3539 | Expr::Classification FromClassification; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3540 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3541 | FromRecordType = PT->getPointeeType(); |
| 3542 | DestType = Method->getThisType(Context); |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3543 | FromClassification = Expr::Classification::makeSimpleLValue(); |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3544 | } else { |
| 3545 | FromRecordType = From->getType(); |
| 3546 | DestType = ImplicitParamRecordType; |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3547 | FromClassification = From->Classify(Context); |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3548 | } |
| 3549 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3550 | // Note that we always use the true parent context when performing |
| 3551 | // the actual argument initialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3552 | ImplicitConversionSequence ICS |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3553 | = TryObjectArgumentInitialization(*this, From->getType(), FromClassification, |
| 3554 | Method, Method->getParent()); |
Argyrios Kyrtzidis | 9813d32 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 3555 | if (ICS.isBad()) { |
| 3556 | if (ICS.Bad.Kind == BadConversionSequence::bad_qualifiers) { |
| 3557 | Qualifiers FromQs = FromRecordType.getQualifiers(); |
| 3558 | Qualifiers ToQs = DestType.getQualifiers(); |
| 3559 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 3560 | if (CVR) { |
| 3561 | Diag(From->getSourceRange().getBegin(), |
| 3562 | diag::err_member_function_call_bad_cvr) |
| 3563 | << Method->getDeclName() << FromRecordType << (CVR - 1) |
| 3564 | << From->getSourceRange(); |
| 3565 | Diag(Method->getLocation(), diag::note_previous_decl) |
| 3566 | << Method->getDeclName(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3567 | return ExprError(); |
Argyrios Kyrtzidis | 9813d32 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 3568 | } |
| 3569 | } |
| 3570 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3571 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3572 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3573 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Argyrios Kyrtzidis | 9813d32 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 3574 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3575 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3576 | if (ICS.Standard.Second == ICK_Derived_To_Base) { |
| 3577 | ExprResult FromRes = |
| 3578 | PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
| 3579 | if (FromRes.isInvalid()) |
| 3580 | return ExprError(); |
| 3581 | From = FromRes.take(); |
| 3582 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3583 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3584 | if (!Context.hasSameType(From->getType(), DestType)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3585 | From = ImpCastExprToType(From, DestType, CK_NoOp, |
| 3586 | From->getType()->isPointerType() ? VK_RValue : VK_LValue).take(); |
| 3587 | return Owned(From); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3588 | } |
| 3589 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3590 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 3591 | /// expression From to bool (C++0x [conv]p3). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3592 | static ImplicitConversionSequence |
| 3593 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
Douglas Gregor | 0bbe94d | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 3594 | // FIXME: This is pretty broken. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3595 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
Anders Carlsson | ef4c721 | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 3596 | // FIXME: Are these flags correct? |
| 3597 | /*SuppressUserConversions=*/false, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3598 | /*AllowExplicit=*/true, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3599 | /*InOverloadResolution=*/false, |
| 3600 | /*CStyle=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
| 3603 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 3604 | /// of the expression From to bool (C++0x [conv]p3). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3605 | ExprResult Sema::PerformContextuallyConvertToBool(Expr *From) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3606 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3607 | if (!ICS.isBad()) |
| 3608 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3609 | |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3610 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3611 | return Diag(From->getSourceRange().getBegin(), |
| 3612 | diag::err_typecheck_bool_condition) |
| 3613 | << From->getType() << From->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3614 | return ExprError(); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3615 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3616 | |
Fariborz Jahanian | cac49a8 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3617 | /// TryContextuallyConvertToObjCId - Attempt to contextually convert the |
| 3618 | /// expression From to 'id'. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3619 | static ImplicitConversionSequence |
| 3620 | TryContextuallyConvertToObjCId(Sema &S, Expr *From) { |
| 3621 | QualType Ty = S.Context.getObjCIdType(); |
| 3622 | return TryImplicitConversion(S, From, Ty, |
| 3623 | // FIXME: Are these flags correct? |
| 3624 | /*SuppressUserConversions=*/false, |
| 3625 | /*AllowExplicit=*/true, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3626 | /*InOverloadResolution=*/false, |
| 3627 | /*CStyle=*/false); |
Fariborz Jahanian | cac49a8 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3628 | } |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3629 | |
Fariborz Jahanian | cac49a8 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3630 | /// PerformContextuallyConvertToObjCId - Perform a contextual conversion |
| 3631 | /// of the expression From to 'id'. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3632 | ExprResult Sema::PerformContextuallyConvertToObjCId(Expr *From) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3633 | QualType Ty = Context.getObjCIdType(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3634 | ImplicitConversionSequence ICS = TryContextuallyConvertToObjCId(*this, From); |
Fariborz Jahanian | cac49a8 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3635 | if (!ICS.isBad()) |
| 3636 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3637 | return ExprError(); |
Fariborz Jahanian | cac49a8 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3638 | } |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3639 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3640 | /// \brief Attempt to convert the given expression to an integral or |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3641 | /// enumeration type. |
| 3642 | /// |
| 3643 | /// This routine will attempt to convert an expression of class type to an |
| 3644 | /// integral or enumeration type, if that class type only has a single |
| 3645 | /// conversion to an integral or enumeration type. |
| 3646 | /// |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3647 | /// \param Loc The source location of the construct that requires the |
| 3648 | /// conversion. |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3649 | /// |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3650 | /// \param FromE The expression we're converting from. |
| 3651 | /// |
| 3652 | /// \param NotIntDiag The diagnostic to be emitted if the expression does not |
| 3653 | /// have integral or enumeration type. |
| 3654 | /// |
| 3655 | /// \param IncompleteDiag The diagnostic to be emitted if the expression has |
| 3656 | /// incomplete class type. |
| 3657 | /// |
| 3658 | /// \param ExplicitConvDiag The diagnostic to be emitted if we're calling an |
| 3659 | /// explicit conversion function (because no implicit conversion functions |
| 3660 | /// were available). This is a recovery mode. |
| 3661 | /// |
| 3662 | /// \param ExplicitConvNote The note to be emitted with \p ExplicitConvDiag, |
| 3663 | /// showing which conversion was picked. |
| 3664 | /// |
| 3665 | /// \param AmbigDiag The diagnostic to be emitted if there is more than one |
| 3666 | /// conversion function that could convert to integral or enumeration type. |
| 3667 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3668 | /// \param AmbigNote The note to be emitted with \p AmbigDiag for each |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3669 | /// usable conversion function. |
| 3670 | /// |
| 3671 | /// \param ConvDiag The diagnostic to be emitted if we are calling a conversion |
| 3672 | /// function, which may be an extension in this case. |
| 3673 | /// |
| 3674 | /// \returns The expression, converted to an integral or enumeration type if |
| 3675 | /// successful. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3676 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3677 | Sema::ConvertToIntegralOrEnumerationType(SourceLocation Loc, Expr *From, |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3678 | const PartialDiagnostic &NotIntDiag, |
| 3679 | const PartialDiagnostic &IncompleteDiag, |
| 3680 | const PartialDiagnostic &ExplicitConvDiag, |
| 3681 | const PartialDiagnostic &ExplicitConvNote, |
| 3682 | const PartialDiagnostic &AmbigDiag, |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3683 | const PartialDiagnostic &AmbigNote, |
| 3684 | const PartialDiagnostic &ConvDiag) { |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3685 | // We can't perform any more checking for type-dependent expressions. |
| 3686 | if (From->isTypeDependent()) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3687 | return Owned(From); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3688 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3689 | // If the expression already has integral or enumeration type, we're golden. |
| 3690 | QualType T = From->getType(); |
| 3691 | if (T->isIntegralOrEnumerationType()) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3692 | return Owned(From); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3693 | |
| 3694 | // FIXME: Check for missing '()' if T is a function type? |
| 3695 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3696 | // 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] | 3697 | // expression of integral or enumeration type. |
| 3698 | const RecordType *RecordTy = T->getAs<RecordType>(); |
| 3699 | if (!RecordTy || !getLangOptions().CPlusPlus) { |
| 3700 | Diag(Loc, NotIntDiag) |
| 3701 | << T << From->getSourceRange(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3702 | return Owned(From); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3703 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3704 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3705 | // We must have a complete class type. |
| 3706 | if (RequireCompleteType(Loc, T, IncompleteDiag)) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3707 | return Owned(From); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3708 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3709 | // Look for a conversion to an integral or enumeration type. |
| 3710 | UnresolvedSet<4> ViableConversions; |
| 3711 | UnresolvedSet<4> ExplicitConversions; |
| 3712 | const UnresolvedSetImpl *Conversions |
| 3713 | = cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3714 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3715 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3716 | E = Conversions->end(); |
| 3717 | I != E; |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3718 | ++I) { |
| 3719 | if (CXXConversionDecl *Conversion |
| 3720 | = dyn_cast<CXXConversionDecl>((*I)->getUnderlyingDecl())) |
| 3721 | if (Conversion->getConversionType().getNonReferenceType() |
| 3722 | ->isIntegralOrEnumerationType()) { |
| 3723 | if (Conversion->isExplicit()) |
| 3724 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
| 3725 | else |
| 3726 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
| 3727 | } |
| 3728 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3729 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3730 | switch (ViableConversions.size()) { |
| 3731 | case 0: |
| 3732 | if (ExplicitConversions.size() == 1) { |
| 3733 | DeclAccessPair Found = ExplicitConversions[0]; |
| 3734 | CXXConversionDecl *Conversion |
| 3735 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3736 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3737 | // The user probably meant to invoke the given explicit |
| 3738 | // conversion; use it. |
| 3739 | QualType ConvTy |
| 3740 | = Conversion->getConversionType().getNonReferenceType(); |
| 3741 | std::string TypeStr; |
| 3742 | ConvTy.getAsStringInternal(TypeStr, Context.PrintingPolicy); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3743 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3744 | Diag(Loc, ExplicitConvDiag) |
| 3745 | << T << ConvTy |
| 3746 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 3747 | "static_cast<" + TypeStr + ">(") |
| 3748 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(From->getLocEnd()), |
| 3749 | ")"); |
| 3750 | Diag(Conversion->getLocation(), ExplicitConvNote) |
| 3751 | << ConvTy->isEnumeralType() << ConvTy; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3752 | |
| 3753 | // 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] | 3754 | // explicit conversion function. |
| 3755 | if (isSFINAEContext()) |
| 3756 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3757 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3758 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3759 | ExprResult Result = BuildCXXMemberCallExpr(From, Found, Conversion); |
| 3760 | if (Result.isInvalid()) |
| 3761 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3762 | |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3763 | From = Result.get(); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3764 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3765 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3766 | // We'll complain below about a non-integral condition type. |
| 3767 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3768 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3769 | case 1: { |
| 3770 | // Apply this conversion. |
| 3771 | DeclAccessPair Found = ViableConversions[0]; |
| 3772 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3773 | |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3774 | CXXConversionDecl *Conversion |
| 3775 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 3776 | QualType ConvTy |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3777 | = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3778 | if (ConvDiag.getDiagID()) { |
| 3779 | if (isSFINAEContext()) |
| 3780 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3781 | |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3782 | Diag(Loc, ConvDiag) |
| 3783 | << T << ConvTy->isEnumeralType() << ConvTy << From->getSourceRange(); |
| 3784 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3785 | |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3786 | ExprResult Result = BuildCXXMemberCallExpr(From, Found, |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3787 | cast<CXXConversionDecl>(Found->getUnderlyingDecl())); |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3788 | if (Result.isInvalid()) |
| 3789 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3790 | |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3791 | From = Result.get(); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3792 | break; |
| 3793 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3794 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3795 | default: |
| 3796 | Diag(Loc, AmbigDiag) |
| 3797 | << T << From->getSourceRange(); |
| 3798 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 3799 | CXXConversionDecl *Conv |
| 3800 | = cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 3801 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 3802 | Diag(Conv->getLocation(), AmbigNote) |
| 3803 | << ConvTy->isEnumeralType() << ConvTy; |
| 3804 | } |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3805 | return Owned(From); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3806 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3807 | |
Douglas Gregor | 5823da3 | 2010-06-29 23:25:20 +0000 | [diff] [blame] | 3808 | if (!From->getType()->isIntegralOrEnumerationType()) |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3809 | Diag(Loc, NotIntDiag) |
| 3810 | << From->getType() << From->getSourceRange(); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3811 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3812 | return Owned(From); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3813 | } |
| 3814 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3815 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3816 | /// candidate functions, using the given function call arguments. If |
| 3817 | /// @p SuppressUserConversions, then don't allow user-defined |
| 3818 | /// conversions via constructors or conversion operators. |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3819 | /// |
| 3820 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 3821 | /// based on an incomplete set of function arguments. This feature is used by |
| 3822 | /// code completion. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3823 | void |
| 3824 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3825 | DeclAccessPair FoundDecl, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3826 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3827 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 3828 | bool SuppressUserConversions, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3829 | bool PartialOverloading) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3830 | const FunctionProtoType* Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3831 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3832 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3833 | assert(!Function->getDescribedFunctionTemplate() && |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3834 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3835 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3836 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3837 | if (!isa<CXXConstructorDecl>(Method)) { |
| 3838 | // If we get here, it's because we're calling a member function |
| 3839 | // that is named without a member access expression (e.g., |
| 3840 | // "this->f") that was either written explicitly or created |
| 3841 | // implicitly. This can happen with a qualified call to a member |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3842 | // function, e.g., X::f(). We use an empty type for the implied |
| 3843 | // object argument (C++ [over.call.func]p3), and the acting context |
| 3844 | // is irrelevant. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3845 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3846 | QualType(), Expr::Classification::makeSimpleLValue(), |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3847 | Args, NumArgs, CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3848 | SuppressUserConversions); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3849 | return; |
| 3850 | } |
| 3851 | // We treat a constructor like a non-member function, since its object |
| 3852 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3853 | } |
| 3854 | |
Douglas Gregor | ff7028a | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 3855 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3856 | return; |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3857 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3858 | // Overload resolution is always an unevaluated context. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3859 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3860 | |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3861 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 3862 | // C++ [class.copy]p3: |
| 3863 | // A member function template is never instantiated to perform the copy |
| 3864 | // of a class object to an object of its class type. |
| 3865 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3866 | if (NumArgs == 1 && |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3867 | Constructor->isSpecializationCopyingObject() && |
Douglas Gregor | 901e717 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 3868 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 3869 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3870 | return; |
| 3871 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3872 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3873 | // Add this candidate |
| 3874 | CandidateSet.push_back(OverloadCandidate()); |
| 3875 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3876 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3877 | Candidate.Function = Function; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3878 | Candidate.Viable = true; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3879 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3880 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 3881 | Candidate.ExplicitCallArguments = NumArgs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3882 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3883 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3884 | |
| 3885 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3886 | // parameters is viable only if it has an ellipsis in its parameter |
| 3887 | // list (8.3.5). |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3888 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
Douglas Gregor | 2a92001 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 3889 | !Proto->isVariadic()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3890 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3891 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3892 | return; |
| 3893 | } |
| 3894 | |
| 3895 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 3896 | // is viable only if the (m+1)st parameter has a default argument |
| 3897 | // (8.3.6). For the purposes of overload resolution, the |
| 3898 | // parameter list is truncated on the right, so that there are |
| 3899 | // exactly m parameters. |
| 3900 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3901 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3902 | // Not enough arguments. |
| 3903 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3904 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3905 | return; |
| 3906 | } |
| 3907 | |
| 3908 | // Determine the implicit conversion sequences for each of the |
| 3909 | // arguments. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3910 | Candidate.Conversions.resize(NumArgs); |
| 3911 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3912 | if (ArgIdx < NumArgsInProto) { |
| 3913 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3914 | // exist for each argument an implicit conversion sequence |
| 3915 | // (13.3.3.1) that converts that argument to the corresponding |
| 3916 | // parameter of F. |
| 3917 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3918 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3919 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3920 | SuppressUserConversions, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3921 | /*InOverloadResolution=*/true); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3922 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 3923 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3924 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3925 | break; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3926 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3927 | } else { |
| 3928 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3929 | // argument for which there is no corresponding parameter is |
| 3930 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3931 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3932 | } |
| 3933 | } |
| 3934 | } |
| 3935 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3936 | /// \brief Add all of the function declarations in the given function set to |
| 3937 | /// the overload canddiate set. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3938 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3939 | Expr **Args, unsigned NumArgs, |
| 3940 | OverloadCandidateSet& CandidateSet, |
| 3941 | bool SuppressUserConversions) { |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3942 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3943 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 3944 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3945 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3946 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3947 | cast<CXXMethodDecl>(FD)->getParent(), |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3948 | Args[0]->getType(), Args[0]->Classify(Context), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3949 | Args + 1, NumArgs - 1, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3950 | CandidateSet, SuppressUserConversions); |
| 3951 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3952 | AddOverloadCandidate(FD, F.getPair(), Args, NumArgs, CandidateSet, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3953 | SuppressUserConversions); |
| 3954 | } else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3955 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3956 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 3957 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3958 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3959 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3960 | /*FIXME: explicit args */ 0, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3961 | Args[0]->getType(), |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3962 | Args[0]->Classify(Context), |
| 3963 | Args + 1, NumArgs - 1, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3964 | CandidateSet, |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3965 | SuppressUserConversions); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3966 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3967 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3968 | /*FIXME: explicit args */ 0, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3969 | Args, NumArgs, CandidateSet, |
| 3970 | SuppressUserConversions); |
| 3971 | } |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3972 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3973 | } |
| 3974 | |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3975 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 3976 | /// method) as a method candidate to the given overload set. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3977 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3978 | QualType ObjectType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3979 | Expr::Classification ObjectClassification, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3980 | Expr **Args, unsigned NumArgs, |
| 3981 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3982 | bool SuppressUserConversions) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3983 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3984 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3985 | |
| 3986 | if (isa<UsingShadowDecl>(Decl)) |
| 3987 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3988 | |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3989 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 3990 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 3991 | "Expected a member function template"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3992 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 3993 | /*ExplicitArgs*/ 0, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3994 | ObjectType, ObjectClassification, Args, NumArgs, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3995 | CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3996 | SuppressUserConversions); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3997 | } else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3998 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3999 | ObjectType, ObjectClassification, Args, NumArgs, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4000 | CandidateSet, SuppressUserConversions); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4001 | } |
| 4002 | } |
| 4003 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4004 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 4005 | /// of candidate functions, using the given function call arguments |
| 4006 | /// and the object argument (@c Object). For example, in a call |
| 4007 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 4008 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 4009 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4010 | /// operators. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4011 | void |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4012 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4013 | CXXRecordDecl *ActingContext, QualType ObjectType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4014 | Expr::Classification ObjectClassification, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4015 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4016 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4017 | bool SuppressUserConversions) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4018 | const FunctionProtoType* Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4019 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4020 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4021 | assert(!isa<CXXConstructorDecl>(Method) && |
| 4022 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4023 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4024 | if (!CandidateSet.isNewCandidate(Method)) |
| 4025 | return; |
| 4026 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4027 | // Overload resolution is always an unevaluated context. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4028 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4029 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4030 | // Add this candidate |
| 4031 | CandidateSet.push_back(OverloadCandidate()); |
| 4032 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4033 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4034 | Candidate.Function = Method; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4035 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4036 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4037 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4038 | |
| 4039 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4040 | |
| 4041 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 4042 | // parameters is viable only if it has an ellipsis in its parameter |
| 4043 | // list (8.3.5). |
| 4044 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 4045 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4046 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4047 | return; |
| 4048 | } |
| 4049 | |
| 4050 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 4051 | // is viable only if the (m+1)st parameter has a default argument |
| 4052 | // (8.3.6). For the purposes of overload resolution, the |
| 4053 | // parameter list is truncated on the right, so that there are |
| 4054 | // exactly m parameters. |
| 4055 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 4056 | if (NumArgs < MinRequiredArgs) { |
| 4057 | // Not enough arguments. |
| 4058 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4059 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4060 | return; |
| 4061 | } |
| 4062 | |
| 4063 | Candidate.Viable = true; |
| 4064 | Candidate.Conversions.resize(NumArgs + 1); |
| 4065 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4066 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4067 | // The implicit object argument is ignored. |
| 4068 | Candidate.IgnoreObjectArgument = true; |
| 4069 | else { |
| 4070 | // Determine the implicit conversion sequence for the object |
| 4071 | // parameter. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4072 | Candidate.Conversions[0] |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4073 | = TryObjectArgumentInitialization(*this, ObjectType, ObjectClassification, |
| 4074 | Method, ActingContext); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4075 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4076 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4077 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4078 | return; |
| 4079 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4080 | } |
| 4081 | |
| 4082 | // Determine the implicit conversion sequences for each of the |
| 4083 | // arguments. |
| 4084 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 4085 | if (ArgIdx < NumArgsInProto) { |
| 4086 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 4087 | // exist for each argument an implicit conversion sequence |
| 4088 | // (13.3.3.1) that converts that argument to the corresponding |
| 4089 | // parameter of F. |
| 4090 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4091 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4092 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4093 | SuppressUserConversions, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 4094 | /*InOverloadResolution=*/true); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4095 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4096 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4097 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4098 | break; |
| 4099 | } |
| 4100 | } else { |
| 4101 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 4102 | // argument for which there is no corresponding parameter is |
| 4103 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4104 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4105 | } |
| 4106 | } |
| 4107 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4108 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4109 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 4110 | /// set, using template argument deduction to produce an appropriate member |
| 4111 | /// function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4112 | void |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4113 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4114 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4115 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4116 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4117 | QualType ObjectType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4118 | Expr::Classification ObjectClassification, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4119 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4120 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4121 | bool SuppressUserConversions) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4122 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 4123 | return; |
| 4124 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4125 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4126 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4127 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4128 | // 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] | 4129 | // candidate functions in the usual way.113) A given name can refer to one |
| 4130 | // or more function templates and also to a set of overloaded non-template |
| 4131 | // functions. In such a case, the candidate functions generated from each |
| 4132 | // function template are combined with the set of non-template candidate |
| 4133 | // functions. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4134 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4135 | FunctionDecl *Specialization = 0; |
| 4136 | if (TemplateDeductionResult Result |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4137 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4138 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4139 | CandidateSet.push_back(OverloadCandidate()); |
| 4140 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 4141 | Candidate.FoundDecl = FoundDecl; |
| 4142 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 4143 | Candidate.Viable = false; |
| 4144 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 4145 | Candidate.IsSurrogate = false; |
| 4146 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4147 | Candidate.ExplicitCallArguments = NumArgs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4148 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4149 | Info); |
| 4150 | return; |
| 4151 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4152 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4153 | // Add the function template specialization produced by template argument |
| 4154 | // deduction as a candidate. |
| 4155 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4156 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4157 | "Specialization is not a member function?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4158 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4159 | ActingContext, ObjectType, ObjectClassification, |
| 4160 | Args, NumArgs, CandidateSet, SuppressUserConversions); |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4163 | /// \brief Add a C++ function template specialization as a candidate |
| 4164 | /// in the candidate set, using template argument deduction to produce |
| 4165 | /// an appropriate function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4166 | void |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4167 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4168 | DeclAccessPair FoundDecl, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4169 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4170 | Expr **Args, unsigned NumArgs, |
| 4171 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4172 | bool SuppressUserConversions) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4173 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 4174 | return; |
| 4175 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4176 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4177 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4178 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4179 | // 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] | 4180 | // candidate functions in the usual way.113) A given name can refer to one |
| 4181 | // or more function templates and also to a set of overloaded non-template |
| 4182 | // functions. In such a case, the candidate functions generated from each |
| 4183 | // function template are combined with the set of non-template candidate |
| 4184 | // functions. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4185 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4186 | FunctionDecl *Specialization = 0; |
| 4187 | if (TemplateDeductionResult Result |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4188 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4189 | Args, NumArgs, Specialization, Info)) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4190 | CandidateSet.push_back(OverloadCandidate()); |
| 4191 | OverloadCandidate &Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4192 | Candidate.FoundDecl = FoundDecl; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4193 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 4194 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4195 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4196 | Candidate.IsSurrogate = false; |
| 4197 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4198 | Candidate.ExplicitCallArguments = NumArgs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4199 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4200 | Info); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4201 | return; |
| 4202 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4203 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4204 | // Add the function template specialization produced by template argument |
| 4205 | // deduction as a candidate. |
| 4206 | assert(Specialization && "Missing function template specialization?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4207 | AddOverloadCandidate(Specialization, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4208 | SuppressUserConversions); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4209 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4210 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4211 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4212 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4213 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4214 | /// 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] | 4215 | /// (which may or may not be the same type as the type that the |
| 4216 | /// conversion function produces). |
| 4217 | void |
| 4218 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4219 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4220 | CXXRecordDecl *ActingContext, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4221 | Expr *From, QualType ToType, |
| 4222 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4223 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 4224 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4225 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4226 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 4227 | return; |
| 4228 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4229 | // Overload resolution is always an unevaluated context. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4230 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4231 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4232 | // Add this candidate |
| 4233 | CandidateSet.push_back(OverloadCandidate()); |
| 4234 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4235 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4236 | Candidate.Function = Conversion; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4237 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4238 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4239 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4240 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 4241 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4242 | Candidate.Viable = true; |
| 4243 | Candidate.Conversions.resize(1); |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4244 | Candidate.ExplicitCallArguments = 1; |
Douglas Gregor | c9ed468 | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4245 | |
Douglas Gregor | 6affc78 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 4246 | // C++ [over.match.funcs]p4: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4247 | // For conversion functions, the function is considered to be a member of |
| 4248 | // the class of the implicit implied object argument for the purpose of |
Douglas Gregor | 6affc78 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 4249 | // defining the type of the implicit object parameter. |
Douglas Gregor | c9ed468 | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4250 | // |
| 4251 | // Determine the implicit conversion sequence for the implicit |
| 4252 | // object parameter. |
| 4253 | QualType ImplicitParamType = From->getType(); |
| 4254 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
| 4255 | ImplicitParamType = FromPtrType->getPointeeType(); |
| 4256 | CXXRecordDecl *ConversionContext |
| 4257 | = cast<CXXRecordDecl>(ImplicitParamType->getAs<RecordType>()->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4258 | |
Douglas Gregor | c9ed468 | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4259 | Candidate.Conversions[0] |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4260 | = TryObjectArgumentInitialization(*this, From->getType(), |
| 4261 | From->Classify(Context), |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4262 | Conversion, ConversionContext); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4263 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4264 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4265 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4266 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4267 | return; |
| 4268 | } |
Douglas Gregor | c9ed468 | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4269 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4270 | // 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] | 4271 | // derived to base as such conversions are given Conversion Rank. They only |
| 4272 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 4273 | QualType FromCanon |
| 4274 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 4275 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 4276 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 4277 | Candidate.Viable = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4278 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 996a6aa | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 4279 | return; |
| 4280 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4281 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4282 | // To determine what the conversion from the result of calling the |
| 4283 | // conversion function to the type we're eventually trying to |
| 4284 | // convert to (ToType), we need to synthesize a call to the |
| 4285 | // conversion function and attempt copy initialization from it. This |
| 4286 | // makes sure that we get the right semantics with respect to |
| 4287 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 4288 | // call on the stack and we don't need its arguments to be |
| 4289 | // well-formed. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4290 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4291 | VK_LValue, From->getLocStart()); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4292 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 4293 | Context.getPointerType(Conversion->getType()), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4294 | CK_FunctionToPointerDecay, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4295 | &ConversionRef, VK_RValue); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4296 | |
Douglas Gregor | 72ebdab | 2010-11-13 19:36:57 +0000 | [diff] [blame] | 4297 | QualType CallResultType |
| 4298 | = Conversion->getConversionType().getNonLValueExprType(Context); |
| 4299 | if (RequireCompleteType(From->getLocStart(), CallResultType, 0)) { |
| 4300 | Candidate.Viable = false; |
| 4301 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 4302 | return; |
| 4303 | } |
| 4304 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4305 | ExprValueKind VK = Expr::getValueKindForType(Conversion->getConversionType()); |
| 4306 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4307 | // 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] | 4308 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 4309 | // allocator). |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4310 | CallExpr Call(Context, &ConversionFn, 0, 0, CallResultType, VK, |
Douglas Gregor | e8f08012 | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 4311 | From->getLocStart()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4312 | ImplicitConversionSequence ICS = |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4313 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 4314 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 4315 | /*InOverloadResolution=*/false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4316 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4317 | switch (ICS.getKind()) { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4318 | case ImplicitConversionSequence::StandardConversion: |
| 4319 | Candidate.FinalConversion = ICS.Standard; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4320 | |
Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 4321 | // C++ [over.ics.user]p3: |
| 4322 | // If the user-defined conversion is specified by a specialization of a |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4323 | // conversion function template, the second standard conversion sequence |
Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 4324 | // shall have exact match rank. |
| 4325 | if (Conversion->getPrimaryTemplate() && |
| 4326 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 4327 | Candidate.Viable = false; |
| 4328 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 4329 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4330 | |
Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4331 | // C++0x [dcl.init.ref]p5: |
| 4332 | // In the second case, if the reference is an rvalue reference and |
| 4333 | // the second standard conversion sequence of the user-defined |
| 4334 | // conversion sequence includes an lvalue-to-rvalue conversion, the |
| 4335 | // program is ill-formed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4336 | if (ToType->isRValueReferenceType() && |
Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4337 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 4338 | Candidate.Viable = false; |
| 4339 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 4340 | } |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4341 | break; |
| 4342 | |
| 4343 | case ImplicitConversionSequence::BadConversion: |
| 4344 | Candidate.Viable = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4345 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4346 | break; |
| 4347 | |
| 4348 | default: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4349 | assert(false && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4350 | "Can only end up with a standard conversion sequence or failure"); |
| 4351 | } |
| 4352 | } |
| 4353 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4354 | /// \brief Adds a conversion function template specialization |
| 4355 | /// candidate to the overload set, using template argument deduction |
| 4356 | /// to deduce the template arguments of the conversion function |
| 4357 | /// template from the type that we are converting to (C++ |
| 4358 | /// [temp.deduct.conv]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4359 | void |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4360 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4361 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4362 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4363 | Expr *From, QualType ToType, |
| 4364 | OverloadCandidateSet &CandidateSet) { |
| 4365 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 4366 | "Only conversion function templates permitted here"); |
| 4367 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4368 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 4369 | return; |
| 4370 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4371 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4372 | CXXConversionDecl *Specialization = 0; |
| 4373 | if (TemplateDeductionResult Result |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4374 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4375 | Specialization, Info)) { |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4376 | CandidateSet.push_back(OverloadCandidate()); |
| 4377 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 4378 | Candidate.FoundDecl = FoundDecl; |
| 4379 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 4380 | Candidate.Viable = false; |
| 4381 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 4382 | Candidate.IsSurrogate = false; |
| 4383 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4384 | Candidate.ExplicitCallArguments = 1; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4385 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4386 | Info); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4387 | return; |
| 4388 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4389 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4390 | // Add the conversion function template specialization produced by |
| 4391 | // template argument deduction as a candidate. |
| 4392 | assert(Specialization && "Missing function template specialization?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4393 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4394 | CandidateSet); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4395 | } |
| 4396 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4397 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 4398 | /// converts the given @c Object to a function pointer via the |
| 4399 | /// conversion function @c Conversion, and then attempts to call it |
| 4400 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 4401 | /// the type of function that we'll eventually be calling. |
| 4402 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4403 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4404 | CXXRecordDecl *ActingContext, |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4405 | const FunctionProtoType *Proto, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4406 | Expr *Object, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4407 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4408 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4409 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 4410 | return; |
| 4411 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4412 | // Overload resolution is always an unevaluated context. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4413 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4414 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4415 | CandidateSet.push_back(OverloadCandidate()); |
| 4416 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4417 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4418 | Candidate.Function = 0; |
| 4419 | Candidate.Surrogate = Conversion; |
| 4420 | Candidate.Viable = true; |
| 4421 | Candidate.IsSurrogate = true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4422 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4423 | Candidate.Conversions.resize(NumArgs + 1); |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4424 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4425 | |
| 4426 | // Determine the implicit conversion sequence for the implicit |
| 4427 | // object parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4428 | ImplicitConversionSequence ObjectInit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4429 | = TryObjectArgumentInitialization(*this, Object->getType(), |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4430 | Object->Classify(Context), |
| 4431 | Conversion, ActingContext); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4432 | if (ObjectInit.isBad()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4433 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4434 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4435 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4436 | return; |
| 4437 | } |
| 4438 | |
| 4439 | // The first conversion is actually a user-defined conversion whose |
| 4440 | // first conversion is ObjectInit's standard conversion (which is |
| 4441 | // effectively a reference binding). Record it as such. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4442 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4443 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 4444 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4445 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4446 | Candidate.Conversions[0].UserDefined.FoundConversionFunction |
Douglas Gregor | 2bbfba0 | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 4447 | = FoundDecl.getDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4448 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4449 | = Candidate.Conversions[0].UserDefined.Before; |
| 4450 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 4451 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4452 | // Find the |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4453 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4454 | |
| 4455 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 4456 | // parameters is viable only if it has an ellipsis in its parameter |
| 4457 | // list (8.3.5). |
| 4458 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 4459 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4460 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4461 | return; |
| 4462 | } |
| 4463 | |
| 4464 | // Function types don't have any default arguments, so just check if |
| 4465 | // we have enough arguments. |
| 4466 | if (NumArgs < NumArgsInProto) { |
| 4467 | // Not enough arguments. |
| 4468 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4469 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4470 | return; |
| 4471 | } |
| 4472 | |
| 4473 | // Determine the implicit conversion sequences for each of the |
| 4474 | // arguments. |
| 4475 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 4476 | if (ArgIdx < NumArgsInProto) { |
| 4477 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 4478 | // exist for each argument an implicit conversion sequence |
| 4479 | // (13.3.3.1) that converts that argument to the corresponding |
| 4480 | // parameter of F. |
| 4481 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4482 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4483 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 4484 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 4485 | /*InOverloadResolution=*/false); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4486 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4487 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4488 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4489 | break; |
| 4490 | } |
| 4491 | } else { |
| 4492 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 4493 | // argument for which there is no corresponding parameter is |
| 4494 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4495 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4496 | } |
| 4497 | } |
| 4498 | } |
| 4499 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4500 | /// \brief Add overload candidates for overloaded operators that are |
| 4501 | /// member functions. |
| 4502 | /// |
| 4503 | /// Add the overloaded operator candidates that are member functions |
| 4504 | /// for the operator Op that was used in an operator expression such |
| 4505 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 4506 | /// CandidateSet will store the added overload candidates. (C++ |
| 4507 | /// [over.match.oper]). |
| 4508 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 4509 | SourceLocation OpLoc, |
| 4510 | Expr **Args, unsigned NumArgs, |
| 4511 | OverloadCandidateSet& CandidateSet, |
| 4512 | SourceRange OpRange) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4513 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4514 | |
| 4515 | // C++ [over.match.oper]p3: |
| 4516 | // For a unary operator @ with an operand of a type whose |
| 4517 | // cv-unqualified version is T1, and for a binary operator @ with |
| 4518 | // a left operand of a type whose cv-unqualified version is T1 and |
| 4519 | // a right operand of a type whose cv-unqualified version is T2, |
| 4520 | // three sets of candidate functions, designated member |
| 4521 | // candidates, non-member candidates and built-in candidates, are |
| 4522 | // constructed as follows: |
| 4523 | QualType T1 = Args[0]->getType(); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4524 | |
| 4525 | // -- If T1 is a class type, the set of member candidates is the |
| 4526 | // result of the qualified lookup of T1::operator@ |
| 4527 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 4528 | // empty. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4529 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 4530 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 4531 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 4532 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4533 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4534 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 4535 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 4536 | Operators.suppressDiagnostics(); |
| 4537 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4538 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 4539 | OperEnd = Operators.end(); |
| 4540 | Oper != OperEnd; |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4541 | ++Oper) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4542 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4543 | Args[0]->Classify(Context), Args + 1, NumArgs - 1, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4544 | CandidateSet, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4545 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4546 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4547 | } |
| 4548 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4549 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 4550 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 4551 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4552 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 4553 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4554 | /// operator. NumContextualBoolArguments is the number of arguments |
| 4555 | /// (at the beginning of the argument list) that will be contextually |
| 4556 | /// converted to bool. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4557 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4558 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4559 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4560 | bool IsAssignmentOperator, |
| 4561 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4562 | // Overload resolution is always an unevaluated context. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4563 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4564 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4565 | // Add this candidate |
| 4566 | CandidateSet.push_back(OverloadCandidate()); |
| 4567 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4568 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4569 | Candidate.Function = 0; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 4570 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4571 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4572 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 4573 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4574 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 4575 | |
| 4576 | // Determine the implicit conversion sequences for each of the |
| 4577 | // arguments. |
| 4578 | Candidate.Viable = true; |
| 4579 | Candidate.Conversions.resize(NumArgs); |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4580 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4581 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4582 | // C++ [over.match.oper]p4: |
| 4583 | // For the built-in assignment operators, conversions of the |
| 4584 | // left operand are restricted as follows: |
| 4585 | // -- no temporaries are introduced to hold the left operand, and |
| 4586 | // -- no user-defined conversions are applied to the left |
| 4587 | // operand to achieve a type match with the left-most |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4588 | // parameter of a built-in candidate. |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4589 | // |
| 4590 | // We block these conversions by turning off user-defined |
| 4591 | // conversions, since that is the only way that initialization of |
| 4592 | // a reference to a non-class type can occur from something that |
| 4593 | // is not of the same type. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4594 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4595 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4596 | "Contextual conversion to bool requires bool type"); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4597 | Candidate.Conversions[ArgIdx] |
| 4598 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4599 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4600 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4601 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 4602 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 4603 | /*InOverloadResolution=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4604 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4605 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4606 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4607 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4608 | break; |
| 4609 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4610 | } |
| 4611 | } |
| 4612 | |
| 4613 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 4614 | /// candidate operator functions for built-in operators (C++ |
| 4615 | /// [over.built]). The types are separated into pointer types and |
| 4616 | /// enumeration types. |
| 4617 | class BuiltinCandidateTypeSet { |
| 4618 | /// TypeSet - A set of types. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4619 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4620 | |
| 4621 | /// PointerTypes - The set of pointer types that will be used in the |
| 4622 | /// built-in candidates. |
| 4623 | TypeSet PointerTypes; |
| 4624 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4625 | /// MemberPointerTypes - The set of member pointer types that will be |
| 4626 | /// used in the built-in candidates. |
| 4627 | TypeSet MemberPointerTypes; |
| 4628 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4629 | /// EnumerationTypes - The set of enumeration types that will be |
| 4630 | /// used in the built-in candidates. |
| 4631 | TypeSet EnumerationTypes; |
| 4632 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4633 | /// \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] | 4634 | /// candidates. |
| 4635 | TypeSet VectorTypes; |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4636 | |
| 4637 | /// \brief A flag indicating non-record types are viable candidates |
| 4638 | bool HasNonRecordTypes; |
| 4639 | |
| 4640 | /// \brief A flag indicating whether either arithmetic or enumeration types |
| 4641 | /// were present in the candidate set. |
| 4642 | bool HasArithmeticOrEnumeralTypes; |
| 4643 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4644 | /// Sema - The semantic analysis instance where we are building the |
| 4645 | /// candidate type set. |
| 4646 | Sema &SemaRef; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4647 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4648 | /// Context - The AST context in which we will build the type sets. |
| 4649 | ASTContext &Context; |
| 4650 | |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4651 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 4652 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4653 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4654 | |
| 4655 | public: |
| 4656 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4657 | typedef TypeSet::iterator iterator; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4658 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4659 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4660 | : HasNonRecordTypes(false), |
| 4661 | HasArithmeticOrEnumeralTypes(false), |
| 4662 | SemaRef(SemaRef), |
| 4663 | Context(SemaRef.Context) { } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4664 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4665 | void AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4666 | SourceLocation Loc, |
| 4667 | bool AllowUserConversions, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4668 | bool AllowExplicitConversions, |
| 4669 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4670 | |
| 4671 | /// pointer_begin - First pointer type found; |
| 4672 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 4673 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4674 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4675 | iterator pointer_end() { return PointerTypes.end(); } |
| 4676 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4677 | /// member_pointer_begin - First member pointer type found; |
| 4678 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 4679 | |
| 4680 | /// member_pointer_end - Past the last member pointer type found; |
| 4681 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 4682 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4683 | /// enumeration_begin - First enumeration type found; |
| 4684 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 4685 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4686 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4687 | iterator enumeration_end() { return EnumerationTypes.end(); } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4688 | |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4689 | iterator vector_begin() { return VectorTypes.begin(); } |
| 4690 | iterator vector_end() { return VectorTypes.end(); } |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4691 | |
| 4692 | bool hasNonRecordTypes() { return HasNonRecordTypes; } |
| 4693 | bool hasArithmeticOrEnumeralTypes() { return HasArithmeticOrEnumeralTypes; } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4694 | }; |
| 4695 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4696 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4697 | /// the set of pointer types along with any more-qualified variants of |
| 4698 | /// that type. For example, if @p Ty is "int const *", this routine |
| 4699 | /// will add "int const *", "int const volatile *", "int const |
| 4700 | /// restrict *", and "int const volatile restrict *" to the set of |
| 4701 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 4702 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4703 | /// |
| 4704 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4705 | bool |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4706 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 4707 | const Qualifiers &VisibleQuals) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4708 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4709 | // Insert this type. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4710 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4711 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4712 | |
Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4713 | QualType PointeeTy; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4714 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
Fariborz Jahanian | f2afc80 | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4715 | bool buildObjCPtr = false; |
Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4716 | if (!PointerTy) { |
Fariborz Jahanian | f2afc80 | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4717 | if (const ObjCObjectPointerType *PTy = Ty->getAs<ObjCObjectPointerType>()) { |
Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4718 | PointeeTy = PTy->getPointeeType(); |
Fariborz Jahanian | f2afc80 | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4719 | buildObjCPtr = true; |
| 4720 | } |
Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4721 | else |
| 4722 | assert(false && "type was not a pointer type!"); |
| 4723 | } |
| 4724 | else |
| 4725 | PointeeTy = PointerTy->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4726 | |
Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 4727 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 4728 | // (the qualifier would sink to the element type), and for another, the |
| 4729 | // only overload situation where it matters is subscript or pointer +- int, |
| 4730 | // and those shouldn't have qualifier variants anyway. |
| 4731 | if (PointeeTy->isArrayType()) |
| 4732 | return true; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4733 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 4ef1d40 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 4734 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | facfdd4 | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 4735 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4736 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 4737 | bool hasRestrict = VisibleQuals.hasRestrict(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4738 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4739 | // Iterate through all strict supersets of BaseCVR. |
| 4740 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 4741 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4742 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 4743 | // in the types. |
| 4744 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 4745 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4746 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Fariborz Jahanian | f2afc80 | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4747 | if (!buildObjCPtr) |
| 4748 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
| 4749 | else |
| 4750 | PointerTypes.insert(Context.getObjCObjectPointerType(QPointeeTy)); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4751 | } |
| 4752 | |
| 4753 | return true; |
| 4754 | } |
| 4755 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4756 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 4757 | /// to the set of pointer types along with any more-qualified variants of |
| 4758 | /// that type. For example, if @p Ty is "int const *", this routine |
| 4759 | /// will add "int const *", "int const volatile *", "int const |
| 4760 | /// restrict *", and "int const volatile restrict *" to the set of |
| 4761 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 4762 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4763 | /// |
| 4764 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4765 | bool |
| 4766 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 4767 | QualType Ty) { |
| 4768 | // Insert this type. |
| 4769 | if (!MemberPointerTypes.insert(Ty)) |
| 4770 | return false; |
| 4771 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4772 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 4773 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4774 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4775 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 4776 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 4777 | // (the qualifier would sink to the element type), and for another, the |
| 4778 | // only overload situation where it matters is subscript or pointer +- int, |
| 4779 | // and those shouldn't have qualifier variants anyway. |
| 4780 | if (PointeeTy->isArrayType()) |
| 4781 | return true; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4782 | const Type *ClassTy = PointerTy->getClass(); |
| 4783 | |
| 4784 | // Iterate through all strict supersets of the pointee type's CVR |
| 4785 | // qualifiers. |
| 4786 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 4787 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 4788 | if ((CVR | BaseCVR) != CVR) continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4789 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4790 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4791 | MemberPointerTypes.insert( |
| 4792 | Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4793 | } |
| 4794 | |
| 4795 | return true; |
| 4796 | } |
| 4797 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4798 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 4799 | /// 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] | 4800 | /// primarily interested in pointer types and enumeration types. We also |
| 4801 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4802 | /// AllowUserConversions is true if we should look at the conversion |
| 4803 | /// functions of a class type, and AllowExplicitConversions if we |
| 4804 | /// should also include the explicit conversion functions of a class |
| 4805 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4806 | void |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4807 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4808 | SourceLocation Loc, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4809 | bool AllowUserConversions, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4810 | bool AllowExplicitConversions, |
| 4811 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4812 | // Only deal with canonical types. |
| 4813 | Ty = Context.getCanonicalType(Ty); |
| 4814 | |
| 4815 | // Look through reference types; they aren't part of the type of an |
| 4816 | // expression for the purposes of conversions. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4817 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4818 | Ty = RefTy->getPointeeType(); |
| 4819 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4820 | // If we're dealing with an array type, decay to the pointer. |
| 4821 | if (Ty->isArrayType()) |
| 4822 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 4823 | |
| 4824 | // Otherwise, we don't care about qualifiers on the type. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4825 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4826 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4827 | // Flag if we ever add a non-record type. |
| 4828 | const RecordType *TyRec = Ty->getAs<RecordType>(); |
| 4829 | HasNonRecordTypes = HasNonRecordTypes || !TyRec; |
| 4830 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4831 | // Flag if we encounter an arithmetic type. |
| 4832 | HasArithmeticOrEnumeralTypes = |
| 4833 | HasArithmeticOrEnumeralTypes || Ty->isArithmeticType(); |
| 4834 | |
Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4835 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
| 4836 | PointerTypes.insert(Ty); |
| 4837 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4838 | // Insert our type, and its more-qualified variants, into the set |
| 4839 | // of types. |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4840 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4841 | return; |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4842 | } else if (Ty->isMemberPointerType()) { |
| 4843 | // Member pointers are far easier, since the pointee can't be converted. |
| 4844 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 4845 | return; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4846 | } else if (Ty->isEnumeralType()) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4847 | HasArithmeticOrEnumeralTypes = true; |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4848 | EnumerationTypes.insert(Ty); |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4849 | } else if (Ty->isVectorType()) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4850 | // We treat vector types as arithmetic types in many contexts as an |
| 4851 | // extension. |
| 4852 | HasArithmeticOrEnumeralTypes = true; |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4853 | VectorTypes.insert(Ty); |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4854 | } else if (AllowUserConversions && TyRec) { |
| 4855 | // No conversion functions in incomplete types. |
| 4856 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) |
| 4857 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4858 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4859 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| 4860 | const UnresolvedSetImpl *Conversions |
| 4861 | = ClassDecl->getVisibleConversionFunctions(); |
| 4862 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 4863 | E = Conversions->end(); I != E; ++I) { |
| 4864 | NamedDecl *D = I.getDecl(); |
| 4865 | if (isa<UsingShadowDecl>(D)) |
| 4866 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4867 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4868 | // Skip conversion function templates; they don't tell us anything |
| 4869 | // about which builtin types we can convert to. |
| 4870 | if (isa<FunctionTemplateDecl>(D)) |
| 4871 | continue; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4872 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4873 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
| 4874 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
| 4875 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
| 4876 | VisibleQuals); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4877 | } |
| 4878 | } |
| 4879 | } |
| 4880 | } |
| 4881 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4882 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 4883 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 4884 | /// given type to the candidate set. |
| 4885 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 4886 | QualType T, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4887 | Expr **Args, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4888 | unsigned NumArgs, |
| 4889 | OverloadCandidateSet &CandidateSet) { |
| 4890 | QualType ParamTypes[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4891 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4892 | // T& operator=(T&, T) |
| 4893 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 4894 | ParamTypes[1] = T; |
| 4895 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4896 | /*IsAssignmentOperator=*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4897 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4898 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 4899 | // volatile T& operator=(volatile T&, T) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4900 | ParamTypes[0] |
| 4901 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4902 | ParamTypes[1] = T; |
| 4903 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4904 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4905 | } |
| 4906 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4907 | |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 4908 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 4909 | /// 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] | 4910 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 4911 | Qualifiers VRQuals; |
| 4912 | const RecordType *TyRec; |
| 4913 | if (const MemberPointerType *RHSMPType = |
| 4914 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4915 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4916 | else |
| 4917 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 4918 | if (!TyRec) { |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4919 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4920 | VRQuals.addVolatile(); |
| 4921 | VRQuals.addRestrict(); |
| 4922 | return VRQuals; |
| 4923 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4924 | |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4925 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4926 | if (!ClassDecl->hasDefinition()) |
| 4927 | return VRQuals; |
| 4928 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4929 | const UnresolvedSetImpl *Conversions = |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 4930 | ClassDecl->getVisibleConversionFunctions(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4931 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4932 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4933 | E = Conversions->end(); I != E; ++I) { |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4934 | NamedDecl *D = I.getDecl(); |
| 4935 | if (isa<UsingShadowDecl>(D)) |
| 4936 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 4937 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4938 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 4939 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 4940 | CanTy = ResTypeRef->getPointeeType(); |
| 4941 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 4942 | // as see them. |
| 4943 | bool done = false; |
| 4944 | while (!done) { |
| 4945 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 4946 | CanTy = ResTypePtr->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4947 | else if (const MemberPointerType *ResTypeMPtr = |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4948 | CanTy->getAs<MemberPointerType>()) |
| 4949 | CanTy = ResTypeMPtr->getPointeeType(); |
| 4950 | else |
| 4951 | done = true; |
| 4952 | if (CanTy.isVolatileQualified()) |
| 4953 | VRQuals.addVolatile(); |
| 4954 | if (CanTy.isRestrictQualified()) |
| 4955 | VRQuals.addRestrict(); |
| 4956 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 4957 | return VRQuals; |
| 4958 | } |
| 4959 | } |
| 4960 | } |
| 4961 | return VRQuals; |
| 4962 | } |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 4963 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 4964 | namespace { |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 4965 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 4966 | /// \brief Helper class to manage the addition of builtin operator overload |
| 4967 | /// candidates. It provides shared state and utility methods used throughout |
| 4968 | /// the process, as well as a helper method to add each group of builtin |
| 4969 | /// operator overloads from the standard to a candidate set. |
| 4970 | class BuiltinOperatorOverloadBuilder { |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 4971 | // Common instance state available to all overload candidate addition methods. |
| 4972 | Sema &S; |
| 4973 | Expr **Args; |
| 4974 | unsigned NumArgs; |
| 4975 | Qualifiers VisibleTypeConversionsQuals; |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4976 | bool HasArithmeticOrEnumeralCandidateType; |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 4977 | llvm::SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes; |
| 4978 | OverloadCandidateSet &CandidateSet; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 4979 | |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 4980 | // Define some constants used to index and iterate over the arithemetic types |
| 4981 | // provided via the getArithmeticType() method below. |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 4982 | // The "promoted arithmetic types" are the arithmetic |
| 4983 | // types are that preserved by promotion (C++ [over.built]p2). |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 4984 | static const unsigned FirstIntegralType = 3; |
| 4985 | static const unsigned LastIntegralType = 18; |
| 4986 | static const unsigned FirstPromotedIntegralType = 3, |
| 4987 | LastPromotedIntegralType = 9; |
| 4988 | static const unsigned FirstPromotedArithmeticType = 0, |
| 4989 | LastPromotedArithmeticType = 9; |
| 4990 | static const unsigned NumArithmeticTypes = 18; |
| 4991 | |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 4992 | /// \brief Get the canonical type for a given arithmetic type index. |
| 4993 | CanQualType getArithmeticType(unsigned index) { |
| 4994 | assert(index < NumArithmeticTypes); |
| 4995 | static CanQualType ASTContext::* const |
| 4996 | ArithmeticTypes[NumArithmeticTypes] = { |
| 4997 | // Start of promoted types. |
| 4998 | &ASTContext::FloatTy, |
| 4999 | &ASTContext::DoubleTy, |
| 5000 | &ASTContext::LongDoubleTy, |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 5001 | |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5002 | // Start of integral types. |
| 5003 | &ASTContext::IntTy, |
| 5004 | &ASTContext::LongTy, |
| 5005 | &ASTContext::LongLongTy, |
| 5006 | &ASTContext::UnsignedIntTy, |
| 5007 | &ASTContext::UnsignedLongTy, |
| 5008 | &ASTContext::UnsignedLongLongTy, |
| 5009 | // End of promoted types. |
| 5010 | |
| 5011 | &ASTContext::BoolTy, |
| 5012 | &ASTContext::CharTy, |
| 5013 | &ASTContext::WCharTy, |
| 5014 | &ASTContext::Char16Ty, |
| 5015 | &ASTContext::Char32Ty, |
| 5016 | &ASTContext::SignedCharTy, |
| 5017 | &ASTContext::ShortTy, |
| 5018 | &ASTContext::UnsignedCharTy, |
| 5019 | &ASTContext::UnsignedShortTy, |
| 5020 | // End of integral types. |
| 5021 | // FIXME: What about complex? |
| 5022 | }; |
| 5023 | return S.Context.*ArithmeticTypes[index]; |
| 5024 | } |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5025 | |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5026 | /// \brief Gets the canonical type resulting from the usual arithemetic |
| 5027 | /// converions for the given arithmetic types. |
| 5028 | CanQualType getUsualArithmeticConversions(unsigned L, unsigned R) { |
| 5029 | // Accelerator table for performing the usual arithmetic conversions. |
| 5030 | // The rules are basically: |
| 5031 | // - if either is floating-point, use the wider floating-point |
| 5032 | // - if same signedness, use the higher rank |
| 5033 | // - if same size, use unsigned of the higher rank |
| 5034 | // - use the larger type |
| 5035 | // These rules, together with the axiom that higher ranks are |
| 5036 | // never smaller, are sufficient to precompute all of these results |
| 5037 | // *except* when dealing with signed types of higher rank. |
| 5038 | // (we could precompute SLL x UI for all known platforms, but it's |
| 5039 | // better not to make any assumptions). |
| 5040 | enum PromotedType { |
| 5041 | Flt, Dbl, LDbl, SI, SL, SLL, UI, UL, ULL, Dep=-1 |
| 5042 | }; |
| 5043 | static PromotedType ConversionsTable[LastPromotedArithmeticType] |
| 5044 | [LastPromotedArithmeticType] = { |
| 5045 | /* Flt*/ { Flt, Dbl, LDbl, Flt, Flt, Flt, Flt, Flt, Flt }, |
| 5046 | /* Dbl*/ { Dbl, Dbl, LDbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl }, |
| 5047 | /*LDbl*/ { LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl }, |
| 5048 | /* SI*/ { Flt, Dbl, LDbl, SI, SL, SLL, UI, UL, ULL }, |
| 5049 | /* SL*/ { Flt, Dbl, LDbl, SL, SL, SLL, Dep, UL, ULL }, |
| 5050 | /* SLL*/ { Flt, Dbl, LDbl, SLL, SLL, SLL, Dep, Dep, ULL }, |
| 5051 | /* UI*/ { Flt, Dbl, LDbl, UI, Dep, Dep, UI, UL, ULL }, |
| 5052 | /* UL*/ { Flt, Dbl, LDbl, UL, UL, Dep, UL, UL, ULL }, |
| 5053 | /* ULL*/ { Flt, Dbl, LDbl, ULL, ULL, ULL, ULL, ULL, ULL }, |
| 5054 | }; |
| 5055 | |
| 5056 | assert(L < LastPromotedArithmeticType); |
| 5057 | assert(R < LastPromotedArithmeticType); |
| 5058 | int Idx = ConversionsTable[L][R]; |
| 5059 | |
| 5060 | // Fast path: the table gives us a concrete answer. |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5061 | if (Idx != Dep) return getArithmeticType(Idx); |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5062 | |
| 5063 | // Slow path: we need to compare widths. |
| 5064 | // An invariant is that the signed type has higher rank. |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5065 | CanQualType LT = getArithmeticType(L), |
| 5066 | RT = getArithmeticType(R); |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5067 | unsigned LW = S.Context.getIntWidth(LT), |
| 5068 | RW = S.Context.getIntWidth(RT); |
| 5069 | |
| 5070 | // If they're different widths, use the signed type. |
| 5071 | if (LW > RW) return LT; |
| 5072 | else if (LW < RW) return RT; |
| 5073 | |
| 5074 | // Otherwise, use the unsigned type of the signed type's rank. |
| 5075 | if (L == SL || R == SL) return S.Context.UnsignedLongTy; |
| 5076 | assert(L == SLL || R == SLL); |
| 5077 | return S.Context.UnsignedLongLongTy; |
| 5078 | } |
| 5079 | |
Chandler Carruth | 5659c0c | 2010-12-12 09:22:45 +0000 | [diff] [blame] | 5080 | /// \brief Helper method to factor out the common pattern of adding overloads |
| 5081 | /// for '++' and '--' builtin operators. |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5082 | void addPlusPlusMinusMinusStyleOverloads(QualType CandidateTy, |
| 5083 | bool HasVolatile) { |
| 5084 | QualType ParamTypes[2] = { |
| 5085 | S.Context.getLValueReferenceType(CandidateTy), |
| 5086 | S.Context.IntTy |
| 5087 | }; |
| 5088 | |
| 5089 | // Non-volatile version. |
| 5090 | if (NumArgs == 1) |
| 5091 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 5092 | else |
| 5093 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 5094 | |
| 5095 | // Use a heuristic to reduce number of builtin candidates in the set: |
| 5096 | // add volatile version only if there are conversions to a volatile type. |
| 5097 | if (HasVolatile) { |
| 5098 | ParamTypes[0] = |
| 5099 | S.Context.getLValueReferenceType( |
| 5100 | S.Context.getVolatileType(CandidateTy)); |
| 5101 | if (NumArgs == 1) |
| 5102 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 5103 | else |
| 5104 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 5105 | } |
| 5106 | } |
| 5107 | |
| 5108 | public: |
| 5109 | BuiltinOperatorOverloadBuilder( |
| 5110 | Sema &S, Expr **Args, unsigned NumArgs, |
| 5111 | Qualifiers VisibleTypeConversionsQuals, |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5112 | bool HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5113 | llvm::SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes, |
| 5114 | OverloadCandidateSet &CandidateSet) |
| 5115 | : S(S), Args(Args), NumArgs(NumArgs), |
| 5116 | VisibleTypeConversionsQuals(VisibleTypeConversionsQuals), |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5117 | HasArithmeticOrEnumeralCandidateType( |
| 5118 | HasArithmeticOrEnumeralCandidateType), |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5119 | CandidateTypes(CandidateTypes), |
| 5120 | CandidateSet(CandidateSet) { |
| 5121 | // Validate some of our static helper constants in debug builds. |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5122 | assert(getArithmeticType(FirstPromotedIntegralType) == S.Context.IntTy && |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5123 | "Invalid first promoted integral type"); |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5124 | assert(getArithmeticType(LastPromotedIntegralType - 1) |
| 5125 | == S.Context.UnsignedLongLongTy && |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5126 | "Invalid last promoted integral type"); |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5127 | assert(getArithmeticType(FirstPromotedArithmeticType) |
| 5128 | == S.Context.FloatTy && |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5129 | "Invalid first promoted arithmetic type"); |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5130 | assert(getArithmeticType(LastPromotedArithmeticType - 1) |
| 5131 | == S.Context.UnsignedLongLongTy && |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5132 | "Invalid last promoted arithmetic type"); |
| 5133 | } |
| 5134 | |
| 5135 | // C++ [over.built]p3: |
| 5136 | // |
| 5137 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 5138 | // is either volatile or empty, there exist candidate operator |
| 5139 | // functions of the form |
| 5140 | // |
| 5141 | // VQ T& operator++(VQ T&); |
| 5142 | // T operator++(VQ T&, int); |
| 5143 | // |
| 5144 | // C++ [over.built]p4: |
| 5145 | // |
| 5146 | // For every pair (T, VQ), where T is an arithmetic type other |
| 5147 | // than bool, and VQ is either volatile or empty, there exist |
| 5148 | // candidate operator functions of the form |
| 5149 | // |
| 5150 | // VQ T& operator--(VQ T&); |
| 5151 | // T operator--(VQ T&, int); |
| 5152 | void addPlusPlusMinusMinusArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5153 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5154 | return; |
| 5155 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5156 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 5157 | Arith < NumArithmeticTypes; ++Arith) { |
| 5158 | addPlusPlusMinusMinusStyleOverloads( |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5159 | getArithmeticType(Arith), |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5160 | VisibleTypeConversionsQuals.hasVolatile()); |
| 5161 | } |
| 5162 | } |
| 5163 | |
| 5164 | // C++ [over.built]p5: |
| 5165 | // |
| 5166 | // For every pair (T, VQ), where T is a cv-qualified or |
| 5167 | // cv-unqualified object type, and VQ is either volatile or |
| 5168 | // empty, there exist candidate operator functions of the form |
| 5169 | // |
| 5170 | // T*VQ& operator++(T*VQ&); |
| 5171 | // T*VQ& operator--(T*VQ&); |
| 5172 | // T* operator++(T*VQ&, int); |
| 5173 | // T* operator--(T*VQ&, int); |
| 5174 | void addPlusPlusMinusMinusPointerOverloads() { |
| 5175 | for (BuiltinCandidateTypeSet::iterator |
| 5176 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5177 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5178 | Ptr != PtrEnd; ++Ptr) { |
| 5179 | // Skip pointer types that aren't pointers to object types. |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5180 | if (!(*Ptr)->getPointeeType()->isObjectType()) |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5181 | continue; |
| 5182 | |
| 5183 | addPlusPlusMinusMinusStyleOverloads(*Ptr, |
| 5184 | (!S.Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 5185 | VisibleTypeConversionsQuals.hasVolatile())); |
| 5186 | } |
| 5187 | } |
| 5188 | |
| 5189 | // C++ [over.built]p6: |
| 5190 | // For every cv-qualified or cv-unqualified object type T, there |
| 5191 | // exist candidate operator functions of the form |
| 5192 | // |
| 5193 | // T& operator*(T*); |
| 5194 | // |
| 5195 | // C++ [over.built]p7: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5196 | // 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] | 5197 | // ref-qualifier, there exist candidate operator functions of the form |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5198 | // T& operator*(T*); |
| 5199 | void addUnaryStarPointerOverloads() { |
| 5200 | for (BuiltinCandidateTypeSet::iterator |
| 5201 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5202 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5203 | Ptr != PtrEnd; ++Ptr) { |
| 5204 | QualType ParamTy = *Ptr; |
| 5205 | QualType PointeeTy = ParamTy->getPointeeType(); |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5206 | if (!PointeeTy->isObjectType() && !PointeeTy->isFunctionType()) |
| 5207 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5208 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5209 | if (const FunctionProtoType *Proto =PointeeTy->getAs<FunctionProtoType>()) |
| 5210 | if (Proto->getTypeQuals() || Proto->getRefQualifier()) |
| 5211 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5212 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5213 | S.AddBuiltinCandidate(S.Context.getLValueReferenceType(PointeeTy), |
| 5214 | &ParamTy, Args, 1, CandidateSet); |
| 5215 | } |
| 5216 | } |
| 5217 | |
| 5218 | // C++ [over.built]p9: |
| 5219 | // For every promoted arithmetic type T, there exist candidate |
| 5220 | // operator functions of the form |
| 5221 | // |
| 5222 | // T operator+(T); |
| 5223 | // T operator-(T); |
| 5224 | void addUnaryPlusOrMinusArithmeticOverloads() { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5225 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5226 | return; |
| 5227 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5228 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 5229 | Arith < LastPromotedArithmeticType; ++Arith) { |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5230 | QualType ArithTy = getArithmeticType(Arith); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5231 | S.AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 5232 | } |
| 5233 | |
| 5234 | // Extension: We also add these operators for vector types. |
| 5235 | for (BuiltinCandidateTypeSet::iterator |
| 5236 | Vec = CandidateTypes[0].vector_begin(), |
| 5237 | VecEnd = CandidateTypes[0].vector_end(); |
| 5238 | Vec != VecEnd; ++Vec) { |
| 5239 | QualType VecTy = *Vec; |
| 5240 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 5241 | } |
| 5242 | } |
| 5243 | |
| 5244 | // C++ [over.built]p8: |
| 5245 | // For every type T, there exist candidate operator functions of |
| 5246 | // the form |
| 5247 | // |
| 5248 | // T* operator+(T*); |
| 5249 | void addUnaryPlusPointerOverloads() { |
| 5250 | for (BuiltinCandidateTypeSet::iterator |
| 5251 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5252 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5253 | Ptr != PtrEnd; ++Ptr) { |
| 5254 | QualType ParamTy = *Ptr; |
| 5255 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 5256 | } |
| 5257 | } |
| 5258 | |
| 5259 | // C++ [over.built]p10: |
| 5260 | // For every promoted integral type T, there exist candidate |
| 5261 | // operator functions of the form |
| 5262 | // |
| 5263 | // T operator~(T); |
| 5264 | void addUnaryTildePromotedIntegralOverloads() { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5265 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5266 | return; |
| 5267 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5268 | for (unsigned Int = FirstPromotedIntegralType; |
| 5269 | Int < LastPromotedIntegralType; ++Int) { |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5270 | QualType IntTy = getArithmeticType(Int); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5271 | S.AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 5272 | } |
| 5273 | |
| 5274 | // Extension: We also add this operator for vector types. |
| 5275 | for (BuiltinCandidateTypeSet::iterator |
| 5276 | Vec = CandidateTypes[0].vector_begin(), |
| 5277 | VecEnd = CandidateTypes[0].vector_end(); |
| 5278 | Vec != VecEnd; ++Vec) { |
| 5279 | QualType VecTy = *Vec; |
| 5280 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 5281 | } |
| 5282 | } |
| 5283 | |
| 5284 | // C++ [over.match.oper]p16: |
| 5285 | // For every pointer to member type T, there exist candidate operator |
| 5286 | // functions of the form |
| 5287 | // |
| 5288 | // bool operator==(T,T); |
| 5289 | // bool operator!=(T,T); |
| 5290 | void addEqualEqualOrNotEqualMemberPointerOverloads() { |
| 5291 | /// Set of (canonical) types that we've already handled. |
| 5292 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5293 | |
| 5294 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 5295 | for (BuiltinCandidateTypeSet::iterator |
| 5296 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 5297 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 5298 | MemPtr != MemPtrEnd; |
| 5299 | ++MemPtr) { |
| 5300 | // Don't add the same builtin candidate twice. |
| 5301 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 5302 | continue; |
| 5303 | |
| 5304 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 5305 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 5306 | CandidateSet); |
| 5307 | } |
| 5308 | } |
| 5309 | } |
| 5310 | |
| 5311 | // C++ [over.built]p15: |
| 5312 | // |
| 5313 | // For every pointer or enumeration type T, there exist |
| 5314 | // candidate operator functions of the form |
| 5315 | // |
| 5316 | // bool operator<(T, T); |
| 5317 | // bool operator>(T, T); |
| 5318 | // bool operator<=(T, T); |
| 5319 | // bool operator>=(T, T); |
| 5320 | // bool operator==(T, T); |
| 5321 | // bool operator!=(T, T); |
Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 5322 | void addRelationalPointerOrEnumeralOverloads() { |
| 5323 | // C++ [over.built]p1: |
| 5324 | // If there is a user-written candidate with the same name and parameter |
| 5325 | // types as a built-in candidate operator function, the built-in operator |
| 5326 | // function is hidden and is not included in the set of candidate |
| 5327 | // functions. |
| 5328 | // |
| 5329 | // The text is actually in a note, but if we don't implement it then we end |
| 5330 | // up with ambiguities when the user provides an overloaded operator for |
| 5331 | // an enumeration type. Note that only enumeration types have this problem, |
| 5332 | // so we track which enumeration types we've seen operators for. Also, the |
| 5333 | // only other overloaded operator with enumeration argumenst, operator=, |
| 5334 | // cannot be overloaded for enumeration types, so this is the only place |
| 5335 | // where we must suppress candidates like this. |
| 5336 | llvm::DenseSet<std::pair<CanQualType, CanQualType> > |
| 5337 | UserDefinedBinaryOperators; |
| 5338 | |
| 5339 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 5340 | if (CandidateTypes[ArgIdx].enumeration_begin() != |
| 5341 | CandidateTypes[ArgIdx].enumeration_end()) { |
| 5342 | for (OverloadCandidateSet::iterator C = CandidateSet.begin(), |
| 5343 | CEnd = CandidateSet.end(); |
| 5344 | C != CEnd; ++C) { |
| 5345 | if (!C->Viable || !C->Function || C->Function->getNumParams() != 2) |
| 5346 | continue; |
| 5347 | |
| 5348 | QualType FirstParamType = |
| 5349 | C->Function->getParamDecl(0)->getType().getUnqualifiedType(); |
| 5350 | QualType SecondParamType = |
| 5351 | C->Function->getParamDecl(1)->getType().getUnqualifiedType(); |
| 5352 | |
| 5353 | // Skip if either parameter isn't of enumeral type. |
| 5354 | if (!FirstParamType->isEnumeralType() || |
| 5355 | !SecondParamType->isEnumeralType()) |
| 5356 | continue; |
| 5357 | |
| 5358 | // Add this operator to the set of known user-defined operators. |
| 5359 | UserDefinedBinaryOperators.insert( |
| 5360 | std::make_pair(S.Context.getCanonicalType(FirstParamType), |
| 5361 | S.Context.getCanonicalType(SecondParamType))); |
| 5362 | } |
| 5363 | } |
| 5364 | } |
| 5365 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5366 | /// Set of (canonical) types that we've already handled. |
| 5367 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5368 | |
| 5369 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 5370 | for (BuiltinCandidateTypeSet::iterator |
| 5371 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 5372 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 5373 | Ptr != PtrEnd; ++Ptr) { |
| 5374 | // Don't add the same builtin candidate twice. |
| 5375 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 5376 | continue; |
| 5377 | |
| 5378 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5379 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 5380 | CandidateSet); |
| 5381 | } |
| 5382 | for (BuiltinCandidateTypeSet::iterator |
| 5383 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 5384 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 5385 | Enum != EnumEnd; ++Enum) { |
| 5386 | CanQualType CanonType = S.Context.getCanonicalType(*Enum); |
| 5387 | |
Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 5388 | // Don't add the same builtin candidate twice, or if a user defined |
| 5389 | // candidate exists. |
| 5390 | if (!AddedTypes.insert(CanonType) || |
| 5391 | UserDefinedBinaryOperators.count(std::make_pair(CanonType, |
| 5392 | CanonType))) |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5393 | continue; |
| 5394 | |
| 5395 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 5396 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 5397 | CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5398 | } |
| 5399 | } |
| 5400 | } |
| 5401 | |
| 5402 | // C++ [over.built]p13: |
| 5403 | // |
| 5404 | // For every cv-qualified or cv-unqualified object type T |
| 5405 | // there exist candidate operator functions of the form |
| 5406 | // |
| 5407 | // T* operator+(T*, ptrdiff_t); |
| 5408 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 5409 | // T* operator-(T*, ptrdiff_t); |
| 5410 | // T* operator+(ptrdiff_t, T*); |
| 5411 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 5412 | // |
| 5413 | // C++ [over.built]p14: |
| 5414 | // |
| 5415 | // For every T, where T is a pointer to object type, there |
| 5416 | // exist candidate operator functions of the form |
| 5417 | // |
| 5418 | // ptrdiff_t operator-(T, T); |
| 5419 | void addBinaryPlusOrMinusPointerOverloads(OverloadedOperatorKind Op) { |
| 5420 | /// Set of (canonical) types that we've already handled. |
| 5421 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5422 | |
| 5423 | for (int Arg = 0; Arg < 2; ++Arg) { |
| 5424 | QualType AsymetricParamTypes[2] = { |
| 5425 | S.Context.getPointerDiffType(), |
| 5426 | S.Context.getPointerDiffType(), |
| 5427 | }; |
| 5428 | for (BuiltinCandidateTypeSet::iterator |
| 5429 | Ptr = CandidateTypes[Arg].pointer_begin(), |
| 5430 | PtrEnd = CandidateTypes[Arg].pointer_end(); |
| 5431 | Ptr != PtrEnd; ++Ptr) { |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5432 | QualType PointeeTy = (*Ptr)->getPointeeType(); |
| 5433 | if (!PointeeTy->isObjectType()) |
| 5434 | continue; |
| 5435 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5436 | AsymetricParamTypes[Arg] = *Ptr; |
| 5437 | if (Arg == 0 || Op == OO_Plus) { |
| 5438 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 5439 | // T* operator+(ptrdiff_t, T*); |
| 5440 | S.AddBuiltinCandidate(*Ptr, AsymetricParamTypes, Args, 2, |
| 5441 | CandidateSet); |
| 5442 | } |
| 5443 | if (Op == OO_Minus) { |
| 5444 | // ptrdiff_t operator-(T, T); |
| 5445 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 5446 | continue; |
| 5447 | |
| 5448 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5449 | S.AddBuiltinCandidate(S.Context.getPointerDiffType(), ParamTypes, |
| 5450 | Args, 2, CandidateSet); |
| 5451 | } |
| 5452 | } |
| 5453 | } |
| 5454 | } |
| 5455 | |
| 5456 | // C++ [over.built]p12: |
| 5457 | // |
| 5458 | // For every pair of promoted arithmetic types L and R, there |
| 5459 | // exist candidate operator functions of the form |
| 5460 | // |
| 5461 | // LR operator*(L, R); |
| 5462 | // LR operator/(L, R); |
| 5463 | // LR operator+(L, R); |
| 5464 | // LR operator-(L, R); |
| 5465 | // bool operator<(L, R); |
| 5466 | // bool operator>(L, R); |
| 5467 | // bool operator<=(L, R); |
| 5468 | // bool operator>=(L, R); |
| 5469 | // bool operator==(L, R); |
| 5470 | // bool operator!=(L, R); |
| 5471 | // |
| 5472 | // where LR is the result of the usual arithmetic conversions |
| 5473 | // between types L and R. |
| 5474 | // |
| 5475 | // C++ [over.built]p24: |
| 5476 | // |
| 5477 | // For every pair of promoted arithmetic types L and R, there exist |
| 5478 | // candidate operator functions of the form |
| 5479 | // |
| 5480 | // LR operator?(bool, L, R); |
| 5481 | // |
| 5482 | // where LR is the result of the usual arithmetic conversions |
| 5483 | // between types L and R. |
| 5484 | // Our candidates ignore the first parameter. |
| 5485 | void addGenericBinaryArithmeticOverloads(bool isComparison) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5486 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5487 | return; |
| 5488 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5489 | for (unsigned Left = FirstPromotedArithmeticType; |
| 5490 | Left < LastPromotedArithmeticType; ++Left) { |
| 5491 | for (unsigned Right = FirstPromotedArithmeticType; |
| 5492 | Right < LastPromotedArithmeticType; ++Right) { |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5493 | QualType LandR[2] = { getArithmeticType(Left), |
| 5494 | getArithmeticType(Right) }; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5495 | QualType Result = |
| 5496 | isComparison ? S.Context.BoolTy |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5497 | : getUsualArithmeticConversions(Left, Right); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5498 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 5499 | } |
| 5500 | } |
| 5501 | |
| 5502 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 5503 | // conditional operator for vector types. |
| 5504 | for (BuiltinCandidateTypeSet::iterator |
| 5505 | Vec1 = CandidateTypes[0].vector_begin(), |
| 5506 | Vec1End = CandidateTypes[0].vector_end(); |
| 5507 | Vec1 != Vec1End; ++Vec1) { |
| 5508 | for (BuiltinCandidateTypeSet::iterator |
| 5509 | Vec2 = CandidateTypes[1].vector_begin(), |
| 5510 | Vec2End = CandidateTypes[1].vector_end(); |
| 5511 | Vec2 != Vec2End; ++Vec2) { |
| 5512 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 5513 | QualType Result = S.Context.BoolTy; |
| 5514 | if (!isComparison) { |
| 5515 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 5516 | Result = *Vec1; |
| 5517 | else |
| 5518 | Result = *Vec2; |
| 5519 | } |
| 5520 | |
| 5521 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 5522 | } |
| 5523 | } |
| 5524 | } |
| 5525 | |
| 5526 | // C++ [over.built]p17: |
| 5527 | // |
| 5528 | // For every pair of promoted integral types L and R, there |
| 5529 | // exist candidate operator functions of the form |
| 5530 | // |
| 5531 | // LR operator%(L, R); |
| 5532 | // LR operator&(L, R); |
| 5533 | // LR operator^(L, R); |
| 5534 | // LR operator|(L, R); |
| 5535 | // L operator<<(L, R); |
| 5536 | // L operator>>(L, R); |
| 5537 | // |
| 5538 | // where LR is the result of the usual arithmetic conversions |
| 5539 | // between types L and R. |
| 5540 | void addBinaryBitwiseArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5541 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5542 | return; |
| 5543 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5544 | for (unsigned Left = FirstPromotedIntegralType; |
| 5545 | Left < LastPromotedIntegralType; ++Left) { |
| 5546 | for (unsigned Right = FirstPromotedIntegralType; |
| 5547 | Right < LastPromotedIntegralType; ++Right) { |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5548 | QualType LandR[2] = { getArithmeticType(Left), |
| 5549 | getArithmeticType(Right) }; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5550 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 5551 | ? LandR[0] |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5552 | : getUsualArithmeticConversions(Left, Right); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5553 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 5554 | } |
| 5555 | } |
| 5556 | } |
| 5557 | |
| 5558 | // C++ [over.built]p20: |
| 5559 | // |
| 5560 | // For every pair (T, VQ), where T is an enumeration or |
| 5561 | // pointer to member type and VQ is either volatile or |
| 5562 | // empty, there exist candidate operator functions of the form |
| 5563 | // |
| 5564 | // VQ T& operator=(VQ T&, T); |
| 5565 | void addAssignmentMemberPointerOrEnumeralOverloads() { |
| 5566 | /// Set of (canonical) types that we've already handled. |
| 5567 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5568 | |
| 5569 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 5570 | for (BuiltinCandidateTypeSet::iterator |
| 5571 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 5572 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 5573 | Enum != EnumEnd; ++Enum) { |
| 5574 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 5575 | continue; |
| 5576 | |
| 5577 | AddBuiltinAssignmentOperatorCandidates(S, *Enum, Args, 2, |
| 5578 | CandidateSet); |
| 5579 | } |
| 5580 | |
| 5581 | for (BuiltinCandidateTypeSet::iterator |
| 5582 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 5583 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 5584 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 5585 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 5586 | continue; |
| 5587 | |
| 5588 | AddBuiltinAssignmentOperatorCandidates(S, *MemPtr, Args, 2, |
| 5589 | CandidateSet); |
| 5590 | } |
| 5591 | } |
| 5592 | } |
| 5593 | |
| 5594 | // C++ [over.built]p19: |
| 5595 | // |
| 5596 | // For every pair (T, VQ), where T is any type and VQ is either |
| 5597 | // volatile or empty, there exist candidate operator functions |
| 5598 | // of the form |
| 5599 | // |
| 5600 | // T*VQ& operator=(T*VQ&, T*); |
| 5601 | // |
| 5602 | // C++ [over.built]p21: |
| 5603 | // |
| 5604 | // For every pair (T, VQ), where T is a cv-qualified or |
| 5605 | // cv-unqualified object type and VQ is either volatile or |
| 5606 | // empty, there exist candidate operator functions of the form |
| 5607 | // |
| 5608 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 5609 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 5610 | void addAssignmentPointerOverloads(bool isEqualOp) { |
| 5611 | /// Set of (canonical) types that we've already handled. |
| 5612 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5613 | |
| 5614 | for (BuiltinCandidateTypeSet::iterator |
| 5615 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5616 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5617 | Ptr != PtrEnd; ++Ptr) { |
| 5618 | // If this is operator=, keep track of the builtin candidates we added. |
| 5619 | if (isEqualOp) |
| 5620 | AddedTypes.insert(S.Context.getCanonicalType(*Ptr)); |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5621 | else if (!(*Ptr)->getPointeeType()->isObjectType()) |
| 5622 | continue; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5623 | |
| 5624 | // non-volatile version |
| 5625 | QualType ParamTypes[2] = { |
| 5626 | S.Context.getLValueReferenceType(*Ptr), |
| 5627 | isEqualOp ? *Ptr : S.Context.getPointerDiffType(), |
| 5628 | }; |
| 5629 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5630 | /*IsAssigmentOperator=*/ isEqualOp); |
| 5631 | |
| 5632 | if (!S.Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 5633 | VisibleTypeConversionsQuals.hasVolatile()) { |
| 5634 | // volatile version |
| 5635 | ParamTypes[0] = |
| 5636 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
| 5637 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5638 | /*IsAssigmentOperator=*/isEqualOp); |
| 5639 | } |
| 5640 | } |
| 5641 | |
| 5642 | if (isEqualOp) { |
| 5643 | for (BuiltinCandidateTypeSet::iterator |
| 5644 | Ptr = CandidateTypes[1].pointer_begin(), |
| 5645 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 5646 | Ptr != PtrEnd; ++Ptr) { |
| 5647 | // Make sure we don't add the same candidate twice. |
| 5648 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 5649 | continue; |
| 5650 | |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 5651 | QualType ParamTypes[2] = { |
| 5652 | S.Context.getLValueReferenceType(*Ptr), |
| 5653 | *Ptr, |
| 5654 | }; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5655 | |
| 5656 | // non-volatile version |
| 5657 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5658 | /*IsAssigmentOperator=*/true); |
| 5659 | |
| 5660 | if (!S.Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 5661 | VisibleTypeConversionsQuals.hasVolatile()) { |
| 5662 | // volatile version |
| 5663 | ParamTypes[0] = |
| 5664 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 5665 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 5666 | CandidateSet, /*IsAssigmentOperator=*/true); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5667 | } |
| 5668 | } |
| 5669 | } |
| 5670 | } |
| 5671 | |
| 5672 | // C++ [over.built]p18: |
| 5673 | // |
| 5674 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 5675 | // VQ is either volatile or empty, and R is a promoted |
| 5676 | // arithmetic type, there exist candidate operator functions of |
| 5677 | // the form |
| 5678 | // |
| 5679 | // VQ L& operator=(VQ L&, R); |
| 5680 | // VQ L& operator*=(VQ L&, R); |
| 5681 | // VQ L& operator/=(VQ L&, R); |
| 5682 | // VQ L& operator+=(VQ L&, R); |
| 5683 | // VQ L& operator-=(VQ L&, R); |
| 5684 | void addAssignmentArithmeticOverloads(bool isEqualOp) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5685 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5686 | return; |
| 5687 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5688 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 5689 | for (unsigned Right = FirstPromotedArithmeticType; |
| 5690 | Right < LastPromotedArithmeticType; ++Right) { |
| 5691 | QualType ParamTypes[2]; |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5692 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5693 | |
| 5694 | // Add this built-in operator as a candidate (VQ is empty). |
| 5695 | ParamTypes[0] = |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5696 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5697 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5698 | /*IsAssigmentOperator=*/isEqualOp); |
| 5699 | |
| 5700 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 5701 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 5702 | ParamTypes[0] = |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5703 | S.Context.getVolatileType(getArithmeticType(Left)); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5704 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 5705 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 5706 | CandidateSet, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5707 | /*IsAssigmentOperator=*/isEqualOp); |
| 5708 | } |
| 5709 | } |
| 5710 | } |
| 5711 | |
| 5712 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 5713 | for (BuiltinCandidateTypeSet::iterator |
| 5714 | Vec1 = CandidateTypes[0].vector_begin(), |
| 5715 | Vec1End = CandidateTypes[0].vector_end(); |
| 5716 | Vec1 != Vec1End; ++Vec1) { |
| 5717 | for (BuiltinCandidateTypeSet::iterator |
| 5718 | Vec2 = CandidateTypes[1].vector_begin(), |
| 5719 | Vec2End = CandidateTypes[1].vector_end(); |
| 5720 | Vec2 != Vec2End; ++Vec2) { |
| 5721 | QualType ParamTypes[2]; |
| 5722 | ParamTypes[1] = *Vec2; |
| 5723 | // Add this built-in operator as a candidate (VQ is empty). |
| 5724 | ParamTypes[0] = S.Context.getLValueReferenceType(*Vec1); |
| 5725 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5726 | /*IsAssigmentOperator=*/isEqualOp); |
| 5727 | |
| 5728 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 5729 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 5730 | ParamTypes[0] = S.Context.getVolatileType(*Vec1); |
| 5731 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 5732 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 5733 | CandidateSet, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5734 | /*IsAssigmentOperator=*/isEqualOp); |
| 5735 | } |
| 5736 | } |
| 5737 | } |
| 5738 | } |
| 5739 | |
| 5740 | // C++ [over.built]p22: |
| 5741 | // |
| 5742 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 5743 | // is either volatile or empty, and R is a promoted integral |
| 5744 | // type, there exist candidate operator functions of the form |
| 5745 | // |
| 5746 | // VQ L& operator%=(VQ L&, R); |
| 5747 | // VQ L& operator<<=(VQ L&, R); |
| 5748 | // VQ L& operator>>=(VQ L&, R); |
| 5749 | // VQ L& operator&=(VQ L&, R); |
| 5750 | // VQ L& operator^=(VQ L&, R); |
| 5751 | // VQ L& operator|=(VQ L&, R); |
| 5752 | void addAssignmentIntegralOverloads() { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5753 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5754 | return; |
| 5755 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5756 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 5757 | for (unsigned Right = FirstPromotedIntegralType; |
| 5758 | Right < LastPromotedIntegralType; ++Right) { |
| 5759 | QualType ParamTypes[2]; |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5760 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5761 | |
| 5762 | // Add this built-in operator as a candidate (VQ is empty). |
| 5763 | ParamTypes[0] = |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5764 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5765 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 5766 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 5767 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5768 | ParamTypes[0] = getArithmeticType(Left); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5769 | ParamTypes[0] = S.Context.getVolatileType(ParamTypes[0]); |
| 5770 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
| 5771 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 5772 | CandidateSet); |
| 5773 | } |
| 5774 | } |
| 5775 | } |
| 5776 | } |
| 5777 | |
| 5778 | // C++ [over.operator]p23: |
| 5779 | // |
| 5780 | // There also exist candidate operator functions of the form |
| 5781 | // |
| 5782 | // bool operator!(bool); |
| 5783 | // bool operator&&(bool, bool); |
| 5784 | // bool operator||(bool, bool); |
| 5785 | void addExclaimOverload() { |
| 5786 | QualType ParamTy = S.Context.BoolTy; |
| 5787 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 5788 | /*IsAssignmentOperator=*/false, |
| 5789 | /*NumContextualBoolArguments=*/1); |
| 5790 | } |
| 5791 | void addAmpAmpOrPipePipeOverload() { |
| 5792 | QualType ParamTypes[2] = { S.Context.BoolTy, S.Context.BoolTy }; |
| 5793 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 5794 | /*IsAssignmentOperator=*/false, |
| 5795 | /*NumContextualBoolArguments=*/2); |
| 5796 | } |
| 5797 | |
| 5798 | // C++ [over.built]p13: |
| 5799 | // |
| 5800 | // For every cv-qualified or cv-unqualified object type T there |
| 5801 | // exist candidate operator functions of the form |
| 5802 | // |
| 5803 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 5804 | // T& operator[](T*, ptrdiff_t); |
| 5805 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 5806 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 5807 | // T& operator[](ptrdiff_t, T*); |
| 5808 | void addSubscriptOverloads() { |
| 5809 | for (BuiltinCandidateTypeSet::iterator |
| 5810 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5811 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5812 | Ptr != PtrEnd; ++Ptr) { |
| 5813 | QualType ParamTypes[2] = { *Ptr, S.Context.getPointerDiffType() }; |
| 5814 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5815 | if (!PointeeType->isObjectType()) |
| 5816 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5817 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5818 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 5819 | |
| 5820 | // T& operator[](T*, ptrdiff_t) |
| 5821 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 5822 | } |
| 5823 | |
| 5824 | for (BuiltinCandidateTypeSet::iterator |
| 5825 | Ptr = CandidateTypes[1].pointer_begin(), |
| 5826 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 5827 | Ptr != PtrEnd; ++Ptr) { |
| 5828 | QualType ParamTypes[2] = { S.Context.getPointerDiffType(), *Ptr }; |
| 5829 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5830 | if (!PointeeType->isObjectType()) |
| 5831 | continue; |
| 5832 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5833 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 5834 | |
| 5835 | // T& operator[](ptrdiff_t, T*) |
| 5836 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 5837 | } |
| 5838 | } |
| 5839 | |
| 5840 | // C++ [over.built]p11: |
| 5841 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 5842 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 5843 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 5844 | // there exist candidate operator functions of the form |
| 5845 | // |
| 5846 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 5847 | // |
| 5848 | // where CV12 is the union of CV1 and CV2. |
| 5849 | void addArrowStarOverloads() { |
| 5850 | for (BuiltinCandidateTypeSet::iterator |
| 5851 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5852 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5853 | Ptr != PtrEnd; ++Ptr) { |
| 5854 | QualType C1Ty = (*Ptr); |
| 5855 | QualType C1; |
| 5856 | QualifierCollector Q1; |
| 5857 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
| 5858 | if (!isa<RecordType>(C1)) |
| 5859 | continue; |
| 5860 | // heuristic to reduce number of builtin candidates in the set. |
| 5861 | // Add volatile/restrict version only if there are conversions to a |
| 5862 | // volatile/restrict type. |
| 5863 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 5864 | continue; |
| 5865 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 5866 | continue; |
| 5867 | for (BuiltinCandidateTypeSet::iterator |
| 5868 | MemPtr = CandidateTypes[1].member_pointer_begin(), |
| 5869 | MemPtrEnd = CandidateTypes[1].member_pointer_end(); |
| 5870 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 5871 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 5872 | QualType C2 = QualType(mptr->getClass(), 0); |
| 5873 | C2 = C2.getUnqualifiedType(); |
| 5874 | if (C1 != C2 && !S.IsDerivedFrom(C1, C2)) |
| 5875 | break; |
| 5876 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 5877 | // build CV12 T& |
| 5878 | QualType T = mptr->getPointeeType(); |
| 5879 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 5880 | T.isVolatileQualified()) |
| 5881 | continue; |
| 5882 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 5883 | T.isRestrictQualified()) |
| 5884 | continue; |
| 5885 | T = Q1.apply(S.Context, T); |
| 5886 | QualType ResultTy = S.Context.getLValueReferenceType(T); |
| 5887 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 5888 | } |
| 5889 | } |
| 5890 | } |
| 5891 | |
| 5892 | // Note that we don't consider the first argument, since it has been |
| 5893 | // contextually converted to bool long ago. The candidates below are |
| 5894 | // therefore added as binary. |
| 5895 | // |
| 5896 | // C++ [over.built]p25: |
| 5897 | // For every type T, where T is a pointer, pointer-to-member, or scoped |
| 5898 | // enumeration type, there exist candidate operator functions of the form |
| 5899 | // |
| 5900 | // T operator?(bool, T, T); |
| 5901 | // |
| 5902 | void addConditionalOperatorOverloads() { |
| 5903 | /// Set of (canonical) types that we've already handled. |
| 5904 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5905 | |
| 5906 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 5907 | for (BuiltinCandidateTypeSet::iterator |
| 5908 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 5909 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 5910 | Ptr != PtrEnd; ++Ptr) { |
| 5911 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 5912 | continue; |
| 5913 | |
| 5914 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5915 | S.AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 5916 | } |
| 5917 | |
| 5918 | for (BuiltinCandidateTypeSet::iterator |
| 5919 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 5920 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 5921 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 5922 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 5923 | continue; |
| 5924 | |
| 5925 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 5926 | S.AddBuiltinCandidate(*MemPtr, ParamTypes, Args, 2, CandidateSet); |
| 5927 | } |
| 5928 | |
| 5929 | if (S.getLangOptions().CPlusPlus0x) { |
| 5930 | for (BuiltinCandidateTypeSet::iterator |
| 5931 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 5932 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 5933 | Enum != EnumEnd; ++Enum) { |
| 5934 | if (!(*Enum)->getAs<EnumType>()->getDecl()->isScoped()) |
| 5935 | continue; |
| 5936 | |
| 5937 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 5938 | continue; |
| 5939 | |
| 5940 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 5941 | S.AddBuiltinCandidate(*Enum, ParamTypes, Args, 2, CandidateSet); |
| 5942 | } |
| 5943 | } |
| 5944 | } |
| 5945 | } |
| 5946 | }; |
| 5947 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5948 | } // end anonymous namespace |
| 5949 | |
| 5950 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 5951 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 5952 | /// on the operator @p Op and the arguments given. For example, if the |
| 5953 | /// operator is a binary '+', this routine might add "int |
| 5954 | /// operator+(int, int)" to cover integer addition. |
| 5955 | void |
| 5956 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 5957 | SourceLocation OpLoc, |
| 5958 | Expr **Args, unsigned NumArgs, |
| 5959 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5960 | // Find all of the types that the arguments can convert to, but only |
| 5961 | // if the operator we're looking at has built-in operator candidates |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5962 | // that make use of these types. Also record whether we encounter non-record |
| 5963 | // candidate types or either arithmetic or enumeral candidate types. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5964 | Qualifiers VisibleTypeConversionsQuals; |
| 5965 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 5966 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 5967 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5968 | |
| 5969 | bool HasNonRecordCandidateType = false; |
| 5970 | bool HasArithmeticOrEnumeralCandidateType = false; |
Douglas Gregor | b37c9af | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 5971 | llvm::SmallVector<BuiltinCandidateTypeSet, 2> CandidateTypes; |
| 5972 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 5973 | CandidateTypes.push_back(BuiltinCandidateTypeSet(*this)); |
| 5974 | CandidateTypes[ArgIdx].AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 5975 | OpLoc, |
| 5976 | true, |
| 5977 | (Op == OO_Exclaim || |
| 5978 | Op == OO_AmpAmp || |
| 5979 | Op == OO_PipePipe), |
| 5980 | VisibleTypeConversionsQuals); |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5981 | HasNonRecordCandidateType = HasNonRecordCandidateType || |
| 5982 | CandidateTypes[ArgIdx].hasNonRecordTypes(); |
| 5983 | HasArithmeticOrEnumeralCandidateType = |
| 5984 | HasArithmeticOrEnumeralCandidateType || |
| 5985 | CandidateTypes[ArgIdx].hasArithmeticOrEnumeralTypes(); |
Douglas Gregor | b37c9af | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 5986 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5987 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5988 | // Exit early when no non-record types have been added to the candidate set |
| 5989 | // for any of the arguments to the operator. |
| 5990 | if (!HasNonRecordCandidateType) |
| 5991 | return; |
| 5992 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5993 | // Setup an object to manage the common state for building overloads. |
| 5994 | BuiltinOperatorOverloadBuilder OpBuilder(*this, Args, NumArgs, |
| 5995 | VisibleTypeConversionsQuals, |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5996 | HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5997 | CandidateTypes, CandidateSet); |
| 5998 | |
| 5999 | // Dispatch over the operation to add in only those overloads which apply. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6000 | switch (Op) { |
| 6001 | case OO_None: |
| 6002 | case NUM_OVERLOADED_OPERATORS: |
| 6003 | assert(false && "Expected an overloaded operator"); |
| 6004 | break; |
| 6005 | |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6006 | case OO_New: |
| 6007 | case OO_Delete: |
| 6008 | case OO_Array_New: |
| 6009 | case OO_Array_Delete: |
| 6010 | case OO_Call: |
| 6011 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
| 6012 | break; |
| 6013 | |
| 6014 | case OO_Comma: |
| 6015 | case OO_Arrow: |
| 6016 | // C++ [over.match.oper]p3: |
| 6017 | // -- For the operator ',', the unary operator '&', or the |
| 6018 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6019 | break; |
| 6020 | |
| 6021 | case OO_Plus: // '+' is either unary or binary |
Chandler Carruth | 9694b9c | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 6022 | if (NumArgs == 1) |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6023 | OpBuilder.addUnaryPlusPointerOverloads(); |
Chandler Carruth | 9694b9c | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 6024 | // Fall through. |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6025 | |
| 6026 | case OO_Minus: // '-' is either unary or binary |
Chandler Carruth | f980244 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 6027 | if (NumArgs == 1) { |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6028 | OpBuilder.addUnaryPlusOrMinusArithmeticOverloads(); |
Chandler Carruth | f980244 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 6029 | } else { |
| 6030 | OpBuilder.addBinaryPlusOrMinusPointerOverloads(Op); |
| 6031 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 6032 | } |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6033 | break; |
| 6034 | |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6035 | case OO_Star: // '*' is either unary or binary |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6036 | if (NumArgs == 1) |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6037 | OpBuilder.addUnaryStarPointerOverloads(); |
| 6038 | else |
| 6039 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 6040 | break; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6041 | |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6042 | case OO_Slash: |
| 6043 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
Chandler Carruth | 9de23cd | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 6044 | break; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6045 | |
| 6046 | case OO_PlusPlus: |
| 6047 | case OO_MinusMinus: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6048 | OpBuilder.addPlusPlusMinusMinusArithmeticOverloads(Op); |
| 6049 | OpBuilder.addPlusPlusMinusMinusPointerOverloads(); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6050 | break; |
| 6051 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6052 | case OO_EqualEqual: |
| 6053 | case OO_ExclaimEqual: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6054 | OpBuilder.addEqualEqualOrNotEqualMemberPointerOverloads(); |
Chandler Carruth | 0375e95 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 6055 | // Fall through. |
Chandler Carruth | 9de23cd | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 6056 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6057 | case OO_Less: |
| 6058 | case OO_Greater: |
| 6059 | case OO_LessEqual: |
| 6060 | case OO_GreaterEqual: |
Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6061 | OpBuilder.addRelationalPointerOrEnumeralOverloads(); |
Chandler Carruth | 0375e95 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 6062 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/true); |
| 6063 | break; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6064 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6065 | case OO_Percent: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6066 | case OO_Caret: |
| 6067 | case OO_Pipe: |
| 6068 | case OO_LessLess: |
| 6069 | case OO_GreaterGreater: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6070 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6071 | break; |
| 6072 | |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6073 | case OO_Amp: // '&' is either unary or binary |
| 6074 | if (NumArgs == 1) |
| 6075 | // C++ [over.match.oper]p3: |
| 6076 | // -- For the operator ',', the unary operator '&', or the |
| 6077 | // operator '->', the built-in candidates set is empty. |
| 6078 | break; |
| 6079 | |
| 6080 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
| 6081 | break; |
| 6082 | |
| 6083 | case OO_Tilde: |
| 6084 | OpBuilder.addUnaryTildePromotedIntegralOverloads(); |
| 6085 | break; |
| 6086 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6087 | case OO_Equal: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6088 | OpBuilder.addAssignmentMemberPointerOrEnumeralOverloads(); |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6089 | // Fall through. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6090 | |
| 6091 | case OO_PlusEqual: |
| 6092 | case OO_MinusEqual: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6093 | OpBuilder.addAssignmentPointerOverloads(Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6094 | // Fall through. |
| 6095 | |
| 6096 | case OO_StarEqual: |
| 6097 | case OO_SlashEqual: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6098 | OpBuilder.addAssignmentArithmeticOverloads(Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6099 | break; |
| 6100 | |
| 6101 | case OO_PercentEqual: |
| 6102 | case OO_LessLessEqual: |
| 6103 | case OO_GreaterGreaterEqual: |
| 6104 | case OO_AmpEqual: |
| 6105 | case OO_CaretEqual: |
| 6106 | case OO_PipeEqual: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6107 | OpBuilder.addAssignmentIntegralOverloads(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6108 | break; |
| 6109 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6110 | case OO_Exclaim: |
| 6111 | OpBuilder.addExclaimOverload(); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6112 | break; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6113 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6114 | case OO_AmpAmp: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6115 | case OO_PipePipe: |
| 6116 | OpBuilder.addAmpAmpOrPipePipeOverload(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6117 | break; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6118 | |
| 6119 | case OO_Subscript: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6120 | OpBuilder.addSubscriptOverloads(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6121 | break; |
| 6122 | |
| 6123 | case OO_ArrowStar: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6124 | OpBuilder.addArrowStarOverloads(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6125 | break; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 6126 | |
| 6127 | case OO_Conditional: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6128 | OpBuilder.addConditionalOperatorOverloads(); |
Chandler Carruth | f980244 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 6129 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 6130 | break; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6131 | } |
| 6132 | } |
| 6133 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6134 | /// \brief Add function candidates found via argument-dependent lookup |
| 6135 | /// to the set of overloading candidates. |
| 6136 | /// |
| 6137 | /// This routine performs argument-dependent name lookup based on the |
| 6138 | /// given function name (which may also be an operator name) and adds |
| 6139 | /// all of the overload candidates found by ADL to the overload |
| 6140 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6141 | void |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6142 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6143 | bool Operator, |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6144 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 6145 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6146 | OverloadCandidateSet& CandidateSet, |
| 6147 | bool PartialOverloading) { |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6148 | ADLResult Fns; |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6149 | |
John McCall | 91f61fc | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 6150 | // FIXME: This approach for uniquing ADL results (and removing |
| 6151 | // redundant candidates from the set) relies on pointer-equality, |
| 6152 | // which means we need to key off the canonical decl. However, |
| 6153 | // always going back to the canonical decl might not get us the |
| 6154 | // right set of default arguments. What default arguments are |
| 6155 | // we supposed to consider on ADL candidates, anyway? |
| 6156 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6157 | // FIXME: Pass in the explicit template arguments? |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6158 | ArgumentDependentLookup(Name, Operator, Args, NumArgs, Fns); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6159 | |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6160 | // Erase all of the candidates we already knew about. |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6161 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 6162 | CandEnd = CandidateSet.end(); |
| 6163 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6164 | if (Cand->Function) { |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6165 | Fns.erase(Cand->Function); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6166 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6167 | Fns.erase(FunTmpl); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6168 | } |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6169 | |
| 6170 | // For each of the ADL candidates we found, add it to the overload |
| 6171 | // set. |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6172 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6173 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6174 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6175 | if (ExplicitTemplateArgs) |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6176 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6177 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6178 | AddOverloadCandidate(FD, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | b05275a | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 6179 | false, PartialOverloading); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6180 | } else |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6181 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6182 | FoundDecl, ExplicitTemplateArgs, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 6183 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6184 | } |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6185 | } |
| 6186 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6187 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 6188 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6189 | bool |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6190 | isBetterOverloadCandidate(Sema &S, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 6191 | const OverloadCandidate &Cand1, |
| 6192 | const OverloadCandidate &Cand2, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6193 | SourceLocation Loc, |
| 6194 | bool UserDefinedConversion) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6195 | // Define viable functions to be better candidates than non-viable |
| 6196 | // functions. |
| 6197 | if (!Cand2.Viable) |
| 6198 | return Cand1.Viable; |
| 6199 | else if (!Cand1.Viable) |
| 6200 | return false; |
| 6201 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6202 | // C++ [over.match.best]p1: |
| 6203 | // |
| 6204 | // -- if F is a static member function, ICS1(F) is defined such |
| 6205 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 6206 | // any function G, and, symmetrically, ICS1(G) is neither |
| 6207 | // better nor worse than ICS1(F). |
| 6208 | unsigned StartArg = 0; |
| 6209 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 6210 | StartArg = 1; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6211 | |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6212 | // C++ [over.match.best]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6213 | // A viable function F1 is defined to be a better function than another |
| 6214 | // 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] | 6215 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6216 | unsigned NumArgs = Cand1.Conversions.size(); |
| 6217 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 6218 | bool HasBetterConversion = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6219 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6220 | switch (CompareImplicitConversionSequences(S, |
| 6221 | Cand1.Conversions[ArgIdx], |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6222 | Cand2.Conversions[ArgIdx])) { |
| 6223 | case ImplicitConversionSequence::Better: |
| 6224 | // Cand1 has a better conversion sequence. |
| 6225 | HasBetterConversion = true; |
| 6226 | break; |
| 6227 | |
| 6228 | case ImplicitConversionSequence::Worse: |
| 6229 | // Cand1 can't be better than Cand2. |
| 6230 | return false; |
| 6231 | |
| 6232 | case ImplicitConversionSequence::Indistinguishable: |
| 6233 | // Do nothing. |
| 6234 | break; |
| 6235 | } |
| 6236 | } |
| 6237 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6238 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6239 | // ICSj(F2), or, if not that, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6240 | if (HasBetterConversion) |
| 6241 | return true; |
| 6242 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6243 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6244 | // specialization, or, if not that, |
Douglas Gregor | ce21919b | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 6245 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6246 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 6247 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6248 | |
| 6249 | // -- F1 and F2 are function template specializations, and the function |
| 6250 | // template for F1 is more specialized than the template for F2 |
| 6251 | // 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] | 6252 | // if not that, |
Douglas Gregor | 55137cb | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 6253 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6254 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6255 | if (FunctionTemplateDecl *BetterTemplate |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6256 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 6257 | Cand2.Function->getPrimaryTemplate(), |
| 6258 | Loc, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6259 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 6260 | : TPOC_Call, |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6261 | Cand1.ExplicitCallArguments)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6262 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6263 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6264 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6265 | // -- the context is an initialization by user-defined conversion |
| 6266 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 6267 | // from the return type of F1 to the destination type (i.e., |
| 6268 | // the type of the entity being initialized) is a better |
| 6269 | // conversion sequence than the standard conversion sequence |
| 6270 | // from the return type of F2 to the destination type. |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6271 | if (UserDefinedConversion && Cand1.Function && Cand2.Function && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6272 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6273 | isa<CXXConversionDecl>(Cand2.Function)) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6274 | switch (CompareStandardConversionSequences(S, |
| 6275 | Cand1.FinalConversion, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6276 | Cand2.FinalConversion)) { |
| 6277 | case ImplicitConversionSequence::Better: |
| 6278 | // Cand1 has a better conversion sequence. |
| 6279 | return true; |
| 6280 | |
| 6281 | case ImplicitConversionSequence::Worse: |
| 6282 | // Cand1 can't be better than Cand2. |
| 6283 | return false; |
| 6284 | |
| 6285 | case ImplicitConversionSequence::Indistinguishable: |
| 6286 | // Do nothing |
| 6287 | break; |
| 6288 | } |
| 6289 | } |
| 6290 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6291 | return false; |
| 6292 | } |
| 6293 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6294 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6295 | /// within an overload candidate set. |
| 6296 | /// |
| 6297 | /// \param CandidateSet the set of candidate functions. |
| 6298 | /// |
| 6299 | /// \param Loc the location of the function name (or operator symbol) for |
| 6300 | /// which overload resolution occurs. |
| 6301 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6302 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6303 | /// function, Best points to the candidate function found. |
| 6304 | /// |
| 6305 | /// \returns The result of overload resolution. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6306 | OverloadingResult |
| 6307 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 6308 | iterator &Best, |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 6309 | bool UserDefinedConversion) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6310 | // Find the best viable function. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6311 | Best = end(); |
| 6312 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
| 6313 | if (Cand->Viable) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6314 | if (Best == end() || isBetterOverloadCandidate(S, *Cand, *Best, Loc, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6315 | UserDefinedConversion)) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6316 | Best = Cand; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6317 | } |
| 6318 | |
| 6319 | // If we didn't find any viable functions, abort. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6320 | if (Best == end()) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6321 | return OR_No_Viable_Function; |
| 6322 | |
| 6323 | // Make sure that this function is better than every other viable |
| 6324 | // function. If not, we have an ambiguity. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6325 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6326 | if (Cand->Viable && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6327 | Cand != Best && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6328 | !isBetterOverloadCandidate(S, *Best, *Cand, Loc, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6329 | UserDefinedConversion)) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6330 | Best = end(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6331 | return OR_Ambiguous; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6332 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6333 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6334 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6335 | // Best is the best viable function. |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6336 | if (Best->Function && |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 6337 | (Best->Function->isDeleted() || Best->Function->isUnavailable())) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6338 | return OR_Deleted; |
| 6339 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6340 | return OR_Success; |
| 6341 | } |
| 6342 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6343 | namespace { |
| 6344 | |
| 6345 | enum OverloadCandidateKind { |
| 6346 | oc_function, |
| 6347 | oc_method, |
| 6348 | oc_constructor, |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6349 | oc_function_template, |
| 6350 | oc_method_template, |
| 6351 | oc_constructor_template, |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6352 | oc_implicit_default_constructor, |
| 6353 | oc_implicit_copy_constructor, |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6354 | oc_implicit_copy_assignment, |
| 6355 | oc_implicit_inherited_constructor |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6356 | }; |
| 6357 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6358 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 6359 | FunctionDecl *Fn, |
| 6360 | std::string &Description) { |
| 6361 | bool isTemplate = false; |
| 6362 | |
| 6363 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 6364 | isTemplate = true; |
| 6365 | Description = S.getTemplateArgumentBindingsText( |
| 6366 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 6367 | } |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6368 | |
| 6369 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6370 | if (!Ctor->isImplicit()) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6371 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6372 | |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6373 | if (Ctor->getInheritedConstructor()) |
| 6374 | return oc_implicit_inherited_constructor; |
| 6375 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6376 | return Ctor->isCopyConstructor() ? oc_implicit_copy_constructor |
| 6377 | : oc_implicit_default_constructor; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6378 | } |
| 6379 | |
| 6380 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 6381 | // This actually gets spelled 'candidate function' for now, but |
| 6382 | // it doesn't hurt to split it out. |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6383 | if (!Meth->isImplicit()) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6384 | return isTemplate ? oc_method_template : oc_method; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6385 | |
Douglas Gregor | ec3bec0 | 2010-09-27 22:37:28 +0000 | [diff] [blame] | 6386 | assert(Meth->isCopyAssignmentOperator() |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6387 | && "implicit method is not copy assignment operator?"); |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6388 | return oc_implicit_copy_assignment; |
| 6389 | } |
| 6390 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6391 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6392 | } |
| 6393 | |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6394 | void MaybeEmitInheritedConstructorNote(Sema &S, FunctionDecl *Fn) { |
| 6395 | const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn); |
| 6396 | if (!Ctor) return; |
| 6397 | |
| 6398 | Ctor = Ctor->getInheritedConstructor(); |
| 6399 | if (!Ctor) return; |
| 6400 | |
| 6401 | S.Diag(Ctor->getLocation(), diag::note_ovl_candidate_inherited_constructor); |
| 6402 | } |
| 6403 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6404 | } // end anonymous namespace |
| 6405 | |
| 6406 | // Notes the location of an overload candidate. |
| 6407 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn) { |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6408 | std::string FnDesc; |
| 6409 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
| 6410 | Diag(Fn->getLocation(), diag::note_ovl_candidate) |
| 6411 | << (unsigned) K << FnDesc; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6412 | MaybeEmitInheritedConstructorNote(*this, Fn); |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6413 | } |
| 6414 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6415 | //Notes the location of all overload candidates designated through |
| 6416 | // OverloadedExpr |
| 6417 | void Sema::NoteAllOverloadCandidates(Expr* OverloadedExpr) { |
| 6418 | assert(OverloadedExpr->getType() == Context.OverloadTy); |
| 6419 | |
| 6420 | OverloadExpr::FindResult Ovl = OverloadExpr::find(OverloadedExpr); |
| 6421 | OverloadExpr *OvlExpr = Ovl.Expression; |
| 6422 | |
| 6423 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 6424 | IEnd = OvlExpr->decls_end(); |
| 6425 | I != IEnd; ++I) { |
| 6426 | if (FunctionTemplateDecl *FunTmpl = |
| 6427 | dyn_cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()) ) { |
| 6428 | NoteOverloadCandidate(FunTmpl->getTemplatedDecl()); |
| 6429 | } else if (FunctionDecl *Fun |
| 6430 | = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl()) ) { |
| 6431 | NoteOverloadCandidate(Fun); |
| 6432 | } |
| 6433 | } |
| 6434 | } |
| 6435 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6436 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 6437 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 6438 | /// target types of the conversion. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6439 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
| 6440 | Sema &S, |
| 6441 | SourceLocation CaretLoc, |
| 6442 | const PartialDiagnostic &PDiag) const { |
| 6443 | S.Diag(CaretLoc, PDiag) |
| 6444 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6445 | for (AmbiguousConversionSequence::const_iterator |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6446 | I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
| 6447 | S.NoteOverloadCandidate(*I); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6448 | } |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6449 | } |
| 6450 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6451 | namespace { |
| 6452 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6453 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 6454 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 6455 | assert(Conv.isBad()); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6456 | assert(Cand->Function && "for now, candidate must be a function"); |
| 6457 | FunctionDecl *Fn = Cand->Function; |
| 6458 | |
| 6459 | // There's a conversion slot for the object argument if this is a |
| 6460 | // non-constructor method. Note that 'I' corresponds the |
| 6461 | // conversion-slot index. |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6462 | bool isObjectArgument = false; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6463 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6464 | if (I == 0) |
| 6465 | isObjectArgument = true; |
| 6466 | else |
| 6467 | I--; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6468 | } |
| 6469 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6470 | std::string FnDesc; |
| 6471 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 6472 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6473 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 6474 | QualType FromTy = Conv.Bad.getFromType(); |
| 6475 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6476 | |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 6477 | if (FromTy == S.Context.OverloadTy) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 6478 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 6479 | Expr *E = FromExpr->IgnoreParens(); |
| 6480 | if (isa<UnaryOperator>(E)) |
| 6481 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6482 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 6483 | |
| 6484 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 6485 | << (unsigned) FnKind << FnDesc |
| 6486 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6487 | << ToTy << Name << I+1; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6488 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 6489 | return; |
| 6490 | } |
| 6491 | |
John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 6492 | // Do some hand-waving analysis to see if the non-viability is due |
| 6493 | // to a qualifier mismatch. |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6494 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 6495 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 6496 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 6497 | CToTy = RT->getPointeeType(); |
| 6498 | else { |
| 6499 | // TODO: detect and diagnose the full richness of const mismatches. |
| 6500 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 6501 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 6502 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 6503 | } |
| 6504 | |
| 6505 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 6506 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
| 6507 | // It is dumb that we have to do this here. |
| 6508 | while (isa<ArrayType>(CFromTy)) |
| 6509 | CFromTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 6510 | while (isa<ArrayType>(CToTy)) |
| 6511 | CToTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 6512 | |
| 6513 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 6514 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 6515 | |
| 6516 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 6517 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 6518 | << (unsigned) FnKind << FnDesc |
| 6519 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6520 | << FromTy |
| 6521 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 6522 | << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6523 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6524 | return; |
| 6525 | } |
| 6526 | |
| 6527 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 6528 | assert(CVR && "unexpected qualifiers mismatch"); |
| 6529 | |
| 6530 | if (isObjectArgument) { |
| 6531 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 6532 | << (unsigned) FnKind << FnDesc |
| 6533 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6534 | << FromTy << (CVR - 1); |
| 6535 | } else { |
| 6536 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 6537 | << (unsigned) FnKind << FnDesc |
| 6538 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6539 | << FromTy << (CVR - 1) << I+1; |
| 6540 | } |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6541 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6542 | return; |
| 6543 | } |
| 6544 | |
John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 6545 | // Diagnose references or pointers to incomplete types differently, |
| 6546 | // since it's far from impossible that the incompleteness triggered |
| 6547 | // the failure. |
| 6548 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 6549 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 6550 | TempFromTy = PTy->getPointeeType(); |
| 6551 | if (TempFromTy->isIncompleteType()) { |
| 6552 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 6553 | << (unsigned) FnKind << FnDesc |
| 6554 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6555 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6556 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 6557 | return; |
| 6558 | } |
| 6559 | |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6560 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6561 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6562 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 6563 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 6564 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 6565 | FromPtrTy->getPointeeType()) && |
| 6566 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 6567 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6568 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6569 | FromPtrTy->getPointeeType())) |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6570 | BaseToDerivedConversion = 1; |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6571 | } |
| 6572 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 6573 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 6574 | if (const ObjCObjectPointerType *ToPtrTy |
| 6575 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 6576 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 6577 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 6578 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 6579 | FromPtrTy->getPointeeType()) && |
| 6580 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6581 | BaseToDerivedConversion = 2; |
| 6582 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
| 6583 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 6584 | !FromTy->isIncompleteType() && |
| 6585 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 6586 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) |
| 6587 | BaseToDerivedConversion = 3; |
| 6588 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6589 | |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6590 | if (BaseToDerivedConversion) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6591 | S.Diag(Fn->getLocation(), |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6592 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6593 | << (unsigned) FnKind << FnDesc |
| 6594 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6595 | << (BaseToDerivedConversion - 1) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6596 | << FromTy << ToTy << I+1; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6597 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6598 | return; |
| 6599 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6600 | |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6601 | // TODO: specialize more based on the kind of mismatch |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6602 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv) |
| 6603 | << (unsigned) FnKind << FnDesc |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6604 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
John McCall | a1709fd | 2010-01-14 00:56:20 +0000 | [diff] [blame] | 6605 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6606 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6607 | } |
| 6608 | |
| 6609 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 6610 | unsigned NumFormalArgs) { |
| 6611 | // TODO: treat calls to a missing default constructor as a special case |
| 6612 | |
| 6613 | FunctionDecl *Fn = Cand->Function; |
| 6614 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 6615 | |
| 6616 | unsigned MinParams = Fn->getMinRequiredArguments(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6617 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6618 | // at least / at most / exactly |
| 6619 | unsigned mode, modeCount; |
| 6620 | if (NumFormalArgs < MinParams) { |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 6621 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 6622 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 6623 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6624 | if (MinParams != FnTy->getNumArgs() || |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 6625 | FnTy->isVariadic() || FnTy->isTemplateVariadic()) |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6626 | mode = 0; // "at least" |
| 6627 | else |
| 6628 | mode = 2; // "exactly" |
| 6629 | modeCount = MinParams; |
| 6630 | } else { |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 6631 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 6632 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 6633 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6634 | if (MinParams != FnTy->getNumArgs()) |
| 6635 | mode = 1; // "at most" |
| 6636 | else |
| 6637 | mode = 2; // "exactly" |
| 6638 | modeCount = FnTy->getNumArgs(); |
| 6639 | } |
| 6640 | |
| 6641 | std::string Description; |
| 6642 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 6643 | |
| 6644 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6645 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 6646 | << modeCount << NumFormalArgs; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6647 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6648 | } |
| 6649 | |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6650 | /// Diagnose a failed template-argument deduction. |
| 6651 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
| 6652 | Expr **Args, unsigned NumArgs) { |
| 6653 | FunctionDecl *Fn = Cand->Function; // pattern |
| 6654 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6655 | TemplateParameter Param = Cand->DeductionFailure.getTemplateParameter(); |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6656 | NamedDecl *ParamD; |
| 6657 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 6658 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 6659 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6660 | switch (Cand->DeductionFailure.Result) { |
| 6661 | case Sema::TDK_Success: |
| 6662 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 6663 | |
| 6664 | case Sema::TDK_Incomplete: { |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6665 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| 6666 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_incomplete_deduction) |
| 6667 | << ParamD->getDeclName(); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6668 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6669 | return; |
| 6670 | } |
| 6671 | |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 6672 | case Sema::TDK_Underqualified: { |
| 6673 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 6674 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 6675 | |
| 6676 | QualType Param = Cand->DeductionFailure.getFirstArg()->getAsType(); |
| 6677 | |
| 6678 | // Param will have been canonicalized, but it should just be a |
| 6679 | // qualified version of ParamD, so move the qualifiers to that. |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6680 | QualifierCollector Qs; |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 6681 | Qs.strip(Param); |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6682 | QualType NonCanonParam = Qs.apply(S.Context, TParam->getTypeForDecl()); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 6683 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 6684 | |
| 6685 | // Arg has also been canonicalized, but there's nothing we can do |
| 6686 | // about that. It also doesn't matter as much, because it won't |
| 6687 | // have any template parameters in it (because deduction isn't |
| 6688 | // done on dependent types). |
| 6689 | QualType Arg = Cand->DeductionFailure.getSecondArg()->getAsType(); |
| 6690 | |
| 6691 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_underqualified) |
| 6692 | << ParamD->getDeclName() << Arg << NonCanonParam; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6693 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 6694 | return; |
| 6695 | } |
| 6696 | |
| 6697 | case Sema::TDK_Inconsistent: { |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6698 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6699 | int which = 0; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6700 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6701 | which = 0; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6702 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6703 | which = 1; |
| 6704 | else { |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6705 | which = 2; |
| 6706 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6707 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6708 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_inconsistent_deduction) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6709 | << which << ParamD->getDeclName() |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6710 | << *Cand->DeductionFailure.getFirstArg() |
| 6711 | << *Cand->DeductionFailure.getSecondArg(); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6712 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6713 | return; |
| 6714 | } |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 6715 | |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6716 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6717 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6718 | if (ParamD->getDeclName()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6719 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6720 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
| 6721 | << ParamD->getDeclName(); |
| 6722 | else { |
| 6723 | int index = 0; |
| 6724 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 6725 | index = TTP->getIndex(); |
| 6726 | else if (NonTypeTemplateParmDecl *NTTP |
| 6727 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 6728 | index = NTTP->getIndex(); |
| 6729 | else |
| 6730 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6731 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6732 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
| 6733 | << (index + 1); |
| 6734 | } |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6735 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6736 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6737 | |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 6738 | case Sema::TDK_TooManyArguments: |
| 6739 | case Sema::TDK_TooFewArguments: |
| 6740 | DiagnoseArityMismatch(S, Cand, NumArgs); |
| 6741 | return; |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 6742 | |
| 6743 | case Sema::TDK_InstantiationDepth: |
| 6744 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_instantiation_depth); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6745 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 6746 | return; |
| 6747 | |
| 6748 | case Sema::TDK_SubstitutionFailure: { |
| 6749 | std::string ArgString; |
| 6750 | if (TemplateArgumentList *Args |
| 6751 | = Cand->DeductionFailure.getTemplateArgumentList()) |
| 6752 | ArgString = S.getTemplateArgumentBindingsText( |
| 6753 | Fn->getDescribedFunctionTemplate()->getTemplateParameters(), |
| 6754 | *Args); |
| 6755 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_substitution_failure) |
| 6756 | << ArgString; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6757 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 6758 | return; |
| 6759 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6760 | |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6761 | // TODO: diagnose these individually, then kill off |
| 6762 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6763 | case Sema::TDK_NonDeducedMismatch: |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6764 | case Sema::TDK_FailedOverloadResolution: |
| 6765 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6766 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6767 | return; |
| 6768 | } |
| 6769 | } |
| 6770 | |
| 6771 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 6772 | /// already generated a primary error at the call site. |
| 6773 | /// |
| 6774 | /// It really does need to be a single diagnostic with its caret |
| 6775 | /// pointed at the candidate declaration. Yes, this creates some |
| 6776 | /// major challenges of technical writing. Yes, this makes pointing |
| 6777 | /// out problems with specific arguments quite awkward. It's still |
| 6778 | /// better than generating twenty screens of text for every failed |
| 6779 | /// overload. |
| 6780 | /// |
| 6781 | /// It would be great to be able to express per-candidate problems |
| 6782 | /// more richly for those diagnostic clients that cared, but we'd |
| 6783 | /// still have to be just as careful with the default diagnostics. |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6784 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| 6785 | Expr **Args, unsigned NumArgs) { |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6786 | FunctionDecl *Fn = Cand->Function; |
| 6787 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6788 | // Note deleted candidates, but only if they're viable. |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 6789 | if (Cand->Viable && (Fn->isDeleted() || Fn->isUnavailable())) { |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6790 | std::string FnDesc; |
| 6791 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6792 | |
| 6793 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6794 | << FnKind << FnDesc << Fn->isDeleted(); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6795 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6796 | return; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6797 | } |
| 6798 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6799 | // We don't really have anything else to say about viable candidates. |
| 6800 | if (Cand->Viable) { |
| 6801 | S.NoteOverloadCandidate(Fn); |
| 6802 | return; |
| 6803 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6804 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6805 | switch (Cand->FailureKind) { |
| 6806 | case ovl_fail_too_many_arguments: |
| 6807 | case ovl_fail_too_few_arguments: |
| 6808 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6809 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6810 | case ovl_fail_bad_deduction: |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6811 | return DiagnoseBadDeduction(S, Cand, Args, NumArgs); |
| 6812 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6813 | case ovl_fail_trivial_conversion: |
| 6814 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 6815 | case ovl_fail_final_conversion_not_exact: |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6816 | return S.NoteOverloadCandidate(Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6817 | |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 6818 | case ovl_fail_bad_conversion: { |
| 6819 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
| 6820 | for (unsigned N = Cand->Conversions.size(); I != N; ++I) |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6821 | if (Cand->Conversions[I].isBad()) |
| 6822 | return DiagnoseBadConversion(S, Cand, I); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6823 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6824 | // FIXME: this currently happens when we're called from SemaInit |
| 6825 | // when user-conversion overload fails. Figure out how to handle |
| 6826 | // those conditions and diagnose them well. |
| 6827 | return S.NoteOverloadCandidate(Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6828 | } |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 6829 | } |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6830 | } |
| 6831 | |
| 6832 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 6833 | // Desugar the type of the surrogate down to a function type, |
| 6834 | // retaining as many typedefs as possible while still showing |
| 6835 | // the function type (and, therefore, its parameter types). |
| 6836 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 6837 | bool isLValueReference = false; |
| 6838 | bool isRValueReference = false; |
| 6839 | bool isPointer = false; |
| 6840 | if (const LValueReferenceType *FnTypeRef = |
| 6841 | FnType->getAs<LValueReferenceType>()) { |
| 6842 | FnType = FnTypeRef->getPointeeType(); |
| 6843 | isLValueReference = true; |
| 6844 | } else if (const RValueReferenceType *FnTypeRef = |
| 6845 | FnType->getAs<RValueReferenceType>()) { |
| 6846 | FnType = FnTypeRef->getPointeeType(); |
| 6847 | isRValueReference = true; |
| 6848 | } |
| 6849 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 6850 | FnType = FnTypePtr->getPointeeType(); |
| 6851 | isPointer = true; |
| 6852 | } |
| 6853 | // Desugar down to a function type. |
| 6854 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 6855 | // Reconstruct the pointer/reference as appropriate. |
| 6856 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 6857 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 6858 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 6859 | |
| 6860 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 6861 | << FnType; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6862 | MaybeEmitInheritedConstructorNote(S, Cand->Surrogate); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6863 | } |
| 6864 | |
| 6865 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 6866 | const char *Opc, |
| 6867 | SourceLocation OpLoc, |
| 6868 | OverloadCandidate *Cand) { |
| 6869 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
| 6870 | std::string TypeStr("operator"); |
| 6871 | TypeStr += Opc; |
| 6872 | TypeStr += "("; |
| 6873 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 6874 | if (Cand->Conversions.size() == 1) { |
| 6875 | TypeStr += ")"; |
| 6876 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 6877 | } else { |
| 6878 | TypeStr += ", "; |
| 6879 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 6880 | TypeStr += ")"; |
| 6881 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 6882 | } |
| 6883 | } |
| 6884 | |
| 6885 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 6886 | OverloadCandidate *Cand) { |
| 6887 | unsigned NoOperands = Cand->Conversions.size(); |
| 6888 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 6889 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6890 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 6891 | if (!ICS.isAmbiguous()) continue; |
| 6892 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6893 | ICS.DiagnoseAmbiguousConversion(S, OpLoc, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6894 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6895 | } |
| 6896 | } |
| 6897 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 6898 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 6899 | if (Cand->Function) |
| 6900 | return Cand->Function->getLocation(); |
John McCall | 982adb5 | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 6901 | if (Cand->IsSurrogate) |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 6902 | return Cand->Surrogate->getLocation(); |
| 6903 | return SourceLocation(); |
| 6904 | } |
| 6905 | |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 6906 | struct CompareOverloadCandidatesForDisplay { |
| 6907 | Sema &S; |
| 6908 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6909 | |
| 6910 | bool operator()(const OverloadCandidate *L, |
| 6911 | const OverloadCandidate *R) { |
John McCall | 982adb5 | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 6912 | // Fast-path this check. |
| 6913 | if (L == R) return false; |
| 6914 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6915 | // Order first by viability. |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 6916 | if (L->Viable) { |
| 6917 | if (!R->Viable) return true; |
| 6918 | |
| 6919 | // TODO: introduce a tri-valued comparison for overload |
| 6920 | // candidates. Would be more worthwhile if we had a sort |
| 6921 | // that could exploit it. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6922 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation())) return true; |
| 6923 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation())) return false; |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 6924 | } else if (R->Viable) |
| 6925 | return false; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6926 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 6927 | assert(L->Viable == R->Viable); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6928 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 6929 | // Criteria by which we can sort non-viable candidates: |
| 6930 | if (!L->Viable) { |
| 6931 | // 1. Arity mismatches come after other candidates. |
| 6932 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 6933 | L->FailureKind == ovl_fail_too_few_arguments) |
| 6934 | return false; |
| 6935 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 6936 | R->FailureKind == ovl_fail_too_few_arguments) |
| 6937 | return true; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6938 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6939 | // 2. Bad conversions come first and are ordered by the number |
| 6940 | // of bad conversions and quality of good conversions. |
| 6941 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 6942 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 6943 | return true; |
| 6944 | |
| 6945 | // If there's any ordering between the defined conversions... |
| 6946 | // FIXME: this might not be transitive. |
| 6947 | assert(L->Conversions.size() == R->Conversions.size()); |
| 6948 | |
| 6949 | int leftBetter = 0; |
John McCall | 21b57fa | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 6950 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
| 6951 | for (unsigned E = L->Conversions.size(); I != E; ++I) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6952 | switch (CompareImplicitConversionSequences(S, |
| 6953 | L->Conversions[I], |
| 6954 | R->Conversions[I])) { |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6955 | case ImplicitConversionSequence::Better: |
| 6956 | leftBetter++; |
| 6957 | break; |
| 6958 | |
| 6959 | case ImplicitConversionSequence::Worse: |
| 6960 | leftBetter--; |
| 6961 | break; |
| 6962 | |
| 6963 | case ImplicitConversionSequence::Indistinguishable: |
| 6964 | break; |
| 6965 | } |
| 6966 | } |
| 6967 | if (leftBetter > 0) return true; |
| 6968 | if (leftBetter < 0) return false; |
| 6969 | |
| 6970 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 6971 | return false; |
| 6972 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 6973 | // TODO: others? |
| 6974 | } |
| 6975 | |
| 6976 | // Sort everything else by location. |
| 6977 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 6978 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 6979 | |
| 6980 | // Put candidates without locations (e.g. builtins) at the end. |
| 6981 | if (LLoc.isInvalid()) return false; |
| 6982 | if (RLoc.isInvalid()) return true; |
| 6983 | |
| 6984 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6985 | } |
| 6986 | }; |
| 6987 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6988 | /// CompleteNonViableCandidate - Normally, overload resolution only |
| 6989 | /// computes up to the first |
| 6990 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
| 6991 | Expr **Args, unsigned NumArgs) { |
| 6992 | assert(!Cand->Viable); |
| 6993 | |
| 6994 | // Don't do anything on failures other than bad conversion. |
| 6995 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 6996 | |
| 6997 | // Skip forward to the first bad conversion. |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 6998 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6999 | unsigned ConvCount = Cand->Conversions.size(); |
| 7000 | while (true) { |
| 7001 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 7002 | ConvIdx++; |
| 7003 | if (Cand->Conversions[ConvIdx - 1].isBad()) |
| 7004 | break; |
| 7005 | } |
| 7006 | |
| 7007 | if (ConvIdx == ConvCount) |
| 7008 | return; |
| 7009 | |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 7010 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 7011 | "remaining conversion is initialized?"); |
| 7012 | |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 7013 | // FIXME: this should probably be preserved from the overload |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7014 | // operation somehow. |
| 7015 | bool SuppressUserConversions = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7016 | |
| 7017 | const FunctionProtoType* Proto; |
| 7018 | unsigned ArgIdx = ConvIdx; |
| 7019 | |
| 7020 | if (Cand->IsSurrogate) { |
| 7021 | QualType ConvType |
| 7022 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 7023 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 7024 | ConvType = ConvPtrType->getPointeeType(); |
| 7025 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 7026 | ArgIdx--; |
| 7027 | } else if (Cand->Function) { |
| 7028 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 7029 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 7030 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 7031 | ArgIdx--; |
| 7032 | } else { |
| 7033 | // Builtin binary operator with a bad first conversion. |
| 7034 | assert(ConvCount <= 3); |
| 7035 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 7036 | Cand->Conversions[ConvIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7037 | = TryCopyInitialization(S, Args[ConvIdx], |
| 7038 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7039 | SuppressUserConversions, |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7040 | /*InOverloadResolution*/ true); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7041 | return; |
| 7042 | } |
| 7043 | |
| 7044 | // Fill in the rest of the conversions. |
| 7045 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 7046 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
| 7047 | if (ArgIdx < NumArgsInProto) |
| 7048 | Cand->Conversions[ConvIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7049 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7050 | SuppressUserConversions, |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7051 | /*InOverloadResolution=*/true); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7052 | else |
| 7053 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 7054 | } |
| 7055 | } |
| 7056 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7057 | } // end anonymous namespace |
| 7058 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7059 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 7060 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7061 | /// set. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7062 | void OverloadCandidateSet::NoteCandidates(Sema &S, |
| 7063 | OverloadCandidateDisplayKind OCD, |
| 7064 | Expr **Args, unsigned NumArgs, |
| 7065 | const char *Opc, |
| 7066 | SourceLocation OpLoc) { |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7067 | // Sort the candidates by viability and position. Sorting directly would |
| 7068 | // be prohibitive, so we make a set of pointers and sort those. |
| 7069 | llvm::SmallVector<OverloadCandidate*, 32> Cands; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7070 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
| 7071 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7072 | if (Cand->Viable) |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7073 | Cands.push_back(Cand); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7074 | else if (OCD == OCD_AllCandidates) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7075 | CompleteNonViableCandidate(S, Cand, Args, NumArgs); |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7076 | if (Cand->Function || Cand->IsSurrogate) |
| 7077 | Cands.push_back(Cand); |
| 7078 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 7079 | // want to list every possible builtin candidate. |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7080 | } |
| 7081 | } |
| 7082 | |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 7083 | std::sort(Cands.begin(), Cands.end(), |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7084 | CompareOverloadCandidatesForDisplay(S)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7085 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7086 | bool ReportedAmbiguousConversions = false; |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7087 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7088 | llvm::SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7089 | const Diagnostic::OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7090 | unsigned CandsShown = 0; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7091 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 7092 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 7093 | |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7094 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 7095 | // the user with. FIXME: This limit should depend on details of the |
| 7096 | // candidate list. |
| 7097 | if (CandsShown >= 4 && ShowOverloads == Diagnostic::Ovl_Best) { |
| 7098 | break; |
| 7099 | } |
| 7100 | ++CandsShown; |
| 7101 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7102 | if (Cand->Function) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7103 | NoteFunctionCandidate(S, Cand, Args, NumArgs); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7104 | else if (Cand->IsSurrogate) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7105 | NoteSurrogateCandidate(S, Cand); |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7106 | else { |
| 7107 | assert(Cand->Viable && |
| 7108 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7109 | // Generally we only see ambiguities including viable builtin |
| 7110 | // operators if overload resolution got screwed up by an |
| 7111 | // ambiguous user-defined conversion. |
| 7112 | // |
| 7113 | // FIXME: It's quite possible for different conversions to see |
| 7114 | // different ambiguities, though. |
| 7115 | if (!ReportedAmbiguousConversions) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7116 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7117 | ReportedAmbiguousConversions = true; |
| 7118 | } |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7119 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7120 | // If this is a viable builtin, print it. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7121 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7122 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7123 | } |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7124 | |
| 7125 | if (I != E) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7126 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7127 | } |
| 7128 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7129 | // [PossiblyAFunctionType] --> [Return] |
| 7130 | // NonFunctionType --> NonFunctionType |
| 7131 | // R (A) --> R(A) |
| 7132 | // R (*)(A) --> R (A) |
| 7133 | // R (&)(A) --> R (A) |
| 7134 | // R (S::*)(A) --> R (A) |
| 7135 | QualType Sema::ExtractUnqualifiedFunctionType(QualType PossiblyAFunctionType) { |
| 7136 | QualType Ret = PossiblyAFunctionType; |
| 7137 | if (const PointerType *ToTypePtr = |
| 7138 | PossiblyAFunctionType->getAs<PointerType>()) |
| 7139 | Ret = ToTypePtr->getPointeeType(); |
| 7140 | else if (const ReferenceType *ToTypeRef = |
| 7141 | PossiblyAFunctionType->getAs<ReferenceType>()) |
| 7142 | Ret = ToTypeRef->getPointeeType(); |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 7143 | else if (const MemberPointerType *MemTypePtr = |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7144 | PossiblyAFunctionType->getAs<MemberPointerType>()) |
| 7145 | Ret = MemTypePtr->getPointeeType(); |
| 7146 | Ret = |
| 7147 | Context.getCanonicalType(Ret).getUnqualifiedType(); |
| 7148 | return Ret; |
| 7149 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7150 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7151 | // A helper class to help with address of function resolution |
| 7152 | // - allows us to avoid passing around all those ugly parameters |
| 7153 | class AddressOfFunctionResolver |
| 7154 | { |
| 7155 | Sema& S; |
| 7156 | Expr* SourceExpr; |
| 7157 | const QualType& TargetType; |
| 7158 | QualType TargetFunctionType; // Extracted function type from target type |
| 7159 | |
| 7160 | bool Complain; |
| 7161 | //DeclAccessPair& ResultFunctionAccessPair; |
| 7162 | ASTContext& Context; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7163 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7164 | bool TargetTypeIsNonStaticMemberFunction; |
| 7165 | bool FoundNonTemplateFunction; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7166 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7167 | OverloadExpr::FindResult OvlExprInfo; |
| 7168 | OverloadExpr *OvlExpr; |
| 7169 | TemplateArgumentListInfo OvlExplicitTemplateArgs; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7170 | llvm::SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7171 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7172 | public: |
| 7173 | AddressOfFunctionResolver(Sema &S, Expr* SourceExpr, |
| 7174 | const QualType& TargetType, bool Complain) |
| 7175 | : S(S), SourceExpr(SourceExpr), TargetType(TargetType), |
| 7176 | Complain(Complain), Context(S.getASTContext()), |
| 7177 | TargetTypeIsNonStaticMemberFunction( |
| 7178 | !!TargetType->getAs<MemberPointerType>()), |
| 7179 | FoundNonTemplateFunction(false), |
| 7180 | OvlExprInfo(OverloadExpr::find(SourceExpr)), |
| 7181 | OvlExpr(OvlExprInfo.Expression) |
| 7182 | { |
| 7183 | ExtractUnqualifiedFunctionTypeFromTargetType(); |
| 7184 | |
| 7185 | if (!TargetFunctionType->isFunctionType()) { |
| 7186 | if (OvlExpr->hasExplicitTemplateArgs()) { |
| 7187 | DeclAccessPair dap; |
| 7188 | if( FunctionDecl* Fn = S.ResolveSingleFunctionTemplateSpecialization( |
| 7189 | OvlExpr, false, &dap) ) { |
Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 7190 | |
| 7191 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 7192 | if (!Method->isStatic()) { |
| 7193 | // If the target type is a non-function type and the function |
| 7194 | // found is a non-static member function, pretend as if that was |
| 7195 | // the target, it's the only possible type to end up with. |
| 7196 | TargetTypeIsNonStaticMemberFunction = true; |
| 7197 | |
| 7198 | // And skip adding the function if its not in the proper form. |
| 7199 | // We'll diagnose this due to an empty set of functions. |
| 7200 | if (!OvlExprInfo.HasFormOfMemberPointer) |
| 7201 | return; |
| 7202 | } |
| 7203 | } |
| 7204 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7205 | Matches.push_back(std::make_pair(dap,Fn)); |
| 7206 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 7207 | } |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7208 | return; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 7209 | } |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7210 | |
| 7211 | if (OvlExpr->hasExplicitTemplateArgs()) |
| 7212 | OvlExpr->getExplicitTemplateArgs().copyInto(OvlExplicitTemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7213 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7214 | if (FindAllFunctionsThatMatchTargetTypeExactly()) { |
| 7215 | // C++ [over.over]p4: |
| 7216 | // If more than one function is selected, [...] |
| 7217 | if (Matches.size() > 1) { |
| 7218 | if (FoundNonTemplateFunction) |
| 7219 | EliminateAllTemplateMatches(); |
| 7220 | else |
| 7221 | EliminateAllExceptMostSpecializedTemplate(); |
| 7222 | } |
| 7223 | } |
| 7224 | } |
| 7225 | |
| 7226 | private: |
| 7227 | bool isTargetTypeAFunction() const { |
| 7228 | return TargetFunctionType->isFunctionType(); |
| 7229 | } |
| 7230 | |
| 7231 | // [ToType] [Return] |
| 7232 | |
| 7233 | // R (*)(A) --> R (A), IsNonStaticMemberFunction = false |
| 7234 | // R (&)(A) --> R (A), IsNonStaticMemberFunction = false |
| 7235 | // R (S::*)(A) --> R (A), IsNonStaticMemberFunction = true |
| 7236 | void inline ExtractUnqualifiedFunctionTypeFromTargetType() { |
| 7237 | TargetFunctionType = S.ExtractUnqualifiedFunctionType(TargetType); |
| 7238 | } |
| 7239 | |
| 7240 | // return true if any matching specializations were found |
| 7241 | bool AddMatchingTemplateFunction(FunctionTemplateDecl* FunctionTemplate, |
| 7242 | const DeclAccessPair& CurAccessFunPair) { |
| 7243 | if (CXXMethodDecl *Method |
| 7244 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
| 7245 | // Skip non-static function templates when converting to pointer, and |
| 7246 | // static when converting to member pointer. |
| 7247 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 7248 | return false; |
| 7249 | } |
| 7250 | else if (TargetTypeIsNonStaticMemberFunction) |
| 7251 | return false; |
| 7252 | |
| 7253 | // C++ [over.over]p2: |
| 7254 | // If the name is a function template, template argument deduction is |
| 7255 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 7256 | // resulting template argument list is used to generate a single |
| 7257 | // function template specialization, which is added to the set of |
| 7258 | // overloaded functions considered. |
| 7259 | FunctionDecl *Specialization = 0; |
| 7260 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
| 7261 | if (Sema::TemplateDeductionResult Result |
| 7262 | = S.DeduceTemplateArguments(FunctionTemplate, |
| 7263 | &OvlExplicitTemplateArgs, |
| 7264 | TargetFunctionType, Specialization, |
| 7265 | Info)) { |
| 7266 | // FIXME: make a note of the failed deduction for diagnostics. |
| 7267 | (void)Result; |
| 7268 | return false; |
| 7269 | } |
| 7270 | |
| 7271 | // Template argument deduction ensures that we have an exact match. |
| 7272 | // This function template specicalization works. |
| 7273 | Specialization = cast<FunctionDecl>(Specialization->getCanonicalDecl()); |
| 7274 | assert(TargetFunctionType |
| 7275 | == Context.getCanonicalType(Specialization->getType())); |
| 7276 | Matches.push_back(std::make_pair(CurAccessFunPair, Specialization)); |
| 7277 | return true; |
| 7278 | } |
| 7279 | |
| 7280 | bool AddMatchingNonTemplateFunction(NamedDecl* Fn, |
| 7281 | const DeclAccessPair& CurAccessFunPair) { |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 7282 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 7283 | // Skip non-static functions when converting to pointer, and static |
| 7284 | // when converting to member pointer. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7285 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 7286 | return false; |
| 7287 | } |
| 7288 | else if (TargetTypeIsNonStaticMemberFunction) |
| 7289 | return false; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7290 | |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 7291 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 7292 | QualType ResultTy; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7293 | if (Context.hasSameUnqualifiedType(TargetFunctionType, |
| 7294 | FunDecl->getType()) || |
| 7295 | IsNoReturnConversion(Context, FunDecl->getType(), TargetFunctionType, |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 7296 | ResultTy)) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7297 | Matches.push_back(std::make_pair(CurAccessFunPair, |
| 7298 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 7299 | FoundNonTemplateFunction = true; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7300 | return true; |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 7301 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7302 | } |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7303 | |
| 7304 | return false; |
| 7305 | } |
| 7306 | |
| 7307 | bool FindAllFunctionsThatMatchTargetTypeExactly() { |
| 7308 | bool Ret = false; |
| 7309 | |
| 7310 | // If the overload expression doesn't have the form of a pointer to |
| 7311 | // member, don't try to convert it to a pointer-to-member type. |
| 7312 | if (IsInvalidFormOfPointerToMemberFunction()) |
| 7313 | return false; |
| 7314 | |
| 7315 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 7316 | E = OvlExpr->decls_end(); |
| 7317 | I != E; ++I) { |
| 7318 | // Look through any using declarations to find the underlying function. |
| 7319 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 7320 | |
| 7321 | // C++ [over.over]p3: |
| 7322 | // Non-member functions and static member functions match |
| 7323 | // targets of type "pointer-to-function" or "reference-to-function." |
| 7324 | // Nonstatic member functions match targets of |
| 7325 | // type "pointer-to-member-function." |
| 7326 | // Note that according to DR 247, the containing class does not matter. |
| 7327 | if (FunctionTemplateDecl *FunctionTemplate |
| 7328 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
| 7329 | if (AddMatchingTemplateFunction(FunctionTemplate, I.getPair())) |
| 7330 | Ret = true; |
| 7331 | } |
| 7332 | // If we have explicit template arguments supplied, skip non-templates. |
| 7333 | else if (!OvlExpr->hasExplicitTemplateArgs() && |
| 7334 | AddMatchingNonTemplateFunction(Fn, I.getPair())) |
| 7335 | Ret = true; |
| 7336 | } |
| 7337 | assert(Ret || Matches.empty()); |
| 7338 | return Ret; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7339 | } |
| 7340 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7341 | void EliminateAllExceptMostSpecializedTemplate() { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7342 | // [...] and any given function template specialization F1 is |
| 7343 | // eliminated if the set contains a second function template |
| 7344 | // specialization whose function template is more specialized |
| 7345 | // than the function template of F1 according to the partial |
| 7346 | // ordering rules of 14.5.5.2. |
| 7347 | |
| 7348 | // The algorithm specified above is quadratic. We instead use a |
| 7349 | // two-pass algorithm (similar to the one used to identify the |
| 7350 | // best viable function in an overload set) that identifies the |
| 7351 | // best function template (if it exists). |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7352 | |
| 7353 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 7354 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 7355 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7356 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7357 | UnresolvedSetIterator Result = |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7358 | S.getMostSpecialized(MatchesCopy.begin(), MatchesCopy.end(), |
| 7359 | TPOC_Other, 0, SourceExpr->getLocStart(), |
| 7360 | S.PDiag(), |
| 7361 | S.PDiag(diag::err_addr_ovl_ambiguous) |
| 7362 | << Matches[0].second->getDeclName(), |
| 7363 | S.PDiag(diag::note_ovl_candidate) |
| 7364 | << (unsigned) oc_function_template, |
| 7365 | Complain); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7366 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7367 | if (Result != MatchesCopy.end()) { |
| 7368 | // Make it the first and only element |
| 7369 | Matches[0].first = Matches[Result - MatchesCopy.begin()].first; |
| 7370 | Matches[0].second = cast<FunctionDecl>(*Result); |
| 7371 | Matches.resize(1); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7372 | } |
| 7373 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7374 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7375 | void EliminateAllTemplateMatches() { |
| 7376 | // [...] any function template specializations in the set are |
| 7377 | // eliminated if the set also contains a non-template function, [...] |
| 7378 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
| 7379 | if (Matches[I].second->getPrimaryTemplate() == 0) |
| 7380 | ++I; |
| 7381 | else { |
| 7382 | Matches[I] = Matches[--N]; |
| 7383 | Matches.set_size(N); |
| 7384 | } |
| 7385 | } |
| 7386 | } |
| 7387 | |
| 7388 | public: |
| 7389 | void ComplainNoMatchesFound() const { |
| 7390 | assert(Matches.empty()); |
| 7391 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_no_viable) |
| 7392 | << OvlExpr->getName() << TargetFunctionType |
| 7393 | << OvlExpr->getSourceRange(); |
| 7394 | S.NoteAllOverloadCandidates(OvlExpr); |
| 7395 | } |
| 7396 | |
| 7397 | bool IsInvalidFormOfPointerToMemberFunction() const { |
| 7398 | return TargetTypeIsNonStaticMemberFunction && |
| 7399 | !OvlExprInfo.HasFormOfMemberPointer; |
| 7400 | } |
| 7401 | |
| 7402 | void ComplainIsInvalidFormOfPointerToMemberFunction() const { |
| 7403 | // TODO: Should we condition this on whether any functions might |
| 7404 | // have matched, or is it more appropriate to do that in callers? |
| 7405 | // TODO: a fixit wouldn't hurt. |
| 7406 | S.Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
| 7407 | << TargetType << OvlExpr->getSourceRange(); |
| 7408 | } |
| 7409 | |
| 7410 | void ComplainOfInvalidConversion() const { |
| 7411 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 7412 | << OvlExpr->getName() << TargetType; |
| 7413 | } |
| 7414 | |
| 7415 | void ComplainMultipleMatchesFound() const { |
| 7416 | assert(Matches.size() > 1); |
| 7417 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 7418 | << OvlExpr->getName() |
| 7419 | << OvlExpr->getSourceRange(); |
| 7420 | S.NoteAllOverloadCandidates(OvlExpr); |
| 7421 | } |
| 7422 | |
| 7423 | int getNumMatches() const { return Matches.size(); } |
| 7424 | |
| 7425 | FunctionDecl* getMatchingFunctionDecl() const { |
| 7426 | if (Matches.size() != 1) return 0; |
| 7427 | return Matches[0].second; |
| 7428 | } |
| 7429 | |
| 7430 | const DeclAccessPair* getMatchingFunctionAccessPair() const { |
| 7431 | if (Matches.size() != 1) return 0; |
| 7432 | return &Matches[0].first; |
| 7433 | } |
| 7434 | }; |
| 7435 | |
| 7436 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 7437 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 7438 | /// expression with overloaded function type and @p ToType is the type |
| 7439 | /// we're trying to resolve to. For example: |
| 7440 | /// |
| 7441 | /// @code |
| 7442 | /// int f(double); |
| 7443 | /// int f(int); |
| 7444 | /// |
| 7445 | /// int (*pfd)(double) = f; // selects f(double) |
| 7446 | /// @endcode |
| 7447 | /// |
| 7448 | /// This routine returns the resulting FunctionDecl if it could be |
| 7449 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 7450 | /// routine will emit diagnostics if there is an error. |
| 7451 | FunctionDecl * |
| 7452 | Sema::ResolveAddressOfOverloadedFunction(Expr *AddressOfExpr, QualType TargetType, |
| 7453 | bool Complain, |
| 7454 | DeclAccessPair &FoundResult) { |
| 7455 | |
| 7456 | assert(AddressOfExpr->getType() == Context.OverloadTy); |
| 7457 | |
| 7458 | AddressOfFunctionResolver Resolver(*this, AddressOfExpr, TargetType, Complain); |
| 7459 | int NumMatches = Resolver.getNumMatches(); |
| 7460 | FunctionDecl* Fn = 0; |
| 7461 | if ( NumMatches == 0 && Complain) { |
| 7462 | if (Resolver.IsInvalidFormOfPointerToMemberFunction()) |
| 7463 | Resolver.ComplainIsInvalidFormOfPointerToMemberFunction(); |
| 7464 | else |
| 7465 | Resolver.ComplainNoMatchesFound(); |
| 7466 | } |
| 7467 | else if (NumMatches > 1 && Complain) |
| 7468 | Resolver.ComplainMultipleMatchesFound(); |
| 7469 | else if (NumMatches == 1) { |
| 7470 | Fn = Resolver.getMatchingFunctionDecl(); |
| 7471 | assert(Fn); |
| 7472 | FoundResult = *Resolver.getMatchingFunctionAccessPair(); |
| 7473 | MarkDeclarationReferenced(AddressOfExpr->getLocStart(), Fn); |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 7474 | if (Complain) |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7475 | CheckAddressOfMemberAccess(AddressOfExpr, FoundResult); |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 7476 | } |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7477 | |
| 7478 | return Fn; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7479 | } |
| 7480 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7481 | /// \brief Given an expression that refers to an overloaded function, try to |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7482 | /// resolve that overloaded function expression down to a single function. |
| 7483 | /// |
| 7484 | /// This routine can only resolve template-ids that refer to a single function |
| 7485 | /// template, where that template-id refers to a single template whose template |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7486 | /// arguments are either provided by the template-id or have defaults, |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7487 | /// as described in C++0x [temp.arg.explicit]p3. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7488 | FunctionDecl *Sema::ResolveSingleFunctionTemplateSpecialization(Expr *From, |
| 7489 | bool Complain, |
| 7490 | DeclAccessPair* FoundResult) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7491 | // C++ [over.over]p1: |
| 7492 | // [...] [Note: any redundant set of parentheses surrounding the |
| 7493 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7494 | // C++ [over.over]p1: |
| 7495 | // [...] The overloaded function name can be preceded by the & |
| 7496 | // operator. |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 7497 | if (From->getType() != Context.OverloadTy) |
| 7498 | return 0; |
| 7499 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7500 | OverloadExpr *OvlExpr = OverloadExpr::find(From).Expression; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7501 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7502 | // If we didn't actually find any template-ids, we're done. |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 7503 | if (!OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7504 | return 0; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 7505 | |
| 7506 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 7507 | OvlExpr->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7508 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7509 | // Look through all of the overloaded functions, searching for one |
| 7510 | // whose type matches exactly. |
| 7511 | FunctionDecl *Matched = 0; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 7512 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 7513 | E = OvlExpr->decls_end(); I != E; ++I) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7514 | // C++0x [temp.arg.explicit]p3: |
| 7515 | // [...] In contexts where deduction is done and fails, or in contexts |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7516 | // where deduction is not done, if a template argument list is |
| 7517 | // specified and it, along with any default template arguments, |
| 7518 | // identifies a single function template specialization, then the |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7519 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | eebe721 | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 7520 | FunctionTemplateDecl *FunctionTemplate |
| 7521 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7522 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7523 | // C++ [over.over]p2: |
| 7524 | // If the name is a function template, template argument deduction is |
| 7525 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 7526 | // resulting template argument list is used to generate a single |
| 7527 | // function template specialization, which is added to the set of |
| 7528 | // overloaded functions considered. |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7529 | FunctionDecl *Specialization = 0; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7530 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7531 | if (TemplateDeductionResult Result |
| 7532 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 7533 | Specialization, Info)) { |
| 7534 | // FIXME: make a note of the failed deduction for diagnostics. |
| 7535 | (void)Result; |
| 7536 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7537 | } |
| 7538 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7539 | // Multiple matches; we can't resolve to a single declaration. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7540 | if (Matched) { |
| 7541 | if (FoundResult) |
| 7542 | *FoundResult = DeclAccessPair(); |
| 7543 | |
| 7544 | if (Complain) { |
| 7545 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 7546 | << OvlExpr->getName(); |
| 7547 | NoteAllOverloadCandidates(OvlExpr); |
| 7548 | } |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7549 | return 0; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7550 | } |
| 7551 | |
| 7552 | if ((Matched = Specialization) && FoundResult) |
| 7553 | *FoundResult = I.getPair(); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7554 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7555 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7556 | return Matched; |
| 7557 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7558 | |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7559 | |
| 7560 | |
| 7561 | |
| 7562 | // Resolve and fix an overloaded expression that |
| 7563 | // can be resolved because it identifies a single function |
| 7564 | // template specialization |
| 7565 | // Last three arguments should only be supplied if Complain = true |
| 7566 | ExprResult Sema::ResolveAndFixSingleFunctionTemplateSpecialization( |
| 7567 | Expr *SrcExpr, bool DoFunctionPointerConverion, bool Complain, |
| 7568 | const SourceRange& OpRangeForComplaining, |
| 7569 | QualType DestTypeForComplaining, |
| 7570 | unsigned DiagIDForComplaining ) { |
| 7571 | |
| 7572 | assert(SrcExpr->getType() == Context.OverloadTy); |
| 7573 | |
| 7574 | DeclAccessPair Found; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7575 | ExprResult SingleFunctionExpression; |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7576 | if (FunctionDecl* Fn = ResolveSingleFunctionTemplateSpecialization( |
| 7577 | SrcExpr, false, // false -> Complain |
| 7578 | &Found)) { |
| 7579 | if (!DiagnoseUseOfDecl(Fn, SrcExpr->getSourceRange().getBegin())) { |
| 7580 | // mark the expression as resolved to Fn |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7581 | SingleFunctionExpression = Owned(FixOverloadedFunctionReference(SrcExpr, |
| 7582 | Found, Fn)); |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7583 | if (DoFunctionPointerConverion) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7584 | SingleFunctionExpression = |
| 7585 | DefaultFunctionArrayLvalueConversion(SingleFunctionExpression.take()); |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7586 | } |
| 7587 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7588 | if (!SingleFunctionExpression.isUsable()) { |
Douglas Gregor | 89f3cd5 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 7589 | if (Complain) { |
| 7590 | OverloadExpr* oe = OverloadExpr::find(SrcExpr).Expression; |
| 7591 | Diag(OpRangeForComplaining.getBegin(), DiagIDForComplaining) |
| 7592 | << oe->getName() << DestTypeForComplaining << OpRangeForComplaining |
| 7593 | << oe->getQualifierLoc().getSourceRange(); |
| 7594 | NoteAllOverloadCandidates(SrcExpr); |
| 7595 | } |
| 7596 | return ExprError(); |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7597 | } |
Douglas Gregor | 89f3cd5 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 7598 | |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7599 | return SingleFunctionExpression; |
| 7600 | } |
| 7601 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7602 | /// \brief Add a single candidate to the overload set. |
| 7603 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7604 | DeclAccessPair FoundDecl, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 7605 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7606 | Expr **Args, unsigned NumArgs, |
| 7607 | OverloadCandidateSet &CandidateSet, |
| 7608 | bool PartialOverloading) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7609 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7610 | if (isa<UsingShadowDecl>(Callee)) |
| 7611 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 7612 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7613 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7614 | assert(!ExplicitTemplateArgs && "Explicit template arguments?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7615 | S.AddOverloadCandidate(Func, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | b05275a | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 7616 | false, PartialOverloading); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7617 | return; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7618 | } |
| 7619 | |
| 7620 | if (FunctionTemplateDecl *FuncTemplate |
| 7621 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7622 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
| 7623 | ExplicitTemplateArgs, |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7624 | Args, NumArgs, CandidateSet); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7625 | return; |
| 7626 | } |
| 7627 | |
| 7628 | assert(false && "unhandled case in overloaded call candidate"); |
| 7629 | |
| 7630 | // do nothing? |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7631 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7632 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7633 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 7634 | /// dependent lookup to the given overload set. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7635 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7636 | Expr **Args, unsigned NumArgs, |
| 7637 | OverloadCandidateSet &CandidateSet, |
| 7638 | bool PartialOverloading) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7639 | |
| 7640 | #ifndef NDEBUG |
| 7641 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 7642 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7643 | // |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7644 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 7645 | // and let Y be the lookup set produced by argument dependent |
| 7646 | // lookup (defined as follows). If X contains |
| 7647 | // |
| 7648 | // -- a declaration of a class member, or |
| 7649 | // |
| 7650 | // -- a block-scope function declaration that is not a |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7651 | // using-declaration, or |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7652 | // |
| 7653 | // -- a declaration that is neither a function or a function |
| 7654 | // template |
| 7655 | // |
| 7656 | // then Y is empty. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7657 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7658 | if (ULE->requiresADL()) { |
| 7659 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 7660 | E = ULE->decls_end(); I != E; ++I) { |
| 7661 | assert(!(*I)->getDeclContext()->isRecord()); |
| 7662 | assert(isa<UsingShadowDecl>(*I) || |
| 7663 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 7664 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7665 | } |
| 7666 | } |
| 7667 | #endif |
| 7668 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7669 | // It would be nice to avoid this copy. |
| 7670 | TemplateArgumentListInfo TABuffer; |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 7671 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7672 | if (ULE->hasExplicitTemplateArgs()) { |
| 7673 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 7674 | ExplicitTemplateArgs = &TABuffer; |
| 7675 | } |
| 7676 | |
| 7677 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 7678 | E = ULE->decls_end(); I != E; ++I) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7679 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7680 | Args, NumArgs, CandidateSet, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7681 | PartialOverloading); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7682 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7683 | if (ULE->requiresADL()) |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7684 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
| 7685 | Args, NumArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7686 | ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7687 | CandidateSet, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7688 | PartialOverloading); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7689 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7690 | |
| 7691 | /// Attempts to recover from a call where no functions were found. |
| 7692 | /// |
| 7693 | /// Returns true if new candidates were found. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7694 | static ExprResult |
Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 7695 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7696 | UnresolvedLookupExpr *ULE, |
| 7697 | SourceLocation LParenLoc, |
| 7698 | Expr **Args, unsigned NumArgs, |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7699 | SourceLocation RParenLoc) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7700 | |
| 7701 | CXXScopeSpec SS; |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 7702 | SS.Adopt(ULE->getQualifierLoc()); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7703 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7704 | TemplateArgumentListInfo TABuffer; |
| 7705 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 7706 | if (ULE->hasExplicitTemplateArgs()) { |
| 7707 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 7708 | ExplicitTemplateArgs = &TABuffer; |
| 7709 | } |
| 7710 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7711 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 7712 | Sema::LookupOrdinaryName); |
Douglas Gregor | 5fd04d4 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 7713 | if (SemaRef.DiagnoseEmptyLookup(S, SS, R, Sema::CTC_Expression)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7714 | return ExprError(); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7715 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7716 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 7717 | |
| 7718 | // Build an implicit member call if appropriate. Just drop the |
| 7719 | // casts and such from the call, we don't really care. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7720 | ExprResult NewFn = ExprError(); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7721 | if ((*R.begin())->isCXXClassMember()) |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7722 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, |
| 7723 | ExplicitTemplateArgs); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7724 | else if (ExplicitTemplateArgs) |
| 7725 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 7726 | else |
| 7727 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 7728 | |
| 7729 | if (NewFn.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7730 | return ExprError(); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7731 | |
| 7732 | // This shouldn't cause an infinite loop because we're giving it |
| 7733 | // an expression with non-empty lookup results, which should never |
| 7734 | // end up here. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7735 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, NewFn.take(), LParenLoc, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 7736 | MultiExprArg(Args, NumArgs), RParenLoc); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7737 | } |
Douglas Gregor | 4038cf4 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 7738 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7739 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7740 | /// (which eventually refers to the declaration Func) and the call |
| 7741 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 7742 | /// to a specific function. If overload resolution succeeds, returns |
| 7743 | /// the function declaration produced by overload |
Douglas Gregor | a60a691 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 7744 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7745 | /// arguments and Fn, and returns NULL. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7746 | ExprResult |
Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 7747 | Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, UnresolvedLookupExpr *ULE, |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7748 | SourceLocation LParenLoc, |
| 7749 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 7750 | SourceLocation RParenLoc, |
| 7751 | Expr *ExecConfig) { |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7752 | #ifndef NDEBUG |
| 7753 | if (ULE->requiresADL()) { |
| 7754 | // To do ADL, we must have found an unqualified name. |
| 7755 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 7756 | |
| 7757 | // We don't perform ADL for implicit declarations of builtins. |
| 7758 | // Verify that this was correctly set up. |
| 7759 | FunctionDecl *F; |
| 7760 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 7761 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 7762 | F->getBuiltinID() && F->isImplicit()) |
| 7763 | assert(0 && "performing ADL for builtin"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7764 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7765 | // We don't perform ADL in C. |
| 7766 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
| 7767 | } |
| 7768 | #endif |
| 7769 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7770 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 7771 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7772 | // Add the functions denoted by the callee to the set of candidate |
| 7773 | // functions, including those from argument-dependent lookup. |
| 7774 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7775 | |
| 7776 | // If we found nothing, try to recover. |
| 7777 | // AddRecoveryCallCandidates diagnoses the error itself, so we just |
| 7778 | // bailout out if it fails. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7779 | if (CandidateSet.empty()) |
Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 7780 | return BuildRecoveryCallExpr(*this, S, Fn, ULE, LParenLoc, Args, NumArgs, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 7781 | RParenLoc); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7782 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7783 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7784 | switch (CandidateSet.BestViableFunction(*this, Fn->getLocStart(), Best)) { |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7785 | case OR_Success: { |
| 7786 | FunctionDecl *FDecl = Best->Function; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 7787 | MarkDeclarationReferenced(Fn->getExprLoc(), FDecl); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7788 | CheckUnresolvedLookupAccess(ULE, Best->FoundDecl); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7789 | DiagnoseUseOfDecl(FDecl? FDecl : Best->FoundDecl.getDecl(), |
| 7790 | ULE->getNameLoc()); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7791 | Fn = FixOverloadedFunctionReference(Fn, Best->FoundDecl, FDecl); |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 7792 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 7793 | ExecConfig); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7794 | } |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7795 | |
| 7796 | case OR_No_Viable_Function: |
Chris Lattner | 45d9d60 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 7797 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7798 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7799 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7800 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7801 | break; |
| 7802 | |
| 7803 | case OR_Ambiguous: |
| 7804 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7805 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7806 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7807 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7808 | |
| 7809 | case OR_Deleted: |
Fariborz Jahanian | bff158d | 2011-02-25 18:38:59 +0000 | [diff] [blame] | 7810 | { |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 7811 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 7812 | << Best->Function->isDeleted() |
| 7813 | << ULE->getName() |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 7814 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 7815 | << Fn->getSourceRange(); |
Fariborz Jahanian | bff158d | 2011-02-25 18:38:59 +0000 | [diff] [blame] | 7816 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
| 7817 | } |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7818 | break; |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7819 | } |
| 7820 | |
Douglas Gregor | b412e17 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 7821 | // Overload resolution failed. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7822 | return ExprError(); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7823 | } |
| 7824 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7825 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 7826 | return Functions.size() > 1 || |
| 7827 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 7828 | } |
| 7829 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7830 | /// \brief Create a unary operation that may resolve to an overloaded |
| 7831 | /// operator. |
| 7832 | /// |
| 7833 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 7834 | /// |
| 7835 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 7836 | /// operator. |
| 7837 | /// |
| 7838 | /// \param Functions The set of non-member functions that will be |
| 7839 | /// considered by overload resolution. The caller needs to build this |
| 7840 | /// set based on the context using, e.g., |
| 7841 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 7842 | /// set should not contain any member functions; those will be added |
| 7843 | /// by CreateOverloadedUnaryOp(). |
| 7844 | /// |
| 7845 | /// \param input The input argument. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7846 | ExprResult |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7847 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 7848 | const UnresolvedSetImpl &Fns, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7849 | Expr *Input) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7850 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7851 | |
| 7852 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 7853 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 7854 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7855 | // TODO: provide better source location info. |
| 7856 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7857 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7858 | if (Input->getObjectKind() == OK_ObjCProperty) { |
| 7859 | ExprResult Result = ConvertPropertyForRValue(Input); |
| 7860 | if (Result.isInvalid()) |
| 7861 | return ExprError(); |
| 7862 | Input = Result.take(); |
| 7863 | } |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 7864 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7865 | Expr *Args[2] = { Input, 0 }; |
| 7866 | unsigned NumArgs = 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7867 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7868 | // For post-increment and post-decrement, add the implicit '0' as |
| 7869 | // the second argument, so that we know this is a post-increment or |
| 7870 | // post-decrement. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7871 | if (Opc == UO_PostInc || Opc == UO_PostDec) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7872 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 7873 | Args[1] = IntegerLiteral::Create(Context, Zero, Context.IntTy, |
| 7874 | SourceLocation()); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7875 | NumArgs = 2; |
| 7876 | } |
| 7877 | |
| 7878 | if (Input->isTypeDependent()) { |
Douglas Gregor | 630dec5 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 7879 | if (Fns.empty()) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7880 | return Owned(new (Context) UnaryOperator(Input, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7881 | Opc, |
Douglas Gregor | 630dec5 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 7882 | Context.DependentTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7883 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 630dec5 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 7884 | OpLoc)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7885 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7886 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7887 | UnresolvedLookupExpr *Fn |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 7888 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 7889 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 7890 | /*ADL*/ true, IsOverloaded(Fns), |
| 7891 | Fns.begin(), Fns.end()); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7892 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 7893 | &Args[0], NumArgs, |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7894 | Context.DependentTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7895 | VK_RValue, |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7896 | OpLoc)); |
| 7897 | } |
| 7898 | |
| 7899 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7900 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7901 | |
| 7902 | // Add the candidates from the given function set. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7903 | AddFunctionCandidates(Fns, &Args[0], NumArgs, CandidateSet, false); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7904 | |
| 7905 | // Add operator candidates that are member functions. |
| 7906 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 7907 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7908 | // Add candidates from ADL. |
| 7909 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Douglas Gregor | 6ec89d4 | 2010-02-05 05:15:43 +0000 | [diff] [blame] | 7910 | Args, NumArgs, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7911 | /*ExplicitTemplateArgs*/ 0, |
| 7912 | CandidateSet); |
| 7913 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7914 | // Add builtin operator candidates. |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 7915 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7916 | |
| 7917 | // Perform overload resolution. |
| 7918 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7919 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7920 | case OR_Success: { |
| 7921 | // We found a built-in operator or an overloaded operator. |
| 7922 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7923 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7924 | if (FnDecl) { |
| 7925 | // We matched an overloaded operator. Build a call to that |
| 7926 | // operator. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7927 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 7928 | MarkDeclarationReferenced(OpLoc, FnDecl); |
| 7929 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7930 | // Convert the arguments. |
| 7931 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7932 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 7933 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7934 | ExprResult InputRes = |
| 7935 | PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 7936 | Best->FoundDecl, Method); |
| 7937 | if (InputRes.isInvalid()) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7938 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7939 | Input = InputRes.take(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7940 | } else { |
| 7941 | // Convert the arguments. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7942 | ExprResult InputInit |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 7943 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 7944 | Context, |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 7945 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7946 | SourceLocation(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7947 | Input); |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 7948 | if (InputInit.isInvalid()) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7949 | return ExprError(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7950 | Input = InputInit.take(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7951 | } |
| 7952 | |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7953 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 7954 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7955 | // Determine the result type. |
| 7956 | QualType ResultTy = FnDecl->getResultType(); |
| 7957 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 7958 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7959 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7960 | // Build the actual expression node. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7961 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl); |
| 7962 | if (FnExpr.isInvalid()) |
| 7963 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7964 | |
Eli Friedman | 030eee4 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 7965 | Args[0] = Input; |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7966 | CallExpr *TheCall = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7967 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7968 | Args, NumArgs, ResultTy, VK, OpLoc); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7969 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7970 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 7971 | FnDecl)) |
| 7972 | return ExprError(); |
| 7973 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7974 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7975 | } else { |
| 7976 | // We matched a built-in operator. Convert the arguments, then |
| 7977 | // break out so that we will build the appropriate built-in |
| 7978 | // operator node. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7979 | ExprResult InputRes = |
| 7980 | PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 7981 | Best->Conversions[0], AA_Passing); |
| 7982 | if (InputRes.isInvalid()) |
| 7983 | return ExprError(); |
| 7984 | Input = InputRes.take(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7985 | break; |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7986 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7987 | } |
| 7988 | |
| 7989 | case OR_No_Viable_Function: |
| 7990 | // No viable function; fall through to handling this as a |
| 7991 | // built-in operator, which will produce an error message for us. |
| 7992 | break; |
| 7993 | |
| 7994 | case OR_Ambiguous: |
| 7995 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 7996 | << UnaryOperator::getOpcodeStr(Opc) |
| 7997 | << Input->getType() |
| 7998 | << Input->getSourceRange(); |
| 7999 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, |
| 8000 | Args, NumArgs, |
| 8001 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
| 8002 | return ExprError(); |
| 8003 | |
| 8004 | case OR_Deleted: |
| 8005 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 8006 | << Best->Function->isDeleted() |
| 8007 | << UnaryOperator::getOpcodeStr(Opc) |
| 8008 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 8009 | << Input->getSourceRange(); |
| 8010 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
| 8011 | return ExprError(); |
| 8012 | } |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8013 | |
| 8014 | // Either we found no viable overloaded operator or we matched a |
| 8015 | // built-in operator. In either case, fall through to trying to |
| 8016 | // build a built-in operation. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8017 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8018 | } |
| 8019 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8020 | /// \brief Create a binary operation that may resolve to an overloaded |
| 8021 | /// operator. |
| 8022 | /// |
| 8023 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 8024 | /// |
| 8025 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 8026 | /// operator. |
| 8027 | /// |
| 8028 | /// \param Functions The set of non-member functions that will be |
| 8029 | /// considered by overload resolution. The caller needs to build this |
| 8030 | /// set based on the context using, e.g., |
| 8031 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 8032 | /// set should not contain any member functions; those will be added |
| 8033 | /// by CreateOverloadedBinOp(). |
| 8034 | /// |
| 8035 | /// \param LHS Left-hand argument. |
| 8036 | /// \param RHS Right-hand argument. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8037 | ExprResult |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8038 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8039 | unsigned OpcIn, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8040 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8041 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8042 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8043 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8044 | |
| 8045 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 8046 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 8047 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 8048 | |
| 8049 | // If either side is type-dependent, create an appropriate dependent |
| 8050 | // expression. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8051 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8052 | if (Fns.empty()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8053 | // If there are no functions to store, just build a dependent |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8054 | // BinaryOperator or CompoundAssignment. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8055 | if (Opc <= BO_Assign || Opc > BO_OrAssign) |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8056 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8057 | Context.DependentTy, |
| 8058 | VK_RValue, OK_Ordinary, |
| 8059 | OpLoc)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8060 | |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8061 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 8062 | Context.DependentTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8063 | VK_LValue, |
| 8064 | OK_Ordinary, |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8065 | Context.DependentTy, |
| 8066 | Context.DependentTy, |
| 8067 | OpLoc)); |
| 8068 | } |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8069 | |
| 8070 | // FIXME: save results of ADL from here? |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8071 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8072 | // TODO: provide better source location info in DNLoc component. |
| 8073 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8074 | UnresolvedLookupExpr *Fn |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 8075 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
| 8076 | NestedNameSpecifierLoc(), OpNameInfo, |
| 8077 | /*ADL*/ true, IsOverloaded(Fns), |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 8078 | Fns.begin(), Fns.end()); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8079 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8080 | Args, 2, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8081 | Context.DependentTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8082 | VK_RValue, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8083 | OpLoc)); |
| 8084 | } |
| 8085 | |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8086 | // Always do property rvalue conversions on the RHS. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8087 | if (Args[1]->getObjectKind() == OK_ObjCProperty) { |
| 8088 | ExprResult Result = ConvertPropertyForRValue(Args[1]); |
| 8089 | if (Result.isInvalid()) |
| 8090 | return ExprError(); |
| 8091 | Args[1] = Result.take(); |
| 8092 | } |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8093 | |
| 8094 | // The LHS is more complicated. |
| 8095 | if (Args[0]->getObjectKind() == OK_ObjCProperty) { |
| 8096 | |
| 8097 | // There's a tension for assignment operators between primitive |
| 8098 | // property assignment and the overloaded operators. |
| 8099 | if (BinaryOperator::isAssignmentOp(Opc)) { |
| 8100 | const ObjCPropertyRefExpr *PRE = LHS->getObjCProperty(); |
| 8101 | |
| 8102 | // Is the property "logically" settable? |
| 8103 | bool Settable = (PRE->isExplicitProperty() || |
| 8104 | PRE->getImplicitPropertySetter()); |
| 8105 | |
| 8106 | // To avoid gratuitously inventing semantics, use the primitive |
| 8107 | // unless it isn't. Thoughts in case we ever really care: |
| 8108 | // - If the property isn't logically settable, we have to |
| 8109 | // load and hope. |
| 8110 | // - If the property is settable and this is simple assignment, |
| 8111 | // we really should use the primitive. |
| 8112 | // - If the property is settable, then we could try overloading |
| 8113 | // on a generic lvalue of the appropriate type; if it works |
| 8114 | // out to a builtin candidate, we would do that same operation |
| 8115 | // on the property, and otherwise just error. |
| 8116 | if (Settable) |
| 8117 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 8118 | } |
| 8119 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8120 | ExprResult Result = ConvertPropertyForRValue(Args[0]); |
| 8121 | if (Result.isInvalid()) |
| 8122 | return ExprError(); |
| 8123 | Args[0] = Result.take(); |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8124 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8125 | |
Sebastian Redl | 6a96bf7 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 8126 | // If this is the assignment operator, we only perform overload resolution |
| 8127 | // if the left-hand side is a class or enumeration type. This is actually |
| 8128 | // a hack. The standard requires that we do overload resolution between the |
| 8129 | // various built-in candidates, but as DR507 points out, this can lead to |
| 8130 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 8131 | // Note that we go the traditional code path for compound assignment forms. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8132 | if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8133 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8134 | |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8135 | // If this is the .* operator, which is not overloadable, just |
| 8136 | // create a built-in binary operator. |
| 8137 | if (Opc == BO_PtrMemD) |
| 8138 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 8139 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8140 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8141 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8142 | |
| 8143 | // Add the candidates from the given function set. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8144 | AddFunctionCandidates(Fns, Args, 2, CandidateSet, false); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8145 | |
| 8146 | // Add operator candidates that are member functions. |
| 8147 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 8148 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8149 | // Add candidates from ADL. |
| 8150 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
| 8151 | Args, 2, |
| 8152 | /*ExplicitTemplateArgs*/ 0, |
| 8153 | CandidateSet); |
| 8154 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8155 | // Add builtin operator candidates. |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 8156 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8157 | |
| 8158 | // Perform overload resolution. |
| 8159 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8160 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 8161 | case OR_Success: { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8162 | // We found a built-in operator or an overloaded operator. |
| 8163 | FunctionDecl *FnDecl = Best->Function; |
| 8164 | |
| 8165 | if (FnDecl) { |
| 8166 | // We matched an overloaded operator. Build a call to that |
| 8167 | // operator. |
| 8168 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8169 | MarkDeclarationReferenced(OpLoc, FnDecl); |
| 8170 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8171 | // Convert the arguments. |
| 8172 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 8173 | // Best->Access is only meaningful for class members. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8174 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 8175 | |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8176 | ExprResult Arg1 = |
| 8177 | PerformCopyInitialization( |
| 8178 | InitializedEntity::InitializeParameter(Context, |
| 8179 | FnDecl->getParamDecl(0)), |
| 8180 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8181 | if (Arg1.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8182 | return ExprError(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8183 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8184 | ExprResult Arg0 = |
| 8185 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 8186 | Best->FoundDecl, Method); |
| 8187 | if (Arg0.isInvalid()) |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8188 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8189 | Args[0] = Arg0.takeAs<Expr>(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8190 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8191 | } else { |
| 8192 | // Convert the arguments. |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8193 | ExprResult Arg0 = PerformCopyInitialization( |
| 8194 | InitializedEntity::InitializeParameter(Context, |
| 8195 | FnDecl->getParamDecl(0)), |
| 8196 | SourceLocation(), Owned(Args[0])); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8197 | if (Arg0.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8198 | return ExprError(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8199 | |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8200 | ExprResult Arg1 = |
| 8201 | PerformCopyInitialization( |
| 8202 | InitializedEntity::InitializeParameter(Context, |
| 8203 | FnDecl->getParamDecl(1)), |
| 8204 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8205 | if (Arg1.isInvalid()) |
| 8206 | return ExprError(); |
| 8207 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 8208 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8209 | } |
| 8210 | |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8211 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 8212 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8213 | // Determine the result type. |
| 8214 | QualType ResultTy = FnDecl->getResultType(); |
| 8215 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 8216 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8217 | |
| 8218 | // Build the actual expression node. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8219 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, OpLoc); |
| 8220 | if (FnExpr.isInvalid()) |
| 8221 | return ExprError(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8222 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8223 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8224 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8225 | Args, 2, ResultTy, VK, OpLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8226 | |
| 8227 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 8228 | FnDecl)) |
| 8229 | return ExprError(); |
| 8230 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8231 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8232 | } else { |
| 8233 | // We matched a built-in operator. Convert the arguments, then |
| 8234 | // break out so that we will build the appropriate built-in |
| 8235 | // operator node. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8236 | ExprResult ArgsRes0 = |
| 8237 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 8238 | Best->Conversions[0], AA_Passing); |
| 8239 | if (ArgsRes0.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8240 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8241 | Args[0] = ArgsRes0.take(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8242 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8243 | ExprResult ArgsRes1 = |
| 8244 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 8245 | Best->Conversions[1], AA_Passing); |
| 8246 | if (ArgsRes1.isInvalid()) |
| 8247 | return ExprError(); |
| 8248 | Args[1] = ArgsRes1.take(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8249 | break; |
| 8250 | } |
| 8251 | } |
| 8252 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8253 | case OR_No_Viable_Function: { |
| 8254 | // C++ [over.match.oper]p9: |
| 8255 | // If the operator is the operator , [...] and there are no |
| 8256 | // viable functions, then the operator is assumed to be the |
| 8257 | // built-in operator and interpreted according to clause 5. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8258 | if (Opc == BO_Comma) |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8259 | break; |
| 8260 | |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8261 | // For class as left operand for assignment or compound assigment |
| 8262 | // operator do not fall through to handling in built-in, but report that |
| 8263 | // no overloaded assignment operator found |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8264 | ExprResult Result = ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8265 | if (Args[0]->getType()->isRecordType() && |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8266 | Opc >= BO_Assign && Opc <= BO_OrAssign) { |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 8267 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 8268 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8269 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8270 | } else { |
| 8271 | // No viable function; try to create a built-in operation, which will |
| 8272 | // produce an error. Then, show the non-viable candidates. |
| 8273 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 8274 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8275 | assert(Result.isInvalid() && |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8276 | "C++ binary operator overloading is missing candidates!"); |
| 8277 | if (Result.isInvalid()) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8278 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 8279 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8280 | return move(Result); |
| 8281 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8282 | |
| 8283 | case OR_Ambiguous: |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 8284 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_binary) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8285 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 8286 | << Args[0]->getType() << Args[1]->getType() |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8287 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8288 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, 2, |
| 8289 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8290 | return ExprError(); |
| 8291 | |
| 8292 | case OR_Deleted: |
| 8293 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 8294 | << Best->Function->isDeleted() |
| 8295 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 8296 | << getDeletedOrUnavailableSuffix(Best->Function) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8297 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8298 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8299 | return ExprError(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8300 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8301 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8302 | // We matched a built-in operator; build it. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8303 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8304 | } |
| 8305 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8306 | ExprResult |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8307 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 8308 | SourceLocation RLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8309 | Expr *Base, Expr *Idx) { |
| 8310 | Expr *Args[2] = { Base, Idx }; |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8311 | DeclarationName OpName = |
| 8312 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 8313 | |
| 8314 | // If either side is type-dependent, create an appropriate dependent |
| 8315 | // expression. |
| 8316 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 8317 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8318 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8319 | // CHECKME: no 'operator' keyword? |
| 8320 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
| 8321 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8322 | UnresolvedLookupExpr *Fn |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 8323 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 8324 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 8325 | /*ADL*/ true, /*Overloaded*/ false, |
| 8326 | UnresolvedSetIterator(), |
| 8327 | UnresolvedSetIterator()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 8328 | // Can't add any actual overloads yet |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8329 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8330 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 8331 | Args, 2, |
| 8332 | Context.DependentTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8333 | VK_RValue, |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8334 | RLoc)); |
| 8335 | } |
| 8336 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8337 | if (Args[0]->getObjectKind() == OK_ObjCProperty) { |
| 8338 | ExprResult Result = ConvertPropertyForRValue(Args[0]); |
| 8339 | if (Result.isInvalid()) |
| 8340 | return ExprError(); |
| 8341 | Args[0] = Result.take(); |
| 8342 | } |
| 8343 | if (Args[1]->getObjectKind() == OK_ObjCProperty) { |
| 8344 | ExprResult Result = ConvertPropertyForRValue(Args[1]); |
| 8345 | if (Result.isInvalid()) |
| 8346 | return ExprError(); |
| 8347 | Args[1] = Result.take(); |
| 8348 | } |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8349 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8350 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8351 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8352 | |
| 8353 | // Subscript can only be overloaded as a member function. |
| 8354 | |
| 8355 | // Add operator candidates that are member functions. |
| 8356 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 8357 | |
| 8358 | // Add builtin operator candidates. |
| 8359 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 8360 | |
| 8361 | // Perform overload resolution. |
| 8362 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8363 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8364 | case OR_Success: { |
| 8365 | // We found a built-in operator or an overloaded operator. |
| 8366 | FunctionDecl *FnDecl = Best->Function; |
| 8367 | |
| 8368 | if (FnDecl) { |
| 8369 | // We matched an overloaded operator. Build a call to that |
| 8370 | // operator. |
| 8371 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8372 | MarkDeclarationReferenced(LLoc, FnDecl); |
| 8373 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8374 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8375 | DiagnoseUseOfDecl(Best->FoundDecl, LLoc); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8376 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8377 | // Convert the arguments. |
| 8378 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8379 | ExprResult Arg0 = |
| 8380 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 8381 | Best->FoundDecl, Method); |
| 8382 | if (Arg0.isInvalid()) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8383 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8384 | Args[0] = Arg0.take(); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8385 | |
Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 8386 | // Convert the arguments. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8387 | ExprResult InputInit |
Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 8388 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 8389 | Context, |
Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 8390 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8391 | SourceLocation(), |
Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 8392 | Owned(Args[1])); |
| 8393 | if (InputInit.isInvalid()) |
| 8394 | return ExprError(); |
| 8395 | |
| 8396 | Args[1] = InputInit.takeAs<Expr>(); |
| 8397 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8398 | // Determine the result type |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8399 | QualType ResultTy = FnDecl->getResultType(); |
| 8400 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 8401 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8402 | |
| 8403 | // Build the actual expression node. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8404 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, LLoc); |
| 8405 | if (FnExpr.isInvalid()) |
| 8406 | return ExprError(); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8407 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8408 | CXXOperatorCallExpr *TheCall = |
| 8409 | new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8410 | FnExpr.take(), Args, 2, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8411 | ResultTy, VK, RLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8412 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8413 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall, |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8414 | FnDecl)) |
| 8415 | return ExprError(); |
| 8416 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8417 | return MaybeBindToTemporary(TheCall); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8418 | } else { |
| 8419 | // We matched a built-in operator. Convert the arguments, then |
| 8420 | // break out so that we will build the appropriate built-in |
| 8421 | // operator node. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8422 | ExprResult ArgsRes0 = |
| 8423 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 8424 | Best->Conversions[0], AA_Passing); |
| 8425 | if (ArgsRes0.isInvalid()) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8426 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8427 | Args[0] = ArgsRes0.take(); |
| 8428 | |
| 8429 | ExprResult ArgsRes1 = |
| 8430 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 8431 | Best->Conversions[1], AA_Passing); |
| 8432 | if (ArgsRes1.isInvalid()) |
| 8433 | return ExprError(); |
| 8434 | Args[1] = ArgsRes1.take(); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8435 | |
| 8436 | break; |
| 8437 | } |
| 8438 | } |
| 8439 | |
| 8440 | case OR_No_Viable_Function: { |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 8441 | if (CandidateSet.empty()) |
| 8442 | Diag(LLoc, diag::err_ovl_no_oper) |
| 8443 | << Args[0]->getType() << /*subscript*/ 0 |
| 8444 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 8445 | else |
| 8446 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 8447 | << Args[0]->getType() |
| 8448 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8449 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 8450 | "[]", LLoc); |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 8451 | return ExprError(); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8452 | } |
| 8453 | |
| 8454 | case OR_Ambiguous: |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 8455 | Diag(LLoc, diag::err_ovl_ambiguous_oper_binary) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8456 | << "[]" |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 8457 | << Args[0]->getType() << Args[1]->getType() |
| 8458 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8459 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, 2, |
| 8460 | "[]", LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8461 | return ExprError(); |
| 8462 | |
| 8463 | case OR_Deleted: |
| 8464 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 8465 | << Best->Function->isDeleted() << "[]" |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 8466 | << getDeletedOrUnavailableSuffix(Best->Function) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8467 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8468 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 8469 | "[]", LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8470 | return ExprError(); |
| 8471 | } |
| 8472 | |
| 8473 | // We matched a built-in operator; build it. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8474 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8475 | } |
| 8476 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8477 | /// BuildCallToMemberFunction - Build a call to a member |
| 8478 | /// function. MemExpr is the expression that refers to the member |
| 8479 | /// function (and includes the object parameter), Args/NumArgs are the |
| 8480 | /// arguments to the function call (not including the object |
| 8481 | /// parameter). The caller needs to validate that the member |
| 8482 | /// expression refers to a member function or an overloaded member |
| 8483 | /// function. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8484 | ExprResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8485 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 8486 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 8487 | unsigned NumArgs, SourceLocation RParenLoc) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8488 | // Dig out the member expression. This holds both the object |
| 8489 | // argument and the member function we're referring to. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8490 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8491 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8492 | MemberExpr *MemExpr; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8493 | CXXMethodDecl *Method = 0; |
John McCall | 3a65ef4 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 8494 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 8495 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8496 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 8497 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8498 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8499 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 8500 | Qualifier = MemExpr->getQualifier(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8501 | } else { |
| 8502 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 8503 | Qualifier = UnresExpr->getQualifier(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8504 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 8505 | QualType ObjectType = UnresExpr->getBaseType(); |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 8506 | Expr::Classification ObjectClassification |
| 8507 | = UnresExpr->isArrow()? Expr::Classification::makeSimpleLValue() |
| 8508 | : UnresExpr->getBase()->Classify(Context); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8509 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8510 | // Add overload candidates |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8511 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8512 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8513 | // FIXME: avoid copy. |
| 8514 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 8515 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 8516 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 8517 | TemplateArgs = &TemplateArgsBuffer; |
| 8518 | } |
| 8519 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8520 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 8521 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 8522 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 8523 | NamedDecl *Func = *I; |
| 8524 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 8525 | if (isa<UsingShadowDecl>(Func)) |
| 8526 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 8527 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 8528 | |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 8529 | // Microsoft supports direct constructor calls. |
| 8530 | if (getLangOptions().Microsoft && isa<CXXConstructorDecl>(Func)) { |
| 8531 | AddOverloadCandidate(cast<CXXConstructorDecl>(Func), I.getPair(), Args, NumArgs, |
| 8532 | CandidateSet); |
| 8533 | } else if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 8534 | // If explicit template arguments were provided, we can't call a |
| 8535 | // non-template member function. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8536 | if (TemplateArgs) |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 8537 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8538 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8539 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8540 | ObjectClassification, |
| 8541 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 8542 | /*SuppressUserConversions=*/false); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8543 | } else { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8544 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8545 | I.getPair(), ActingDC, TemplateArgs, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8546 | ObjectType, ObjectClassification, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 8547 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 8548 | /*SuppressUsedConversions=*/false); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8549 | } |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 8550 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8551 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8552 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 8553 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8554 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8555 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getLocStart(), |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 8556 | Best)) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8557 | case OR_Success: |
| 8558 | Method = cast<CXXMethodDecl>(Best->Function); |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8559 | MarkDeclarationReferenced(UnresExpr->getMemberLoc(), Method); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8560 | FoundDecl = Best->FoundDecl; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8561 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8562 | DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8563 | break; |
| 8564 | |
| 8565 | case OR_No_Viable_Function: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8566 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8567 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 8568 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8569 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8570 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8571 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8572 | |
| 8573 | case OR_Ambiguous: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8574 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 8575 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8576 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8577 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8578 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8579 | |
| 8580 | case OR_Deleted: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8581 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8582 | << Best->Function->isDeleted() |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 8583 | << DeclName |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 8584 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 8585 | << MemExprE->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8586 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8587 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8588 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8589 | } |
| 8590 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8591 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8592 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8593 | // If overload resolution picked a static member, build a |
| 8594 | // non-member call based on that function. |
| 8595 | if (Method->isStatic()) { |
| 8596 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 8597 | Args, NumArgs, RParenLoc); |
| 8598 | } |
| 8599 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8600 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8601 | } |
| 8602 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8603 | QualType ResultType = Method->getResultType(); |
| 8604 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 8605 | ResultType = ResultType.getNonLValueExprType(Context); |
| 8606 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8607 | assert(Method && "Member call to something that isn't a method?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8608 | CXXMemberCallExpr *TheCall = |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8609 | new (Context) CXXMemberCallExpr(Context, MemExprE, Args, NumArgs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8610 | ResultType, VK, RParenLoc); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8611 | |
Anders Carlsson | c4859ba | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 8612 | // Check for a valid return type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8613 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8614 | TheCall, Method)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8615 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8616 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8617 | // Convert the object argument (for a non-static member function call). |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8618 | // We only need to do this if there was actually an overload; otherwise |
| 8619 | // it was done at lookup. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8620 | if (!Method->isStatic()) { |
| 8621 | ExprResult ObjectArg = |
| 8622 | PerformObjectArgumentInitialization(MemExpr->getBase(), Qualifier, |
| 8623 | FoundDecl, Method); |
| 8624 | if (ObjectArg.isInvalid()) |
| 8625 | return ExprError(); |
| 8626 | MemExpr->setBase(ObjectArg.take()); |
| 8627 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8628 | |
| 8629 | // Convert the rest of the arguments |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8630 | const FunctionProtoType *Proto = |
| 8631 | Method->getType()->getAs<FunctionProtoType>(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8632 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8633 | RParenLoc)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8634 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8635 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8636 | if (CheckFunctionCall(Method, TheCall)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8637 | return ExprError(); |
Anders Carlsson | 8c84c20 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 8638 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8639 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8640 | } |
| 8641 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8642 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 8643 | /// type (C++ [over.call.object]), which can end up invoking an |
| 8644 | /// overloaded function call operator (@c operator()) or performing a |
| 8645 | /// user-defined conversion on the object argument. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8646 | ExprResult |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8647 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, |
Douglas Gregor | b0846b0 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 8648 | SourceLocation LParenLoc, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8649 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8650 | SourceLocation RParenLoc) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8651 | ExprResult Object = Owned(Obj); |
| 8652 | if (Object.get()->getObjectKind() == OK_ObjCProperty) { |
| 8653 | Object = ConvertPropertyForRValue(Object.take()); |
| 8654 | if (Object.isInvalid()) |
| 8655 | return ExprError(); |
| 8656 | } |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8657 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8658 | assert(Object.get()->getType()->isRecordType() && "Requires object type argument"); |
| 8659 | const RecordType *Record = Object.get()->getType()->getAs<RecordType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8660 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8661 | // C++ [over.call.object]p1: |
| 8662 | // If the primary-expression E in the function call syntax |
Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 8663 | // 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] | 8664 | // candidate functions includes at least the function call |
| 8665 | // operators of T. The function call operators of T are obtained by |
| 8666 | // ordinary lookup of the name operator() in the context of |
| 8667 | // (E).operator(). |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8668 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 8669 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 8670 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8671 | if (RequireCompleteType(LParenLoc, Object.get()->getType(), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8672 | PDiag(diag::err_incomplete_object_call) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8673 | << Object.get()->getSourceRange())) |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 8674 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8675 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8676 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 8677 | LookupQualifiedName(R, Record->getDecl()); |
| 8678 | R.suppressDiagnostics(); |
| 8679 | |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 8680 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 8681 | Oper != OperEnd; ++Oper) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8682 | AddMethodCandidate(Oper.getPair(), Object.get()->getType(), |
| 8683 | Object.get()->Classify(Context), Args, NumArgs, CandidateSet, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 8684 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 8685 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8686 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8687 | // C++ [over.call.object]p2: |
| 8688 | // In addition, for each conversion function declared in T of the |
| 8689 | // form |
| 8690 | // |
| 8691 | // operator conversion-type-id () cv-qualifier; |
| 8692 | // |
| 8693 | // where cv-qualifier is the same cv-qualification as, or a |
| 8694 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | f49fdf8 | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 8695 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 8696 | // R", or the type "reference to pointer to function of |
| 8697 | // (P1,...,Pn) returning R", or the type "reference to function |
| 8698 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8699 | // is also considered as a candidate function. Similarly, |
| 8700 | // surrogate call functions are added to the set of candidate |
| 8701 | // functions for each conversion function declared in an |
| 8702 | // accessible base class provided the function is not hidden |
| 8703 | // within T by another intervening declaration. |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 8704 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 2159182 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 8705 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 8706 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8707 | E = Conversions->end(); I != E; ++I) { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 8708 | NamedDecl *D = *I; |
| 8709 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 8710 | if (isa<UsingShadowDecl>(D)) |
| 8711 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8712 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 8713 | // Skip over templated conversion functions; they aren't |
| 8714 | // surrogates. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 8715 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 8716 | continue; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 8717 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 8718 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8719 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 8720 | // Strip the reference type (if any) and then the pointer type (if |
| 8721 | // any) to get down to what might be a function type. |
| 8722 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 8723 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 8724 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8725 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 8726 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8727 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8728 | Object.get(), Args, NumArgs, CandidateSet); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8729 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8730 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8731 | // Perform overload resolution. |
| 8732 | OverloadCandidateSet::iterator Best; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8733 | switch (CandidateSet.BestViableFunction(*this, Object.get()->getLocStart(), |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8734 | Best)) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8735 | case OR_Success: |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8736 | // Overload resolution succeeded; we'll build the appropriate call |
| 8737 | // below. |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8738 | break; |
| 8739 | |
| 8740 | case OR_No_Viable_Function: |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 8741 | if (CandidateSet.empty()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8742 | Diag(Object.get()->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 8743 | << Object.get()->getType() << /*call*/ 1 |
| 8744 | << Object.get()->getSourceRange(); |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 8745 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8746 | Diag(Object.get()->getSourceRange().getBegin(), |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 8747 | diag::err_ovl_no_viable_object_call) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8748 | << Object.get()->getType() << Object.get()->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8749 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8750 | break; |
| 8751 | |
| 8752 | case OR_Ambiguous: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8753 | Diag(Object.get()->getSourceRange().getBegin(), |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8754 | diag::err_ovl_ambiguous_object_call) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8755 | << Object.get()->getType() << Object.get()->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8756 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8757 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8758 | |
| 8759 | case OR_Deleted: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8760 | Diag(Object.get()->getSourceRange().getBegin(), |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8761 | diag::err_ovl_deleted_object_call) |
| 8762 | << Best->Function->isDeleted() |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8763 | << Object.get()->getType() |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 8764 | << getDeletedOrUnavailableSuffix(Best->Function) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8765 | << Object.get()->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8766 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8767 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8768 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8769 | |
Douglas Gregor | b412e17 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 8770 | if (Best == CandidateSet.end()) |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8771 | return true; |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8772 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8773 | if (Best->Function == 0) { |
| 8774 | // Since there is no function declaration, this is one of the |
| 8775 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8776 | CXXConversionDecl *Conv |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8777 | = cast<CXXConversionDecl>( |
| 8778 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 8779 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8780 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8781 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 49ec2e6 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 8782 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8783 | // We selected one of the surrogate functions that converts the |
| 8784 | // object parameter to a function pointer. Perform the conversion |
| 8785 | // on the object argument, then let ActOnCallExpr finish the job. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8786 | |
Fariborz Jahanian | 774cf79 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 8787 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 8788 | // and then call it. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8789 | ExprResult Call = BuildCXXMemberCallExpr(Object.get(), Best->FoundDecl, Conv); |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 8790 | if (Call.isInvalid()) |
| 8791 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8792 | |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 8793 | return ActOnCallExpr(S, Call.get(), LParenLoc, MultiExprArg(Args, NumArgs), |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 8794 | RParenLoc); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8795 | } |
| 8796 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8797 | MarkDeclarationReferenced(LParenLoc, Best->Function); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8798 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8799 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 49ec2e6 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 8800 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8801 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 8802 | // that calls this method, using Object for the implicit object |
| 8803 | // parameter and passing along the remaining arguments. |
| 8804 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8805 | const FunctionProtoType *Proto = |
| 8806 | Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8807 | |
| 8808 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 8809 | unsigned NumArgsToCheck = NumArgs; |
| 8810 | |
| 8811 | // Build the full argument list for the method call (the |
| 8812 | // implicit object parameter is placed at the beginning of the |
| 8813 | // list). |
| 8814 | Expr **MethodArgs; |
| 8815 | if (NumArgs < NumArgsInProto) { |
| 8816 | NumArgsToCheck = NumArgsInProto; |
| 8817 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 8818 | } else { |
| 8819 | MethodArgs = new Expr*[NumArgs + 1]; |
| 8820 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8821 | MethodArgs[0] = Object.get(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8822 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 8823 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8824 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8825 | ExprResult NewFn = CreateFunctionRefExpr(*this, Method); |
| 8826 | if (NewFn.isInvalid()) |
| 8827 | return true; |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8828 | |
| 8829 | // Once we've built TheCall, all of the expressions are properly |
| 8830 | // owned. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8831 | QualType ResultTy = Method->getResultType(); |
| 8832 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 8833 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 8834 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8835 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8836 | new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn.take(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8837 | MethodArgs, NumArgs + 1, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8838 | ResultTy, VK, RParenLoc); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8839 | delete [] MethodArgs; |
| 8840 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8841 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall, |
Anders Carlsson | 3d5829c | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 8842 | Method)) |
| 8843 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8844 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 8845 | // We may have default arguments. If so, we need to allocate more |
| 8846 | // slots in the call for them. |
| 8847 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 8848 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 8849 | else if (NumArgs > NumArgsInProto) |
| 8850 | NumArgsToCheck = NumArgsInProto; |
| 8851 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 8852 | bool IsError = false; |
| 8853 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8854 | // Initialize the implicit object parameter. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8855 | ExprResult ObjRes = |
| 8856 | PerformObjectArgumentInitialization(Object.get(), /*Qualifier=*/0, |
| 8857 | Best->FoundDecl, Method); |
| 8858 | if (ObjRes.isInvalid()) |
| 8859 | IsError = true; |
| 8860 | else |
| 8861 | Object = move(ObjRes); |
| 8862 | TheCall->setArg(0, Object.take()); |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 8863 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8864 | // Check the argument types. |
| 8865 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8866 | Expr *Arg; |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 8867 | if (i < NumArgs) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8868 | Arg = Args[i]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8869 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 8870 | // Pass the argument. |
Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 8871 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8872 | ExprResult InputInit |
Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 8873 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 8874 | Context, |
Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 8875 | Method->getParamDecl(i)), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8876 | SourceLocation(), Arg); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8877 | |
Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 8878 | IsError |= InputInit.isInvalid(); |
| 8879 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 8880 | } else { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8881 | ExprResult DefArg |
Douglas Gregor | 1bc688d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 8882 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 8883 | if (DefArg.isInvalid()) { |
| 8884 | IsError = true; |
| 8885 | break; |
| 8886 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8887 | |
Douglas Gregor | 1bc688d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 8888 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 8889 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8890 | |
| 8891 | TheCall->setArg(i + 1, Arg); |
| 8892 | } |
| 8893 | |
| 8894 | // If this is a variadic call, handle args passed through "...". |
| 8895 | if (Proto->isVariadic()) { |
| 8896 | // Promote the arguments (C99 6.5.2.2p7). |
| 8897 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8898 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0); |
| 8899 | IsError |= Arg.isInvalid(); |
| 8900 | TheCall->setArg(i + 1, Arg.take()); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8901 | } |
| 8902 | } |
| 8903 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 8904 | if (IsError) return true; |
| 8905 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8906 | if (CheckFunctionCall(Method, TheCall)) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 8907 | return true; |
| 8908 | |
John McCall | e172be5 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 8909 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8910 | } |
| 8911 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 8912 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8913 | /// (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] | 8914 | /// @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] | 8915 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8916 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc) { |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8917 | assert(Base->getType()->isRecordType() && |
| 8918 | "left-hand side must have class type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8919 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8920 | if (Base->getObjectKind() == OK_ObjCProperty) { |
| 8921 | ExprResult Result = ConvertPropertyForRValue(Base); |
| 8922 | if (Result.isInvalid()) |
| 8923 | return ExprError(); |
| 8924 | Base = Result.take(); |
| 8925 | } |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8926 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8927 | SourceLocation Loc = Base->getExprLoc(); |
| 8928 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 8929 | // C++ [over.ref]p1: |
| 8930 | // |
| 8931 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 8932 | // for a class object x of type T if T::operator->() exists and if |
| 8933 | // the operator is selected as the best match function by the |
| 8934 | // overload resolution mechanism (13.3). |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8935 | DeclarationName OpName = |
| 8936 | Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8937 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 8938 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 8939 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8940 | if (RequireCompleteType(Loc, Base->getType(), |
Eli Friedman | 132e70b | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 8941 | PDiag(diag::err_typecheck_incomplete_tag) |
| 8942 | << Base->getSourceRange())) |
| 8943 | return ExprError(); |
| 8944 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8945 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 8946 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 8947 | R.suppressDiagnostics(); |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 8948 | |
| 8949 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 8950 | Oper != OperEnd; ++Oper) { |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 8951 | AddMethodCandidate(Oper.getPair(), Base->getType(), Base->Classify(Context), |
| 8952 | 0, 0, CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 8953 | } |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 8954 | |
| 8955 | // Perform overload resolution. |
| 8956 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8957 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 8958 | case OR_Success: |
| 8959 | // Overload resolution succeeded; we'll build the call below. |
| 8960 | break; |
| 8961 | |
| 8962 | case OR_No_Viable_Function: |
| 8963 | if (CandidateSet.empty()) |
| 8964 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 8965 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 8966 | else |
| 8967 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 8968 | << "operator->" << Base->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8969 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 8970 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 8971 | |
| 8972 | case OR_Ambiguous: |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 8973 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 8974 | << "->" << Base->getType() << Base->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8975 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, &Base, 1); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 8976 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8977 | |
| 8978 | case OR_Deleted: |
| 8979 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 8980 | << Best->Function->isDeleted() |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 8981 | << "->" |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 8982 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 8983 | << Base->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8984 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 8985 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 8986 | } |
| 8987 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8988 | MarkDeclarationReferenced(OpLoc, Best->Function); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8989 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8990 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8991 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 8992 | // Convert the object parameter. |
| 8993 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8994 | ExprResult BaseResult = |
| 8995 | PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 8996 | Best->FoundDecl, Method); |
| 8997 | if (BaseResult.isInvalid()) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 8998 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8999 | Base = BaseResult.take(); |
Douglas Gregor | 9ecea26 | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 9000 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9001 | // Build the operator call. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9002 | ExprResult FnExpr = CreateFunctionRefExpr(*this, Method); |
| 9003 | if (FnExpr.isInvalid()) |
| 9004 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9005 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9006 | QualType ResultTy = Method->getResultType(); |
| 9007 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 9008 | ResultTy = ResultTy.getNonLValueExprType(Context); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9009 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9010 | new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr.take(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9011 | &Base, 1, ResultTy, VK, OpLoc); |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 9012 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9013 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall, |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 9014 | Method)) |
| 9015 | return ExprError(); |
Eli Friedman | 2d9c47e | 2011-04-04 01:18:25 +0000 | [diff] [blame] | 9016 | |
| 9017 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9018 | } |
| 9019 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9020 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 9021 | /// a C++ overloaded function (possibly with some parentheses and |
| 9022 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 9023 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | fcb4ab4 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 9024 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 9025 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9026 | FunctionDecl *Fn) { |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9027 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9028 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 9029 | Found, Fn); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9030 | if (SubExpr == PE->getSubExpr()) |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9031 | return PE; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9032 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9033 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9034 | } |
| 9035 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9036 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9037 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 9038 | Found, Fn); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9039 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9040 | SubExpr->getType()) && |
Douglas Gregor | 091f042 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 9041 | "Implicit cast type cannot be determined from overload"); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 9042 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9043 | if (SubExpr == ICE->getSubExpr()) |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9044 | return ICE; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9045 | |
| 9046 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 9047 | ICE->getCastKind(), |
| 9048 | SubExpr, 0, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 9049 | ICE->getValueKind()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9050 | } |
| 9051 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9052 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9053 | assert(UnOp->getOpcode() == UO_AddrOf && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9054 | "Can only take the address of an overloaded function"); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 9055 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 9056 | if (Method->isStatic()) { |
| 9057 | // Do nothing: static member functions aren't any different |
| 9058 | // from non-member functions. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9059 | } else { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 9060 | // Fix the sub expression, which really has to be an |
| 9061 | // UnresolvedLookupExpr holding an overloaded member function |
| 9062 | // or template. |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9063 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 9064 | Found, Fn); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9065 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9066 | return UnOp; |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9067 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9068 | assert(isa<DeclRefExpr>(SubExpr) |
| 9069 | && "fixed to something other than a decl ref"); |
| 9070 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 9071 | && "fixed to a member ref with no nested name qualifier"); |
| 9072 | |
| 9073 | // We have taken the address of a pointer to member |
| 9074 | // function. Perform the computation here so that we get the |
| 9075 | // appropriate pointer to member type. |
| 9076 | QualType ClassType |
| 9077 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 9078 | QualType MemPtrType |
| 9079 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 9080 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9081 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, MemPtrType, |
| 9082 | VK_RValue, OK_Ordinary, |
| 9083 | UnOp->getOperatorLoc()); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 9084 | } |
| 9085 | } |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9086 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 9087 | Found, Fn); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9088 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9089 | return UnOp; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9090 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9091 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9092 | Context.getPointerType(SubExpr->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9093 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9094 | UnOp->getOperatorLoc()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9095 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9096 | |
| 9097 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9098 | // FIXME: avoid copy. |
| 9099 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 9100 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9101 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 9102 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 9103 | } |
| 9104 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9105 | return DeclRefExpr::Create(Context, |
Douglas Gregor | ea972d3 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 9106 | ULE->getQualifierLoc(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9107 | Fn, |
| 9108 | ULE->getNameLoc(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9109 | Fn->getType(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9110 | VK_LValue, |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9111 | TemplateArgs); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9112 | } |
| 9113 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9114 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 9115 | // FIXME: avoid copy. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9116 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 9117 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 9118 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 9119 | TemplateArgs = &TemplateArgsBuffer; |
| 9120 | } |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 9121 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9122 | Expr *Base; |
| 9123 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9124 | // If we're filling in a static method where we used to have an |
| 9125 | // implicit member access, rewrite to a simple decl ref. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9126 | if (MemExpr->isImplicitAccess()) { |
| 9127 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 9128 | return DeclRefExpr::Create(Context, |
Douglas Gregor | ea972d3 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 9129 | MemExpr->getQualifierLoc(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9130 | Fn, |
| 9131 | MemExpr->getMemberLoc(), |
| 9132 | Fn->getType(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9133 | VK_LValue, |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9134 | TemplateArgs); |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 9135 | } else { |
| 9136 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 9137 | if (MemExpr->getQualifier()) |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 9138 | Loc = MemExpr->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 9139 | Base = new (Context) CXXThisExpr(Loc, |
| 9140 | MemExpr->getBaseType(), |
| 9141 | /*isImplicit=*/true); |
| 9142 | } |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9143 | } else |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9144 | Base = MemExpr->getBase(); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9145 | |
| 9146 | return MemberExpr::Create(Context, Base, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9147 | MemExpr->isArrow(), |
Douglas Gregor | ea972d3 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 9148 | MemExpr->getQualifierLoc(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9149 | Fn, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9150 | Found, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9151 | MemExpr->getMemberNameInfo(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9152 | TemplateArgs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9153 | Fn->getType(), |
| 9154 | cast<CXXMethodDecl>(Fn)->isStatic() |
| 9155 | ? VK_LValue : VK_RValue, |
| 9156 | OK_Ordinary); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9157 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9158 | |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9159 | llvm_unreachable("Invalid reference to overloaded function"); |
| 9160 | return E; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9161 | } |
| 9162 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9163 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9164 | DeclAccessPair Found, |
| 9165 | FunctionDecl *Fn) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9166 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 9167 | } |
| 9168 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9169 | } // end namespace clang |