Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1 | //===--- SemaOverload.cpp - C++ Overloading ---------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides Sema routines for C++ overloading. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Lookup.h" |
| 16 | #include "clang/Sema/Initialization.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Template.h" |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 18 | #include "clang/Sema/TemplateDeduction.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 22 | #include "clang/AST/CXXInheritance.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 24 | #include "clang/AST/Expr.h" |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeOrdering.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | 661b493 | 2010-09-12 04:28:07 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseSet.h" |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 32 | #include <algorithm> |
| 33 | |
| 34 | namespace clang { |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 35 | using namespace sema; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 36 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 37 | /// A convenience routine for creating a decayed reference to a |
| 38 | /// function. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 39 | static ExprResult |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 40 | CreateFunctionRefExpr(Sema &S, FunctionDecl *Fn, |
| 41 | SourceLocation Loc = SourceLocation()) { |
John Wiegley | 429bb27 | 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 | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 47 | } |
| 48 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 49 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 50 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 51 | StandardConversionSequence &SCS, |
| 52 | bool CStyle); |
Fariborz Jahanian | d97f558 | 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 | 120d63c | 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 | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | ImplicitConversionCategory |
Douglas Gregor | 8e9bebd | 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 | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 93 | ICC_Identity, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 94 | ICC_Qualification_Adjustment, |
| 95 | ICC_Promotion, |
| 96 | ICC_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 97 | ICC_Promotion, |
| 98 | ICC_Conversion, |
| 99 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 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 | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 105 | ICC_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 106 | ICC_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 107 | ICC_Conversion, |
| 108 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 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 | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 124 | ICR_Exact_Match, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 125 | ICR_Promotion, |
| 126 | ICR_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 127 | ICR_Promotion, |
| 128 | ICR_Conversion, |
| 129 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 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 | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 135 | ICR_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 136 | ICR_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 137 | ICR_Conversion, |
| 138 | ICR_Conversion, |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 139 | ICR_Complex_Real_Conversion, |
| 140 | ICR_Conversion, |
| 141 | ICR_Conversion |
Douglas Gregor | 8e9bebd | 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 | 2550d70 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 149 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 8e9bebd | 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 | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 154 | "Noreturn adjustment", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 155 | "Qualification", |
| 156 | "Integral promotion", |
| 157 | "Floating point promotion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 158 | "Complex promotion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 159 | "Integral conversion", |
| 160 | "Floating conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 161 | "Complex conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 162 | "Floating-integral conversion", |
| 163 | "Pointer conversion", |
| 164 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 165 | "Boolean conversion", |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 166 | "Compatible-types conversion", |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 167 | "Derived-to-base conversion", |
| 168 | "Vector conversion", |
| 169 | "Vector splat", |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 170 | "Complex-real conversion", |
| 171 | "Block Pointer conversion", |
| 172 | "Transparent Union Conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 173 | }; |
| 174 | return Name[Kind]; |
| 175 | } |
| 176 | |
Douglas Gregor | 60d62c2 | 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 | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 183 | DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 184 | ReferenceBinding = false; |
| 185 | DirectBinding = false; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 186 | IsLvalueReference = true; |
| 187 | BindsToFunctionLvalue = false; |
| 188 | BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 189 | BindsImplicitObjectArgumentWithoutRefQualifier = false; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 190 | CopyConstructor = 0; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Douglas Gregor | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 210 | /// (C++ 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 8e9bebd | 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 | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 216 | if (getToType(1)->isBooleanType() && |
John McCall | ddb0ce7 | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 217 | (getFromType()->isPointerType() || |
| 218 | getFromType()->isObjCObjectPointerType() || |
| 219 | getFromType()->isBlockPointerType() || |
Anders Carlsson | c8df0b6 | 2010-11-05 00:12:09 +0000 | [diff] [blame] | 220 | getFromType()->isNullPtrType() || |
Douglas Gregor | 8e9bebd | 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 | bc0805a | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | bool |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 232 | StandardConversionSequence:: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 234 | QualType FromType = getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 235 | QualType ToType = getToType(1); |
Douglas Gregor | bc0805a | 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 | |
Douglas Gregor | f9af524 | 2011-04-15 20:45:44 +0000 | [diff] [blame] | 243 | if (Second == ICK_Pointer_Conversion && FromType->isAnyPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 244 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 245 | return ToPtrType->getPointeeType()->isVoidType(); |
| 246 | |
| 247 | return false; |
| 248 | } |
| 249 | |
Douglas Gregor | 8e9bebd | 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 | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 253 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 254 | bool PrintedSomething = false; |
| 255 | if (First != ICK_Identity) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 256 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 257 | PrintedSomething = true; |
| 258 | } |
| 259 | |
| 260 | if (Second != ICK_Identity) { |
| 261 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 262 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 263 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 264 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 265 | |
| 266 | if (CopyConstructor) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 267 | OS << " (by copy constructor)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 268 | } else if (DirectBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 269 | OS << " (direct reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 270 | } else if (ReferenceBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 271 | OS << " (reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 272 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 273 | PrintedSomething = true; |
| 274 | } |
| 275 | |
| 276 | if (Third != ICK_Identity) { |
| 277 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 278 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 279 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 280 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 281 | PrintedSomething = true; |
| 282 | } |
| 283 | |
| 284 | if (!PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 285 | OS << "No conversions required"; |
Douglas Gregor | 8e9bebd | 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 | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 292 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 293 | if (Before.First || Before.Second || Before.Third) { |
| 294 | Before.DebugPrint(); |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 295 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 296 | } |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 297 | OS << '\'' << ConversionFunction << '\''; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 298 | if (After.First || After.Second || After.Third) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 299 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 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 | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 307 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 308 | switch (ConversionKind) { |
| 309 | case StandardConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 310 | OS << "Standard conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 311 | Standard.DebugPrint(); |
| 312 | break; |
| 313 | case UserDefinedConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 314 | OS << "User-defined conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 315 | UserDefined.DebugPrint(); |
| 316 | break; |
| 317 | case EllipsisConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 318 | OS << "Ellipsis conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 319 | break; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 320 | case AmbiguousConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 321 | OS << "Ambiguous conversion"; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 322 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 323 | case BadConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 324 | OS << "Bad conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 325 | break; |
| 326 | } |
| 327 | |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 328 | OS << "\n"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 329 | } |
| 330 | |
John McCall | 1d31833 | 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 | a933319 | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 355 | |
Douglas Gregor | a933319 | 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 | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 359 | static MakeDeductionFailureInfo(ASTContext &Context, |
| 360 | Sema::TemplateDeductionResult TDK, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 361 | TemplateDeductionInfo &Info) { |
Douglas Gregor | a933319 | 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 | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 368 | case Sema::TDK_TooManyArguments: |
| 369 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 370 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 371 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 372 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 373 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 374 | Result.Data = Info.Param.getOpaqueValue(); |
| 375 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 376 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 377 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 378 | case Sema::TDK_Underqualified: { |
Douglas Gregor | ff5adac | 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 | a933319 | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 387 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 388 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 389 | Result.Data = Info.take(); |
| 390 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 391 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 392 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 393 | case Sema::TDK_FailedOverloadResolution: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 394 | break; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 395 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 396 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 397 | return Result; |
| 398 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 399 | |
Douglas Gregor | a933319 | 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 | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 405 | case Sema::TDK_TooManyArguments: |
| 406 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 407 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 408 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 409 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 410 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 411 | case Sema::TDK_Underqualified: |
Douglas Gregor | aaa045d | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 412 | // FIXME: Destroy the data? |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 413 | Data = 0; |
| 414 | break; |
Douglas Gregor | ec20f46 | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 420 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 421 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 422 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 423 | case Sema::TDK_FailedOverloadResolution: |
| 424 | break; |
| 425 | } |
| 426 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 427 | |
| 428 | TemplateParameter |
Douglas Gregor | a933319 | 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 | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 433 | case Sema::TDK_TooManyArguments: |
| 434 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 435 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 436 | return TemplateParameter(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 437 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 438 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 439 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 440 | return TemplateParameter::getFromOpaqueValue(Data); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 441 | |
| 442 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 443 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 444 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 445 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 446 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 447 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 448 | case Sema::TDK_FailedOverloadResolution: |
| 449 | break; |
| 450 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 451 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 452 | return TemplateParameter(); |
| 453 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 454 | |
Douglas Gregor | ec20f46 | 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 | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 465 | case Sema::TDK_Underqualified: |
Douglas Gregor | ec20f46 | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 470 | |
Douglas Gregor | ec20f46 | 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 | a933319 | 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 | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 485 | case Sema::TDK_TooManyArguments: |
| 486 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 487 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 488 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 489 | return 0; |
| 490 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 491 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 492 | case Sema::TDK_Underqualified: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 493 | return &static_cast<DFIParamWithArguments*>(Data)->FirstArg; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 494 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 495 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 496 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 497 | case Sema::TDK_FailedOverloadResolution: |
| 498 | break; |
| 499 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 500 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 501 | return 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 502 | } |
Douglas Gregor | a933319 | 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 | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 510 | case Sema::TDK_TooManyArguments: |
| 511 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 512 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 513 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 514 | return 0; |
| 515 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 516 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 517 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 518 | return &static_cast<DFIParamWithArguments*>(Data)->SecondArg; |
| 519 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 520 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 521 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 522 | case Sema::TDK_FailedOverloadResolution: |
| 523 | break; |
| 524 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 525 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | void OverloadCandidateSet::clear() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 530 | inherited::clear(); |
| 531 | Functions.clear(); |
| 532 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 533 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 534 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 51fa86f | 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 | 871b2e7 | 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 | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 550 | // so IsOverload will not be used. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 551 | // |
John McCall | 51fa86f | 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 | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 556 | // |
John McCall | 51fa86f | 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 | 8e9bebd | 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 | ad00b77 | 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 | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 568 | Sema::OverloadKind |
John McCall | ad00b77 | 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 | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 571 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 572 | I != E; ++I) { |
John McCall | ad00b77 | 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 | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 594 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | ad00b77 | 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 | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 601 | Match = *I; |
| 602 | return Ovl_Match; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 603 | } |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 604 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | ad00b77 | 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 | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 611 | Match = *I; |
| 612 | return Ovl_Match; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 613 | } |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 614 | } else if (isa<UsingDecl>(OldD)) { |
John McCall | 9f54ad4 | 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 | d7945c6 | 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 | 9f54ad4 | 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 | 6826314 | 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 | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 628 | Match = *I; |
| 629 | return Ovl_NonFunction; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 630 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 631 | } |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 632 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 633 | return Ovl_Overload; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 634 | } |
| 635 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 636 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| 637 | bool UseUsingDeclRules) { |
John McCall | 7b49202 | 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 | 6826314 | 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 | f4c7371 | 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 | 6826314 | 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 | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 675 | !FunctionArgTypesAreEqual(OldType, NewType))) |
John McCall | 6826314 | 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 | ad00b77 | 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 | 6826314 | 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 | 57c9f4f | 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 | 6826314 | 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 | 57c9f4f | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 709 | (OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers() || |
Douglas Gregor | b145ee6 | 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 | dfbb02a | 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 | b145ee6 | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 725 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 726 | return true; |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 727 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 728 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 729 | // The signatures match; this is not an overload. |
| 730 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Douglas Gregor | 27c8dc0 | 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 | 8e9bebd | 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 | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 751 | /// |
| 752 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 753 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 754 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 755 | /// permitted. |
John McCall | 120d63c | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 759 | bool AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 760 | bool InOverloadResolution, |
| 761 | bool CStyle) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 762 | ImplicitConversionSequence ICS; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 763 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 764 | ICS.Standard, CStyle)) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 765 | ICS.setStandard(); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 766 | return ICS; |
| 767 | } |
| 768 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 769 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 770 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 771 | return ICS; |
| 772 | } |
| 773 | |
Douglas Gregor | 604eb65 | 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 | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 783 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
| 784 | S.IsDerivedFrom(FromType, ToType))) { |
Douglas Gregor | 3fbaf3e | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 789 | |
Douglas Gregor | 3fbaf3e | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 795 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 796 | // Determine whether this is considered a derived-to-base conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 797 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 798 | ICS.Standard.Second = ICK_Derived_To_Base; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 799 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 800 | return ICS; |
| 801 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 802 | |
Douglas Gregor | 604eb65 | 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 | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 807 | return ICS; |
| 808 | } |
| 809 | |
| 810 | // Attempt user-defined conversion. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 811 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 812 | OverloadingResult UserDefResult |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 813 | = IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, Conversions, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 814 | AllowExplicit); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 815 | |
| 816 | if (UserDefResult == OR_Success) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 817 | ICS.setUserDefined(); |
Douglas Gregor | 396b7cd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 826 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 827 | QualType FromCanon |
John McCall | 120d63c | 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 | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 831 | if (Constructor->isCopyConstructor() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 832 | (FromCanon == ToCanon || S.IsDerivedFrom(FromCanon, ToCanon))) { |
Douglas Gregor | 225c41e | 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 | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 835 | ICS.setStandard(); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 836 | ICS.Standard.setAsIdentityConversion(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 837 | ICS.Standard.setFromType(From->getType()); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 838 | ICS.Standard.setAllToTypes(ToType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 839 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 840 | if (ToCanon != FromCanon) |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 841 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 842 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 843 | } |
Douglas Gregor | 734d986 | 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 | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 852 | if (SuppressUserConversions && ICS.isUserDefined()) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 853 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 854 | } |
John McCall | cefd3ad | 2010-01-13 22:30:33 +0000 | [diff] [blame] | 855 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
John McCall | 1d31833 | 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 | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 863 | } else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 864 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 865 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 866 | |
| 867 | return ICS; |
| 868 | } |
| 869 | |
John McCall | 120d63c | 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 | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 875 | bool InOverloadResolution, |
| 876 | bool CStyle) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 877 | ImplicitConversionSequence ICS |
| 878 | = clang::TryImplicitConversion(*this, Initializer, Entity.getType(), |
| 879 | SuppressUserConversions, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 880 | AllowExplicitConversions, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 881 | InOverloadResolution, |
| 882 | CStyle); |
John McCall | 120d63c | 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 | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 890 | /// PerformImplicitConversion - Perform an implicit conversion of the |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 891 | /// expression From to the type ToType. Returns the |
Douglas Gregor | 575c63a | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 895 | ExprResult |
| 896 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 575c63a | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 902 | ExprResult |
| 903 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 904 | AssignmentAction Action, bool AllowExplicit, |
| 905 | ImplicitConversionSequence& ICS) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 906 | ICS = clang::TryImplicitConversion(*this, From, ToType, |
| 907 | /*SuppressUserConversions=*/false, |
| 908 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 909 | /*InOverloadResolution=*/false, |
| 910 | /*CStyle=*/false); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 911 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 912 | } |
NAKAMURA Takumi | dfbb02a | 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 | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 915 | /// conversion that strips "noreturn" off the nested function type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 916 | static bool IsNoReturnConversion(ASTContext &Context, QualType FromType, |
Douglas Gregor | 43c79c2 | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 920 | |
John McCall | 00ccbef | 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 | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 943 | |
John McCall | 00ccbef | 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 | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 959 | return true; |
| 960 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 961 | |
Douglas Gregor | fb4a543 | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 967 | static bool IsVectorConversion(ASTContext &Context, QualType FromType, |
| 968 | QualType ToType, ImplicitConversionKind &ICK) { |
Douglas Gregor | fb4a543 | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 984 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 985 | // Vector splat from any arithmetic type to a vector. |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 986 | if (FromType->isArithmeticType()) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 987 | ICK = ICK_Vector_Splat; |
| 988 | return true; |
| 989 | } |
| 990 | } |
Douglas Gregor | 255210e | 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 | c45eb9c | 2010-08-08 05:02:51 +0000 | [diff] [blame] | 998 | (Context.getLangOptions().LaxVectorConversions && |
| 999 | (Context.getTypeSize(FromType) == Context.getTypeSize(ToType)))) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1000 | ICK = ICK_Vector_Conversion; |
| 1001 | return true; |
| 1002 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1003 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1004 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1005 | return false; |
| 1006 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1007 | |
Douglas Gregor | 60d62c2 | 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 | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1016 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 1017 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1018 | StandardConversionSequence &SCS, |
| 1019 | bool CStyle) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1020 | QualType FromType = From->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1021 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1022 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1023 | SCS.setAsIdentityConversion(); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1024 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1025 | SCS.IncompatibleObjC = false; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1026 | SCS.setFromType(FromType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1027 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1028 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1029 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1030 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1031 | if (FromType->isRecordType() || ToType->isRecordType()) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1032 | if (S.getLangOptions().CPlusPlus) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1033 | return false; |
| 1034 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1035 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Douglas Gregor | 8e9bebd | 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 | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1042 | if (FromType == S.Context.OverloadTy) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1043 | DeclAccessPair AccessPair; |
| 1044 | if (FunctionDecl *Fn |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1045 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1046 | AccessPair)) { |
Douglas Gregor | ad4e02f | 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 | 1be8eec | 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 | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1062 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1063 | |
Chandler Carruth | 9043423 | 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 | fc5c8fc | 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 | 9043423 | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1084 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1085 | // Check that we've computed the proper type after overload resolution. |
Chandler Carruth | 9043423 | 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 | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1089 | } else { |
| 1090 | return false; |
| 1091 | } |
Anders Carlsson | 2bd6250 | 2010-11-04 05:28:09 +0000 | [diff] [blame] | 1092 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1093 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 8e9bebd | 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 | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1096 | bool argIsLValue = From->isLValue(); |
| 1097 | if (argIsLValue && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1098 | !FromType->isFunctionType() && !FromType->isArrayType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1099 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1100 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 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 | f9201e0 | 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 | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1106 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1107 | } else if (FromType->isArrayType()) { |
| 1108 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1109 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 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 | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1114 | FromType = S.Context.getArrayDecayedType(FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1115 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1116 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1117 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1118 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 8e9bebd | 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 | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1124 | SCS.Second = ICK_Identity; |
| 1125 | SCS.Third = ICK_Qualification; |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1126 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1127 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1128 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1129 | } else if (FromType->isFunctionType() && argIsLValue) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1130 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1131 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 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 | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1136 | FromType = S.Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1137 | } else { |
| 1138 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1139 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1140 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1141 | SCS.setToType(0, FromType); |
Douglas Gregor | 8e9bebd | 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 | f9201e0 | 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 | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1149 | bool IncompatibleObjC = false; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1150 | ImplicitConversionKind SecondICK = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1151 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 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 | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1154 | SCS.Second = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1155 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1156 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1157 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1158 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1159 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1160 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1161 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1162 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1163 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1164 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1165 | SCS.Second = ICK_Complex_Promotion; |
| 1166 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 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 | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1176 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1177 | ToType->isIntegralType(S.Context)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1178 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1179 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1180 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1181 | } else if (FromType->isAnyComplexType() && ToType->isComplexType()) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1182 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1183 | SCS.Second = ICK_Complex_Conversion; |
| 1184 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1185 | } else if ((FromType->isAnyComplexType() && ToType->isArithmeticType()) || |
| 1186 | (ToType->isAnyComplexType() && FromType->isArithmeticType())) { |
Chandler Carruth | 23a370f | 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 | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1190 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
Chandler Carruth | 23a370f | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1194 | } else if ((FromType->isRealFloatingType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1195 | ToType->isIntegralType(S.Context)) || |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1196 | (FromType->isIntegralOrUnscopedEnumerationType() && |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1197 | ToType->isRealFloatingType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1198 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1199 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1200 | FromType = ToType.getUnqualifiedType(); |
Fariborz Jahanian | e3c8c64 | 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 | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1203 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1204 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1205 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1206 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1207 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1208 | FromType = FromType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1209 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1210 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1211 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1212 | SCS.Second = ICK_Pointer_Member; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1213 | } else if (IsVectorConversion(S.Context, FromType, ToType, SecondICK)) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1214 | SCS.Second = SecondICK; |
| 1215 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1216 | } else if (!S.getLangOptions().CPlusPlus && |
| 1217 | S.Context.typesAreCompatible(ToType, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1218 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1219 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1220 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1221 | } else if (IsNoReturnConversion(S.Context, FromType, ToType, FromType)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1222 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1223 | SCS.Second = ICK_NoReturn_Adjustment; |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1224 | } else if (IsTransparentUnionStandardConversion(S, From, ToType, |
| 1225 | InOverloadResolution, |
| 1226 | SCS, CStyle)) { |
| 1227 | SCS.Second = ICK_TransparentUnionConversion; |
| 1228 | FromType = ToType; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1229 | } else { |
| 1230 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1231 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1232 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1233 | SCS.setToType(1, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1234 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1235 | QualType CanonFrom; |
| 1236 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1237 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1238 | if (S.IsQualificationConversion(FromType, ToType, CStyle)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1239 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1240 | FromType = ToType; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1241 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1242 | CanonTo = S.Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1243 | } else { |
| 1244 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1245 | SCS.Third = ICK_Identity; |
| 1246 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1247 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1248 | // [...] Any difference in top-level cv-qualification is |
| 1249 | // subsumed by the initialization itself and does not constitute |
| 1250 | // a conversion. [...] |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1251 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1252 | CanonTo = S.Context.getCanonicalType(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1253 | if (CanonFrom.getLocalUnqualifiedType() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1254 | == CanonTo.getLocalUnqualifiedType() && |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 1255 | (CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers() |
| 1256 | || CanonFrom.getObjCGCAttr() != CanonTo.getObjCGCAttr())) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1257 | FromType = ToType; |
| 1258 | CanonFrom = CanonTo; |
| 1259 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1260 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1261 | SCS.setToType(2, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1262 | |
| 1263 | // If we have not converted the argument type to the parameter type, |
| 1264 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1265 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1266 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1267 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1268 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1269 | } |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1270 | |
| 1271 | static bool |
| 1272 | IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 1273 | QualType &ToType, |
| 1274 | bool InOverloadResolution, |
| 1275 | StandardConversionSequence &SCS, |
| 1276 | bool CStyle) { |
| 1277 | |
| 1278 | const RecordType *UT = ToType->getAsUnionType(); |
| 1279 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 1280 | return false; |
| 1281 | // The field to initialize within the transparent union. |
| 1282 | RecordDecl *UD = UT->getDecl(); |
| 1283 | // It's compatible if the expression matches any of the fields. |
| 1284 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1285 | itend = UD->field_end(); |
| 1286 | it != itend; ++it) { |
| 1287 | if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS, CStyle)) { |
| 1288 | ToType = it->getType(); |
| 1289 | return true; |
| 1290 | } |
| 1291 | } |
| 1292 | return false; |
| 1293 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1294 | |
| 1295 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1296 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1297 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1298 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1299 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1300 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1301 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1302 | if (!To) { |
| 1303 | return false; |
| 1304 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1305 | |
| 1306 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1307 | // unsigned short int can be converted to an rvalue of type int if |
| 1308 | // int can represent all the values of the source type; otherwise, |
| 1309 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1310 | // int (C++ 4.5p1). |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1311 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1312 | !FromType->isEnumeralType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1313 | if (// We can promote any signed, promotable integer type to an int |
| 1314 | (FromType->isSignedIntegerType() || |
| 1315 | // We can promote any unsigned integer type whose size is |
| 1316 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1317 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1318 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1319 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1322 | return To->getKind() == BuiltinType::UInt; |
| 1323 | } |
| 1324 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1325 | // C++0x [conv.prom]p3: |
| 1326 | // A prvalue of an unscoped enumeration type whose underlying type is not |
| 1327 | // fixed (7.2) can be converted to an rvalue a prvalue of the first of the |
| 1328 | // following types that can represent all the values of the enumeration |
| 1329 | // (i.e., the values in the range bmin to bmax as described in 7.2): int, |
| 1330 | // unsigned int, long int, unsigned long int, long long int, or unsigned |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1331 | // long long int. If none of the types in that list can represent all the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1332 | // values of the enumeration, an rvalue a prvalue of an unscoped enumeration |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1333 | // type can be converted to an rvalue a prvalue of the extended integer type |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1334 | // with lowest integer conversion rank (4.13) greater than the rank of long |
| 1335 | // long in which all the values of the enumeration can be represented. If |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1336 | // there are two such extended types, the signed one is chosen. |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1337 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
| 1338 | // C++0x 7.2p9: Note that this implicit enum to int conversion is not |
| 1339 | // provided for a scoped enumeration. |
| 1340 | if (FromEnumType->getDecl()->isScoped()) |
| 1341 | return false; |
| 1342 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1343 | // We have already pre-calculated the promotion type, so this is trivial. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1344 | if (ToType->isIntegerType() && |
Douglas Gregor | 5dde160 | 2010-09-12 03:38:25 +0000 | [diff] [blame] | 1345 | !RequireCompleteType(From->getLocStart(), FromType, PDiag())) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1346 | return Context.hasSameUnqualifiedType(ToType, |
| 1347 | FromEnumType->getDecl()->getPromotionType()); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1348 | } |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1349 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1350 | // C++0x [conv.prom]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1351 | // A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted |
| 1352 | // to an rvalue a prvalue of the first of the following types that can |
| 1353 | // represent all the values of its underlying type: int, unsigned int, |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1354 | // long int, unsigned long int, long long int, or unsigned long long int. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1355 | // If none of the types in that list can represent all the values of its |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1356 | // underlying type, an rvalue a prvalue of type char16_t, char32_t, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1357 | // or wchar_t can be converted to an rvalue a prvalue of its underlying |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1358 | // type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1359 | if (FromType->isAnyCharacterType() && !FromType->isCharType() && |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1360 | ToType->isIntegerType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1361 | // Determine whether the type we're converting from is signed or |
| 1362 | // unsigned. |
| 1363 | bool FromIsSigned; |
| 1364 | uint64_t FromSize = Context.getTypeSize(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1365 | |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1366 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 1367 | FromIsSigned = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1368 | |
| 1369 | // The types we'll try to promote to, in the appropriate |
| 1370 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1371 | QualType PromoteTypes[6] = { |
| 1372 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1373 | Context.LongTy, Context.UnsignedLongTy , |
| 1374 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1375 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1376 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1377 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1378 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1379 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1380 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1381 | // We found the type that we can promote to. If this is the |
| 1382 | // type we wanted, we have a promotion. Otherwise, no |
| 1383 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1384 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1385 | } |
| 1386 | } |
| 1387 | } |
| 1388 | |
| 1389 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1390 | // rvalue of type int if int can represent all the values of the |
| 1391 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1392 | // unsigned int can represent all the values of the bit-field. If |
| 1393 | // the bit-field is larger yet, no integral promotion applies to |
| 1394 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1395 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1396 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1397 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1398 | using llvm::APSInt; |
| 1399 | if (From) |
| 1400 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1401 | APSInt BitWidth; |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1402 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1403 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1404 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1405 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1406 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1407 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1408 | if (BitWidth < ToSize || |
| 1409 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1410 | return To->getKind() == BuiltinType::Int; |
| 1411 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1413 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1414 | // that fits into an unsigned int? |
| 1415 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1416 | return To->getKind() == BuiltinType::UInt; |
| 1417 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1419 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1420 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1421 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1422 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1423 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1424 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1425 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1426 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1427 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1428 | |
| 1429 | return false; |
| 1430 | } |
| 1431 | |
| 1432 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1433 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1434 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1435 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1436 | /// An rvalue of type float can be converted to an rvalue of type |
| 1437 | /// double. (C++ 4.6p1). |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1438 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1439 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1440 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1441 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1442 | return true; |
| 1443 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1444 | // C99 6.3.1.5p1: |
| 1445 | // When a float is promoted to double or long double, or a |
| 1446 | // double is promoted to long double [...]. |
| 1447 | if (!getLangOptions().CPlusPlus && |
| 1448 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1449 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1450 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1451 | return true; |
| 1452 | } |
| 1453 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1454 | return false; |
| 1455 | } |
| 1456 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1457 | /// \brief Determine if a conversion is a complex promotion. |
| 1458 | /// |
| 1459 | /// A complex promotion is defined as a complex -> complex conversion |
| 1460 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1461 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1462 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1463 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1464 | if (!FromComplex) |
| 1465 | return false; |
| 1466 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1467 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1468 | if (!ToComplex) |
| 1469 | return false; |
| 1470 | |
| 1471 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1472 | ToComplex->getElementType()) || |
| 1473 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1474 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1477 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1478 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1479 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1480 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1481 | /// the right set of qualifiers on its pointee. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | static QualType |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1483 | BuildSimilarlyQualifiedPointerType(const Type *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1484 | QualType ToPointee, QualType ToType, |
| 1485 | ASTContext &Context) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1486 | assert((FromPtr->getTypeClass() == Type::Pointer || |
| 1487 | FromPtr->getTypeClass() == Type::ObjCObjectPointer) && |
| 1488 | "Invalid similarly-qualified pointer type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1489 | |
Douglas Gregor | 143c7ac | 2010-12-06 22:09:19 +0000 | [diff] [blame] | 1490 | /// \brief Conversions to 'id' subsume cv-qualifier conversions. |
| 1491 | if (ToType->isObjCIdType() || ToType->isObjCQualifiedIdType()) |
| 1492 | return ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1493 | |
| 1494 | QualType CanonFromPointee |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1495 | = Context.getCanonicalType(FromPtr->getPointeeType()); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1496 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1497 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1498 | |
| 1499 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1500 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1501 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1502 | if (!ToType.isNull()) |
Douglas Gregor | af7bea5 | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1503 | return ToType.getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1504 | |
| 1505 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1506 | // already. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1507 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1508 | return Context.getObjCObjectPointerType(ToPointee); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1509 | return Context.getPointerType(ToPointee); |
| 1510 | } |
| 1511 | |
| 1512 | // Just build a canonical type that has the right qualifiers. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1513 | QualType QualifiedCanonToPointee |
| 1514 | = Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1515 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1516 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1517 | return Context.getObjCObjectPointerType(QualifiedCanonToPointee); |
| 1518 | return Context.getPointerType(QualifiedCanonToPointee); |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1519 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1520 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1521 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1522 | bool InOverloadResolution, |
| 1523 | ASTContext &Context) { |
| 1524 | // Handle value-dependent integral null pointer constants correctly. |
| 1525 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1526 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1527 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1528 | return !InOverloadResolution; |
| 1529 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1530 | return Expr->isNullPointerConstant(Context, |
| 1531 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1532 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1533 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1534 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1535 | /// IsPointerConversion - Determines whether the conversion of the |
| 1536 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1537 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1538 | /// 4.10). If so, returns true and places the converted type (that |
| 1539 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1540 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1541 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1542 | /// This routine also supports conversions to and from block pointers |
| 1543 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1544 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1545 | /// appropriate overloading rules for Objective-C, we may want to |
| 1546 | /// split the Objective-C checks into a different routine; however, |
| 1547 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1548 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1549 | /// set if the conversion is an allowed Objective-C conversion that |
| 1550 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1551 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1552 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1553 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1554 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1555 | IncompatibleObjC = false; |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1556 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, |
| 1557 | IncompatibleObjC)) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1558 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1559 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1560 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1561 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1562 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1563 | ConvertedType = ToType; |
| 1564 | return true; |
| 1565 | } |
| 1566 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1567 | // Blocks: Block pointers can be converted to void*. |
| 1568 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1569 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1570 | ConvertedType = ToType; |
| 1571 | return true; |
| 1572 | } |
| 1573 | // Blocks: A null pointer constant can be converted to a block |
| 1574 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1575 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1576 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1577 | ConvertedType = ToType; |
| 1578 | return true; |
| 1579 | } |
| 1580 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1581 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1582 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1583 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1584 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1585 | ConvertedType = ToType; |
| 1586 | return true; |
| 1587 | } |
| 1588 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1589 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1590 | if (!ToTypePtr) |
| 1591 | return false; |
| 1592 | |
| 1593 | // A null pointer constant can be converted to a pointer type (C++ 4.10p1). |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1594 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1595 | ConvertedType = ToType; |
| 1596 | return true; |
| 1597 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1598 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1599 | // Beyond this point, both types need to be pointers |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1600 | // , including objective-c pointers. |
| 1601 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 1602 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType()) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1603 | ConvertedType = BuildSimilarlyQualifiedPointerType( |
| 1604 | FromType->getAs<ObjCObjectPointerType>(), |
| 1605 | ToPointeeType, |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1606 | ToType, Context); |
| 1607 | return true; |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1608 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1609 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1610 | if (!FromTypePtr) |
| 1611 | return false; |
| 1612 | |
| 1613 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1614 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1615 | // If the unqualified pointee types are the same, this can't be a |
Douglas Gregor | 4e938f57b | 2010-08-18 21:25:30 +0000 | [diff] [blame] | 1616 | // pointer conversion, so don't do all of the work below. |
| 1617 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
| 1618 | return false; |
| 1619 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1620 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1621 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1622 | // 4.10p2). |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1623 | if (FromPointeeType->isIncompleteOrObjectType() && |
| 1624 | ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1625 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1626 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1627 | ToType, Context); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1628 | return true; |
| 1629 | } |
| 1630 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1631 | // When we're overloading in C, we allow a special kind of pointer |
| 1632 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1633 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1634 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1635 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1636 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1637 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1638 | return true; |
| 1639 | } |
| 1640 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1641 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1642 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1643 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1644 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1645 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1646 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1647 | // necessitates this conversion is ill-formed. The result of the |
| 1648 | // conversion is a pointer to the base class sub-object of the |
| 1649 | // derived class object. The null pointer value is converted to |
| 1650 | // the null pointer value of the destination type. |
| 1651 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1652 | // Note that we do not check for ambiguity or inaccessibility |
| 1653 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1654 | if (getLangOptions().CPlusPlus && |
| 1655 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | bf1764c | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 1656 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | 2685eab | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1657 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1658 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1659 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1660 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1661 | ToType, Context); |
| 1662 | return true; |
| 1663 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1664 | |
Fariborz Jahanian | 5da3c08 | 2011-04-14 20:33:36 +0000 | [diff] [blame] | 1665 | if (FromPointeeType->isVectorType() && ToPointeeType->isVectorType() && |
| 1666 | Context.areCompatibleVectorTypes(FromPointeeType, ToPointeeType)) { |
| 1667 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 1668 | ToPointeeType, |
| 1669 | ToType, Context); |
| 1670 | return true; |
| 1671 | } |
| 1672 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1673 | return false; |
| 1674 | } |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1675 | |
| 1676 | /// \brief Adopt the given qualifiers for the given type. |
| 1677 | static QualType AdoptQualifiers(ASTContext &Context, QualType T, Qualifiers Qs){ |
| 1678 | Qualifiers TQs = T.getQualifiers(); |
| 1679 | |
| 1680 | // Check whether qualifiers already match. |
| 1681 | if (TQs == Qs) |
| 1682 | return T; |
| 1683 | |
| 1684 | if (Qs.compatiblyIncludes(TQs)) |
| 1685 | return Context.getQualifiedType(T, Qs); |
| 1686 | |
| 1687 | return Context.getQualifiedType(T.getUnqualifiedType(), Qs); |
| 1688 | } |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1689 | |
| 1690 | /// isObjCPointerConversion - Determines whether this is an |
| 1691 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1692 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1693 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1694 | QualType& ConvertedType, |
| 1695 | bool &IncompatibleObjC) { |
| 1696 | if (!getLangOptions().ObjC1) |
| 1697 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1698 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1699 | // The set of qualifiers on the type we're converting from. |
| 1700 | Qualifiers FromQualifiers = FromType.getQualifiers(); |
| 1701 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1702 | // First, we handle all conversions on ObjC object pointer types. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1703 | const ObjCObjectPointerType* ToObjCPtr = |
| 1704 | ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1705 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1706 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1707 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1708 | if (ToObjCPtr && FromObjCPtr) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1709 | // If the pointee types are the same (ignoring qualifications), |
| 1710 | // then this is not a pointer conversion. |
| 1711 | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
| 1712 | FromObjCPtr->getPointeeType())) |
| 1713 | return false; |
| 1714 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1715 | // Check for compatible |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1716 | // Objective C++: We're able to convert between "id" or "Class" and a |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1717 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1718 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1719 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1720 | return true; |
| 1721 | } |
| 1722 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1723 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1724 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1725 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1726 | /*compare=*/false)) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1727 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1728 | return true; |
| 1729 | } |
| 1730 | // Objective C++: We're able to convert from a pointer to an |
| 1731 | // interface to a pointer to a different interface. |
| 1732 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 1733 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 1734 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
| 1735 | if (getLangOptions().CPlusPlus && LHS && RHS && |
| 1736 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 1737 | FromObjCPtr->getPointeeType())) |
| 1738 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1739 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1740 | ToObjCPtr->getPointeeType(), |
| 1741 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1742 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1743 | return true; |
| 1744 | } |
| 1745 | |
| 1746 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1747 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1748 | // interfaces, which is permitted. However, we're going to |
| 1749 | // complain about it. |
| 1750 | IncompatibleObjC = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1751 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1752 | ToObjCPtr->getPointeeType(), |
| 1753 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1754 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1755 | return true; |
| 1756 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1757 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1758 | // Beyond this point, both types need to be C pointers or block pointers. |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1759 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1760 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1761 | ToPointeeType = ToCPtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1762 | else if (const BlockPointerType *ToBlockPtr = |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1763 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 1764 | // Objective C++: We're able to convert from a pointer to any object |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1765 | // to a block pointer type. |
| 1766 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1767 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1768 | return true; |
| 1769 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1770 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1771 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1772 | else if (FromType->getAs<BlockPointerType>() && |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1773 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1774 | // Objective C++: We're able to convert from a block pointer type to a |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 1775 | // pointer to any object. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1776 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1777 | return true; |
| 1778 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1779 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1780 | return false; |
| 1781 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1782 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1783 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1784 | FromPointeeType = FromCPtr->getPointeeType(); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1785 | else if (const BlockPointerType *FromBlockPtr = |
| 1786 | FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1787 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1788 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1789 | return false; |
| 1790 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1791 | // If we have pointers to pointers, recursively check whether this |
| 1792 | // is an Objective-C conversion. |
| 1793 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1794 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1795 | IncompatibleObjC)) { |
| 1796 | // We always complain about this conversion. |
| 1797 | IncompatibleObjC = true; |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1798 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1799 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1800 | return true; |
| 1801 | } |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1802 | // Allow conversion of pointee being objective-c pointer to another one; |
| 1803 | // as in I* to id. |
| 1804 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 1805 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 1806 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1807 | IncompatibleObjC)) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1808 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1809 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1810 | return true; |
| 1811 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1812 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1813 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1814 | // differences in the argument and result types are in Objective-C |
| 1815 | // pointer conversions. If so, we permit the conversion (but |
| 1816 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1817 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1818 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1819 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1820 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1821 | if (FromFunctionType && ToFunctionType) { |
| 1822 | // If the function types are exactly the same, this isn't an |
| 1823 | // Objective-C pointer conversion. |
| 1824 | if (Context.getCanonicalType(FromPointeeType) |
| 1825 | == Context.getCanonicalType(ToPointeeType)) |
| 1826 | return false; |
| 1827 | |
| 1828 | // Perform the quick checks that will tell us whether these |
| 1829 | // function types are obviously different. |
| 1830 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1831 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1832 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1833 | return false; |
| 1834 | |
| 1835 | bool HasObjCConversion = false; |
| 1836 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1837 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1838 | // Okay, the types match exactly. Nothing to do. |
| 1839 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1840 | ToFunctionType->getResultType(), |
| 1841 | ConvertedType, IncompatibleObjC)) { |
| 1842 | // Okay, we have an Objective-C pointer conversion. |
| 1843 | HasObjCConversion = true; |
| 1844 | } else { |
| 1845 | // Function types are too different. Abort. |
| 1846 | return false; |
| 1847 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1848 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1849 | // Check argument types. |
| 1850 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1851 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1852 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1853 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1854 | if (Context.getCanonicalType(FromArgType) |
| 1855 | == Context.getCanonicalType(ToArgType)) { |
| 1856 | // Okay, the types match exactly. Nothing to do. |
| 1857 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1858 | ConvertedType, IncompatibleObjC)) { |
| 1859 | // Okay, we have an Objective-C pointer conversion. |
| 1860 | HasObjCConversion = true; |
| 1861 | } else { |
| 1862 | // Argument types are too different. Abort. |
| 1863 | return false; |
| 1864 | } |
| 1865 | } |
| 1866 | |
| 1867 | if (HasObjCConversion) { |
| 1868 | // We had an Objective-C conversion. Allow this pointer |
| 1869 | // conversion, but complain about it. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1870 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1871 | IncompatibleObjC = true; |
| 1872 | return true; |
| 1873 | } |
| 1874 | } |
| 1875 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1876 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1877 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1878 | |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1879 | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
| 1880 | QualType& ConvertedType) { |
| 1881 | QualType ToPointeeType; |
| 1882 | if (const BlockPointerType *ToBlockPtr = |
| 1883 | ToType->getAs<BlockPointerType>()) |
| 1884 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 1885 | else |
| 1886 | return false; |
| 1887 | |
| 1888 | QualType FromPointeeType; |
| 1889 | if (const BlockPointerType *FromBlockPtr = |
| 1890 | FromType->getAs<BlockPointerType>()) |
| 1891 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1892 | else |
| 1893 | return false; |
| 1894 | // We have pointer to blocks, check whether the only |
| 1895 | // differences in the argument and result types are in Objective-C |
| 1896 | // pointer conversions. If so, we permit the conversion. |
| 1897 | |
| 1898 | const FunctionProtoType *FromFunctionType |
| 1899 | = FromPointeeType->getAs<FunctionProtoType>(); |
| 1900 | const FunctionProtoType *ToFunctionType |
| 1901 | = ToPointeeType->getAs<FunctionProtoType>(); |
| 1902 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 1903 | if (!FromFunctionType || !ToFunctionType) |
| 1904 | return false; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1905 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 1906 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1907 | return true; |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 1908 | |
| 1909 | // Perform the quick checks that will tell us whether these |
| 1910 | // function types are obviously different. |
| 1911 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1912 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
| 1913 | return false; |
| 1914 | |
| 1915 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
| 1916 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
| 1917 | if (FromEInfo != ToEInfo) |
| 1918 | return false; |
| 1919 | |
| 1920 | bool IncompatibleObjC = false; |
Fariborz Jahanian | 462dae5 | 2011-02-13 20:11:42 +0000 | [diff] [blame] | 1921 | if (Context.hasSameType(FromFunctionType->getResultType(), |
| 1922 | ToFunctionType->getResultType())) { |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 1923 | // Okay, the types match exactly. Nothing to do. |
| 1924 | } else { |
| 1925 | QualType RHS = FromFunctionType->getResultType(); |
| 1926 | QualType LHS = ToFunctionType->getResultType(); |
| 1927 | if ((!getLangOptions().CPlusPlus || !RHS->isRecordType()) && |
| 1928 | !RHS.hasQualifiers() && LHS.hasQualifiers()) |
| 1929 | LHS = LHS.getUnqualifiedType(); |
| 1930 | |
| 1931 | if (Context.hasSameType(RHS,LHS)) { |
| 1932 | // OK exact match. |
| 1933 | } else if (isObjCPointerConversion(RHS, LHS, |
| 1934 | ConvertedType, IncompatibleObjC)) { |
| 1935 | if (IncompatibleObjC) |
| 1936 | return false; |
| 1937 | // Okay, we have an Objective-C pointer conversion. |
| 1938 | } |
| 1939 | else |
| 1940 | return false; |
| 1941 | } |
| 1942 | |
| 1943 | // Check argument types. |
| 1944 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1945 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1946 | IncompatibleObjC = false; |
| 1947 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1948 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1949 | if (Context.hasSameType(FromArgType, ToArgType)) { |
| 1950 | // Okay, the types match exactly. Nothing to do. |
| 1951 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
| 1952 | ConvertedType, IncompatibleObjC)) { |
| 1953 | if (IncompatibleObjC) |
| 1954 | return false; |
| 1955 | // Okay, we have an Objective-C pointer conversion. |
| 1956 | } else |
| 1957 | // Argument types are too different. Abort. |
| 1958 | return false; |
| 1959 | } |
| 1960 | ConvertedType = ToType; |
| 1961 | return true; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1964 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
| 1965 | /// for equlity of their argument types. Caller has already checked that |
| 1966 | /// they have same number of arguments. This routine assumes that Objective-C |
| 1967 | /// pointer types which only differ in their protocol qualifiers are equal. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1968 | bool Sema::FunctionArgTypesAreEqual(const FunctionProtoType *OldType, |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1969 | const FunctionProtoType *NewType) { |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1970 | if (!getLangOptions().ObjC1) |
| 1971 | return std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 1972 | NewType->arg_type_begin()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1973 | |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1974 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 1975 | N = NewType->arg_type_begin(), |
| 1976 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
| 1977 | QualType ToType = (*O); |
| 1978 | QualType FromType = (*N); |
| 1979 | if (ToType != FromType) { |
| 1980 | if (const PointerType *PTTo = ToType->getAs<PointerType>()) { |
| 1981 | if (const PointerType *PTFr = FromType->getAs<PointerType>()) |
Chandler Carruth | 0ee93de | 2010-05-06 00:15:06 +0000 | [diff] [blame] | 1982 | if ((PTTo->getPointeeType()->isObjCQualifiedIdType() && |
| 1983 | PTFr->getPointeeType()->isObjCQualifiedIdType()) || |
| 1984 | (PTTo->getPointeeType()->isObjCQualifiedClassType() && |
| 1985 | PTFr->getPointeeType()->isObjCQualifiedClassType())) |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1986 | continue; |
| 1987 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1988 | else if (const ObjCObjectPointerType *PTTo = |
| 1989 | ToType->getAs<ObjCObjectPointerType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1990 | if (const ObjCObjectPointerType *PTFr = |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1991 | FromType->getAs<ObjCObjectPointerType>()) |
| 1992 | if (PTTo->getInterfaceDecl() == PTFr->getInterfaceDecl()) |
| 1993 | continue; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1994 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1995 | return false; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1996 | } |
| 1997 | } |
| 1998 | return true; |
| 1999 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2000 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2001 | /// CheckPointerConversion - Check the pointer conversion from the |
| 2002 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2003 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2004 | /// conversions for which IsPointerConversion has already returned |
| 2005 | /// true. It returns true and produces a diagnostic if there was an |
| 2006 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2007 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2008 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2009 | CXXCastPath& BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2010 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2011 | QualType FromType = From->getType(); |
Argyrios Kyrtzidis | b335872 | 2010-09-28 14:54:11 +0000 | [diff] [blame] | 2012 | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2013 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2014 | Kind = CK_BitCast; |
| 2015 | |
Chandler Carruth | 88f0aed | 2011-04-09 07:32:05 +0000 | [diff] [blame] | 2016 | if (!IsCStyleOrFunctionalCast && |
| 2017 | Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy) && |
| 2018 | From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 2019 | DiagRuntimeBehavior(From->getExprLoc(), From, |
Chandler Carruth | b600669 | 2011-04-09 07:48:17 +0000 | [diff] [blame] | 2020 | PDiag(diag::warn_impcast_bool_to_null_pointer) |
| 2021 | << ToType << From->getSourceRange()); |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 2022 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2023 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 2024 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2025 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 2026 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 2027 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2028 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 2029 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2030 | // We must have a derived-to-base conversion. Check an |
| 2031 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2032 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 2033 | From->getExprLoc(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2034 | From->getSourceRange(), &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2035 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2036 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2037 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2038 | // The conversion was successful. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2039 | Kind = CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2040 | } |
| 2041 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2042 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2043 | FromType->getAs<ObjCObjectPointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2044 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2045 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2046 | // Objective-C++ conversions are always okay. |
| 2047 | // FIXME: We should have a different class of conversions for the |
| 2048 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2049 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2050 | return false; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2051 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2052 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2053 | |
| 2054 | // We shouldn't fall into this case unless it's valid for other |
| 2055 | // reasons. |
| 2056 | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 2057 | Kind = CK_NullToPointer; |
| 2058 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2059 | return false; |
| 2060 | } |
| 2061 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2062 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 2063 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 2064 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 2065 | /// If so, returns true and places the converted type (that might differ from |
| 2066 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 2067 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2068 | QualType ToType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2069 | bool InOverloadResolution, |
| 2070 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2071 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2072 | if (!ToTypePtr) |
| 2073 | return false; |
| 2074 | |
| 2075 | // A null pointer constant can be converted to a member pointer (C++ 4.11p1) |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2076 | if (From->isNullPointerConstant(Context, |
| 2077 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 2078 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2079 | ConvertedType = ToType; |
| 2080 | return true; |
| 2081 | } |
| 2082 | |
| 2083 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2084 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2085 | if (!FromTypePtr) |
| 2086 | return false; |
| 2087 | |
| 2088 | // A pointer to member of B can be converted to a pointer to member of D, |
| 2089 | // where D is derived from B (C++ 4.11p2). |
| 2090 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 2091 | QualType ToClass(ToTypePtr->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2092 | |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2093 | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
| 2094 | !RequireCompleteType(From->getLocStart(), ToClass, PDiag()) && |
| 2095 | IsDerivedFrom(ToClass, FromClass)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2096 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 2097 | ToClass.getTypePtr()); |
| 2098 | return true; |
| 2099 | } |
| 2100 | |
| 2101 | return false; |
| 2102 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2103 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2104 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 2105 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2106 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2107 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 2108 | /// true and produces a diagnostic if there was an error, or returns false |
| 2109 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2110 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2111 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2112 | CXXCastPath &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2113 | bool IgnoreBaseAccess) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2114 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2115 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2116 | if (!FromPtrType) { |
| 2117 | // This must be a null pointer to member pointer conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2118 | assert(From->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2119 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2120 | "Expr must be null pointer constant!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2121 | Kind = CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2122 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2123 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2124 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2125 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2126 | assert(ToPtrType && "No member pointer cast has a target type " |
| 2127 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2128 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2129 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 2130 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2131 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2132 | // FIXME: What about dependent types? |
| 2133 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 2134 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2135 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2136 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 2137 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2138 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 2139 | assert(DerivationOkay && |
| 2140 | "Should not have been called if derivation isn't OK."); |
| 2141 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2142 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2143 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 2144 | getUnqualifiedType())) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2145 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 2146 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 2147 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 2148 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2149 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2150 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2151 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2152 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 2153 | << FromClass << ToClass << QualType(VBase, 0) |
| 2154 | << From->getSourceRange(); |
| 2155 | return true; |
| 2156 | } |
| 2157 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2158 | if (!IgnoreBaseAccess) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2159 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 2160 | Paths.front(), |
| 2161 | diag::err_downcast_from_inaccessible_base); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2162 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2163 | // Must be a base to derived member conversion. |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2164 | BuildBasePathArray(Paths, BasePath); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2165 | Kind = CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2166 | return false; |
| 2167 | } |
| 2168 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2169 | /// IsQualificationConversion - Determines whether the conversion from |
| 2170 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 2171 | /// (C++ 4.4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2172 | bool |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2173 | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2174 | bool CStyle) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2175 | FromType = Context.getCanonicalType(FromType); |
| 2176 | ToType = Context.getCanonicalType(ToType); |
| 2177 | |
| 2178 | // If FromType and ToType are the same type, this is not a |
| 2179 | // qualification conversion. |
Sebastian Redl | 22c9240 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 2180 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2181 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2182 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2183 | // (C++ 4.4p4): |
| 2184 | // A conversion can add cv-qualifiers at levels other than the first |
| 2185 | // in multi-level pointers, subject to the following rules: [...] |
| 2186 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2187 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2188 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2189 | // Within each iteration of the loop, we check the qualifiers to |
| 2190 | // determine if this still looks like a qualification |
| 2191 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2192 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2193 | // until there are no more pointers or pointers-to-members left to |
| 2194 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2195 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2196 | |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2197 | Qualifiers FromQuals = FromType.getQualifiers(); |
| 2198 | Qualifiers ToQuals = ToType.getQualifiers(); |
| 2199 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2200 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 2201 | // 2,j, and similarly for volatile. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2202 | if (!CStyle && !ToQuals.compatiblyIncludes(FromQuals)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2203 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2204 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2205 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 2206 | // every cv for 0 < k < j. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2207 | if (!CStyle && FromQuals.getCVRQualifiers() != ToQuals.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2208 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2209 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2210 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2211 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 2212 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2213 | PreviousToQualsIncludeConst |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2214 | = PreviousToQualsIncludeConst && ToQuals.hasConst(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2215 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2216 | |
| 2217 | // We are left with FromType and ToType being the pointee types |
| 2218 | // after unwrapping the original FromType and ToType the same number |
| 2219 | // of types. If we unwrapped any pointers, and if FromType and |
| 2220 | // ToType have the same unqualified type (since we checked |
| 2221 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2222 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2225 | /// Determines whether there is a user-defined conversion sequence |
| 2226 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 2227 | /// ToType. If such a conversion exists, User will contain the |
| 2228 | /// user-defined conversion sequence that performs such a conversion |
| 2229 | /// and this routine will return true. Otherwise, this routine returns |
| 2230 | /// false and User is unspecified. |
| 2231 | /// |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2232 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 2233 | /// "explicit" conversion functions as well as non-explicit conversion |
| 2234 | /// functions (C++0x [class.conv.fct]p2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2235 | static OverloadingResult |
| 2236 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 2237 | UserDefinedConversionSequence& User, |
| 2238 | OverloadCandidateSet& CandidateSet, |
| 2239 | bool AllowExplicit) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2240 | // Whether we will only visit constructors. |
| 2241 | bool ConstructorsOnly = false; |
| 2242 | |
| 2243 | // If the type we are conversion to is a class type, enumerate its |
| 2244 | // constructors. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2245 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2246 | // C++ [over.match.ctor]p1: |
| 2247 | // When objects of class type are direct-initialized (8.5), or |
| 2248 | // copy-initialized from an expression of the same or a |
| 2249 | // derived class type (8.5), overload resolution selects the |
| 2250 | // constructor. [...] For copy-initialization, the candidate |
| 2251 | // functions are all the converting constructors (12.3.1) of |
| 2252 | // that class. The argument list is the expression-list within |
| 2253 | // the parentheses of the initializer. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2254 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2255 | (From->getType()->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2256 | S.IsDerivedFrom(From->getType(), ToType))) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2257 | ConstructorsOnly = true; |
| 2258 | |
Argyrios Kyrtzidis | e36bca6 | 2011-04-22 17:45:37 +0000 | [diff] [blame] | 2259 | S.RequireCompleteType(From->getLocStart(), ToType, S.PDiag()); |
| 2260 | // RequireCompleteType may have returned true due to some invalid decl |
| 2261 | // during template instantiation, but ToType may be complete enough now |
| 2262 | // to try to recover. |
| 2263 | if (ToType->isIncompleteType()) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 2264 | // We're not going to find any constructors. |
| 2265 | } else if (CXXRecordDecl *ToRecordDecl |
| 2266 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2267 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2268 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(ToRecordDecl); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2269 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2270 | NamedDecl *D = *Con; |
| 2271 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2272 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2273 | // Find the constructor (which may be a template). |
| 2274 | CXXConstructorDecl *Constructor = 0; |
| 2275 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2276 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2277 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2278 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2279 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 2280 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2281 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2282 | |
Fariborz Jahanian | 52ab92b | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 2283 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 2284 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2285 | if (ConstructorTmpl) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2286 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2287 | /*ExplicitArgs*/ 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2288 | &From, 1, CandidateSet, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2289 | /*SuppressUserConversions=*/ |
| 2290 | !ConstructorsOnly); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2291 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 2292 | // Allow one user-defined conversion when user specifies a |
| 2293 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2294 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 2295 | &From, 1, CandidateSet, |
| 2296 | /*SuppressUserConversions=*/ |
| 2297 | !ConstructorsOnly); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2298 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2299 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2300 | } |
| 2301 | } |
| 2302 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2303 | // Enumerate conversion functions, if we're allowed to. |
| 2304 | if (ConstructorsOnly) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2305 | } else if (S.RequireCompleteType(From->getLocStart(), From->getType(), |
| 2306 | S.PDiag(0) << From->getSourceRange())) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2307 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2308 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2309 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2310 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2311 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 2312 | // Add all of the conversion functions as candidates. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2313 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 2314 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2315 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2316 | E = Conversions->end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2317 | DeclAccessPair FoundDecl = I.getPair(); |
| 2318 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2319 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2320 | if (isa<UsingShadowDecl>(D)) |
| 2321 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2322 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2323 | CXXConversionDecl *Conv; |
| 2324 | FunctionTemplateDecl *ConvTemplate; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2325 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 2326 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2327 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2328 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2329 | |
| 2330 | if (AllowExplicit || !Conv->isExplicit()) { |
| 2331 | if (ConvTemplate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2332 | S.AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
| 2333 | ActingContext, From, ToType, |
| 2334 | CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2335 | else |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2336 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, |
| 2337 | From, ToType, CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2338 | } |
| 2339 | } |
| 2340 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2341 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2342 | |
| 2343 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 2344 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2345 | case OR_Success: |
| 2346 | // Record the standard conversion we used and the conversion function. |
| 2347 | if (CXXConstructorDecl *Constructor |
| 2348 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2349 | S.MarkDeclarationReferenced(From->getLocStart(), Constructor); |
| 2350 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2351 | // C++ [over.ics.user]p1: |
| 2352 | // If the user-defined conversion is specified by a |
| 2353 | // constructor (12.3.1), the initial standard conversion |
| 2354 | // sequence converts the source type to the type required by |
| 2355 | // the argument of the constructor. |
| 2356 | // |
| 2357 | QualType ThisType = Constructor->getThisType(S.Context); |
| 2358 | if (Best->Conversions[0].isEllipsis()) |
| 2359 | User.EllipsisConversion = true; |
| 2360 | else { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2361 | User.Before = Best->Conversions[0].Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2362 | User.EllipsisConversion = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2363 | } |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2364 | User.ConversionFunction = Constructor; |
Douglas Gregor | 83eecbe | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 2365 | User.FoundConversionFunction = Best->FoundDecl.getDecl(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2366 | User.After.setAsIdentityConversion(); |
| 2367 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 2368 | User.After.setAllToTypes(ToType); |
| 2369 | return OR_Success; |
| 2370 | } else if (CXXConversionDecl *Conversion |
| 2371 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2372 | S.MarkDeclarationReferenced(From->getLocStart(), Conversion); |
| 2373 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2374 | // C++ [over.ics.user]p1: |
| 2375 | // |
| 2376 | // [...] If the user-defined conversion is specified by a |
| 2377 | // conversion function (12.3.2), the initial standard |
| 2378 | // conversion sequence converts the source type to the |
| 2379 | // implicit object parameter of the conversion function. |
| 2380 | User.Before = Best->Conversions[0].Standard; |
| 2381 | User.ConversionFunction = Conversion; |
Douglas Gregor | 83eecbe | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 2382 | User.FoundConversionFunction = Best->FoundDecl.getDecl(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2383 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2384 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2385 | // C++ [over.ics.user]p2: |
| 2386 | // The second standard conversion sequence converts the |
| 2387 | // result of the user-defined conversion to the target type |
| 2388 | // for the sequence. Since an implicit conversion sequence |
| 2389 | // is an initialization, the special rules for |
| 2390 | // initialization by user-defined conversion apply when |
| 2391 | // selecting the best user-defined conversion for a |
| 2392 | // user-defined conversion sequence (see 13.3.3 and |
| 2393 | // 13.3.3.1). |
| 2394 | User.After = Best->FinalConversion; |
| 2395 | return OR_Success; |
| 2396 | } else { |
| 2397 | llvm_unreachable("Not a constructor or conversion function?"); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2398 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2399 | } |
| 2400 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2401 | case OR_No_Viable_Function: |
| 2402 | return OR_No_Viable_Function; |
| 2403 | case OR_Deleted: |
| 2404 | // No conversion here! We're done. |
| 2405 | return OR_Deleted; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2406 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2407 | case OR_Ambiguous: |
| 2408 | return OR_Ambiguous; |
| 2409 | } |
| 2410 | |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2411 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2412 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2413 | |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2414 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2415 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2416 | ImplicitConversionSequence ICS; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2417 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2418 | OverloadingResult OvResult = |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2419 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2420 | CandidateSet, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2421 | if (OvResult == OR_Ambiguous) |
| 2422 | Diag(From->getSourceRange().getBegin(), |
| 2423 | diag::err_typecheck_ambiguous_condition) |
| 2424 | << From->getType() << ToType << From->getSourceRange(); |
| 2425 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 2426 | Diag(From->getSourceRange().getBegin(), |
| 2427 | diag::err_typecheck_nonviable_condition) |
| 2428 | << From->getType() << ToType << From->getSourceRange(); |
| 2429 | else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2430 | return false; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2431 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &From, 1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2432 | return true; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2433 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2434 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2435 | /// CompareImplicitConversionSequences - Compare two implicit |
| 2436 | /// conversion sequences to determine whether one is better than the |
| 2437 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2438 | static ImplicitConversionSequence::CompareKind |
| 2439 | CompareImplicitConversionSequences(Sema &S, |
| 2440 | const ImplicitConversionSequence& ICS1, |
| 2441 | const ImplicitConversionSequence& ICS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2442 | { |
| 2443 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 2444 | // conversion sequences (as defined in 13.3.3.1) |
| 2445 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 2446 | // conversion sequence than a user-defined conversion sequence or |
| 2447 | // an ellipsis conversion sequence, and |
| 2448 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 2449 | // conversion sequence than an ellipsis conversion sequence |
| 2450 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2451 | // |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2452 | // C++0x [over.best.ics]p10: |
| 2453 | // For the purpose of ranking implicit conversion sequences as |
| 2454 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 2455 | // treated as a user-defined sequence that is indistinguishable |
| 2456 | // from any other user-defined conversion sequence. |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2457 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 2458 | return ImplicitConversionSequence::Better; |
| 2459 | else if (ICS2.getKindRank() < ICS1.getKindRank()) |
| 2460 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2461 | |
Benjamin Kramer | b6eee07 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 2462 | // The following checks require both conversion sequences to be of |
| 2463 | // the same kind. |
| 2464 | if (ICS1.getKind() != ICS2.getKind()) |
| 2465 | return ImplicitConversionSequence::Indistinguishable; |
| 2466 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2467 | // Two implicit conversion sequences of the same form are |
| 2468 | // indistinguishable conversion sequences unless one of the |
| 2469 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2470 | if (ICS1.isStandard()) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2471 | return CompareStandardConversionSequences(S, ICS1.Standard, ICS2.Standard); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2472 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2473 | // User-defined conversion sequence U1 is a better conversion |
| 2474 | // sequence than another user-defined conversion sequence U2 if |
| 2475 | // they contain the same user-defined conversion function or |
| 2476 | // constructor and if the second standard conversion sequence of |
| 2477 | // U1 is better than the second standard conversion sequence of |
| 2478 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2479 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2480 | ICS2.UserDefined.ConversionFunction) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2481 | return CompareStandardConversionSequences(S, |
| 2482 | ICS1.UserDefined.After, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2483 | ICS2.UserDefined.After); |
| 2484 | } |
| 2485 | |
| 2486 | return ImplicitConversionSequence::Indistinguishable; |
| 2487 | } |
| 2488 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2489 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 2490 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 2491 | Qualifiers Quals; |
| 2492 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2493 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2494 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2495 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2496 | return Context.hasSameUnqualifiedType(T1, T2); |
| 2497 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2498 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2499 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 2500 | // determine if one is a proper subset of the other. |
| 2501 | static ImplicitConversionSequence::CompareKind |
| 2502 | compareStandardConversionSubsets(ASTContext &Context, |
| 2503 | const StandardConversionSequence& SCS1, |
| 2504 | const StandardConversionSequence& SCS2) { |
| 2505 | ImplicitConversionSequence::CompareKind Result |
| 2506 | = ImplicitConversionSequence::Indistinguishable; |
| 2507 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2508 | // the identity conversion sequence is considered to be a subsequence of |
Douglas Gregor | ae65f4b | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 2509 | // any non-identity conversion sequence |
| 2510 | if (SCS1.ReferenceBinding == SCS2.ReferenceBinding) { |
| 2511 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 2512 | return ImplicitConversionSequence::Better; |
| 2513 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 2514 | return ImplicitConversionSequence::Worse; |
| 2515 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2516 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2517 | if (SCS1.Second != SCS2.Second) { |
| 2518 | if (SCS1.Second == ICK_Identity) |
| 2519 | Result = ImplicitConversionSequence::Better; |
| 2520 | else if (SCS2.Second == ICK_Identity) |
| 2521 | Result = ImplicitConversionSequence::Worse; |
| 2522 | else |
| 2523 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2524 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2525 | return ImplicitConversionSequence::Indistinguishable; |
| 2526 | |
| 2527 | if (SCS1.Third == SCS2.Third) { |
| 2528 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 2529 | : ImplicitConversionSequence::Indistinguishable; |
| 2530 | } |
| 2531 | |
| 2532 | if (SCS1.Third == ICK_Identity) |
| 2533 | return Result == ImplicitConversionSequence::Worse |
| 2534 | ? ImplicitConversionSequence::Indistinguishable |
| 2535 | : ImplicitConversionSequence::Better; |
| 2536 | |
| 2537 | if (SCS2.Third == ICK_Identity) |
| 2538 | return Result == ImplicitConversionSequence::Better |
| 2539 | ? ImplicitConversionSequence::Indistinguishable |
| 2540 | : ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2541 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2542 | return ImplicitConversionSequence::Indistinguishable; |
| 2543 | } |
| 2544 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2545 | /// \brief Determine whether one of the given reference bindings is better |
| 2546 | /// than the other based on what kind of bindings they are. |
| 2547 | static bool isBetterReferenceBindingKind(const StandardConversionSequence &SCS1, |
| 2548 | const StandardConversionSequence &SCS2) { |
| 2549 | // C++0x [over.ics.rank]p3b4: |
| 2550 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 2551 | // implicit object parameter of a non-static member function declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2552 | // without a ref-qualifier, and *either* S1 binds an rvalue reference |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2553 | // to an rvalue and S2 binds an lvalue reference *or S1 binds an |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2554 | // lvalue reference to a function lvalue and S2 binds an rvalue |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2555 | // reference*. |
| 2556 | // |
| 2557 | // FIXME: Rvalue references. We're going rogue with the above edits, |
| 2558 | // because the semantics in the current C++0x working paper (N3225 at the |
| 2559 | // time of this writing) break the standard definition of std::forward |
| 2560 | // and std::reference_wrapper when dealing with references to functions. |
| 2561 | // Proposed wording changes submitted to CWG for consideration. |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 2562 | if (SCS1.BindsImplicitObjectArgumentWithoutRefQualifier || |
| 2563 | SCS2.BindsImplicitObjectArgumentWithoutRefQualifier) |
| 2564 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2565 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2566 | return (!SCS1.IsLvalueReference && SCS1.BindsToRvalue && |
| 2567 | SCS2.IsLvalueReference) || |
| 2568 | (SCS1.IsLvalueReference && SCS1.BindsToFunctionLvalue && |
| 2569 | !SCS2.IsLvalueReference); |
| 2570 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2571 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2572 | /// CompareStandardConversionSequences - Compare two standard |
| 2573 | /// conversion sequences to determine whether one is better than the |
| 2574 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2575 | static ImplicitConversionSequence::CompareKind |
| 2576 | CompareStandardConversionSequences(Sema &S, |
| 2577 | const StandardConversionSequence& SCS1, |
| 2578 | const StandardConversionSequence& SCS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2579 | { |
| 2580 | // Standard conversion sequence S1 is a better conversion sequence |
| 2581 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 2582 | |
| 2583 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 2584 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 2585 | // excluding any Lvalue Transformation; the identity conversion |
| 2586 | // sequence is considered to be a subsequence of any |
| 2587 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2588 | if (ImplicitConversionSequence::CompareKind CK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2589 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2590 | return CK; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2591 | |
| 2592 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 2593 | // defined below), or, if not that, |
| 2594 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 2595 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 2596 | if (Rank1 < Rank2) |
| 2597 | return ImplicitConversionSequence::Better; |
| 2598 | else if (Rank2 < Rank1) |
| 2599 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2600 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2601 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 2602 | // are indistinguishable unless one of the following rules |
| 2603 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2604 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2605 | // A conversion that is not a conversion of a pointer, or |
| 2606 | // pointer to member, to bool is better than another conversion |
| 2607 | // that is such a conversion. |
| 2608 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 2609 | return SCS2.isPointerConversionToBool() |
| 2610 | ? ImplicitConversionSequence::Better |
| 2611 | : ImplicitConversionSequence::Worse; |
| 2612 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2613 | // C++ [over.ics.rank]p4b2: |
| 2614 | // |
| 2615 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2616 | // conversion of B* to A* is better than conversion of B* to |
| 2617 | // void*, and conversion of A* to void* is better than conversion |
| 2618 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2619 | bool SCS1ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2620 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2621 | bool SCS2ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2622 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2623 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 2624 | // Exactly one of the conversion sequences is a conversion to |
| 2625 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2626 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 2627 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2628 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 2629 | // Neither conversion sequence converts to a void pointer; compare |
| 2630 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2631 | if (ImplicitConversionSequence::CompareKind DerivedCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2632 | = CompareDerivedToBaseConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2633 | return DerivedCK; |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 2634 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid && |
| 2635 | !S.Context.hasSameType(SCS1.getFromType(), SCS2.getFromType())) { |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2636 | // Both conversion sequences are conversions to void |
| 2637 | // pointers. Compare the source types to determine if there's an |
| 2638 | // inheritance relationship in their sources. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2639 | QualType FromType1 = SCS1.getFromType(); |
| 2640 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2641 | |
| 2642 | // Adjust the types we're converting from via the array-to-pointer |
| 2643 | // conversion, if we need to. |
| 2644 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2645 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2646 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2647 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2648 | |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 2649 | QualType FromPointee1 = FromType1->getPointeeType().getUnqualifiedType(); |
| 2650 | QualType FromPointee2 = FromType2->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2651 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2652 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2653 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2654 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2655 | return ImplicitConversionSequence::Worse; |
| 2656 | |
| 2657 | // Objective-C++: If one interface is more specific than the |
| 2658 | // other, it is the better one. |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 2659 | const ObjCObjectPointerType* FromObjCPtr1 |
| 2660 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 2661 | const ObjCObjectPointerType* FromObjCPtr2 |
| 2662 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 2663 | if (FromObjCPtr1 && FromObjCPtr2) { |
| 2664 | bool AssignLeft = S.Context.canAssignObjCInterfaces(FromObjCPtr1, |
| 2665 | FromObjCPtr2); |
| 2666 | bool AssignRight = S.Context.canAssignObjCInterfaces(FromObjCPtr2, |
| 2667 | FromObjCPtr1); |
| 2668 | if (AssignLeft != AssignRight) { |
| 2669 | return AssignLeft? ImplicitConversionSequence::Better |
| 2670 | : ImplicitConversionSequence::Worse; |
| 2671 | } |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2672 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2673 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2674 | |
| 2675 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 2676 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2677 | if (ImplicitConversionSequence::CompareKind QualCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2678 | = CompareQualificationConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2679 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2680 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2681 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2682 | // Check for a better reference binding based on the kind of bindings. |
| 2683 | if (isBetterReferenceBindingKind(SCS1, SCS2)) |
| 2684 | return ImplicitConversionSequence::Better; |
| 2685 | else if (isBetterReferenceBindingKind(SCS2, SCS1)) |
| 2686 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2687 | |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 2688 | // C++ [over.ics.rank]p3b4: |
| 2689 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 2690 | // which the references refer are the same type except for |
| 2691 | // top-level cv-qualifiers, and the type to which the reference |
| 2692 | // initialized by S2 refers is more cv-qualified than the type |
| 2693 | // to which the reference initialized by S1 refers. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2694 | QualType T1 = SCS1.getToType(2); |
| 2695 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2696 | T1 = S.Context.getCanonicalType(T1); |
| 2697 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2698 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 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); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2701 | if (UnqualT1 == UnqualT2) { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2702 | // If the type is an array type, promote the element qualifiers to the |
| 2703 | // type for comparison. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2704 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2705 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2706 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2707 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2708 | if (T2.isMoreQualifiedThan(T1)) |
| 2709 | return ImplicitConversionSequence::Better; |
| 2710 | else if (T1.isMoreQualifiedThan(T2)) |
| 2711 | return ImplicitConversionSequence::Worse; |
| 2712 | } |
| 2713 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2714 | |
| 2715 | return ImplicitConversionSequence::Indistinguishable; |
| 2716 | } |
| 2717 | |
| 2718 | /// CompareQualificationConversions - Compares two standard conversion |
| 2719 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2720 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 2721 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2722 | CompareQualificationConversions(Sema &S, |
| 2723 | const StandardConversionSequence& SCS1, |
| 2724 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 2725 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2726 | // -- S1 and S2 differ only in their qualification conversion and |
| 2727 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 2728 | // cv-qualification signature of type T1 is a proper subset of |
| 2729 | // the cv-qualification signature of type T2, and S1 is not the |
| 2730 | // deprecated string literal array-to-pointer conversion (4.2). |
| 2731 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 2732 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 2733 | return ImplicitConversionSequence::Indistinguishable; |
| 2734 | |
| 2735 | // FIXME: the example in the standard doesn't use a qualification |
| 2736 | // conversion (!) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2737 | QualType T1 = SCS1.getToType(2); |
| 2738 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2739 | T1 = S.Context.getCanonicalType(T1); |
| 2740 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2741 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2742 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2743 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2744 | |
| 2745 | // If the types are the same, we won't learn anything by unwrapped |
| 2746 | // them. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2747 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2748 | return ImplicitConversionSequence::Indistinguishable; |
| 2749 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2750 | // If the type is an array type, promote the element qualifiers to the type |
| 2751 | // for comparison. |
| 2752 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2753 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2754 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2755 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2756 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2757 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2758 | = ImplicitConversionSequence::Indistinguishable; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2759 | while (S.Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2760 | // Within each iteration of the loop, we check the qualifiers to |
| 2761 | // determine if this still looks like a qualification |
| 2762 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2763 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2764 | // until there are no more pointers or pointers-to-members left |
| 2765 | // to unwrap. This essentially mimics what |
| 2766 | // IsQualificationConversion does, but here we're checking for a |
| 2767 | // strict subset of qualifiers. |
| 2768 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 2769 | // The qualifiers are the same, so this doesn't tell us anything |
| 2770 | // about how the sequences rank. |
| 2771 | ; |
| 2772 | else if (T2.isMoreQualifiedThan(T1)) { |
| 2773 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 2774 | if (Result == ImplicitConversionSequence::Worse) |
| 2775 | // Neither has qualifiers that are a subset of the other's |
| 2776 | // qualifiers. |
| 2777 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2778 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2779 | Result = ImplicitConversionSequence::Better; |
| 2780 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 2781 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 2782 | if (Result == ImplicitConversionSequence::Better) |
| 2783 | // Neither has qualifiers that are a subset of the other's |
| 2784 | // qualifiers. |
| 2785 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2786 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2787 | Result = ImplicitConversionSequence::Worse; |
| 2788 | } else { |
| 2789 | // Qualifiers are disjoint. |
| 2790 | return ImplicitConversionSequence::Indistinguishable; |
| 2791 | } |
| 2792 | |
| 2793 | // If the types after this point are equivalent, we're done. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2794 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2795 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2798 | // Check that the winning standard conversion sequence isn't using |
| 2799 | // the deprecated string literal array to pointer conversion. |
| 2800 | switch (Result) { |
| 2801 | case ImplicitConversionSequence::Better: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2802 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2803 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2804 | break; |
| 2805 | |
| 2806 | case ImplicitConversionSequence::Indistinguishable: |
| 2807 | break; |
| 2808 | |
| 2809 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2810 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2811 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2812 | break; |
| 2813 | } |
| 2814 | |
| 2815 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2818 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 2819 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2820 | /// various kinds of derived-to-base conversions (C++ |
| 2821 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 2822 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2823 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2824 | CompareDerivedToBaseConversions(Sema &S, |
| 2825 | const StandardConversionSequence& SCS1, |
| 2826 | const StandardConversionSequence& SCS2) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2827 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2828 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2829 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2830 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2831 | |
| 2832 | // Adjust the types we're converting from via the array-to-pointer |
| 2833 | // conversion, if we need to. |
| 2834 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2835 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2836 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2837 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2838 | |
| 2839 | // Canonicalize all of the types. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2840 | FromType1 = S.Context.getCanonicalType(FromType1); |
| 2841 | ToType1 = S.Context.getCanonicalType(ToType1); |
| 2842 | FromType2 = S.Context.getCanonicalType(FromType2); |
| 2843 | ToType2 = S.Context.getCanonicalType(ToType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2844 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2845 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2846 | // |
| 2847 | // If class B is derived directly or indirectly from class A and |
| 2848 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2849 | // |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2850 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2851 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 2852 | SCS2.Second == ICK_Pointer_Conversion && |
| 2853 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 2854 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 2855 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2856 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2857 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2858 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2859 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2860 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2861 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2862 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2863 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2864 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2865 | // -- conversion of C* to B* is better than conversion of C* to A*, |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2866 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2867 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2868 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2869 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2870 | return ImplicitConversionSequence::Worse; |
| 2871 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2872 | |
| 2873 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 2874 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2875 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2876 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2877 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2878 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 2879 | } |
| 2880 | } else if (SCS1.Second == ICK_Pointer_Conversion && |
| 2881 | SCS2.Second == ICK_Pointer_Conversion) { |
| 2882 | const ObjCObjectPointerType *FromPtr1 |
| 2883 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 2884 | const ObjCObjectPointerType *FromPtr2 |
| 2885 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 2886 | const ObjCObjectPointerType *ToPtr1 |
| 2887 | = ToType1->getAs<ObjCObjectPointerType>(); |
| 2888 | const ObjCObjectPointerType *ToPtr2 |
| 2889 | = ToType2->getAs<ObjCObjectPointerType>(); |
| 2890 | |
| 2891 | if (FromPtr1 && FromPtr2 && ToPtr1 && ToPtr2) { |
| 2892 | // Apply the same conversion ranking rules for Objective-C pointer types |
| 2893 | // that we do for C++ pointers to class types. However, we employ the |
| 2894 | // Objective-C pseudo-subtyping relationship used for assignment of |
| 2895 | // Objective-C pointer types. |
| 2896 | bool FromAssignLeft |
| 2897 | = S.Context.canAssignObjCInterfaces(FromPtr1, FromPtr2); |
| 2898 | bool FromAssignRight |
| 2899 | = S.Context.canAssignObjCInterfaces(FromPtr2, FromPtr1); |
| 2900 | bool ToAssignLeft |
| 2901 | = S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2); |
| 2902 | bool ToAssignRight |
| 2903 | = S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1); |
| 2904 | |
| 2905 | // A conversion to an a non-id object pointer type or qualified 'id' |
| 2906 | // type is better than a conversion to 'id'. |
| 2907 | if (ToPtr1->isObjCIdType() && |
| 2908 | (ToPtr2->isObjCQualifiedIdType() || ToPtr2->getInterfaceDecl())) |
| 2909 | return ImplicitConversionSequence::Worse; |
| 2910 | if (ToPtr2->isObjCIdType() && |
| 2911 | (ToPtr1->isObjCQualifiedIdType() || ToPtr1->getInterfaceDecl())) |
| 2912 | return ImplicitConversionSequence::Better; |
| 2913 | |
| 2914 | // A conversion to a non-id object pointer type is better than a |
| 2915 | // conversion to a qualified 'id' type |
| 2916 | if (ToPtr1->isObjCQualifiedIdType() && ToPtr2->getInterfaceDecl()) |
| 2917 | return ImplicitConversionSequence::Worse; |
| 2918 | if (ToPtr2->isObjCQualifiedIdType() && ToPtr1->getInterfaceDecl()) |
| 2919 | return ImplicitConversionSequence::Better; |
| 2920 | |
| 2921 | // A conversion to an a non-Class object pointer type or qualified 'Class' |
| 2922 | // type is better than a conversion to 'Class'. |
| 2923 | if (ToPtr1->isObjCClassType() && |
| 2924 | (ToPtr2->isObjCQualifiedClassType() || ToPtr2->getInterfaceDecl())) |
| 2925 | return ImplicitConversionSequence::Worse; |
| 2926 | if (ToPtr2->isObjCClassType() && |
| 2927 | (ToPtr1->isObjCQualifiedClassType() || ToPtr1->getInterfaceDecl())) |
| 2928 | return ImplicitConversionSequence::Better; |
| 2929 | |
| 2930 | // A conversion to a non-Class object pointer type is better than a |
| 2931 | // conversion to a qualified 'Class' type. |
| 2932 | if (ToPtr1->isObjCQualifiedClassType() && ToPtr2->getInterfaceDecl()) |
| 2933 | return ImplicitConversionSequence::Worse; |
| 2934 | if (ToPtr2->isObjCQualifiedClassType() && ToPtr1->getInterfaceDecl()) |
| 2935 | return ImplicitConversionSequence::Better; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2936 | |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 2937 | // -- "conversion of C* to B* is better than conversion of C* to A*," |
| 2938 | if (S.Context.hasSameType(FromType1, FromType2) && |
| 2939 | !FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType() && |
| 2940 | (ToAssignLeft != ToAssignRight)) |
| 2941 | return ToAssignLeft? ImplicitConversionSequence::Worse |
| 2942 | : ImplicitConversionSequence::Better; |
| 2943 | |
| 2944 | // -- "conversion of B* to A* is better than conversion of C* to A*," |
| 2945 | if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) && |
| 2946 | (FromAssignLeft != FromAssignRight)) |
| 2947 | return FromAssignLeft? ImplicitConversionSequence::Better |
| 2948 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2949 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2950 | } |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 2951 | |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2952 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2953 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 2954 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 2955 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2956 | const MemberPointerType * FromMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2957 | FromType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2958 | const MemberPointerType * ToMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2959 | ToType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2960 | const MemberPointerType * FromMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2961 | FromType2->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2962 | const MemberPointerType * ToMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2963 | ToType2->getAs<MemberPointerType>(); |
| 2964 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 2965 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 2966 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 2967 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 2968 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 2969 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 2970 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 2971 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2972 | // conversion of A::* to B::* is better than conversion of A::* to C::*, |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2973 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2974 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2975 | return ImplicitConversionSequence::Worse; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2976 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2977 | return ImplicitConversionSequence::Better; |
| 2978 | } |
| 2979 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 2980 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2981 | if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2982 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2983 | else if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2984 | return ImplicitConversionSequence::Worse; |
| 2985 | } |
| 2986 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2987 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2988 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2989 | // -- conversion of C to B is better than conversion of C to A, |
Douglas Gregor | 9e23932 | 2010-02-25 19:01:05 +0000 | [diff] [blame] | 2990 | // -- binding of an expression of type C to a reference of type |
| 2991 | // B& is better than binding an expression of type C to a |
| 2992 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2993 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2994 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 2995 | if (S.IsDerivedFrom(ToType1, ToType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2996 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2997 | else if (S.IsDerivedFrom(ToType2, ToType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2998 | return ImplicitConversionSequence::Worse; |
| 2999 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3000 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3001 | // -- conversion of B to A is better than conversion of C to A. |
Douglas Gregor | 9e23932 | 2010-02-25 19:01:05 +0000 | [diff] [blame] | 3002 | // -- binding of an expression of type B to a reference of type |
| 3003 | // A& is better than binding an expression of type C to a |
| 3004 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3005 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3006 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3007 | if (S.IsDerivedFrom(FromType2, FromType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3008 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3009 | else if (S.IsDerivedFrom(FromType1, FromType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3010 | return ImplicitConversionSequence::Worse; |
| 3011 | } |
| 3012 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3013 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3014 | return ImplicitConversionSequence::Indistinguishable; |
| 3015 | } |
| 3016 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3017 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 3018 | /// determine whether they are reference-related, |
| 3019 | /// reference-compatible, reference-compatible with added |
| 3020 | /// qualification, or incompatible, for use in C++ initialization by |
| 3021 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 3022 | /// type, and the first type (T1) is the pointee type of the reference |
| 3023 | /// type being initialized. |
| 3024 | Sema::ReferenceCompareResult |
| 3025 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 3026 | QualType OrigT1, QualType OrigT2, |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3027 | bool &DerivedToBase, |
| 3028 | bool &ObjCConversion) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3029 | assert(!OrigT1->isReferenceType() && |
| 3030 | "T1 must be the pointee type of the reference type"); |
| 3031 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 3032 | |
| 3033 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 3034 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 3035 | Qualifiers T1Quals, T2Quals; |
| 3036 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3037 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 3038 | |
| 3039 | // C++ [dcl.init.ref]p4: |
| 3040 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 3041 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 3042 | // T1 is a base class of T2. |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3043 | DerivedToBase = false; |
| 3044 | ObjCConversion = false; |
| 3045 | if (UnqualT1 == UnqualT2) { |
| 3046 | // Nothing to do. |
| 3047 | } else if (!RequireCompleteType(Loc, OrigT2, PDiag()) && |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3048 | IsDerivedFrom(UnqualT2, UnqualT1)) |
| 3049 | DerivedToBase = true; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3050 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
| 3051 | UnqualT2->isObjCObjectOrInterfaceType() && |
| 3052 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
| 3053 | ObjCConversion = true; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3054 | else |
| 3055 | return Ref_Incompatible; |
| 3056 | |
| 3057 | // At this point, we know that T1 and T2 are reference-related (at |
| 3058 | // least). |
| 3059 | |
| 3060 | // If the type is an array type, promote the element qualifiers to the type |
| 3061 | // for comparison. |
| 3062 | if (isa<ArrayType>(T1) && T1Quals) |
| 3063 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 3064 | if (isa<ArrayType>(T2) && T2Quals) |
| 3065 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 3066 | |
| 3067 | // C++ [dcl.init.ref]p4: |
| 3068 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 3069 | // reference-related to T2 and cv1 is the same cv-qualification |
| 3070 | // as, or greater cv-qualification than, cv2. For purposes of |
| 3071 | // overload resolution, cases for which cv1 is greater |
| 3072 | // cv-qualification than cv2 are identified as |
| 3073 | // reference-compatible with added qualification (see 13.3.3.2). |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 3074 | // |
| 3075 | // Note that we also require equivalence of Objective-C GC and address-space |
| 3076 | // qualifiers when performing these computations, so that e.g., an int in |
| 3077 | // address space 1 is not reference-compatible with an int in address |
| 3078 | // space 2. |
| 3079 | if (T1Quals == T2Quals) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3080 | return Ref_Compatible; |
| 3081 | else if (T1.isMoreQualifiedThan(T2)) |
| 3082 | return Ref_Compatible_With_Added_Qualification; |
| 3083 | else |
| 3084 | return Ref_Related; |
| 3085 | } |
| 3086 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3087 | /// \brief Look for a user-defined conversion to an value reference-compatible |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3088 | /// with DeclType. Return true if something definite is found. |
| 3089 | static bool |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3090 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
| 3091 | QualType DeclType, SourceLocation DeclLoc, |
| 3092 | Expr *Init, QualType T2, bool AllowRvalues, |
| 3093 | bool AllowExplicit) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3094 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 3095 | CXXRecordDecl *T2RecordDecl |
| 3096 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 3097 | |
| 3098 | OverloadCandidateSet CandidateSet(DeclLoc); |
| 3099 | const UnresolvedSetImpl *Conversions |
| 3100 | = T2RecordDecl->getVisibleConversionFunctions(); |
| 3101 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 3102 | E = Conversions->end(); I != E; ++I) { |
| 3103 | NamedDecl *D = *I; |
| 3104 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3105 | if (isa<UsingShadowDecl>(D)) |
| 3106 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3107 | |
| 3108 | FunctionTemplateDecl *ConvTemplate |
| 3109 | = dyn_cast<FunctionTemplateDecl>(D); |
| 3110 | CXXConversionDecl *Conv; |
| 3111 | if (ConvTemplate) |
| 3112 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3113 | else |
| 3114 | Conv = cast<CXXConversionDecl>(D); |
| 3115 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3116 | // If this is an explicit conversion, and we're not allowed to consider |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3117 | // explicit conversions, skip it. |
| 3118 | if (!AllowExplicit && Conv->isExplicit()) |
| 3119 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3120 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3121 | if (AllowRvalues) { |
| 3122 | bool DerivedToBase = false; |
| 3123 | bool ObjCConversion = false; |
| 3124 | if (!ConvTemplate && |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3125 | S.CompareReferenceRelationship( |
| 3126 | DeclLoc, |
| 3127 | Conv->getConversionType().getNonReferenceType() |
| 3128 | .getUnqualifiedType(), |
| 3129 | DeclType.getNonReferenceType().getUnqualifiedType(), |
| 3130 | DerivedToBase, ObjCConversion) == |
| 3131 | Sema::Ref_Incompatible) |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3132 | continue; |
| 3133 | } else { |
| 3134 | // If the conversion function doesn't return a reference type, |
| 3135 | // it can't be considered for this conversion. An rvalue reference |
| 3136 | // is only acceptable if its referencee is a function type. |
| 3137 | |
| 3138 | const ReferenceType *RefType = |
| 3139 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 3140 | if (!RefType || |
| 3141 | (!RefType->isLValueReferenceType() && |
| 3142 | !RefType->getPointeeType()->isFunctionType())) |
| 3143 | continue; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3144 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3145 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3146 | if (ConvTemplate) |
| 3147 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3148 | Init, DeclType, CandidateSet); |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3149 | else |
| 3150 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3151 | DeclType, CandidateSet); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3155 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3156 | case OR_Success: |
| 3157 | // C++ [over.ics.ref]p1: |
| 3158 | // |
| 3159 | // [...] If the parameter binds directly to the result of |
| 3160 | // applying a conversion function to the argument |
| 3161 | // expression, the implicit conversion sequence is a |
| 3162 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 3163 | // second standard conversion sequence either an identity |
| 3164 | // conversion or, if the conversion function returns an |
| 3165 | // entity of a type that is a derived class of the parameter |
| 3166 | // type, a derived-to-base Conversion. |
| 3167 | if (!Best->FinalConversion.DirectBinding) |
| 3168 | return false; |
| 3169 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3170 | if (Best->Function) |
| 3171 | S.MarkDeclarationReferenced(DeclLoc, Best->Function); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3172 | ICS.setUserDefined(); |
| 3173 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 3174 | ICS.UserDefined.After = Best->FinalConversion; |
| 3175 | ICS.UserDefined.ConversionFunction = Best->Function; |
Douglas Gregor | 83eecbe | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 3176 | ICS.UserDefined.FoundConversionFunction = Best->FoundDecl.getDecl(); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3177 | ICS.UserDefined.EllipsisConversion = false; |
| 3178 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 3179 | ICS.UserDefined.After.DirectBinding && |
| 3180 | "Expected a direct reference binding!"); |
| 3181 | return true; |
| 3182 | |
| 3183 | case OR_Ambiguous: |
| 3184 | ICS.setAmbiguous(); |
| 3185 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3186 | Cand != CandidateSet.end(); ++Cand) |
| 3187 | if (Cand->Viable) |
| 3188 | ICS.Ambiguous.addConversion(Cand->Function); |
| 3189 | return true; |
| 3190 | |
| 3191 | case OR_No_Viable_Function: |
| 3192 | case OR_Deleted: |
| 3193 | // There was no suitable conversion, or we found a deleted |
| 3194 | // conversion; continue with other checks. |
| 3195 | return false; |
| 3196 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3197 | |
Eric Christopher | 1c3d502 | 2010-06-30 18:36:32 +0000 | [diff] [blame] | 3198 | return false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3199 | } |
| 3200 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3201 | /// \brief Compute an implicit conversion sequence for reference |
| 3202 | /// initialization. |
| 3203 | static ImplicitConversionSequence |
| 3204 | TryReferenceInit(Sema &S, Expr *&Init, QualType DeclType, |
| 3205 | SourceLocation DeclLoc, |
| 3206 | bool SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 3207 | bool AllowExplicit) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3208 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 3209 | |
| 3210 | // Most paths end in a failed conversion. |
| 3211 | ImplicitConversionSequence ICS; |
| 3212 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 3213 | |
| 3214 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 3215 | QualType T2 = Init->getType(); |
| 3216 | |
| 3217 | // If the initializer is the address of an overloaded function, try |
| 3218 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3219 | // type of the resulting function. |
| 3220 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 3221 | DeclAccessPair Found; |
| 3222 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 3223 | false, Found)) |
| 3224 | T2 = Fn->getType(); |
| 3225 | } |
| 3226 | |
| 3227 | // Compute some basic properties of the types and the initializer. |
| 3228 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 3229 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3230 | bool ObjCConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3231 | Expr::Classification InitCategory = Init->Classify(S.Context); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3232 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3233 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase, |
| 3234 | ObjCConversion); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3235 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3236 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3237 | // C++0x [dcl.init.ref]p5: |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 3238 | // A reference to type "cv1 T1" is initialized by an expression |
| 3239 | // of type "cv2 T2" as follows: |
| 3240 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3241 | // -- If reference is an lvalue reference and the initializer expression |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3242 | if (!isRValRef) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3243 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 3244 | // reference-compatible with "cv2 T2," or |
| 3245 | // |
| 3246 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 3247 | if (InitCategory.isLValue() && |
| 3248 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3249 | // C++ [over.ics.ref]p1: |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3250 | // When a parameter of reference type binds directly (8.5.3) |
| 3251 | // to an argument expression, the implicit conversion sequence |
| 3252 | // is the identity conversion, unless the argument expression |
| 3253 | // has a type that is a derived class of the parameter type, |
| 3254 | // in which case the implicit conversion sequence is a |
| 3255 | // derived-to-base Conversion (13.3.3.1). |
| 3256 | ICS.setStandard(); |
| 3257 | ICS.Standard.First = ICK_Identity; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3258 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 3259 | : ObjCConversion? ICK_Compatible_Conversion |
| 3260 | : ICK_Identity; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3261 | ICS.Standard.Third = ICK_Identity; |
| 3262 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 3263 | ICS.Standard.setToType(0, T2); |
| 3264 | ICS.Standard.setToType(1, T1); |
| 3265 | ICS.Standard.setToType(2, T1); |
| 3266 | ICS.Standard.ReferenceBinding = true; |
| 3267 | ICS.Standard.DirectBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3268 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3269 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 3270 | ICS.Standard.BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3271 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3272 | ICS.Standard.CopyConstructor = 0; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3273 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3274 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 3275 | // derived-to-base conversions is suppressed when we're |
| 3276 | // computing the implicit conversion sequence (C++ |
| 3277 | // [over.best.ics]p2). |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3278 | return ICS; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3279 | } |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3280 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3281 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 3282 | // not reference-related to T2, and can be implicitly |
| 3283 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 3284 | // is reference-compatible with "cv3 T3" 92) (this |
| 3285 | // conversion is selected by enumerating the applicable |
| 3286 | // conversion functions (13.3.1.6) and choosing the best |
| 3287 | // one through overload resolution (13.3)), |
| 3288 | if (!SuppressUserConversions && T2->isRecordType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3289 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3290 | RefRelationship == Sema::Ref_Incompatible) { |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3291 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 3292 | Init, T2, /*AllowRvalues=*/false, |
| 3293 | AllowExplicit)) |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3294 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3295 | } |
| 3296 | } |
| 3297 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3298 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 3299 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3300 | // shall be an rvalue reference. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3301 | // |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 3302 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 3303 | // point, which is that, due to p2 (which short-circuits reference |
| 3304 | // binding by only attempting a simple conversion for non-direct |
| 3305 | // bindings) and p3's strange wording, we allow a const volatile |
| 3306 | // reference to bind to an rvalue. Hence the check for the presence |
| 3307 | // of "const" rather than checking for "const" being the only |
| 3308 | // qualifier. |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3309 | // This is also the point where rvalue references and lvalue inits no longer |
| 3310 | // go together. |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 3311 | if (!isRValRef && !T1.isConstQualified()) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3312 | return ICS; |
| 3313 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3314 | // -- If the initializer expression |
| 3315 | // |
| 3316 | // -- is an xvalue, class prvalue, array prvalue or function |
| 3317 | // lvalue and "cv1T1" is reference-compatible with "cv2 T2", or |
| 3318 | if (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification && |
| 3319 | (InitCategory.isXValue() || |
| 3320 | (InitCategory.isPRValue() && (T2->isRecordType() || T2->isArrayType())) || |
| 3321 | (InitCategory.isLValue() && T2->isFunctionType()))) { |
| 3322 | ICS.setStandard(); |
| 3323 | ICS.Standard.First = ICK_Identity; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3324 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3325 | : ObjCConversion? ICK_Compatible_Conversion |
| 3326 | : ICK_Identity; |
| 3327 | ICS.Standard.Third = ICK_Identity; |
| 3328 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 3329 | ICS.Standard.setToType(0, T2); |
| 3330 | ICS.Standard.setToType(1, T1); |
| 3331 | ICS.Standard.setToType(2, T1); |
| 3332 | ICS.Standard.ReferenceBinding = true; |
| 3333 | // In C++0x, this is always a direct binding. In C++98/03, it's a direct |
| 3334 | // binding unless we're binding to a class prvalue. |
| 3335 | // Note: Although xvalues wouldn't normally show up in C++98/03 code, we |
| 3336 | // allow the use of rvalue references in C++98/03 for the benefit of |
| 3337 | // standard library implementors; therefore, we need the xvalue check here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3338 | ICS.Standard.DirectBinding = |
| 3339 | S.getLangOptions().CPlusPlus0x || |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3340 | (InitCategory.isPRValue() && !T2->isRecordType()); |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3341 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3342 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3343 | ICS.Standard.BindsToRvalue = InitCategory.isRValue(); |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3344 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3345 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3346 | return ICS; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3347 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3348 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3349 | // -- has a class type (i.e., T2 is a class type), where T1 is not |
| 3350 | // reference-related to T2, and can be implicitly converted to |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3351 | // an xvalue, class prvalue, or function lvalue of type |
| 3352 | // "cv3 T3", where "cv1 T1" is reference-compatible with |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3353 | // "cv3 T3", |
| 3354 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3355 | // then the reference is bound to the value of the initializer |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3356 | // expression in the first case and to the result of the conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3357 | // in the second case (or, in either case, to an appropriate base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3358 | // class subobject). |
| 3359 | if (!SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3360 | T2->isRecordType() && !S.RequireCompleteType(DeclLoc, T2, 0) && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3361 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 3362 | Init, T2, /*AllowRvalues=*/true, |
| 3363 | AllowExplicit)) { |
| 3364 | // In the second case, if the reference is an rvalue reference |
| 3365 | // and the second standard conversion sequence of the |
| 3366 | // user-defined conversion sequence includes an lvalue-to-rvalue |
| 3367 | // conversion, the program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3368 | if (ICS.isUserDefined() && isRValRef && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3369 | ICS.UserDefined.After.First == ICK_Lvalue_To_Rvalue) |
| 3370 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 3371 | |
Douglas Gregor | 68ed68b | 2011-01-21 16:36:05 +0000 | [diff] [blame] | 3372 | return ICS; |
Rafael Espindola | aa5952c | 2011-01-22 15:32:35 +0000 | [diff] [blame] | 3373 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3374 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3375 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 3376 | // initialized from the initializer expression using the |
| 3377 | // rules for a non-reference copy initialization (8.5). The |
| 3378 | // reference is then bound to the temporary. If T1 is |
| 3379 | // reference-related to T2, cv1 must be the same |
| 3380 | // cv-qualification as, or greater cv-qualification than, |
| 3381 | // cv2; otherwise, the program is ill-formed. |
| 3382 | if (RefRelationship == Sema::Ref_Related) { |
| 3383 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 3384 | // we would be reference-compatible or reference-compatible with |
| 3385 | // added qualification. But that wasn't the case, so the reference |
| 3386 | // initialization fails. |
| 3387 | return ICS; |
| 3388 | } |
| 3389 | |
| 3390 | // If at least one of the types is a class type, the types are not |
| 3391 | // related, and we aren't allowed any user conversions, the |
| 3392 | // reference binding fails. This case is important for breaking |
| 3393 | // recursion, since TryImplicitConversion below will attempt to |
| 3394 | // create a temporary through the use of a copy constructor. |
| 3395 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 3396 | (T1->isRecordType() || T2->isRecordType())) |
| 3397 | return ICS; |
| 3398 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 3399 | // If T1 is reference-related to T2 and the reference is an rvalue |
| 3400 | // reference, the initializer expression shall not be an lvalue. |
| 3401 | if (RefRelationship >= Sema::Ref_Related && |
| 3402 | isRValRef && Init->Classify(S.Context).isLValue()) |
| 3403 | return ICS; |
| 3404 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3405 | // C++ [over.ics.ref]p2: |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3406 | // When a parameter of reference type is not bound directly to |
| 3407 | // an argument expression, the conversion sequence is the one |
| 3408 | // required to convert the argument expression to the |
| 3409 | // underlying type of the reference according to |
| 3410 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 3411 | // to copy-initializing a temporary of the underlying type with |
| 3412 | // the argument expression. Any difference in top-level |
| 3413 | // cv-qualification is subsumed by the initialization itself |
| 3414 | // and does not constitute a conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3415 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
| 3416 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3417 | /*InOverloadResolution=*/false, |
| 3418 | /*CStyle=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3419 | |
| 3420 | // Of course, that's still a reference binding. |
| 3421 | if (ICS.isStandard()) { |
| 3422 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3423 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3424 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 3425 | ICS.Standard.BindsToRvalue = true; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3426 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3427 | } else if (ICS.isUserDefined()) { |
| 3428 | ICS.UserDefined.After.ReferenceBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3429 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3430 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 3431 | ICS.Standard.BindsToRvalue = true; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3432 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3433 | } |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 3434 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3435 | return ICS; |
| 3436 | } |
| 3437 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3438 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 3439 | /// ToType from the expression From. Return the implicit conversion |
| 3440 | /// sequence required to pass this argument, which may be a bad |
| 3441 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3442 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | 74e386e | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 3443 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3444 | static ImplicitConversionSequence |
| 3445 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3446 | bool SuppressUserConversions, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3447 | bool InOverloadResolution) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3448 | if (ToType->isReferenceType()) |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3449 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3450 | /*FIXME:*/From->getLocStart(), |
| 3451 | SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 3452 | /*AllowExplicit=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3453 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3454 | return TryImplicitConversion(S, From, ToType, |
| 3455 | SuppressUserConversions, |
| 3456 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3457 | InOverloadResolution, |
| 3458 | /*CStyle=*/false); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3459 | } |
| 3460 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3461 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 3462 | /// parameter of the given member function (@c Method) from the |
| 3463 | /// expression @p From. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3464 | static ImplicitConversionSequence |
| 3465 | TryObjectArgumentInitialization(Sema &S, QualType OrigFromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3466 | Expr::Classification FromClassification, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3467 | CXXMethodDecl *Method, |
| 3468 | CXXRecordDecl *ActingContext) { |
| 3469 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 3470 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 3471 | // const volatile object. |
| 3472 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 3473 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3474 | QualType ImplicitParamType = S.Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3475 | |
| 3476 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 3477 | // to exit early. |
| 3478 | ImplicitConversionSequence ICS; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3479 | |
| 3480 | // We need to have an object of class type. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 3481 | QualType FromType = OrigFromType; |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3482 | if (const PointerType *PT = FromType->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3483 | FromType = PT->getPointeeType(); |
| 3484 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3485 | // When we had a pointer, it's implicitly dereferenced, so we |
| 3486 | // better have an lvalue. |
| 3487 | assert(FromClassification.isLValue()); |
| 3488 | } |
| 3489 | |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3490 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3491 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3492 | // C++0x [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3493 | // For non-static member functions, the type of the implicit object |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3494 | // parameter is |
| 3495 | // |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3496 | // - "lvalue reference to cv X" for functions declared without a |
| 3497 | // ref-qualifier or with the & ref-qualifier |
| 3498 | // - "rvalue reference to cv X" for functions declared with the && |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3499 | // ref-qualifier |
| 3500 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3501 | // where X is the class of which the function is a member and cv is the |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3502 | // cv-qualification on the member function declaration. |
| 3503 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3504 | // However, when finding an implicit conversion sequence for the argument, we |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3505 | // are not allowed to create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3506 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 3507 | // reference binding here, that allows class rvalues to bind to |
| 3508 | // non-constant references. |
| 3509 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3510 | // First check the qualifiers. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3511 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3512 | if (ImplicitParamType.getCVRQualifiers() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3513 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3514 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3515 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
| 3516 | OrigFromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3517 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3518 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3519 | |
| 3520 | // Check that we have either the same type or a derived type. It |
| 3521 | // affects the conversion rank. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3522 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3523 | ImplicitConversionKind SecondKind; |
| 3524 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 3525 | SecondKind = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3526 | } else if (S.IsDerivedFrom(FromType, ClassType)) |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3527 | SecondKind = ICK_Derived_To_Base; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3528 | else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3529 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 3530 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3531 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3532 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3533 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3534 | // Check the ref-qualifier. |
| 3535 | switch (Method->getRefQualifier()) { |
| 3536 | case RQ_None: |
| 3537 | // Do nothing; we don't care about lvalueness or rvalueness. |
| 3538 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3539 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3540 | case RQ_LValue: |
| 3541 | if (!FromClassification.isLValue() && Quals != Qualifiers::Const) { |
| 3542 | // non-const lvalue reference cannot bind to an rvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3543 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3544 | ImplicitParamType); |
| 3545 | return ICS; |
| 3546 | } |
| 3547 | break; |
| 3548 | |
| 3549 | case RQ_RValue: |
| 3550 | if (!FromClassification.isRValue()) { |
| 3551 | // rvalue reference cannot bind to an lvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3552 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3553 | ImplicitParamType); |
| 3554 | return ICS; |
| 3555 | } |
| 3556 | break; |
| 3557 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3558 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3559 | // Success. Mark this as a reference binding. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3560 | ICS.setStandard(); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3561 | ICS.Standard.setAsIdentityConversion(); |
| 3562 | ICS.Standard.Second = SecondKind; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3563 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3564 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3565 | ICS.Standard.ReferenceBinding = true; |
| 3566 | ICS.Standard.DirectBinding = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3567 | ICS.Standard.IsLvalueReference = Method->getRefQualifier() != RQ_RValue; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3568 | ICS.Standard.BindsToFunctionLvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3569 | ICS.Standard.BindsToRvalue = FromClassification.isRValue(); |
| 3570 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier |
| 3571 | = (Method->getRefQualifier() == RQ_None); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3572 | return ICS; |
| 3573 | } |
| 3574 | |
| 3575 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 3576 | /// the implicit object parameter for the given Method with the given |
| 3577 | /// expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3578 | ExprResult |
| 3579 | Sema::PerformObjectArgumentInitialization(Expr *From, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3580 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3581 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3582 | CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3583 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3584 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3585 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3586 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3587 | Expr::Classification FromClassification; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3588 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3589 | FromRecordType = PT->getPointeeType(); |
| 3590 | DestType = Method->getThisType(Context); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3591 | FromClassification = Expr::Classification::makeSimpleLValue(); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3592 | } else { |
| 3593 | FromRecordType = From->getType(); |
| 3594 | DestType = ImplicitParamRecordType; |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3595 | FromClassification = From->Classify(Context); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3596 | } |
| 3597 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3598 | // Note that we always use the true parent context when performing |
| 3599 | // the actual argument initialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3600 | ImplicitConversionSequence ICS |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3601 | = TryObjectArgumentInitialization(*this, From->getType(), FromClassification, |
| 3602 | Method, Method->getParent()); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 3603 | if (ICS.isBad()) { |
| 3604 | if (ICS.Bad.Kind == BadConversionSequence::bad_qualifiers) { |
| 3605 | Qualifiers FromQs = FromRecordType.getQualifiers(); |
| 3606 | Qualifiers ToQs = DestType.getQualifiers(); |
| 3607 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 3608 | if (CVR) { |
| 3609 | Diag(From->getSourceRange().getBegin(), |
| 3610 | diag::err_member_function_call_bad_cvr) |
| 3611 | << Method->getDeclName() << FromRecordType << (CVR - 1) |
| 3612 | << From->getSourceRange(); |
| 3613 | Diag(Method->getLocation(), diag::note_previous_decl) |
| 3614 | << Method->getDeclName(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3615 | return ExprError(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 3616 | } |
| 3617 | } |
| 3618 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3619 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3620 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3621 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 3622 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3623 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3624 | if (ICS.Standard.Second == ICK_Derived_To_Base) { |
| 3625 | ExprResult FromRes = |
| 3626 | PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
| 3627 | if (FromRes.isInvalid()) |
| 3628 | return ExprError(); |
| 3629 | From = FromRes.take(); |
| 3630 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3631 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3632 | if (!Context.hasSameType(From->getType(), DestType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3633 | From = ImpCastExprToType(From, DestType, CK_NoOp, |
| 3634 | From->getType()->isPointerType() ? VK_RValue : VK_LValue).take(); |
| 3635 | return Owned(From); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3636 | } |
| 3637 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3638 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 3639 | /// expression From to bool (C++0x [conv]p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3640 | static ImplicitConversionSequence |
| 3641 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
Douglas Gregor | c6dfe19 | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 3642 | // FIXME: This is pretty broken. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3643 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 3644 | // FIXME: Are these flags correct? |
| 3645 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3646 | /*AllowExplicit=*/true, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3647 | /*InOverloadResolution=*/false, |
| 3648 | /*CStyle=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3649 | } |
| 3650 | |
| 3651 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 3652 | /// of the expression From to bool (C++0x [conv]p3). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3653 | ExprResult Sema::PerformContextuallyConvertToBool(Expr *From) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3654 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3655 | if (!ICS.isBad()) |
| 3656 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3657 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3658 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3659 | return Diag(From->getSourceRange().getBegin(), |
| 3660 | diag::err_typecheck_bool_condition) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3661 | << From->getType() << From->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3662 | return ExprError(); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3663 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3664 | |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3665 | /// TryContextuallyConvertToObjCId - Attempt to contextually convert the |
| 3666 | /// expression From to 'id'. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3667 | static ImplicitConversionSequence |
| 3668 | TryContextuallyConvertToObjCId(Sema &S, Expr *From) { |
| 3669 | QualType Ty = S.Context.getObjCIdType(); |
| 3670 | return TryImplicitConversion(S, From, Ty, |
| 3671 | // FIXME: Are these flags correct? |
| 3672 | /*SuppressUserConversions=*/false, |
| 3673 | /*AllowExplicit=*/true, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3674 | /*InOverloadResolution=*/false, |
| 3675 | /*CStyle=*/false); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3676 | } |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3677 | |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3678 | /// PerformContextuallyConvertToObjCId - Perform a contextual conversion |
| 3679 | /// of the expression From to 'id'. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3680 | ExprResult Sema::PerformContextuallyConvertToObjCId(Expr *From) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3681 | QualType Ty = Context.getObjCIdType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3682 | ImplicitConversionSequence ICS = TryContextuallyConvertToObjCId(*this, From); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3683 | if (!ICS.isBad()) |
| 3684 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3685 | return ExprError(); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3686 | } |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3687 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3688 | /// \brief Attempt to convert the given expression to an integral or |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3689 | /// enumeration type. |
| 3690 | /// |
| 3691 | /// This routine will attempt to convert an expression of class type to an |
| 3692 | /// integral or enumeration type, if that class type only has a single |
| 3693 | /// conversion to an integral or enumeration type. |
| 3694 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3695 | /// \param Loc The source location of the construct that requires the |
| 3696 | /// conversion. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3697 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3698 | /// \param FromE The expression we're converting from. |
| 3699 | /// |
| 3700 | /// \param NotIntDiag The diagnostic to be emitted if the expression does not |
| 3701 | /// have integral or enumeration type. |
| 3702 | /// |
| 3703 | /// \param IncompleteDiag The diagnostic to be emitted if the expression has |
| 3704 | /// incomplete class type. |
| 3705 | /// |
| 3706 | /// \param ExplicitConvDiag The diagnostic to be emitted if we're calling an |
| 3707 | /// explicit conversion function (because no implicit conversion functions |
| 3708 | /// were available). This is a recovery mode. |
| 3709 | /// |
| 3710 | /// \param ExplicitConvNote The note to be emitted with \p ExplicitConvDiag, |
| 3711 | /// showing which conversion was picked. |
| 3712 | /// |
| 3713 | /// \param AmbigDiag The diagnostic to be emitted if there is more than one |
| 3714 | /// conversion function that could convert to integral or enumeration type. |
| 3715 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3716 | /// \param AmbigNote The note to be emitted with \p AmbigDiag for each |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3717 | /// usable conversion function. |
| 3718 | /// |
| 3719 | /// \param ConvDiag The diagnostic to be emitted if we are calling a conversion |
| 3720 | /// function, which may be an extension in this case. |
| 3721 | /// |
| 3722 | /// \returns The expression, converted to an integral or enumeration type if |
| 3723 | /// successful. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3724 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3725 | Sema::ConvertToIntegralOrEnumerationType(SourceLocation Loc, Expr *From, |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3726 | const PartialDiagnostic &NotIntDiag, |
| 3727 | const PartialDiagnostic &IncompleteDiag, |
| 3728 | const PartialDiagnostic &ExplicitConvDiag, |
| 3729 | const PartialDiagnostic &ExplicitConvNote, |
| 3730 | const PartialDiagnostic &AmbigDiag, |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3731 | const PartialDiagnostic &AmbigNote, |
| 3732 | const PartialDiagnostic &ConvDiag) { |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3733 | // We can't perform any more checking for type-dependent expressions. |
| 3734 | if (From->isTypeDependent()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3735 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3736 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3737 | // If the expression already has integral or enumeration type, we're golden. |
| 3738 | QualType T = From->getType(); |
| 3739 | if (T->isIntegralOrEnumerationType()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3740 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3741 | |
| 3742 | // FIXME: Check for missing '()' if T is a function type? |
| 3743 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3744 | // If we don't have a class type in C++, there's no way we can get an |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3745 | // expression of integral or enumeration type. |
| 3746 | const RecordType *RecordTy = T->getAs<RecordType>(); |
| 3747 | if (!RecordTy || !getLangOptions().CPlusPlus) { |
| 3748 | Diag(Loc, NotIntDiag) |
| 3749 | << T << From->getSourceRange(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3750 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3751 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3752 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3753 | // We must have a complete class type. |
| 3754 | if (RequireCompleteType(Loc, T, IncompleteDiag)) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3755 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3756 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3757 | // Look for a conversion to an integral or enumeration type. |
| 3758 | UnresolvedSet<4> ViableConversions; |
| 3759 | UnresolvedSet<4> ExplicitConversions; |
| 3760 | const UnresolvedSetImpl *Conversions |
| 3761 | = cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3762 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3763 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3764 | E = Conversions->end(); |
| 3765 | I != E; |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3766 | ++I) { |
| 3767 | if (CXXConversionDecl *Conversion |
| 3768 | = dyn_cast<CXXConversionDecl>((*I)->getUnderlyingDecl())) |
| 3769 | if (Conversion->getConversionType().getNonReferenceType() |
| 3770 | ->isIntegralOrEnumerationType()) { |
| 3771 | if (Conversion->isExplicit()) |
| 3772 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
| 3773 | else |
| 3774 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
| 3775 | } |
| 3776 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3777 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3778 | switch (ViableConversions.size()) { |
| 3779 | case 0: |
| 3780 | if (ExplicitConversions.size() == 1) { |
| 3781 | DeclAccessPair Found = ExplicitConversions[0]; |
| 3782 | CXXConversionDecl *Conversion |
| 3783 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3784 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3785 | // The user probably meant to invoke the given explicit |
| 3786 | // conversion; use it. |
| 3787 | QualType ConvTy |
| 3788 | = Conversion->getConversionType().getNonReferenceType(); |
| 3789 | std::string TypeStr; |
| 3790 | ConvTy.getAsStringInternal(TypeStr, Context.PrintingPolicy); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3791 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3792 | Diag(Loc, ExplicitConvDiag) |
| 3793 | << T << ConvTy |
| 3794 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 3795 | "static_cast<" + TypeStr + ">(") |
| 3796 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(From->getLocEnd()), |
| 3797 | ")"); |
| 3798 | Diag(Conversion->getLocation(), ExplicitConvNote) |
| 3799 | << ConvTy->isEnumeralType() << ConvTy; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3800 | |
| 3801 | // If we aren't in a SFINAE context, build a call to the |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3802 | // explicit conversion function. |
| 3803 | if (isSFINAEContext()) |
| 3804 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3805 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3806 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3807 | ExprResult Result = BuildCXXMemberCallExpr(From, Found, Conversion); |
| 3808 | if (Result.isInvalid()) |
| 3809 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3810 | |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3811 | From = Result.get(); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3812 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3813 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3814 | // We'll complain below about a non-integral condition type. |
| 3815 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3816 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3817 | case 1: { |
| 3818 | // Apply this conversion. |
| 3819 | DeclAccessPair Found = ViableConversions[0]; |
| 3820 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3821 | |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3822 | CXXConversionDecl *Conversion |
| 3823 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 3824 | QualType ConvTy |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3825 | = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3826 | if (ConvDiag.getDiagID()) { |
| 3827 | if (isSFINAEContext()) |
| 3828 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3829 | |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3830 | Diag(Loc, ConvDiag) |
| 3831 | << T << ConvTy->isEnumeralType() << ConvTy << From->getSourceRange(); |
| 3832 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3833 | |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3834 | ExprResult Result = BuildCXXMemberCallExpr(From, Found, |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3835 | cast<CXXConversionDecl>(Found->getUnderlyingDecl())); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3836 | if (Result.isInvalid()) |
| 3837 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3838 | |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3839 | From = Result.get(); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3840 | break; |
| 3841 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3842 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3843 | default: |
| 3844 | Diag(Loc, AmbigDiag) |
| 3845 | << T << From->getSourceRange(); |
| 3846 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 3847 | CXXConversionDecl *Conv |
| 3848 | = cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 3849 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 3850 | Diag(Conv->getLocation(), AmbigNote) |
| 3851 | << ConvTy->isEnumeralType() << ConvTy; |
| 3852 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3853 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3854 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3855 | |
Douglas Gregor | acb0bd8 | 2010-06-29 23:25:20 +0000 | [diff] [blame] | 3856 | if (!From->getType()->isIntegralOrEnumerationType()) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3857 | Diag(Loc, NotIntDiag) |
| 3858 | << From->getType() << From->getSourceRange(); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3859 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3860 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3863 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3864 | /// candidate functions, using the given function call arguments. If |
| 3865 | /// @p SuppressUserConversions, then don't allow user-defined |
| 3866 | /// conversions via constructors or conversion operators. |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3867 | /// |
| 3868 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 3869 | /// based on an incomplete set of function arguments. This feature is used by |
| 3870 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3871 | void |
| 3872 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3873 | DeclAccessPair FoundDecl, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3874 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3875 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 3876 | bool SuppressUserConversions, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3877 | bool PartialOverloading) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3878 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3879 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3880 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3881 | assert(!Function->getDescribedFunctionTemplate() && |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3882 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3883 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3884 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3885 | if (!isa<CXXConstructorDecl>(Method)) { |
| 3886 | // If we get here, it's because we're calling a member function |
| 3887 | // that is named without a member access expression (e.g., |
| 3888 | // "this->f") that was either written explicitly or created |
| 3889 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3890 | // function, e.g., X::f(). We use an empty type for the implied |
| 3891 | // object argument (C++ [over.call.func]p3), and the acting context |
| 3892 | // is irrelevant. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3893 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3894 | QualType(), Expr::Classification::makeSimpleLValue(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3895 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3896 | SuppressUserConversions); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3897 | return; |
| 3898 | } |
| 3899 | // We treat a constructor like a non-member function, since its object |
| 3900 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3901 | } |
| 3902 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 3903 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3904 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3905 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3906 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3907 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3908 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3909 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 3910 | // C++ [class.copy]p3: |
| 3911 | // A member function template is never instantiated to perform the copy |
| 3912 | // of a class object to an object of its class type. |
| 3913 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3914 | if (NumArgs == 1 && |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3915 | Constructor->isSpecializationCopyingObject() && |
Douglas Gregor | 1211606 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 3916 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 3917 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3918 | return; |
| 3919 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3920 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3921 | // Add this candidate |
| 3922 | CandidateSet.push_back(OverloadCandidate()); |
| 3923 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3924 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3925 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3926 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3927 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3928 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 3929 | Candidate.ExplicitCallArguments = NumArgs; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3930 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3931 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3932 | |
| 3933 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3934 | // parameters is viable only if it has an ellipsis in its parameter |
| 3935 | // list (8.3.5). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3936 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 3937 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3938 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3939 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3940 | return; |
| 3941 | } |
| 3942 | |
| 3943 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 3944 | // is viable only if the (m+1)st parameter has a default argument |
| 3945 | // (8.3.6). For the purposes of overload resolution, the |
| 3946 | // parameter list is truncated on the right, so that there are |
| 3947 | // exactly m parameters. |
| 3948 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3949 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3950 | // Not enough arguments. |
| 3951 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3952 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3953 | return; |
| 3954 | } |
| 3955 | |
| 3956 | // Determine the implicit conversion sequences for each of the |
| 3957 | // arguments. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3958 | Candidate.Conversions.resize(NumArgs); |
| 3959 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3960 | if (ArgIdx < NumArgsInProto) { |
| 3961 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3962 | // exist for each argument an implicit conversion sequence |
| 3963 | // (13.3.3.1) that converts that argument to the corresponding |
| 3964 | // parameter of F. |
| 3965 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3966 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3967 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3968 | SuppressUserConversions, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3969 | /*InOverloadResolution=*/true); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3970 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 3971 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3972 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3973 | break; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3974 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3975 | } else { |
| 3976 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3977 | // argument for which there is no corresponding parameter is |
| 3978 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3979 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3980 | } |
| 3981 | } |
| 3982 | } |
| 3983 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3984 | /// \brief Add all of the function declarations in the given function set to |
| 3985 | /// the overload canddiate set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3986 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3987 | Expr **Args, unsigned NumArgs, |
| 3988 | OverloadCandidateSet& CandidateSet, |
| 3989 | bool SuppressUserConversions) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3990 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3991 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 3992 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3993 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3994 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3995 | cast<CXXMethodDecl>(FD)->getParent(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3996 | Args[0]->getType(), Args[0]->Classify(Context), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3997 | Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3998 | CandidateSet, SuppressUserConversions); |
| 3999 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4000 | AddOverloadCandidate(FD, F.getPair(), Args, NumArgs, CandidateSet, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4001 | SuppressUserConversions); |
| 4002 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4003 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4004 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 4005 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4006 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4007 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4008 | /*FIXME: explicit args */ 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4009 | Args[0]->getType(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4010 | Args[0]->Classify(Context), |
| 4011 | Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4012 | CandidateSet, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4013 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4014 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4015 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4016 | /*FIXME: explicit args */ 0, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4017 | Args, NumArgs, CandidateSet, |
| 4018 | SuppressUserConversions); |
| 4019 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4020 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4021 | } |
| 4022 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4023 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 4024 | /// method) as a method candidate to the given overload set. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4025 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4026 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4027 | Expr::Classification ObjectClassification, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4028 | Expr **Args, unsigned NumArgs, |
| 4029 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4030 | bool SuppressUserConversions) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4031 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4032 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4033 | |
| 4034 | if (isa<UsingShadowDecl>(Decl)) |
| 4035 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4036 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4037 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 4038 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 4039 | "Expected a member function template"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4040 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 4041 | /*ExplicitArgs*/ 0, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4042 | ObjectType, ObjectClassification, Args, NumArgs, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4043 | CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4044 | SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4045 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4046 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4047 | ObjectType, ObjectClassification, Args, NumArgs, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4048 | CandidateSet, SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4049 | } |
| 4050 | } |
| 4051 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4052 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 4053 | /// of candidate functions, using the given function call arguments |
| 4054 | /// and the object argument (@c Object). For example, in a call |
| 4055 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 4056 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 4057 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4058 | /// operators. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4059 | void |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4060 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4061 | CXXRecordDecl *ActingContext, QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4062 | Expr::Classification ObjectClassification, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4063 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4064 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4065 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4066 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4067 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4068 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4069 | assert(!isa<CXXConstructorDecl>(Method) && |
| 4070 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4071 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4072 | if (!CandidateSet.isNewCandidate(Method)) |
| 4073 | return; |
| 4074 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4075 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4076 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4077 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4078 | // Add this candidate |
| 4079 | CandidateSet.push_back(OverloadCandidate()); |
| 4080 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4081 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4082 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4083 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4084 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4085 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4086 | |
| 4087 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4088 | |
| 4089 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 4090 | // parameters is viable only if it has an ellipsis in its parameter |
| 4091 | // list (8.3.5). |
| 4092 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 4093 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4094 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4095 | return; |
| 4096 | } |
| 4097 | |
| 4098 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 4099 | // is viable only if the (m+1)st parameter has a default argument |
| 4100 | // (8.3.6). For the purposes of overload resolution, the |
| 4101 | // parameter list is truncated on the right, so that there are |
| 4102 | // exactly m parameters. |
| 4103 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 4104 | if (NumArgs < MinRequiredArgs) { |
| 4105 | // Not enough arguments. |
| 4106 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4107 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4108 | return; |
| 4109 | } |
| 4110 | |
| 4111 | Candidate.Viable = true; |
| 4112 | Candidate.Conversions.resize(NumArgs + 1); |
| 4113 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4114 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4115 | // The implicit object argument is ignored. |
| 4116 | Candidate.IgnoreObjectArgument = true; |
| 4117 | else { |
| 4118 | // Determine the implicit conversion sequence for the object |
| 4119 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4120 | Candidate.Conversions[0] |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4121 | = TryObjectArgumentInitialization(*this, ObjectType, ObjectClassification, |
| 4122 | Method, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4123 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4124 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4125 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4126 | return; |
| 4127 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4128 | } |
| 4129 | |
| 4130 | // Determine the implicit conversion sequences for each of the |
| 4131 | // arguments. |
| 4132 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 4133 | if (ArgIdx < NumArgsInProto) { |
| 4134 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 4135 | // exist for each argument an implicit conversion sequence |
| 4136 | // (13.3.3.1) that converts that argument to the corresponding |
| 4137 | // parameter of F. |
| 4138 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4139 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4140 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4141 | SuppressUserConversions, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 4142 | /*InOverloadResolution=*/true); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4143 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4144 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4145 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4146 | break; |
| 4147 | } |
| 4148 | } else { |
| 4149 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 4150 | // argument for which there is no corresponding parameter is |
| 4151 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4152 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4153 | } |
| 4154 | } |
| 4155 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4156 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4157 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 4158 | /// set, using template argument deduction to produce an appropriate member |
| 4159 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4160 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4161 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4162 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4163 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4164 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4165 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4166 | Expr::Classification ObjectClassification, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4167 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4168 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4169 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4170 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 4171 | return; |
| 4172 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4173 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4174 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4175 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4176 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4177 | // candidate functions in the usual way.113) A given name can refer to one |
| 4178 | // or more function templates and also to a set of overloaded non-template |
| 4179 | // functions. In such a case, the candidate functions generated from each |
| 4180 | // function template are combined with the set of non-template candidate |
| 4181 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4182 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4183 | FunctionDecl *Specialization = 0; |
| 4184 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4185 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4186 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4187 | CandidateSet.push_back(OverloadCandidate()); |
| 4188 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 4189 | Candidate.FoundDecl = FoundDecl; |
| 4190 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 4191 | Candidate.Viable = false; |
| 4192 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 4193 | Candidate.IsSurrogate = false; |
| 4194 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4195 | Candidate.ExplicitCallArguments = NumArgs; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4196 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4197 | Info); |
| 4198 | return; |
| 4199 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4200 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4201 | // Add the function template specialization produced by template argument |
| 4202 | // deduction as a candidate. |
| 4203 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4204 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4205 | "Specialization is not a member function?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4206 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4207 | ActingContext, ObjectType, ObjectClassification, |
| 4208 | Args, NumArgs, CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4211 | /// \brief Add a C++ function template specialization as a candidate |
| 4212 | /// in the candidate set, using template argument deduction to produce |
| 4213 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4214 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4215 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4216 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4217 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4218 | Expr **Args, unsigned NumArgs, |
| 4219 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4220 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4221 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 4222 | return; |
| 4223 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4224 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4225 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4226 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4227 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4228 | // candidate functions in the usual way.113) A given name can refer to one |
| 4229 | // or more function templates and also to a set of overloaded non-template |
| 4230 | // functions. In such a case, the candidate functions generated from each |
| 4231 | // function template are combined with the set of non-template candidate |
| 4232 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4233 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4234 | FunctionDecl *Specialization = 0; |
| 4235 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4236 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4237 | Args, NumArgs, Specialization, Info)) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4238 | CandidateSet.push_back(OverloadCandidate()); |
| 4239 | OverloadCandidate &Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4240 | Candidate.FoundDecl = FoundDecl; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4241 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 4242 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4243 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4244 | Candidate.IsSurrogate = false; |
| 4245 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4246 | Candidate.ExplicitCallArguments = NumArgs; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4247 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4248 | Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4249 | return; |
| 4250 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4251 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4252 | // Add the function template specialization produced by template argument |
| 4253 | // deduction as a candidate. |
| 4254 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4255 | AddOverloadCandidate(Specialization, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4256 | SuppressUserConversions); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4257 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4258 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4259 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4260 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4261 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4262 | /// and ToType is the type that we're eventually trying to convert to |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4263 | /// (which may or may not be the same type as the type that the |
| 4264 | /// conversion function produces). |
| 4265 | void |
| 4266 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4267 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4268 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4269 | Expr *From, QualType ToType, |
| 4270 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4271 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 4272 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4273 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4274 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 4275 | return; |
| 4276 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4277 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4278 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4279 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4280 | // Add this candidate |
| 4281 | CandidateSet.push_back(OverloadCandidate()); |
| 4282 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4283 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4284 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4285 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4286 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4287 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4288 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 4289 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4290 | Candidate.Viable = true; |
| 4291 | Candidate.Conversions.resize(1); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4292 | Candidate.ExplicitCallArguments = 1; |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4293 | |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 4294 | // C++ [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4295 | // For conversion functions, the function is considered to be a member of |
| 4296 | // the class of the implicit implied object argument for the purpose of |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 4297 | // defining the type of the implicit object parameter. |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4298 | // |
| 4299 | // Determine the implicit conversion sequence for the implicit |
| 4300 | // object parameter. |
| 4301 | QualType ImplicitParamType = From->getType(); |
| 4302 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
| 4303 | ImplicitParamType = FromPtrType->getPointeeType(); |
| 4304 | CXXRecordDecl *ConversionContext |
| 4305 | = cast<CXXRecordDecl>(ImplicitParamType->getAs<RecordType>()->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4306 | |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4307 | Candidate.Conversions[0] |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4308 | = TryObjectArgumentInitialization(*this, From->getType(), |
| 4309 | From->Classify(Context), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4310 | Conversion, ConversionContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4311 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4312 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4313 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4314 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4315 | return; |
| 4316 | } |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4317 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4318 | // We won't go through a user-define type conversion function to convert a |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 4319 | // derived to base as such conversions are given Conversion Rank. They only |
| 4320 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 4321 | QualType FromCanon |
| 4322 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 4323 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 4324 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 4325 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4326 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 4327 | return; |
| 4328 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4329 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4330 | // To determine what the conversion from the result of calling the |
| 4331 | // conversion function to the type we're eventually trying to |
| 4332 | // convert to (ToType), we need to synthesize a call to the |
| 4333 | // conversion function and attempt copy initialization from it. This |
| 4334 | // makes sure that we get the right semantics with respect to |
| 4335 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 4336 | // call on the stack and we don't need its arguments to be |
| 4337 | // well-formed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4338 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4339 | VK_LValue, From->getLocStart()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4340 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 4341 | Context.getPointerType(Conversion->getType()), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4342 | CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4343 | &ConversionRef, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4344 | |
Douglas Gregor | 7d14d38 | 2010-11-13 19:36:57 +0000 | [diff] [blame] | 4345 | QualType CallResultType |
| 4346 | = Conversion->getConversionType().getNonLValueExprType(Context); |
| 4347 | if (RequireCompleteType(From->getLocStart(), CallResultType, 0)) { |
| 4348 | Candidate.Viable = false; |
| 4349 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 4350 | return; |
| 4351 | } |
| 4352 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4353 | ExprValueKind VK = Expr::getValueKindForType(Conversion->getConversionType()); |
| 4354 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4355 | // Note that it is safe to allocate CallExpr on the stack here because |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4356 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 4357 | // allocator). |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4358 | CallExpr Call(Context, &ConversionFn, 0, 0, CallResultType, VK, |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 4359 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4360 | ImplicitConversionSequence ICS = |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4361 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 4362 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 4363 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4364 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4365 | switch (ICS.getKind()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4366 | case ImplicitConversionSequence::StandardConversion: |
| 4367 | Candidate.FinalConversion = ICS.Standard; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4368 | |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 4369 | // C++ [over.ics.user]p3: |
| 4370 | // If the user-defined conversion is specified by a specialization of a |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4371 | // conversion function template, the second standard conversion sequence |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 4372 | // shall have exact match rank. |
| 4373 | if (Conversion->getPrimaryTemplate() && |
| 4374 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 4375 | Candidate.Viable = false; |
| 4376 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 4377 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4378 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4379 | // C++0x [dcl.init.ref]p5: |
| 4380 | // In the second case, if the reference is an rvalue reference and |
| 4381 | // the second standard conversion sequence of the user-defined |
| 4382 | // conversion sequence includes an lvalue-to-rvalue conversion, the |
| 4383 | // program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4384 | if (ToType->isRValueReferenceType() && |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4385 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 4386 | Candidate.Viable = false; |
| 4387 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 4388 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4389 | break; |
| 4390 | |
| 4391 | case ImplicitConversionSequence::BadConversion: |
| 4392 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4393 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4394 | break; |
| 4395 | |
| 4396 | default: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4397 | assert(false && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4398 | "Can only end up with a standard conversion sequence or failure"); |
| 4399 | } |
| 4400 | } |
| 4401 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4402 | /// \brief Adds a conversion function template specialization |
| 4403 | /// candidate to the overload set, using template argument deduction |
| 4404 | /// to deduce the template arguments of the conversion function |
| 4405 | /// template from the type that we are converting to (C++ |
| 4406 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4407 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4408 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4409 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4410 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4411 | Expr *From, QualType ToType, |
| 4412 | OverloadCandidateSet &CandidateSet) { |
| 4413 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 4414 | "Only conversion function templates permitted here"); |
| 4415 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4416 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 4417 | return; |
| 4418 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4419 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4420 | CXXConversionDecl *Specialization = 0; |
| 4421 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4422 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4423 | Specialization, Info)) { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4424 | CandidateSet.push_back(OverloadCandidate()); |
| 4425 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 4426 | Candidate.FoundDecl = FoundDecl; |
| 4427 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 4428 | Candidate.Viable = false; |
| 4429 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 4430 | Candidate.IsSurrogate = false; |
| 4431 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4432 | Candidate.ExplicitCallArguments = 1; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4433 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4434 | Info); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4435 | return; |
| 4436 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4437 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4438 | // Add the conversion function template specialization produced by |
| 4439 | // template argument deduction as a candidate. |
| 4440 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4441 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4442 | CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4443 | } |
| 4444 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4445 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 4446 | /// converts the given @c Object to a function pointer via the |
| 4447 | /// conversion function @c Conversion, and then attempts to call it |
| 4448 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 4449 | /// the type of function that we'll eventually be calling. |
| 4450 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4451 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4452 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4453 | const FunctionProtoType *Proto, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4454 | Expr *Object, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4455 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4456 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4457 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 4458 | return; |
| 4459 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4460 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4461 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4462 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4463 | CandidateSet.push_back(OverloadCandidate()); |
| 4464 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4465 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4466 | Candidate.Function = 0; |
| 4467 | Candidate.Surrogate = Conversion; |
| 4468 | Candidate.Viable = true; |
| 4469 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4470 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4471 | Candidate.Conversions.resize(NumArgs + 1); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4472 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4473 | |
| 4474 | // Determine the implicit conversion sequence for the implicit |
| 4475 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4476 | ImplicitConversionSequence ObjectInit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4477 | = TryObjectArgumentInitialization(*this, Object->getType(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4478 | Object->Classify(Context), |
| 4479 | Conversion, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4480 | if (ObjectInit.isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4481 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4482 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4483 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4484 | return; |
| 4485 | } |
| 4486 | |
| 4487 | // The first conversion is actually a user-defined conversion whose |
| 4488 | // first conversion is ObjectInit's standard conversion (which is |
| 4489 | // effectively a reference binding). Record it as such. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4490 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4491 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 4492 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4493 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4494 | Candidate.Conversions[0].UserDefined.FoundConversionFunction |
Douglas Gregor | 83eecbe | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 4495 | = FoundDecl.getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4496 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4497 | = Candidate.Conversions[0].UserDefined.Before; |
| 4498 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 4499 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4500 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4501 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4502 | |
| 4503 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 4504 | // parameters is viable only if it has an ellipsis in its parameter |
| 4505 | // list (8.3.5). |
| 4506 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 4507 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4508 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4509 | return; |
| 4510 | } |
| 4511 | |
| 4512 | // Function types don't have any default arguments, so just check if |
| 4513 | // we have enough arguments. |
| 4514 | if (NumArgs < NumArgsInProto) { |
| 4515 | // Not enough arguments. |
| 4516 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4517 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4518 | return; |
| 4519 | } |
| 4520 | |
| 4521 | // Determine the implicit conversion sequences for each of the |
| 4522 | // arguments. |
| 4523 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 4524 | if (ArgIdx < NumArgsInProto) { |
| 4525 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 4526 | // exist for each argument an implicit conversion sequence |
| 4527 | // (13.3.3.1) that converts that argument to the corresponding |
| 4528 | // parameter of F. |
| 4529 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4530 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4531 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 4532 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 4533 | /*InOverloadResolution=*/false); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4534 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4535 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4536 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4537 | break; |
| 4538 | } |
| 4539 | } else { |
| 4540 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 4541 | // argument for which there is no corresponding parameter is |
| 4542 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4543 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4544 | } |
| 4545 | } |
| 4546 | } |
| 4547 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4548 | /// \brief Add overload candidates for overloaded operators that are |
| 4549 | /// member functions. |
| 4550 | /// |
| 4551 | /// Add the overloaded operator candidates that are member functions |
| 4552 | /// for the operator Op that was used in an operator expression such |
| 4553 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 4554 | /// CandidateSet will store the added overload candidates. (C++ |
| 4555 | /// [over.match.oper]). |
| 4556 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 4557 | SourceLocation OpLoc, |
| 4558 | Expr **Args, unsigned NumArgs, |
| 4559 | OverloadCandidateSet& CandidateSet, |
| 4560 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4561 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4562 | |
| 4563 | // C++ [over.match.oper]p3: |
| 4564 | // For a unary operator @ with an operand of a type whose |
| 4565 | // cv-unqualified version is T1, and for a binary operator @ with |
| 4566 | // a left operand of a type whose cv-unqualified version is T1 and |
| 4567 | // a right operand of a type whose cv-unqualified version is T2, |
| 4568 | // three sets of candidate functions, designated member |
| 4569 | // candidates, non-member candidates and built-in candidates, are |
| 4570 | // constructed as follows: |
| 4571 | QualType T1 = Args[0]->getType(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4572 | |
| 4573 | // -- If T1 is a class type, the set of member candidates is the |
| 4574 | // result of the qualified lookup of T1::operator@ |
| 4575 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 4576 | // empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4577 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 4578 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 4579 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 4580 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4581 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4582 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 4583 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 4584 | Operators.suppressDiagnostics(); |
| 4585 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4586 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 4587 | OperEnd = Operators.end(); |
| 4588 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4589 | ++Oper) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4590 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4591 | Args[0]->Classify(Context), Args + 1, NumArgs - 1, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4592 | CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4593 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4594 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4595 | } |
| 4596 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4597 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 4598 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 4599 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4600 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 4601 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4602 | /// operator. NumContextualBoolArguments is the number of arguments |
| 4603 | /// (at the beginning of the argument list) that will be contextually |
| 4604 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4605 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4606 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4607 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4608 | bool IsAssignmentOperator, |
| 4609 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4610 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4611 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4612 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4613 | // Add this candidate |
| 4614 | CandidateSet.push_back(OverloadCandidate()); |
| 4615 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4616 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4617 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 4618 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4619 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4620 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 4621 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4622 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 4623 | |
| 4624 | // Determine the implicit conversion sequences for each of the |
| 4625 | // arguments. |
| 4626 | Candidate.Viable = true; |
| 4627 | Candidate.Conversions.resize(NumArgs); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4628 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4629 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4630 | // C++ [over.match.oper]p4: |
| 4631 | // For the built-in assignment operators, conversions of the |
| 4632 | // left operand are restricted as follows: |
| 4633 | // -- no temporaries are introduced to hold the left operand, and |
| 4634 | // -- no user-defined conversions are applied to the left |
| 4635 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4636 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4637 | // |
| 4638 | // We block these conversions by turning off user-defined |
| 4639 | // conversions, since that is the only way that initialization of |
| 4640 | // a reference to a non-class type can occur from something that |
| 4641 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4642 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4643 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4644 | "Contextual conversion to bool requires bool type"); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4645 | Candidate.Conversions[ArgIdx] |
| 4646 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4647 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4648 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4649 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 4650 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 4651 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4652 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4653 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4654 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4655 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4656 | break; |
| 4657 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4658 | } |
| 4659 | } |
| 4660 | |
| 4661 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 4662 | /// candidate operator functions for built-in operators (C++ |
| 4663 | /// [over.built]). The types are separated into pointer types and |
| 4664 | /// enumeration types. |
| 4665 | class BuiltinCandidateTypeSet { |
| 4666 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4667 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4668 | |
| 4669 | /// PointerTypes - The set of pointer types that will be used in the |
| 4670 | /// built-in candidates. |
| 4671 | TypeSet PointerTypes; |
| 4672 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4673 | /// MemberPointerTypes - The set of member pointer types that will be |
| 4674 | /// used in the built-in candidates. |
| 4675 | TypeSet MemberPointerTypes; |
| 4676 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4677 | /// EnumerationTypes - The set of enumeration types that will be |
| 4678 | /// used in the built-in candidates. |
| 4679 | TypeSet EnumerationTypes; |
| 4680 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4681 | /// \brief The set of vector types that will be used in the built-in |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4682 | /// candidates. |
| 4683 | TypeSet VectorTypes; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4684 | |
| 4685 | /// \brief A flag indicating non-record types are viable candidates |
| 4686 | bool HasNonRecordTypes; |
| 4687 | |
| 4688 | /// \brief A flag indicating whether either arithmetic or enumeration types |
| 4689 | /// were present in the candidate set. |
| 4690 | bool HasArithmeticOrEnumeralTypes; |
| 4691 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4692 | /// Sema - The semantic analysis instance where we are building the |
| 4693 | /// candidate type set. |
| 4694 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4695 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4696 | /// Context - The AST context in which we will build the type sets. |
| 4697 | ASTContext &Context; |
| 4698 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4699 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 4700 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4701 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4702 | |
| 4703 | public: |
| 4704 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4705 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4706 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4707 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4708 | : HasNonRecordTypes(false), |
| 4709 | HasArithmeticOrEnumeralTypes(false), |
| 4710 | SemaRef(SemaRef), |
| 4711 | Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4712 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4713 | void AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4714 | SourceLocation Loc, |
| 4715 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4716 | bool AllowExplicitConversions, |
| 4717 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4718 | |
| 4719 | /// pointer_begin - First pointer type found; |
| 4720 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 4721 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4722 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4723 | iterator pointer_end() { return PointerTypes.end(); } |
| 4724 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4725 | /// member_pointer_begin - First member pointer type found; |
| 4726 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 4727 | |
| 4728 | /// member_pointer_end - Past the last member pointer type found; |
| 4729 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 4730 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4731 | /// enumeration_begin - First enumeration type found; |
| 4732 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 4733 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4734 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4735 | iterator enumeration_end() { return EnumerationTypes.end(); } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4736 | |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4737 | iterator vector_begin() { return VectorTypes.begin(); } |
| 4738 | iterator vector_end() { return VectorTypes.end(); } |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4739 | |
| 4740 | bool hasNonRecordTypes() { return HasNonRecordTypes; } |
| 4741 | bool hasArithmeticOrEnumeralTypes() { return HasArithmeticOrEnumeralTypes; } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4742 | }; |
| 4743 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4744 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4745 | /// the set of pointer types along with any more-qualified variants of |
| 4746 | /// that type. For example, if @p Ty is "int const *", this routine |
| 4747 | /// will add "int const *", "int const volatile *", "int const |
| 4748 | /// restrict *", and "int const volatile restrict *" to the set of |
| 4749 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 4750 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4751 | /// |
| 4752 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4753 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4754 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 4755 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4756 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4757 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4758 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4759 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4760 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4761 | QualType PointeeTy; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4762 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4763 | bool buildObjCPtr = false; |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4764 | if (!PointerTy) { |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4765 | if (const ObjCObjectPointerType *PTy = Ty->getAs<ObjCObjectPointerType>()) { |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4766 | PointeeTy = PTy->getPointeeType(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4767 | buildObjCPtr = true; |
| 4768 | } |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4769 | else |
| 4770 | assert(false && "type was not a pointer type!"); |
| 4771 | } |
| 4772 | else |
| 4773 | PointeeTy = PointerTy->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4774 | |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 4775 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 4776 | // (the qualifier would sink to the element type), and for another, the |
| 4777 | // only overload situation where it matters is subscript or pointer +- int, |
| 4778 | // and those shouldn't have qualifier variants anyway. |
| 4779 | if (PointeeTy->isArrayType()) |
| 4780 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4781 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 4782 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | d411b3f | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 4783 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4784 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 4785 | bool hasRestrict = VisibleQuals.hasRestrict(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4786 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4787 | // Iterate through all strict supersets of BaseCVR. |
| 4788 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 4789 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4790 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 4791 | // in the types. |
| 4792 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 4793 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4794 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4795 | if (!buildObjCPtr) |
| 4796 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
| 4797 | else |
| 4798 | PointerTypes.insert(Context.getObjCObjectPointerType(QPointeeTy)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4799 | } |
| 4800 | |
| 4801 | return true; |
| 4802 | } |
| 4803 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4804 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 4805 | /// to the set of pointer types along with any more-qualified variants of |
| 4806 | /// that type. For example, if @p Ty is "int const *", this routine |
| 4807 | /// will add "int const *", "int const volatile *", "int const |
| 4808 | /// restrict *", and "int const volatile restrict *" to the set of |
| 4809 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 4810 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4811 | /// |
| 4812 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4813 | bool |
| 4814 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 4815 | QualType Ty) { |
| 4816 | // Insert this type. |
| 4817 | if (!MemberPointerTypes.insert(Ty)) |
| 4818 | return false; |
| 4819 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4820 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 4821 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4822 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4823 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 4824 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 4825 | // (the qualifier would sink to the element type), and for another, the |
| 4826 | // only overload situation where it matters is subscript or pointer +- int, |
| 4827 | // and those shouldn't have qualifier variants anyway. |
| 4828 | if (PointeeTy->isArrayType()) |
| 4829 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4830 | const Type *ClassTy = PointerTy->getClass(); |
| 4831 | |
| 4832 | // Iterate through all strict supersets of the pointee type's CVR |
| 4833 | // qualifiers. |
| 4834 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 4835 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 4836 | if ((CVR | BaseCVR) != CVR) continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4837 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4838 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4839 | MemberPointerTypes.insert( |
| 4840 | Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4841 | } |
| 4842 | |
| 4843 | return true; |
| 4844 | } |
| 4845 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4846 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 4847 | /// Ty can be implicit converted to the given set of @p Types. We're |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4848 | /// primarily interested in pointer types and enumeration types. We also |
| 4849 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4850 | /// AllowUserConversions is true if we should look at the conversion |
| 4851 | /// functions of a class type, and AllowExplicitConversions if we |
| 4852 | /// should also include the explicit conversion functions of a class |
| 4853 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4854 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4855 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4856 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4857 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4858 | bool AllowExplicitConversions, |
| 4859 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4860 | // Only deal with canonical types. |
| 4861 | Ty = Context.getCanonicalType(Ty); |
| 4862 | |
| 4863 | // Look through reference types; they aren't part of the type of an |
| 4864 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4865 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4866 | Ty = RefTy->getPointeeType(); |
| 4867 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4868 | // If we're dealing with an array type, decay to the pointer. |
| 4869 | if (Ty->isArrayType()) |
| 4870 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 4871 | |
| 4872 | // Otherwise, we don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4873 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4874 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4875 | // Flag if we ever add a non-record type. |
| 4876 | const RecordType *TyRec = Ty->getAs<RecordType>(); |
| 4877 | HasNonRecordTypes = HasNonRecordTypes || !TyRec; |
| 4878 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4879 | // Flag if we encounter an arithmetic type. |
| 4880 | HasArithmeticOrEnumeralTypes = |
| 4881 | HasArithmeticOrEnumeralTypes || Ty->isArithmeticType(); |
| 4882 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4883 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
| 4884 | PointerTypes.insert(Ty); |
| 4885 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4886 | // Insert our type, and its more-qualified variants, into the set |
| 4887 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4888 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4889 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4890 | } else if (Ty->isMemberPointerType()) { |
| 4891 | // Member pointers are far easier, since the pointee can't be converted. |
| 4892 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 4893 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4894 | } else if (Ty->isEnumeralType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4895 | HasArithmeticOrEnumeralTypes = true; |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4896 | EnumerationTypes.insert(Ty); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4897 | } else if (Ty->isVectorType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4898 | // We treat vector types as arithmetic types in many contexts as an |
| 4899 | // extension. |
| 4900 | HasArithmeticOrEnumeralTypes = true; |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4901 | VectorTypes.insert(Ty); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4902 | } else if (AllowUserConversions && TyRec) { |
| 4903 | // No conversion functions in incomplete types. |
| 4904 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) |
| 4905 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4906 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4907 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| 4908 | const UnresolvedSetImpl *Conversions |
| 4909 | = ClassDecl->getVisibleConversionFunctions(); |
| 4910 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 4911 | E = Conversions->end(); I != E; ++I) { |
| 4912 | NamedDecl *D = I.getDecl(); |
| 4913 | if (isa<UsingShadowDecl>(D)) |
| 4914 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4915 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4916 | // Skip conversion function templates; they don't tell us anything |
| 4917 | // about which builtin types we can convert to. |
| 4918 | if (isa<FunctionTemplateDecl>(D)) |
| 4919 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4920 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4921 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
| 4922 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
| 4923 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
| 4924 | VisibleQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4925 | } |
| 4926 | } |
| 4927 | } |
| 4928 | } |
| 4929 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4930 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 4931 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 4932 | /// given type to the candidate set. |
| 4933 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 4934 | QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4935 | Expr **Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4936 | unsigned NumArgs, |
| 4937 | OverloadCandidateSet &CandidateSet) { |
| 4938 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4939 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4940 | // T& operator=(T&, T) |
| 4941 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 4942 | ParamTypes[1] = T; |
| 4943 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4944 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4945 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4946 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 4947 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4948 | ParamTypes[0] |
| 4949 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4950 | ParamTypes[1] = T; |
| 4951 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4952 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4953 | } |
| 4954 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4955 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 4956 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 4957 | /// if any, found in visible type conversion functions found in ArgExpr's type. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4958 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 4959 | Qualifiers VRQuals; |
| 4960 | const RecordType *TyRec; |
| 4961 | if (const MemberPointerType *RHSMPType = |
| 4962 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4963 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4964 | else |
| 4965 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 4966 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4967 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4968 | VRQuals.addVolatile(); |
| 4969 | VRQuals.addRestrict(); |
| 4970 | return VRQuals; |
| 4971 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4972 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4973 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4974 | if (!ClassDecl->hasDefinition()) |
| 4975 | return VRQuals; |
| 4976 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4977 | const UnresolvedSetImpl *Conversions = |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 4978 | ClassDecl->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4979 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4980 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4981 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4982 | NamedDecl *D = I.getDecl(); |
| 4983 | if (isa<UsingShadowDecl>(D)) |
| 4984 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 4985 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4986 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 4987 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 4988 | CanTy = ResTypeRef->getPointeeType(); |
| 4989 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 4990 | // as see them. |
| 4991 | bool done = false; |
| 4992 | while (!done) { |
| 4993 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 4994 | CanTy = ResTypePtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4995 | else if (const MemberPointerType *ResTypeMPtr = |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4996 | CanTy->getAs<MemberPointerType>()) |
| 4997 | CanTy = ResTypeMPtr->getPointeeType(); |
| 4998 | else |
| 4999 | done = true; |
| 5000 | if (CanTy.isVolatileQualified()) |
| 5001 | VRQuals.addVolatile(); |
| 5002 | if (CanTy.isRestrictQualified()) |
| 5003 | VRQuals.addRestrict(); |
| 5004 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 5005 | return VRQuals; |
| 5006 | } |
| 5007 | } |
| 5008 | } |
| 5009 | return VRQuals; |
| 5010 | } |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 5011 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5012 | namespace { |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 5013 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5014 | /// \brief Helper class to manage the addition of builtin operator overload |
| 5015 | /// candidates. It provides shared state and utility methods used throughout |
| 5016 | /// the process, as well as a helper method to add each group of builtin |
| 5017 | /// operator overloads from the standard to a candidate set. |
| 5018 | class BuiltinOperatorOverloadBuilder { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5019 | // Common instance state available to all overload candidate addition methods. |
| 5020 | Sema &S; |
| 5021 | Expr **Args; |
| 5022 | unsigned NumArgs; |
| 5023 | Qualifiers VisibleTypeConversionsQuals; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5024 | bool HasArithmeticOrEnumeralCandidateType; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5025 | llvm::SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes; |
| 5026 | OverloadCandidateSet &CandidateSet; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5027 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5028 | // Define some constants used to index and iterate over the arithemetic types |
| 5029 | // provided via the getArithmeticType() method below. |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 5030 | // The "promoted arithmetic types" are the arithmetic |
| 5031 | // types are that preserved by promotion (C++ [over.built]p2). |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 5032 | static const unsigned FirstIntegralType = 3; |
| 5033 | static const unsigned LastIntegralType = 18; |
| 5034 | static const unsigned FirstPromotedIntegralType = 3, |
| 5035 | LastPromotedIntegralType = 9; |
| 5036 | static const unsigned FirstPromotedArithmeticType = 0, |
| 5037 | LastPromotedArithmeticType = 9; |
| 5038 | static const unsigned NumArithmeticTypes = 18; |
| 5039 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5040 | /// \brief Get the canonical type for a given arithmetic type index. |
| 5041 | CanQualType getArithmeticType(unsigned index) { |
| 5042 | assert(index < NumArithmeticTypes); |
| 5043 | static CanQualType ASTContext::* const |
| 5044 | ArithmeticTypes[NumArithmeticTypes] = { |
| 5045 | // Start of promoted types. |
| 5046 | &ASTContext::FloatTy, |
| 5047 | &ASTContext::DoubleTy, |
| 5048 | &ASTContext::LongDoubleTy, |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 5049 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5050 | // Start of integral types. |
| 5051 | &ASTContext::IntTy, |
| 5052 | &ASTContext::LongTy, |
| 5053 | &ASTContext::LongLongTy, |
| 5054 | &ASTContext::UnsignedIntTy, |
| 5055 | &ASTContext::UnsignedLongTy, |
| 5056 | &ASTContext::UnsignedLongLongTy, |
| 5057 | // End of promoted types. |
| 5058 | |
| 5059 | &ASTContext::BoolTy, |
| 5060 | &ASTContext::CharTy, |
| 5061 | &ASTContext::WCharTy, |
| 5062 | &ASTContext::Char16Ty, |
| 5063 | &ASTContext::Char32Ty, |
| 5064 | &ASTContext::SignedCharTy, |
| 5065 | &ASTContext::ShortTy, |
| 5066 | &ASTContext::UnsignedCharTy, |
| 5067 | &ASTContext::UnsignedShortTy, |
| 5068 | // End of integral types. |
| 5069 | // FIXME: What about complex? |
| 5070 | }; |
| 5071 | return S.Context.*ArithmeticTypes[index]; |
| 5072 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5073 | |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5074 | /// \brief Gets the canonical type resulting from the usual arithemetic |
| 5075 | /// converions for the given arithmetic types. |
| 5076 | CanQualType getUsualArithmeticConversions(unsigned L, unsigned R) { |
| 5077 | // Accelerator table for performing the usual arithmetic conversions. |
| 5078 | // The rules are basically: |
| 5079 | // - if either is floating-point, use the wider floating-point |
| 5080 | // - if same signedness, use the higher rank |
| 5081 | // - if same size, use unsigned of the higher rank |
| 5082 | // - use the larger type |
| 5083 | // These rules, together with the axiom that higher ranks are |
| 5084 | // never smaller, are sufficient to precompute all of these results |
| 5085 | // *except* when dealing with signed types of higher rank. |
| 5086 | // (we could precompute SLL x UI for all known platforms, but it's |
| 5087 | // better not to make any assumptions). |
| 5088 | enum PromotedType { |
| 5089 | Flt, Dbl, LDbl, SI, SL, SLL, UI, UL, ULL, Dep=-1 |
| 5090 | }; |
| 5091 | static PromotedType ConversionsTable[LastPromotedArithmeticType] |
| 5092 | [LastPromotedArithmeticType] = { |
| 5093 | /* Flt*/ { Flt, Dbl, LDbl, Flt, Flt, Flt, Flt, Flt, Flt }, |
| 5094 | /* Dbl*/ { Dbl, Dbl, LDbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl }, |
| 5095 | /*LDbl*/ { LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl }, |
| 5096 | /* SI*/ { Flt, Dbl, LDbl, SI, SL, SLL, UI, UL, ULL }, |
| 5097 | /* SL*/ { Flt, Dbl, LDbl, SL, SL, SLL, Dep, UL, ULL }, |
| 5098 | /* SLL*/ { Flt, Dbl, LDbl, SLL, SLL, SLL, Dep, Dep, ULL }, |
| 5099 | /* UI*/ { Flt, Dbl, LDbl, UI, Dep, Dep, UI, UL, ULL }, |
| 5100 | /* UL*/ { Flt, Dbl, LDbl, UL, UL, Dep, UL, UL, ULL }, |
| 5101 | /* ULL*/ { Flt, Dbl, LDbl, ULL, ULL, ULL, ULL, ULL, ULL }, |
| 5102 | }; |
| 5103 | |
| 5104 | assert(L < LastPromotedArithmeticType); |
| 5105 | assert(R < LastPromotedArithmeticType); |
| 5106 | int Idx = ConversionsTable[L][R]; |
| 5107 | |
| 5108 | // Fast path: the table gives us a concrete answer. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5109 | if (Idx != Dep) return getArithmeticType(Idx); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5110 | |
| 5111 | // Slow path: we need to compare widths. |
| 5112 | // An invariant is that the signed type has higher rank. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5113 | CanQualType LT = getArithmeticType(L), |
| 5114 | RT = getArithmeticType(R); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5115 | unsigned LW = S.Context.getIntWidth(LT), |
| 5116 | RW = S.Context.getIntWidth(RT); |
| 5117 | |
| 5118 | // If they're different widths, use the signed type. |
| 5119 | if (LW > RW) return LT; |
| 5120 | else if (LW < RW) return RT; |
| 5121 | |
| 5122 | // Otherwise, use the unsigned type of the signed type's rank. |
| 5123 | if (L == SL || R == SL) return S.Context.UnsignedLongTy; |
| 5124 | assert(L == SLL || R == SLL); |
| 5125 | return S.Context.UnsignedLongLongTy; |
| 5126 | } |
| 5127 | |
Chandler Carruth | 3c69dc4 | 2010-12-12 09:22:45 +0000 | [diff] [blame] | 5128 | /// \brief Helper method to factor out the common pattern of adding overloads |
| 5129 | /// for '++' and '--' builtin operators. |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5130 | void addPlusPlusMinusMinusStyleOverloads(QualType CandidateTy, |
| 5131 | bool HasVolatile) { |
| 5132 | QualType ParamTypes[2] = { |
| 5133 | S.Context.getLValueReferenceType(CandidateTy), |
| 5134 | S.Context.IntTy |
| 5135 | }; |
| 5136 | |
| 5137 | // Non-volatile version. |
| 5138 | if (NumArgs == 1) |
| 5139 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 5140 | else |
| 5141 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 5142 | |
| 5143 | // Use a heuristic to reduce number of builtin candidates in the set: |
| 5144 | // add volatile version only if there are conversions to a volatile type. |
| 5145 | if (HasVolatile) { |
| 5146 | ParamTypes[0] = |
| 5147 | S.Context.getLValueReferenceType( |
| 5148 | S.Context.getVolatileType(CandidateTy)); |
| 5149 | if (NumArgs == 1) |
| 5150 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 5151 | else |
| 5152 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 5153 | } |
| 5154 | } |
| 5155 | |
| 5156 | public: |
| 5157 | BuiltinOperatorOverloadBuilder( |
| 5158 | Sema &S, Expr **Args, unsigned NumArgs, |
| 5159 | Qualifiers VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5160 | bool HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5161 | llvm::SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes, |
| 5162 | OverloadCandidateSet &CandidateSet) |
| 5163 | : S(S), Args(Args), NumArgs(NumArgs), |
| 5164 | VisibleTypeConversionsQuals(VisibleTypeConversionsQuals), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5165 | HasArithmeticOrEnumeralCandidateType( |
| 5166 | HasArithmeticOrEnumeralCandidateType), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5167 | CandidateTypes(CandidateTypes), |
| 5168 | CandidateSet(CandidateSet) { |
| 5169 | // Validate some of our static helper constants in debug builds. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5170 | assert(getArithmeticType(FirstPromotedIntegralType) == S.Context.IntTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5171 | "Invalid first promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5172 | assert(getArithmeticType(LastPromotedIntegralType - 1) |
| 5173 | == S.Context.UnsignedLongLongTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5174 | "Invalid last promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5175 | assert(getArithmeticType(FirstPromotedArithmeticType) |
| 5176 | == S.Context.FloatTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5177 | "Invalid first promoted arithmetic type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5178 | assert(getArithmeticType(LastPromotedArithmeticType - 1) |
| 5179 | == S.Context.UnsignedLongLongTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5180 | "Invalid last promoted arithmetic type"); |
| 5181 | } |
| 5182 | |
| 5183 | // C++ [over.built]p3: |
| 5184 | // |
| 5185 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 5186 | // is either volatile or empty, there exist candidate operator |
| 5187 | // functions of the form |
| 5188 | // |
| 5189 | // VQ T& operator++(VQ T&); |
| 5190 | // T operator++(VQ T&, int); |
| 5191 | // |
| 5192 | // C++ [over.built]p4: |
| 5193 | // |
| 5194 | // For every pair (T, VQ), where T is an arithmetic type other |
| 5195 | // than bool, and VQ is either volatile or empty, there exist |
| 5196 | // candidate operator functions of the form |
| 5197 | // |
| 5198 | // VQ T& operator--(VQ T&); |
| 5199 | // T operator--(VQ T&, int); |
| 5200 | void addPlusPlusMinusMinusArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5201 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5202 | return; |
| 5203 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5204 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 5205 | Arith < NumArithmeticTypes; ++Arith) { |
| 5206 | addPlusPlusMinusMinusStyleOverloads( |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5207 | getArithmeticType(Arith), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5208 | VisibleTypeConversionsQuals.hasVolatile()); |
| 5209 | } |
| 5210 | } |
| 5211 | |
| 5212 | // C++ [over.built]p5: |
| 5213 | // |
| 5214 | // For every pair (T, VQ), where T is a cv-qualified or |
| 5215 | // cv-unqualified object type, and VQ is either volatile or |
| 5216 | // empty, there exist candidate operator functions of the form |
| 5217 | // |
| 5218 | // T*VQ& operator++(T*VQ&); |
| 5219 | // T*VQ& operator--(T*VQ&); |
| 5220 | // T* operator++(T*VQ&, int); |
| 5221 | // T* operator--(T*VQ&, int); |
| 5222 | void addPlusPlusMinusMinusPointerOverloads() { |
| 5223 | for (BuiltinCandidateTypeSet::iterator |
| 5224 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5225 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5226 | Ptr != PtrEnd; ++Ptr) { |
| 5227 | // Skip pointer types that aren't pointers to object types. |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5228 | if (!(*Ptr)->getPointeeType()->isObjectType()) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5229 | continue; |
| 5230 | |
| 5231 | addPlusPlusMinusMinusStyleOverloads(*Ptr, |
| 5232 | (!S.Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 5233 | VisibleTypeConversionsQuals.hasVolatile())); |
| 5234 | } |
| 5235 | } |
| 5236 | |
| 5237 | // C++ [over.built]p6: |
| 5238 | // For every cv-qualified or cv-unqualified object type T, there |
| 5239 | // exist candidate operator functions of the form |
| 5240 | // |
| 5241 | // T& operator*(T*); |
| 5242 | // |
| 5243 | // C++ [over.built]p7: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5244 | // For every function type T that does not have cv-qualifiers or a |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5245 | // ref-qualifier, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5246 | // T& operator*(T*); |
| 5247 | void addUnaryStarPointerOverloads() { |
| 5248 | for (BuiltinCandidateTypeSet::iterator |
| 5249 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5250 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5251 | Ptr != PtrEnd; ++Ptr) { |
| 5252 | QualType ParamTy = *Ptr; |
| 5253 | QualType PointeeTy = ParamTy->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5254 | if (!PointeeTy->isObjectType() && !PointeeTy->isFunctionType()) |
| 5255 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5256 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5257 | if (const FunctionProtoType *Proto =PointeeTy->getAs<FunctionProtoType>()) |
| 5258 | if (Proto->getTypeQuals() || Proto->getRefQualifier()) |
| 5259 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5260 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5261 | S.AddBuiltinCandidate(S.Context.getLValueReferenceType(PointeeTy), |
| 5262 | &ParamTy, Args, 1, CandidateSet); |
| 5263 | } |
| 5264 | } |
| 5265 | |
| 5266 | // C++ [over.built]p9: |
| 5267 | // For every promoted arithmetic type T, there exist candidate |
| 5268 | // operator functions of the form |
| 5269 | // |
| 5270 | // T operator+(T); |
| 5271 | // T operator-(T); |
| 5272 | void addUnaryPlusOrMinusArithmeticOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5273 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5274 | return; |
| 5275 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5276 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 5277 | Arith < LastPromotedArithmeticType; ++Arith) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5278 | QualType ArithTy = getArithmeticType(Arith); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5279 | S.AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 5280 | } |
| 5281 | |
| 5282 | // Extension: We also add these operators for vector types. |
| 5283 | for (BuiltinCandidateTypeSet::iterator |
| 5284 | Vec = CandidateTypes[0].vector_begin(), |
| 5285 | VecEnd = CandidateTypes[0].vector_end(); |
| 5286 | Vec != VecEnd; ++Vec) { |
| 5287 | QualType VecTy = *Vec; |
| 5288 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 5289 | } |
| 5290 | } |
| 5291 | |
| 5292 | // C++ [over.built]p8: |
| 5293 | // For every type T, there exist candidate operator functions of |
| 5294 | // the form |
| 5295 | // |
| 5296 | // T* operator+(T*); |
| 5297 | void addUnaryPlusPointerOverloads() { |
| 5298 | for (BuiltinCandidateTypeSet::iterator |
| 5299 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5300 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5301 | Ptr != PtrEnd; ++Ptr) { |
| 5302 | QualType ParamTy = *Ptr; |
| 5303 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 5304 | } |
| 5305 | } |
| 5306 | |
| 5307 | // C++ [over.built]p10: |
| 5308 | // For every promoted integral type T, there exist candidate |
| 5309 | // operator functions of the form |
| 5310 | // |
| 5311 | // T operator~(T); |
| 5312 | void addUnaryTildePromotedIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5313 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5314 | return; |
| 5315 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5316 | for (unsigned Int = FirstPromotedIntegralType; |
| 5317 | Int < LastPromotedIntegralType; ++Int) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5318 | QualType IntTy = getArithmeticType(Int); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5319 | S.AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 5320 | } |
| 5321 | |
| 5322 | // Extension: We also add this operator for vector types. |
| 5323 | for (BuiltinCandidateTypeSet::iterator |
| 5324 | Vec = CandidateTypes[0].vector_begin(), |
| 5325 | VecEnd = CandidateTypes[0].vector_end(); |
| 5326 | Vec != VecEnd; ++Vec) { |
| 5327 | QualType VecTy = *Vec; |
| 5328 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 5329 | } |
| 5330 | } |
| 5331 | |
| 5332 | // C++ [over.match.oper]p16: |
| 5333 | // For every pointer to member type T, there exist candidate operator |
| 5334 | // functions of the form |
| 5335 | // |
| 5336 | // bool operator==(T,T); |
| 5337 | // bool operator!=(T,T); |
| 5338 | void addEqualEqualOrNotEqualMemberPointerOverloads() { |
| 5339 | /// Set of (canonical) types that we've already handled. |
| 5340 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5341 | |
| 5342 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 5343 | for (BuiltinCandidateTypeSet::iterator |
| 5344 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 5345 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 5346 | MemPtr != MemPtrEnd; |
| 5347 | ++MemPtr) { |
| 5348 | // Don't add the same builtin candidate twice. |
| 5349 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 5350 | continue; |
| 5351 | |
| 5352 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 5353 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 5354 | CandidateSet); |
| 5355 | } |
| 5356 | } |
| 5357 | } |
| 5358 | |
| 5359 | // C++ [over.built]p15: |
| 5360 | // |
| 5361 | // For every pointer or enumeration type T, there exist |
| 5362 | // candidate operator functions of the form |
| 5363 | // |
| 5364 | // bool operator<(T, T); |
| 5365 | // bool operator>(T, T); |
| 5366 | // bool operator<=(T, T); |
| 5367 | // bool operator>=(T, T); |
| 5368 | // bool operator==(T, T); |
| 5369 | // bool operator!=(T, T); |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 5370 | void addRelationalPointerOrEnumeralOverloads() { |
| 5371 | // C++ [over.built]p1: |
| 5372 | // If there is a user-written candidate with the same name and parameter |
| 5373 | // types as a built-in candidate operator function, the built-in operator |
| 5374 | // function is hidden and is not included in the set of candidate |
| 5375 | // functions. |
| 5376 | // |
| 5377 | // The text is actually in a note, but if we don't implement it then we end |
| 5378 | // up with ambiguities when the user provides an overloaded operator for |
| 5379 | // an enumeration type. Note that only enumeration types have this problem, |
| 5380 | // so we track which enumeration types we've seen operators for. Also, the |
| 5381 | // only other overloaded operator with enumeration argumenst, operator=, |
| 5382 | // cannot be overloaded for enumeration types, so this is the only place |
| 5383 | // where we must suppress candidates like this. |
| 5384 | llvm::DenseSet<std::pair<CanQualType, CanQualType> > |
| 5385 | UserDefinedBinaryOperators; |
| 5386 | |
| 5387 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 5388 | if (CandidateTypes[ArgIdx].enumeration_begin() != |
| 5389 | CandidateTypes[ArgIdx].enumeration_end()) { |
| 5390 | for (OverloadCandidateSet::iterator C = CandidateSet.begin(), |
| 5391 | CEnd = CandidateSet.end(); |
| 5392 | C != CEnd; ++C) { |
| 5393 | if (!C->Viable || !C->Function || C->Function->getNumParams() != 2) |
| 5394 | continue; |
| 5395 | |
| 5396 | QualType FirstParamType = |
| 5397 | C->Function->getParamDecl(0)->getType().getUnqualifiedType(); |
| 5398 | QualType SecondParamType = |
| 5399 | C->Function->getParamDecl(1)->getType().getUnqualifiedType(); |
| 5400 | |
| 5401 | // Skip if either parameter isn't of enumeral type. |
| 5402 | if (!FirstParamType->isEnumeralType() || |
| 5403 | !SecondParamType->isEnumeralType()) |
| 5404 | continue; |
| 5405 | |
| 5406 | // Add this operator to the set of known user-defined operators. |
| 5407 | UserDefinedBinaryOperators.insert( |
| 5408 | std::make_pair(S.Context.getCanonicalType(FirstParamType), |
| 5409 | S.Context.getCanonicalType(SecondParamType))); |
| 5410 | } |
| 5411 | } |
| 5412 | } |
| 5413 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5414 | /// Set of (canonical) types that we've already handled. |
| 5415 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5416 | |
| 5417 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 5418 | for (BuiltinCandidateTypeSet::iterator |
| 5419 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 5420 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 5421 | Ptr != PtrEnd; ++Ptr) { |
| 5422 | // Don't add the same builtin candidate twice. |
| 5423 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 5424 | continue; |
| 5425 | |
| 5426 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5427 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 5428 | CandidateSet); |
| 5429 | } |
| 5430 | for (BuiltinCandidateTypeSet::iterator |
| 5431 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 5432 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 5433 | Enum != EnumEnd; ++Enum) { |
| 5434 | CanQualType CanonType = S.Context.getCanonicalType(*Enum); |
| 5435 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 5436 | // Don't add the same builtin candidate twice, or if a user defined |
| 5437 | // candidate exists. |
| 5438 | if (!AddedTypes.insert(CanonType) || |
| 5439 | UserDefinedBinaryOperators.count(std::make_pair(CanonType, |
| 5440 | CanonType))) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5441 | continue; |
| 5442 | |
| 5443 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 5444 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 5445 | CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5446 | } |
| 5447 | } |
| 5448 | } |
| 5449 | |
| 5450 | // C++ [over.built]p13: |
| 5451 | // |
| 5452 | // For every cv-qualified or cv-unqualified object type T |
| 5453 | // there exist candidate operator functions of the form |
| 5454 | // |
| 5455 | // T* operator+(T*, ptrdiff_t); |
| 5456 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 5457 | // T* operator-(T*, ptrdiff_t); |
| 5458 | // T* operator+(ptrdiff_t, T*); |
| 5459 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 5460 | // |
| 5461 | // C++ [over.built]p14: |
| 5462 | // |
| 5463 | // For every T, where T is a pointer to object type, there |
| 5464 | // exist candidate operator functions of the form |
| 5465 | // |
| 5466 | // ptrdiff_t operator-(T, T); |
| 5467 | void addBinaryPlusOrMinusPointerOverloads(OverloadedOperatorKind Op) { |
| 5468 | /// Set of (canonical) types that we've already handled. |
| 5469 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5470 | |
| 5471 | for (int Arg = 0; Arg < 2; ++Arg) { |
| 5472 | QualType AsymetricParamTypes[2] = { |
| 5473 | S.Context.getPointerDiffType(), |
| 5474 | S.Context.getPointerDiffType(), |
| 5475 | }; |
| 5476 | for (BuiltinCandidateTypeSet::iterator |
| 5477 | Ptr = CandidateTypes[Arg].pointer_begin(), |
| 5478 | PtrEnd = CandidateTypes[Arg].pointer_end(); |
| 5479 | Ptr != PtrEnd; ++Ptr) { |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5480 | QualType PointeeTy = (*Ptr)->getPointeeType(); |
| 5481 | if (!PointeeTy->isObjectType()) |
| 5482 | continue; |
| 5483 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5484 | AsymetricParamTypes[Arg] = *Ptr; |
| 5485 | if (Arg == 0 || Op == OO_Plus) { |
| 5486 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 5487 | // T* operator+(ptrdiff_t, T*); |
| 5488 | S.AddBuiltinCandidate(*Ptr, AsymetricParamTypes, Args, 2, |
| 5489 | CandidateSet); |
| 5490 | } |
| 5491 | if (Op == OO_Minus) { |
| 5492 | // ptrdiff_t operator-(T, T); |
| 5493 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 5494 | continue; |
| 5495 | |
| 5496 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5497 | S.AddBuiltinCandidate(S.Context.getPointerDiffType(), ParamTypes, |
| 5498 | Args, 2, CandidateSet); |
| 5499 | } |
| 5500 | } |
| 5501 | } |
| 5502 | } |
| 5503 | |
| 5504 | // C++ [over.built]p12: |
| 5505 | // |
| 5506 | // For every pair of promoted arithmetic types L and R, there |
| 5507 | // exist candidate operator functions of the form |
| 5508 | // |
| 5509 | // LR operator*(L, R); |
| 5510 | // LR operator/(L, R); |
| 5511 | // LR operator+(L, R); |
| 5512 | // LR operator-(L, R); |
| 5513 | // bool operator<(L, R); |
| 5514 | // bool operator>(L, R); |
| 5515 | // bool operator<=(L, R); |
| 5516 | // bool operator>=(L, R); |
| 5517 | // bool operator==(L, R); |
| 5518 | // bool operator!=(L, R); |
| 5519 | // |
| 5520 | // where LR is the result of the usual arithmetic conversions |
| 5521 | // between types L and R. |
| 5522 | // |
| 5523 | // C++ [over.built]p24: |
| 5524 | // |
| 5525 | // For every pair of promoted arithmetic types L and R, there exist |
| 5526 | // candidate operator functions of the form |
| 5527 | // |
| 5528 | // LR operator?(bool, L, R); |
| 5529 | // |
| 5530 | // where LR is the result of the usual arithmetic conversions |
| 5531 | // between types L and R. |
| 5532 | // Our candidates ignore the first parameter. |
| 5533 | void addGenericBinaryArithmeticOverloads(bool isComparison) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5534 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5535 | return; |
| 5536 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5537 | for (unsigned Left = FirstPromotedArithmeticType; |
| 5538 | Left < LastPromotedArithmeticType; ++Left) { |
| 5539 | for (unsigned Right = FirstPromotedArithmeticType; |
| 5540 | Right < LastPromotedArithmeticType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5541 | QualType LandR[2] = { getArithmeticType(Left), |
| 5542 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5543 | QualType Result = |
| 5544 | isComparison ? S.Context.BoolTy |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5545 | : getUsualArithmeticConversions(Left, Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5546 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 5547 | } |
| 5548 | } |
| 5549 | |
| 5550 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 5551 | // conditional operator for vector types. |
| 5552 | for (BuiltinCandidateTypeSet::iterator |
| 5553 | Vec1 = CandidateTypes[0].vector_begin(), |
| 5554 | Vec1End = CandidateTypes[0].vector_end(); |
| 5555 | Vec1 != Vec1End; ++Vec1) { |
| 5556 | for (BuiltinCandidateTypeSet::iterator |
| 5557 | Vec2 = CandidateTypes[1].vector_begin(), |
| 5558 | Vec2End = CandidateTypes[1].vector_end(); |
| 5559 | Vec2 != Vec2End; ++Vec2) { |
| 5560 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 5561 | QualType Result = S.Context.BoolTy; |
| 5562 | if (!isComparison) { |
| 5563 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 5564 | Result = *Vec1; |
| 5565 | else |
| 5566 | Result = *Vec2; |
| 5567 | } |
| 5568 | |
| 5569 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 5570 | } |
| 5571 | } |
| 5572 | } |
| 5573 | |
| 5574 | // C++ [over.built]p17: |
| 5575 | // |
| 5576 | // For every pair of promoted integral types L and R, there |
| 5577 | // exist candidate operator functions of the form |
| 5578 | // |
| 5579 | // LR operator%(L, R); |
| 5580 | // LR operator&(L, R); |
| 5581 | // LR operator^(L, R); |
| 5582 | // LR operator|(L, R); |
| 5583 | // L operator<<(L, R); |
| 5584 | // L operator>>(L, R); |
| 5585 | // |
| 5586 | // where LR is the result of the usual arithmetic conversions |
| 5587 | // between types L and R. |
| 5588 | void addBinaryBitwiseArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5589 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5590 | return; |
| 5591 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5592 | for (unsigned Left = FirstPromotedIntegralType; |
| 5593 | Left < LastPromotedIntegralType; ++Left) { |
| 5594 | for (unsigned Right = FirstPromotedIntegralType; |
| 5595 | Right < LastPromotedIntegralType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5596 | QualType LandR[2] = { getArithmeticType(Left), |
| 5597 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5598 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 5599 | ? LandR[0] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5600 | : getUsualArithmeticConversions(Left, Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5601 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 5602 | } |
| 5603 | } |
| 5604 | } |
| 5605 | |
| 5606 | // C++ [over.built]p20: |
| 5607 | // |
| 5608 | // For every pair (T, VQ), where T is an enumeration or |
| 5609 | // pointer to member type and VQ is either volatile or |
| 5610 | // empty, there exist candidate operator functions of the form |
| 5611 | // |
| 5612 | // VQ T& operator=(VQ T&, T); |
| 5613 | void addAssignmentMemberPointerOrEnumeralOverloads() { |
| 5614 | /// Set of (canonical) types that we've already handled. |
| 5615 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5616 | |
| 5617 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 5618 | for (BuiltinCandidateTypeSet::iterator |
| 5619 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 5620 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 5621 | Enum != EnumEnd; ++Enum) { |
| 5622 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 5623 | continue; |
| 5624 | |
| 5625 | AddBuiltinAssignmentOperatorCandidates(S, *Enum, Args, 2, |
| 5626 | CandidateSet); |
| 5627 | } |
| 5628 | |
| 5629 | for (BuiltinCandidateTypeSet::iterator |
| 5630 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 5631 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 5632 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 5633 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 5634 | continue; |
| 5635 | |
| 5636 | AddBuiltinAssignmentOperatorCandidates(S, *MemPtr, Args, 2, |
| 5637 | CandidateSet); |
| 5638 | } |
| 5639 | } |
| 5640 | } |
| 5641 | |
| 5642 | // C++ [over.built]p19: |
| 5643 | // |
| 5644 | // For every pair (T, VQ), where T is any type and VQ is either |
| 5645 | // volatile or empty, there exist candidate operator functions |
| 5646 | // of the form |
| 5647 | // |
| 5648 | // T*VQ& operator=(T*VQ&, T*); |
| 5649 | // |
| 5650 | // C++ [over.built]p21: |
| 5651 | // |
| 5652 | // For every pair (T, VQ), where T is a cv-qualified or |
| 5653 | // cv-unqualified object type and VQ is either volatile or |
| 5654 | // empty, there exist candidate operator functions of the form |
| 5655 | // |
| 5656 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 5657 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 5658 | void addAssignmentPointerOverloads(bool isEqualOp) { |
| 5659 | /// Set of (canonical) types that we've already handled. |
| 5660 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5661 | |
| 5662 | for (BuiltinCandidateTypeSet::iterator |
| 5663 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5664 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5665 | Ptr != PtrEnd; ++Ptr) { |
| 5666 | // If this is operator=, keep track of the builtin candidates we added. |
| 5667 | if (isEqualOp) |
| 5668 | AddedTypes.insert(S.Context.getCanonicalType(*Ptr)); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5669 | else if (!(*Ptr)->getPointeeType()->isObjectType()) |
| 5670 | continue; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5671 | |
| 5672 | // non-volatile version |
| 5673 | QualType ParamTypes[2] = { |
| 5674 | S.Context.getLValueReferenceType(*Ptr), |
| 5675 | isEqualOp ? *Ptr : S.Context.getPointerDiffType(), |
| 5676 | }; |
| 5677 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5678 | /*IsAssigmentOperator=*/ isEqualOp); |
| 5679 | |
| 5680 | if (!S.Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 5681 | VisibleTypeConversionsQuals.hasVolatile()) { |
| 5682 | // volatile version |
| 5683 | ParamTypes[0] = |
| 5684 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
| 5685 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5686 | /*IsAssigmentOperator=*/isEqualOp); |
| 5687 | } |
| 5688 | } |
| 5689 | |
| 5690 | if (isEqualOp) { |
| 5691 | for (BuiltinCandidateTypeSet::iterator |
| 5692 | Ptr = CandidateTypes[1].pointer_begin(), |
| 5693 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 5694 | Ptr != PtrEnd; ++Ptr) { |
| 5695 | // Make sure we don't add the same candidate twice. |
| 5696 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 5697 | continue; |
| 5698 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 5699 | QualType ParamTypes[2] = { |
| 5700 | S.Context.getLValueReferenceType(*Ptr), |
| 5701 | *Ptr, |
| 5702 | }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5703 | |
| 5704 | // non-volatile version |
| 5705 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5706 | /*IsAssigmentOperator=*/true); |
| 5707 | |
| 5708 | if (!S.Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 5709 | VisibleTypeConversionsQuals.hasVolatile()) { |
| 5710 | // volatile version |
| 5711 | ParamTypes[0] = |
| 5712 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 5713 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 5714 | CandidateSet, /*IsAssigmentOperator=*/true); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5715 | } |
| 5716 | } |
| 5717 | } |
| 5718 | } |
| 5719 | |
| 5720 | // C++ [over.built]p18: |
| 5721 | // |
| 5722 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 5723 | // VQ is either volatile or empty, and R is a promoted |
| 5724 | // arithmetic type, there exist candidate operator functions of |
| 5725 | // the form |
| 5726 | // |
| 5727 | // VQ L& operator=(VQ L&, R); |
| 5728 | // VQ L& operator*=(VQ L&, R); |
| 5729 | // VQ L& operator/=(VQ L&, R); |
| 5730 | // VQ L& operator+=(VQ L&, R); |
| 5731 | // VQ L& operator-=(VQ L&, R); |
| 5732 | void addAssignmentArithmeticOverloads(bool isEqualOp) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5733 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5734 | return; |
| 5735 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5736 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 5737 | for (unsigned Right = FirstPromotedArithmeticType; |
| 5738 | Right < LastPromotedArithmeticType; ++Right) { |
| 5739 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5740 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5741 | |
| 5742 | // Add this built-in operator as a candidate (VQ is empty). |
| 5743 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5744 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5745 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5746 | /*IsAssigmentOperator=*/isEqualOp); |
| 5747 | |
| 5748 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 5749 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 5750 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5751 | S.Context.getVolatileType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5752 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 5753 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 5754 | CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5755 | /*IsAssigmentOperator=*/isEqualOp); |
| 5756 | } |
| 5757 | } |
| 5758 | } |
| 5759 | |
| 5760 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 5761 | for (BuiltinCandidateTypeSet::iterator |
| 5762 | Vec1 = CandidateTypes[0].vector_begin(), |
| 5763 | Vec1End = CandidateTypes[0].vector_end(); |
| 5764 | Vec1 != Vec1End; ++Vec1) { |
| 5765 | for (BuiltinCandidateTypeSet::iterator |
| 5766 | Vec2 = CandidateTypes[1].vector_begin(), |
| 5767 | Vec2End = CandidateTypes[1].vector_end(); |
| 5768 | Vec2 != Vec2End; ++Vec2) { |
| 5769 | QualType ParamTypes[2]; |
| 5770 | ParamTypes[1] = *Vec2; |
| 5771 | // Add this built-in operator as a candidate (VQ is empty). |
| 5772 | ParamTypes[0] = S.Context.getLValueReferenceType(*Vec1); |
| 5773 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5774 | /*IsAssigmentOperator=*/isEqualOp); |
| 5775 | |
| 5776 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 5777 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 5778 | ParamTypes[0] = S.Context.getVolatileType(*Vec1); |
| 5779 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 5780 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 5781 | CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5782 | /*IsAssigmentOperator=*/isEqualOp); |
| 5783 | } |
| 5784 | } |
| 5785 | } |
| 5786 | } |
| 5787 | |
| 5788 | // C++ [over.built]p22: |
| 5789 | // |
| 5790 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 5791 | // is either volatile or empty, and R is a promoted integral |
| 5792 | // type, there exist candidate operator functions of the form |
| 5793 | // |
| 5794 | // VQ L& operator%=(VQ L&, R); |
| 5795 | // VQ L& operator<<=(VQ L&, R); |
| 5796 | // VQ L& operator>>=(VQ L&, R); |
| 5797 | // VQ L& operator&=(VQ L&, R); |
| 5798 | // VQ L& operator^=(VQ L&, R); |
| 5799 | // VQ L& operator|=(VQ L&, R); |
| 5800 | void addAssignmentIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5801 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5802 | return; |
| 5803 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5804 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 5805 | for (unsigned Right = FirstPromotedIntegralType; |
| 5806 | Right < LastPromotedIntegralType; ++Right) { |
| 5807 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5808 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5809 | |
| 5810 | // Add this built-in operator as a candidate (VQ is empty). |
| 5811 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5812 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5813 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 5814 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 5815 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5816 | ParamTypes[0] = getArithmeticType(Left); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5817 | ParamTypes[0] = S.Context.getVolatileType(ParamTypes[0]); |
| 5818 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
| 5819 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 5820 | CandidateSet); |
| 5821 | } |
| 5822 | } |
| 5823 | } |
| 5824 | } |
| 5825 | |
| 5826 | // C++ [over.operator]p23: |
| 5827 | // |
| 5828 | // There also exist candidate operator functions of the form |
| 5829 | // |
| 5830 | // bool operator!(bool); |
| 5831 | // bool operator&&(bool, bool); |
| 5832 | // bool operator||(bool, bool); |
| 5833 | void addExclaimOverload() { |
| 5834 | QualType ParamTy = S.Context.BoolTy; |
| 5835 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 5836 | /*IsAssignmentOperator=*/false, |
| 5837 | /*NumContextualBoolArguments=*/1); |
| 5838 | } |
| 5839 | void addAmpAmpOrPipePipeOverload() { |
| 5840 | QualType ParamTypes[2] = { S.Context.BoolTy, S.Context.BoolTy }; |
| 5841 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 5842 | /*IsAssignmentOperator=*/false, |
| 5843 | /*NumContextualBoolArguments=*/2); |
| 5844 | } |
| 5845 | |
| 5846 | // C++ [over.built]p13: |
| 5847 | // |
| 5848 | // For every cv-qualified or cv-unqualified object type T there |
| 5849 | // exist candidate operator functions of the form |
| 5850 | // |
| 5851 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 5852 | // T& operator[](T*, ptrdiff_t); |
| 5853 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 5854 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 5855 | // T& operator[](ptrdiff_t, T*); |
| 5856 | void addSubscriptOverloads() { |
| 5857 | for (BuiltinCandidateTypeSet::iterator |
| 5858 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5859 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5860 | Ptr != PtrEnd; ++Ptr) { |
| 5861 | QualType ParamTypes[2] = { *Ptr, S.Context.getPointerDiffType() }; |
| 5862 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5863 | if (!PointeeType->isObjectType()) |
| 5864 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5865 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5866 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 5867 | |
| 5868 | // T& operator[](T*, ptrdiff_t) |
| 5869 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 5870 | } |
| 5871 | |
| 5872 | for (BuiltinCandidateTypeSet::iterator |
| 5873 | Ptr = CandidateTypes[1].pointer_begin(), |
| 5874 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 5875 | Ptr != PtrEnd; ++Ptr) { |
| 5876 | QualType ParamTypes[2] = { S.Context.getPointerDiffType(), *Ptr }; |
| 5877 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5878 | if (!PointeeType->isObjectType()) |
| 5879 | continue; |
| 5880 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5881 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 5882 | |
| 5883 | // T& operator[](ptrdiff_t, T*) |
| 5884 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 5885 | } |
| 5886 | } |
| 5887 | |
| 5888 | // C++ [over.built]p11: |
| 5889 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 5890 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 5891 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 5892 | // there exist candidate operator functions of the form |
| 5893 | // |
| 5894 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 5895 | // |
| 5896 | // where CV12 is the union of CV1 and CV2. |
| 5897 | void addArrowStarOverloads() { |
| 5898 | for (BuiltinCandidateTypeSet::iterator |
| 5899 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5900 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5901 | Ptr != PtrEnd; ++Ptr) { |
| 5902 | QualType C1Ty = (*Ptr); |
| 5903 | QualType C1; |
| 5904 | QualifierCollector Q1; |
| 5905 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
| 5906 | if (!isa<RecordType>(C1)) |
| 5907 | continue; |
| 5908 | // heuristic to reduce number of builtin candidates in the set. |
| 5909 | // Add volatile/restrict version only if there are conversions to a |
| 5910 | // volatile/restrict type. |
| 5911 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 5912 | continue; |
| 5913 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 5914 | continue; |
| 5915 | for (BuiltinCandidateTypeSet::iterator |
| 5916 | MemPtr = CandidateTypes[1].member_pointer_begin(), |
| 5917 | MemPtrEnd = CandidateTypes[1].member_pointer_end(); |
| 5918 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 5919 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 5920 | QualType C2 = QualType(mptr->getClass(), 0); |
| 5921 | C2 = C2.getUnqualifiedType(); |
| 5922 | if (C1 != C2 && !S.IsDerivedFrom(C1, C2)) |
| 5923 | break; |
| 5924 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 5925 | // build CV12 T& |
| 5926 | QualType T = mptr->getPointeeType(); |
| 5927 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 5928 | T.isVolatileQualified()) |
| 5929 | continue; |
| 5930 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 5931 | T.isRestrictQualified()) |
| 5932 | continue; |
| 5933 | T = Q1.apply(S.Context, T); |
| 5934 | QualType ResultTy = S.Context.getLValueReferenceType(T); |
| 5935 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 5936 | } |
| 5937 | } |
| 5938 | } |
| 5939 | |
| 5940 | // Note that we don't consider the first argument, since it has been |
| 5941 | // contextually converted to bool long ago. The candidates below are |
| 5942 | // therefore added as binary. |
| 5943 | // |
| 5944 | // C++ [over.built]p25: |
| 5945 | // For every type T, where T is a pointer, pointer-to-member, or scoped |
| 5946 | // enumeration type, there exist candidate operator functions of the form |
| 5947 | // |
| 5948 | // T operator?(bool, T, T); |
| 5949 | // |
| 5950 | void addConditionalOperatorOverloads() { |
| 5951 | /// Set of (canonical) types that we've already handled. |
| 5952 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5953 | |
| 5954 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 5955 | for (BuiltinCandidateTypeSet::iterator |
| 5956 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 5957 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 5958 | Ptr != PtrEnd; ++Ptr) { |
| 5959 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 5960 | continue; |
| 5961 | |
| 5962 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5963 | S.AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 5964 | } |
| 5965 | |
| 5966 | for (BuiltinCandidateTypeSet::iterator |
| 5967 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 5968 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 5969 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 5970 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 5971 | continue; |
| 5972 | |
| 5973 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 5974 | S.AddBuiltinCandidate(*MemPtr, ParamTypes, Args, 2, CandidateSet); |
| 5975 | } |
| 5976 | |
| 5977 | if (S.getLangOptions().CPlusPlus0x) { |
| 5978 | for (BuiltinCandidateTypeSet::iterator |
| 5979 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 5980 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 5981 | Enum != EnumEnd; ++Enum) { |
| 5982 | if (!(*Enum)->getAs<EnumType>()->getDecl()->isScoped()) |
| 5983 | continue; |
| 5984 | |
| 5985 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 5986 | continue; |
| 5987 | |
| 5988 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 5989 | S.AddBuiltinCandidate(*Enum, ParamTypes, Args, 2, CandidateSet); |
| 5990 | } |
| 5991 | } |
| 5992 | } |
| 5993 | } |
| 5994 | }; |
| 5995 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5996 | } // end anonymous namespace |
| 5997 | |
| 5998 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 5999 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 6000 | /// on the operator @p Op and the arguments given. For example, if the |
| 6001 | /// operator is a binary '+', this routine might add "int |
| 6002 | /// operator+(int, int)" to cover integer addition. |
| 6003 | void |
| 6004 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 6005 | SourceLocation OpLoc, |
| 6006 | Expr **Args, unsigned NumArgs, |
| 6007 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6008 | // Find all of the types that the arguments can convert to, but only |
| 6009 | // if the operator we're looking at has built-in operator candidates |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6010 | // that make use of these types. Also record whether we encounter non-record |
| 6011 | // candidate types or either arithmetic or enumeral candidate types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6012 | Qualifiers VisibleTypeConversionsQuals; |
| 6013 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 6014 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 6015 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6016 | |
| 6017 | bool HasNonRecordCandidateType = false; |
| 6018 | bool HasArithmeticOrEnumeralCandidateType = false; |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 6019 | llvm::SmallVector<BuiltinCandidateTypeSet, 2> CandidateTypes; |
| 6020 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 6021 | CandidateTypes.push_back(BuiltinCandidateTypeSet(*this)); |
| 6022 | CandidateTypes[ArgIdx].AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 6023 | OpLoc, |
| 6024 | true, |
| 6025 | (Op == OO_Exclaim || |
| 6026 | Op == OO_AmpAmp || |
| 6027 | Op == OO_PipePipe), |
| 6028 | VisibleTypeConversionsQuals); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6029 | HasNonRecordCandidateType = HasNonRecordCandidateType || |
| 6030 | CandidateTypes[ArgIdx].hasNonRecordTypes(); |
| 6031 | HasArithmeticOrEnumeralCandidateType = |
| 6032 | HasArithmeticOrEnumeralCandidateType || |
| 6033 | CandidateTypes[ArgIdx].hasArithmeticOrEnumeralTypes(); |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 6034 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6035 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6036 | // Exit early when no non-record types have been added to the candidate set |
| 6037 | // for any of the arguments to the operator. |
| 6038 | if (!HasNonRecordCandidateType) |
| 6039 | return; |
| 6040 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6041 | // Setup an object to manage the common state for building overloads. |
| 6042 | BuiltinOperatorOverloadBuilder OpBuilder(*this, Args, NumArgs, |
| 6043 | VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6044 | HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6045 | CandidateTypes, CandidateSet); |
| 6046 | |
| 6047 | // Dispatch over the operation to add in only those overloads which apply. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6048 | switch (Op) { |
| 6049 | case OO_None: |
| 6050 | case NUM_OVERLOADED_OPERATORS: |
| 6051 | assert(false && "Expected an overloaded operator"); |
| 6052 | break; |
| 6053 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6054 | case OO_New: |
| 6055 | case OO_Delete: |
| 6056 | case OO_Array_New: |
| 6057 | case OO_Array_Delete: |
| 6058 | case OO_Call: |
| 6059 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
| 6060 | break; |
| 6061 | |
| 6062 | case OO_Comma: |
| 6063 | case OO_Arrow: |
| 6064 | // C++ [over.match.oper]p3: |
| 6065 | // -- For the operator ',', the unary operator '&', or the |
| 6066 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6067 | break; |
| 6068 | |
| 6069 | case OO_Plus: // '+' is either unary or binary |
Chandler Carruth | 32fe0d0 | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 6070 | if (NumArgs == 1) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6071 | OpBuilder.addUnaryPlusPointerOverloads(); |
Chandler Carruth | 32fe0d0 | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 6072 | // Fall through. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6073 | |
| 6074 | case OO_Minus: // '-' is either unary or binary |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 6075 | if (NumArgs == 1) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6076 | OpBuilder.addUnaryPlusOrMinusArithmeticOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 6077 | } else { |
| 6078 | OpBuilder.addBinaryPlusOrMinusPointerOverloads(Op); |
| 6079 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 6080 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6081 | break; |
| 6082 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6083 | case OO_Star: // '*' is either unary or binary |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6084 | if (NumArgs == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6085 | OpBuilder.addUnaryStarPointerOverloads(); |
| 6086 | else |
| 6087 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 6088 | break; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6089 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6090 | case OO_Slash: |
| 6091 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 6092 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6093 | |
| 6094 | case OO_PlusPlus: |
| 6095 | case OO_MinusMinus: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6096 | OpBuilder.addPlusPlusMinusMinusArithmeticOverloads(Op); |
| 6097 | OpBuilder.addPlusPlusMinusMinusPointerOverloads(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6098 | break; |
| 6099 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6100 | case OO_EqualEqual: |
| 6101 | case OO_ExclaimEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6102 | OpBuilder.addEqualEqualOrNotEqualMemberPointerOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 6103 | // Fall through. |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 6104 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6105 | case OO_Less: |
| 6106 | case OO_Greater: |
| 6107 | case OO_LessEqual: |
| 6108 | case OO_GreaterEqual: |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6109 | OpBuilder.addRelationalPointerOrEnumeralOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 6110 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/true); |
| 6111 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6112 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6113 | case OO_Percent: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6114 | case OO_Caret: |
| 6115 | case OO_Pipe: |
| 6116 | case OO_LessLess: |
| 6117 | case OO_GreaterGreater: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6118 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6119 | break; |
| 6120 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6121 | case OO_Amp: // '&' is either unary or binary |
| 6122 | if (NumArgs == 1) |
| 6123 | // C++ [over.match.oper]p3: |
| 6124 | // -- For the operator ',', the unary operator '&', or the |
| 6125 | // operator '->', the built-in candidates set is empty. |
| 6126 | break; |
| 6127 | |
| 6128 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
| 6129 | break; |
| 6130 | |
| 6131 | case OO_Tilde: |
| 6132 | OpBuilder.addUnaryTildePromotedIntegralOverloads(); |
| 6133 | break; |
| 6134 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6135 | case OO_Equal: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6136 | OpBuilder.addAssignmentMemberPointerOrEnumeralOverloads(); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6137 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6138 | |
| 6139 | case OO_PlusEqual: |
| 6140 | case OO_MinusEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6141 | OpBuilder.addAssignmentPointerOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6142 | // Fall through. |
| 6143 | |
| 6144 | case OO_StarEqual: |
| 6145 | case OO_SlashEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6146 | OpBuilder.addAssignmentArithmeticOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6147 | break; |
| 6148 | |
| 6149 | case OO_PercentEqual: |
| 6150 | case OO_LessLessEqual: |
| 6151 | case OO_GreaterGreaterEqual: |
| 6152 | case OO_AmpEqual: |
| 6153 | case OO_CaretEqual: |
| 6154 | case OO_PipeEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6155 | OpBuilder.addAssignmentIntegralOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6156 | break; |
| 6157 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6158 | case OO_Exclaim: |
| 6159 | OpBuilder.addExclaimOverload(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6160 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6161 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6162 | case OO_AmpAmp: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6163 | case OO_PipePipe: |
| 6164 | OpBuilder.addAmpAmpOrPipePipeOverload(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6165 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6166 | |
| 6167 | case OO_Subscript: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6168 | OpBuilder.addSubscriptOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6169 | break; |
| 6170 | |
| 6171 | case OO_ArrowStar: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6172 | OpBuilder.addArrowStarOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6173 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 6174 | |
| 6175 | case OO_Conditional: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6176 | OpBuilder.addConditionalOperatorOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 6177 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 6178 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6179 | } |
| 6180 | } |
| 6181 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6182 | /// \brief Add function candidates found via argument-dependent lookup |
| 6183 | /// to the set of overloading candidates. |
| 6184 | /// |
| 6185 | /// This routine performs argument-dependent name lookup based on the |
| 6186 | /// given function name (which may also be an operator name) and adds |
| 6187 | /// all of the overload candidates found by ADL to the overload |
| 6188 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6189 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6190 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6191 | bool Operator, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6192 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 6193 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6194 | OverloadCandidateSet& CandidateSet, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 6195 | bool PartialOverloading, |
| 6196 | bool StdNamespaceIsAssociated) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6197 | ADLResult Fns; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6198 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 6199 | // FIXME: This approach for uniquing ADL results (and removing |
| 6200 | // redundant candidates from the set) relies on pointer-equality, |
| 6201 | // which means we need to key off the canonical decl. However, |
| 6202 | // always going back to the canonical decl might not get us the |
| 6203 | // right set of default arguments. What default arguments are |
| 6204 | // we supposed to consider on ADL candidates, anyway? |
| 6205 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6206 | // FIXME: Pass in the explicit template arguments? |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 6207 | ArgumentDependentLookup(Name, Operator, Args, NumArgs, Fns, |
| 6208 | StdNamespaceIsAssociated); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6209 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6210 | // Erase all of the candidates we already knew about. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6211 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 6212 | CandEnd = CandidateSet.end(); |
| 6213 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6214 | if (Cand->Function) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6215 | Fns.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6216 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6217 | Fns.erase(FunTmpl); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6218 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6219 | |
| 6220 | // For each of the ADL candidates we found, add it to the overload |
| 6221 | // set. |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6222 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6223 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6224 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6225 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6226 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6227 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6228 | AddOverloadCandidate(FD, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 6229 | false, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6230 | } else |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6231 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6232 | FoundDecl, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 6233 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6234 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6235 | } |
| 6236 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6237 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 6238 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6239 | bool |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6240 | isBetterOverloadCandidate(Sema &S, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 6241 | const OverloadCandidate &Cand1, |
| 6242 | const OverloadCandidate &Cand2, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6243 | SourceLocation Loc, |
| 6244 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6245 | // Define viable functions to be better candidates than non-viable |
| 6246 | // functions. |
| 6247 | if (!Cand2.Viable) |
| 6248 | return Cand1.Viable; |
| 6249 | else if (!Cand1.Viable) |
| 6250 | return false; |
| 6251 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6252 | // C++ [over.match.best]p1: |
| 6253 | // |
| 6254 | // -- if F is a static member function, ICS1(F) is defined such |
| 6255 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 6256 | // any function G, and, symmetrically, ICS1(G) is neither |
| 6257 | // better nor worse than ICS1(F). |
| 6258 | unsigned StartArg = 0; |
| 6259 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 6260 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6261 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6262 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6263 | // A viable function F1 is defined to be a better function than another |
| 6264 | // viable function F2 if for all arguments i, ICSi(F1) is not a worse |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6265 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6266 | unsigned NumArgs = Cand1.Conversions.size(); |
| 6267 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 6268 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6269 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6270 | switch (CompareImplicitConversionSequences(S, |
| 6271 | Cand1.Conversions[ArgIdx], |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6272 | Cand2.Conversions[ArgIdx])) { |
| 6273 | case ImplicitConversionSequence::Better: |
| 6274 | // Cand1 has a better conversion sequence. |
| 6275 | HasBetterConversion = true; |
| 6276 | break; |
| 6277 | |
| 6278 | case ImplicitConversionSequence::Worse: |
| 6279 | // Cand1 can't be better than Cand2. |
| 6280 | return false; |
| 6281 | |
| 6282 | case ImplicitConversionSequence::Indistinguishable: |
| 6283 | // Do nothing. |
| 6284 | break; |
| 6285 | } |
| 6286 | } |
| 6287 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6288 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6289 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6290 | if (HasBetterConversion) |
| 6291 | return true; |
| 6292 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6293 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6294 | // specialization, or, if not that, |
Douglas Gregor | ccd4713 | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 6295 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6296 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 6297 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6298 | |
| 6299 | // -- F1 and F2 are function template specializations, and the function |
| 6300 | // template for F1 is more specialized than the template for F2 |
| 6301 | // according to the partial ordering rules described in 14.5.5.2, or, |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6302 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 6303 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6304 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6305 | if (FunctionTemplateDecl *BetterTemplate |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6306 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 6307 | Cand2.Function->getPrimaryTemplate(), |
| 6308 | Loc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6309 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 6310 | : TPOC_Call, |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6311 | Cand1.ExplicitCallArguments)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6312 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6313 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6314 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6315 | // -- the context is an initialization by user-defined conversion |
| 6316 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 6317 | // from the return type of F1 to the destination type (i.e., |
| 6318 | // the type of the entity being initialized) is a better |
| 6319 | // conversion sequence than the standard conversion sequence |
| 6320 | // from the return type of F2 to the destination type. |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6321 | if (UserDefinedConversion && Cand1.Function && Cand2.Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6322 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6323 | isa<CXXConversionDecl>(Cand2.Function)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6324 | switch (CompareStandardConversionSequences(S, |
| 6325 | Cand1.FinalConversion, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6326 | Cand2.FinalConversion)) { |
| 6327 | case ImplicitConversionSequence::Better: |
| 6328 | // Cand1 has a better conversion sequence. |
| 6329 | return true; |
| 6330 | |
| 6331 | case ImplicitConversionSequence::Worse: |
| 6332 | // Cand1 can't be better than Cand2. |
| 6333 | return false; |
| 6334 | |
| 6335 | case ImplicitConversionSequence::Indistinguishable: |
| 6336 | // Do nothing |
| 6337 | break; |
| 6338 | } |
| 6339 | } |
| 6340 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6341 | return false; |
| 6342 | } |
| 6343 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6344 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6345 | /// within an overload candidate set. |
| 6346 | /// |
| 6347 | /// \param CandidateSet the set of candidate functions. |
| 6348 | /// |
| 6349 | /// \param Loc the location of the function name (or operator symbol) for |
| 6350 | /// which overload resolution occurs. |
| 6351 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6352 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6353 | /// function, Best points to the candidate function found. |
| 6354 | /// |
| 6355 | /// \returns The result of overload resolution. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6356 | OverloadingResult |
| 6357 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 6358 | iterator &Best, |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 6359 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6360 | // Find the best viable function. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6361 | Best = end(); |
| 6362 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
| 6363 | if (Cand->Viable) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6364 | if (Best == end() || isBetterOverloadCandidate(S, *Cand, *Best, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6365 | UserDefinedConversion)) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6366 | Best = Cand; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6367 | } |
| 6368 | |
| 6369 | // If we didn't find any viable functions, abort. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6370 | if (Best == end()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6371 | return OR_No_Viable_Function; |
| 6372 | |
| 6373 | // Make sure that this function is better than every other viable |
| 6374 | // function. If not, we have an ambiguity. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6375 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6376 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6377 | Cand != Best && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6378 | !isBetterOverloadCandidate(S, *Best, *Cand, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6379 | UserDefinedConversion)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6380 | Best = end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6381 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6382 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6383 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6384 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6385 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6386 | if (Best->Function && |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 6387 | (Best->Function->isDeleted() || Best->Function->isUnavailable())) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6388 | return OR_Deleted; |
| 6389 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6390 | return OR_Success; |
| 6391 | } |
| 6392 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6393 | namespace { |
| 6394 | |
| 6395 | enum OverloadCandidateKind { |
| 6396 | oc_function, |
| 6397 | oc_method, |
| 6398 | oc_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6399 | oc_function_template, |
| 6400 | oc_method_template, |
| 6401 | oc_constructor_template, |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6402 | oc_implicit_default_constructor, |
| 6403 | oc_implicit_copy_constructor, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6404 | oc_implicit_copy_assignment, |
| 6405 | oc_implicit_inherited_constructor |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6406 | }; |
| 6407 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6408 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 6409 | FunctionDecl *Fn, |
| 6410 | std::string &Description) { |
| 6411 | bool isTemplate = false; |
| 6412 | |
| 6413 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 6414 | isTemplate = true; |
| 6415 | Description = S.getTemplateArgumentBindingsText( |
| 6416 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 6417 | } |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6418 | |
| 6419 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6420 | if (!Ctor->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6421 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6422 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6423 | if (Ctor->getInheritedConstructor()) |
| 6424 | return oc_implicit_inherited_constructor; |
| 6425 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6426 | return Ctor->isCopyConstructor() ? oc_implicit_copy_constructor |
| 6427 | : oc_implicit_default_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6428 | } |
| 6429 | |
| 6430 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 6431 | // This actually gets spelled 'candidate function' for now, but |
| 6432 | // it doesn't hurt to split it out. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6433 | if (!Meth->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6434 | return isTemplate ? oc_method_template : oc_method; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6435 | |
Douglas Gregor | 3e9438b | 2010-09-27 22:37:28 +0000 | [diff] [blame] | 6436 | assert(Meth->isCopyAssignmentOperator() |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6437 | && "implicit method is not copy assignment operator?"); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6438 | return oc_implicit_copy_assignment; |
| 6439 | } |
| 6440 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6441 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6442 | } |
| 6443 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6444 | void MaybeEmitInheritedConstructorNote(Sema &S, FunctionDecl *Fn) { |
| 6445 | const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn); |
| 6446 | if (!Ctor) return; |
| 6447 | |
| 6448 | Ctor = Ctor->getInheritedConstructor(); |
| 6449 | if (!Ctor) return; |
| 6450 | |
| 6451 | S.Diag(Ctor->getLocation(), diag::note_ovl_candidate_inherited_constructor); |
| 6452 | } |
| 6453 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6454 | } // end anonymous namespace |
| 6455 | |
| 6456 | // Notes the location of an overload candidate. |
| 6457 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6458 | std::string FnDesc; |
| 6459 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
| 6460 | Diag(Fn->getLocation(), diag::note_ovl_candidate) |
| 6461 | << (unsigned) K << FnDesc; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6462 | MaybeEmitInheritedConstructorNote(*this, Fn); |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6463 | } |
| 6464 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6465 | //Notes the location of all overload candidates designated through |
| 6466 | // OverloadedExpr |
| 6467 | void Sema::NoteAllOverloadCandidates(Expr* OverloadedExpr) { |
| 6468 | assert(OverloadedExpr->getType() == Context.OverloadTy); |
| 6469 | |
| 6470 | OverloadExpr::FindResult Ovl = OverloadExpr::find(OverloadedExpr); |
| 6471 | OverloadExpr *OvlExpr = Ovl.Expression; |
| 6472 | |
| 6473 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 6474 | IEnd = OvlExpr->decls_end(); |
| 6475 | I != IEnd; ++I) { |
| 6476 | if (FunctionTemplateDecl *FunTmpl = |
| 6477 | dyn_cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()) ) { |
| 6478 | NoteOverloadCandidate(FunTmpl->getTemplatedDecl()); |
| 6479 | } else if (FunctionDecl *Fun |
| 6480 | = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl()) ) { |
| 6481 | NoteOverloadCandidate(Fun); |
| 6482 | } |
| 6483 | } |
| 6484 | } |
| 6485 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6486 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 6487 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 6488 | /// target types of the conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6489 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
| 6490 | Sema &S, |
| 6491 | SourceLocation CaretLoc, |
| 6492 | const PartialDiagnostic &PDiag) const { |
| 6493 | S.Diag(CaretLoc, PDiag) |
| 6494 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6495 | for (AmbiguousConversionSequence::const_iterator |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6496 | I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
| 6497 | S.NoteOverloadCandidate(*I); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6498 | } |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6499 | } |
| 6500 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6501 | namespace { |
| 6502 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6503 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 6504 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 6505 | assert(Conv.isBad()); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6506 | assert(Cand->Function && "for now, candidate must be a function"); |
| 6507 | FunctionDecl *Fn = Cand->Function; |
| 6508 | |
| 6509 | // There's a conversion slot for the object argument if this is a |
| 6510 | // non-constructor method. Note that 'I' corresponds the |
| 6511 | // conversion-slot index. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6512 | bool isObjectArgument = false; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6513 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6514 | if (I == 0) |
| 6515 | isObjectArgument = true; |
| 6516 | else |
| 6517 | I--; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6518 | } |
| 6519 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6520 | std::string FnDesc; |
| 6521 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 6522 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6523 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 6524 | QualType FromTy = Conv.Bad.getFromType(); |
| 6525 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6526 | |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 6527 | if (FromTy == S.Context.OverloadTy) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 6528 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 6529 | Expr *E = FromExpr->IgnoreParens(); |
| 6530 | if (isa<UnaryOperator>(E)) |
| 6531 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6532 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 6533 | |
| 6534 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 6535 | << (unsigned) FnKind << FnDesc |
| 6536 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6537 | << ToTy << Name << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6538 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 6539 | return; |
| 6540 | } |
| 6541 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 6542 | // Do some hand-waving analysis to see if the non-viability is due |
| 6543 | // to a qualifier mismatch. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6544 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 6545 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 6546 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 6547 | CToTy = RT->getPointeeType(); |
| 6548 | else { |
| 6549 | // TODO: detect and diagnose the full richness of const mismatches. |
| 6550 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 6551 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 6552 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 6553 | } |
| 6554 | |
| 6555 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 6556 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
| 6557 | // It is dumb that we have to do this here. |
| 6558 | while (isa<ArrayType>(CFromTy)) |
| 6559 | CFromTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 6560 | while (isa<ArrayType>(CToTy)) |
| 6561 | CToTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 6562 | |
| 6563 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 6564 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 6565 | |
| 6566 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 6567 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 6568 | << (unsigned) FnKind << FnDesc |
| 6569 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6570 | << FromTy |
| 6571 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 6572 | << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6573 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6574 | return; |
| 6575 | } |
| 6576 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 6577 | if (FromQs.getObjCGCAttr() != ToQs.getObjCGCAttr()) { |
| 6578 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_gc) |
| 6579 | << (unsigned) FnKind << FnDesc |
| 6580 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6581 | << FromTy |
| 6582 | << FromQs.getObjCGCAttr() << ToQs.getObjCGCAttr() |
| 6583 | << (unsigned) isObjectArgument << I+1; |
| 6584 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 6585 | return; |
| 6586 | } |
| 6587 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6588 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 6589 | assert(CVR && "unexpected qualifiers mismatch"); |
| 6590 | |
| 6591 | if (isObjectArgument) { |
| 6592 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 6593 | << (unsigned) FnKind << FnDesc |
| 6594 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6595 | << FromTy << (CVR - 1); |
| 6596 | } else { |
| 6597 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 6598 | << (unsigned) FnKind << FnDesc |
| 6599 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6600 | << FromTy << (CVR - 1) << I+1; |
| 6601 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6602 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6603 | return; |
| 6604 | } |
| 6605 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 6606 | // Diagnose references or pointers to incomplete types differently, |
| 6607 | // since it's far from impossible that the incompleteness triggered |
| 6608 | // the failure. |
| 6609 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 6610 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 6611 | TempFromTy = PTy->getPointeeType(); |
| 6612 | if (TempFromTy->isIncompleteType()) { |
| 6613 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 6614 | << (unsigned) FnKind << FnDesc |
| 6615 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6616 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6617 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 6618 | return; |
| 6619 | } |
| 6620 | |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6621 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6622 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6623 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 6624 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 6625 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 6626 | FromPtrTy->getPointeeType()) && |
| 6627 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 6628 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6629 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6630 | FromPtrTy->getPointeeType())) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6631 | BaseToDerivedConversion = 1; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6632 | } |
| 6633 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 6634 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 6635 | if (const ObjCObjectPointerType *ToPtrTy |
| 6636 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 6637 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 6638 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 6639 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 6640 | FromPtrTy->getPointeeType()) && |
| 6641 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6642 | BaseToDerivedConversion = 2; |
| 6643 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
| 6644 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 6645 | !FromTy->isIncompleteType() && |
| 6646 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 6647 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) |
| 6648 | BaseToDerivedConversion = 3; |
| 6649 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6650 | |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6651 | if (BaseToDerivedConversion) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6652 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6653 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6654 | << (unsigned) FnKind << FnDesc |
| 6655 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6656 | << (BaseToDerivedConversion - 1) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6657 | << FromTy << ToTy << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6658 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6659 | return; |
| 6660 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6661 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6662 | // TODO: specialize more based on the kind of mismatch |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6663 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv) |
| 6664 | << (unsigned) FnKind << FnDesc |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6665 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
John McCall | e81e15e | 2010-01-14 00:56:20 +0000 | [diff] [blame] | 6666 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6667 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6668 | } |
| 6669 | |
| 6670 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 6671 | unsigned NumFormalArgs) { |
| 6672 | // TODO: treat calls to a missing default constructor as a special case |
| 6673 | |
| 6674 | FunctionDecl *Fn = Cand->Function; |
| 6675 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 6676 | |
| 6677 | unsigned MinParams = Fn->getMinRequiredArguments(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6678 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6679 | // at least / at most / exactly |
| 6680 | unsigned mode, modeCount; |
| 6681 | if (NumFormalArgs < MinParams) { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 6682 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 6683 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 6684 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6685 | if (MinParams != FnTy->getNumArgs() || |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 6686 | FnTy->isVariadic() || FnTy->isTemplateVariadic()) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6687 | mode = 0; // "at least" |
| 6688 | else |
| 6689 | mode = 2; // "exactly" |
| 6690 | modeCount = MinParams; |
| 6691 | } else { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 6692 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 6693 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 6694 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6695 | if (MinParams != FnTy->getNumArgs()) |
| 6696 | mode = 1; // "at most" |
| 6697 | else |
| 6698 | mode = 2; // "exactly" |
| 6699 | modeCount = FnTy->getNumArgs(); |
| 6700 | } |
| 6701 | |
| 6702 | std::string Description; |
| 6703 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 6704 | |
| 6705 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6706 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 6707 | << modeCount << NumFormalArgs; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6708 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6709 | } |
| 6710 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6711 | /// Diagnose a failed template-argument deduction. |
| 6712 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
| 6713 | Expr **Args, unsigned NumArgs) { |
| 6714 | FunctionDecl *Fn = Cand->Function; // pattern |
| 6715 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6716 | TemplateParameter Param = Cand->DeductionFailure.getTemplateParameter(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6717 | NamedDecl *ParamD; |
| 6718 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 6719 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 6720 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6721 | switch (Cand->DeductionFailure.Result) { |
| 6722 | case Sema::TDK_Success: |
| 6723 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 6724 | |
| 6725 | case Sema::TDK_Incomplete: { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6726 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| 6727 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_incomplete_deduction) |
| 6728 | << ParamD->getDeclName(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6729 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6730 | return; |
| 6731 | } |
| 6732 | |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 6733 | case Sema::TDK_Underqualified: { |
| 6734 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 6735 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 6736 | |
| 6737 | QualType Param = Cand->DeductionFailure.getFirstArg()->getAsType(); |
| 6738 | |
| 6739 | // Param will have been canonicalized, but it should just be a |
| 6740 | // qualified version of ParamD, so move the qualifiers to that. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6741 | QualifierCollector Qs; |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 6742 | Qs.strip(Param); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6743 | QualType NonCanonParam = Qs.apply(S.Context, TParam->getTypeForDecl()); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 6744 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 6745 | |
| 6746 | // Arg has also been canonicalized, but there's nothing we can do |
| 6747 | // about that. It also doesn't matter as much, because it won't |
| 6748 | // have any template parameters in it (because deduction isn't |
| 6749 | // done on dependent types). |
| 6750 | QualType Arg = Cand->DeductionFailure.getSecondArg()->getAsType(); |
| 6751 | |
| 6752 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_underqualified) |
| 6753 | << ParamD->getDeclName() << Arg << NonCanonParam; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6754 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 6755 | return; |
| 6756 | } |
| 6757 | |
| 6758 | case Sema::TDK_Inconsistent: { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6759 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6760 | int which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6761 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6762 | which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6763 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6764 | which = 1; |
| 6765 | else { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6766 | which = 2; |
| 6767 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6768 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6769 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_inconsistent_deduction) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6770 | << which << ParamD->getDeclName() |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6771 | << *Cand->DeductionFailure.getFirstArg() |
| 6772 | << *Cand->DeductionFailure.getSecondArg(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6773 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6774 | return; |
| 6775 | } |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 6776 | |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6777 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6778 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6779 | if (ParamD->getDeclName()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6780 | S.Diag(Fn->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6781 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
| 6782 | << ParamD->getDeclName(); |
| 6783 | else { |
| 6784 | int index = 0; |
| 6785 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 6786 | index = TTP->getIndex(); |
| 6787 | else if (NonTypeTemplateParmDecl *NTTP |
| 6788 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 6789 | index = NTTP->getIndex(); |
| 6790 | else |
| 6791 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6792 | S.Diag(Fn->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6793 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
| 6794 | << (index + 1); |
| 6795 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6796 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6797 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6798 | |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 6799 | case Sema::TDK_TooManyArguments: |
| 6800 | case Sema::TDK_TooFewArguments: |
| 6801 | DiagnoseArityMismatch(S, Cand, NumArgs); |
| 6802 | return; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 6803 | |
| 6804 | case Sema::TDK_InstantiationDepth: |
| 6805 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_instantiation_depth); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6806 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 6807 | return; |
| 6808 | |
| 6809 | case Sema::TDK_SubstitutionFailure: { |
| 6810 | std::string ArgString; |
| 6811 | if (TemplateArgumentList *Args |
| 6812 | = Cand->DeductionFailure.getTemplateArgumentList()) |
| 6813 | ArgString = S.getTemplateArgumentBindingsText( |
| 6814 | Fn->getDescribedFunctionTemplate()->getTemplateParameters(), |
| 6815 | *Args); |
| 6816 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_substitution_failure) |
| 6817 | << ArgString; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6818 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 6819 | return; |
| 6820 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6821 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6822 | // TODO: diagnose these individually, then kill off |
| 6823 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6824 | case Sema::TDK_NonDeducedMismatch: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6825 | case Sema::TDK_FailedOverloadResolution: |
| 6826 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6827 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6828 | return; |
| 6829 | } |
| 6830 | } |
| 6831 | |
| 6832 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 6833 | /// already generated a primary error at the call site. |
| 6834 | /// |
| 6835 | /// It really does need to be a single diagnostic with its caret |
| 6836 | /// pointed at the candidate declaration. Yes, this creates some |
| 6837 | /// major challenges of technical writing. Yes, this makes pointing |
| 6838 | /// out problems with specific arguments quite awkward. It's still |
| 6839 | /// better than generating twenty screens of text for every failed |
| 6840 | /// overload. |
| 6841 | /// |
| 6842 | /// It would be great to be able to express per-candidate problems |
| 6843 | /// more richly for those diagnostic clients that cared, but we'd |
| 6844 | /// still have to be just as careful with the default diagnostics. |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6845 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| 6846 | Expr **Args, unsigned NumArgs) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6847 | FunctionDecl *Fn = Cand->Function; |
| 6848 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6849 | // Note deleted candidates, but only if they're viable. |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 6850 | if (Cand->Viable && (Fn->isDeleted() || Fn->isUnavailable())) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6851 | std::string FnDesc; |
| 6852 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6853 | |
| 6854 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6855 | << FnKind << FnDesc << Fn->isDeleted(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6856 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6857 | return; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6858 | } |
| 6859 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6860 | // We don't really have anything else to say about viable candidates. |
| 6861 | if (Cand->Viable) { |
| 6862 | S.NoteOverloadCandidate(Fn); |
| 6863 | return; |
| 6864 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6865 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6866 | switch (Cand->FailureKind) { |
| 6867 | case ovl_fail_too_many_arguments: |
| 6868 | case ovl_fail_too_few_arguments: |
| 6869 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6870 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6871 | case ovl_fail_bad_deduction: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6872 | return DiagnoseBadDeduction(S, Cand, Args, NumArgs); |
| 6873 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6874 | case ovl_fail_trivial_conversion: |
| 6875 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 6876 | case ovl_fail_final_conversion_not_exact: |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6877 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6878 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 6879 | case ovl_fail_bad_conversion: { |
| 6880 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
| 6881 | for (unsigned N = Cand->Conversions.size(); I != N; ++I) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6882 | if (Cand->Conversions[I].isBad()) |
| 6883 | return DiagnoseBadConversion(S, Cand, I); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6884 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6885 | // FIXME: this currently happens when we're called from SemaInit |
| 6886 | // when user-conversion overload fails. Figure out how to handle |
| 6887 | // those conditions and diagnose them well. |
| 6888 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6889 | } |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 6890 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6891 | } |
| 6892 | |
| 6893 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 6894 | // Desugar the type of the surrogate down to a function type, |
| 6895 | // retaining as many typedefs as possible while still showing |
| 6896 | // the function type (and, therefore, its parameter types). |
| 6897 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 6898 | bool isLValueReference = false; |
| 6899 | bool isRValueReference = false; |
| 6900 | bool isPointer = false; |
| 6901 | if (const LValueReferenceType *FnTypeRef = |
| 6902 | FnType->getAs<LValueReferenceType>()) { |
| 6903 | FnType = FnTypeRef->getPointeeType(); |
| 6904 | isLValueReference = true; |
| 6905 | } else if (const RValueReferenceType *FnTypeRef = |
| 6906 | FnType->getAs<RValueReferenceType>()) { |
| 6907 | FnType = FnTypeRef->getPointeeType(); |
| 6908 | isRValueReference = true; |
| 6909 | } |
| 6910 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 6911 | FnType = FnTypePtr->getPointeeType(); |
| 6912 | isPointer = true; |
| 6913 | } |
| 6914 | // Desugar down to a function type. |
| 6915 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 6916 | // Reconstruct the pointer/reference as appropriate. |
| 6917 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 6918 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 6919 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 6920 | |
| 6921 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 6922 | << FnType; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6923 | MaybeEmitInheritedConstructorNote(S, Cand->Surrogate); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6924 | } |
| 6925 | |
| 6926 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 6927 | const char *Opc, |
| 6928 | SourceLocation OpLoc, |
| 6929 | OverloadCandidate *Cand) { |
| 6930 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
| 6931 | std::string TypeStr("operator"); |
| 6932 | TypeStr += Opc; |
| 6933 | TypeStr += "("; |
| 6934 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 6935 | if (Cand->Conversions.size() == 1) { |
| 6936 | TypeStr += ")"; |
| 6937 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 6938 | } else { |
| 6939 | TypeStr += ", "; |
| 6940 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 6941 | TypeStr += ")"; |
| 6942 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 6943 | } |
| 6944 | } |
| 6945 | |
| 6946 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 6947 | OverloadCandidate *Cand) { |
| 6948 | unsigned NoOperands = Cand->Conversions.size(); |
| 6949 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 6950 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6951 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 6952 | if (!ICS.isAmbiguous()) continue; |
| 6953 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6954 | ICS.DiagnoseAmbiguousConversion(S, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6955 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6956 | } |
| 6957 | } |
| 6958 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 6959 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 6960 | if (Cand->Function) |
| 6961 | return Cand->Function->getLocation(); |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 6962 | if (Cand->IsSurrogate) |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 6963 | return Cand->Surrogate->getLocation(); |
| 6964 | return SourceLocation(); |
| 6965 | } |
| 6966 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 6967 | struct CompareOverloadCandidatesForDisplay { |
| 6968 | Sema &S; |
| 6969 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6970 | |
| 6971 | bool operator()(const OverloadCandidate *L, |
| 6972 | const OverloadCandidate *R) { |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 6973 | // Fast-path this check. |
| 6974 | if (L == R) return false; |
| 6975 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6976 | // Order first by viability. |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 6977 | if (L->Viable) { |
| 6978 | if (!R->Viable) return true; |
| 6979 | |
| 6980 | // TODO: introduce a tri-valued comparison for overload |
| 6981 | // candidates. Would be more worthwhile if we had a sort |
| 6982 | // that could exploit it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6983 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation())) return true; |
| 6984 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation())) return false; |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 6985 | } else if (R->Viable) |
| 6986 | return false; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6987 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 6988 | assert(L->Viable == R->Viable); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6989 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 6990 | // Criteria by which we can sort non-viable candidates: |
| 6991 | if (!L->Viable) { |
| 6992 | // 1. Arity mismatches come after other candidates. |
| 6993 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 6994 | L->FailureKind == ovl_fail_too_few_arguments) |
| 6995 | return false; |
| 6996 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 6997 | R->FailureKind == ovl_fail_too_few_arguments) |
| 6998 | return true; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6999 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7000 | // 2. Bad conversions come first and are ordered by the number |
| 7001 | // of bad conversions and quality of good conversions. |
| 7002 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 7003 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 7004 | return true; |
| 7005 | |
| 7006 | // If there's any ordering between the defined conversions... |
| 7007 | // FIXME: this might not be transitive. |
| 7008 | assert(L->Conversions.size() == R->Conversions.size()); |
| 7009 | |
| 7010 | int leftBetter = 0; |
John McCall | 3a81337 | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 7011 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
| 7012 | for (unsigned E = L->Conversions.size(); I != E; ++I) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7013 | switch (CompareImplicitConversionSequences(S, |
| 7014 | L->Conversions[I], |
| 7015 | R->Conversions[I])) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7016 | case ImplicitConversionSequence::Better: |
| 7017 | leftBetter++; |
| 7018 | break; |
| 7019 | |
| 7020 | case ImplicitConversionSequence::Worse: |
| 7021 | leftBetter--; |
| 7022 | break; |
| 7023 | |
| 7024 | case ImplicitConversionSequence::Indistinguishable: |
| 7025 | break; |
| 7026 | } |
| 7027 | } |
| 7028 | if (leftBetter > 0) return true; |
| 7029 | if (leftBetter < 0) return false; |
| 7030 | |
| 7031 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 7032 | return false; |
| 7033 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 7034 | // TODO: others? |
| 7035 | } |
| 7036 | |
| 7037 | // Sort everything else by location. |
| 7038 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 7039 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 7040 | |
| 7041 | // Put candidates without locations (e.g. builtins) at the end. |
| 7042 | if (LLoc.isInvalid()) return false; |
| 7043 | if (RLoc.isInvalid()) return true; |
| 7044 | |
| 7045 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7046 | } |
| 7047 | }; |
| 7048 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7049 | /// CompleteNonViableCandidate - Normally, overload resolution only |
| 7050 | /// computes up to the first |
| 7051 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
| 7052 | Expr **Args, unsigned NumArgs) { |
| 7053 | assert(!Cand->Viable); |
| 7054 | |
| 7055 | // Don't do anything on failures other than bad conversion. |
| 7056 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 7057 | |
| 7058 | // Skip forward to the first bad conversion. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 7059 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7060 | unsigned ConvCount = Cand->Conversions.size(); |
| 7061 | while (true) { |
| 7062 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 7063 | ConvIdx++; |
| 7064 | if (Cand->Conversions[ConvIdx - 1].isBad()) |
| 7065 | break; |
| 7066 | } |
| 7067 | |
| 7068 | if (ConvIdx == ConvCount) |
| 7069 | return; |
| 7070 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 7071 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 7072 | "remaining conversion is initialized?"); |
| 7073 | |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 7074 | // FIXME: this should probably be preserved from the overload |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7075 | // operation somehow. |
| 7076 | bool SuppressUserConversions = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7077 | |
| 7078 | const FunctionProtoType* Proto; |
| 7079 | unsigned ArgIdx = ConvIdx; |
| 7080 | |
| 7081 | if (Cand->IsSurrogate) { |
| 7082 | QualType ConvType |
| 7083 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 7084 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 7085 | ConvType = ConvPtrType->getPointeeType(); |
| 7086 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 7087 | ArgIdx--; |
| 7088 | } else if (Cand->Function) { |
| 7089 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 7090 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 7091 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 7092 | ArgIdx--; |
| 7093 | } else { |
| 7094 | // Builtin binary operator with a bad first conversion. |
| 7095 | assert(ConvCount <= 3); |
| 7096 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 7097 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7098 | = TryCopyInitialization(S, Args[ConvIdx], |
| 7099 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7100 | SuppressUserConversions, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7101 | /*InOverloadResolution*/ true); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7102 | return; |
| 7103 | } |
| 7104 | |
| 7105 | // Fill in the rest of the conversions. |
| 7106 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 7107 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
| 7108 | if (ArgIdx < NumArgsInProto) |
| 7109 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7110 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7111 | SuppressUserConversions, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7112 | /*InOverloadResolution=*/true); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7113 | else |
| 7114 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 7115 | } |
| 7116 | } |
| 7117 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7118 | } // end anonymous namespace |
| 7119 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7120 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 7121 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7122 | /// set. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7123 | void OverloadCandidateSet::NoteCandidates(Sema &S, |
| 7124 | OverloadCandidateDisplayKind OCD, |
| 7125 | Expr **Args, unsigned NumArgs, |
| 7126 | const char *Opc, |
| 7127 | SourceLocation OpLoc) { |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7128 | // Sort the candidates by viability and position. Sorting directly would |
| 7129 | // be prohibitive, so we make a set of pointers and sort those. |
| 7130 | llvm::SmallVector<OverloadCandidate*, 32> Cands; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7131 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
| 7132 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7133 | if (Cand->Viable) |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7134 | Cands.push_back(Cand); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7135 | else if (OCD == OCD_AllCandidates) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7136 | CompleteNonViableCandidate(S, Cand, Args, NumArgs); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7137 | if (Cand->Function || Cand->IsSurrogate) |
| 7138 | Cands.push_back(Cand); |
| 7139 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 7140 | // want to list every possible builtin candidate. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7141 | } |
| 7142 | } |
| 7143 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 7144 | std::sort(Cands.begin(), Cands.end(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7145 | CompareOverloadCandidatesForDisplay(S)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7146 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7147 | bool ReportedAmbiguousConversions = false; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7148 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7149 | llvm::SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7150 | const Diagnostic::OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7151 | unsigned CandsShown = 0; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7152 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 7153 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 7154 | |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7155 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 7156 | // the user with. FIXME: This limit should depend on details of the |
| 7157 | // candidate list. |
| 7158 | if (CandsShown >= 4 && ShowOverloads == Diagnostic::Ovl_Best) { |
| 7159 | break; |
| 7160 | } |
| 7161 | ++CandsShown; |
| 7162 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7163 | if (Cand->Function) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7164 | NoteFunctionCandidate(S, Cand, Args, NumArgs); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7165 | else if (Cand->IsSurrogate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7166 | NoteSurrogateCandidate(S, Cand); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7167 | else { |
| 7168 | assert(Cand->Viable && |
| 7169 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7170 | // Generally we only see ambiguities including viable builtin |
| 7171 | // operators if overload resolution got screwed up by an |
| 7172 | // ambiguous user-defined conversion. |
| 7173 | // |
| 7174 | // FIXME: It's quite possible for different conversions to see |
| 7175 | // different ambiguities, though. |
| 7176 | if (!ReportedAmbiguousConversions) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7177 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7178 | ReportedAmbiguousConversions = true; |
| 7179 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7180 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7181 | // If this is a viable builtin, print it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7182 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7183 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7184 | } |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7185 | |
| 7186 | if (I != E) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7187 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7188 | } |
| 7189 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7190 | // [PossiblyAFunctionType] --> [Return] |
| 7191 | // NonFunctionType --> NonFunctionType |
| 7192 | // R (A) --> R(A) |
| 7193 | // R (*)(A) --> R (A) |
| 7194 | // R (&)(A) --> R (A) |
| 7195 | // R (S::*)(A) --> R (A) |
| 7196 | QualType Sema::ExtractUnqualifiedFunctionType(QualType PossiblyAFunctionType) { |
| 7197 | QualType Ret = PossiblyAFunctionType; |
| 7198 | if (const PointerType *ToTypePtr = |
| 7199 | PossiblyAFunctionType->getAs<PointerType>()) |
| 7200 | Ret = ToTypePtr->getPointeeType(); |
| 7201 | else if (const ReferenceType *ToTypeRef = |
| 7202 | PossiblyAFunctionType->getAs<ReferenceType>()) |
| 7203 | Ret = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 7204 | else if (const MemberPointerType *MemTypePtr = |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7205 | PossiblyAFunctionType->getAs<MemberPointerType>()) |
| 7206 | Ret = MemTypePtr->getPointeeType(); |
| 7207 | Ret = |
| 7208 | Context.getCanonicalType(Ret).getUnqualifiedType(); |
| 7209 | return Ret; |
| 7210 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7211 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7212 | // A helper class to help with address of function resolution |
| 7213 | // - allows us to avoid passing around all those ugly parameters |
| 7214 | class AddressOfFunctionResolver |
| 7215 | { |
| 7216 | Sema& S; |
| 7217 | Expr* SourceExpr; |
| 7218 | const QualType& TargetType; |
| 7219 | QualType TargetFunctionType; // Extracted function type from target type |
| 7220 | |
| 7221 | bool Complain; |
| 7222 | //DeclAccessPair& ResultFunctionAccessPair; |
| 7223 | ASTContext& Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7224 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7225 | bool TargetTypeIsNonStaticMemberFunction; |
| 7226 | bool FoundNonTemplateFunction; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7227 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7228 | OverloadExpr::FindResult OvlExprInfo; |
| 7229 | OverloadExpr *OvlExpr; |
| 7230 | TemplateArgumentListInfo OvlExplicitTemplateArgs; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7231 | llvm::SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7232 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7233 | public: |
| 7234 | AddressOfFunctionResolver(Sema &S, Expr* SourceExpr, |
| 7235 | const QualType& TargetType, bool Complain) |
| 7236 | : S(S), SourceExpr(SourceExpr), TargetType(TargetType), |
| 7237 | Complain(Complain), Context(S.getASTContext()), |
| 7238 | TargetTypeIsNonStaticMemberFunction( |
| 7239 | !!TargetType->getAs<MemberPointerType>()), |
| 7240 | FoundNonTemplateFunction(false), |
| 7241 | OvlExprInfo(OverloadExpr::find(SourceExpr)), |
| 7242 | OvlExpr(OvlExprInfo.Expression) |
| 7243 | { |
| 7244 | ExtractUnqualifiedFunctionTypeFromTargetType(); |
| 7245 | |
| 7246 | if (!TargetFunctionType->isFunctionType()) { |
| 7247 | if (OvlExpr->hasExplicitTemplateArgs()) { |
| 7248 | DeclAccessPair dap; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7249 | if (FunctionDecl* Fn = S.ResolveSingleFunctionTemplateSpecialization( |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7250 | OvlExpr, false, &dap) ) { |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 7251 | |
| 7252 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 7253 | if (!Method->isStatic()) { |
| 7254 | // If the target type is a non-function type and the function |
| 7255 | // found is a non-static member function, pretend as if that was |
| 7256 | // the target, it's the only possible type to end up with. |
| 7257 | TargetTypeIsNonStaticMemberFunction = true; |
| 7258 | |
| 7259 | // And skip adding the function if its not in the proper form. |
| 7260 | // We'll diagnose this due to an empty set of functions. |
| 7261 | if (!OvlExprInfo.HasFormOfMemberPointer) |
| 7262 | return; |
| 7263 | } |
| 7264 | } |
| 7265 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7266 | Matches.push_back(std::make_pair(dap,Fn)); |
| 7267 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 7268 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7269 | return; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 7270 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7271 | |
| 7272 | if (OvlExpr->hasExplicitTemplateArgs()) |
| 7273 | OvlExpr->getExplicitTemplateArgs().copyInto(OvlExplicitTemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7274 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7275 | if (FindAllFunctionsThatMatchTargetTypeExactly()) { |
| 7276 | // C++ [over.over]p4: |
| 7277 | // If more than one function is selected, [...] |
| 7278 | if (Matches.size() > 1) { |
| 7279 | if (FoundNonTemplateFunction) |
| 7280 | EliminateAllTemplateMatches(); |
| 7281 | else |
| 7282 | EliminateAllExceptMostSpecializedTemplate(); |
| 7283 | } |
| 7284 | } |
| 7285 | } |
| 7286 | |
| 7287 | private: |
| 7288 | bool isTargetTypeAFunction() const { |
| 7289 | return TargetFunctionType->isFunctionType(); |
| 7290 | } |
| 7291 | |
| 7292 | // [ToType] [Return] |
| 7293 | |
| 7294 | // R (*)(A) --> R (A), IsNonStaticMemberFunction = false |
| 7295 | // R (&)(A) --> R (A), IsNonStaticMemberFunction = false |
| 7296 | // R (S::*)(A) --> R (A), IsNonStaticMemberFunction = true |
| 7297 | void inline ExtractUnqualifiedFunctionTypeFromTargetType() { |
| 7298 | TargetFunctionType = S.ExtractUnqualifiedFunctionType(TargetType); |
| 7299 | } |
| 7300 | |
| 7301 | // return true if any matching specializations were found |
| 7302 | bool AddMatchingTemplateFunction(FunctionTemplateDecl* FunctionTemplate, |
| 7303 | const DeclAccessPair& CurAccessFunPair) { |
| 7304 | if (CXXMethodDecl *Method |
| 7305 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
| 7306 | // Skip non-static function templates when converting to pointer, and |
| 7307 | // static when converting to member pointer. |
| 7308 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 7309 | return false; |
| 7310 | } |
| 7311 | else if (TargetTypeIsNonStaticMemberFunction) |
| 7312 | return false; |
| 7313 | |
| 7314 | // C++ [over.over]p2: |
| 7315 | // If the name is a function template, template argument deduction is |
| 7316 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 7317 | // resulting template argument list is used to generate a single |
| 7318 | // function template specialization, which is added to the set of |
| 7319 | // overloaded functions considered. |
| 7320 | FunctionDecl *Specialization = 0; |
| 7321 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
| 7322 | if (Sema::TemplateDeductionResult Result |
| 7323 | = S.DeduceTemplateArguments(FunctionTemplate, |
| 7324 | &OvlExplicitTemplateArgs, |
| 7325 | TargetFunctionType, Specialization, |
| 7326 | Info)) { |
| 7327 | // FIXME: make a note of the failed deduction for diagnostics. |
| 7328 | (void)Result; |
| 7329 | return false; |
| 7330 | } |
| 7331 | |
| 7332 | // Template argument deduction ensures that we have an exact match. |
| 7333 | // This function template specicalization works. |
| 7334 | Specialization = cast<FunctionDecl>(Specialization->getCanonicalDecl()); |
| 7335 | assert(TargetFunctionType |
| 7336 | == Context.getCanonicalType(Specialization->getType())); |
| 7337 | Matches.push_back(std::make_pair(CurAccessFunPair, Specialization)); |
| 7338 | return true; |
| 7339 | } |
| 7340 | |
| 7341 | bool AddMatchingNonTemplateFunction(NamedDecl* Fn, |
| 7342 | const DeclAccessPair& CurAccessFunPair) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 7343 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 7344 | // Skip non-static functions when converting to pointer, and static |
| 7345 | // when converting to member pointer. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7346 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 7347 | return false; |
| 7348 | } |
| 7349 | else if (TargetTypeIsNonStaticMemberFunction) |
| 7350 | return false; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7351 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 7352 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 7353 | QualType ResultTy; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7354 | if (Context.hasSameUnqualifiedType(TargetFunctionType, |
| 7355 | FunDecl->getType()) || |
| 7356 | IsNoReturnConversion(Context, FunDecl->getType(), TargetFunctionType, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 7357 | ResultTy)) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7358 | Matches.push_back(std::make_pair(CurAccessFunPair, |
| 7359 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 7360 | FoundNonTemplateFunction = true; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7361 | return true; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 7362 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7363 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7364 | |
| 7365 | return false; |
| 7366 | } |
| 7367 | |
| 7368 | bool FindAllFunctionsThatMatchTargetTypeExactly() { |
| 7369 | bool Ret = false; |
| 7370 | |
| 7371 | // If the overload expression doesn't have the form of a pointer to |
| 7372 | // member, don't try to convert it to a pointer-to-member type. |
| 7373 | if (IsInvalidFormOfPointerToMemberFunction()) |
| 7374 | return false; |
| 7375 | |
| 7376 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 7377 | E = OvlExpr->decls_end(); |
| 7378 | I != E; ++I) { |
| 7379 | // Look through any using declarations to find the underlying function. |
| 7380 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 7381 | |
| 7382 | // C++ [over.over]p3: |
| 7383 | // Non-member functions and static member functions match |
| 7384 | // targets of type "pointer-to-function" or "reference-to-function." |
| 7385 | // Nonstatic member functions match targets of |
| 7386 | // type "pointer-to-member-function." |
| 7387 | // Note that according to DR 247, the containing class does not matter. |
| 7388 | if (FunctionTemplateDecl *FunctionTemplate |
| 7389 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
| 7390 | if (AddMatchingTemplateFunction(FunctionTemplate, I.getPair())) |
| 7391 | Ret = true; |
| 7392 | } |
| 7393 | // If we have explicit template arguments supplied, skip non-templates. |
| 7394 | else if (!OvlExpr->hasExplicitTemplateArgs() && |
| 7395 | AddMatchingNonTemplateFunction(Fn, I.getPair())) |
| 7396 | Ret = true; |
| 7397 | } |
| 7398 | assert(Ret || Matches.empty()); |
| 7399 | return Ret; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7400 | } |
| 7401 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7402 | void EliminateAllExceptMostSpecializedTemplate() { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7403 | // [...] and any given function template specialization F1 is |
| 7404 | // eliminated if the set contains a second function template |
| 7405 | // specialization whose function template is more specialized |
| 7406 | // than the function template of F1 according to the partial |
| 7407 | // ordering rules of 14.5.5.2. |
| 7408 | |
| 7409 | // The algorithm specified above is quadratic. We instead use a |
| 7410 | // two-pass algorithm (similar to the one used to identify the |
| 7411 | // best viable function in an overload set) that identifies the |
| 7412 | // best function template (if it exists). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7413 | |
| 7414 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 7415 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 7416 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7417 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7418 | UnresolvedSetIterator Result = |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7419 | S.getMostSpecialized(MatchesCopy.begin(), MatchesCopy.end(), |
| 7420 | TPOC_Other, 0, SourceExpr->getLocStart(), |
| 7421 | S.PDiag(), |
| 7422 | S.PDiag(diag::err_addr_ovl_ambiguous) |
| 7423 | << Matches[0].second->getDeclName(), |
| 7424 | S.PDiag(diag::note_ovl_candidate) |
| 7425 | << (unsigned) oc_function_template, |
| 7426 | Complain); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7427 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7428 | if (Result != MatchesCopy.end()) { |
| 7429 | // Make it the first and only element |
| 7430 | Matches[0].first = Matches[Result - MatchesCopy.begin()].first; |
| 7431 | Matches[0].second = cast<FunctionDecl>(*Result); |
| 7432 | Matches.resize(1); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7433 | } |
| 7434 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7435 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7436 | void EliminateAllTemplateMatches() { |
| 7437 | // [...] any function template specializations in the set are |
| 7438 | // eliminated if the set also contains a non-template function, [...] |
| 7439 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
| 7440 | if (Matches[I].second->getPrimaryTemplate() == 0) |
| 7441 | ++I; |
| 7442 | else { |
| 7443 | Matches[I] = Matches[--N]; |
| 7444 | Matches.set_size(N); |
| 7445 | } |
| 7446 | } |
| 7447 | } |
| 7448 | |
| 7449 | public: |
| 7450 | void ComplainNoMatchesFound() const { |
| 7451 | assert(Matches.empty()); |
| 7452 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_no_viable) |
| 7453 | << OvlExpr->getName() << TargetFunctionType |
| 7454 | << OvlExpr->getSourceRange(); |
| 7455 | S.NoteAllOverloadCandidates(OvlExpr); |
| 7456 | } |
| 7457 | |
| 7458 | bool IsInvalidFormOfPointerToMemberFunction() const { |
| 7459 | return TargetTypeIsNonStaticMemberFunction && |
| 7460 | !OvlExprInfo.HasFormOfMemberPointer; |
| 7461 | } |
| 7462 | |
| 7463 | void ComplainIsInvalidFormOfPointerToMemberFunction() const { |
| 7464 | // TODO: Should we condition this on whether any functions might |
| 7465 | // have matched, or is it more appropriate to do that in callers? |
| 7466 | // TODO: a fixit wouldn't hurt. |
| 7467 | S.Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
| 7468 | << TargetType << OvlExpr->getSourceRange(); |
| 7469 | } |
| 7470 | |
| 7471 | void ComplainOfInvalidConversion() const { |
| 7472 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 7473 | << OvlExpr->getName() << TargetType; |
| 7474 | } |
| 7475 | |
| 7476 | void ComplainMultipleMatchesFound() const { |
| 7477 | assert(Matches.size() > 1); |
| 7478 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 7479 | << OvlExpr->getName() |
| 7480 | << OvlExpr->getSourceRange(); |
| 7481 | S.NoteAllOverloadCandidates(OvlExpr); |
| 7482 | } |
| 7483 | |
| 7484 | int getNumMatches() const { return Matches.size(); } |
| 7485 | |
| 7486 | FunctionDecl* getMatchingFunctionDecl() const { |
| 7487 | if (Matches.size() != 1) return 0; |
| 7488 | return Matches[0].second; |
| 7489 | } |
| 7490 | |
| 7491 | const DeclAccessPair* getMatchingFunctionAccessPair() const { |
| 7492 | if (Matches.size() != 1) return 0; |
| 7493 | return &Matches[0].first; |
| 7494 | } |
| 7495 | }; |
| 7496 | |
| 7497 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 7498 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 7499 | /// expression with overloaded function type and @p ToType is the type |
| 7500 | /// we're trying to resolve to. For example: |
| 7501 | /// |
| 7502 | /// @code |
| 7503 | /// int f(double); |
| 7504 | /// int f(int); |
| 7505 | /// |
| 7506 | /// int (*pfd)(double) = f; // selects f(double) |
| 7507 | /// @endcode |
| 7508 | /// |
| 7509 | /// This routine returns the resulting FunctionDecl if it could be |
| 7510 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 7511 | /// routine will emit diagnostics if there is an error. |
| 7512 | FunctionDecl * |
| 7513 | Sema::ResolveAddressOfOverloadedFunction(Expr *AddressOfExpr, QualType TargetType, |
| 7514 | bool Complain, |
| 7515 | DeclAccessPair &FoundResult) { |
| 7516 | |
| 7517 | assert(AddressOfExpr->getType() == Context.OverloadTy); |
| 7518 | |
| 7519 | AddressOfFunctionResolver Resolver(*this, AddressOfExpr, TargetType, Complain); |
| 7520 | int NumMatches = Resolver.getNumMatches(); |
| 7521 | FunctionDecl* Fn = 0; |
| 7522 | if ( NumMatches == 0 && Complain) { |
| 7523 | if (Resolver.IsInvalidFormOfPointerToMemberFunction()) |
| 7524 | Resolver.ComplainIsInvalidFormOfPointerToMemberFunction(); |
| 7525 | else |
| 7526 | Resolver.ComplainNoMatchesFound(); |
| 7527 | } |
| 7528 | else if (NumMatches > 1 && Complain) |
| 7529 | Resolver.ComplainMultipleMatchesFound(); |
| 7530 | else if (NumMatches == 1) { |
| 7531 | Fn = Resolver.getMatchingFunctionDecl(); |
| 7532 | assert(Fn); |
| 7533 | FoundResult = *Resolver.getMatchingFunctionAccessPair(); |
| 7534 | MarkDeclarationReferenced(AddressOfExpr->getLocStart(), Fn); |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 7535 | if (Complain) |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7536 | CheckAddressOfMemberAccess(AddressOfExpr, FoundResult); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 7537 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7538 | |
| 7539 | return Fn; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7540 | } |
| 7541 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7542 | /// \brief Given an expression that refers to an overloaded function, try to |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7543 | /// resolve that overloaded function expression down to a single function. |
| 7544 | /// |
| 7545 | /// This routine can only resolve template-ids that refer to a single function |
| 7546 | /// template, where that template-id refers to a single template whose template |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7547 | /// arguments are either provided by the template-id or have defaults, |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7548 | /// as described in C++0x [temp.arg.explicit]p3. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7549 | FunctionDecl * |
| 7550 | Sema::ResolveSingleFunctionTemplateSpecialization(OverloadExpr *ovl, |
| 7551 | bool Complain, |
| 7552 | DeclAccessPair *FoundResult) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7553 | // C++ [over.over]p1: |
| 7554 | // [...] [Note: any redundant set of parentheses surrounding the |
| 7555 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7556 | // C++ [over.over]p1: |
| 7557 | // [...] The overloaded function name can be preceded by the & |
| 7558 | // operator. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7559 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7560 | // If we didn't actually find any template-ids, we're done. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7561 | if (!ovl->hasExplicitTemplateArgs()) |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7562 | return 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 7563 | |
| 7564 | TemplateArgumentListInfo ExplicitTemplateArgs; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7565 | ovl->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7566 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7567 | // Look through all of the overloaded functions, searching for one |
| 7568 | // whose type matches exactly. |
| 7569 | FunctionDecl *Matched = 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7570 | for (UnresolvedSetIterator I = ovl->decls_begin(), |
| 7571 | E = ovl->decls_end(); I != E; ++I) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7572 | // C++0x [temp.arg.explicit]p3: |
| 7573 | // [...] In contexts where deduction is done and fails, or in contexts |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7574 | // where deduction is not done, if a template argument list is |
| 7575 | // specified and it, along with any default template arguments, |
| 7576 | // identifies a single function template specialization, then the |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7577 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | 66a8c9a | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 7578 | FunctionTemplateDecl *FunctionTemplate |
| 7579 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7580 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7581 | // C++ [over.over]p2: |
| 7582 | // If the name is a function template, template argument deduction is |
| 7583 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 7584 | // resulting template argument list is used to generate a single |
| 7585 | // function template specialization, which is added to the set of |
| 7586 | // overloaded functions considered. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7587 | FunctionDecl *Specialization = 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7588 | TemplateDeductionInfo Info(Context, ovl->getNameLoc()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7589 | if (TemplateDeductionResult Result |
| 7590 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 7591 | Specialization, Info)) { |
| 7592 | // FIXME: make a note of the failed deduction for diagnostics. |
| 7593 | (void)Result; |
| 7594 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7595 | } |
| 7596 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7597 | assert(Specialization && "no specialization and no error?"); |
| 7598 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7599 | // Multiple matches; we can't resolve to a single declaration. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7600 | if (Matched) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7601 | if (Complain) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7602 | Diag(ovl->getExprLoc(), diag::err_addr_ovl_ambiguous) |
| 7603 | << ovl->getName(); |
| 7604 | NoteAllOverloadCandidates(ovl); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7605 | } |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7606 | return 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7607 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7608 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7609 | Matched = Specialization; |
| 7610 | if (FoundResult) *FoundResult = I.getPair(); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7611 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7612 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7613 | return Matched; |
| 7614 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7615 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7616 | |
| 7617 | |
| 7618 | |
| 7619 | // Resolve and fix an overloaded expression that |
| 7620 | // can be resolved because it identifies a single function |
| 7621 | // template specialization |
| 7622 | // Last three arguments should only be supplied if Complain = true |
| 7623 | ExprResult Sema::ResolveAndFixSingleFunctionTemplateSpecialization( |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7624 | Expr *SrcExpr, bool doFunctionPointerConverion, bool complain, |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7625 | const SourceRange& OpRangeForComplaining, |
| 7626 | QualType DestTypeForComplaining, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7627 | unsigned DiagIDForComplaining) { |
| 7628 | assert(SrcExpr->getType() == Context.OverloadTy); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7629 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7630 | OverloadExpr::FindResult ovl = OverloadExpr::find(SrcExpr); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7631 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7632 | DeclAccessPair found; |
| 7633 | ExprResult SingleFunctionExpression; |
| 7634 | if (FunctionDecl *fn = ResolveSingleFunctionTemplateSpecialization( |
| 7635 | ovl.Expression, /*complain*/ false, &found)) { |
| 7636 | if (DiagnoseUseOfDecl(fn, SrcExpr->getSourceRange().getBegin())) |
| 7637 | return ExprError(); |
| 7638 | |
| 7639 | // It is only correct to resolve to an instance method if we're |
| 7640 | // resolving a form that's permitted to be a pointer to member. |
| 7641 | // Otherwise we'll end up making a bound member expression, which |
| 7642 | // is illegal in all the contexts we resolve like this. |
| 7643 | if (!ovl.HasFormOfMemberPointer && |
| 7644 | isa<CXXMethodDecl>(fn) && |
| 7645 | cast<CXXMethodDecl>(fn)->isInstance()) { |
| 7646 | if (complain) { |
| 7647 | Diag(ovl.Expression->getExprLoc(), |
| 7648 | diag::err_invalid_use_of_bound_member_func) |
| 7649 | << ovl.Expression->getSourceRange(); |
| 7650 | // TODO: I believe we only end up here if there's a mix of |
| 7651 | // static and non-static candidates (otherwise the expression |
| 7652 | // would have 'bound member' type, not 'overload' type). |
| 7653 | // Ideally we would note which candidate was chosen and why |
| 7654 | // the static candidates were rejected. |
| 7655 | } |
| 7656 | |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 7657 | return ExprError(); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7658 | } |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 7659 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7660 | // Fix the expresion to refer to 'fn'. |
| 7661 | SingleFunctionExpression = |
| 7662 | Owned(FixOverloadedFunctionReference(SrcExpr, found, fn)); |
| 7663 | |
| 7664 | // If desired, do function-to-pointer decay. |
| 7665 | if (doFunctionPointerConverion) |
| 7666 | SingleFunctionExpression = |
| 7667 | DefaultFunctionArrayLvalueConversion(SingleFunctionExpression.take()); |
| 7668 | } |
| 7669 | |
| 7670 | if (!SingleFunctionExpression.isUsable()) { |
| 7671 | if (complain) { |
| 7672 | Diag(OpRangeForComplaining.getBegin(), DiagIDForComplaining) |
| 7673 | << ovl.Expression->getName() |
| 7674 | << DestTypeForComplaining |
| 7675 | << OpRangeForComplaining |
| 7676 | << ovl.Expression->getQualifierLoc().getSourceRange(); |
| 7677 | NoteAllOverloadCandidates(SrcExpr); |
| 7678 | } |
| 7679 | return ExprError(); |
| 7680 | } |
| 7681 | |
| 7682 | return SingleFunctionExpression; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7683 | } |
| 7684 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7685 | /// \brief Add a single candidate to the overload set. |
| 7686 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7687 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 7688 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7689 | Expr **Args, unsigned NumArgs, |
| 7690 | OverloadCandidateSet &CandidateSet, |
| 7691 | bool PartialOverloading) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7692 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7693 | if (isa<UsingShadowDecl>(Callee)) |
| 7694 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 7695 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7696 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7697 | assert(!ExplicitTemplateArgs && "Explicit template arguments?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7698 | S.AddOverloadCandidate(Func, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 7699 | false, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7700 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7701 | } |
| 7702 | |
| 7703 | if (FunctionTemplateDecl *FuncTemplate |
| 7704 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7705 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
| 7706 | ExplicitTemplateArgs, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7707 | Args, NumArgs, CandidateSet); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7708 | return; |
| 7709 | } |
| 7710 | |
| 7711 | assert(false && "unhandled case in overloaded call candidate"); |
| 7712 | |
| 7713 | // do nothing? |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7714 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7715 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7716 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 7717 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7718 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7719 | Expr **Args, unsigned NumArgs, |
| 7720 | OverloadCandidateSet &CandidateSet, |
| 7721 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7722 | |
| 7723 | #ifndef NDEBUG |
| 7724 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 7725 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7726 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7727 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 7728 | // and let Y be the lookup set produced by argument dependent |
| 7729 | // lookup (defined as follows). If X contains |
| 7730 | // |
| 7731 | // -- a declaration of a class member, or |
| 7732 | // |
| 7733 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7734 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7735 | // |
| 7736 | // -- a declaration that is neither a function or a function |
| 7737 | // template |
| 7738 | // |
| 7739 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7740 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7741 | if (ULE->requiresADL()) { |
| 7742 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 7743 | E = ULE->decls_end(); I != E; ++I) { |
| 7744 | assert(!(*I)->getDeclContext()->isRecord()); |
| 7745 | assert(isa<UsingShadowDecl>(*I) || |
| 7746 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 7747 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7748 | } |
| 7749 | } |
| 7750 | #endif |
| 7751 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7752 | // It would be nice to avoid this copy. |
| 7753 | TemplateArgumentListInfo TABuffer; |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 7754 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7755 | if (ULE->hasExplicitTemplateArgs()) { |
| 7756 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 7757 | ExplicitTemplateArgs = &TABuffer; |
| 7758 | } |
| 7759 | |
| 7760 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 7761 | E = ULE->decls_end(); I != E; ++I) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7762 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7763 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7764 | PartialOverloading); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7765 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7766 | if (ULE->requiresADL()) |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7767 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
| 7768 | Args, NumArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7769 | ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7770 | CandidateSet, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 7771 | PartialOverloading, |
| 7772 | ULE->isStdAssociatedNamespace()); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7773 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7774 | |
| 7775 | /// Attempts to recover from a call where no functions were found. |
| 7776 | /// |
| 7777 | /// Returns true if new candidates were found. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7778 | static ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 7779 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7780 | UnresolvedLookupExpr *ULE, |
| 7781 | SourceLocation LParenLoc, |
| 7782 | Expr **Args, unsigned NumArgs, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7783 | SourceLocation RParenLoc) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7784 | |
| 7785 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 7786 | SS.Adopt(ULE->getQualifierLoc()); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7787 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7788 | TemplateArgumentListInfo TABuffer; |
| 7789 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 7790 | if (ULE->hasExplicitTemplateArgs()) { |
| 7791 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 7792 | ExplicitTemplateArgs = &TABuffer; |
| 7793 | } |
| 7794 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7795 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 7796 | Sema::LookupOrdinaryName); |
Douglas Gregor | 91f7ac7 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 7797 | if (SemaRef.DiagnoseEmptyLookup(S, SS, R, Sema::CTC_Expression)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7798 | return ExprError(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7799 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7800 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 7801 | |
| 7802 | // Build an implicit member call if appropriate. Just drop the |
| 7803 | // casts and such from the call, we don't really care. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7804 | ExprResult NewFn = ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7805 | if ((*R.begin())->isCXXClassMember()) |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7806 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, |
| 7807 | ExplicitTemplateArgs); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7808 | else if (ExplicitTemplateArgs) |
| 7809 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 7810 | else |
| 7811 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 7812 | |
| 7813 | if (NewFn.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7814 | return ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7815 | |
| 7816 | // This shouldn't cause an infinite loop because we're giving it |
| 7817 | // an expression with non-empty lookup results, which should never |
| 7818 | // end up here. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7819 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, NewFn.take(), LParenLoc, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 7820 | MultiExprArg(Args, NumArgs), RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7821 | } |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 7822 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7823 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7824 | /// (which eventually refers to the declaration Func) and the call |
| 7825 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 7826 | /// to a specific function. If overload resolution succeeds, returns |
| 7827 | /// the function declaration produced by overload |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 7828 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7829 | /// arguments and Fn, and returns NULL. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7830 | ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 7831 | Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, UnresolvedLookupExpr *ULE, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7832 | SourceLocation LParenLoc, |
| 7833 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 7834 | SourceLocation RParenLoc, |
| 7835 | Expr *ExecConfig) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7836 | #ifndef NDEBUG |
| 7837 | if (ULE->requiresADL()) { |
| 7838 | // To do ADL, we must have found an unqualified name. |
| 7839 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 7840 | |
| 7841 | // We don't perform ADL for implicit declarations of builtins. |
| 7842 | // Verify that this was correctly set up. |
| 7843 | FunctionDecl *F; |
| 7844 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 7845 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 7846 | F->getBuiltinID() && F->isImplicit()) |
| 7847 | assert(0 && "performing ADL for builtin"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7848 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7849 | // We don't perform ADL in C. |
| 7850 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 7851 | } else |
| 7852 | assert(!ULE->isStdAssociatedNamespace() && |
| 7853 | "std is associated namespace but not doing ADL"); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7854 | #endif |
| 7855 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7856 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 7857 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7858 | // Add the functions denoted by the callee to the set of candidate |
| 7859 | // functions, including those from argument-dependent lookup. |
| 7860 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7861 | |
| 7862 | // If we found nothing, try to recover. |
| 7863 | // AddRecoveryCallCandidates diagnoses the error itself, so we just |
| 7864 | // bailout out if it fails. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7865 | if (CandidateSet.empty()) |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 7866 | return BuildRecoveryCallExpr(*this, S, Fn, ULE, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 7867 | RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7868 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7869 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7870 | switch (CandidateSet.BestViableFunction(*this, Fn->getLocStart(), Best)) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7871 | case OR_Success: { |
| 7872 | FunctionDecl *FDecl = Best->Function; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 7873 | MarkDeclarationReferenced(Fn->getExprLoc(), FDecl); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7874 | CheckUnresolvedLookupAccess(ULE, Best->FoundDecl); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7875 | DiagnoseUseOfDecl(FDecl? FDecl : Best->FoundDecl.getDecl(), |
| 7876 | ULE->getNameLoc()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7877 | Fn = FixOverloadedFunctionReference(Fn, Best->FoundDecl, FDecl); |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 7878 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 7879 | ExecConfig); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7880 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7881 | |
| 7882 | case OR_No_Viable_Function: |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 7883 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7884 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7885 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7886 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7887 | break; |
| 7888 | |
| 7889 | case OR_Ambiguous: |
| 7890 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7891 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7892 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7893 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7894 | |
| 7895 | case OR_Deleted: |
Fariborz Jahanian | 2b982b7 | 2011-02-25 18:38:59 +0000 | [diff] [blame] | 7896 | { |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 7897 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 7898 | << Best->Function->isDeleted() |
| 7899 | << ULE->getName() |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 7900 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 7901 | << Fn->getSourceRange(); |
Fariborz Jahanian | 2b982b7 | 2011-02-25 18:38:59 +0000 | [diff] [blame] | 7902 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
| 7903 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7904 | break; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7905 | } |
| 7906 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 7907 | // Overload resolution failed. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7908 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7909 | } |
| 7910 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7911 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 7912 | return Functions.size() > 1 || |
| 7913 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 7914 | } |
| 7915 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7916 | /// \brief Create a unary operation that may resolve to an overloaded |
| 7917 | /// operator. |
| 7918 | /// |
| 7919 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 7920 | /// |
| 7921 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 7922 | /// operator. |
| 7923 | /// |
| 7924 | /// \param Functions The set of non-member functions that will be |
| 7925 | /// considered by overload resolution. The caller needs to build this |
| 7926 | /// set based on the context using, e.g., |
| 7927 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 7928 | /// set should not contain any member functions; those will be added |
| 7929 | /// by CreateOverloadedUnaryOp(). |
| 7930 | /// |
| 7931 | /// \param input The input argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7932 | ExprResult |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7933 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 7934 | const UnresolvedSetImpl &Fns, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7935 | Expr *Input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7936 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7937 | |
| 7938 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 7939 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 7940 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7941 | // TODO: provide better source location info. |
| 7942 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7943 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7944 | if (Input->getObjectKind() == OK_ObjCProperty) { |
| 7945 | ExprResult Result = ConvertPropertyForRValue(Input); |
| 7946 | if (Result.isInvalid()) |
| 7947 | return ExprError(); |
| 7948 | Input = Result.take(); |
| 7949 | } |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 7950 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7951 | Expr *Args[2] = { Input, 0 }; |
| 7952 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7953 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7954 | // For post-increment and post-decrement, add the implicit '0' as |
| 7955 | // the second argument, so that we know this is a post-increment or |
| 7956 | // post-decrement. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7957 | if (Opc == UO_PostInc || Opc == UO_PostDec) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7958 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 7959 | Args[1] = IntegerLiteral::Create(Context, Zero, Context.IntTy, |
| 7960 | SourceLocation()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7961 | NumArgs = 2; |
| 7962 | } |
| 7963 | |
| 7964 | if (Input->isTypeDependent()) { |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 7965 | if (Fns.empty()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7966 | return Owned(new (Context) UnaryOperator(Input, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7967 | Opc, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 7968 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7969 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 7970 | OpLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7971 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7972 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7973 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 7974 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 7975 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 7976 | /*ADL*/ true, IsOverloaded(Fns), |
| 7977 | Fns.begin(), Fns.end()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7978 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 7979 | &Args[0], NumArgs, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7980 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7981 | VK_RValue, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7982 | OpLoc)); |
| 7983 | } |
| 7984 | |
| 7985 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7986 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7987 | |
| 7988 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7989 | AddFunctionCandidates(Fns, &Args[0], NumArgs, CandidateSet, false); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7990 | |
| 7991 | // Add operator candidates that are member functions. |
| 7992 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 7993 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7994 | // Add candidates from ADL. |
| 7995 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Douglas Gregor | dc81c88 | 2010-02-05 05:15:43 +0000 | [diff] [blame] | 7996 | Args, NumArgs, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7997 | /*ExplicitTemplateArgs*/ 0, |
| 7998 | CandidateSet); |
| 7999 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8000 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 8001 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8002 | |
| 8003 | // Perform overload resolution. |
| 8004 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8005 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8006 | case OR_Success: { |
| 8007 | // We found a built-in operator or an overloaded operator. |
| 8008 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8009 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8010 | if (FnDecl) { |
| 8011 | // We matched an overloaded operator. Build a call to that |
| 8012 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8013 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8014 | MarkDeclarationReferenced(OpLoc, FnDecl); |
| 8015 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8016 | // Convert the arguments. |
| 8017 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8018 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 8019 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8020 | ExprResult InputRes = |
| 8021 | PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 8022 | Best->FoundDecl, Method); |
| 8023 | if (InputRes.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8024 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8025 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8026 | } else { |
| 8027 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8028 | ExprResult InputInit |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 8029 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 8030 | Context, |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 8031 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8032 | SourceLocation(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8033 | Input); |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 8034 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8035 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8036 | Input = InputInit.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8037 | } |
| 8038 | |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8039 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 8040 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8041 | // Determine the result type. |
| 8042 | QualType ResultTy = FnDecl->getResultType(); |
| 8043 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 8044 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8045 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8046 | // Build the actual expression node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8047 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl); |
| 8048 | if (FnExpr.isInvalid()) |
| 8049 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8050 | |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 8051 | Args[0] = Input; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8052 | CallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8053 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8054 | Args, NumArgs, ResultTy, VK, OpLoc); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8055 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8056 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 8057 | FnDecl)) |
| 8058 | return ExprError(); |
| 8059 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8060 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8061 | } else { |
| 8062 | // We matched a built-in operator. Convert the arguments, then |
| 8063 | // break out so that we will build the appropriate built-in |
| 8064 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8065 | ExprResult InputRes = |
| 8066 | PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 8067 | Best->Conversions[0], AA_Passing); |
| 8068 | if (InputRes.isInvalid()) |
| 8069 | return ExprError(); |
| 8070 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8071 | break; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8072 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8073 | } |
| 8074 | |
| 8075 | case OR_No_Viable_Function: |
| 8076 | // No viable function; fall through to handling this as a |
| 8077 | // built-in operator, which will produce an error message for us. |
| 8078 | break; |
| 8079 | |
| 8080 | case OR_Ambiguous: |
| 8081 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 8082 | << UnaryOperator::getOpcodeStr(Opc) |
| 8083 | << Input->getType() |
| 8084 | << Input->getSourceRange(); |
| 8085 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, |
| 8086 | Args, NumArgs, |
| 8087 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
| 8088 | return ExprError(); |
| 8089 | |
| 8090 | case OR_Deleted: |
| 8091 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 8092 | << Best->Function->isDeleted() |
| 8093 | << UnaryOperator::getOpcodeStr(Opc) |
| 8094 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 8095 | << Input->getSourceRange(); |
| 8096 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
| 8097 | return ExprError(); |
| 8098 | } |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8099 | |
| 8100 | // Either we found no viable overloaded operator or we matched a |
| 8101 | // built-in operator. In either case, fall through to trying to |
| 8102 | // build a built-in operation. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8103 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8104 | } |
| 8105 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8106 | /// \brief Create a binary operation that may resolve to an overloaded |
| 8107 | /// operator. |
| 8108 | /// |
| 8109 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 8110 | /// |
| 8111 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 8112 | /// operator. |
| 8113 | /// |
| 8114 | /// \param Functions The set of non-member functions that will be |
| 8115 | /// considered by overload resolution. The caller needs to build this |
| 8116 | /// set based on the context using, e.g., |
| 8117 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 8118 | /// set should not contain any member functions; those will be added |
| 8119 | /// by CreateOverloadedBinOp(). |
| 8120 | /// |
| 8121 | /// \param LHS Left-hand argument. |
| 8122 | /// \param RHS Right-hand argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8123 | ExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8124 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8125 | unsigned OpcIn, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8126 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8127 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8128 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8129 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8130 | |
| 8131 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 8132 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 8133 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 8134 | |
| 8135 | // If either side is type-dependent, create an appropriate dependent |
| 8136 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8137 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8138 | if (Fns.empty()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8139 | // If there are no functions to store, just build a dependent |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8140 | // BinaryOperator or CompoundAssignment. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8141 | if (Opc <= BO_Assign || Opc > BO_OrAssign) |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8142 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8143 | Context.DependentTy, |
| 8144 | VK_RValue, OK_Ordinary, |
| 8145 | OpLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8146 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8147 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 8148 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8149 | VK_LValue, |
| 8150 | OK_Ordinary, |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8151 | Context.DependentTy, |
| 8152 | Context.DependentTy, |
| 8153 | OpLoc)); |
| 8154 | } |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8155 | |
| 8156 | // FIXME: save results of ADL from here? |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8157 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8158 | // TODO: provide better source location info in DNLoc component. |
| 8159 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8160 | UnresolvedLookupExpr *Fn |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 8161 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
| 8162 | NestedNameSpecifierLoc(), OpNameInfo, |
| 8163 | /*ADL*/ true, IsOverloaded(Fns), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 8164 | Fns.begin(), Fns.end()); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8165 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8166 | Args, 2, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8167 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8168 | VK_RValue, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8169 | OpLoc)); |
| 8170 | } |
| 8171 | |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8172 | // Always do property rvalue conversions on the RHS. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8173 | if (Args[1]->getObjectKind() == OK_ObjCProperty) { |
| 8174 | ExprResult Result = ConvertPropertyForRValue(Args[1]); |
| 8175 | if (Result.isInvalid()) |
| 8176 | return ExprError(); |
| 8177 | Args[1] = Result.take(); |
| 8178 | } |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8179 | |
| 8180 | // The LHS is more complicated. |
| 8181 | if (Args[0]->getObjectKind() == OK_ObjCProperty) { |
| 8182 | |
| 8183 | // There's a tension for assignment operators between primitive |
| 8184 | // property assignment and the overloaded operators. |
| 8185 | if (BinaryOperator::isAssignmentOp(Opc)) { |
| 8186 | const ObjCPropertyRefExpr *PRE = LHS->getObjCProperty(); |
| 8187 | |
| 8188 | // Is the property "logically" settable? |
| 8189 | bool Settable = (PRE->isExplicitProperty() || |
| 8190 | PRE->getImplicitPropertySetter()); |
| 8191 | |
| 8192 | // To avoid gratuitously inventing semantics, use the primitive |
| 8193 | // unless it isn't. Thoughts in case we ever really care: |
| 8194 | // - If the property isn't logically settable, we have to |
| 8195 | // load and hope. |
| 8196 | // - If the property is settable and this is simple assignment, |
| 8197 | // we really should use the primitive. |
| 8198 | // - If the property is settable, then we could try overloading |
| 8199 | // on a generic lvalue of the appropriate type; if it works |
| 8200 | // out to a builtin candidate, we would do that same operation |
| 8201 | // on the property, and otherwise just error. |
| 8202 | if (Settable) |
| 8203 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 8204 | } |
| 8205 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8206 | ExprResult Result = ConvertPropertyForRValue(Args[0]); |
| 8207 | if (Result.isInvalid()) |
| 8208 | return ExprError(); |
| 8209 | Args[0] = Result.take(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8210 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8211 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 8212 | // If this is the assignment operator, we only perform overload resolution |
| 8213 | // if the left-hand side is a class or enumeration type. This is actually |
| 8214 | // a hack. The standard requires that we do overload resolution between the |
| 8215 | // various built-in candidates, but as DR507 points out, this can lead to |
| 8216 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 8217 | // Note that we go the traditional code path for compound assignment forms. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8218 | if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8219 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8220 | |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8221 | // If this is the .* operator, which is not overloadable, just |
| 8222 | // create a built-in binary operator. |
| 8223 | if (Opc == BO_PtrMemD) |
| 8224 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 8225 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8226 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8227 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8228 | |
| 8229 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8230 | AddFunctionCandidates(Fns, Args, 2, CandidateSet, false); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8231 | |
| 8232 | // Add operator candidates that are member functions. |
| 8233 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 8234 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8235 | // Add candidates from ADL. |
| 8236 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
| 8237 | Args, 2, |
| 8238 | /*ExplicitTemplateArgs*/ 0, |
| 8239 | CandidateSet); |
| 8240 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8241 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 8242 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8243 | |
| 8244 | // Perform overload resolution. |
| 8245 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8246 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 8247 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8248 | // We found a built-in operator or an overloaded operator. |
| 8249 | FunctionDecl *FnDecl = Best->Function; |
| 8250 | |
| 8251 | if (FnDecl) { |
| 8252 | // We matched an overloaded operator. Build a call to that |
| 8253 | // operator. |
| 8254 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8255 | MarkDeclarationReferenced(OpLoc, FnDecl); |
| 8256 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8257 | // Convert the arguments. |
| 8258 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 8259 | // Best->Access is only meaningful for class members. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8260 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 8261 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8262 | ExprResult Arg1 = |
| 8263 | PerformCopyInitialization( |
| 8264 | InitializedEntity::InitializeParameter(Context, |
| 8265 | FnDecl->getParamDecl(0)), |
| 8266 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8267 | if (Arg1.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8268 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8269 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8270 | ExprResult Arg0 = |
| 8271 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 8272 | Best->FoundDecl, Method); |
| 8273 | if (Arg0.isInvalid()) |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8274 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8275 | Args[0] = Arg0.takeAs<Expr>(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8276 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8277 | } else { |
| 8278 | // Convert the arguments. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8279 | ExprResult Arg0 = PerformCopyInitialization( |
| 8280 | InitializedEntity::InitializeParameter(Context, |
| 8281 | FnDecl->getParamDecl(0)), |
| 8282 | SourceLocation(), Owned(Args[0])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8283 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8284 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8285 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8286 | ExprResult Arg1 = |
| 8287 | PerformCopyInitialization( |
| 8288 | InitializedEntity::InitializeParameter(Context, |
| 8289 | FnDecl->getParamDecl(1)), |
| 8290 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8291 | if (Arg1.isInvalid()) |
| 8292 | return ExprError(); |
| 8293 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 8294 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8295 | } |
| 8296 | |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8297 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 8298 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8299 | // Determine the result type. |
| 8300 | QualType ResultTy = FnDecl->getResultType(); |
| 8301 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 8302 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8303 | |
| 8304 | // Build the actual expression node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8305 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, OpLoc); |
| 8306 | if (FnExpr.isInvalid()) |
| 8307 | return ExprError(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8308 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8309 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8310 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8311 | Args, 2, ResultTy, VK, OpLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8312 | |
| 8313 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 8314 | FnDecl)) |
| 8315 | return ExprError(); |
| 8316 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8317 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8318 | } else { |
| 8319 | // We matched a built-in operator. Convert the arguments, then |
| 8320 | // break out so that we will build the appropriate built-in |
| 8321 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8322 | ExprResult ArgsRes0 = |
| 8323 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 8324 | Best->Conversions[0], AA_Passing); |
| 8325 | if (ArgsRes0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8326 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8327 | Args[0] = ArgsRes0.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8328 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8329 | ExprResult ArgsRes1 = |
| 8330 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 8331 | Best->Conversions[1], AA_Passing); |
| 8332 | if (ArgsRes1.isInvalid()) |
| 8333 | return ExprError(); |
| 8334 | Args[1] = ArgsRes1.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8335 | break; |
| 8336 | } |
| 8337 | } |
| 8338 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8339 | case OR_No_Viable_Function: { |
| 8340 | // C++ [over.match.oper]p9: |
| 8341 | // If the operator is the operator , [...] and there are no |
| 8342 | // viable functions, then the operator is assumed to be the |
| 8343 | // built-in operator and interpreted according to clause 5. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8344 | if (Opc == BO_Comma) |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8345 | break; |
| 8346 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8347 | // For class as left operand for assignment or compound assigment |
| 8348 | // operator do not fall through to handling in built-in, but report that |
| 8349 | // no overloaded assignment operator found |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8350 | ExprResult Result = ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8351 | if (Args[0]->getType()->isRecordType() && |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8352 | Opc >= BO_Assign && Opc <= BO_OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 8353 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 8354 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8355 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8356 | } else { |
| 8357 | // No viable function; try to create a built-in operation, which will |
| 8358 | // produce an error. Then, show the non-viable candidates. |
| 8359 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 8360 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8361 | assert(Result.isInvalid() && |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8362 | "C++ binary operator overloading is missing candidates!"); |
| 8363 | if (Result.isInvalid()) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8364 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 8365 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8366 | return move(Result); |
| 8367 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8368 | |
| 8369 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 8370 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_binary) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8371 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 8372 | << Args[0]->getType() << Args[1]->getType() |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8373 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8374 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, 2, |
| 8375 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8376 | return ExprError(); |
| 8377 | |
| 8378 | case OR_Deleted: |
| 8379 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 8380 | << Best->Function->isDeleted() |
| 8381 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 8382 | << getDeletedOrUnavailableSuffix(Best->Function) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8383 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8384 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8385 | return ExprError(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8386 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8387 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8388 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8389 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8390 | } |
| 8391 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8392 | ExprResult |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8393 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 8394 | SourceLocation RLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8395 | Expr *Base, Expr *Idx) { |
| 8396 | Expr *Args[2] = { Base, Idx }; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8397 | DeclarationName OpName = |
| 8398 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 8399 | |
| 8400 | // If either side is type-dependent, create an appropriate dependent |
| 8401 | // expression. |
| 8402 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 8403 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8404 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8405 | // CHECKME: no 'operator' keyword? |
| 8406 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
| 8407 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8408 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 8409 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 8410 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 8411 | /*ADL*/ true, /*Overloaded*/ false, |
| 8412 | UnresolvedSetIterator(), |
| 8413 | UnresolvedSetIterator()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 8414 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8415 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8416 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 8417 | Args, 2, |
| 8418 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8419 | VK_RValue, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8420 | RLoc)); |
| 8421 | } |
| 8422 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8423 | if (Args[0]->getObjectKind() == OK_ObjCProperty) { |
| 8424 | ExprResult Result = ConvertPropertyForRValue(Args[0]); |
| 8425 | if (Result.isInvalid()) |
| 8426 | return ExprError(); |
| 8427 | Args[0] = Result.take(); |
| 8428 | } |
| 8429 | if (Args[1]->getObjectKind() == OK_ObjCProperty) { |
| 8430 | ExprResult Result = ConvertPropertyForRValue(Args[1]); |
| 8431 | if (Result.isInvalid()) |
| 8432 | return ExprError(); |
| 8433 | Args[1] = Result.take(); |
| 8434 | } |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8435 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8436 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8437 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8438 | |
| 8439 | // Subscript can only be overloaded as a member function. |
| 8440 | |
| 8441 | // Add operator candidates that are member functions. |
| 8442 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 8443 | |
| 8444 | // Add builtin operator candidates. |
| 8445 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 8446 | |
| 8447 | // Perform overload resolution. |
| 8448 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8449 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8450 | case OR_Success: { |
| 8451 | // We found a built-in operator or an overloaded operator. |
| 8452 | FunctionDecl *FnDecl = Best->Function; |
| 8453 | |
| 8454 | if (FnDecl) { |
| 8455 | // We matched an overloaded operator. Build a call to that |
| 8456 | // operator. |
| 8457 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8458 | MarkDeclarationReferenced(LLoc, FnDecl); |
| 8459 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8460 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8461 | DiagnoseUseOfDecl(Best->FoundDecl, LLoc); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8462 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8463 | // Convert the arguments. |
| 8464 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8465 | ExprResult Arg0 = |
| 8466 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 8467 | Best->FoundDecl, Method); |
| 8468 | if (Arg0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8469 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8470 | Args[0] = Arg0.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8471 | |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 8472 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8473 | ExprResult InputInit |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 8474 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 8475 | Context, |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 8476 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8477 | SourceLocation(), |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 8478 | Owned(Args[1])); |
| 8479 | if (InputInit.isInvalid()) |
| 8480 | return ExprError(); |
| 8481 | |
| 8482 | Args[1] = InputInit.takeAs<Expr>(); |
| 8483 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8484 | // Determine the result type |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8485 | QualType ResultTy = FnDecl->getResultType(); |
| 8486 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 8487 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8488 | |
| 8489 | // Build the actual expression node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8490 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, LLoc); |
| 8491 | if (FnExpr.isInvalid()) |
| 8492 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8493 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8494 | CXXOperatorCallExpr *TheCall = |
| 8495 | new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8496 | FnExpr.take(), Args, 2, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8497 | ResultTy, VK, RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8498 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8499 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8500 | FnDecl)) |
| 8501 | return ExprError(); |
| 8502 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8503 | return MaybeBindToTemporary(TheCall); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8504 | } else { |
| 8505 | // We matched a built-in operator. Convert the arguments, then |
| 8506 | // break out so that we will build the appropriate built-in |
| 8507 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8508 | ExprResult ArgsRes0 = |
| 8509 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 8510 | Best->Conversions[0], AA_Passing); |
| 8511 | if (ArgsRes0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8512 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8513 | Args[0] = ArgsRes0.take(); |
| 8514 | |
| 8515 | ExprResult ArgsRes1 = |
| 8516 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 8517 | Best->Conversions[1], AA_Passing); |
| 8518 | if (ArgsRes1.isInvalid()) |
| 8519 | return ExprError(); |
| 8520 | Args[1] = ArgsRes1.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8521 | |
| 8522 | break; |
| 8523 | } |
| 8524 | } |
| 8525 | |
| 8526 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 8527 | if (CandidateSet.empty()) |
| 8528 | Diag(LLoc, diag::err_ovl_no_oper) |
| 8529 | << Args[0]->getType() << /*subscript*/ 0 |
| 8530 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 8531 | else |
| 8532 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 8533 | << Args[0]->getType() |
| 8534 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8535 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 8536 | "[]", LLoc); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 8537 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8538 | } |
| 8539 | |
| 8540 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 8541 | Diag(LLoc, diag::err_ovl_ambiguous_oper_binary) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8542 | << "[]" |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 8543 | << Args[0]->getType() << Args[1]->getType() |
| 8544 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8545 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, 2, |
| 8546 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8547 | return ExprError(); |
| 8548 | |
| 8549 | case OR_Deleted: |
| 8550 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 8551 | << Best->Function->isDeleted() << "[]" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 8552 | << getDeletedOrUnavailableSuffix(Best->Function) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8553 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8554 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 8555 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8556 | return ExprError(); |
| 8557 | } |
| 8558 | |
| 8559 | // We matched a built-in operator; build it. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8560 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8561 | } |
| 8562 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8563 | /// BuildCallToMemberFunction - Build a call to a member |
| 8564 | /// function. MemExpr is the expression that refers to the member |
| 8565 | /// function (and includes the object parameter), Args/NumArgs are the |
| 8566 | /// arguments to the function call (not including the object |
| 8567 | /// parameter). The caller needs to validate that the member |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8568 | /// expression refers to a non-static member function or an overloaded |
| 8569 | /// member function. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8570 | ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8571 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 8572 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 8573 | unsigned NumArgs, SourceLocation RParenLoc) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8574 | assert(MemExprE->getType() == Context.BoundMemberTy || |
| 8575 | MemExprE->getType() == Context.OverloadTy); |
| 8576 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8577 | // Dig out the member expression. This holds both the object |
| 8578 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8579 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8580 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8581 | // Determine whether this is a call to a pointer-to-member function. |
| 8582 | if (BinaryOperator *op = dyn_cast<BinaryOperator>(NakedMemExpr)) { |
| 8583 | assert(op->getType() == Context.BoundMemberTy); |
| 8584 | assert(op->getOpcode() == BO_PtrMemD || op->getOpcode() == BO_PtrMemI); |
| 8585 | |
| 8586 | QualType fnType = |
| 8587 | op->getRHS()->getType()->castAs<MemberPointerType>()->getPointeeType(); |
| 8588 | |
| 8589 | const FunctionProtoType *proto = fnType->castAs<FunctionProtoType>(); |
| 8590 | QualType resultType = proto->getCallResultType(Context); |
| 8591 | ExprValueKind valueKind = Expr::getValueKindForType(proto->getResultType()); |
| 8592 | |
| 8593 | // Check that the object type isn't more qualified than the |
| 8594 | // member function we're calling. |
| 8595 | Qualifiers funcQuals = Qualifiers::fromCVRMask(proto->getTypeQuals()); |
| 8596 | |
| 8597 | QualType objectType = op->getLHS()->getType(); |
| 8598 | if (op->getOpcode() == BO_PtrMemI) |
| 8599 | objectType = objectType->castAs<PointerType>()->getPointeeType(); |
| 8600 | Qualifiers objectQuals = objectType.getQualifiers(); |
| 8601 | |
| 8602 | Qualifiers difference = objectQuals - funcQuals; |
| 8603 | difference.removeObjCGCAttr(); |
| 8604 | difference.removeAddressSpace(); |
| 8605 | if (difference) { |
| 8606 | std::string qualsString = difference.getAsString(); |
| 8607 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
| 8608 | << fnType.getUnqualifiedType() |
| 8609 | << qualsString |
| 8610 | << (qualsString.find(' ') == std::string::npos ? 1 : 2); |
| 8611 | } |
| 8612 | |
| 8613 | CXXMemberCallExpr *call |
| 8614 | = new (Context) CXXMemberCallExpr(Context, MemExprE, Args, NumArgs, |
| 8615 | resultType, valueKind, RParenLoc); |
| 8616 | |
| 8617 | if (CheckCallReturnType(proto->getResultType(), |
| 8618 | op->getRHS()->getSourceRange().getBegin(), |
| 8619 | call, 0)) |
| 8620 | return ExprError(); |
| 8621 | |
| 8622 | if (ConvertArgumentsForCall(call, op, 0, proto, Args, NumArgs, RParenLoc)) |
| 8623 | return ExprError(); |
| 8624 | |
| 8625 | return MaybeBindToTemporary(call); |
| 8626 | } |
| 8627 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8628 | MemberExpr *MemExpr; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8629 | CXXMethodDecl *Method = 0; |
John McCall | bb6fb46 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 8630 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 8631 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8632 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 8633 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8634 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8635 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 8636 | Qualifier = MemExpr->getQualifier(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8637 | } else { |
| 8638 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 8639 | Qualifier = UnresExpr->getQualifier(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8640 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 8641 | QualType ObjectType = UnresExpr->getBaseType(); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 8642 | Expr::Classification ObjectClassification |
| 8643 | = UnresExpr->isArrow()? Expr::Classification::makeSimpleLValue() |
| 8644 | : UnresExpr->getBase()->Classify(Context); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8645 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8646 | // Add overload candidates |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8647 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8648 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8649 | // FIXME: avoid copy. |
| 8650 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 8651 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 8652 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 8653 | TemplateArgs = &TemplateArgsBuffer; |
| 8654 | } |
| 8655 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8656 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 8657 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 8658 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 8659 | NamedDecl *Func = *I; |
| 8660 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 8661 | if (isa<UsingShadowDecl>(Func)) |
| 8662 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 8663 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 8664 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 8665 | // Microsoft supports direct constructor calls. |
| 8666 | if (getLangOptions().Microsoft && isa<CXXConstructorDecl>(Func)) { |
| 8667 | AddOverloadCandidate(cast<CXXConstructorDecl>(Func), I.getPair(), Args, NumArgs, |
| 8668 | CandidateSet); |
| 8669 | } else if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 8670 | // If explicit template arguments were provided, we can't call a |
| 8671 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8672 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 8673 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8674 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8675 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8676 | ObjectClassification, |
| 8677 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 8678 | /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8679 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8680 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8681 | I.getPair(), ActingDC, TemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8682 | ObjectType, ObjectClassification, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 8683 | Args, NumArgs, CandidateSet, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 8684 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8685 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 8686 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8687 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8688 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 8689 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8690 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8691 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getLocStart(), |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 8692 | Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8693 | case OR_Success: |
| 8694 | Method = cast<CXXMethodDecl>(Best->Function); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8695 | MarkDeclarationReferenced(UnresExpr->getMemberLoc(), Method); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8696 | FoundDecl = Best->FoundDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8697 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8698 | DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8699 | break; |
| 8700 | |
| 8701 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8702 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8703 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 8704 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8705 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8706 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8707 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8708 | |
| 8709 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8710 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 8711 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8712 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8713 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8714 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8715 | |
| 8716 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8717 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8718 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 8719 | << DeclName |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 8720 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 8721 | << MemExprE->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8722 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8723 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8724 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8725 | } |
| 8726 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8727 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8728 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8729 | // If overload resolution picked a static member, build a |
| 8730 | // non-member call based on that function. |
| 8731 | if (Method->isStatic()) { |
| 8732 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 8733 | Args, NumArgs, RParenLoc); |
| 8734 | } |
| 8735 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8736 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8737 | } |
| 8738 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8739 | QualType ResultType = Method->getResultType(); |
| 8740 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 8741 | ResultType = ResultType.getNonLValueExprType(Context); |
| 8742 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8743 | assert(Method && "Member call to something that isn't a method?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8744 | CXXMemberCallExpr *TheCall = |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8745 | new (Context) CXXMemberCallExpr(Context, MemExprE, Args, NumArgs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8746 | ResultType, VK, RParenLoc); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8747 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 8748 | // Check for a valid return type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8749 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8750 | TheCall, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8751 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8752 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8753 | // Convert the object argument (for a non-static member function call). |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8754 | // We only need to do this if there was actually an overload; otherwise |
| 8755 | // it was done at lookup. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8756 | if (!Method->isStatic()) { |
| 8757 | ExprResult ObjectArg = |
| 8758 | PerformObjectArgumentInitialization(MemExpr->getBase(), Qualifier, |
| 8759 | FoundDecl, Method); |
| 8760 | if (ObjectArg.isInvalid()) |
| 8761 | return ExprError(); |
| 8762 | MemExpr->setBase(ObjectArg.take()); |
| 8763 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8764 | |
| 8765 | // Convert the rest of the arguments |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8766 | const FunctionProtoType *Proto = |
| 8767 | Method->getType()->getAs<FunctionProtoType>(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8768 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8769 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8770 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8771 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8772 | if (CheckFunctionCall(Method, TheCall)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8773 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 8774 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8775 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8776 | } |
| 8777 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8778 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 8779 | /// type (C++ [over.call.object]), which can end up invoking an |
| 8780 | /// overloaded function call operator (@c operator()) or performing a |
| 8781 | /// user-defined conversion on the object argument. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8782 | ExprResult |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8783 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 8784 | SourceLocation LParenLoc, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8785 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8786 | SourceLocation RParenLoc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8787 | ExprResult Object = Owned(Obj); |
| 8788 | if (Object.get()->getObjectKind() == OK_ObjCProperty) { |
| 8789 | Object = ConvertPropertyForRValue(Object.take()); |
| 8790 | if (Object.isInvalid()) |
| 8791 | return ExprError(); |
| 8792 | } |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8793 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8794 | assert(Object.get()->getType()->isRecordType() && "Requires object type argument"); |
| 8795 | const RecordType *Record = Object.get()->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8796 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8797 | // C++ [over.call.object]p1: |
| 8798 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 8799 | // evaluates to a class object of type "cv T", then the set of |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8800 | // candidate functions includes at least the function call |
| 8801 | // operators of T. The function call operators of T are obtained by |
| 8802 | // ordinary lookup of the name operator() in the context of |
| 8803 | // (E).operator(). |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8804 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 8805 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 8806 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8807 | if (RequireCompleteType(LParenLoc, Object.get()->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8808 | PDiag(diag::err_incomplete_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8809 | << Object.get()->getSourceRange())) |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 8810 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8811 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8812 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 8813 | LookupQualifiedName(R, Record->getDecl()); |
| 8814 | R.suppressDiagnostics(); |
| 8815 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 8816 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 8817 | Oper != OperEnd; ++Oper) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8818 | AddMethodCandidate(Oper.getPair(), Object.get()->getType(), |
| 8819 | Object.get()->Classify(Context), Args, NumArgs, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 8820 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 8821 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8822 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8823 | // C++ [over.call.object]p2: |
| 8824 | // In addition, for each conversion function declared in T of the |
| 8825 | // form |
| 8826 | // |
| 8827 | // operator conversion-type-id () cv-qualifier; |
| 8828 | // |
| 8829 | // where cv-qualifier is the same cv-qualification as, or a |
| 8830 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 8831 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 8832 | // R", or the type "reference to pointer to function of |
| 8833 | // (P1,...,Pn) returning R", or the type "reference to function |
| 8834 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8835 | // is also considered as a candidate function. Similarly, |
| 8836 | // surrogate call functions are added to the set of candidate |
| 8837 | // functions for each conversion function declared in an |
| 8838 | // accessible base class provided the function is not hidden |
| 8839 | // within T by another intervening declaration. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 8840 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 8841 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 8842 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8843 | E = Conversions->end(); I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 8844 | NamedDecl *D = *I; |
| 8845 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 8846 | if (isa<UsingShadowDecl>(D)) |
| 8847 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8848 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 8849 | // Skip over templated conversion functions; they aren't |
| 8850 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 8851 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 8852 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 8853 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 8854 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8855 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 8856 | // Strip the reference type (if any) and then the pointer type (if |
| 8857 | // any) to get down to what might be a function type. |
| 8858 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 8859 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 8860 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8861 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 8862 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8863 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8864 | Object.get(), Args, NumArgs, CandidateSet); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8865 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8866 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8867 | // Perform overload resolution. |
| 8868 | OverloadCandidateSet::iterator Best; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8869 | switch (CandidateSet.BestViableFunction(*this, Object.get()->getLocStart(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8870 | Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8871 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8872 | // Overload resolution succeeded; we'll build the appropriate call |
| 8873 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8874 | break; |
| 8875 | |
| 8876 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 8877 | if (CandidateSet.empty()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8878 | Diag(Object.get()->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 8879 | << Object.get()->getType() << /*call*/ 1 |
| 8880 | << Object.get()->getSourceRange(); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 8881 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8882 | Diag(Object.get()->getSourceRange().getBegin(), |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 8883 | diag::err_ovl_no_viable_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8884 | << Object.get()->getType() << Object.get()->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8885 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8886 | break; |
| 8887 | |
| 8888 | case OR_Ambiguous: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8889 | Diag(Object.get()->getSourceRange().getBegin(), |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8890 | diag::err_ovl_ambiguous_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8891 | << Object.get()->getType() << Object.get()->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8892 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8893 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8894 | |
| 8895 | case OR_Deleted: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8896 | Diag(Object.get()->getSourceRange().getBegin(), |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8897 | diag::err_ovl_deleted_object_call) |
| 8898 | << Best->Function->isDeleted() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8899 | << Object.get()->getType() |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 8900 | << getDeletedOrUnavailableSuffix(Best->Function) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8901 | << Object.get()->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8902 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8903 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8904 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8905 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 8906 | if (Best == CandidateSet.end()) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8907 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8908 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8909 | if (Best->Function == 0) { |
| 8910 | // Since there is no function declaration, this is one of the |
| 8911 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8912 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8913 | = cast<CXXConversionDecl>( |
| 8914 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 8915 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8916 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8917 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 8918 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8919 | // We selected one of the surrogate functions that converts the |
| 8920 | // object parameter to a function pointer. Perform the conversion |
| 8921 | // on the object argument, then let ActOnCallExpr finish the job. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8922 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 8923 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 8924 | // and then call it. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8925 | ExprResult Call = BuildCXXMemberCallExpr(Object.get(), Best->FoundDecl, Conv); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 8926 | if (Call.isInvalid()) |
| 8927 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8928 | |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 8929 | return ActOnCallExpr(S, Call.get(), LParenLoc, MultiExprArg(Args, NumArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 8930 | RParenLoc); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8931 | } |
| 8932 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8933 | MarkDeclarationReferenced(LParenLoc, Best->Function); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8934 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8935 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 8936 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8937 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 8938 | // that calls this method, using Object for the implicit object |
| 8939 | // parameter and passing along the remaining arguments. |
| 8940 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8941 | const FunctionProtoType *Proto = |
| 8942 | Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8943 | |
| 8944 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 8945 | unsigned NumArgsToCheck = NumArgs; |
| 8946 | |
| 8947 | // Build the full argument list for the method call (the |
| 8948 | // implicit object parameter is placed at the beginning of the |
| 8949 | // list). |
| 8950 | Expr **MethodArgs; |
| 8951 | if (NumArgs < NumArgsInProto) { |
| 8952 | NumArgsToCheck = NumArgsInProto; |
| 8953 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 8954 | } else { |
| 8955 | MethodArgs = new Expr*[NumArgs + 1]; |
| 8956 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8957 | MethodArgs[0] = Object.get(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8958 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 8959 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8960 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8961 | ExprResult NewFn = CreateFunctionRefExpr(*this, Method); |
| 8962 | if (NewFn.isInvalid()) |
| 8963 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8964 | |
| 8965 | // Once we've built TheCall, all of the expressions are properly |
| 8966 | // owned. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8967 | QualType ResultTy = Method->getResultType(); |
| 8968 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 8969 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 8970 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8971 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8972 | new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn.take(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8973 | MethodArgs, NumArgs + 1, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8974 | ResultTy, VK, RParenLoc); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8975 | delete [] MethodArgs; |
| 8976 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8977 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall, |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 8978 | Method)) |
| 8979 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8980 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 8981 | // We may have default arguments. If so, we need to allocate more |
| 8982 | // slots in the call for them. |
| 8983 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 8984 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 8985 | else if (NumArgs > NumArgsInProto) |
| 8986 | NumArgsToCheck = NumArgsInProto; |
| 8987 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 8988 | bool IsError = false; |
| 8989 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8990 | // Initialize the implicit object parameter. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8991 | ExprResult ObjRes = |
| 8992 | PerformObjectArgumentInitialization(Object.get(), /*Qualifier=*/0, |
| 8993 | Best->FoundDecl, Method); |
| 8994 | if (ObjRes.isInvalid()) |
| 8995 | IsError = true; |
| 8996 | else |
| 8997 | Object = move(ObjRes); |
| 8998 | TheCall->setArg(0, Object.take()); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 8999 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9000 | // Check the argument types. |
| 9001 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9002 | Expr *Arg; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 9003 | if (i < NumArgs) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9004 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9005 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 9006 | // Pass the argument. |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 9007 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9008 | ExprResult InputInit |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 9009 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 9010 | Context, |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 9011 | Method->getParamDecl(i)), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9012 | SourceLocation(), Arg); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9013 | |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 9014 | IsError |= InputInit.isInvalid(); |
| 9015 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 9016 | } else { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9017 | ExprResult DefArg |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 9018 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 9019 | if (DefArg.isInvalid()) { |
| 9020 | IsError = true; |
| 9021 | break; |
| 9022 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9023 | |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 9024 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 9025 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9026 | |
| 9027 | TheCall->setArg(i + 1, Arg); |
| 9028 | } |
| 9029 | |
| 9030 | // If this is a variadic call, handle args passed through "...". |
| 9031 | if (Proto->isVariadic()) { |
| 9032 | // Promote the arguments (C99 6.5.2.2p7). |
| 9033 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9034 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0); |
| 9035 | IsError |= Arg.isInvalid(); |
| 9036 | TheCall->setArg(i + 1, Arg.take()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9037 | } |
| 9038 | } |
| 9039 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 9040 | if (IsError) return true; |
| 9041 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9042 | if (CheckFunctionCall(Method, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 9043 | return true; |
| 9044 | |
John McCall | 182f709 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 9045 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9046 | } |
| 9047 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9048 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9049 | /// (if one exists), where @c Base is an expression of class type and |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9050 | /// @c Member is the name of the member we're trying to find. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9051 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9052 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc) { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 9053 | assert(Base->getType()->isRecordType() && |
| 9054 | "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9055 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9056 | if (Base->getObjectKind() == OK_ObjCProperty) { |
| 9057 | ExprResult Result = ConvertPropertyForRValue(Base); |
| 9058 | if (Result.isInvalid()) |
| 9059 | return ExprError(); |
| 9060 | Base = Result.take(); |
| 9061 | } |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 9062 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9063 | SourceLocation Loc = Base->getExprLoc(); |
| 9064 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9065 | // C++ [over.ref]p1: |
| 9066 | // |
| 9067 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 9068 | // for a class object x of type T if T::operator->() exists and if |
| 9069 | // the operator is selected as the best match function by the |
| 9070 | // overload resolution mechanism (13.3). |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 9071 | DeclarationName OpName = |
| 9072 | Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9073 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 9074 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 9075 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9076 | if (RequireCompleteType(Loc, Base->getType(), |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 9077 | PDiag(diag::err_typecheck_incomplete_tag) |
| 9078 | << Base->getSourceRange())) |
| 9079 | return ExprError(); |
| 9080 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9081 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 9082 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 9083 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 9084 | |
| 9085 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 9086 | Oper != OperEnd; ++Oper) { |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 9087 | AddMethodCandidate(Oper.getPair(), Base->getType(), Base->Classify(Context), |
| 9088 | 0, 0, CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 9089 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9090 | |
| 9091 | // Perform overload resolution. |
| 9092 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9093 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9094 | case OR_Success: |
| 9095 | // Overload resolution succeeded; we'll build the call below. |
| 9096 | break; |
| 9097 | |
| 9098 | case OR_No_Viable_Function: |
| 9099 | if (CandidateSet.empty()) |
| 9100 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 9101 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9102 | else |
| 9103 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 9104 | << "operator->" << Base->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9105 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 9106 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9107 | |
| 9108 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 9109 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 9110 | << "->" << Base->getType() << Base->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9111 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 9112 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9113 | |
| 9114 | case OR_Deleted: |
| 9115 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 9116 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 9117 | << "->" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 9118 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 9119 | << Base->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9120 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 9121 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9122 | } |
| 9123 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 9124 | MarkDeclarationReferenced(OpLoc, Best->Function); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9125 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 9126 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9127 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9128 | // Convert the object parameter. |
| 9129 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9130 | ExprResult BaseResult = |
| 9131 | PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 9132 | Best->FoundDecl, Method); |
| 9133 | if (BaseResult.isInvalid()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 9134 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9135 | Base = BaseResult.take(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 9136 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9137 | // Build the operator call. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9138 | ExprResult FnExpr = CreateFunctionRefExpr(*this, Method); |
| 9139 | if (FnExpr.isInvalid()) |
| 9140 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9141 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9142 | QualType ResultTy = Method->getResultType(); |
| 9143 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 9144 | ResultTy = ResultTy.getNonLValueExprType(Context); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9145 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9146 | new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr.take(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9147 | &Base, 1, ResultTy, VK, OpLoc); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 9148 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9149 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 9150 | Method)) |
| 9151 | return ExprError(); |
Eli Friedman | d593190 | 2011-04-04 01:18:25 +0000 | [diff] [blame] | 9152 | |
| 9153 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9154 | } |
| 9155 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9156 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 9157 | /// a C++ overloaded function (possibly with some parentheses and |
| 9158 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 9159 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 9160 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 9161 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9162 | FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9163 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9164 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 9165 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9166 | if (SubExpr == PE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9167 | return PE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9168 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9169 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9170 | } |
| 9171 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9172 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9173 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 9174 | Found, Fn); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9175 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9176 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 9177 | "Implicit cast type cannot be determined from overload"); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 9178 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9179 | if (SubExpr == ICE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9180 | return ICE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9181 | |
| 9182 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 9183 | ICE->getCastKind(), |
| 9184 | SubExpr, 0, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 9185 | ICE->getValueKind()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9186 | } |
| 9187 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9188 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9189 | assert(UnOp->getOpcode() == UO_AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9190 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 9191 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 9192 | if (Method->isStatic()) { |
| 9193 | // Do nothing: static member functions aren't any different |
| 9194 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9195 | } else { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 9196 | // Fix the sub expression, which really has to be an |
| 9197 | // UnresolvedLookupExpr holding an overloaded member function |
| 9198 | // or template. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9199 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 9200 | Found, Fn); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9201 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9202 | return UnOp; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9203 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9204 | assert(isa<DeclRefExpr>(SubExpr) |
| 9205 | && "fixed to something other than a decl ref"); |
| 9206 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 9207 | && "fixed to a member ref with no nested name qualifier"); |
| 9208 | |
| 9209 | // We have taken the address of a pointer to member |
| 9210 | // function. Perform the computation here so that we get the |
| 9211 | // appropriate pointer to member type. |
| 9212 | QualType ClassType |
| 9213 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 9214 | QualType MemPtrType |
| 9215 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 9216 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9217 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, MemPtrType, |
| 9218 | VK_RValue, OK_Ordinary, |
| 9219 | UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 9220 | } |
| 9221 | } |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9222 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 9223 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9224 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9225 | return UnOp; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9226 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9227 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9228 | Context.getPointerType(SubExpr->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9229 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9230 | UnOp->getOperatorLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9231 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9232 | |
| 9233 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9234 | // FIXME: avoid copy. |
| 9235 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 9236 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9237 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 9238 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 9239 | } |
| 9240 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9241 | return DeclRefExpr::Create(Context, |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 9242 | ULE->getQualifierLoc(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9243 | Fn, |
| 9244 | ULE->getNameLoc(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9245 | Fn->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9246 | VK_LValue, |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9247 | TemplateArgs); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9248 | } |
| 9249 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9250 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 9251 | // FIXME: avoid copy. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9252 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 9253 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 9254 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 9255 | TemplateArgs = &TemplateArgsBuffer; |
| 9256 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 9257 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9258 | Expr *Base; |
| 9259 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9260 | // If we're filling in a static method where we used to have an |
| 9261 | // implicit member access, rewrite to a simple decl ref. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9262 | if (MemExpr->isImplicitAccess()) { |
| 9263 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 9264 | return DeclRefExpr::Create(Context, |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 9265 | MemExpr->getQualifierLoc(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9266 | Fn, |
| 9267 | MemExpr->getMemberLoc(), |
| 9268 | Fn->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9269 | VK_LValue, |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9270 | TemplateArgs); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 9271 | } else { |
| 9272 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 9273 | if (MemExpr->getQualifier()) |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 9274 | Loc = MemExpr->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 9275 | Base = new (Context) CXXThisExpr(Loc, |
| 9276 | MemExpr->getBaseType(), |
| 9277 | /*isImplicit=*/true); |
| 9278 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9279 | } else |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9280 | Base = MemExpr->getBase(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9281 | |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9282 | ExprValueKind valueKind; |
| 9283 | QualType type; |
| 9284 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 9285 | valueKind = VK_LValue; |
| 9286 | type = Fn->getType(); |
| 9287 | } else { |
| 9288 | valueKind = VK_RValue; |
| 9289 | type = Context.BoundMemberTy; |
| 9290 | } |
| 9291 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9292 | return MemberExpr::Create(Context, Base, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9293 | MemExpr->isArrow(), |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 9294 | MemExpr->getQualifierLoc(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9295 | Fn, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9296 | Found, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9297 | MemExpr->getMemberNameInfo(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9298 | TemplateArgs, |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9299 | type, valueKind, OK_Ordinary); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9300 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9301 | |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9302 | llvm_unreachable("Invalid reference to overloaded function"); |
| 9303 | return E; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9304 | } |
| 9305 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9306 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9307 | DeclAccessPair Found, |
| 9308 | FunctionDecl *Fn) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9309 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 9310 | } |
| 9311 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9312 | } // end namespace clang |