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 | |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 1631 | // MSVC allows implicit function to void* type conversion. |
| 1632 | if (getLangOptions().Microsoft && FromPointeeType->isFunctionType() && |
| 1633 | ToPointeeType->isVoidType()) { |
| 1634 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 1635 | ToPointeeType, |
| 1636 | ToType, Context); |
| 1637 | return true; |
| 1638 | } |
| 1639 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1640 | // When we're overloading in C, we allow a special kind of pointer |
| 1641 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1642 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1643 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1644 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1645 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1646 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1647 | return true; |
| 1648 | } |
| 1649 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1650 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1651 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1652 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1653 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1654 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1655 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1656 | // necessitates this conversion is ill-formed. The result of the |
| 1657 | // conversion is a pointer to the base class sub-object of the |
| 1658 | // derived class object. The null pointer value is converted to |
| 1659 | // the null pointer value of the destination type. |
| 1660 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1661 | // Note that we do not check for ambiguity or inaccessibility |
| 1662 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1663 | if (getLangOptions().CPlusPlus && |
| 1664 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | bf1764c | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 1665 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | 2685eab | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1666 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1667 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1668 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1669 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1670 | ToType, Context); |
| 1671 | return true; |
| 1672 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1673 | |
Fariborz Jahanian | 5da3c08 | 2011-04-14 20:33:36 +0000 | [diff] [blame] | 1674 | if (FromPointeeType->isVectorType() && ToPointeeType->isVectorType() && |
| 1675 | Context.areCompatibleVectorTypes(FromPointeeType, ToPointeeType)) { |
| 1676 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 1677 | ToPointeeType, |
| 1678 | ToType, Context); |
| 1679 | return true; |
| 1680 | } |
| 1681 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1682 | return false; |
| 1683 | } |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1684 | |
| 1685 | /// \brief Adopt the given qualifiers for the given type. |
| 1686 | static QualType AdoptQualifiers(ASTContext &Context, QualType T, Qualifiers Qs){ |
| 1687 | Qualifiers TQs = T.getQualifiers(); |
| 1688 | |
| 1689 | // Check whether qualifiers already match. |
| 1690 | if (TQs == Qs) |
| 1691 | return T; |
| 1692 | |
| 1693 | if (Qs.compatiblyIncludes(TQs)) |
| 1694 | return Context.getQualifiedType(T, Qs); |
| 1695 | |
| 1696 | return Context.getQualifiedType(T.getUnqualifiedType(), Qs); |
| 1697 | } |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1698 | |
| 1699 | /// isObjCPointerConversion - Determines whether this is an |
| 1700 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1701 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1702 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1703 | QualType& ConvertedType, |
| 1704 | bool &IncompatibleObjC) { |
| 1705 | if (!getLangOptions().ObjC1) |
| 1706 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1707 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1708 | // The set of qualifiers on the type we're converting from. |
| 1709 | Qualifiers FromQualifiers = FromType.getQualifiers(); |
| 1710 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1711 | // First, we handle all conversions on ObjC object pointer types. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1712 | const ObjCObjectPointerType* ToObjCPtr = |
| 1713 | ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1714 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1715 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1716 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1717 | if (ToObjCPtr && FromObjCPtr) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1718 | // If the pointee types are the same (ignoring qualifications), |
| 1719 | // then this is not a pointer conversion. |
| 1720 | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
| 1721 | FromObjCPtr->getPointeeType())) |
| 1722 | return false; |
| 1723 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1724 | // Check for compatible |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1725 | // 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] | 1726 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1727 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1728 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1729 | return true; |
| 1730 | } |
| 1731 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1732 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1733 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1734 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1735 | /*compare=*/false)) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1736 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1737 | return true; |
| 1738 | } |
| 1739 | // Objective C++: We're able to convert from a pointer to an |
| 1740 | // interface to a pointer to a different interface. |
| 1741 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 1742 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 1743 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
| 1744 | if (getLangOptions().CPlusPlus && LHS && RHS && |
| 1745 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 1746 | FromObjCPtr->getPointeeType())) |
| 1747 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1748 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1749 | ToObjCPtr->getPointeeType(), |
| 1750 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1751 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1752 | return true; |
| 1753 | } |
| 1754 | |
| 1755 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1756 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1757 | // interfaces, which is permitted. However, we're going to |
| 1758 | // complain about it. |
| 1759 | IncompatibleObjC = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1760 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1761 | ToObjCPtr->getPointeeType(), |
| 1762 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1763 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1764 | return true; |
| 1765 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1766 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1767 | // 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] | 1768 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1769 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1770 | ToPointeeType = ToCPtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1771 | else if (const BlockPointerType *ToBlockPtr = |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1772 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 1773 | // 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] | 1774 | // to a block pointer type. |
| 1775 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1776 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1777 | return true; |
| 1778 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1779 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1780 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1781 | else if (FromType->getAs<BlockPointerType>() && |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1782 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1783 | // 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] | 1784 | // pointer to any object. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1785 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1786 | return true; |
| 1787 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1788 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1789 | return false; |
| 1790 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1791 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1792 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1793 | FromPointeeType = FromCPtr->getPointeeType(); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1794 | else if (const BlockPointerType *FromBlockPtr = |
| 1795 | FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1796 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1797 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1798 | return false; |
| 1799 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1800 | // If we have pointers to pointers, recursively check whether this |
| 1801 | // is an Objective-C conversion. |
| 1802 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1803 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1804 | IncompatibleObjC)) { |
| 1805 | // We always complain about this conversion. |
| 1806 | IncompatibleObjC = true; |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1807 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1808 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1809 | return true; |
| 1810 | } |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1811 | // Allow conversion of pointee being objective-c pointer to another one; |
| 1812 | // as in I* to id. |
| 1813 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 1814 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 1815 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1816 | IncompatibleObjC)) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1817 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1818 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1819 | return true; |
| 1820 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1821 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1822 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1823 | // differences in the argument and result types are in Objective-C |
| 1824 | // pointer conversions. If so, we permit the conversion (but |
| 1825 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1826 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1827 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1828 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1829 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1830 | if (FromFunctionType && ToFunctionType) { |
| 1831 | // If the function types are exactly the same, this isn't an |
| 1832 | // Objective-C pointer conversion. |
| 1833 | if (Context.getCanonicalType(FromPointeeType) |
| 1834 | == Context.getCanonicalType(ToPointeeType)) |
| 1835 | return false; |
| 1836 | |
| 1837 | // Perform the quick checks that will tell us whether these |
| 1838 | // function types are obviously different. |
| 1839 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1840 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1841 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1842 | return false; |
| 1843 | |
| 1844 | bool HasObjCConversion = false; |
| 1845 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1846 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1847 | // Okay, the types match exactly. Nothing to do. |
| 1848 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1849 | ToFunctionType->getResultType(), |
| 1850 | ConvertedType, IncompatibleObjC)) { |
| 1851 | // Okay, we have an Objective-C pointer conversion. |
| 1852 | HasObjCConversion = true; |
| 1853 | } else { |
| 1854 | // Function types are too different. Abort. |
| 1855 | return false; |
| 1856 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1857 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1858 | // Check argument types. |
| 1859 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1860 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1861 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1862 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1863 | if (Context.getCanonicalType(FromArgType) |
| 1864 | == Context.getCanonicalType(ToArgType)) { |
| 1865 | // Okay, the types match exactly. Nothing to do. |
| 1866 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1867 | ConvertedType, IncompatibleObjC)) { |
| 1868 | // Okay, we have an Objective-C pointer conversion. |
| 1869 | HasObjCConversion = true; |
| 1870 | } else { |
| 1871 | // Argument types are too different. Abort. |
| 1872 | return false; |
| 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | if (HasObjCConversion) { |
| 1877 | // We had an Objective-C conversion. Allow this pointer |
| 1878 | // conversion, but complain about it. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1879 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1880 | IncompatibleObjC = true; |
| 1881 | return true; |
| 1882 | } |
| 1883 | } |
| 1884 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1885 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1886 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1887 | |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1888 | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
| 1889 | QualType& ConvertedType) { |
| 1890 | QualType ToPointeeType; |
| 1891 | if (const BlockPointerType *ToBlockPtr = |
| 1892 | ToType->getAs<BlockPointerType>()) |
| 1893 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 1894 | else |
| 1895 | return false; |
| 1896 | |
| 1897 | QualType FromPointeeType; |
| 1898 | if (const BlockPointerType *FromBlockPtr = |
| 1899 | FromType->getAs<BlockPointerType>()) |
| 1900 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1901 | else |
| 1902 | return false; |
| 1903 | // We have pointer to blocks, check whether the only |
| 1904 | // differences in the argument and result types are in Objective-C |
| 1905 | // pointer conversions. If so, we permit the conversion. |
| 1906 | |
| 1907 | const FunctionProtoType *FromFunctionType |
| 1908 | = FromPointeeType->getAs<FunctionProtoType>(); |
| 1909 | const FunctionProtoType *ToFunctionType |
| 1910 | = ToPointeeType->getAs<FunctionProtoType>(); |
| 1911 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 1912 | if (!FromFunctionType || !ToFunctionType) |
| 1913 | return false; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1914 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 1915 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1916 | return true; |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 1917 | |
| 1918 | // Perform the quick checks that will tell us whether these |
| 1919 | // function types are obviously different. |
| 1920 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1921 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
| 1922 | return false; |
| 1923 | |
| 1924 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
| 1925 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
| 1926 | if (FromEInfo != ToEInfo) |
| 1927 | return false; |
| 1928 | |
| 1929 | bool IncompatibleObjC = false; |
Fariborz Jahanian | 462dae5 | 2011-02-13 20:11:42 +0000 | [diff] [blame] | 1930 | if (Context.hasSameType(FromFunctionType->getResultType(), |
| 1931 | ToFunctionType->getResultType())) { |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 1932 | // Okay, the types match exactly. Nothing to do. |
| 1933 | } else { |
| 1934 | QualType RHS = FromFunctionType->getResultType(); |
| 1935 | QualType LHS = ToFunctionType->getResultType(); |
| 1936 | if ((!getLangOptions().CPlusPlus || !RHS->isRecordType()) && |
| 1937 | !RHS.hasQualifiers() && LHS.hasQualifiers()) |
| 1938 | LHS = LHS.getUnqualifiedType(); |
| 1939 | |
| 1940 | if (Context.hasSameType(RHS,LHS)) { |
| 1941 | // OK exact match. |
| 1942 | } else if (isObjCPointerConversion(RHS, LHS, |
| 1943 | ConvertedType, IncompatibleObjC)) { |
| 1944 | if (IncompatibleObjC) |
| 1945 | return false; |
| 1946 | // Okay, we have an Objective-C pointer conversion. |
| 1947 | } |
| 1948 | else |
| 1949 | return false; |
| 1950 | } |
| 1951 | |
| 1952 | // Check argument types. |
| 1953 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1954 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1955 | IncompatibleObjC = false; |
| 1956 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1957 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1958 | if (Context.hasSameType(FromArgType, ToArgType)) { |
| 1959 | // Okay, the types match exactly. Nothing to do. |
| 1960 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
| 1961 | ConvertedType, IncompatibleObjC)) { |
| 1962 | if (IncompatibleObjC) |
| 1963 | return false; |
| 1964 | // Okay, we have an Objective-C pointer conversion. |
| 1965 | } else |
| 1966 | // Argument types are too different. Abort. |
| 1967 | return false; |
| 1968 | } |
| 1969 | ConvertedType = ToType; |
| 1970 | return true; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1973 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
| 1974 | /// for equlity of their argument types. Caller has already checked that |
| 1975 | /// they have same number of arguments. This routine assumes that Objective-C |
| 1976 | /// pointer types which only differ in their protocol qualifiers are equal. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1977 | bool Sema::FunctionArgTypesAreEqual(const FunctionProtoType *OldType, |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1978 | const FunctionProtoType *NewType) { |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1979 | if (!getLangOptions().ObjC1) |
| 1980 | return std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 1981 | NewType->arg_type_begin()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1982 | |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1983 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 1984 | N = NewType->arg_type_begin(), |
| 1985 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
| 1986 | QualType ToType = (*O); |
| 1987 | QualType FromType = (*N); |
| 1988 | if (ToType != FromType) { |
| 1989 | if (const PointerType *PTTo = ToType->getAs<PointerType>()) { |
| 1990 | if (const PointerType *PTFr = FromType->getAs<PointerType>()) |
Chandler Carruth | 0ee93de | 2010-05-06 00:15:06 +0000 | [diff] [blame] | 1991 | if ((PTTo->getPointeeType()->isObjCQualifiedIdType() && |
| 1992 | PTFr->getPointeeType()->isObjCQualifiedIdType()) || |
| 1993 | (PTTo->getPointeeType()->isObjCQualifiedClassType() && |
| 1994 | PTFr->getPointeeType()->isObjCQualifiedClassType())) |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1995 | continue; |
| 1996 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1997 | else if (const ObjCObjectPointerType *PTTo = |
| 1998 | ToType->getAs<ObjCObjectPointerType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1999 | if (const ObjCObjectPointerType *PTFr = |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2000 | FromType->getAs<ObjCObjectPointerType>()) |
| 2001 | if (PTTo->getInterfaceDecl() == PTFr->getInterfaceDecl()) |
| 2002 | continue; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2003 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2004 | return false; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2005 | } |
| 2006 | } |
| 2007 | return true; |
| 2008 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2009 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2010 | /// CheckPointerConversion - Check the pointer conversion from the |
| 2011 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2012 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2013 | /// conversions for which IsPointerConversion has already returned |
| 2014 | /// true. It returns true and produces a diagnostic if there was an |
| 2015 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2016 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2017 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2018 | CXXCastPath& BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2019 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2020 | QualType FromType = From->getType(); |
Argyrios Kyrtzidis | b335872 | 2010-09-28 14:54:11 +0000 | [diff] [blame] | 2021 | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2022 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2023 | Kind = CK_BitCast; |
| 2024 | |
Chandler Carruth | 88f0aed | 2011-04-09 07:32:05 +0000 | [diff] [blame] | 2025 | if (!IsCStyleOrFunctionalCast && |
| 2026 | Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy) && |
| 2027 | From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 2028 | DiagRuntimeBehavior(From->getExprLoc(), From, |
Chandler Carruth | b600669 | 2011-04-09 07:48:17 +0000 | [diff] [blame] | 2029 | PDiag(diag::warn_impcast_bool_to_null_pointer) |
| 2030 | << ToType << From->getSourceRange()); |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 2031 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2032 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 2033 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2034 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 2035 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 2036 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2037 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 2038 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2039 | // We must have a derived-to-base conversion. Check an |
| 2040 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2041 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 2042 | From->getExprLoc(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2043 | From->getSourceRange(), &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2044 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2045 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2046 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2047 | // The conversion was successful. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2048 | Kind = CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2049 | } |
| 2050 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2051 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2052 | FromType->getAs<ObjCObjectPointerType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2053 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2054 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2055 | // Objective-C++ conversions are always okay. |
| 2056 | // FIXME: We should have a different class of conversions for the |
| 2057 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2058 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2059 | return false; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2060 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2061 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2062 | |
| 2063 | // We shouldn't fall into this case unless it's valid for other |
| 2064 | // reasons. |
| 2065 | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 2066 | Kind = CK_NullToPointer; |
| 2067 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2068 | return false; |
| 2069 | } |
| 2070 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2071 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 2072 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 2073 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 2074 | /// If so, returns true and places the converted type (that might differ from |
| 2075 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 2076 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2077 | QualType ToType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2078 | bool InOverloadResolution, |
| 2079 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2080 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2081 | if (!ToTypePtr) |
| 2082 | return false; |
| 2083 | |
| 2084 | // 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] | 2085 | if (From->isNullPointerConstant(Context, |
| 2086 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 2087 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2088 | ConvertedType = ToType; |
| 2089 | return true; |
| 2090 | } |
| 2091 | |
| 2092 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2093 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2094 | if (!FromTypePtr) |
| 2095 | return false; |
| 2096 | |
| 2097 | // A pointer to member of B can be converted to a pointer to member of D, |
| 2098 | // where D is derived from B (C++ 4.11p2). |
| 2099 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 2100 | QualType ToClass(ToTypePtr->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2101 | |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2102 | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
| 2103 | !RequireCompleteType(From->getLocStart(), ToClass, PDiag()) && |
| 2104 | IsDerivedFrom(ToClass, FromClass)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2105 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 2106 | ToClass.getTypePtr()); |
| 2107 | return true; |
| 2108 | } |
| 2109 | |
| 2110 | return false; |
| 2111 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2112 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2113 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 2114 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2115 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2116 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 2117 | /// true and produces a diagnostic if there was an error, or returns false |
| 2118 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2119 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2120 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2121 | CXXCastPath &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2122 | bool IgnoreBaseAccess) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2123 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2124 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2125 | if (!FromPtrType) { |
| 2126 | // This must be a null pointer to member pointer conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2127 | assert(From->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2128 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2129 | "Expr must be null pointer constant!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2130 | Kind = CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2131 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2132 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2133 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2134 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2135 | assert(ToPtrType && "No member pointer cast has a target type " |
| 2136 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2137 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2138 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 2139 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2140 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2141 | // FIXME: What about dependent types? |
| 2142 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 2143 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2144 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2145 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 2146 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2147 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 2148 | assert(DerivationOkay && |
| 2149 | "Should not have been called if derivation isn't OK."); |
| 2150 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2151 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2152 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 2153 | getUnqualifiedType())) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2154 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 2155 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 2156 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 2157 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2158 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2159 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2160 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2161 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 2162 | << FromClass << ToClass << QualType(VBase, 0) |
| 2163 | << From->getSourceRange(); |
| 2164 | return true; |
| 2165 | } |
| 2166 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2167 | if (!IgnoreBaseAccess) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2168 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 2169 | Paths.front(), |
| 2170 | diag::err_downcast_from_inaccessible_base); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2171 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2172 | // Must be a base to derived member conversion. |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2173 | BuildBasePathArray(Paths, BasePath); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2174 | Kind = CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2175 | return false; |
| 2176 | } |
| 2177 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2178 | /// IsQualificationConversion - Determines whether the conversion from |
| 2179 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 2180 | /// (C++ 4.4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2181 | bool |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2182 | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2183 | bool CStyle) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2184 | FromType = Context.getCanonicalType(FromType); |
| 2185 | ToType = Context.getCanonicalType(ToType); |
| 2186 | |
| 2187 | // If FromType and ToType are the same type, this is not a |
| 2188 | // qualification conversion. |
Sebastian Redl | 22c9240 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 2189 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2190 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2191 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2192 | // (C++ 4.4p4): |
| 2193 | // A conversion can add cv-qualifiers at levels other than the first |
| 2194 | // in multi-level pointers, subject to the following rules: [...] |
| 2195 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2196 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2197 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2198 | // Within each iteration of the loop, we check the qualifiers to |
| 2199 | // determine if this still looks like a qualification |
| 2200 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2201 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2202 | // until there are no more pointers or pointers-to-members left to |
| 2203 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2204 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2205 | |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2206 | Qualifiers FromQuals = FromType.getQualifiers(); |
| 2207 | Qualifiers ToQuals = ToType.getQualifiers(); |
| 2208 | |
Douglas Gregor | 377e1bd | 2011-05-08 06:09:53 +0000 | [diff] [blame] | 2209 | // Allow addition/removal of GC attributes but not changing GC attributes. |
| 2210 | if (FromQuals.getObjCGCAttr() != ToQuals.getObjCGCAttr() && |
| 2211 | (!FromQuals.hasObjCGCAttr() || !ToQuals.hasObjCGCAttr())) { |
| 2212 | FromQuals.removeObjCGCAttr(); |
| 2213 | ToQuals.removeObjCGCAttr(); |
| 2214 | } |
| 2215 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2216 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 2217 | // 2,j, and similarly for volatile. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2218 | if (!CStyle && !ToQuals.compatiblyIncludes(FromQuals)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2219 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2220 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2221 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 2222 | // every cv for 0 < k < j. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2223 | if (!CStyle && FromQuals.getCVRQualifiers() != ToQuals.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2224 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2225 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2226 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2227 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 2228 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2229 | PreviousToQualsIncludeConst |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2230 | = PreviousToQualsIncludeConst && ToQuals.hasConst(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2231 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2232 | |
| 2233 | // We are left with FromType and ToType being the pointee types |
| 2234 | // after unwrapping the original FromType and ToType the same number |
| 2235 | // of types. If we unwrapped any pointers, and if FromType and |
| 2236 | // ToType have the same unqualified type (since we checked |
| 2237 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2238 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2241 | /// Determines whether there is a user-defined conversion sequence |
| 2242 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 2243 | /// ToType. If such a conversion exists, User will contain the |
| 2244 | /// user-defined conversion sequence that performs such a conversion |
| 2245 | /// and this routine will return true. Otherwise, this routine returns |
| 2246 | /// false and User is unspecified. |
| 2247 | /// |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2248 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 2249 | /// "explicit" conversion functions as well as non-explicit conversion |
| 2250 | /// functions (C++0x [class.conv.fct]p2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2251 | static OverloadingResult |
| 2252 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 2253 | UserDefinedConversionSequence& User, |
| 2254 | OverloadCandidateSet& CandidateSet, |
| 2255 | bool AllowExplicit) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2256 | // Whether we will only visit constructors. |
| 2257 | bool ConstructorsOnly = false; |
| 2258 | |
| 2259 | // If the type we are conversion to is a class type, enumerate its |
| 2260 | // constructors. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2261 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2262 | // C++ [over.match.ctor]p1: |
| 2263 | // When objects of class type are direct-initialized (8.5), or |
| 2264 | // copy-initialized from an expression of the same or a |
| 2265 | // derived class type (8.5), overload resolution selects the |
| 2266 | // constructor. [...] For copy-initialization, the candidate |
| 2267 | // functions are all the converting constructors (12.3.1) of |
| 2268 | // that class. The argument list is the expression-list within |
| 2269 | // the parentheses of the initializer. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2270 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2271 | (From->getType()->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2272 | S.IsDerivedFrom(From->getType(), ToType))) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2273 | ConstructorsOnly = true; |
| 2274 | |
Argyrios Kyrtzidis | e36bca6 | 2011-04-22 17:45:37 +0000 | [diff] [blame] | 2275 | S.RequireCompleteType(From->getLocStart(), ToType, S.PDiag()); |
| 2276 | // RequireCompleteType may have returned true due to some invalid decl |
| 2277 | // during template instantiation, but ToType may be complete enough now |
| 2278 | // to try to recover. |
| 2279 | if (ToType->isIncompleteType()) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 2280 | // We're not going to find any constructors. |
| 2281 | } else if (CXXRecordDecl *ToRecordDecl |
| 2282 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2283 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2284 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(ToRecordDecl); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2285 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2286 | NamedDecl *D = *Con; |
| 2287 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2288 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2289 | // Find the constructor (which may be a template). |
| 2290 | CXXConstructorDecl *Constructor = 0; |
| 2291 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2292 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2293 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2294 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2295 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 2296 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2297 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2298 | |
Fariborz Jahanian | 52ab92b | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 2299 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 2300 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2301 | if (ConstructorTmpl) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2302 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2303 | /*ExplicitArgs*/ 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2304 | &From, 1, CandidateSet, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2305 | /*SuppressUserConversions=*/ |
| 2306 | !ConstructorsOnly); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2307 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 2308 | // Allow one user-defined conversion when user specifies a |
| 2309 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2310 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 2311 | &From, 1, CandidateSet, |
| 2312 | /*SuppressUserConversions=*/ |
| 2313 | !ConstructorsOnly); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2314 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2315 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2316 | } |
| 2317 | } |
| 2318 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2319 | // Enumerate conversion functions, if we're allowed to. |
| 2320 | if (ConstructorsOnly) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2321 | } else if (S.RequireCompleteType(From->getLocStart(), From->getType(), |
| 2322 | S.PDiag(0) << From->getSourceRange())) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2323 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2324 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2325 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2326 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2327 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 2328 | // Add all of the conversion functions as candidates. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2329 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 2330 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2331 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2332 | E = Conversions->end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2333 | DeclAccessPair FoundDecl = I.getPair(); |
| 2334 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2335 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2336 | if (isa<UsingShadowDecl>(D)) |
| 2337 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2338 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2339 | CXXConversionDecl *Conv; |
| 2340 | FunctionTemplateDecl *ConvTemplate; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2341 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 2342 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2343 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2344 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2345 | |
| 2346 | if (AllowExplicit || !Conv->isExplicit()) { |
| 2347 | if (ConvTemplate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2348 | S.AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
| 2349 | ActingContext, From, ToType, |
| 2350 | CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2351 | else |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2352 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, |
| 2353 | From, ToType, CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2354 | } |
| 2355 | } |
| 2356 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2357 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2358 | |
| 2359 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 2360 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2361 | case OR_Success: |
| 2362 | // Record the standard conversion we used and the conversion function. |
| 2363 | if (CXXConstructorDecl *Constructor |
| 2364 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2365 | S.MarkDeclarationReferenced(From->getLocStart(), Constructor); |
| 2366 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2367 | // C++ [over.ics.user]p1: |
| 2368 | // If the user-defined conversion is specified by a |
| 2369 | // constructor (12.3.1), the initial standard conversion |
| 2370 | // sequence converts the source type to the type required by |
| 2371 | // the argument of the constructor. |
| 2372 | // |
| 2373 | QualType ThisType = Constructor->getThisType(S.Context); |
| 2374 | if (Best->Conversions[0].isEllipsis()) |
| 2375 | User.EllipsisConversion = true; |
| 2376 | else { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2377 | User.Before = Best->Conversions[0].Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2378 | User.EllipsisConversion = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2379 | } |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2380 | User.ConversionFunction = Constructor; |
Douglas Gregor | 83eecbe | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 2381 | User.FoundConversionFunction = Best->FoundDecl.getDecl(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2382 | User.After.setAsIdentityConversion(); |
| 2383 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 2384 | User.After.setAllToTypes(ToType); |
| 2385 | return OR_Success; |
| 2386 | } else if (CXXConversionDecl *Conversion |
| 2387 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2388 | S.MarkDeclarationReferenced(From->getLocStart(), Conversion); |
| 2389 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2390 | // C++ [over.ics.user]p1: |
| 2391 | // |
| 2392 | // [...] If the user-defined conversion is specified by a |
| 2393 | // conversion function (12.3.2), the initial standard |
| 2394 | // conversion sequence converts the source type to the |
| 2395 | // implicit object parameter of the conversion function. |
| 2396 | User.Before = Best->Conversions[0].Standard; |
| 2397 | User.ConversionFunction = Conversion; |
Douglas Gregor | 83eecbe | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 2398 | User.FoundConversionFunction = Best->FoundDecl.getDecl(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2399 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2400 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2401 | // C++ [over.ics.user]p2: |
| 2402 | // The second standard conversion sequence converts the |
| 2403 | // result of the user-defined conversion to the target type |
| 2404 | // for the sequence. Since an implicit conversion sequence |
| 2405 | // is an initialization, the special rules for |
| 2406 | // initialization by user-defined conversion apply when |
| 2407 | // selecting the best user-defined conversion for a |
| 2408 | // user-defined conversion sequence (see 13.3.3 and |
| 2409 | // 13.3.3.1). |
| 2410 | User.After = Best->FinalConversion; |
| 2411 | return OR_Success; |
| 2412 | } else { |
| 2413 | llvm_unreachable("Not a constructor or conversion function?"); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2414 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2417 | case OR_No_Viable_Function: |
| 2418 | return OR_No_Viable_Function; |
| 2419 | case OR_Deleted: |
| 2420 | // No conversion here! We're done. |
| 2421 | return OR_Deleted; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2422 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2423 | case OR_Ambiguous: |
| 2424 | return OR_Ambiguous; |
| 2425 | } |
| 2426 | |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2427 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2428 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2429 | |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2430 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2431 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2432 | ImplicitConversionSequence ICS; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2433 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2434 | OverloadingResult OvResult = |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2435 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2436 | CandidateSet, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2437 | if (OvResult == OR_Ambiguous) |
| 2438 | Diag(From->getSourceRange().getBegin(), |
| 2439 | diag::err_typecheck_ambiguous_condition) |
| 2440 | << From->getType() << ToType << From->getSourceRange(); |
| 2441 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 2442 | Diag(From->getSourceRange().getBegin(), |
| 2443 | diag::err_typecheck_nonviable_condition) |
| 2444 | << From->getType() << ToType << From->getSourceRange(); |
| 2445 | else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2446 | return false; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2447 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &From, 1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2448 | return true; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2449 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2450 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2451 | /// CompareImplicitConversionSequences - Compare two implicit |
| 2452 | /// conversion sequences to determine whether one is better than the |
| 2453 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2454 | static ImplicitConversionSequence::CompareKind |
| 2455 | CompareImplicitConversionSequences(Sema &S, |
| 2456 | const ImplicitConversionSequence& ICS1, |
| 2457 | const ImplicitConversionSequence& ICS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2458 | { |
| 2459 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 2460 | // conversion sequences (as defined in 13.3.3.1) |
| 2461 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 2462 | // conversion sequence than a user-defined conversion sequence or |
| 2463 | // an ellipsis conversion sequence, and |
| 2464 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 2465 | // conversion sequence than an ellipsis conversion sequence |
| 2466 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2467 | // |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2468 | // C++0x [over.best.ics]p10: |
| 2469 | // For the purpose of ranking implicit conversion sequences as |
| 2470 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 2471 | // treated as a user-defined sequence that is indistinguishable |
| 2472 | // from any other user-defined conversion sequence. |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2473 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 2474 | return ImplicitConversionSequence::Better; |
| 2475 | else if (ICS2.getKindRank() < ICS1.getKindRank()) |
| 2476 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2477 | |
Benjamin Kramer | b6eee07 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 2478 | // The following checks require both conversion sequences to be of |
| 2479 | // the same kind. |
| 2480 | if (ICS1.getKind() != ICS2.getKind()) |
| 2481 | return ImplicitConversionSequence::Indistinguishable; |
| 2482 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2483 | // Two implicit conversion sequences of the same form are |
| 2484 | // indistinguishable conversion sequences unless one of the |
| 2485 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2486 | if (ICS1.isStandard()) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2487 | return CompareStandardConversionSequences(S, ICS1.Standard, ICS2.Standard); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2488 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2489 | // User-defined conversion sequence U1 is a better conversion |
| 2490 | // sequence than another user-defined conversion sequence U2 if |
| 2491 | // they contain the same user-defined conversion function or |
| 2492 | // constructor and if the second standard conversion sequence of |
| 2493 | // U1 is better than the second standard conversion sequence of |
| 2494 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2495 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2496 | ICS2.UserDefined.ConversionFunction) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2497 | return CompareStandardConversionSequences(S, |
| 2498 | ICS1.UserDefined.After, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2499 | ICS2.UserDefined.After); |
| 2500 | } |
| 2501 | |
| 2502 | return ImplicitConversionSequence::Indistinguishable; |
| 2503 | } |
| 2504 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2505 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 2506 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 2507 | Qualifiers Quals; |
| 2508 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2509 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2510 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2511 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2512 | return Context.hasSameUnqualifiedType(T1, T2); |
| 2513 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2514 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2515 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 2516 | // determine if one is a proper subset of the other. |
| 2517 | static ImplicitConversionSequence::CompareKind |
| 2518 | compareStandardConversionSubsets(ASTContext &Context, |
| 2519 | const StandardConversionSequence& SCS1, |
| 2520 | const StandardConversionSequence& SCS2) { |
| 2521 | ImplicitConversionSequence::CompareKind Result |
| 2522 | = ImplicitConversionSequence::Indistinguishable; |
| 2523 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2524 | // the identity conversion sequence is considered to be a subsequence of |
Douglas Gregor | ae65f4b | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 2525 | // any non-identity conversion sequence |
Douglas Gregor | 4ae5b72 | 2011-06-05 06:15:20 +0000 | [diff] [blame] | 2526 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 2527 | return ImplicitConversionSequence::Better; |
| 2528 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 2529 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2530 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2531 | if (SCS1.Second != SCS2.Second) { |
| 2532 | if (SCS1.Second == ICK_Identity) |
| 2533 | Result = ImplicitConversionSequence::Better; |
| 2534 | else if (SCS2.Second == ICK_Identity) |
| 2535 | Result = ImplicitConversionSequence::Worse; |
| 2536 | else |
| 2537 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2538 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2539 | return ImplicitConversionSequence::Indistinguishable; |
| 2540 | |
| 2541 | if (SCS1.Third == SCS2.Third) { |
| 2542 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 2543 | : ImplicitConversionSequence::Indistinguishable; |
| 2544 | } |
| 2545 | |
| 2546 | if (SCS1.Third == ICK_Identity) |
| 2547 | return Result == ImplicitConversionSequence::Worse |
| 2548 | ? ImplicitConversionSequence::Indistinguishable |
| 2549 | : ImplicitConversionSequence::Better; |
| 2550 | |
| 2551 | if (SCS2.Third == ICK_Identity) |
| 2552 | return Result == ImplicitConversionSequence::Better |
| 2553 | ? ImplicitConversionSequence::Indistinguishable |
| 2554 | : ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2555 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2556 | return ImplicitConversionSequence::Indistinguishable; |
| 2557 | } |
| 2558 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2559 | /// \brief Determine whether one of the given reference bindings is better |
| 2560 | /// than the other based on what kind of bindings they are. |
| 2561 | static bool isBetterReferenceBindingKind(const StandardConversionSequence &SCS1, |
| 2562 | const StandardConversionSequence &SCS2) { |
| 2563 | // C++0x [over.ics.rank]p3b4: |
| 2564 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 2565 | // implicit object parameter of a non-static member function declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2566 | // without a ref-qualifier, and *either* S1 binds an rvalue reference |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2567 | // 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] | 2568 | // lvalue reference to a function lvalue and S2 binds an rvalue |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2569 | // reference*. |
| 2570 | // |
| 2571 | // FIXME: Rvalue references. We're going rogue with the above edits, |
| 2572 | // because the semantics in the current C++0x working paper (N3225 at the |
| 2573 | // time of this writing) break the standard definition of std::forward |
| 2574 | // and std::reference_wrapper when dealing with references to functions. |
| 2575 | // Proposed wording changes submitted to CWG for consideration. |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 2576 | if (SCS1.BindsImplicitObjectArgumentWithoutRefQualifier || |
| 2577 | SCS2.BindsImplicitObjectArgumentWithoutRefQualifier) |
| 2578 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2579 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2580 | return (!SCS1.IsLvalueReference && SCS1.BindsToRvalue && |
| 2581 | SCS2.IsLvalueReference) || |
| 2582 | (SCS1.IsLvalueReference && SCS1.BindsToFunctionLvalue && |
| 2583 | !SCS2.IsLvalueReference); |
| 2584 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2585 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2586 | /// CompareStandardConversionSequences - Compare two standard |
| 2587 | /// conversion sequences to determine whether one is better than the |
| 2588 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2589 | static ImplicitConversionSequence::CompareKind |
| 2590 | CompareStandardConversionSequences(Sema &S, |
| 2591 | const StandardConversionSequence& SCS1, |
| 2592 | const StandardConversionSequence& SCS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2593 | { |
| 2594 | // Standard conversion sequence S1 is a better conversion sequence |
| 2595 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 2596 | |
| 2597 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 2598 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 2599 | // excluding any Lvalue Transformation; the identity conversion |
| 2600 | // sequence is considered to be a subsequence of any |
| 2601 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2602 | if (ImplicitConversionSequence::CompareKind CK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2603 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2604 | return CK; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2605 | |
| 2606 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 2607 | // defined below), or, if not that, |
| 2608 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 2609 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 2610 | if (Rank1 < Rank2) |
| 2611 | return ImplicitConversionSequence::Better; |
| 2612 | else if (Rank2 < Rank1) |
| 2613 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2614 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2615 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 2616 | // are indistinguishable unless one of the following rules |
| 2617 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2618 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2619 | // A conversion that is not a conversion of a pointer, or |
| 2620 | // pointer to member, to bool is better than another conversion |
| 2621 | // that is such a conversion. |
| 2622 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 2623 | return SCS2.isPointerConversionToBool() |
| 2624 | ? ImplicitConversionSequence::Better |
| 2625 | : ImplicitConversionSequence::Worse; |
| 2626 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2627 | // C++ [over.ics.rank]p4b2: |
| 2628 | // |
| 2629 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2630 | // conversion of B* to A* is better than conversion of B* to |
| 2631 | // void*, and conversion of A* to void* is better than conversion |
| 2632 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2633 | bool SCS1ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2634 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2635 | bool SCS2ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2636 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2637 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 2638 | // Exactly one of the conversion sequences is a conversion to |
| 2639 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2640 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 2641 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2642 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 2643 | // Neither conversion sequence converts to a void pointer; compare |
| 2644 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2645 | if (ImplicitConversionSequence::CompareKind DerivedCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2646 | = CompareDerivedToBaseConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2647 | return DerivedCK; |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 2648 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid && |
| 2649 | !S.Context.hasSameType(SCS1.getFromType(), SCS2.getFromType())) { |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2650 | // Both conversion sequences are conversions to void |
| 2651 | // pointers. Compare the source types to determine if there's an |
| 2652 | // inheritance relationship in their sources. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2653 | QualType FromType1 = SCS1.getFromType(); |
| 2654 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2655 | |
| 2656 | // Adjust the types we're converting from via the array-to-pointer |
| 2657 | // conversion, if we need to. |
| 2658 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2659 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2660 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2661 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2662 | |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 2663 | QualType FromPointee1 = FromType1->getPointeeType().getUnqualifiedType(); |
| 2664 | QualType FromPointee2 = FromType2->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2665 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2666 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2667 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2668 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2669 | return ImplicitConversionSequence::Worse; |
| 2670 | |
| 2671 | // Objective-C++: If one interface is more specific than the |
| 2672 | // other, it is the better one. |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 2673 | const ObjCObjectPointerType* FromObjCPtr1 |
| 2674 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 2675 | const ObjCObjectPointerType* FromObjCPtr2 |
| 2676 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 2677 | if (FromObjCPtr1 && FromObjCPtr2) { |
| 2678 | bool AssignLeft = S.Context.canAssignObjCInterfaces(FromObjCPtr1, |
| 2679 | FromObjCPtr2); |
| 2680 | bool AssignRight = S.Context.canAssignObjCInterfaces(FromObjCPtr2, |
| 2681 | FromObjCPtr1); |
| 2682 | if (AssignLeft != AssignRight) { |
| 2683 | return AssignLeft? ImplicitConversionSequence::Better |
| 2684 | : ImplicitConversionSequence::Worse; |
| 2685 | } |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2686 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2687 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2688 | |
| 2689 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 2690 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2691 | if (ImplicitConversionSequence::CompareKind QualCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2692 | = CompareQualificationConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2693 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2694 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2695 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2696 | // Check for a better reference binding based on the kind of bindings. |
| 2697 | if (isBetterReferenceBindingKind(SCS1, SCS2)) |
| 2698 | return ImplicitConversionSequence::Better; |
| 2699 | else if (isBetterReferenceBindingKind(SCS2, SCS1)) |
| 2700 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2701 | |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 2702 | // C++ [over.ics.rank]p3b4: |
| 2703 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 2704 | // which the references refer are the same type except for |
| 2705 | // top-level cv-qualifiers, and the type to which the reference |
| 2706 | // initialized by S2 refers is more cv-qualified than the type |
| 2707 | // to which the reference initialized by S1 refers. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2708 | QualType T1 = SCS1.getToType(2); |
| 2709 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2710 | T1 = S.Context.getCanonicalType(T1); |
| 2711 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2712 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2713 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2714 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2715 | if (UnqualT1 == UnqualT2) { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2716 | // If the type is an array type, promote the element qualifiers to the |
| 2717 | // type for comparison. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2718 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2719 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2720 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2721 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2722 | if (T2.isMoreQualifiedThan(T1)) |
| 2723 | return ImplicitConversionSequence::Better; |
| 2724 | else if (T1.isMoreQualifiedThan(T2)) |
| 2725 | return ImplicitConversionSequence::Worse; |
| 2726 | } |
| 2727 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2728 | |
| 2729 | return ImplicitConversionSequence::Indistinguishable; |
| 2730 | } |
| 2731 | |
| 2732 | /// CompareQualificationConversions - Compares two standard conversion |
| 2733 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2734 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 2735 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2736 | CompareQualificationConversions(Sema &S, |
| 2737 | const StandardConversionSequence& SCS1, |
| 2738 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 2739 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2740 | // -- S1 and S2 differ only in their qualification conversion and |
| 2741 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 2742 | // cv-qualification signature of type T1 is a proper subset of |
| 2743 | // the cv-qualification signature of type T2, and S1 is not the |
| 2744 | // deprecated string literal array-to-pointer conversion (4.2). |
| 2745 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 2746 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 2747 | return ImplicitConversionSequence::Indistinguishable; |
| 2748 | |
| 2749 | // FIXME: the example in the standard doesn't use a qualification |
| 2750 | // conversion (!) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2751 | QualType T1 = SCS1.getToType(2); |
| 2752 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2753 | T1 = S.Context.getCanonicalType(T1); |
| 2754 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2755 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2756 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2757 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2758 | |
| 2759 | // If the types are the same, we won't learn anything by unwrapped |
| 2760 | // them. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2761 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2762 | return ImplicitConversionSequence::Indistinguishable; |
| 2763 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2764 | // If the type is an array type, promote the element qualifiers to the type |
| 2765 | // for comparison. |
| 2766 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2767 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2768 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2769 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2770 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2771 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2772 | = ImplicitConversionSequence::Indistinguishable; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2773 | while (S.Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2774 | // Within each iteration of the loop, we check the qualifiers to |
| 2775 | // determine if this still looks like a qualification |
| 2776 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2777 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2778 | // until there are no more pointers or pointers-to-members left |
| 2779 | // to unwrap. This essentially mimics what |
| 2780 | // IsQualificationConversion does, but here we're checking for a |
| 2781 | // strict subset of qualifiers. |
| 2782 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 2783 | // The qualifiers are the same, so this doesn't tell us anything |
| 2784 | // about how the sequences rank. |
| 2785 | ; |
| 2786 | else if (T2.isMoreQualifiedThan(T1)) { |
| 2787 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 2788 | if (Result == ImplicitConversionSequence::Worse) |
| 2789 | // Neither has qualifiers that are a subset of the other's |
| 2790 | // qualifiers. |
| 2791 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2792 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2793 | Result = ImplicitConversionSequence::Better; |
| 2794 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 2795 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 2796 | if (Result == ImplicitConversionSequence::Better) |
| 2797 | // Neither has qualifiers that are a subset of the other's |
| 2798 | // qualifiers. |
| 2799 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2800 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2801 | Result = ImplicitConversionSequence::Worse; |
| 2802 | } else { |
| 2803 | // Qualifiers are disjoint. |
| 2804 | return ImplicitConversionSequence::Indistinguishable; |
| 2805 | } |
| 2806 | |
| 2807 | // If the types after this point are equivalent, we're done. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2808 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2809 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2812 | // Check that the winning standard conversion sequence isn't using |
| 2813 | // the deprecated string literal array to pointer conversion. |
| 2814 | switch (Result) { |
| 2815 | case ImplicitConversionSequence::Better: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2816 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2817 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2818 | break; |
| 2819 | |
| 2820 | case ImplicitConversionSequence::Indistinguishable: |
| 2821 | break; |
| 2822 | |
| 2823 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2824 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2825 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2826 | break; |
| 2827 | } |
| 2828 | |
| 2829 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2832 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 2833 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2834 | /// various kinds of derived-to-base conversions (C++ |
| 2835 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 2836 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2837 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2838 | CompareDerivedToBaseConversions(Sema &S, |
| 2839 | const StandardConversionSequence& SCS1, |
| 2840 | const StandardConversionSequence& SCS2) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2841 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2842 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2843 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2844 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2845 | |
| 2846 | // Adjust the types we're converting from via the array-to-pointer |
| 2847 | // conversion, if we need to. |
| 2848 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2849 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2850 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2851 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2852 | |
| 2853 | // Canonicalize all of the types. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2854 | FromType1 = S.Context.getCanonicalType(FromType1); |
| 2855 | ToType1 = S.Context.getCanonicalType(ToType1); |
| 2856 | FromType2 = S.Context.getCanonicalType(FromType2); |
| 2857 | ToType2 = S.Context.getCanonicalType(ToType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2858 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2859 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2860 | // |
| 2861 | // If class B is derived directly or indirectly from class A and |
| 2862 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2863 | // |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2864 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2865 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 2866 | SCS2.Second == ICK_Pointer_Conversion && |
| 2867 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 2868 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 2869 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2870 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2871 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2872 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2873 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2874 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2875 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2876 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2877 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2878 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2879 | // -- 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] | 2880 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2881 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2882 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2883 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2884 | return ImplicitConversionSequence::Worse; |
| 2885 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2886 | |
| 2887 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 2888 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2889 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2890 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2891 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2892 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 2893 | } |
| 2894 | } else if (SCS1.Second == ICK_Pointer_Conversion && |
| 2895 | SCS2.Second == ICK_Pointer_Conversion) { |
| 2896 | const ObjCObjectPointerType *FromPtr1 |
| 2897 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 2898 | const ObjCObjectPointerType *FromPtr2 |
| 2899 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 2900 | const ObjCObjectPointerType *ToPtr1 |
| 2901 | = ToType1->getAs<ObjCObjectPointerType>(); |
| 2902 | const ObjCObjectPointerType *ToPtr2 |
| 2903 | = ToType2->getAs<ObjCObjectPointerType>(); |
| 2904 | |
| 2905 | if (FromPtr1 && FromPtr2 && ToPtr1 && ToPtr2) { |
| 2906 | // Apply the same conversion ranking rules for Objective-C pointer types |
| 2907 | // that we do for C++ pointers to class types. However, we employ the |
| 2908 | // Objective-C pseudo-subtyping relationship used for assignment of |
| 2909 | // Objective-C pointer types. |
| 2910 | bool FromAssignLeft |
| 2911 | = S.Context.canAssignObjCInterfaces(FromPtr1, FromPtr2); |
| 2912 | bool FromAssignRight |
| 2913 | = S.Context.canAssignObjCInterfaces(FromPtr2, FromPtr1); |
| 2914 | bool ToAssignLeft |
| 2915 | = S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2); |
| 2916 | bool ToAssignRight |
| 2917 | = S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1); |
| 2918 | |
| 2919 | // A conversion to an a non-id object pointer type or qualified 'id' |
| 2920 | // type is better than a conversion to 'id'. |
| 2921 | if (ToPtr1->isObjCIdType() && |
| 2922 | (ToPtr2->isObjCQualifiedIdType() || ToPtr2->getInterfaceDecl())) |
| 2923 | return ImplicitConversionSequence::Worse; |
| 2924 | if (ToPtr2->isObjCIdType() && |
| 2925 | (ToPtr1->isObjCQualifiedIdType() || ToPtr1->getInterfaceDecl())) |
| 2926 | return ImplicitConversionSequence::Better; |
| 2927 | |
| 2928 | // A conversion to a non-id object pointer type is better than a |
| 2929 | // conversion to a qualified 'id' type |
| 2930 | if (ToPtr1->isObjCQualifiedIdType() && ToPtr2->getInterfaceDecl()) |
| 2931 | return ImplicitConversionSequence::Worse; |
| 2932 | if (ToPtr2->isObjCQualifiedIdType() && ToPtr1->getInterfaceDecl()) |
| 2933 | return ImplicitConversionSequence::Better; |
| 2934 | |
| 2935 | // A conversion to an a non-Class object pointer type or qualified 'Class' |
| 2936 | // type is better than a conversion to 'Class'. |
| 2937 | if (ToPtr1->isObjCClassType() && |
| 2938 | (ToPtr2->isObjCQualifiedClassType() || ToPtr2->getInterfaceDecl())) |
| 2939 | return ImplicitConversionSequence::Worse; |
| 2940 | if (ToPtr2->isObjCClassType() && |
| 2941 | (ToPtr1->isObjCQualifiedClassType() || ToPtr1->getInterfaceDecl())) |
| 2942 | return ImplicitConversionSequence::Better; |
| 2943 | |
| 2944 | // A conversion to a non-Class object pointer type is better than a |
| 2945 | // conversion to a qualified 'Class' type. |
| 2946 | if (ToPtr1->isObjCQualifiedClassType() && ToPtr2->getInterfaceDecl()) |
| 2947 | return ImplicitConversionSequence::Worse; |
| 2948 | if (ToPtr2->isObjCQualifiedClassType() && ToPtr1->getInterfaceDecl()) |
| 2949 | return ImplicitConversionSequence::Better; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2950 | |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 2951 | // -- "conversion of C* to B* is better than conversion of C* to A*," |
| 2952 | if (S.Context.hasSameType(FromType1, FromType2) && |
| 2953 | !FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType() && |
| 2954 | (ToAssignLeft != ToAssignRight)) |
| 2955 | return ToAssignLeft? ImplicitConversionSequence::Worse |
| 2956 | : ImplicitConversionSequence::Better; |
| 2957 | |
| 2958 | // -- "conversion of B* to A* is better than conversion of C* to A*," |
| 2959 | if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) && |
| 2960 | (FromAssignLeft != FromAssignRight)) |
| 2961 | return FromAssignLeft? ImplicitConversionSequence::Better |
| 2962 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2963 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2964 | } |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 2965 | |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2966 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2967 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 2968 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 2969 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2970 | const MemberPointerType * FromMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2971 | FromType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2972 | const MemberPointerType * ToMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2973 | ToType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2974 | const MemberPointerType * FromMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2975 | FromType2->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2976 | const MemberPointerType * ToMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2977 | ToType2->getAs<MemberPointerType>(); |
| 2978 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 2979 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 2980 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 2981 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 2982 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 2983 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 2984 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 2985 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2986 | // 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] | 2987 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2988 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2989 | return ImplicitConversionSequence::Worse; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2990 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2991 | return ImplicitConversionSequence::Better; |
| 2992 | } |
| 2993 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 2994 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2995 | if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2996 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2997 | else if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2998 | return ImplicitConversionSequence::Worse; |
| 2999 | } |
| 3000 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3001 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3002 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3003 | // -- 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] | 3004 | // -- binding of an expression of type C to a reference of type |
| 3005 | // B& is better than binding an expression of type C to a |
| 3006 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3007 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3008 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3009 | if (S.IsDerivedFrom(ToType1, ToType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3010 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3011 | else if (S.IsDerivedFrom(ToType2, ToType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3012 | return ImplicitConversionSequence::Worse; |
| 3013 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3014 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3015 | // -- 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] | 3016 | // -- binding of an expression of type B to a reference of type |
| 3017 | // A& is better than binding an expression of type C to a |
| 3018 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3019 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3020 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3021 | if (S.IsDerivedFrom(FromType2, FromType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3022 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3023 | else if (S.IsDerivedFrom(FromType1, FromType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3024 | return ImplicitConversionSequence::Worse; |
| 3025 | } |
| 3026 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3027 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3028 | return ImplicitConversionSequence::Indistinguishable; |
| 3029 | } |
| 3030 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3031 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 3032 | /// determine whether they are reference-related, |
| 3033 | /// reference-compatible, reference-compatible with added |
| 3034 | /// qualification, or incompatible, for use in C++ initialization by |
| 3035 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 3036 | /// type, and the first type (T1) is the pointee type of the reference |
| 3037 | /// type being initialized. |
| 3038 | Sema::ReferenceCompareResult |
| 3039 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 3040 | QualType OrigT1, QualType OrigT2, |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3041 | bool &DerivedToBase, |
| 3042 | bool &ObjCConversion) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3043 | assert(!OrigT1->isReferenceType() && |
| 3044 | "T1 must be the pointee type of the reference type"); |
| 3045 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 3046 | |
| 3047 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 3048 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 3049 | Qualifiers T1Quals, T2Quals; |
| 3050 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3051 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 3052 | |
| 3053 | // C++ [dcl.init.ref]p4: |
| 3054 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 3055 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 3056 | // T1 is a base class of T2. |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3057 | DerivedToBase = false; |
| 3058 | ObjCConversion = false; |
| 3059 | if (UnqualT1 == UnqualT2) { |
| 3060 | // Nothing to do. |
| 3061 | } else if (!RequireCompleteType(Loc, OrigT2, PDiag()) && |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3062 | IsDerivedFrom(UnqualT2, UnqualT1)) |
| 3063 | DerivedToBase = true; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3064 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
| 3065 | UnqualT2->isObjCObjectOrInterfaceType() && |
| 3066 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
| 3067 | ObjCConversion = true; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3068 | else |
| 3069 | return Ref_Incompatible; |
| 3070 | |
| 3071 | // At this point, we know that T1 and T2 are reference-related (at |
| 3072 | // least). |
| 3073 | |
| 3074 | // If the type is an array type, promote the element qualifiers to the type |
| 3075 | // for comparison. |
| 3076 | if (isa<ArrayType>(T1) && T1Quals) |
| 3077 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 3078 | if (isa<ArrayType>(T2) && T2Quals) |
| 3079 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 3080 | |
| 3081 | // C++ [dcl.init.ref]p4: |
| 3082 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 3083 | // reference-related to T2 and cv1 is the same cv-qualification |
| 3084 | // as, or greater cv-qualification than, cv2. For purposes of |
| 3085 | // overload resolution, cases for which cv1 is greater |
| 3086 | // cv-qualification than cv2 are identified as |
| 3087 | // reference-compatible with added qualification (see 13.3.3.2). |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 3088 | // |
| 3089 | // Note that we also require equivalence of Objective-C GC and address-space |
| 3090 | // qualifiers when performing these computations, so that e.g., an int in |
| 3091 | // address space 1 is not reference-compatible with an int in address |
| 3092 | // space 2. |
| 3093 | if (T1Quals == T2Quals) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3094 | return Ref_Compatible; |
| 3095 | else if (T1.isMoreQualifiedThan(T2)) |
| 3096 | return Ref_Compatible_With_Added_Qualification; |
| 3097 | else |
| 3098 | return Ref_Related; |
| 3099 | } |
| 3100 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3101 | /// \brief Look for a user-defined conversion to an value reference-compatible |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3102 | /// with DeclType. Return true if something definite is found. |
| 3103 | static bool |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3104 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
| 3105 | QualType DeclType, SourceLocation DeclLoc, |
| 3106 | Expr *Init, QualType T2, bool AllowRvalues, |
| 3107 | bool AllowExplicit) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3108 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 3109 | CXXRecordDecl *T2RecordDecl |
| 3110 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 3111 | |
| 3112 | OverloadCandidateSet CandidateSet(DeclLoc); |
| 3113 | const UnresolvedSetImpl *Conversions |
| 3114 | = T2RecordDecl->getVisibleConversionFunctions(); |
| 3115 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 3116 | E = Conversions->end(); I != E; ++I) { |
| 3117 | NamedDecl *D = *I; |
| 3118 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3119 | if (isa<UsingShadowDecl>(D)) |
| 3120 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3121 | |
| 3122 | FunctionTemplateDecl *ConvTemplate |
| 3123 | = dyn_cast<FunctionTemplateDecl>(D); |
| 3124 | CXXConversionDecl *Conv; |
| 3125 | if (ConvTemplate) |
| 3126 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3127 | else |
| 3128 | Conv = cast<CXXConversionDecl>(D); |
| 3129 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3130 | // 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] | 3131 | // explicit conversions, skip it. |
| 3132 | if (!AllowExplicit && Conv->isExplicit()) |
| 3133 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3134 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3135 | if (AllowRvalues) { |
| 3136 | bool DerivedToBase = false; |
| 3137 | bool ObjCConversion = false; |
| 3138 | if (!ConvTemplate && |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3139 | S.CompareReferenceRelationship( |
| 3140 | DeclLoc, |
| 3141 | Conv->getConversionType().getNonReferenceType() |
| 3142 | .getUnqualifiedType(), |
| 3143 | DeclType.getNonReferenceType().getUnqualifiedType(), |
| 3144 | DerivedToBase, ObjCConversion) == |
| 3145 | Sema::Ref_Incompatible) |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3146 | continue; |
| 3147 | } else { |
| 3148 | // If the conversion function doesn't return a reference type, |
| 3149 | // it can't be considered for this conversion. An rvalue reference |
| 3150 | // is only acceptable if its referencee is a function type. |
| 3151 | |
| 3152 | const ReferenceType *RefType = |
| 3153 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 3154 | if (!RefType || |
| 3155 | (!RefType->isLValueReferenceType() && |
| 3156 | !RefType->getPointeeType()->isFunctionType())) |
| 3157 | continue; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3158 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3159 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3160 | if (ConvTemplate) |
| 3161 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3162 | Init, DeclType, CandidateSet); |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3163 | else |
| 3164 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3165 | DeclType, CandidateSet); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3166 | } |
| 3167 | |
| 3168 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3169 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3170 | case OR_Success: |
| 3171 | // C++ [over.ics.ref]p1: |
| 3172 | // |
| 3173 | // [...] If the parameter binds directly to the result of |
| 3174 | // applying a conversion function to the argument |
| 3175 | // expression, the implicit conversion sequence is a |
| 3176 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 3177 | // second standard conversion sequence either an identity |
| 3178 | // conversion or, if the conversion function returns an |
| 3179 | // entity of a type that is a derived class of the parameter |
| 3180 | // type, a derived-to-base Conversion. |
| 3181 | if (!Best->FinalConversion.DirectBinding) |
| 3182 | return false; |
| 3183 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3184 | if (Best->Function) |
| 3185 | S.MarkDeclarationReferenced(DeclLoc, Best->Function); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3186 | ICS.setUserDefined(); |
| 3187 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 3188 | ICS.UserDefined.After = Best->FinalConversion; |
| 3189 | ICS.UserDefined.ConversionFunction = Best->Function; |
Douglas Gregor | 83eecbe | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 3190 | ICS.UserDefined.FoundConversionFunction = Best->FoundDecl.getDecl(); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3191 | ICS.UserDefined.EllipsisConversion = false; |
| 3192 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 3193 | ICS.UserDefined.After.DirectBinding && |
| 3194 | "Expected a direct reference binding!"); |
| 3195 | return true; |
| 3196 | |
| 3197 | case OR_Ambiguous: |
| 3198 | ICS.setAmbiguous(); |
| 3199 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3200 | Cand != CandidateSet.end(); ++Cand) |
| 3201 | if (Cand->Viable) |
| 3202 | ICS.Ambiguous.addConversion(Cand->Function); |
| 3203 | return true; |
| 3204 | |
| 3205 | case OR_No_Viable_Function: |
| 3206 | case OR_Deleted: |
| 3207 | // There was no suitable conversion, or we found a deleted |
| 3208 | // conversion; continue with other checks. |
| 3209 | return false; |
| 3210 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3211 | |
Eric Christopher | 1c3d502 | 2010-06-30 18:36:32 +0000 | [diff] [blame] | 3212 | return false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3213 | } |
| 3214 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3215 | /// \brief Compute an implicit conversion sequence for reference |
| 3216 | /// initialization. |
| 3217 | static ImplicitConversionSequence |
| 3218 | TryReferenceInit(Sema &S, Expr *&Init, QualType DeclType, |
| 3219 | SourceLocation DeclLoc, |
| 3220 | bool SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 3221 | bool AllowExplicit) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3222 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 3223 | |
| 3224 | // Most paths end in a failed conversion. |
| 3225 | ImplicitConversionSequence ICS; |
| 3226 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 3227 | |
| 3228 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 3229 | QualType T2 = Init->getType(); |
| 3230 | |
| 3231 | // If the initializer is the address of an overloaded function, try |
| 3232 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3233 | // type of the resulting function. |
| 3234 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 3235 | DeclAccessPair Found; |
| 3236 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 3237 | false, Found)) |
| 3238 | T2 = Fn->getType(); |
| 3239 | } |
| 3240 | |
| 3241 | // Compute some basic properties of the types and the initializer. |
| 3242 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 3243 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3244 | bool ObjCConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3245 | Expr::Classification InitCategory = Init->Classify(S.Context); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3246 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3247 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase, |
| 3248 | ObjCConversion); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3249 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3250 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3251 | // C++0x [dcl.init.ref]p5: |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 3252 | // A reference to type "cv1 T1" is initialized by an expression |
| 3253 | // of type "cv2 T2" as follows: |
| 3254 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3255 | // -- If reference is an lvalue reference and the initializer expression |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3256 | if (!isRValRef) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3257 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 3258 | // reference-compatible with "cv2 T2," or |
| 3259 | // |
| 3260 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 3261 | if (InitCategory.isLValue() && |
| 3262 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3263 | // C++ [over.ics.ref]p1: |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3264 | // When a parameter of reference type binds directly (8.5.3) |
| 3265 | // to an argument expression, the implicit conversion sequence |
| 3266 | // is the identity conversion, unless the argument expression |
| 3267 | // has a type that is a derived class of the parameter type, |
| 3268 | // in which case the implicit conversion sequence is a |
| 3269 | // derived-to-base Conversion (13.3.3.1). |
| 3270 | ICS.setStandard(); |
| 3271 | ICS.Standard.First = ICK_Identity; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3272 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 3273 | : ObjCConversion? ICK_Compatible_Conversion |
| 3274 | : ICK_Identity; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3275 | ICS.Standard.Third = ICK_Identity; |
| 3276 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 3277 | ICS.Standard.setToType(0, T2); |
| 3278 | ICS.Standard.setToType(1, T1); |
| 3279 | ICS.Standard.setToType(2, T1); |
| 3280 | ICS.Standard.ReferenceBinding = true; |
| 3281 | ICS.Standard.DirectBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3282 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3283 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 3284 | ICS.Standard.BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3285 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3286 | ICS.Standard.CopyConstructor = 0; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3287 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3288 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 3289 | // derived-to-base conversions is suppressed when we're |
| 3290 | // computing the implicit conversion sequence (C++ |
| 3291 | // [over.best.ics]p2). |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3292 | return ICS; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3293 | } |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3294 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3295 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 3296 | // not reference-related to T2, and can be implicitly |
| 3297 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 3298 | // is reference-compatible with "cv3 T3" 92) (this |
| 3299 | // conversion is selected by enumerating the applicable |
| 3300 | // conversion functions (13.3.1.6) and choosing the best |
| 3301 | // one through overload resolution (13.3)), |
| 3302 | if (!SuppressUserConversions && T2->isRecordType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3303 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3304 | RefRelationship == Sema::Ref_Incompatible) { |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3305 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 3306 | Init, T2, /*AllowRvalues=*/false, |
| 3307 | AllowExplicit)) |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3308 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3309 | } |
| 3310 | } |
| 3311 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3312 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 3313 | // 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] | 3314 | // shall be an rvalue reference. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3315 | // |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 3316 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 3317 | // point, which is that, due to p2 (which short-circuits reference |
| 3318 | // binding by only attempting a simple conversion for non-direct |
| 3319 | // bindings) and p3's strange wording, we allow a const volatile |
| 3320 | // reference to bind to an rvalue. Hence the check for the presence |
| 3321 | // of "const" rather than checking for "const" being the only |
| 3322 | // qualifier. |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3323 | // This is also the point where rvalue references and lvalue inits no longer |
| 3324 | // go together. |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 3325 | if (!isRValRef && !T1.isConstQualified()) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3326 | return ICS; |
| 3327 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3328 | // -- If the initializer expression |
| 3329 | // |
| 3330 | // -- is an xvalue, class prvalue, array prvalue or function |
| 3331 | // lvalue and "cv1T1" is reference-compatible with "cv2 T2", or |
| 3332 | if (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification && |
| 3333 | (InitCategory.isXValue() || |
| 3334 | (InitCategory.isPRValue() && (T2->isRecordType() || T2->isArrayType())) || |
| 3335 | (InitCategory.isLValue() && T2->isFunctionType()))) { |
| 3336 | ICS.setStandard(); |
| 3337 | ICS.Standard.First = ICK_Identity; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3338 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3339 | : ObjCConversion? ICK_Compatible_Conversion |
| 3340 | : ICK_Identity; |
| 3341 | ICS.Standard.Third = ICK_Identity; |
| 3342 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 3343 | ICS.Standard.setToType(0, T2); |
| 3344 | ICS.Standard.setToType(1, T1); |
| 3345 | ICS.Standard.setToType(2, T1); |
| 3346 | ICS.Standard.ReferenceBinding = true; |
| 3347 | // In C++0x, this is always a direct binding. In C++98/03, it's a direct |
| 3348 | // binding unless we're binding to a class prvalue. |
| 3349 | // Note: Although xvalues wouldn't normally show up in C++98/03 code, we |
| 3350 | // allow the use of rvalue references in C++98/03 for the benefit of |
| 3351 | // standard library implementors; therefore, we need the xvalue check here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3352 | ICS.Standard.DirectBinding = |
| 3353 | S.getLangOptions().CPlusPlus0x || |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3354 | (InitCategory.isPRValue() && !T2->isRecordType()); |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3355 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3356 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3357 | ICS.Standard.BindsToRvalue = InitCategory.isRValue(); |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3358 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3359 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3360 | return ICS; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3361 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3362 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3363 | // -- has a class type (i.e., T2 is a class type), where T1 is not |
| 3364 | // reference-related to T2, and can be implicitly converted to |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3365 | // an xvalue, class prvalue, or function lvalue of type |
| 3366 | // "cv3 T3", where "cv1 T1" is reference-compatible with |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3367 | // "cv3 T3", |
| 3368 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3369 | // then the reference is bound to the value of the initializer |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3370 | // expression in the first case and to the result of the conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3371 | // in the second case (or, in either case, to an appropriate base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3372 | // class subobject). |
| 3373 | if (!SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3374 | T2->isRecordType() && !S.RequireCompleteType(DeclLoc, T2, 0) && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3375 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 3376 | Init, T2, /*AllowRvalues=*/true, |
| 3377 | AllowExplicit)) { |
| 3378 | // In the second case, if the reference is an rvalue reference |
| 3379 | // and the second standard conversion sequence of the |
| 3380 | // user-defined conversion sequence includes an lvalue-to-rvalue |
| 3381 | // conversion, the program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3382 | if (ICS.isUserDefined() && isRValRef && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3383 | ICS.UserDefined.After.First == ICK_Lvalue_To_Rvalue) |
| 3384 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 3385 | |
Douglas Gregor | 68ed68b | 2011-01-21 16:36:05 +0000 | [diff] [blame] | 3386 | return ICS; |
Rafael Espindola | aa5952c | 2011-01-22 15:32:35 +0000 | [diff] [blame] | 3387 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3388 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3389 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 3390 | // initialized from the initializer expression using the |
| 3391 | // rules for a non-reference copy initialization (8.5). The |
| 3392 | // reference is then bound to the temporary. If T1 is |
| 3393 | // reference-related to T2, cv1 must be the same |
| 3394 | // cv-qualification as, or greater cv-qualification than, |
| 3395 | // cv2; otherwise, the program is ill-formed. |
| 3396 | if (RefRelationship == Sema::Ref_Related) { |
| 3397 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 3398 | // we would be reference-compatible or reference-compatible with |
| 3399 | // added qualification. But that wasn't the case, so the reference |
| 3400 | // initialization fails. |
| 3401 | return ICS; |
| 3402 | } |
| 3403 | |
| 3404 | // If at least one of the types is a class type, the types are not |
| 3405 | // related, and we aren't allowed any user conversions, the |
| 3406 | // reference binding fails. This case is important for breaking |
| 3407 | // recursion, since TryImplicitConversion below will attempt to |
| 3408 | // create a temporary through the use of a copy constructor. |
| 3409 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 3410 | (T1->isRecordType() || T2->isRecordType())) |
| 3411 | return ICS; |
| 3412 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 3413 | // If T1 is reference-related to T2 and the reference is an rvalue |
| 3414 | // reference, the initializer expression shall not be an lvalue. |
| 3415 | if (RefRelationship >= Sema::Ref_Related && |
| 3416 | isRValRef && Init->Classify(S.Context).isLValue()) |
| 3417 | return ICS; |
| 3418 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3419 | // C++ [over.ics.ref]p2: |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3420 | // When a parameter of reference type is not bound directly to |
| 3421 | // an argument expression, the conversion sequence is the one |
| 3422 | // required to convert the argument expression to the |
| 3423 | // underlying type of the reference according to |
| 3424 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 3425 | // to copy-initializing a temporary of the underlying type with |
| 3426 | // the argument expression. Any difference in top-level |
| 3427 | // cv-qualification is subsumed by the initialization itself |
| 3428 | // and does not constitute a conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3429 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
| 3430 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3431 | /*InOverloadResolution=*/false, |
| 3432 | /*CStyle=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3433 | |
| 3434 | // Of course, that's still a reference binding. |
| 3435 | if (ICS.isStandard()) { |
| 3436 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3437 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3438 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 3439 | ICS.Standard.BindsToRvalue = true; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3440 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3441 | } else if (ICS.isUserDefined()) { |
| 3442 | ICS.UserDefined.After.ReferenceBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3443 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3444 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 3445 | ICS.Standard.BindsToRvalue = true; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3446 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3447 | } |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 3448 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3449 | return ICS; |
| 3450 | } |
| 3451 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3452 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 3453 | /// ToType from the expression From. Return the implicit conversion |
| 3454 | /// sequence required to pass this argument, which may be a bad |
| 3455 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3456 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | 74e386e | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 3457 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3458 | static ImplicitConversionSequence |
| 3459 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3460 | bool SuppressUserConversions, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3461 | bool InOverloadResolution) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3462 | if (ToType->isReferenceType()) |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3463 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3464 | /*FIXME:*/From->getLocStart(), |
| 3465 | SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 3466 | /*AllowExplicit=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3467 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3468 | return TryImplicitConversion(S, From, ToType, |
| 3469 | SuppressUserConversions, |
| 3470 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3471 | InOverloadResolution, |
| 3472 | /*CStyle=*/false); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3473 | } |
| 3474 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3475 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 3476 | /// parameter of the given member function (@c Method) from the |
| 3477 | /// expression @p From. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3478 | static ImplicitConversionSequence |
| 3479 | TryObjectArgumentInitialization(Sema &S, QualType OrigFromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3480 | Expr::Classification FromClassification, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3481 | CXXMethodDecl *Method, |
| 3482 | CXXRecordDecl *ActingContext) { |
| 3483 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 3484 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 3485 | // const volatile object. |
| 3486 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 3487 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3488 | QualType ImplicitParamType = S.Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3489 | |
| 3490 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 3491 | // to exit early. |
| 3492 | ImplicitConversionSequence ICS; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3493 | |
| 3494 | // We need to have an object of class type. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 3495 | QualType FromType = OrigFromType; |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3496 | if (const PointerType *PT = FromType->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3497 | FromType = PT->getPointeeType(); |
| 3498 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3499 | // When we had a pointer, it's implicitly dereferenced, so we |
| 3500 | // better have an lvalue. |
| 3501 | assert(FromClassification.isLValue()); |
| 3502 | } |
| 3503 | |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3504 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3505 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3506 | // C++0x [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3507 | // For non-static member functions, the type of the implicit object |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3508 | // parameter is |
| 3509 | // |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3510 | // - "lvalue reference to cv X" for functions declared without a |
| 3511 | // ref-qualifier or with the & ref-qualifier |
| 3512 | // - "rvalue reference to cv X" for functions declared with the && |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3513 | // ref-qualifier |
| 3514 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3515 | // 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] | 3516 | // cv-qualification on the member function declaration. |
| 3517 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3518 | // However, when finding an implicit conversion sequence for the argument, we |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3519 | // are not allowed to create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3520 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 3521 | // reference binding here, that allows class rvalues to bind to |
| 3522 | // non-constant references. |
| 3523 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3524 | // First check the qualifiers. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3525 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3526 | if (ImplicitParamType.getCVRQualifiers() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3527 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3528 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3529 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
| 3530 | OrigFromType, 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 | |
| 3534 | // Check that we have either the same type or a derived type. It |
| 3535 | // affects the conversion rank. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3536 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3537 | ImplicitConversionKind SecondKind; |
| 3538 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 3539 | SecondKind = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3540 | } else if (S.IsDerivedFrom(FromType, ClassType)) |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3541 | SecondKind = ICK_Derived_To_Base; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3542 | else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3543 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 3544 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3545 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3546 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3547 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3548 | // Check the ref-qualifier. |
| 3549 | switch (Method->getRefQualifier()) { |
| 3550 | case RQ_None: |
| 3551 | // Do nothing; we don't care about lvalueness or rvalueness. |
| 3552 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3553 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3554 | case RQ_LValue: |
| 3555 | if (!FromClassification.isLValue() && Quals != Qualifiers::Const) { |
| 3556 | // non-const lvalue reference cannot bind to an rvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3557 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3558 | ImplicitParamType); |
| 3559 | return ICS; |
| 3560 | } |
| 3561 | break; |
| 3562 | |
| 3563 | case RQ_RValue: |
| 3564 | if (!FromClassification.isRValue()) { |
| 3565 | // rvalue reference cannot bind to an lvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3566 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3567 | ImplicitParamType); |
| 3568 | return ICS; |
| 3569 | } |
| 3570 | break; |
| 3571 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3572 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3573 | // Success. Mark this as a reference binding. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3574 | ICS.setStandard(); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3575 | ICS.Standard.setAsIdentityConversion(); |
| 3576 | ICS.Standard.Second = SecondKind; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3577 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3578 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3579 | ICS.Standard.ReferenceBinding = true; |
| 3580 | ICS.Standard.DirectBinding = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3581 | ICS.Standard.IsLvalueReference = Method->getRefQualifier() != RQ_RValue; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3582 | ICS.Standard.BindsToFunctionLvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3583 | ICS.Standard.BindsToRvalue = FromClassification.isRValue(); |
| 3584 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier |
| 3585 | = (Method->getRefQualifier() == RQ_None); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3586 | return ICS; |
| 3587 | } |
| 3588 | |
| 3589 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 3590 | /// the implicit object parameter for the given Method with the given |
| 3591 | /// expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3592 | ExprResult |
| 3593 | Sema::PerformObjectArgumentInitialization(Expr *From, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3594 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3595 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3596 | CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3597 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3598 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3599 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3600 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3601 | Expr::Classification FromClassification; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3602 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3603 | FromRecordType = PT->getPointeeType(); |
| 3604 | DestType = Method->getThisType(Context); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3605 | FromClassification = Expr::Classification::makeSimpleLValue(); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3606 | } else { |
| 3607 | FromRecordType = From->getType(); |
| 3608 | DestType = ImplicitParamRecordType; |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3609 | FromClassification = From->Classify(Context); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3610 | } |
| 3611 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3612 | // Note that we always use the true parent context when performing |
| 3613 | // the actual argument initialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3614 | ImplicitConversionSequence ICS |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3615 | = TryObjectArgumentInitialization(*this, From->getType(), FromClassification, |
| 3616 | Method, Method->getParent()); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 3617 | if (ICS.isBad()) { |
| 3618 | if (ICS.Bad.Kind == BadConversionSequence::bad_qualifiers) { |
| 3619 | Qualifiers FromQs = FromRecordType.getQualifiers(); |
| 3620 | Qualifiers ToQs = DestType.getQualifiers(); |
| 3621 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 3622 | if (CVR) { |
| 3623 | Diag(From->getSourceRange().getBegin(), |
| 3624 | diag::err_member_function_call_bad_cvr) |
| 3625 | << Method->getDeclName() << FromRecordType << (CVR - 1) |
| 3626 | << From->getSourceRange(); |
| 3627 | Diag(Method->getLocation(), diag::note_previous_decl) |
| 3628 | << Method->getDeclName(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3629 | return ExprError(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 3630 | } |
| 3631 | } |
| 3632 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3633 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3634 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3635 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 3636 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3637 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3638 | if (ICS.Standard.Second == ICK_Derived_To_Base) { |
| 3639 | ExprResult FromRes = |
| 3640 | PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
| 3641 | if (FromRes.isInvalid()) |
| 3642 | return ExprError(); |
| 3643 | From = FromRes.take(); |
| 3644 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3645 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3646 | if (!Context.hasSameType(From->getType(), DestType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3647 | From = ImpCastExprToType(From, DestType, CK_NoOp, |
| 3648 | From->getType()->isPointerType() ? VK_RValue : VK_LValue).take(); |
| 3649 | return Owned(From); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3650 | } |
| 3651 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3652 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 3653 | /// expression From to bool (C++0x [conv]p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3654 | static ImplicitConversionSequence |
| 3655 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
Douglas Gregor | c6dfe19 | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 3656 | // FIXME: This is pretty broken. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3657 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 3658 | // FIXME: Are these flags correct? |
| 3659 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3660 | /*AllowExplicit=*/true, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3661 | /*InOverloadResolution=*/false, |
| 3662 | /*CStyle=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3663 | } |
| 3664 | |
| 3665 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 3666 | /// of the expression From to bool (C++0x [conv]p3). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3667 | ExprResult Sema::PerformContextuallyConvertToBool(Expr *From) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3668 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3669 | if (!ICS.isBad()) |
| 3670 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3671 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3672 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3673 | return Diag(From->getSourceRange().getBegin(), |
| 3674 | diag::err_typecheck_bool_condition) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3675 | << From->getType() << From->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3676 | return ExprError(); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3677 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3678 | |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3679 | /// TryContextuallyConvertToObjCId - Attempt to contextually convert the |
| 3680 | /// expression From to 'id'. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3681 | static ImplicitConversionSequence |
| 3682 | TryContextuallyConvertToObjCId(Sema &S, Expr *From) { |
| 3683 | QualType Ty = S.Context.getObjCIdType(); |
| 3684 | return TryImplicitConversion(S, From, Ty, |
| 3685 | // FIXME: Are these flags correct? |
| 3686 | /*SuppressUserConversions=*/false, |
| 3687 | /*AllowExplicit=*/true, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3688 | /*InOverloadResolution=*/false, |
| 3689 | /*CStyle=*/false); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3690 | } |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3691 | |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3692 | /// PerformContextuallyConvertToObjCId - Perform a contextual conversion |
| 3693 | /// of the expression From to 'id'. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3694 | ExprResult Sema::PerformContextuallyConvertToObjCId(Expr *From) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3695 | QualType Ty = Context.getObjCIdType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3696 | ImplicitConversionSequence ICS = TryContextuallyConvertToObjCId(*this, From); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3697 | if (!ICS.isBad()) |
| 3698 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3699 | return ExprError(); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3700 | } |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3701 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3702 | /// \brief Attempt to convert the given expression to an integral or |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3703 | /// enumeration type. |
| 3704 | /// |
| 3705 | /// This routine will attempt to convert an expression of class type to an |
| 3706 | /// integral or enumeration type, if that class type only has a single |
| 3707 | /// conversion to an integral or enumeration type. |
| 3708 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3709 | /// \param Loc The source location of the construct that requires the |
| 3710 | /// conversion. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3711 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3712 | /// \param FromE The expression we're converting from. |
| 3713 | /// |
| 3714 | /// \param NotIntDiag The diagnostic to be emitted if the expression does not |
| 3715 | /// have integral or enumeration type. |
| 3716 | /// |
| 3717 | /// \param IncompleteDiag The diagnostic to be emitted if the expression has |
| 3718 | /// incomplete class type. |
| 3719 | /// |
| 3720 | /// \param ExplicitConvDiag The diagnostic to be emitted if we're calling an |
| 3721 | /// explicit conversion function (because no implicit conversion functions |
| 3722 | /// were available). This is a recovery mode. |
| 3723 | /// |
| 3724 | /// \param ExplicitConvNote The note to be emitted with \p ExplicitConvDiag, |
| 3725 | /// showing which conversion was picked. |
| 3726 | /// |
| 3727 | /// \param AmbigDiag The diagnostic to be emitted if there is more than one |
| 3728 | /// conversion function that could convert to integral or enumeration type. |
| 3729 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3730 | /// \param AmbigNote The note to be emitted with \p AmbigDiag for each |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3731 | /// usable conversion function. |
| 3732 | /// |
| 3733 | /// \param ConvDiag The diagnostic to be emitted if we are calling a conversion |
| 3734 | /// function, which may be an extension in this case. |
| 3735 | /// |
| 3736 | /// \returns The expression, converted to an integral or enumeration type if |
| 3737 | /// successful. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3738 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3739 | Sema::ConvertToIntegralOrEnumerationType(SourceLocation Loc, Expr *From, |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3740 | const PartialDiagnostic &NotIntDiag, |
| 3741 | const PartialDiagnostic &IncompleteDiag, |
| 3742 | const PartialDiagnostic &ExplicitConvDiag, |
| 3743 | const PartialDiagnostic &ExplicitConvNote, |
| 3744 | const PartialDiagnostic &AmbigDiag, |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3745 | const PartialDiagnostic &AmbigNote, |
| 3746 | const PartialDiagnostic &ConvDiag) { |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3747 | // We can't perform any more checking for type-dependent expressions. |
| 3748 | if (From->isTypeDependent()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3749 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3750 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3751 | // If the expression already has integral or enumeration type, we're golden. |
| 3752 | QualType T = From->getType(); |
| 3753 | if (T->isIntegralOrEnumerationType()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3754 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3755 | |
| 3756 | // FIXME: Check for missing '()' if T is a function type? |
| 3757 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3758 | // 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] | 3759 | // expression of integral or enumeration type. |
| 3760 | const RecordType *RecordTy = T->getAs<RecordType>(); |
| 3761 | if (!RecordTy || !getLangOptions().CPlusPlus) { |
| 3762 | Diag(Loc, NotIntDiag) |
| 3763 | << T << From->getSourceRange(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3764 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3765 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3766 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3767 | // We must have a complete class type. |
| 3768 | if (RequireCompleteType(Loc, T, IncompleteDiag)) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3769 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3770 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3771 | // Look for a conversion to an integral or enumeration type. |
| 3772 | UnresolvedSet<4> ViableConversions; |
| 3773 | UnresolvedSet<4> ExplicitConversions; |
| 3774 | const UnresolvedSetImpl *Conversions |
| 3775 | = cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3776 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3777 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3778 | E = Conversions->end(); |
| 3779 | I != E; |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3780 | ++I) { |
| 3781 | if (CXXConversionDecl *Conversion |
| 3782 | = dyn_cast<CXXConversionDecl>((*I)->getUnderlyingDecl())) |
| 3783 | if (Conversion->getConversionType().getNonReferenceType() |
| 3784 | ->isIntegralOrEnumerationType()) { |
| 3785 | if (Conversion->isExplicit()) |
| 3786 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
| 3787 | else |
| 3788 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
| 3789 | } |
| 3790 | } |
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 | switch (ViableConversions.size()) { |
| 3793 | case 0: |
| 3794 | if (ExplicitConversions.size() == 1) { |
| 3795 | DeclAccessPair Found = ExplicitConversions[0]; |
| 3796 | CXXConversionDecl *Conversion |
| 3797 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3798 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3799 | // The user probably meant to invoke the given explicit |
| 3800 | // conversion; use it. |
| 3801 | QualType ConvTy |
| 3802 | = Conversion->getConversionType().getNonReferenceType(); |
| 3803 | std::string TypeStr; |
| 3804 | ConvTy.getAsStringInternal(TypeStr, Context.PrintingPolicy); |
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 | Diag(Loc, ExplicitConvDiag) |
| 3807 | << T << ConvTy |
| 3808 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 3809 | "static_cast<" + TypeStr + ">(") |
| 3810 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(From->getLocEnd()), |
| 3811 | ")"); |
| 3812 | Diag(Conversion->getLocation(), ExplicitConvNote) |
| 3813 | << ConvTy->isEnumeralType() << ConvTy; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3814 | |
| 3815 | // 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] | 3816 | // explicit conversion function. |
| 3817 | if (isSFINAEContext()) |
| 3818 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3819 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3820 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3821 | ExprResult Result = BuildCXXMemberCallExpr(From, Found, Conversion); |
| 3822 | if (Result.isInvalid()) |
| 3823 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3824 | |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3825 | From = Result.get(); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3826 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3827 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3828 | // We'll complain below about a non-integral condition type. |
| 3829 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3830 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3831 | case 1: { |
| 3832 | // Apply this conversion. |
| 3833 | DeclAccessPair Found = ViableConversions[0]; |
| 3834 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3835 | |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3836 | CXXConversionDecl *Conversion |
| 3837 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 3838 | QualType ConvTy |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3839 | = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3840 | if (ConvDiag.getDiagID()) { |
| 3841 | if (isSFINAEContext()) |
| 3842 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3843 | |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3844 | Diag(Loc, ConvDiag) |
| 3845 | << T << ConvTy->isEnumeralType() << ConvTy << From->getSourceRange(); |
| 3846 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3847 | |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3848 | ExprResult Result = BuildCXXMemberCallExpr(From, Found, |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3849 | cast<CXXConversionDecl>(Found->getUnderlyingDecl())); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3850 | if (Result.isInvalid()) |
| 3851 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3852 | |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3853 | From = Result.get(); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3854 | break; |
| 3855 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3856 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3857 | default: |
| 3858 | Diag(Loc, AmbigDiag) |
| 3859 | << T << From->getSourceRange(); |
| 3860 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 3861 | CXXConversionDecl *Conv |
| 3862 | = cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 3863 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 3864 | Diag(Conv->getLocation(), AmbigNote) |
| 3865 | << ConvTy->isEnumeralType() << ConvTy; |
| 3866 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3867 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3868 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3869 | |
Douglas Gregor | acb0bd8 | 2010-06-29 23:25:20 +0000 | [diff] [blame] | 3870 | if (!From->getType()->isIntegralOrEnumerationType()) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3871 | Diag(Loc, NotIntDiag) |
| 3872 | << From->getType() << From->getSourceRange(); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3873 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3874 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3875 | } |
| 3876 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3877 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3878 | /// candidate functions, using the given function call arguments. If |
| 3879 | /// @p SuppressUserConversions, then don't allow user-defined |
| 3880 | /// conversions via constructors or conversion operators. |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3881 | /// |
| 3882 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 3883 | /// based on an incomplete set of function arguments. This feature is used by |
| 3884 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3885 | void |
| 3886 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3887 | DeclAccessPair FoundDecl, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3888 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3889 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 3890 | bool SuppressUserConversions, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3891 | bool PartialOverloading) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3892 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3893 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3894 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3895 | assert(!Function->getDescribedFunctionTemplate() && |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3896 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3897 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3898 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3899 | if (!isa<CXXConstructorDecl>(Method)) { |
| 3900 | // If we get here, it's because we're calling a member function |
| 3901 | // that is named without a member access expression (e.g., |
| 3902 | // "this->f") that was either written explicitly or created |
| 3903 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3904 | // function, e.g., X::f(). We use an empty type for the implied |
| 3905 | // object argument (C++ [over.call.func]p3), and the acting context |
| 3906 | // is irrelevant. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3907 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3908 | QualType(), Expr::Classification::makeSimpleLValue(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3909 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3910 | SuppressUserConversions); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3911 | return; |
| 3912 | } |
| 3913 | // We treat a constructor like a non-member function, since its object |
| 3914 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3915 | } |
| 3916 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 3917 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3918 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3919 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3920 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3921 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3922 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3923 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 3924 | // C++ [class.copy]p3: |
| 3925 | // A member function template is never instantiated to perform the copy |
| 3926 | // of a class object to an object of its class type. |
| 3927 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3928 | if (NumArgs == 1 && |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3929 | Constructor->isSpecializationCopyingObject() && |
Douglas Gregor | 1211606 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 3930 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 3931 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3932 | return; |
| 3933 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3934 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3935 | // Add this candidate |
| 3936 | CandidateSet.push_back(OverloadCandidate()); |
| 3937 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3938 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3939 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3940 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3941 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3942 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 3943 | Candidate.ExplicitCallArguments = NumArgs; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3944 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3945 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3946 | |
| 3947 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3948 | // parameters is viable only if it has an ellipsis in its parameter |
| 3949 | // list (8.3.5). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3950 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 3951 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3952 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3953 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3954 | return; |
| 3955 | } |
| 3956 | |
| 3957 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 3958 | // is viable only if the (m+1)st parameter has a default argument |
| 3959 | // (8.3.6). For the purposes of overload resolution, the |
| 3960 | // parameter list is truncated on the right, so that there are |
| 3961 | // exactly m parameters. |
| 3962 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3963 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3964 | // Not enough arguments. |
| 3965 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3966 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3967 | return; |
| 3968 | } |
| 3969 | |
| 3970 | // Determine the implicit conversion sequences for each of the |
| 3971 | // arguments. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3972 | Candidate.Conversions.resize(NumArgs); |
| 3973 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3974 | if (ArgIdx < NumArgsInProto) { |
| 3975 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3976 | // exist for each argument an implicit conversion sequence |
| 3977 | // (13.3.3.1) that converts that argument to the corresponding |
| 3978 | // parameter of F. |
| 3979 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3980 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3981 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3982 | SuppressUserConversions, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3983 | /*InOverloadResolution=*/true); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3984 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 3985 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3986 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3987 | break; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3988 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3989 | } else { |
| 3990 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3991 | // argument for which there is no corresponding parameter is |
| 3992 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3993 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3994 | } |
| 3995 | } |
| 3996 | } |
| 3997 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3998 | /// \brief Add all of the function declarations in the given function set to |
| 3999 | /// the overload canddiate set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4000 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4001 | Expr **Args, unsigned NumArgs, |
| 4002 | OverloadCandidateSet& CandidateSet, |
| 4003 | bool SuppressUserConversions) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4004 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4005 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 4006 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4007 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4008 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4009 | cast<CXXMethodDecl>(FD)->getParent(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4010 | Args[0]->getType(), Args[0]->Classify(Context), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4011 | Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4012 | CandidateSet, SuppressUserConversions); |
| 4013 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4014 | AddOverloadCandidate(FD, F.getPair(), Args, NumArgs, CandidateSet, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4015 | SuppressUserConversions); |
| 4016 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4017 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4018 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 4019 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4020 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4021 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4022 | /*FIXME: explicit args */ 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4023 | Args[0]->getType(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4024 | Args[0]->Classify(Context), |
| 4025 | Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4026 | CandidateSet, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4027 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4028 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4029 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4030 | /*FIXME: explicit args */ 0, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4031 | Args, NumArgs, CandidateSet, |
| 4032 | SuppressUserConversions); |
| 4033 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4034 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4035 | } |
| 4036 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4037 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 4038 | /// method) as a method candidate to the given overload set. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4039 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4040 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4041 | Expr::Classification ObjectClassification, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4042 | Expr **Args, unsigned NumArgs, |
| 4043 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4044 | bool SuppressUserConversions) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4045 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4046 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4047 | |
| 4048 | if (isa<UsingShadowDecl>(Decl)) |
| 4049 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4050 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4051 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 4052 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 4053 | "Expected a member function template"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4054 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 4055 | /*ExplicitArgs*/ 0, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4056 | ObjectType, ObjectClassification, Args, NumArgs, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4057 | CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4058 | SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4059 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4060 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4061 | ObjectType, ObjectClassification, Args, NumArgs, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4062 | CandidateSet, SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4063 | } |
| 4064 | } |
| 4065 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4066 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 4067 | /// of candidate functions, using the given function call arguments |
| 4068 | /// and the object argument (@c Object). For example, in a call |
| 4069 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 4070 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 4071 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4072 | /// operators. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4073 | void |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4074 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4075 | CXXRecordDecl *ActingContext, QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4076 | Expr::Classification ObjectClassification, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4077 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4078 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4079 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4080 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4081 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4082 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4083 | assert(!isa<CXXConstructorDecl>(Method) && |
| 4084 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4085 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4086 | if (!CandidateSet.isNewCandidate(Method)) |
| 4087 | return; |
| 4088 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4089 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4090 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4091 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4092 | // Add this candidate |
| 4093 | CandidateSet.push_back(OverloadCandidate()); |
| 4094 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4095 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4096 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4097 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4098 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4099 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4100 | |
| 4101 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4102 | |
| 4103 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 4104 | // parameters is viable only if it has an ellipsis in its parameter |
| 4105 | // list (8.3.5). |
| 4106 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 4107 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4108 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4109 | return; |
| 4110 | } |
| 4111 | |
| 4112 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 4113 | // is viable only if the (m+1)st parameter has a default argument |
| 4114 | // (8.3.6). For the purposes of overload resolution, the |
| 4115 | // parameter list is truncated on the right, so that there are |
| 4116 | // exactly m parameters. |
| 4117 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 4118 | if (NumArgs < MinRequiredArgs) { |
| 4119 | // Not enough arguments. |
| 4120 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4121 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4122 | return; |
| 4123 | } |
| 4124 | |
| 4125 | Candidate.Viable = true; |
| 4126 | Candidate.Conversions.resize(NumArgs + 1); |
| 4127 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4128 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4129 | // The implicit object argument is ignored. |
| 4130 | Candidate.IgnoreObjectArgument = true; |
| 4131 | else { |
| 4132 | // Determine the implicit conversion sequence for the object |
| 4133 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4134 | Candidate.Conversions[0] |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4135 | = TryObjectArgumentInitialization(*this, ObjectType, ObjectClassification, |
| 4136 | Method, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4137 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4138 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4139 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4140 | return; |
| 4141 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4142 | } |
| 4143 | |
| 4144 | // Determine the implicit conversion sequences for each of the |
| 4145 | // arguments. |
| 4146 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 4147 | if (ArgIdx < NumArgsInProto) { |
| 4148 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 4149 | // exist for each argument an implicit conversion sequence |
| 4150 | // (13.3.3.1) that converts that argument to the corresponding |
| 4151 | // parameter of F. |
| 4152 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4153 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4154 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4155 | SuppressUserConversions, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 4156 | /*InOverloadResolution=*/true); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4157 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4158 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4159 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4160 | break; |
| 4161 | } |
| 4162 | } else { |
| 4163 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 4164 | // argument for which there is no corresponding parameter is |
| 4165 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4166 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4167 | } |
| 4168 | } |
| 4169 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4170 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4171 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 4172 | /// set, using template argument deduction to produce an appropriate member |
| 4173 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4174 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4175 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4176 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4177 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4178 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4179 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4180 | Expr::Classification ObjectClassification, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4181 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4182 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4183 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4184 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 4185 | return; |
| 4186 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4187 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4188 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4189 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4190 | // 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] | 4191 | // candidate functions in the usual way.113) A given name can refer to one |
| 4192 | // or more function templates and also to a set of overloaded non-template |
| 4193 | // functions. In such a case, the candidate functions generated from each |
| 4194 | // function template are combined with the set of non-template candidate |
| 4195 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4196 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4197 | FunctionDecl *Specialization = 0; |
| 4198 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4199 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4200 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4201 | CandidateSet.push_back(OverloadCandidate()); |
| 4202 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 4203 | Candidate.FoundDecl = FoundDecl; |
| 4204 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 4205 | Candidate.Viable = false; |
| 4206 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 4207 | Candidate.IsSurrogate = false; |
| 4208 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4209 | Candidate.ExplicitCallArguments = NumArgs; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4210 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4211 | Info); |
| 4212 | return; |
| 4213 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4214 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4215 | // Add the function template specialization produced by template argument |
| 4216 | // deduction as a candidate. |
| 4217 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4218 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4219 | "Specialization is not a member function?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4220 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4221 | ActingContext, ObjectType, ObjectClassification, |
| 4222 | Args, NumArgs, CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4223 | } |
| 4224 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4225 | /// \brief Add a C++ function template specialization as a candidate |
| 4226 | /// in the candidate set, using template argument deduction to produce |
| 4227 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4228 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4229 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4230 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4231 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4232 | Expr **Args, unsigned NumArgs, |
| 4233 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4234 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4235 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 4236 | return; |
| 4237 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4238 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4239 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4240 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4241 | // 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] | 4242 | // candidate functions in the usual way.113) A given name can refer to one |
| 4243 | // or more function templates and also to a set of overloaded non-template |
| 4244 | // functions. In such a case, the candidate functions generated from each |
| 4245 | // function template are combined with the set of non-template candidate |
| 4246 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4247 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4248 | FunctionDecl *Specialization = 0; |
| 4249 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4250 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4251 | Args, NumArgs, Specialization, Info)) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4252 | CandidateSet.push_back(OverloadCandidate()); |
| 4253 | OverloadCandidate &Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4254 | Candidate.FoundDecl = FoundDecl; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4255 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 4256 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4257 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4258 | Candidate.IsSurrogate = false; |
| 4259 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4260 | Candidate.ExplicitCallArguments = NumArgs; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4261 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4262 | Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4263 | return; |
| 4264 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4265 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4266 | // Add the function template specialization produced by template argument |
| 4267 | // deduction as a candidate. |
| 4268 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4269 | AddOverloadCandidate(Specialization, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4270 | SuppressUserConversions); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4271 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4272 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4273 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4274 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4275 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4276 | /// 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] | 4277 | /// (which may or may not be the same type as the type that the |
| 4278 | /// conversion function produces). |
| 4279 | void |
| 4280 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4281 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4282 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4283 | Expr *From, QualType ToType, |
| 4284 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4285 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 4286 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4287 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4288 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 4289 | return; |
| 4290 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4291 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4292 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4293 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4294 | // Add this candidate |
| 4295 | CandidateSet.push_back(OverloadCandidate()); |
| 4296 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4297 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4298 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4299 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4300 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4301 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4302 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 4303 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4304 | Candidate.Viable = true; |
| 4305 | Candidate.Conversions.resize(1); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4306 | Candidate.ExplicitCallArguments = 1; |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4307 | |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 4308 | // C++ [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4309 | // For conversion functions, the function is considered to be a member of |
| 4310 | // the class of the implicit implied object argument for the purpose of |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 4311 | // defining the type of the implicit object parameter. |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4312 | // |
| 4313 | // Determine the implicit conversion sequence for the implicit |
| 4314 | // object parameter. |
| 4315 | QualType ImplicitParamType = From->getType(); |
| 4316 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
| 4317 | ImplicitParamType = FromPtrType->getPointeeType(); |
| 4318 | CXXRecordDecl *ConversionContext |
| 4319 | = cast<CXXRecordDecl>(ImplicitParamType->getAs<RecordType>()->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4320 | |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4321 | Candidate.Conversions[0] |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4322 | = TryObjectArgumentInitialization(*this, From->getType(), |
| 4323 | From->Classify(Context), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4324 | Conversion, ConversionContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4325 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4326 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4327 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4328 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4329 | return; |
| 4330 | } |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4331 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4332 | // 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] | 4333 | // derived to base as such conversions are given Conversion Rank. They only |
| 4334 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 4335 | QualType FromCanon |
| 4336 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 4337 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 4338 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 4339 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4340 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 4341 | return; |
| 4342 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4343 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4344 | // To determine what the conversion from the result of calling the |
| 4345 | // conversion function to the type we're eventually trying to |
| 4346 | // convert to (ToType), we need to synthesize a call to the |
| 4347 | // conversion function and attempt copy initialization from it. This |
| 4348 | // makes sure that we get the right semantics with respect to |
| 4349 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 4350 | // call on the stack and we don't need its arguments to be |
| 4351 | // well-formed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4352 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4353 | VK_LValue, From->getLocStart()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4354 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 4355 | Context.getPointerType(Conversion->getType()), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4356 | CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4357 | &ConversionRef, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4358 | |
Douglas Gregor | 7d14d38 | 2010-11-13 19:36:57 +0000 | [diff] [blame] | 4359 | QualType CallResultType |
| 4360 | = Conversion->getConversionType().getNonLValueExprType(Context); |
| 4361 | if (RequireCompleteType(From->getLocStart(), CallResultType, 0)) { |
| 4362 | Candidate.Viable = false; |
| 4363 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 4364 | return; |
| 4365 | } |
| 4366 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4367 | ExprValueKind VK = Expr::getValueKindForType(Conversion->getConversionType()); |
| 4368 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4369 | // 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] | 4370 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 4371 | // allocator). |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4372 | CallExpr Call(Context, &ConversionFn, 0, 0, CallResultType, VK, |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 4373 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4374 | ImplicitConversionSequence ICS = |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4375 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 4376 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 4377 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4378 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4379 | switch (ICS.getKind()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4380 | case ImplicitConversionSequence::StandardConversion: |
| 4381 | Candidate.FinalConversion = ICS.Standard; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4382 | |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 4383 | // C++ [over.ics.user]p3: |
| 4384 | // If the user-defined conversion is specified by a specialization of a |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4385 | // conversion function template, the second standard conversion sequence |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 4386 | // shall have exact match rank. |
| 4387 | if (Conversion->getPrimaryTemplate() && |
| 4388 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 4389 | Candidate.Viable = false; |
| 4390 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 4391 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4392 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4393 | // C++0x [dcl.init.ref]p5: |
| 4394 | // In the second case, if the reference is an rvalue reference and |
| 4395 | // the second standard conversion sequence of the user-defined |
| 4396 | // conversion sequence includes an lvalue-to-rvalue conversion, the |
| 4397 | // program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4398 | if (ToType->isRValueReferenceType() && |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4399 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 4400 | Candidate.Viable = false; |
| 4401 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 4402 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4403 | break; |
| 4404 | |
| 4405 | case ImplicitConversionSequence::BadConversion: |
| 4406 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4407 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4408 | break; |
| 4409 | |
| 4410 | default: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4411 | assert(false && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4412 | "Can only end up with a standard conversion sequence or failure"); |
| 4413 | } |
| 4414 | } |
| 4415 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4416 | /// \brief Adds a conversion function template specialization |
| 4417 | /// candidate to the overload set, using template argument deduction |
| 4418 | /// to deduce the template arguments of the conversion function |
| 4419 | /// template from the type that we are converting to (C++ |
| 4420 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4421 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4422 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4423 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4424 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4425 | Expr *From, QualType ToType, |
| 4426 | OverloadCandidateSet &CandidateSet) { |
| 4427 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 4428 | "Only conversion function templates permitted here"); |
| 4429 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4430 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 4431 | return; |
| 4432 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4433 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4434 | CXXConversionDecl *Specialization = 0; |
| 4435 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4436 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4437 | Specialization, Info)) { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4438 | CandidateSet.push_back(OverloadCandidate()); |
| 4439 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 4440 | Candidate.FoundDecl = FoundDecl; |
| 4441 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 4442 | Candidate.Viable = false; |
| 4443 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 4444 | Candidate.IsSurrogate = false; |
| 4445 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4446 | Candidate.ExplicitCallArguments = 1; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4447 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4448 | Info); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4449 | return; |
| 4450 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4451 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4452 | // Add the conversion function template specialization produced by |
| 4453 | // template argument deduction as a candidate. |
| 4454 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4455 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4456 | CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4457 | } |
| 4458 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4459 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 4460 | /// converts the given @c Object to a function pointer via the |
| 4461 | /// conversion function @c Conversion, and then attempts to call it |
| 4462 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 4463 | /// the type of function that we'll eventually be calling. |
| 4464 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4465 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4466 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4467 | const FunctionProtoType *Proto, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4468 | Expr *Object, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4469 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4470 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4471 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 4472 | return; |
| 4473 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4474 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4475 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4476 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4477 | CandidateSet.push_back(OverloadCandidate()); |
| 4478 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4479 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4480 | Candidate.Function = 0; |
| 4481 | Candidate.Surrogate = Conversion; |
| 4482 | Candidate.Viable = true; |
| 4483 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4484 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4485 | Candidate.Conversions.resize(NumArgs + 1); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4486 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4487 | |
| 4488 | // Determine the implicit conversion sequence for the implicit |
| 4489 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4490 | ImplicitConversionSequence ObjectInit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4491 | = TryObjectArgumentInitialization(*this, Object->getType(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4492 | Object->Classify(Context), |
| 4493 | Conversion, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4494 | if (ObjectInit.isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4495 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4496 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4497 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4498 | return; |
| 4499 | } |
| 4500 | |
| 4501 | // The first conversion is actually a user-defined conversion whose |
| 4502 | // first conversion is ObjectInit's standard conversion (which is |
| 4503 | // effectively a reference binding). Record it as such. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4504 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4505 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 4506 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4507 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4508 | Candidate.Conversions[0].UserDefined.FoundConversionFunction |
Douglas Gregor | 83eecbe | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 4509 | = FoundDecl.getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4510 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4511 | = Candidate.Conversions[0].UserDefined.Before; |
| 4512 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 4513 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4514 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4515 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4516 | |
| 4517 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 4518 | // parameters is viable only if it has an ellipsis in its parameter |
| 4519 | // list (8.3.5). |
| 4520 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 4521 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4522 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4523 | return; |
| 4524 | } |
| 4525 | |
| 4526 | // Function types don't have any default arguments, so just check if |
| 4527 | // we have enough arguments. |
| 4528 | if (NumArgs < NumArgsInProto) { |
| 4529 | // Not enough arguments. |
| 4530 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4531 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4532 | return; |
| 4533 | } |
| 4534 | |
| 4535 | // Determine the implicit conversion sequences for each of the |
| 4536 | // arguments. |
| 4537 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 4538 | if (ArgIdx < NumArgsInProto) { |
| 4539 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 4540 | // exist for each argument an implicit conversion sequence |
| 4541 | // (13.3.3.1) that converts that argument to the corresponding |
| 4542 | // parameter of F. |
| 4543 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4544 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4545 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 4546 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 4547 | /*InOverloadResolution=*/false); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4548 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4549 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4550 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4551 | break; |
| 4552 | } |
| 4553 | } else { |
| 4554 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 4555 | // argument for which there is no corresponding parameter is |
| 4556 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4557 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4558 | } |
| 4559 | } |
| 4560 | } |
| 4561 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4562 | /// \brief Add overload candidates for overloaded operators that are |
| 4563 | /// member functions. |
| 4564 | /// |
| 4565 | /// Add the overloaded operator candidates that are member functions |
| 4566 | /// for the operator Op that was used in an operator expression such |
| 4567 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 4568 | /// CandidateSet will store the added overload candidates. (C++ |
| 4569 | /// [over.match.oper]). |
| 4570 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 4571 | SourceLocation OpLoc, |
| 4572 | Expr **Args, unsigned NumArgs, |
| 4573 | OverloadCandidateSet& CandidateSet, |
| 4574 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4575 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4576 | |
| 4577 | // C++ [over.match.oper]p3: |
| 4578 | // For a unary operator @ with an operand of a type whose |
| 4579 | // cv-unqualified version is T1, and for a binary operator @ with |
| 4580 | // a left operand of a type whose cv-unqualified version is T1 and |
| 4581 | // a right operand of a type whose cv-unqualified version is T2, |
| 4582 | // three sets of candidate functions, designated member |
| 4583 | // candidates, non-member candidates and built-in candidates, are |
| 4584 | // constructed as follows: |
| 4585 | QualType T1 = Args[0]->getType(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4586 | |
| 4587 | // -- If T1 is a class type, the set of member candidates is the |
| 4588 | // result of the qualified lookup of T1::operator@ |
| 4589 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 4590 | // empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4591 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 4592 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 4593 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 4594 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4595 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4596 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 4597 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 4598 | Operators.suppressDiagnostics(); |
| 4599 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4600 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 4601 | OperEnd = Operators.end(); |
| 4602 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4603 | ++Oper) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4604 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4605 | Args[0]->Classify(Context), Args + 1, NumArgs - 1, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4606 | CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4607 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4608 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4611 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 4612 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 4613 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4614 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 4615 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4616 | /// operator. NumContextualBoolArguments is the number of arguments |
| 4617 | /// (at the beginning of the argument list) that will be contextually |
| 4618 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4619 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4620 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4621 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4622 | bool IsAssignmentOperator, |
| 4623 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4624 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4625 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4626 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4627 | // Add this candidate |
| 4628 | CandidateSet.push_back(OverloadCandidate()); |
| 4629 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4630 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4631 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 4632 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4633 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4634 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 4635 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4636 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 4637 | |
| 4638 | // Determine the implicit conversion sequences for each of the |
| 4639 | // arguments. |
| 4640 | Candidate.Viable = true; |
| 4641 | Candidate.Conversions.resize(NumArgs); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4642 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4643 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4644 | // C++ [over.match.oper]p4: |
| 4645 | // For the built-in assignment operators, conversions of the |
| 4646 | // left operand are restricted as follows: |
| 4647 | // -- no temporaries are introduced to hold the left operand, and |
| 4648 | // -- no user-defined conversions are applied to the left |
| 4649 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4650 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4651 | // |
| 4652 | // We block these conversions by turning off user-defined |
| 4653 | // conversions, since that is the only way that initialization of |
| 4654 | // a reference to a non-class type can occur from something that |
| 4655 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4656 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4657 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4658 | "Contextual conversion to bool requires bool type"); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4659 | Candidate.Conversions[ArgIdx] |
| 4660 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4661 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4662 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4663 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 4664 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 4665 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4666 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4667 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4668 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4669 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4670 | break; |
| 4671 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4672 | } |
| 4673 | } |
| 4674 | |
| 4675 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 4676 | /// candidate operator functions for built-in operators (C++ |
| 4677 | /// [over.built]). The types are separated into pointer types and |
| 4678 | /// enumeration types. |
| 4679 | class BuiltinCandidateTypeSet { |
| 4680 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4681 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4682 | |
| 4683 | /// PointerTypes - The set of pointer types that will be used in the |
| 4684 | /// built-in candidates. |
| 4685 | TypeSet PointerTypes; |
| 4686 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4687 | /// MemberPointerTypes - The set of member pointer types that will be |
| 4688 | /// used in the built-in candidates. |
| 4689 | TypeSet MemberPointerTypes; |
| 4690 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4691 | /// EnumerationTypes - The set of enumeration types that will be |
| 4692 | /// used in the built-in candidates. |
| 4693 | TypeSet EnumerationTypes; |
| 4694 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4695 | /// \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] | 4696 | /// candidates. |
| 4697 | TypeSet VectorTypes; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4698 | |
| 4699 | /// \brief A flag indicating non-record types are viable candidates |
| 4700 | bool HasNonRecordTypes; |
| 4701 | |
| 4702 | /// \brief A flag indicating whether either arithmetic or enumeration types |
| 4703 | /// were present in the candidate set. |
| 4704 | bool HasArithmeticOrEnumeralTypes; |
| 4705 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4706 | /// \brief A flag indicating whether the nullptr type was present in the |
| 4707 | /// candidate set. |
| 4708 | bool HasNullPtrType; |
| 4709 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4710 | /// Sema - The semantic analysis instance where we are building the |
| 4711 | /// candidate type set. |
| 4712 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4713 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4714 | /// Context - The AST context in which we will build the type sets. |
| 4715 | ASTContext &Context; |
| 4716 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4717 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 4718 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4719 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4720 | |
| 4721 | public: |
| 4722 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4723 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4724 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4725 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4726 | : HasNonRecordTypes(false), |
| 4727 | HasArithmeticOrEnumeralTypes(false), |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4728 | HasNullPtrType(false), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4729 | SemaRef(SemaRef), |
| 4730 | Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4731 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4732 | void AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4733 | SourceLocation Loc, |
| 4734 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4735 | bool AllowExplicitConversions, |
| 4736 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4737 | |
| 4738 | /// pointer_begin - First pointer type found; |
| 4739 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 4740 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4741 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4742 | iterator pointer_end() { return PointerTypes.end(); } |
| 4743 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4744 | /// member_pointer_begin - First member pointer type found; |
| 4745 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 4746 | |
| 4747 | /// member_pointer_end - Past the last member pointer type found; |
| 4748 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 4749 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4750 | /// enumeration_begin - First enumeration type found; |
| 4751 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 4752 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4753 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4754 | iterator enumeration_end() { return EnumerationTypes.end(); } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4755 | |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4756 | iterator vector_begin() { return VectorTypes.begin(); } |
| 4757 | iterator vector_end() { return VectorTypes.end(); } |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4758 | |
| 4759 | bool hasNonRecordTypes() { return HasNonRecordTypes; } |
| 4760 | bool hasArithmeticOrEnumeralTypes() { return HasArithmeticOrEnumeralTypes; } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4761 | bool hasNullPtrType() const { return HasNullPtrType; } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4762 | }; |
| 4763 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4764 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4765 | /// the set of pointer types along with any more-qualified variants of |
| 4766 | /// that type. For example, if @p Ty is "int const *", this routine |
| 4767 | /// will add "int const *", "int const volatile *", "int const |
| 4768 | /// restrict *", and "int const volatile restrict *" to the set of |
| 4769 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 4770 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4771 | /// |
| 4772 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4773 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4774 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 4775 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4776 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4777 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4778 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4779 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4780 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4781 | QualType PointeeTy; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4782 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4783 | bool buildObjCPtr = false; |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4784 | if (!PointerTy) { |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4785 | if (const ObjCObjectPointerType *PTy = Ty->getAs<ObjCObjectPointerType>()) { |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4786 | PointeeTy = PTy->getPointeeType(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4787 | buildObjCPtr = true; |
| 4788 | } |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4789 | else |
| 4790 | assert(false && "type was not a pointer type!"); |
| 4791 | } |
| 4792 | else |
| 4793 | PointeeTy = PointerTy->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4794 | |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 4795 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 4796 | // (the qualifier would sink to the element type), and for another, the |
| 4797 | // only overload situation where it matters is subscript or pointer +- int, |
| 4798 | // and those shouldn't have qualifier variants anyway. |
| 4799 | if (PointeeTy->isArrayType()) |
| 4800 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4801 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 4802 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | d411b3f | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 4803 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4804 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 4805 | bool hasRestrict = VisibleQuals.hasRestrict(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4806 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4807 | // Iterate through all strict supersets of BaseCVR. |
| 4808 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 4809 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4810 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 4811 | // in the types. |
| 4812 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 4813 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4814 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4815 | if (!buildObjCPtr) |
| 4816 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
| 4817 | else |
| 4818 | PointerTypes.insert(Context.getObjCObjectPointerType(QPointeeTy)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4819 | } |
| 4820 | |
| 4821 | return true; |
| 4822 | } |
| 4823 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4824 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 4825 | /// to the set of pointer types along with any more-qualified variants of |
| 4826 | /// that type. For example, if @p Ty is "int const *", this routine |
| 4827 | /// will add "int const *", "int const volatile *", "int const |
| 4828 | /// restrict *", and "int const volatile restrict *" to the set of |
| 4829 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 4830 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4831 | /// |
| 4832 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4833 | bool |
| 4834 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 4835 | QualType Ty) { |
| 4836 | // Insert this type. |
| 4837 | if (!MemberPointerTypes.insert(Ty)) |
| 4838 | return false; |
| 4839 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4840 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 4841 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4842 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4843 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 4844 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 4845 | // (the qualifier would sink to the element type), and for another, the |
| 4846 | // only overload situation where it matters is subscript or pointer +- int, |
| 4847 | // and those shouldn't have qualifier variants anyway. |
| 4848 | if (PointeeTy->isArrayType()) |
| 4849 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4850 | const Type *ClassTy = PointerTy->getClass(); |
| 4851 | |
| 4852 | // Iterate through all strict supersets of the pointee type's CVR |
| 4853 | // qualifiers. |
| 4854 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 4855 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 4856 | if ((CVR | BaseCVR) != CVR) continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4857 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4858 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4859 | MemberPointerTypes.insert( |
| 4860 | Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4861 | } |
| 4862 | |
| 4863 | return true; |
| 4864 | } |
| 4865 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4866 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 4867 | /// 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] | 4868 | /// primarily interested in pointer types and enumeration types. We also |
| 4869 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4870 | /// AllowUserConversions is true if we should look at the conversion |
| 4871 | /// functions of a class type, and AllowExplicitConversions if we |
| 4872 | /// should also include the explicit conversion functions of a class |
| 4873 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4874 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4875 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4876 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4877 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4878 | bool AllowExplicitConversions, |
| 4879 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4880 | // Only deal with canonical types. |
| 4881 | Ty = Context.getCanonicalType(Ty); |
| 4882 | |
| 4883 | // Look through reference types; they aren't part of the type of an |
| 4884 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4885 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4886 | Ty = RefTy->getPointeeType(); |
| 4887 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4888 | // If we're dealing with an array type, decay to the pointer. |
| 4889 | if (Ty->isArrayType()) |
| 4890 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 4891 | |
| 4892 | // Otherwise, we don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4893 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4894 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4895 | // Flag if we ever add a non-record type. |
| 4896 | const RecordType *TyRec = Ty->getAs<RecordType>(); |
| 4897 | HasNonRecordTypes = HasNonRecordTypes || !TyRec; |
| 4898 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4899 | // Flag if we encounter an arithmetic type. |
| 4900 | HasArithmeticOrEnumeralTypes = |
| 4901 | HasArithmeticOrEnumeralTypes || Ty->isArithmeticType(); |
| 4902 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4903 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
| 4904 | PointerTypes.insert(Ty); |
| 4905 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4906 | // Insert our type, and its more-qualified variants, into the set |
| 4907 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4908 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4909 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4910 | } else if (Ty->isMemberPointerType()) { |
| 4911 | // Member pointers are far easier, since the pointee can't be converted. |
| 4912 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 4913 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4914 | } else if (Ty->isEnumeralType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4915 | HasArithmeticOrEnumeralTypes = true; |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4916 | EnumerationTypes.insert(Ty); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4917 | } else if (Ty->isVectorType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4918 | // We treat vector types as arithmetic types in many contexts as an |
| 4919 | // extension. |
| 4920 | HasArithmeticOrEnumeralTypes = true; |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4921 | VectorTypes.insert(Ty); |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4922 | } else if (Ty->isNullPtrType()) { |
| 4923 | HasNullPtrType = true; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4924 | } else if (AllowUserConversions && TyRec) { |
| 4925 | // No conversion functions in incomplete types. |
| 4926 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) |
| 4927 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4928 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4929 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| 4930 | const UnresolvedSetImpl *Conversions |
| 4931 | = ClassDecl->getVisibleConversionFunctions(); |
| 4932 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 4933 | E = Conversions->end(); I != E; ++I) { |
| 4934 | NamedDecl *D = I.getDecl(); |
| 4935 | if (isa<UsingShadowDecl>(D)) |
| 4936 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4937 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4938 | // Skip conversion function templates; they don't tell us anything |
| 4939 | // about which builtin types we can convert to. |
| 4940 | if (isa<FunctionTemplateDecl>(D)) |
| 4941 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4942 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4943 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
| 4944 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
| 4945 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
| 4946 | VisibleQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4947 | } |
| 4948 | } |
| 4949 | } |
| 4950 | } |
| 4951 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4952 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 4953 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 4954 | /// given type to the candidate set. |
| 4955 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 4956 | QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4957 | Expr **Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4958 | unsigned NumArgs, |
| 4959 | OverloadCandidateSet &CandidateSet) { |
| 4960 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4961 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4962 | // T& operator=(T&, T) |
| 4963 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 4964 | ParamTypes[1] = T; |
| 4965 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4966 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4967 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4968 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 4969 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4970 | ParamTypes[0] |
| 4971 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4972 | ParamTypes[1] = T; |
| 4973 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4974 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4975 | } |
| 4976 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4977 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 4978 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 4979 | /// 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] | 4980 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 4981 | Qualifiers VRQuals; |
| 4982 | const RecordType *TyRec; |
| 4983 | if (const MemberPointerType *RHSMPType = |
| 4984 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4985 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4986 | else |
| 4987 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 4988 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4989 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4990 | VRQuals.addVolatile(); |
| 4991 | VRQuals.addRestrict(); |
| 4992 | return VRQuals; |
| 4993 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4994 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4995 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4996 | if (!ClassDecl->hasDefinition()) |
| 4997 | return VRQuals; |
| 4998 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4999 | const UnresolvedSetImpl *Conversions = |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 5000 | ClassDecl->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5001 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 5002 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5003 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 5004 | NamedDecl *D = I.getDecl(); |
| 5005 | if (isa<UsingShadowDecl>(D)) |
| 5006 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 5007 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5008 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 5009 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 5010 | CanTy = ResTypeRef->getPointeeType(); |
| 5011 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 5012 | // as see them. |
| 5013 | bool done = false; |
| 5014 | while (!done) { |
| 5015 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 5016 | CanTy = ResTypePtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5017 | else if (const MemberPointerType *ResTypeMPtr = |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5018 | CanTy->getAs<MemberPointerType>()) |
| 5019 | CanTy = ResTypeMPtr->getPointeeType(); |
| 5020 | else |
| 5021 | done = true; |
| 5022 | if (CanTy.isVolatileQualified()) |
| 5023 | VRQuals.addVolatile(); |
| 5024 | if (CanTy.isRestrictQualified()) |
| 5025 | VRQuals.addRestrict(); |
| 5026 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 5027 | return VRQuals; |
| 5028 | } |
| 5029 | } |
| 5030 | } |
| 5031 | return VRQuals; |
| 5032 | } |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 5033 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5034 | namespace { |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 5035 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5036 | /// \brief Helper class to manage the addition of builtin operator overload |
| 5037 | /// candidates. It provides shared state and utility methods used throughout |
| 5038 | /// the process, as well as a helper method to add each group of builtin |
| 5039 | /// operator overloads from the standard to a candidate set. |
| 5040 | class BuiltinOperatorOverloadBuilder { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5041 | // Common instance state available to all overload candidate addition methods. |
| 5042 | Sema &S; |
| 5043 | Expr **Args; |
| 5044 | unsigned NumArgs; |
| 5045 | Qualifiers VisibleTypeConversionsQuals; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5046 | bool HasArithmeticOrEnumeralCandidateType; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5047 | llvm::SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes; |
| 5048 | OverloadCandidateSet &CandidateSet; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5049 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5050 | // Define some constants used to index and iterate over the arithemetic types |
| 5051 | // provided via the getArithmeticType() method below. |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 5052 | // The "promoted arithmetic types" are the arithmetic |
| 5053 | // types are that preserved by promotion (C++ [over.built]p2). |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 5054 | static const unsigned FirstIntegralType = 3; |
| 5055 | static const unsigned LastIntegralType = 18; |
| 5056 | static const unsigned FirstPromotedIntegralType = 3, |
| 5057 | LastPromotedIntegralType = 9; |
| 5058 | static const unsigned FirstPromotedArithmeticType = 0, |
| 5059 | LastPromotedArithmeticType = 9; |
| 5060 | static const unsigned NumArithmeticTypes = 18; |
| 5061 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5062 | /// \brief Get the canonical type for a given arithmetic type index. |
| 5063 | CanQualType getArithmeticType(unsigned index) { |
| 5064 | assert(index < NumArithmeticTypes); |
| 5065 | static CanQualType ASTContext::* const |
| 5066 | ArithmeticTypes[NumArithmeticTypes] = { |
| 5067 | // Start of promoted types. |
| 5068 | &ASTContext::FloatTy, |
| 5069 | &ASTContext::DoubleTy, |
| 5070 | &ASTContext::LongDoubleTy, |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 5071 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5072 | // Start of integral types. |
| 5073 | &ASTContext::IntTy, |
| 5074 | &ASTContext::LongTy, |
| 5075 | &ASTContext::LongLongTy, |
| 5076 | &ASTContext::UnsignedIntTy, |
| 5077 | &ASTContext::UnsignedLongTy, |
| 5078 | &ASTContext::UnsignedLongLongTy, |
| 5079 | // End of promoted types. |
| 5080 | |
| 5081 | &ASTContext::BoolTy, |
| 5082 | &ASTContext::CharTy, |
| 5083 | &ASTContext::WCharTy, |
| 5084 | &ASTContext::Char16Ty, |
| 5085 | &ASTContext::Char32Ty, |
| 5086 | &ASTContext::SignedCharTy, |
| 5087 | &ASTContext::ShortTy, |
| 5088 | &ASTContext::UnsignedCharTy, |
| 5089 | &ASTContext::UnsignedShortTy, |
| 5090 | // End of integral types. |
| 5091 | // FIXME: What about complex? |
| 5092 | }; |
| 5093 | return S.Context.*ArithmeticTypes[index]; |
| 5094 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5095 | |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5096 | /// \brief Gets the canonical type resulting from the usual arithemetic |
| 5097 | /// converions for the given arithmetic types. |
| 5098 | CanQualType getUsualArithmeticConversions(unsigned L, unsigned R) { |
| 5099 | // Accelerator table for performing the usual arithmetic conversions. |
| 5100 | // The rules are basically: |
| 5101 | // - if either is floating-point, use the wider floating-point |
| 5102 | // - if same signedness, use the higher rank |
| 5103 | // - if same size, use unsigned of the higher rank |
| 5104 | // - use the larger type |
| 5105 | // These rules, together with the axiom that higher ranks are |
| 5106 | // never smaller, are sufficient to precompute all of these results |
| 5107 | // *except* when dealing with signed types of higher rank. |
| 5108 | // (we could precompute SLL x UI for all known platforms, but it's |
| 5109 | // better not to make any assumptions). |
| 5110 | enum PromotedType { |
| 5111 | Flt, Dbl, LDbl, SI, SL, SLL, UI, UL, ULL, Dep=-1 |
| 5112 | }; |
| 5113 | static PromotedType ConversionsTable[LastPromotedArithmeticType] |
| 5114 | [LastPromotedArithmeticType] = { |
| 5115 | /* Flt*/ { Flt, Dbl, LDbl, Flt, Flt, Flt, Flt, Flt, Flt }, |
| 5116 | /* Dbl*/ { Dbl, Dbl, LDbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl }, |
| 5117 | /*LDbl*/ { LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl }, |
| 5118 | /* SI*/ { Flt, Dbl, LDbl, SI, SL, SLL, UI, UL, ULL }, |
| 5119 | /* SL*/ { Flt, Dbl, LDbl, SL, SL, SLL, Dep, UL, ULL }, |
| 5120 | /* SLL*/ { Flt, Dbl, LDbl, SLL, SLL, SLL, Dep, Dep, ULL }, |
| 5121 | /* UI*/ { Flt, Dbl, LDbl, UI, Dep, Dep, UI, UL, ULL }, |
| 5122 | /* UL*/ { Flt, Dbl, LDbl, UL, UL, Dep, UL, UL, ULL }, |
| 5123 | /* ULL*/ { Flt, Dbl, LDbl, ULL, ULL, ULL, ULL, ULL, ULL }, |
| 5124 | }; |
| 5125 | |
| 5126 | assert(L < LastPromotedArithmeticType); |
| 5127 | assert(R < LastPromotedArithmeticType); |
| 5128 | int Idx = ConversionsTable[L][R]; |
| 5129 | |
| 5130 | // Fast path: the table gives us a concrete answer. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5131 | if (Idx != Dep) return getArithmeticType(Idx); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5132 | |
| 5133 | // Slow path: we need to compare widths. |
| 5134 | // An invariant is that the signed type has higher rank. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5135 | CanQualType LT = getArithmeticType(L), |
| 5136 | RT = getArithmeticType(R); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5137 | unsigned LW = S.Context.getIntWidth(LT), |
| 5138 | RW = S.Context.getIntWidth(RT); |
| 5139 | |
| 5140 | // If they're different widths, use the signed type. |
| 5141 | if (LW > RW) return LT; |
| 5142 | else if (LW < RW) return RT; |
| 5143 | |
| 5144 | // Otherwise, use the unsigned type of the signed type's rank. |
| 5145 | if (L == SL || R == SL) return S.Context.UnsignedLongTy; |
| 5146 | assert(L == SLL || R == SLL); |
| 5147 | return S.Context.UnsignedLongLongTy; |
| 5148 | } |
| 5149 | |
Chandler Carruth | 3c69dc4 | 2010-12-12 09:22:45 +0000 | [diff] [blame] | 5150 | /// \brief Helper method to factor out the common pattern of adding overloads |
| 5151 | /// for '++' and '--' builtin operators. |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5152 | void addPlusPlusMinusMinusStyleOverloads(QualType CandidateTy, |
| 5153 | bool HasVolatile) { |
| 5154 | QualType ParamTypes[2] = { |
| 5155 | S.Context.getLValueReferenceType(CandidateTy), |
| 5156 | S.Context.IntTy |
| 5157 | }; |
| 5158 | |
| 5159 | // Non-volatile version. |
| 5160 | if (NumArgs == 1) |
| 5161 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 5162 | else |
| 5163 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 5164 | |
| 5165 | // Use a heuristic to reduce number of builtin candidates in the set: |
| 5166 | // add volatile version only if there are conversions to a volatile type. |
| 5167 | if (HasVolatile) { |
| 5168 | ParamTypes[0] = |
| 5169 | S.Context.getLValueReferenceType( |
| 5170 | S.Context.getVolatileType(CandidateTy)); |
| 5171 | if (NumArgs == 1) |
| 5172 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 5173 | else |
| 5174 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 5175 | } |
| 5176 | } |
| 5177 | |
| 5178 | public: |
| 5179 | BuiltinOperatorOverloadBuilder( |
| 5180 | Sema &S, Expr **Args, unsigned NumArgs, |
| 5181 | Qualifiers VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5182 | bool HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5183 | llvm::SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes, |
| 5184 | OverloadCandidateSet &CandidateSet) |
| 5185 | : S(S), Args(Args), NumArgs(NumArgs), |
| 5186 | VisibleTypeConversionsQuals(VisibleTypeConversionsQuals), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5187 | HasArithmeticOrEnumeralCandidateType( |
| 5188 | HasArithmeticOrEnumeralCandidateType), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5189 | CandidateTypes(CandidateTypes), |
| 5190 | CandidateSet(CandidateSet) { |
| 5191 | // Validate some of our static helper constants in debug builds. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5192 | assert(getArithmeticType(FirstPromotedIntegralType) == S.Context.IntTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5193 | "Invalid first promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5194 | assert(getArithmeticType(LastPromotedIntegralType - 1) |
| 5195 | == S.Context.UnsignedLongLongTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5196 | "Invalid last promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5197 | assert(getArithmeticType(FirstPromotedArithmeticType) |
| 5198 | == S.Context.FloatTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5199 | "Invalid first promoted arithmetic type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5200 | assert(getArithmeticType(LastPromotedArithmeticType - 1) |
| 5201 | == S.Context.UnsignedLongLongTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5202 | "Invalid last promoted arithmetic type"); |
| 5203 | } |
| 5204 | |
| 5205 | // C++ [over.built]p3: |
| 5206 | // |
| 5207 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 5208 | // is either volatile or empty, there exist candidate operator |
| 5209 | // functions of the form |
| 5210 | // |
| 5211 | // VQ T& operator++(VQ T&); |
| 5212 | // T operator++(VQ T&, int); |
| 5213 | // |
| 5214 | // C++ [over.built]p4: |
| 5215 | // |
| 5216 | // For every pair (T, VQ), where T is an arithmetic type other |
| 5217 | // than bool, and VQ is either volatile or empty, there exist |
| 5218 | // candidate operator functions of the form |
| 5219 | // |
| 5220 | // VQ T& operator--(VQ T&); |
| 5221 | // T operator--(VQ T&, int); |
| 5222 | void addPlusPlusMinusMinusArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5223 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5224 | return; |
| 5225 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5226 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 5227 | Arith < NumArithmeticTypes; ++Arith) { |
| 5228 | addPlusPlusMinusMinusStyleOverloads( |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5229 | getArithmeticType(Arith), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5230 | VisibleTypeConversionsQuals.hasVolatile()); |
| 5231 | } |
| 5232 | } |
| 5233 | |
| 5234 | // C++ [over.built]p5: |
| 5235 | // |
| 5236 | // For every pair (T, VQ), where T is a cv-qualified or |
| 5237 | // cv-unqualified object type, and VQ is either volatile or |
| 5238 | // empty, there exist candidate operator functions of the form |
| 5239 | // |
| 5240 | // T*VQ& operator++(T*VQ&); |
| 5241 | // T*VQ& operator--(T*VQ&); |
| 5242 | // T* operator++(T*VQ&, int); |
| 5243 | // T* operator--(T*VQ&, int); |
| 5244 | void addPlusPlusMinusMinusPointerOverloads() { |
| 5245 | for (BuiltinCandidateTypeSet::iterator |
| 5246 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5247 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5248 | Ptr != PtrEnd; ++Ptr) { |
| 5249 | // Skip pointer types that aren't pointers to object types. |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5250 | if (!(*Ptr)->getPointeeType()->isObjectType()) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5251 | continue; |
| 5252 | |
| 5253 | addPlusPlusMinusMinusStyleOverloads(*Ptr, |
| 5254 | (!S.Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 5255 | VisibleTypeConversionsQuals.hasVolatile())); |
| 5256 | } |
| 5257 | } |
| 5258 | |
| 5259 | // C++ [over.built]p6: |
| 5260 | // For every cv-qualified or cv-unqualified object type T, there |
| 5261 | // exist candidate operator functions of the form |
| 5262 | // |
| 5263 | // T& operator*(T*); |
| 5264 | // |
| 5265 | // C++ [over.built]p7: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5266 | // 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] | 5267 | // ref-qualifier, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5268 | // T& operator*(T*); |
| 5269 | void addUnaryStarPointerOverloads() { |
| 5270 | for (BuiltinCandidateTypeSet::iterator |
| 5271 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5272 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5273 | Ptr != PtrEnd; ++Ptr) { |
| 5274 | QualType ParamTy = *Ptr; |
| 5275 | QualType PointeeTy = ParamTy->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5276 | if (!PointeeTy->isObjectType() && !PointeeTy->isFunctionType()) |
| 5277 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5278 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5279 | if (const FunctionProtoType *Proto =PointeeTy->getAs<FunctionProtoType>()) |
| 5280 | if (Proto->getTypeQuals() || Proto->getRefQualifier()) |
| 5281 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5282 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5283 | S.AddBuiltinCandidate(S.Context.getLValueReferenceType(PointeeTy), |
| 5284 | &ParamTy, Args, 1, CandidateSet); |
| 5285 | } |
| 5286 | } |
| 5287 | |
| 5288 | // C++ [over.built]p9: |
| 5289 | // For every promoted arithmetic type T, there exist candidate |
| 5290 | // operator functions of the form |
| 5291 | // |
| 5292 | // T operator+(T); |
| 5293 | // T operator-(T); |
| 5294 | void addUnaryPlusOrMinusArithmeticOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5295 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5296 | return; |
| 5297 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5298 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 5299 | Arith < LastPromotedArithmeticType; ++Arith) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5300 | QualType ArithTy = getArithmeticType(Arith); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5301 | S.AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 5302 | } |
| 5303 | |
| 5304 | // Extension: We also add these operators for vector types. |
| 5305 | for (BuiltinCandidateTypeSet::iterator |
| 5306 | Vec = CandidateTypes[0].vector_begin(), |
| 5307 | VecEnd = CandidateTypes[0].vector_end(); |
| 5308 | Vec != VecEnd; ++Vec) { |
| 5309 | QualType VecTy = *Vec; |
| 5310 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 5311 | } |
| 5312 | } |
| 5313 | |
| 5314 | // C++ [over.built]p8: |
| 5315 | // For every type T, there exist candidate operator functions of |
| 5316 | // the form |
| 5317 | // |
| 5318 | // T* operator+(T*); |
| 5319 | void addUnaryPlusPointerOverloads() { |
| 5320 | for (BuiltinCandidateTypeSet::iterator |
| 5321 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5322 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5323 | Ptr != PtrEnd; ++Ptr) { |
| 5324 | QualType ParamTy = *Ptr; |
| 5325 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 5326 | } |
| 5327 | } |
| 5328 | |
| 5329 | // C++ [over.built]p10: |
| 5330 | // For every promoted integral type T, there exist candidate |
| 5331 | // operator functions of the form |
| 5332 | // |
| 5333 | // T operator~(T); |
| 5334 | void addUnaryTildePromotedIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5335 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5336 | return; |
| 5337 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5338 | for (unsigned Int = FirstPromotedIntegralType; |
| 5339 | Int < LastPromotedIntegralType; ++Int) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5340 | QualType IntTy = getArithmeticType(Int); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5341 | S.AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 5342 | } |
| 5343 | |
| 5344 | // Extension: We also add this operator for vector types. |
| 5345 | for (BuiltinCandidateTypeSet::iterator |
| 5346 | Vec = CandidateTypes[0].vector_begin(), |
| 5347 | VecEnd = CandidateTypes[0].vector_end(); |
| 5348 | Vec != VecEnd; ++Vec) { |
| 5349 | QualType VecTy = *Vec; |
| 5350 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 5351 | } |
| 5352 | } |
| 5353 | |
| 5354 | // C++ [over.match.oper]p16: |
| 5355 | // For every pointer to member type T, there exist candidate operator |
| 5356 | // functions of the form |
| 5357 | // |
| 5358 | // bool operator==(T,T); |
| 5359 | // bool operator!=(T,T); |
| 5360 | void addEqualEqualOrNotEqualMemberPointerOverloads() { |
| 5361 | /// Set of (canonical) types that we've already handled. |
| 5362 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5363 | |
| 5364 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 5365 | for (BuiltinCandidateTypeSet::iterator |
| 5366 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 5367 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 5368 | MemPtr != MemPtrEnd; |
| 5369 | ++MemPtr) { |
| 5370 | // Don't add the same builtin candidate twice. |
| 5371 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 5372 | continue; |
| 5373 | |
| 5374 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 5375 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 5376 | CandidateSet); |
| 5377 | } |
| 5378 | } |
| 5379 | } |
| 5380 | |
| 5381 | // C++ [over.built]p15: |
| 5382 | // |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 5383 | // For every T, where T is an enumeration type, a pointer type, or |
| 5384 | // std::nullptr_t, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5385 | // |
| 5386 | // bool operator<(T, T); |
| 5387 | // bool operator>(T, T); |
| 5388 | // bool operator<=(T, T); |
| 5389 | // bool operator>=(T, T); |
| 5390 | // bool operator==(T, T); |
| 5391 | // bool operator!=(T, T); |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 5392 | void addRelationalPointerOrEnumeralOverloads() { |
| 5393 | // C++ [over.built]p1: |
| 5394 | // If there is a user-written candidate with the same name and parameter |
| 5395 | // types as a built-in candidate operator function, the built-in operator |
| 5396 | // function is hidden and is not included in the set of candidate |
| 5397 | // functions. |
| 5398 | // |
| 5399 | // The text is actually in a note, but if we don't implement it then we end |
| 5400 | // up with ambiguities when the user provides an overloaded operator for |
| 5401 | // an enumeration type. Note that only enumeration types have this problem, |
| 5402 | // so we track which enumeration types we've seen operators for. Also, the |
| 5403 | // only other overloaded operator with enumeration argumenst, operator=, |
| 5404 | // cannot be overloaded for enumeration types, so this is the only place |
| 5405 | // where we must suppress candidates like this. |
| 5406 | llvm::DenseSet<std::pair<CanQualType, CanQualType> > |
| 5407 | UserDefinedBinaryOperators; |
| 5408 | |
| 5409 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 5410 | if (CandidateTypes[ArgIdx].enumeration_begin() != |
| 5411 | CandidateTypes[ArgIdx].enumeration_end()) { |
| 5412 | for (OverloadCandidateSet::iterator C = CandidateSet.begin(), |
| 5413 | CEnd = CandidateSet.end(); |
| 5414 | C != CEnd; ++C) { |
| 5415 | if (!C->Viable || !C->Function || C->Function->getNumParams() != 2) |
| 5416 | continue; |
| 5417 | |
| 5418 | QualType FirstParamType = |
| 5419 | C->Function->getParamDecl(0)->getType().getUnqualifiedType(); |
| 5420 | QualType SecondParamType = |
| 5421 | C->Function->getParamDecl(1)->getType().getUnqualifiedType(); |
| 5422 | |
| 5423 | // Skip if either parameter isn't of enumeral type. |
| 5424 | if (!FirstParamType->isEnumeralType() || |
| 5425 | !SecondParamType->isEnumeralType()) |
| 5426 | continue; |
| 5427 | |
| 5428 | // Add this operator to the set of known user-defined operators. |
| 5429 | UserDefinedBinaryOperators.insert( |
| 5430 | std::make_pair(S.Context.getCanonicalType(FirstParamType), |
| 5431 | S.Context.getCanonicalType(SecondParamType))); |
| 5432 | } |
| 5433 | } |
| 5434 | } |
| 5435 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5436 | /// Set of (canonical) types that we've already handled. |
| 5437 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5438 | |
| 5439 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 5440 | for (BuiltinCandidateTypeSet::iterator |
| 5441 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 5442 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 5443 | Ptr != PtrEnd; ++Ptr) { |
| 5444 | // Don't add the same builtin candidate twice. |
| 5445 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 5446 | continue; |
| 5447 | |
| 5448 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5449 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 5450 | CandidateSet); |
| 5451 | } |
| 5452 | for (BuiltinCandidateTypeSet::iterator |
| 5453 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 5454 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 5455 | Enum != EnumEnd; ++Enum) { |
| 5456 | CanQualType CanonType = S.Context.getCanonicalType(*Enum); |
| 5457 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 5458 | // Don't add the same builtin candidate twice, or if a user defined |
| 5459 | // candidate exists. |
| 5460 | if (!AddedTypes.insert(CanonType) || |
| 5461 | UserDefinedBinaryOperators.count(std::make_pair(CanonType, |
| 5462 | CanonType))) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5463 | continue; |
| 5464 | |
| 5465 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 5466 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 5467 | CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5468 | } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 5469 | |
| 5470 | if (CandidateTypes[ArgIdx].hasNullPtrType()) { |
| 5471 | CanQualType NullPtrTy = S.Context.getCanonicalType(S.Context.NullPtrTy); |
| 5472 | if (AddedTypes.insert(NullPtrTy) && |
| 5473 | !UserDefinedBinaryOperators.count(std::make_pair(NullPtrTy, |
| 5474 | NullPtrTy))) { |
| 5475 | QualType ParamTypes[2] = { NullPtrTy, NullPtrTy }; |
| 5476 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 5477 | CandidateSet); |
| 5478 | } |
| 5479 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5480 | } |
| 5481 | } |
| 5482 | |
| 5483 | // C++ [over.built]p13: |
| 5484 | // |
| 5485 | // For every cv-qualified or cv-unqualified object type T |
| 5486 | // there exist candidate operator functions of the form |
| 5487 | // |
| 5488 | // T* operator+(T*, ptrdiff_t); |
| 5489 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 5490 | // T* operator-(T*, ptrdiff_t); |
| 5491 | // T* operator+(ptrdiff_t, T*); |
| 5492 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 5493 | // |
| 5494 | // C++ [over.built]p14: |
| 5495 | // |
| 5496 | // For every T, where T is a pointer to object type, there |
| 5497 | // exist candidate operator functions of the form |
| 5498 | // |
| 5499 | // ptrdiff_t operator-(T, T); |
| 5500 | void addBinaryPlusOrMinusPointerOverloads(OverloadedOperatorKind Op) { |
| 5501 | /// Set of (canonical) types that we've already handled. |
| 5502 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5503 | |
| 5504 | for (int Arg = 0; Arg < 2; ++Arg) { |
| 5505 | QualType AsymetricParamTypes[2] = { |
| 5506 | S.Context.getPointerDiffType(), |
| 5507 | S.Context.getPointerDiffType(), |
| 5508 | }; |
| 5509 | for (BuiltinCandidateTypeSet::iterator |
| 5510 | Ptr = CandidateTypes[Arg].pointer_begin(), |
| 5511 | PtrEnd = CandidateTypes[Arg].pointer_end(); |
| 5512 | Ptr != PtrEnd; ++Ptr) { |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5513 | QualType PointeeTy = (*Ptr)->getPointeeType(); |
| 5514 | if (!PointeeTy->isObjectType()) |
| 5515 | continue; |
| 5516 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5517 | AsymetricParamTypes[Arg] = *Ptr; |
| 5518 | if (Arg == 0 || Op == OO_Plus) { |
| 5519 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 5520 | // T* operator+(ptrdiff_t, T*); |
| 5521 | S.AddBuiltinCandidate(*Ptr, AsymetricParamTypes, Args, 2, |
| 5522 | CandidateSet); |
| 5523 | } |
| 5524 | if (Op == OO_Minus) { |
| 5525 | // ptrdiff_t operator-(T, T); |
| 5526 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 5527 | continue; |
| 5528 | |
| 5529 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5530 | S.AddBuiltinCandidate(S.Context.getPointerDiffType(), ParamTypes, |
| 5531 | Args, 2, CandidateSet); |
| 5532 | } |
| 5533 | } |
| 5534 | } |
| 5535 | } |
| 5536 | |
| 5537 | // C++ [over.built]p12: |
| 5538 | // |
| 5539 | // For every pair of promoted arithmetic types L and R, there |
| 5540 | // exist candidate operator functions of the form |
| 5541 | // |
| 5542 | // LR operator*(L, R); |
| 5543 | // LR operator/(L, R); |
| 5544 | // LR operator+(L, R); |
| 5545 | // LR operator-(L, R); |
| 5546 | // bool operator<(L, R); |
| 5547 | // bool operator>(L, R); |
| 5548 | // bool operator<=(L, R); |
| 5549 | // bool operator>=(L, R); |
| 5550 | // bool operator==(L, R); |
| 5551 | // bool operator!=(L, R); |
| 5552 | // |
| 5553 | // where LR is the result of the usual arithmetic conversions |
| 5554 | // between types L and R. |
| 5555 | // |
| 5556 | // C++ [over.built]p24: |
| 5557 | // |
| 5558 | // For every pair of promoted arithmetic types L and R, there exist |
| 5559 | // candidate operator functions of the form |
| 5560 | // |
| 5561 | // LR operator?(bool, L, R); |
| 5562 | // |
| 5563 | // where LR is the result of the usual arithmetic conversions |
| 5564 | // between types L and R. |
| 5565 | // Our candidates ignore the first parameter. |
| 5566 | void addGenericBinaryArithmeticOverloads(bool isComparison) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5567 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5568 | return; |
| 5569 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5570 | for (unsigned Left = FirstPromotedArithmeticType; |
| 5571 | Left < LastPromotedArithmeticType; ++Left) { |
| 5572 | for (unsigned Right = FirstPromotedArithmeticType; |
| 5573 | Right < LastPromotedArithmeticType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5574 | QualType LandR[2] = { getArithmeticType(Left), |
| 5575 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5576 | QualType Result = |
| 5577 | isComparison ? S.Context.BoolTy |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5578 | : getUsualArithmeticConversions(Left, Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5579 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 5580 | } |
| 5581 | } |
| 5582 | |
| 5583 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 5584 | // conditional operator for vector types. |
| 5585 | for (BuiltinCandidateTypeSet::iterator |
| 5586 | Vec1 = CandidateTypes[0].vector_begin(), |
| 5587 | Vec1End = CandidateTypes[0].vector_end(); |
| 5588 | Vec1 != Vec1End; ++Vec1) { |
| 5589 | for (BuiltinCandidateTypeSet::iterator |
| 5590 | Vec2 = CandidateTypes[1].vector_begin(), |
| 5591 | Vec2End = CandidateTypes[1].vector_end(); |
| 5592 | Vec2 != Vec2End; ++Vec2) { |
| 5593 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 5594 | QualType Result = S.Context.BoolTy; |
| 5595 | if (!isComparison) { |
| 5596 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 5597 | Result = *Vec1; |
| 5598 | else |
| 5599 | Result = *Vec2; |
| 5600 | } |
| 5601 | |
| 5602 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 5603 | } |
| 5604 | } |
| 5605 | } |
| 5606 | |
| 5607 | // C++ [over.built]p17: |
| 5608 | // |
| 5609 | // For every pair of promoted integral types L and R, there |
| 5610 | // exist candidate operator functions of the form |
| 5611 | // |
| 5612 | // LR operator%(L, R); |
| 5613 | // LR operator&(L, R); |
| 5614 | // LR operator^(L, R); |
| 5615 | // LR operator|(L, R); |
| 5616 | // L operator<<(L, R); |
| 5617 | // L operator>>(L, R); |
| 5618 | // |
| 5619 | // where LR is the result of the usual arithmetic conversions |
| 5620 | // between types L and R. |
| 5621 | void addBinaryBitwiseArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5622 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5623 | return; |
| 5624 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5625 | for (unsigned Left = FirstPromotedIntegralType; |
| 5626 | Left < LastPromotedIntegralType; ++Left) { |
| 5627 | for (unsigned Right = FirstPromotedIntegralType; |
| 5628 | Right < LastPromotedIntegralType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5629 | QualType LandR[2] = { getArithmeticType(Left), |
| 5630 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5631 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 5632 | ? LandR[0] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5633 | : getUsualArithmeticConversions(Left, Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5634 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 5635 | } |
| 5636 | } |
| 5637 | } |
| 5638 | |
| 5639 | // C++ [over.built]p20: |
| 5640 | // |
| 5641 | // For every pair (T, VQ), where T is an enumeration or |
| 5642 | // pointer to member type and VQ is either volatile or |
| 5643 | // empty, there exist candidate operator functions of the form |
| 5644 | // |
| 5645 | // VQ T& operator=(VQ T&, T); |
| 5646 | void addAssignmentMemberPointerOrEnumeralOverloads() { |
| 5647 | /// Set of (canonical) types that we've already handled. |
| 5648 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5649 | |
| 5650 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 5651 | for (BuiltinCandidateTypeSet::iterator |
| 5652 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 5653 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 5654 | Enum != EnumEnd; ++Enum) { |
| 5655 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 5656 | continue; |
| 5657 | |
| 5658 | AddBuiltinAssignmentOperatorCandidates(S, *Enum, Args, 2, |
| 5659 | CandidateSet); |
| 5660 | } |
| 5661 | |
| 5662 | for (BuiltinCandidateTypeSet::iterator |
| 5663 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 5664 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 5665 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 5666 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 5667 | continue; |
| 5668 | |
| 5669 | AddBuiltinAssignmentOperatorCandidates(S, *MemPtr, Args, 2, |
| 5670 | CandidateSet); |
| 5671 | } |
| 5672 | } |
| 5673 | } |
| 5674 | |
| 5675 | // C++ [over.built]p19: |
| 5676 | // |
| 5677 | // For every pair (T, VQ), where T is any type and VQ is either |
| 5678 | // volatile or empty, there exist candidate operator functions |
| 5679 | // of the form |
| 5680 | // |
| 5681 | // T*VQ& operator=(T*VQ&, T*); |
| 5682 | // |
| 5683 | // C++ [over.built]p21: |
| 5684 | // |
| 5685 | // For every pair (T, VQ), where T is a cv-qualified or |
| 5686 | // cv-unqualified object type and VQ is either volatile or |
| 5687 | // empty, there exist candidate operator functions of the form |
| 5688 | // |
| 5689 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 5690 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 5691 | void addAssignmentPointerOverloads(bool isEqualOp) { |
| 5692 | /// Set of (canonical) types that we've already handled. |
| 5693 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5694 | |
| 5695 | for (BuiltinCandidateTypeSet::iterator |
| 5696 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5697 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5698 | Ptr != PtrEnd; ++Ptr) { |
| 5699 | // If this is operator=, keep track of the builtin candidates we added. |
| 5700 | if (isEqualOp) |
| 5701 | AddedTypes.insert(S.Context.getCanonicalType(*Ptr)); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5702 | else if (!(*Ptr)->getPointeeType()->isObjectType()) |
| 5703 | continue; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5704 | |
| 5705 | // non-volatile version |
| 5706 | QualType ParamTypes[2] = { |
| 5707 | S.Context.getLValueReferenceType(*Ptr), |
| 5708 | isEqualOp ? *Ptr : S.Context.getPointerDiffType(), |
| 5709 | }; |
| 5710 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5711 | /*IsAssigmentOperator=*/ isEqualOp); |
| 5712 | |
| 5713 | if (!S.Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 5714 | VisibleTypeConversionsQuals.hasVolatile()) { |
| 5715 | // volatile version |
| 5716 | ParamTypes[0] = |
| 5717 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
| 5718 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5719 | /*IsAssigmentOperator=*/isEqualOp); |
| 5720 | } |
| 5721 | } |
| 5722 | |
| 5723 | if (isEqualOp) { |
| 5724 | for (BuiltinCandidateTypeSet::iterator |
| 5725 | Ptr = CandidateTypes[1].pointer_begin(), |
| 5726 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 5727 | Ptr != PtrEnd; ++Ptr) { |
| 5728 | // Make sure we don't add the same candidate twice. |
| 5729 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 5730 | continue; |
| 5731 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 5732 | QualType ParamTypes[2] = { |
| 5733 | S.Context.getLValueReferenceType(*Ptr), |
| 5734 | *Ptr, |
| 5735 | }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5736 | |
| 5737 | // non-volatile version |
| 5738 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5739 | /*IsAssigmentOperator=*/true); |
| 5740 | |
| 5741 | if (!S.Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 5742 | VisibleTypeConversionsQuals.hasVolatile()) { |
| 5743 | // volatile version |
| 5744 | ParamTypes[0] = |
| 5745 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 5746 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 5747 | CandidateSet, /*IsAssigmentOperator=*/true); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5748 | } |
| 5749 | } |
| 5750 | } |
| 5751 | } |
| 5752 | |
| 5753 | // C++ [over.built]p18: |
| 5754 | // |
| 5755 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 5756 | // VQ is either volatile or empty, and R is a promoted |
| 5757 | // arithmetic type, there exist candidate operator functions of |
| 5758 | // the form |
| 5759 | // |
| 5760 | // VQ L& operator=(VQ L&, R); |
| 5761 | // VQ L& operator*=(VQ L&, R); |
| 5762 | // VQ L& operator/=(VQ L&, R); |
| 5763 | // VQ L& operator+=(VQ L&, R); |
| 5764 | // VQ L& operator-=(VQ L&, R); |
| 5765 | void addAssignmentArithmeticOverloads(bool isEqualOp) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5766 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5767 | return; |
| 5768 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5769 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 5770 | for (unsigned Right = FirstPromotedArithmeticType; |
| 5771 | Right < LastPromotedArithmeticType; ++Right) { |
| 5772 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5773 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5774 | |
| 5775 | // Add this built-in operator as a candidate (VQ is empty). |
| 5776 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5777 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5778 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5779 | /*IsAssigmentOperator=*/isEqualOp); |
| 5780 | |
| 5781 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 5782 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 5783 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5784 | S.Context.getVolatileType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5785 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 5786 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 5787 | CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5788 | /*IsAssigmentOperator=*/isEqualOp); |
| 5789 | } |
| 5790 | } |
| 5791 | } |
| 5792 | |
| 5793 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 5794 | for (BuiltinCandidateTypeSet::iterator |
| 5795 | Vec1 = CandidateTypes[0].vector_begin(), |
| 5796 | Vec1End = CandidateTypes[0].vector_end(); |
| 5797 | Vec1 != Vec1End; ++Vec1) { |
| 5798 | for (BuiltinCandidateTypeSet::iterator |
| 5799 | Vec2 = CandidateTypes[1].vector_begin(), |
| 5800 | Vec2End = CandidateTypes[1].vector_end(); |
| 5801 | Vec2 != Vec2End; ++Vec2) { |
| 5802 | QualType ParamTypes[2]; |
| 5803 | ParamTypes[1] = *Vec2; |
| 5804 | // Add this built-in operator as a candidate (VQ is empty). |
| 5805 | ParamTypes[0] = S.Context.getLValueReferenceType(*Vec1); |
| 5806 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5807 | /*IsAssigmentOperator=*/isEqualOp); |
| 5808 | |
| 5809 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 5810 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 5811 | ParamTypes[0] = S.Context.getVolatileType(*Vec1); |
| 5812 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 5813 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 5814 | CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5815 | /*IsAssigmentOperator=*/isEqualOp); |
| 5816 | } |
| 5817 | } |
| 5818 | } |
| 5819 | } |
| 5820 | |
| 5821 | // C++ [over.built]p22: |
| 5822 | // |
| 5823 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 5824 | // is either volatile or empty, and R is a promoted integral |
| 5825 | // type, there exist candidate operator functions of the form |
| 5826 | // |
| 5827 | // VQ L& operator%=(VQ L&, R); |
| 5828 | // VQ L& operator<<=(VQ L&, R); |
| 5829 | // VQ L& operator>>=(VQ L&, R); |
| 5830 | // VQ L& operator&=(VQ L&, R); |
| 5831 | // VQ L& operator^=(VQ L&, R); |
| 5832 | // VQ L& operator|=(VQ L&, R); |
| 5833 | void addAssignmentIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5834 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5835 | return; |
| 5836 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5837 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 5838 | for (unsigned Right = FirstPromotedIntegralType; |
| 5839 | Right < LastPromotedIntegralType; ++Right) { |
| 5840 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5841 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5842 | |
| 5843 | // Add this built-in operator as a candidate (VQ is empty). |
| 5844 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5845 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5846 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 5847 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 5848 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5849 | ParamTypes[0] = getArithmeticType(Left); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5850 | ParamTypes[0] = S.Context.getVolatileType(ParamTypes[0]); |
| 5851 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
| 5852 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 5853 | CandidateSet); |
| 5854 | } |
| 5855 | } |
| 5856 | } |
| 5857 | } |
| 5858 | |
| 5859 | // C++ [over.operator]p23: |
| 5860 | // |
| 5861 | // There also exist candidate operator functions of the form |
| 5862 | // |
| 5863 | // bool operator!(bool); |
| 5864 | // bool operator&&(bool, bool); |
| 5865 | // bool operator||(bool, bool); |
| 5866 | void addExclaimOverload() { |
| 5867 | QualType ParamTy = S.Context.BoolTy; |
| 5868 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 5869 | /*IsAssignmentOperator=*/false, |
| 5870 | /*NumContextualBoolArguments=*/1); |
| 5871 | } |
| 5872 | void addAmpAmpOrPipePipeOverload() { |
| 5873 | QualType ParamTypes[2] = { S.Context.BoolTy, S.Context.BoolTy }; |
| 5874 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 5875 | /*IsAssignmentOperator=*/false, |
| 5876 | /*NumContextualBoolArguments=*/2); |
| 5877 | } |
| 5878 | |
| 5879 | // C++ [over.built]p13: |
| 5880 | // |
| 5881 | // For every cv-qualified or cv-unqualified object type T there |
| 5882 | // exist candidate operator functions of the form |
| 5883 | // |
| 5884 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 5885 | // T& operator[](T*, ptrdiff_t); |
| 5886 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 5887 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 5888 | // T& operator[](ptrdiff_t, T*); |
| 5889 | void addSubscriptOverloads() { |
| 5890 | for (BuiltinCandidateTypeSet::iterator |
| 5891 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5892 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5893 | Ptr != PtrEnd; ++Ptr) { |
| 5894 | QualType ParamTypes[2] = { *Ptr, S.Context.getPointerDiffType() }; |
| 5895 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5896 | if (!PointeeType->isObjectType()) |
| 5897 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5898 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5899 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 5900 | |
| 5901 | // T& operator[](T*, ptrdiff_t) |
| 5902 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 5903 | } |
| 5904 | |
| 5905 | for (BuiltinCandidateTypeSet::iterator |
| 5906 | Ptr = CandidateTypes[1].pointer_begin(), |
| 5907 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 5908 | Ptr != PtrEnd; ++Ptr) { |
| 5909 | QualType ParamTypes[2] = { S.Context.getPointerDiffType(), *Ptr }; |
| 5910 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5911 | if (!PointeeType->isObjectType()) |
| 5912 | continue; |
| 5913 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5914 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 5915 | |
| 5916 | // T& operator[](ptrdiff_t, T*) |
| 5917 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 5918 | } |
| 5919 | } |
| 5920 | |
| 5921 | // C++ [over.built]p11: |
| 5922 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 5923 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 5924 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 5925 | // there exist candidate operator functions of the form |
| 5926 | // |
| 5927 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 5928 | // |
| 5929 | // where CV12 is the union of CV1 and CV2. |
| 5930 | void addArrowStarOverloads() { |
| 5931 | for (BuiltinCandidateTypeSet::iterator |
| 5932 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5933 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5934 | Ptr != PtrEnd; ++Ptr) { |
| 5935 | QualType C1Ty = (*Ptr); |
| 5936 | QualType C1; |
| 5937 | QualifierCollector Q1; |
| 5938 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
| 5939 | if (!isa<RecordType>(C1)) |
| 5940 | continue; |
| 5941 | // heuristic to reduce number of builtin candidates in the set. |
| 5942 | // Add volatile/restrict version only if there are conversions to a |
| 5943 | // volatile/restrict type. |
| 5944 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 5945 | continue; |
| 5946 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 5947 | continue; |
| 5948 | for (BuiltinCandidateTypeSet::iterator |
| 5949 | MemPtr = CandidateTypes[1].member_pointer_begin(), |
| 5950 | MemPtrEnd = CandidateTypes[1].member_pointer_end(); |
| 5951 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 5952 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 5953 | QualType C2 = QualType(mptr->getClass(), 0); |
| 5954 | C2 = C2.getUnqualifiedType(); |
| 5955 | if (C1 != C2 && !S.IsDerivedFrom(C1, C2)) |
| 5956 | break; |
| 5957 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 5958 | // build CV12 T& |
| 5959 | QualType T = mptr->getPointeeType(); |
| 5960 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 5961 | T.isVolatileQualified()) |
| 5962 | continue; |
| 5963 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 5964 | T.isRestrictQualified()) |
| 5965 | continue; |
| 5966 | T = Q1.apply(S.Context, T); |
| 5967 | QualType ResultTy = S.Context.getLValueReferenceType(T); |
| 5968 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 5969 | } |
| 5970 | } |
| 5971 | } |
| 5972 | |
| 5973 | // Note that we don't consider the first argument, since it has been |
| 5974 | // contextually converted to bool long ago. The candidates below are |
| 5975 | // therefore added as binary. |
| 5976 | // |
| 5977 | // C++ [over.built]p25: |
| 5978 | // For every type T, where T is a pointer, pointer-to-member, or scoped |
| 5979 | // enumeration type, there exist candidate operator functions of the form |
| 5980 | // |
| 5981 | // T operator?(bool, T, T); |
| 5982 | // |
| 5983 | void addConditionalOperatorOverloads() { |
| 5984 | /// Set of (canonical) types that we've already handled. |
| 5985 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5986 | |
| 5987 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 5988 | for (BuiltinCandidateTypeSet::iterator |
| 5989 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 5990 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 5991 | Ptr != PtrEnd; ++Ptr) { |
| 5992 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 5993 | continue; |
| 5994 | |
| 5995 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5996 | S.AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 5997 | } |
| 5998 | |
| 5999 | for (BuiltinCandidateTypeSet::iterator |
| 6000 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 6001 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 6002 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 6003 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 6004 | continue; |
| 6005 | |
| 6006 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 6007 | S.AddBuiltinCandidate(*MemPtr, ParamTypes, Args, 2, CandidateSet); |
| 6008 | } |
| 6009 | |
| 6010 | if (S.getLangOptions().CPlusPlus0x) { |
| 6011 | for (BuiltinCandidateTypeSet::iterator |
| 6012 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 6013 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 6014 | Enum != EnumEnd; ++Enum) { |
| 6015 | if (!(*Enum)->getAs<EnumType>()->getDecl()->isScoped()) |
| 6016 | continue; |
| 6017 | |
| 6018 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 6019 | continue; |
| 6020 | |
| 6021 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 6022 | S.AddBuiltinCandidate(*Enum, ParamTypes, Args, 2, CandidateSet); |
| 6023 | } |
| 6024 | } |
| 6025 | } |
| 6026 | } |
| 6027 | }; |
| 6028 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6029 | } // end anonymous namespace |
| 6030 | |
| 6031 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 6032 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 6033 | /// on the operator @p Op and the arguments given. For example, if the |
| 6034 | /// operator is a binary '+', this routine might add "int |
| 6035 | /// operator+(int, int)" to cover integer addition. |
| 6036 | void |
| 6037 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 6038 | SourceLocation OpLoc, |
| 6039 | Expr **Args, unsigned NumArgs, |
| 6040 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6041 | // Find all of the types that the arguments can convert to, but only |
| 6042 | // if the operator we're looking at has built-in operator candidates |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6043 | // that make use of these types. Also record whether we encounter non-record |
| 6044 | // candidate types or either arithmetic or enumeral candidate types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6045 | Qualifiers VisibleTypeConversionsQuals; |
| 6046 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 6047 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 6048 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6049 | |
| 6050 | bool HasNonRecordCandidateType = false; |
| 6051 | bool HasArithmeticOrEnumeralCandidateType = false; |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 6052 | llvm::SmallVector<BuiltinCandidateTypeSet, 2> CandidateTypes; |
| 6053 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 6054 | CandidateTypes.push_back(BuiltinCandidateTypeSet(*this)); |
| 6055 | CandidateTypes[ArgIdx].AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 6056 | OpLoc, |
| 6057 | true, |
| 6058 | (Op == OO_Exclaim || |
| 6059 | Op == OO_AmpAmp || |
| 6060 | Op == OO_PipePipe), |
| 6061 | VisibleTypeConversionsQuals); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6062 | HasNonRecordCandidateType = HasNonRecordCandidateType || |
| 6063 | CandidateTypes[ArgIdx].hasNonRecordTypes(); |
| 6064 | HasArithmeticOrEnumeralCandidateType = |
| 6065 | HasArithmeticOrEnumeralCandidateType || |
| 6066 | CandidateTypes[ArgIdx].hasArithmeticOrEnumeralTypes(); |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 6067 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6068 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6069 | // Exit early when no non-record types have been added to the candidate set |
| 6070 | // for any of the arguments to the operator. |
| 6071 | if (!HasNonRecordCandidateType) |
| 6072 | return; |
| 6073 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6074 | // Setup an object to manage the common state for building overloads. |
| 6075 | BuiltinOperatorOverloadBuilder OpBuilder(*this, Args, NumArgs, |
| 6076 | VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6077 | HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6078 | CandidateTypes, CandidateSet); |
| 6079 | |
| 6080 | // Dispatch over the operation to add in only those overloads which apply. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6081 | switch (Op) { |
| 6082 | case OO_None: |
| 6083 | case NUM_OVERLOADED_OPERATORS: |
| 6084 | assert(false && "Expected an overloaded operator"); |
| 6085 | break; |
| 6086 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6087 | case OO_New: |
| 6088 | case OO_Delete: |
| 6089 | case OO_Array_New: |
| 6090 | case OO_Array_Delete: |
| 6091 | case OO_Call: |
| 6092 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
| 6093 | break; |
| 6094 | |
| 6095 | case OO_Comma: |
| 6096 | case OO_Arrow: |
| 6097 | // C++ [over.match.oper]p3: |
| 6098 | // -- For the operator ',', the unary operator '&', or the |
| 6099 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6100 | break; |
| 6101 | |
| 6102 | case OO_Plus: // '+' is either unary or binary |
Chandler Carruth | 32fe0d0 | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 6103 | if (NumArgs == 1) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6104 | OpBuilder.addUnaryPlusPointerOverloads(); |
Chandler Carruth | 32fe0d0 | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 6105 | // Fall through. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6106 | |
| 6107 | case OO_Minus: // '-' is either unary or binary |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 6108 | if (NumArgs == 1) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6109 | OpBuilder.addUnaryPlusOrMinusArithmeticOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 6110 | } else { |
| 6111 | OpBuilder.addBinaryPlusOrMinusPointerOverloads(Op); |
| 6112 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 6113 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6114 | break; |
| 6115 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6116 | case OO_Star: // '*' is either unary or binary |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6117 | if (NumArgs == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6118 | OpBuilder.addUnaryStarPointerOverloads(); |
| 6119 | else |
| 6120 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 6121 | break; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6122 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6123 | case OO_Slash: |
| 6124 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 6125 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6126 | |
| 6127 | case OO_PlusPlus: |
| 6128 | case OO_MinusMinus: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6129 | OpBuilder.addPlusPlusMinusMinusArithmeticOverloads(Op); |
| 6130 | OpBuilder.addPlusPlusMinusMinusPointerOverloads(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6131 | break; |
| 6132 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6133 | case OO_EqualEqual: |
| 6134 | case OO_ExclaimEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6135 | OpBuilder.addEqualEqualOrNotEqualMemberPointerOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 6136 | // Fall through. |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 6137 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6138 | case OO_Less: |
| 6139 | case OO_Greater: |
| 6140 | case OO_LessEqual: |
| 6141 | case OO_GreaterEqual: |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6142 | OpBuilder.addRelationalPointerOrEnumeralOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 6143 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/true); |
| 6144 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6145 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6146 | case OO_Percent: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6147 | case OO_Caret: |
| 6148 | case OO_Pipe: |
| 6149 | case OO_LessLess: |
| 6150 | case OO_GreaterGreater: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6151 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6152 | break; |
| 6153 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6154 | case OO_Amp: // '&' is either unary or binary |
| 6155 | if (NumArgs == 1) |
| 6156 | // C++ [over.match.oper]p3: |
| 6157 | // -- For the operator ',', the unary operator '&', or the |
| 6158 | // operator '->', the built-in candidates set is empty. |
| 6159 | break; |
| 6160 | |
| 6161 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
| 6162 | break; |
| 6163 | |
| 6164 | case OO_Tilde: |
| 6165 | OpBuilder.addUnaryTildePromotedIntegralOverloads(); |
| 6166 | break; |
| 6167 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6168 | case OO_Equal: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6169 | OpBuilder.addAssignmentMemberPointerOrEnumeralOverloads(); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6170 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6171 | |
| 6172 | case OO_PlusEqual: |
| 6173 | case OO_MinusEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6174 | OpBuilder.addAssignmentPointerOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6175 | // Fall through. |
| 6176 | |
| 6177 | case OO_StarEqual: |
| 6178 | case OO_SlashEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6179 | OpBuilder.addAssignmentArithmeticOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6180 | break; |
| 6181 | |
| 6182 | case OO_PercentEqual: |
| 6183 | case OO_LessLessEqual: |
| 6184 | case OO_GreaterGreaterEqual: |
| 6185 | case OO_AmpEqual: |
| 6186 | case OO_CaretEqual: |
| 6187 | case OO_PipeEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6188 | OpBuilder.addAssignmentIntegralOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6189 | break; |
| 6190 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6191 | case OO_Exclaim: |
| 6192 | OpBuilder.addExclaimOverload(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6193 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6194 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6195 | case OO_AmpAmp: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6196 | case OO_PipePipe: |
| 6197 | OpBuilder.addAmpAmpOrPipePipeOverload(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6198 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6199 | |
| 6200 | case OO_Subscript: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6201 | OpBuilder.addSubscriptOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6202 | break; |
| 6203 | |
| 6204 | case OO_ArrowStar: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6205 | OpBuilder.addArrowStarOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6206 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 6207 | |
| 6208 | case OO_Conditional: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6209 | OpBuilder.addConditionalOperatorOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 6210 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 6211 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6212 | } |
| 6213 | } |
| 6214 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6215 | /// \brief Add function candidates found via argument-dependent lookup |
| 6216 | /// to the set of overloading candidates. |
| 6217 | /// |
| 6218 | /// This routine performs argument-dependent name lookup based on the |
| 6219 | /// given function name (which may also be an operator name) and adds |
| 6220 | /// all of the overload candidates found by ADL to the overload |
| 6221 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6222 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6223 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6224 | bool Operator, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6225 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 6226 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6227 | OverloadCandidateSet& CandidateSet, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 6228 | bool PartialOverloading, |
| 6229 | bool StdNamespaceIsAssociated) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6230 | ADLResult Fns; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6231 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 6232 | // FIXME: This approach for uniquing ADL results (and removing |
| 6233 | // redundant candidates from the set) relies on pointer-equality, |
| 6234 | // which means we need to key off the canonical decl. However, |
| 6235 | // always going back to the canonical decl might not get us the |
| 6236 | // right set of default arguments. What default arguments are |
| 6237 | // we supposed to consider on ADL candidates, anyway? |
| 6238 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6239 | // FIXME: Pass in the explicit template arguments? |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 6240 | ArgumentDependentLookup(Name, Operator, Args, NumArgs, Fns, |
| 6241 | StdNamespaceIsAssociated); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6242 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6243 | // Erase all of the candidates we already knew about. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6244 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 6245 | CandEnd = CandidateSet.end(); |
| 6246 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6247 | if (Cand->Function) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6248 | Fns.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6249 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6250 | Fns.erase(FunTmpl); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6251 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6252 | |
| 6253 | // For each of the ADL candidates we found, add it to the overload |
| 6254 | // set. |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6255 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6256 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6257 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6258 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6259 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6260 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6261 | AddOverloadCandidate(FD, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 6262 | false, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6263 | } else |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6264 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6265 | FoundDecl, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 6266 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6267 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6268 | } |
| 6269 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6270 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 6271 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6272 | bool |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6273 | isBetterOverloadCandidate(Sema &S, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 6274 | const OverloadCandidate &Cand1, |
| 6275 | const OverloadCandidate &Cand2, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6276 | SourceLocation Loc, |
| 6277 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6278 | // Define viable functions to be better candidates than non-viable |
| 6279 | // functions. |
| 6280 | if (!Cand2.Viable) |
| 6281 | return Cand1.Viable; |
| 6282 | else if (!Cand1.Viable) |
| 6283 | return false; |
| 6284 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6285 | // C++ [over.match.best]p1: |
| 6286 | // |
| 6287 | // -- if F is a static member function, ICS1(F) is defined such |
| 6288 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 6289 | // any function G, and, symmetrically, ICS1(G) is neither |
| 6290 | // better nor worse than ICS1(F). |
| 6291 | unsigned StartArg = 0; |
| 6292 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 6293 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6294 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6295 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6296 | // A viable function F1 is defined to be a better function than another |
| 6297 | // 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] | 6298 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6299 | unsigned NumArgs = Cand1.Conversions.size(); |
| 6300 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 6301 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6302 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6303 | switch (CompareImplicitConversionSequences(S, |
| 6304 | Cand1.Conversions[ArgIdx], |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6305 | Cand2.Conversions[ArgIdx])) { |
| 6306 | case ImplicitConversionSequence::Better: |
| 6307 | // Cand1 has a better conversion sequence. |
| 6308 | HasBetterConversion = true; |
| 6309 | break; |
| 6310 | |
| 6311 | case ImplicitConversionSequence::Worse: |
| 6312 | // Cand1 can't be better than Cand2. |
| 6313 | return false; |
| 6314 | |
| 6315 | case ImplicitConversionSequence::Indistinguishable: |
| 6316 | // Do nothing. |
| 6317 | break; |
| 6318 | } |
| 6319 | } |
| 6320 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6321 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6322 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6323 | if (HasBetterConversion) |
| 6324 | return true; |
| 6325 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6326 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6327 | // specialization, or, if not that, |
Douglas Gregor | ccd4713 | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 6328 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6329 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 6330 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6331 | |
| 6332 | // -- F1 and F2 are function template specializations, and the function |
| 6333 | // template for F1 is more specialized than the template for F2 |
| 6334 | // 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] | 6335 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 6336 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6337 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6338 | if (FunctionTemplateDecl *BetterTemplate |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6339 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 6340 | Cand2.Function->getPrimaryTemplate(), |
| 6341 | Loc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6342 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 6343 | : TPOC_Call, |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6344 | Cand1.ExplicitCallArguments)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6345 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6346 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6347 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6348 | // -- the context is an initialization by user-defined conversion |
| 6349 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 6350 | // from the return type of F1 to the destination type (i.e., |
| 6351 | // the type of the entity being initialized) is a better |
| 6352 | // conversion sequence than the standard conversion sequence |
| 6353 | // from the return type of F2 to the destination type. |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6354 | if (UserDefinedConversion && Cand1.Function && Cand2.Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6355 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6356 | isa<CXXConversionDecl>(Cand2.Function)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6357 | switch (CompareStandardConversionSequences(S, |
| 6358 | Cand1.FinalConversion, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6359 | Cand2.FinalConversion)) { |
| 6360 | case ImplicitConversionSequence::Better: |
| 6361 | // Cand1 has a better conversion sequence. |
| 6362 | return true; |
| 6363 | |
| 6364 | case ImplicitConversionSequence::Worse: |
| 6365 | // Cand1 can't be better than Cand2. |
| 6366 | return false; |
| 6367 | |
| 6368 | case ImplicitConversionSequence::Indistinguishable: |
| 6369 | // Do nothing |
| 6370 | break; |
| 6371 | } |
| 6372 | } |
| 6373 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6374 | return false; |
| 6375 | } |
| 6376 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6377 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6378 | /// within an overload candidate set. |
| 6379 | /// |
| 6380 | /// \param CandidateSet the set of candidate functions. |
| 6381 | /// |
| 6382 | /// \param Loc the location of the function name (or operator symbol) for |
| 6383 | /// which overload resolution occurs. |
| 6384 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6385 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6386 | /// function, Best points to the candidate function found. |
| 6387 | /// |
| 6388 | /// \returns The result of overload resolution. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6389 | OverloadingResult |
| 6390 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 6391 | iterator &Best, |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 6392 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6393 | // Find the best viable function. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6394 | Best = end(); |
| 6395 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
| 6396 | if (Cand->Viable) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6397 | if (Best == end() || isBetterOverloadCandidate(S, *Cand, *Best, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6398 | UserDefinedConversion)) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6399 | Best = Cand; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6400 | } |
| 6401 | |
| 6402 | // If we didn't find any viable functions, abort. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6403 | if (Best == end()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6404 | return OR_No_Viable_Function; |
| 6405 | |
| 6406 | // Make sure that this function is better than every other viable |
| 6407 | // function. If not, we have an ambiguity. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6408 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6409 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6410 | Cand != Best && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6411 | !isBetterOverloadCandidate(S, *Best, *Cand, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6412 | UserDefinedConversion)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6413 | Best = end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6414 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6415 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6416 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6417 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6418 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6419 | if (Best->Function && |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 6420 | (Best->Function->isDeleted() || Best->Function->isUnavailable())) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6421 | return OR_Deleted; |
| 6422 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6423 | return OR_Success; |
| 6424 | } |
| 6425 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6426 | namespace { |
| 6427 | |
| 6428 | enum OverloadCandidateKind { |
| 6429 | oc_function, |
| 6430 | oc_method, |
| 6431 | oc_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6432 | oc_function_template, |
| 6433 | oc_method_template, |
| 6434 | oc_constructor_template, |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6435 | oc_implicit_default_constructor, |
| 6436 | oc_implicit_copy_constructor, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 6437 | oc_implicit_move_constructor, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6438 | oc_implicit_copy_assignment, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 6439 | oc_implicit_move_assignment, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6440 | oc_implicit_inherited_constructor |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6441 | }; |
| 6442 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6443 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 6444 | FunctionDecl *Fn, |
| 6445 | std::string &Description) { |
| 6446 | bool isTemplate = false; |
| 6447 | |
| 6448 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 6449 | isTemplate = true; |
| 6450 | Description = S.getTemplateArgumentBindingsText( |
| 6451 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 6452 | } |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6453 | |
| 6454 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6455 | if (!Ctor->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6456 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6457 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6458 | if (Ctor->getInheritedConstructor()) |
| 6459 | return oc_implicit_inherited_constructor; |
| 6460 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 6461 | if (Ctor->isDefaultConstructor()) |
| 6462 | return oc_implicit_default_constructor; |
| 6463 | |
| 6464 | if (Ctor->isMoveConstructor()) |
| 6465 | return oc_implicit_move_constructor; |
| 6466 | |
| 6467 | assert(Ctor->isCopyConstructor() && |
| 6468 | "unexpected sort of implicit constructor"); |
| 6469 | return oc_implicit_copy_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6470 | } |
| 6471 | |
| 6472 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 6473 | // This actually gets spelled 'candidate function' for now, but |
| 6474 | // it doesn't hurt to split it out. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6475 | if (!Meth->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6476 | return isTemplate ? oc_method_template : oc_method; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6477 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 6478 | if (Meth->isMoveAssignmentOperator()) |
| 6479 | return oc_implicit_move_assignment; |
| 6480 | |
Douglas Gregor | 3e9438b | 2010-09-27 22:37:28 +0000 | [diff] [blame] | 6481 | assert(Meth->isCopyAssignmentOperator() |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6482 | && "implicit method is not copy assignment operator?"); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6483 | return oc_implicit_copy_assignment; |
| 6484 | } |
| 6485 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6486 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6487 | } |
| 6488 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6489 | void MaybeEmitInheritedConstructorNote(Sema &S, FunctionDecl *Fn) { |
| 6490 | const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn); |
| 6491 | if (!Ctor) return; |
| 6492 | |
| 6493 | Ctor = Ctor->getInheritedConstructor(); |
| 6494 | if (!Ctor) return; |
| 6495 | |
| 6496 | S.Diag(Ctor->getLocation(), diag::note_ovl_candidate_inherited_constructor); |
| 6497 | } |
| 6498 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6499 | } // end anonymous namespace |
| 6500 | |
| 6501 | // Notes the location of an overload candidate. |
| 6502 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6503 | std::string FnDesc; |
| 6504 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
| 6505 | Diag(Fn->getLocation(), diag::note_ovl_candidate) |
| 6506 | << (unsigned) K << FnDesc; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6507 | MaybeEmitInheritedConstructorNote(*this, Fn); |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6508 | } |
| 6509 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6510 | //Notes the location of all overload candidates designated through |
| 6511 | // OverloadedExpr |
| 6512 | void Sema::NoteAllOverloadCandidates(Expr* OverloadedExpr) { |
| 6513 | assert(OverloadedExpr->getType() == Context.OverloadTy); |
| 6514 | |
| 6515 | OverloadExpr::FindResult Ovl = OverloadExpr::find(OverloadedExpr); |
| 6516 | OverloadExpr *OvlExpr = Ovl.Expression; |
| 6517 | |
| 6518 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 6519 | IEnd = OvlExpr->decls_end(); |
| 6520 | I != IEnd; ++I) { |
| 6521 | if (FunctionTemplateDecl *FunTmpl = |
| 6522 | dyn_cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()) ) { |
| 6523 | NoteOverloadCandidate(FunTmpl->getTemplatedDecl()); |
| 6524 | } else if (FunctionDecl *Fun |
| 6525 | = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl()) ) { |
| 6526 | NoteOverloadCandidate(Fun); |
| 6527 | } |
| 6528 | } |
| 6529 | } |
| 6530 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6531 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 6532 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 6533 | /// target types of the conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6534 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
| 6535 | Sema &S, |
| 6536 | SourceLocation CaretLoc, |
| 6537 | const PartialDiagnostic &PDiag) const { |
| 6538 | S.Diag(CaretLoc, PDiag) |
| 6539 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6540 | for (AmbiguousConversionSequence::const_iterator |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6541 | I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
| 6542 | S.NoteOverloadCandidate(*I); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6543 | } |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6544 | } |
| 6545 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6546 | namespace { |
| 6547 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6548 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 6549 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 6550 | assert(Conv.isBad()); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6551 | assert(Cand->Function && "for now, candidate must be a function"); |
| 6552 | FunctionDecl *Fn = Cand->Function; |
| 6553 | |
| 6554 | // There's a conversion slot for the object argument if this is a |
| 6555 | // non-constructor method. Note that 'I' corresponds the |
| 6556 | // conversion-slot index. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6557 | bool isObjectArgument = false; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6558 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6559 | if (I == 0) |
| 6560 | isObjectArgument = true; |
| 6561 | else |
| 6562 | I--; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6563 | } |
| 6564 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6565 | std::string FnDesc; |
| 6566 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 6567 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6568 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 6569 | QualType FromTy = Conv.Bad.getFromType(); |
| 6570 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6571 | |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 6572 | if (FromTy == S.Context.OverloadTy) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 6573 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 6574 | Expr *E = FromExpr->IgnoreParens(); |
| 6575 | if (isa<UnaryOperator>(E)) |
| 6576 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6577 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 6578 | |
| 6579 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 6580 | << (unsigned) FnKind << FnDesc |
| 6581 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6582 | << ToTy << Name << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6583 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 6584 | return; |
| 6585 | } |
| 6586 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 6587 | // Do some hand-waving analysis to see if the non-viability is due |
| 6588 | // to a qualifier mismatch. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6589 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 6590 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 6591 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 6592 | CToTy = RT->getPointeeType(); |
| 6593 | else { |
| 6594 | // TODO: detect and diagnose the full richness of const mismatches. |
| 6595 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 6596 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 6597 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 6598 | } |
| 6599 | |
| 6600 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 6601 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
| 6602 | // It is dumb that we have to do this here. |
| 6603 | while (isa<ArrayType>(CFromTy)) |
| 6604 | CFromTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 6605 | while (isa<ArrayType>(CToTy)) |
| 6606 | CToTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 6607 | |
| 6608 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 6609 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 6610 | |
| 6611 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 6612 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 6613 | << (unsigned) FnKind << FnDesc |
| 6614 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6615 | << FromTy |
| 6616 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 6617 | << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6618 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6619 | return; |
| 6620 | } |
| 6621 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 6622 | if (FromQs.getObjCGCAttr() != ToQs.getObjCGCAttr()) { |
| 6623 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_gc) |
| 6624 | << (unsigned) FnKind << FnDesc |
| 6625 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6626 | << FromTy |
| 6627 | << FromQs.getObjCGCAttr() << ToQs.getObjCGCAttr() |
| 6628 | << (unsigned) isObjectArgument << I+1; |
| 6629 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 6630 | return; |
| 6631 | } |
| 6632 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6633 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 6634 | assert(CVR && "unexpected qualifiers mismatch"); |
| 6635 | |
| 6636 | if (isObjectArgument) { |
| 6637 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 6638 | << (unsigned) FnKind << FnDesc |
| 6639 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6640 | << FromTy << (CVR - 1); |
| 6641 | } else { |
| 6642 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 6643 | << (unsigned) FnKind << FnDesc |
| 6644 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6645 | << FromTy << (CVR - 1) << I+1; |
| 6646 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6647 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6648 | return; |
| 6649 | } |
| 6650 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 6651 | // Diagnose references or pointers to incomplete types differently, |
| 6652 | // since it's far from impossible that the incompleteness triggered |
| 6653 | // the failure. |
| 6654 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 6655 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 6656 | TempFromTy = PTy->getPointeeType(); |
| 6657 | if (TempFromTy->isIncompleteType()) { |
| 6658 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 6659 | << (unsigned) FnKind << FnDesc |
| 6660 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6661 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6662 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 6663 | return; |
| 6664 | } |
| 6665 | |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6666 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6667 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6668 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 6669 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 6670 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 6671 | FromPtrTy->getPointeeType()) && |
| 6672 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 6673 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6674 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6675 | FromPtrTy->getPointeeType())) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6676 | BaseToDerivedConversion = 1; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6677 | } |
| 6678 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 6679 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 6680 | if (const ObjCObjectPointerType *ToPtrTy |
| 6681 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 6682 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 6683 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 6684 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 6685 | FromPtrTy->getPointeeType()) && |
| 6686 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6687 | BaseToDerivedConversion = 2; |
| 6688 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
| 6689 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 6690 | !FromTy->isIncompleteType() && |
| 6691 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 6692 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) |
| 6693 | BaseToDerivedConversion = 3; |
| 6694 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6695 | |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6696 | if (BaseToDerivedConversion) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6697 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6698 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6699 | << (unsigned) FnKind << FnDesc |
| 6700 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6701 | << (BaseToDerivedConversion - 1) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6702 | << FromTy << ToTy << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6703 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6704 | return; |
| 6705 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6706 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6707 | // TODO: specialize more based on the kind of mismatch |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6708 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv) |
| 6709 | << (unsigned) FnKind << FnDesc |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6710 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
John McCall | e81e15e | 2010-01-14 00:56:20 +0000 | [diff] [blame] | 6711 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6712 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6713 | } |
| 6714 | |
| 6715 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 6716 | unsigned NumFormalArgs) { |
| 6717 | // TODO: treat calls to a missing default constructor as a special case |
| 6718 | |
| 6719 | FunctionDecl *Fn = Cand->Function; |
| 6720 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 6721 | |
| 6722 | unsigned MinParams = Fn->getMinRequiredArguments(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6723 | |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 6724 | // With invalid overloaded operators, it's possible that we think we |
| 6725 | // have an arity mismatch when it fact it looks like we have the |
| 6726 | // right number of arguments, because only overloaded operators have |
| 6727 | // the weird behavior of overloading member and non-member functions. |
| 6728 | // Just don't report anything. |
| 6729 | if (Fn->isInvalidDecl() && |
| 6730 | Fn->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 6731 | return; |
| 6732 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6733 | // at least / at most / exactly |
| 6734 | unsigned mode, modeCount; |
| 6735 | if (NumFormalArgs < MinParams) { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 6736 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 6737 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 6738 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6739 | if (MinParams != FnTy->getNumArgs() || |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 6740 | FnTy->isVariadic() || FnTy->isTemplateVariadic()) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6741 | mode = 0; // "at least" |
| 6742 | else |
| 6743 | mode = 2; // "exactly" |
| 6744 | modeCount = MinParams; |
| 6745 | } else { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 6746 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 6747 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 6748 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6749 | if (MinParams != FnTy->getNumArgs()) |
| 6750 | mode = 1; // "at most" |
| 6751 | else |
| 6752 | mode = 2; // "exactly" |
| 6753 | modeCount = FnTy->getNumArgs(); |
| 6754 | } |
| 6755 | |
| 6756 | std::string Description; |
| 6757 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 6758 | |
| 6759 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6760 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 6761 | << modeCount << NumFormalArgs; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6762 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6763 | } |
| 6764 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6765 | /// Diagnose a failed template-argument deduction. |
| 6766 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
| 6767 | Expr **Args, unsigned NumArgs) { |
| 6768 | FunctionDecl *Fn = Cand->Function; // pattern |
| 6769 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6770 | TemplateParameter Param = Cand->DeductionFailure.getTemplateParameter(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6771 | NamedDecl *ParamD; |
| 6772 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 6773 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 6774 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6775 | switch (Cand->DeductionFailure.Result) { |
| 6776 | case Sema::TDK_Success: |
| 6777 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 6778 | |
| 6779 | case Sema::TDK_Incomplete: { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6780 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| 6781 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_incomplete_deduction) |
| 6782 | << ParamD->getDeclName(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6783 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6784 | return; |
| 6785 | } |
| 6786 | |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 6787 | case Sema::TDK_Underqualified: { |
| 6788 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 6789 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 6790 | |
| 6791 | QualType Param = Cand->DeductionFailure.getFirstArg()->getAsType(); |
| 6792 | |
| 6793 | // Param will have been canonicalized, but it should just be a |
| 6794 | // qualified version of ParamD, so move the qualifiers to that. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6795 | QualifierCollector Qs; |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 6796 | Qs.strip(Param); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6797 | QualType NonCanonParam = Qs.apply(S.Context, TParam->getTypeForDecl()); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 6798 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 6799 | |
| 6800 | // Arg has also been canonicalized, but there's nothing we can do |
| 6801 | // about that. It also doesn't matter as much, because it won't |
| 6802 | // have any template parameters in it (because deduction isn't |
| 6803 | // done on dependent types). |
| 6804 | QualType Arg = Cand->DeductionFailure.getSecondArg()->getAsType(); |
| 6805 | |
| 6806 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_underqualified) |
| 6807 | << ParamD->getDeclName() << Arg << NonCanonParam; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6808 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 6809 | return; |
| 6810 | } |
| 6811 | |
| 6812 | case Sema::TDK_Inconsistent: { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6813 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6814 | int which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6815 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6816 | which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6817 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6818 | which = 1; |
| 6819 | else { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6820 | which = 2; |
| 6821 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6822 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6823 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_inconsistent_deduction) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6824 | << which << ParamD->getDeclName() |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6825 | << *Cand->DeductionFailure.getFirstArg() |
| 6826 | << *Cand->DeductionFailure.getSecondArg(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6827 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6828 | return; |
| 6829 | } |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 6830 | |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6831 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6832 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6833 | if (ParamD->getDeclName()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6834 | S.Diag(Fn->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6835 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
| 6836 | << ParamD->getDeclName(); |
| 6837 | else { |
| 6838 | int index = 0; |
| 6839 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 6840 | index = TTP->getIndex(); |
| 6841 | else if (NonTypeTemplateParmDecl *NTTP |
| 6842 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 6843 | index = NTTP->getIndex(); |
| 6844 | else |
| 6845 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6846 | S.Diag(Fn->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6847 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
| 6848 | << (index + 1); |
| 6849 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6850 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6851 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6852 | |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 6853 | case Sema::TDK_TooManyArguments: |
| 6854 | case Sema::TDK_TooFewArguments: |
| 6855 | DiagnoseArityMismatch(S, Cand, NumArgs); |
| 6856 | return; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 6857 | |
| 6858 | case Sema::TDK_InstantiationDepth: |
| 6859 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_instantiation_depth); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6860 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 6861 | return; |
| 6862 | |
| 6863 | case Sema::TDK_SubstitutionFailure: { |
| 6864 | std::string ArgString; |
| 6865 | if (TemplateArgumentList *Args |
| 6866 | = Cand->DeductionFailure.getTemplateArgumentList()) |
| 6867 | ArgString = S.getTemplateArgumentBindingsText( |
| 6868 | Fn->getDescribedFunctionTemplate()->getTemplateParameters(), |
| 6869 | *Args); |
| 6870 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_substitution_failure) |
| 6871 | << ArgString; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6872 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 6873 | return; |
| 6874 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6875 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6876 | // TODO: diagnose these individually, then kill off |
| 6877 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6878 | case Sema::TDK_NonDeducedMismatch: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6879 | case Sema::TDK_FailedOverloadResolution: |
| 6880 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6881 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6882 | return; |
| 6883 | } |
| 6884 | } |
| 6885 | |
| 6886 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 6887 | /// already generated a primary error at the call site. |
| 6888 | /// |
| 6889 | /// It really does need to be a single diagnostic with its caret |
| 6890 | /// pointed at the candidate declaration. Yes, this creates some |
| 6891 | /// major challenges of technical writing. Yes, this makes pointing |
| 6892 | /// out problems with specific arguments quite awkward. It's still |
| 6893 | /// better than generating twenty screens of text for every failed |
| 6894 | /// overload. |
| 6895 | /// |
| 6896 | /// It would be great to be able to express per-candidate problems |
| 6897 | /// more richly for those diagnostic clients that cared, but we'd |
| 6898 | /// still have to be just as careful with the default diagnostics. |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6899 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| 6900 | Expr **Args, unsigned NumArgs) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6901 | FunctionDecl *Fn = Cand->Function; |
| 6902 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6903 | // Note deleted candidates, but only if they're viable. |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 6904 | if (Cand->Viable && (Fn->isDeleted() || Fn->isUnavailable())) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6905 | std::string FnDesc; |
| 6906 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6907 | |
| 6908 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6909 | << FnKind << FnDesc << Fn->isDeleted(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6910 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6911 | return; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6912 | } |
| 6913 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6914 | // We don't really have anything else to say about viable candidates. |
| 6915 | if (Cand->Viable) { |
| 6916 | S.NoteOverloadCandidate(Fn); |
| 6917 | return; |
| 6918 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6919 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6920 | switch (Cand->FailureKind) { |
| 6921 | case ovl_fail_too_many_arguments: |
| 6922 | case ovl_fail_too_few_arguments: |
| 6923 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6924 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6925 | case ovl_fail_bad_deduction: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6926 | return DiagnoseBadDeduction(S, Cand, Args, NumArgs); |
| 6927 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6928 | case ovl_fail_trivial_conversion: |
| 6929 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 6930 | case ovl_fail_final_conversion_not_exact: |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6931 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6932 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 6933 | case ovl_fail_bad_conversion: { |
| 6934 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
| 6935 | for (unsigned N = Cand->Conversions.size(); I != N; ++I) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6936 | if (Cand->Conversions[I].isBad()) |
| 6937 | return DiagnoseBadConversion(S, Cand, I); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6938 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6939 | // FIXME: this currently happens when we're called from SemaInit |
| 6940 | // when user-conversion overload fails. Figure out how to handle |
| 6941 | // those conditions and diagnose them well. |
| 6942 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6943 | } |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 6944 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6945 | } |
| 6946 | |
| 6947 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 6948 | // Desugar the type of the surrogate down to a function type, |
| 6949 | // retaining as many typedefs as possible while still showing |
| 6950 | // the function type (and, therefore, its parameter types). |
| 6951 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 6952 | bool isLValueReference = false; |
| 6953 | bool isRValueReference = false; |
| 6954 | bool isPointer = false; |
| 6955 | if (const LValueReferenceType *FnTypeRef = |
| 6956 | FnType->getAs<LValueReferenceType>()) { |
| 6957 | FnType = FnTypeRef->getPointeeType(); |
| 6958 | isLValueReference = true; |
| 6959 | } else if (const RValueReferenceType *FnTypeRef = |
| 6960 | FnType->getAs<RValueReferenceType>()) { |
| 6961 | FnType = FnTypeRef->getPointeeType(); |
| 6962 | isRValueReference = true; |
| 6963 | } |
| 6964 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 6965 | FnType = FnTypePtr->getPointeeType(); |
| 6966 | isPointer = true; |
| 6967 | } |
| 6968 | // Desugar down to a function type. |
| 6969 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 6970 | // Reconstruct the pointer/reference as appropriate. |
| 6971 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 6972 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 6973 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 6974 | |
| 6975 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 6976 | << FnType; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6977 | MaybeEmitInheritedConstructorNote(S, Cand->Surrogate); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6978 | } |
| 6979 | |
| 6980 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 6981 | const char *Opc, |
| 6982 | SourceLocation OpLoc, |
| 6983 | OverloadCandidate *Cand) { |
| 6984 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
| 6985 | std::string TypeStr("operator"); |
| 6986 | TypeStr += Opc; |
| 6987 | TypeStr += "("; |
| 6988 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 6989 | if (Cand->Conversions.size() == 1) { |
| 6990 | TypeStr += ")"; |
| 6991 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 6992 | } else { |
| 6993 | TypeStr += ", "; |
| 6994 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 6995 | TypeStr += ")"; |
| 6996 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 6997 | } |
| 6998 | } |
| 6999 | |
| 7000 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 7001 | OverloadCandidate *Cand) { |
| 7002 | unsigned NoOperands = Cand->Conversions.size(); |
| 7003 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 7004 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7005 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 7006 | if (!ICS.isAmbiguous()) continue; |
| 7007 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7008 | ICS.DiagnoseAmbiguousConversion(S, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7009 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7010 | } |
| 7011 | } |
| 7012 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 7013 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 7014 | if (Cand->Function) |
| 7015 | return Cand->Function->getLocation(); |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 7016 | if (Cand->IsSurrogate) |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 7017 | return Cand->Surrogate->getLocation(); |
| 7018 | return SourceLocation(); |
| 7019 | } |
| 7020 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 7021 | struct CompareOverloadCandidatesForDisplay { |
| 7022 | Sema &S; |
| 7023 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7024 | |
| 7025 | bool operator()(const OverloadCandidate *L, |
| 7026 | const OverloadCandidate *R) { |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 7027 | // Fast-path this check. |
| 7028 | if (L == R) return false; |
| 7029 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7030 | // Order first by viability. |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 7031 | if (L->Viable) { |
| 7032 | if (!R->Viable) return true; |
| 7033 | |
| 7034 | // TODO: introduce a tri-valued comparison for overload |
| 7035 | // candidates. Would be more worthwhile if we had a sort |
| 7036 | // that could exploit it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7037 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation())) return true; |
| 7038 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation())) return false; |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 7039 | } else if (R->Viable) |
| 7040 | return false; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7041 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 7042 | assert(L->Viable == R->Viable); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7043 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 7044 | // Criteria by which we can sort non-viable candidates: |
| 7045 | if (!L->Viable) { |
| 7046 | // 1. Arity mismatches come after other candidates. |
| 7047 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 7048 | L->FailureKind == ovl_fail_too_few_arguments) |
| 7049 | return false; |
| 7050 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 7051 | R->FailureKind == ovl_fail_too_few_arguments) |
| 7052 | return true; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7053 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7054 | // 2. Bad conversions come first and are ordered by the number |
| 7055 | // of bad conversions and quality of good conversions. |
| 7056 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 7057 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 7058 | return true; |
| 7059 | |
| 7060 | // If there's any ordering between the defined conversions... |
| 7061 | // FIXME: this might not be transitive. |
| 7062 | assert(L->Conversions.size() == R->Conversions.size()); |
| 7063 | |
| 7064 | int leftBetter = 0; |
John McCall | 3a81337 | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 7065 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
| 7066 | for (unsigned E = L->Conversions.size(); I != E; ++I) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7067 | switch (CompareImplicitConversionSequences(S, |
| 7068 | L->Conversions[I], |
| 7069 | R->Conversions[I])) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7070 | case ImplicitConversionSequence::Better: |
| 7071 | leftBetter++; |
| 7072 | break; |
| 7073 | |
| 7074 | case ImplicitConversionSequence::Worse: |
| 7075 | leftBetter--; |
| 7076 | break; |
| 7077 | |
| 7078 | case ImplicitConversionSequence::Indistinguishable: |
| 7079 | break; |
| 7080 | } |
| 7081 | } |
| 7082 | if (leftBetter > 0) return true; |
| 7083 | if (leftBetter < 0) return false; |
| 7084 | |
| 7085 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 7086 | return false; |
| 7087 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 7088 | // TODO: others? |
| 7089 | } |
| 7090 | |
| 7091 | // Sort everything else by location. |
| 7092 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 7093 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 7094 | |
| 7095 | // Put candidates without locations (e.g. builtins) at the end. |
| 7096 | if (LLoc.isInvalid()) return false; |
| 7097 | if (RLoc.isInvalid()) return true; |
| 7098 | |
| 7099 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7100 | } |
| 7101 | }; |
| 7102 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7103 | /// CompleteNonViableCandidate - Normally, overload resolution only |
| 7104 | /// computes up to the first |
| 7105 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
| 7106 | Expr **Args, unsigned NumArgs) { |
| 7107 | assert(!Cand->Viable); |
| 7108 | |
| 7109 | // Don't do anything on failures other than bad conversion. |
| 7110 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 7111 | |
| 7112 | // Skip forward to the first bad conversion. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 7113 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7114 | unsigned ConvCount = Cand->Conversions.size(); |
| 7115 | while (true) { |
| 7116 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 7117 | ConvIdx++; |
| 7118 | if (Cand->Conversions[ConvIdx - 1].isBad()) |
| 7119 | break; |
| 7120 | } |
| 7121 | |
| 7122 | if (ConvIdx == ConvCount) |
| 7123 | return; |
| 7124 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 7125 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 7126 | "remaining conversion is initialized?"); |
| 7127 | |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 7128 | // FIXME: this should probably be preserved from the overload |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7129 | // operation somehow. |
| 7130 | bool SuppressUserConversions = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7131 | |
| 7132 | const FunctionProtoType* Proto; |
| 7133 | unsigned ArgIdx = ConvIdx; |
| 7134 | |
| 7135 | if (Cand->IsSurrogate) { |
| 7136 | QualType ConvType |
| 7137 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 7138 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 7139 | ConvType = ConvPtrType->getPointeeType(); |
| 7140 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 7141 | ArgIdx--; |
| 7142 | } else if (Cand->Function) { |
| 7143 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 7144 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 7145 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 7146 | ArgIdx--; |
| 7147 | } else { |
| 7148 | // Builtin binary operator with a bad first conversion. |
| 7149 | assert(ConvCount <= 3); |
| 7150 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 7151 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7152 | = TryCopyInitialization(S, Args[ConvIdx], |
| 7153 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7154 | SuppressUserConversions, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7155 | /*InOverloadResolution*/ true); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7156 | return; |
| 7157 | } |
| 7158 | |
| 7159 | // Fill in the rest of the conversions. |
| 7160 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 7161 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
| 7162 | if (ArgIdx < NumArgsInProto) |
| 7163 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7164 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7165 | SuppressUserConversions, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7166 | /*InOverloadResolution=*/true); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7167 | else |
| 7168 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 7169 | } |
| 7170 | } |
| 7171 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7172 | } // end anonymous namespace |
| 7173 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7174 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 7175 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7176 | /// set. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7177 | void OverloadCandidateSet::NoteCandidates(Sema &S, |
| 7178 | OverloadCandidateDisplayKind OCD, |
| 7179 | Expr **Args, unsigned NumArgs, |
| 7180 | const char *Opc, |
| 7181 | SourceLocation OpLoc) { |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7182 | // Sort the candidates by viability and position. Sorting directly would |
| 7183 | // be prohibitive, so we make a set of pointers and sort those. |
| 7184 | llvm::SmallVector<OverloadCandidate*, 32> Cands; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7185 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
| 7186 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7187 | if (Cand->Viable) |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7188 | Cands.push_back(Cand); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7189 | else if (OCD == OCD_AllCandidates) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7190 | CompleteNonViableCandidate(S, Cand, Args, NumArgs); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7191 | if (Cand->Function || Cand->IsSurrogate) |
| 7192 | Cands.push_back(Cand); |
| 7193 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 7194 | // want to list every possible builtin candidate. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7195 | } |
| 7196 | } |
| 7197 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 7198 | std::sort(Cands.begin(), Cands.end(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7199 | CompareOverloadCandidatesForDisplay(S)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7200 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7201 | bool ReportedAmbiguousConversions = false; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7202 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7203 | llvm::SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7204 | const Diagnostic::OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7205 | unsigned CandsShown = 0; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7206 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 7207 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 7208 | |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7209 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 7210 | // the user with. FIXME: This limit should depend on details of the |
| 7211 | // candidate list. |
| 7212 | if (CandsShown >= 4 && ShowOverloads == Diagnostic::Ovl_Best) { |
| 7213 | break; |
| 7214 | } |
| 7215 | ++CandsShown; |
| 7216 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7217 | if (Cand->Function) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7218 | NoteFunctionCandidate(S, Cand, Args, NumArgs); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7219 | else if (Cand->IsSurrogate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7220 | NoteSurrogateCandidate(S, Cand); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7221 | else { |
| 7222 | assert(Cand->Viable && |
| 7223 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7224 | // Generally we only see ambiguities including viable builtin |
| 7225 | // operators if overload resolution got screwed up by an |
| 7226 | // ambiguous user-defined conversion. |
| 7227 | // |
| 7228 | // FIXME: It's quite possible for different conversions to see |
| 7229 | // different ambiguities, though. |
| 7230 | if (!ReportedAmbiguousConversions) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7231 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7232 | ReportedAmbiguousConversions = true; |
| 7233 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7234 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7235 | // If this is a viable builtin, print it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7236 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7237 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7238 | } |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7239 | |
| 7240 | if (I != E) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7241 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7242 | } |
| 7243 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7244 | // [PossiblyAFunctionType] --> [Return] |
| 7245 | // NonFunctionType --> NonFunctionType |
| 7246 | // R (A) --> R(A) |
| 7247 | // R (*)(A) --> R (A) |
| 7248 | // R (&)(A) --> R (A) |
| 7249 | // R (S::*)(A) --> R (A) |
| 7250 | QualType Sema::ExtractUnqualifiedFunctionType(QualType PossiblyAFunctionType) { |
| 7251 | QualType Ret = PossiblyAFunctionType; |
| 7252 | if (const PointerType *ToTypePtr = |
| 7253 | PossiblyAFunctionType->getAs<PointerType>()) |
| 7254 | Ret = ToTypePtr->getPointeeType(); |
| 7255 | else if (const ReferenceType *ToTypeRef = |
| 7256 | PossiblyAFunctionType->getAs<ReferenceType>()) |
| 7257 | Ret = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 7258 | else if (const MemberPointerType *MemTypePtr = |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7259 | PossiblyAFunctionType->getAs<MemberPointerType>()) |
| 7260 | Ret = MemTypePtr->getPointeeType(); |
| 7261 | Ret = |
| 7262 | Context.getCanonicalType(Ret).getUnqualifiedType(); |
| 7263 | return Ret; |
| 7264 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7265 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7266 | // A helper class to help with address of function resolution |
| 7267 | // - allows us to avoid passing around all those ugly parameters |
| 7268 | class AddressOfFunctionResolver |
| 7269 | { |
| 7270 | Sema& S; |
| 7271 | Expr* SourceExpr; |
| 7272 | const QualType& TargetType; |
| 7273 | QualType TargetFunctionType; // Extracted function type from target type |
| 7274 | |
| 7275 | bool Complain; |
| 7276 | //DeclAccessPair& ResultFunctionAccessPair; |
| 7277 | ASTContext& Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7278 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7279 | bool TargetTypeIsNonStaticMemberFunction; |
| 7280 | bool FoundNonTemplateFunction; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7281 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7282 | OverloadExpr::FindResult OvlExprInfo; |
| 7283 | OverloadExpr *OvlExpr; |
| 7284 | TemplateArgumentListInfo OvlExplicitTemplateArgs; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7285 | llvm::SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7286 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7287 | public: |
| 7288 | AddressOfFunctionResolver(Sema &S, Expr* SourceExpr, |
| 7289 | const QualType& TargetType, bool Complain) |
| 7290 | : S(S), SourceExpr(SourceExpr), TargetType(TargetType), |
| 7291 | Complain(Complain), Context(S.getASTContext()), |
| 7292 | TargetTypeIsNonStaticMemberFunction( |
| 7293 | !!TargetType->getAs<MemberPointerType>()), |
| 7294 | FoundNonTemplateFunction(false), |
| 7295 | OvlExprInfo(OverloadExpr::find(SourceExpr)), |
| 7296 | OvlExpr(OvlExprInfo.Expression) |
| 7297 | { |
| 7298 | ExtractUnqualifiedFunctionTypeFromTargetType(); |
| 7299 | |
| 7300 | if (!TargetFunctionType->isFunctionType()) { |
| 7301 | if (OvlExpr->hasExplicitTemplateArgs()) { |
| 7302 | DeclAccessPair dap; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7303 | if (FunctionDecl* Fn = S.ResolveSingleFunctionTemplateSpecialization( |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7304 | OvlExpr, false, &dap) ) { |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 7305 | |
| 7306 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 7307 | if (!Method->isStatic()) { |
| 7308 | // If the target type is a non-function type and the function |
| 7309 | // found is a non-static member function, pretend as if that was |
| 7310 | // the target, it's the only possible type to end up with. |
| 7311 | TargetTypeIsNonStaticMemberFunction = true; |
| 7312 | |
| 7313 | // And skip adding the function if its not in the proper form. |
| 7314 | // We'll diagnose this due to an empty set of functions. |
| 7315 | if (!OvlExprInfo.HasFormOfMemberPointer) |
| 7316 | return; |
| 7317 | } |
| 7318 | } |
| 7319 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7320 | Matches.push_back(std::make_pair(dap,Fn)); |
| 7321 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 7322 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7323 | return; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 7324 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7325 | |
| 7326 | if (OvlExpr->hasExplicitTemplateArgs()) |
| 7327 | OvlExpr->getExplicitTemplateArgs().copyInto(OvlExplicitTemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7328 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7329 | if (FindAllFunctionsThatMatchTargetTypeExactly()) { |
| 7330 | // C++ [over.over]p4: |
| 7331 | // If more than one function is selected, [...] |
| 7332 | if (Matches.size() > 1) { |
| 7333 | if (FoundNonTemplateFunction) |
| 7334 | EliminateAllTemplateMatches(); |
| 7335 | else |
| 7336 | EliminateAllExceptMostSpecializedTemplate(); |
| 7337 | } |
| 7338 | } |
| 7339 | } |
| 7340 | |
| 7341 | private: |
| 7342 | bool isTargetTypeAFunction() const { |
| 7343 | return TargetFunctionType->isFunctionType(); |
| 7344 | } |
| 7345 | |
| 7346 | // [ToType] [Return] |
| 7347 | |
| 7348 | // R (*)(A) --> R (A), IsNonStaticMemberFunction = false |
| 7349 | // R (&)(A) --> R (A), IsNonStaticMemberFunction = false |
| 7350 | // R (S::*)(A) --> R (A), IsNonStaticMemberFunction = true |
| 7351 | void inline ExtractUnqualifiedFunctionTypeFromTargetType() { |
| 7352 | TargetFunctionType = S.ExtractUnqualifiedFunctionType(TargetType); |
| 7353 | } |
| 7354 | |
| 7355 | // return true if any matching specializations were found |
| 7356 | bool AddMatchingTemplateFunction(FunctionTemplateDecl* FunctionTemplate, |
| 7357 | const DeclAccessPair& CurAccessFunPair) { |
| 7358 | if (CXXMethodDecl *Method |
| 7359 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
| 7360 | // Skip non-static function templates when converting to pointer, and |
| 7361 | // static when converting to member pointer. |
| 7362 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 7363 | return false; |
| 7364 | } |
| 7365 | else if (TargetTypeIsNonStaticMemberFunction) |
| 7366 | return false; |
| 7367 | |
| 7368 | // C++ [over.over]p2: |
| 7369 | // If the name is a function template, template argument deduction is |
| 7370 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 7371 | // resulting template argument list is used to generate a single |
| 7372 | // function template specialization, which is added to the set of |
| 7373 | // overloaded functions considered. |
| 7374 | FunctionDecl *Specialization = 0; |
| 7375 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
| 7376 | if (Sema::TemplateDeductionResult Result |
| 7377 | = S.DeduceTemplateArguments(FunctionTemplate, |
| 7378 | &OvlExplicitTemplateArgs, |
| 7379 | TargetFunctionType, Specialization, |
| 7380 | Info)) { |
| 7381 | // FIXME: make a note of the failed deduction for diagnostics. |
| 7382 | (void)Result; |
| 7383 | return false; |
| 7384 | } |
| 7385 | |
| 7386 | // Template argument deduction ensures that we have an exact match. |
| 7387 | // This function template specicalization works. |
| 7388 | Specialization = cast<FunctionDecl>(Specialization->getCanonicalDecl()); |
| 7389 | assert(TargetFunctionType |
| 7390 | == Context.getCanonicalType(Specialization->getType())); |
| 7391 | Matches.push_back(std::make_pair(CurAccessFunPair, Specialization)); |
| 7392 | return true; |
| 7393 | } |
| 7394 | |
| 7395 | bool AddMatchingNonTemplateFunction(NamedDecl* Fn, |
| 7396 | const DeclAccessPair& CurAccessFunPair) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 7397 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 7398 | // Skip non-static functions when converting to pointer, and static |
| 7399 | // when converting to member pointer. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7400 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 7401 | return false; |
| 7402 | } |
| 7403 | else if (TargetTypeIsNonStaticMemberFunction) |
| 7404 | return false; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7405 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 7406 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 7407 | QualType ResultTy; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7408 | if (Context.hasSameUnqualifiedType(TargetFunctionType, |
| 7409 | FunDecl->getType()) || |
| 7410 | IsNoReturnConversion(Context, FunDecl->getType(), TargetFunctionType, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 7411 | ResultTy)) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7412 | Matches.push_back(std::make_pair(CurAccessFunPair, |
| 7413 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 7414 | FoundNonTemplateFunction = true; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7415 | return true; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 7416 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7417 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7418 | |
| 7419 | return false; |
| 7420 | } |
| 7421 | |
| 7422 | bool FindAllFunctionsThatMatchTargetTypeExactly() { |
| 7423 | bool Ret = false; |
| 7424 | |
| 7425 | // If the overload expression doesn't have the form of a pointer to |
| 7426 | // member, don't try to convert it to a pointer-to-member type. |
| 7427 | if (IsInvalidFormOfPointerToMemberFunction()) |
| 7428 | return false; |
| 7429 | |
| 7430 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 7431 | E = OvlExpr->decls_end(); |
| 7432 | I != E; ++I) { |
| 7433 | // Look through any using declarations to find the underlying function. |
| 7434 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 7435 | |
| 7436 | // C++ [over.over]p3: |
| 7437 | // Non-member functions and static member functions match |
| 7438 | // targets of type "pointer-to-function" or "reference-to-function." |
| 7439 | // Nonstatic member functions match targets of |
| 7440 | // type "pointer-to-member-function." |
| 7441 | // Note that according to DR 247, the containing class does not matter. |
| 7442 | if (FunctionTemplateDecl *FunctionTemplate |
| 7443 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
| 7444 | if (AddMatchingTemplateFunction(FunctionTemplate, I.getPair())) |
| 7445 | Ret = true; |
| 7446 | } |
| 7447 | // If we have explicit template arguments supplied, skip non-templates. |
| 7448 | else if (!OvlExpr->hasExplicitTemplateArgs() && |
| 7449 | AddMatchingNonTemplateFunction(Fn, I.getPair())) |
| 7450 | Ret = true; |
| 7451 | } |
| 7452 | assert(Ret || Matches.empty()); |
| 7453 | return Ret; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7454 | } |
| 7455 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7456 | void EliminateAllExceptMostSpecializedTemplate() { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7457 | // [...] and any given function template specialization F1 is |
| 7458 | // eliminated if the set contains a second function template |
| 7459 | // specialization whose function template is more specialized |
| 7460 | // than the function template of F1 according to the partial |
| 7461 | // ordering rules of 14.5.5.2. |
| 7462 | |
| 7463 | // The algorithm specified above is quadratic. We instead use a |
| 7464 | // two-pass algorithm (similar to the one used to identify the |
| 7465 | // best viable function in an overload set) that identifies the |
| 7466 | // best function template (if it exists). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7467 | |
| 7468 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 7469 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 7470 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7471 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7472 | UnresolvedSetIterator Result = |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7473 | S.getMostSpecialized(MatchesCopy.begin(), MatchesCopy.end(), |
| 7474 | TPOC_Other, 0, SourceExpr->getLocStart(), |
| 7475 | S.PDiag(), |
| 7476 | S.PDiag(diag::err_addr_ovl_ambiguous) |
| 7477 | << Matches[0].second->getDeclName(), |
| 7478 | S.PDiag(diag::note_ovl_candidate) |
| 7479 | << (unsigned) oc_function_template, |
| 7480 | Complain); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7481 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7482 | if (Result != MatchesCopy.end()) { |
| 7483 | // Make it the first and only element |
| 7484 | Matches[0].first = Matches[Result - MatchesCopy.begin()].first; |
| 7485 | Matches[0].second = cast<FunctionDecl>(*Result); |
| 7486 | Matches.resize(1); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7487 | } |
| 7488 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7489 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7490 | void EliminateAllTemplateMatches() { |
| 7491 | // [...] any function template specializations in the set are |
| 7492 | // eliminated if the set also contains a non-template function, [...] |
| 7493 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
| 7494 | if (Matches[I].second->getPrimaryTemplate() == 0) |
| 7495 | ++I; |
| 7496 | else { |
| 7497 | Matches[I] = Matches[--N]; |
| 7498 | Matches.set_size(N); |
| 7499 | } |
| 7500 | } |
| 7501 | } |
| 7502 | |
| 7503 | public: |
| 7504 | void ComplainNoMatchesFound() const { |
| 7505 | assert(Matches.empty()); |
| 7506 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_no_viable) |
| 7507 | << OvlExpr->getName() << TargetFunctionType |
| 7508 | << OvlExpr->getSourceRange(); |
| 7509 | S.NoteAllOverloadCandidates(OvlExpr); |
| 7510 | } |
| 7511 | |
| 7512 | bool IsInvalidFormOfPointerToMemberFunction() const { |
| 7513 | return TargetTypeIsNonStaticMemberFunction && |
| 7514 | !OvlExprInfo.HasFormOfMemberPointer; |
| 7515 | } |
| 7516 | |
| 7517 | void ComplainIsInvalidFormOfPointerToMemberFunction() const { |
| 7518 | // TODO: Should we condition this on whether any functions might |
| 7519 | // have matched, or is it more appropriate to do that in callers? |
| 7520 | // TODO: a fixit wouldn't hurt. |
| 7521 | S.Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
| 7522 | << TargetType << OvlExpr->getSourceRange(); |
| 7523 | } |
| 7524 | |
| 7525 | void ComplainOfInvalidConversion() const { |
| 7526 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 7527 | << OvlExpr->getName() << TargetType; |
| 7528 | } |
| 7529 | |
| 7530 | void ComplainMultipleMatchesFound() const { |
| 7531 | assert(Matches.size() > 1); |
| 7532 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 7533 | << OvlExpr->getName() |
| 7534 | << OvlExpr->getSourceRange(); |
| 7535 | S.NoteAllOverloadCandidates(OvlExpr); |
| 7536 | } |
| 7537 | |
| 7538 | int getNumMatches() const { return Matches.size(); } |
| 7539 | |
| 7540 | FunctionDecl* getMatchingFunctionDecl() const { |
| 7541 | if (Matches.size() != 1) return 0; |
| 7542 | return Matches[0].second; |
| 7543 | } |
| 7544 | |
| 7545 | const DeclAccessPair* getMatchingFunctionAccessPair() const { |
| 7546 | if (Matches.size() != 1) return 0; |
| 7547 | return &Matches[0].first; |
| 7548 | } |
| 7549 | }; |
| 7550 | |
| 7551 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 7552 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 7553 | /// expression with overloaded function type and @p ToType is the type |
| 7554 | /// we're trying to resolve to. For example: |
| 7555 | /// |
| 7556 | /// @code |
| 7557 | /// int f(double); |
| 7558 | /// int f(int); |
| 7559 | /// |
| 7560 | /// int (*pfd)(double) = f; // selects f(double) |
| 7561 | /// @endcode |
| 7562 | /// |
| 7563 | /// This routine returns the resulting FunctionDecl if it could be |
| 7564 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 7565 | /// routine will emit diagnostics if there is an error. |
| 7566 | FunctionDecl * |
| 7567 | Sema::ResolveAddressOfOverloadedFunction(Expr *AddressOfExpr, QualType TargetType, |
| 7568 | bool Complain, |
| 7569 | DeclAccessPair &FoundResult) { |
| 7570 | |
| 7571 | assert(AddressOfExpr->getType() == Context.OverloadTy); |
| 7572 | |
| 7573 | AddressOfFunctionResolver Resolver(*this, AddressOfExpr, TargetType, Complain); |
| 7574 | int NumMatches = Resolver.getNumMatches(); |
| 7575 | FunctionDecl* Fn = 0; |
| 7576 | if ( NumMatches == 0 && Complain) { |
| 7577 | if (Resolver.IsInvalidFormOfPointerToMemberFunction()) |
| 7578 | Resolver.ComplainIsInvalidFormOfPointerToMemberFunction(); |
| 7579 | else |
| 7580 | Resolver.ComplainNoMatchesFound(); |
| 7581 | } |
| 7582 | else if (NumMatches > 1 && Complain) |
| 7583 | Resolver.ComplainMultipleMatchesFound(); |
| 7584 | else if (NumMatches == 1) { |
| 7585 | Fn = Resolver.getMatchingFunctionDecl(); |
| 7586 | assert(Fn); |
| 7587 | FoundResult = *Resolver.getMatchingFunctionAccessPair(); |
| 7588 | MarkDeclarationReferenced(AddressOfExpr->getLocStart(), Fn); |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 7589 | if (Complain) |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7590 | CheckAddressOfMemberAccess(AddressOfExpr, FoundResult); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 7591 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7592 | |
| 7593 | return Fn; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7594 | } |
| 7595 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7596 | /// \brief Given an expression that refers to an overloaded function, try to |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7597 | /// resolve that overloaded function expression down to a single function. |
| 7598 | /// |
| 7599 | /// This routine can only resolve template-ids that refer to a single function |
| 7600 | /// template, where that template-id refers to a single template whose template |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7601 | /// arguments are either provided by the template-id or have defaults, |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7602 | /// as described in C++0x [temp.arg.explicit]p3. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7603 | FunctionDecl * |
| 7604 | Sema::ResolveSingleFunctionTemplateSpecialization(OverloadExpr *ovl, |
| 7605 | bool Complain, |
| 7606 | DeclAccessPair *FoundResult) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7607 | // C++ [over.over]p1: |
| 7608 | // [...] [Note: any redundant set of parentheses surrounding the |
| 7609 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7610 | // C++ [over.over]p1: |
| 7611 | // [...] The overloaded function name can be preceded by the & |
| 7612 | // operator. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7613 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7614 | // If we didn't actually find any template-ids, we're done. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7615 | if (!ovl->hasExplicitTemplateArgs()) |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7616 | return 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 7617 | |
| 7618 | TemplateArgumentListInfo ExplicitTemplateArgs; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7619 | ovl->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7620 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7621 | // Look through all of the overloaded functions, searching for one |
| 7622 | // whose type matches exactly. |
| 7623 | FunctionDecl *Matched = 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7624 | for (UnresolvedSetIterator I = ovl->decls_begin(), |
| 7625 | E = ovl->decls_end(); I != E; ++I) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7626 | // C++0x [temp.arg.explicit]p3: |
| 7627 | // [...] In contexts where deduction is done and fails, or in contexts |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7628 | // where deduction is not done, if a template argument list is |
| 7629 | // specified and it, along with any default template arguments, |
| 7630 | // identifies a single function template specialization, then the |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7631 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | 66a8c9a | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 7632 | FunctionTemplateDecl *FunctionTemplate |
| 7633 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7634 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7635 | // C++ [over.over]p2: |
| 7636 | // If the name is a function template, template argument deduction is |
| 7637 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 7638 | // resulting template argument list is used to generate a single |
| 7639 | // function template specialization, which is added to the set of |
| 7640 | // overloaded functions considered. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7641 | FunctionDecl *Specialization = 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7642 | TemplateDeductionInfo Info(Context, ovl->getNameLoc()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7643 | if (TemplateDeductionResult Result |
| 7644 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 7645 | Specialization, Info)) { |
| 7646 | // FIXME: make a note of the failed deduction for diagnostics. |
| 7647 | (void)Result; |
| 7648 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7649 | } |
| 7650 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7651 | assert(Specialization && "no specialization and no error?"); |
| 7652 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7653 | // Multiple matches; we can't resolve to a single declaration. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7654 | if (Matched) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7655 | if (Complain) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7656 | Diag(ovl->getExprLoc(), diag::err_addr_ovl_ambiguous) |
| 7657 | << ovl->getName(); |
| 7658 | NoteAllOverloadCandidates(ovl); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7659 | } |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7660 | return 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7661 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7662 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7663 | Matched = Specialization; |
| 7664 | if (FoundResult) *FoundResult = I.getPair(); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7665 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7666 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7667 | return Matched; |
| 7668 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7669 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7670 | |
| 7671 | |
| 7672 | |
| 7673 | // Resolve and fix an overloaded expression that |
| 7674 | // can be resolved because it identifies a single function |
| 7675 | // template specialization |
| 7676 | // Last three arguments should only be supplied if Complain = true |
| 7677 | ExprResult Sema::ResolveAndFixSingleFunctionTemplateSpecialization( |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7678 | Expr *SrcExpr, bool doFunctionPointerConverion, bool complain, |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7679 | const SourceRange& OpRangeForComplaining, |
| 7680 | QualType DestTypeForComplaining, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7681 | unsigned DiagIDForComplaining) { |
| 7682 | assert(SrcExpr->getType() == Context.OverloadTy); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7683 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7684 | OverloadExpr::FindResult ovl = OverloadExpr::find(SrcExpr); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7685 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7686 | DeclAccessPair found; |
| 7687 | ExprResult SingleFunctionExpression; |
| 7688 | if (FunctionDecl *fn = ResolveSingleFunctionTemplateSpecialization( |
| 7689 | ovl.Expression, /*complain*/ false, &found)) { |
| 7690 | if (DiagnoseUseOfDecl(fn, SrcExpr->getSourceRange().getBegin())) |
| 7691 | return ExprError(); |
| 7692 | |
| 7693 | // It is only correct to resolve to an instance method if we're |
| 7694 | // resolving a form that's permitted to be a pointer to member. |
| 7695 | // Otherwise we'll end up making a bound member expression, which |
| 7696 | // is illegal in all the contexts we resolve like this. |
| 7697 | if (!ovl.HasFormOfMemberPointer && |
| 7698 | isa<CXXMethodDecl>(fn) && |
| 7699 | cast<CXXMethodDecl>(fn)->isInstance()) { |
| 7700 | if (complain) { |
| 7701 | Diag(ovl.Expression->getExprLoc(), |
| 7702 | diag::err_invalid_use_of_bound_member_func) |
| 7703 | << ovl.Expression->getSourceRange(); |
| 7704 | // TODO: I believe we only end up here if there's a mix of |
| 7705 | // static and non-static candidates (otherwise the expression |
| 7706 | // would have 'bound member' type, not 'overload' type). |
| 7707 | // Ideally we would note which candidate was chosen and why |
| 7708 | // the static candidates were rejected. |
| 7709 | } |
| 7710 | |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 7711 | return ExprError(); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7712 | } |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 7713 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7714 | // Fix the expresion to refer to 'fn'. |
| 7715 | SingleFunctionExpression = |
| 7716 | Owned(FixOverloadedFunctionReference(SrcExpr, found, fn)); |
| 7717 | |
| 7718 | // If desired, do function-to-pointer decay. |
| 7719 | if (doFunctionPointerConverion) |
| 7720 | SingleFunctionExpression = |
| 7721 | DefaultFunctionArrayLvalueConversion(SingleFunctionExpression.take()); |
| 7722 | } |
| 7723 | |
| 7724 | if (!SingleFunctionExpression.isUsable()) { |
| 7725 | if (complain) { |
| 7726 | Diag(OpRangeForComplaining.getBegin(), DiagIDForComplaining) |
| 7727 | << ovl.Expression->getName() |
| 7728 | << DestTypeForComplaining |
| 7729 | << OpRangeForComplaining |
| 7730 | << ovl.Expression->getQualifierLoc().getSourceRange(); |
| 7731 | NoteAllOverloadCandidates(SrcExpr); |
| 7732 | } |
| 7733 | return ExprError(); |
| 7734 | } |
| 7735 | |
| 7736 | return SingleFunctionExpression; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7737 | } |
| 7738 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7739 | /// \brief Add a single candidate to the overload set. |
| 7740 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7741 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 7742 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7743 | Expr **Args, unsigned NumArgs, |
| 7744 | OverloadCandidateSet &CandidateSet, |
| 7745 | bool PartialOverloading) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7746 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7747 | if (isa<UsingShadowDecl>(Callee)) |
| 7748 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 7749 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7750 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7751 | assert(!ExplicitTemplateArgs && "Explicit template arguments?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7752 | S.AddOverloadCandidate(Func, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 7753 | false, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7754 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7755 | } |
| 7756 | |
| 7757 | if (FunctionTemplateDecl *FuncTemplate |
| 7758 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7759 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
| 7760 | ExplicitTemplateArgs, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7761 | Args, NumArgs, CandidateSet); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7762 | return; |
| 7763 | } |
| 7764 | |
| 7765 | assert(false && "unhandled case in overloaded call candidate"); |
| 7766 | |
| 7767 | // do nothing? |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7768 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7769 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7770 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 7771 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7772 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7773 | Expr **Args, unsigned NumArgs, |
| 7774 | OverloadCandidateSet &CandidateSet, |
| 7775 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7776 | |
| 7777 | #ifndef NDEBUG |
| 7778 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 7779 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7780 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7781 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 7782 | // and let Y be the lookup set produced by argument dependent |
| 7783 | // lookup (defined as follows). If X contains |
| 7784 | // |
| 7785 | // -- a declaration of a class member, or |
| 7786 | // |
| 7787 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7788 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7789 | // |
| 7790 | // -- a declaration that is neither a function or a function |
| 7791 | // template |
| 7792 | // |
| 7793 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7794 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7795 | if (ULE->requiresADL()) { |
| 7796 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 7797 | E = ULE->decls_end(); I != E; ++I) { |
| 7798 | assert(!(*I)->getDeclContext()->isRecord()); |
| 7799 | assert(isa<UsingShadowDecl>(*I) || |
| 7800 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 7801 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7802 | } |
| 7803 | } |
| 7804 | #endif |
| 7805 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7806 | // It would be nice to avoid this copy. |
| 7807 | TemplateArgumentListInfo TABuffer; |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 7808 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7809 | if (ULE->hasExplicitTemplateArgs()) { |
| 7810 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 7811 | ExplicitTemplateArgs = &TABuffer; |
| 7812 | } |
| 7813 | |
| 7814 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 7815 | E = ULE->decls_end(); I != E; ++I) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7816 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7817 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7818 | PartialOverloading); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7819 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7820 | if (ULE->requiresADL()) |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7821 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
| 7822 | Args, NumArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7823 | ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7824 | CandidateSet, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 7825 | PartialOverloading, |
| 7826 | ULE->isStdAssociatedNamespace()); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7827 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7828 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 7829 | /// Attempt to recover from an ill-formed use of a non-dependent name in a |
| 7830 | /// template, where the non-dependent name was declared after the template |
| 7831 | /// was defined. This is common in code written for a compilers which do not |
| 7832 | /// correctly implement two-stage name lookup. |
| 7833 | /// |
| 7834 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 7835 | static bool |
| 7836 | DiagnoseTwoPhaseLookup(Sema &SemaRef, SourceLocation FnLoc, |
| 7837 | const CXXScopeSpec &SS, LookupResult &R, |
| 7838 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 7839 | Expr **Args, unsigned NumArgs) { |
| 7840 | if (SemaRef.ActiveTemplateInstantiations.empty() || !SS.isEmpty()) |
| 7841 | return false; |
| 7842 | |
| 7843 | for (DeclContext *DC = SemaRef.CurContext; DC; DC = DC->getParent()) { |
| 7844 | SemaRef.LookupQualifiedName(R, DC); |
| 7845 | |
| 7846 | if (!R.empty()) { |
| 7847 | R.suppressDiagnostics(); |
| 7848 | |
| 7849 | if (isa<CXXRecordDecl>(DC)) { |
| 7850 | // Don't diagnose names we find in classes; we get much better |
| 7851 | // diagnostics for these from DiagnoseEmptyLookup. |
| 7852 | R.clear(); |
| 7853 | return false; |
| 7854 | } |
| 7855 | |
| 7856 | OverloadCandidateSet Candidates(FnLoc); |
| 7857 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 7858 | AddOverloadedCallCandidate(SemaRef, I.getPair(), |
| 7859 | ExplicitTemplateArgs, Args, NumArgs, |
| 7860 | Candidates, false); |
| 7861 | |
| 7862 | OverloadCandidateSet::iterator Best; |
| 7863 | if (Candidates.BestViableFunction(SemaRef, FnLoc, Best) != OR_Success) |
| 7864 | // No viable functions. Don't bother the user with notes for functions |
| 7865 | // which don't work and shouldn't be found anyway. |
| 7866 | return false; |
| 7867 | |
| 7868 | // Find the namespaces where ADL would have looked, and suggest |
| 7869 | // declaring the function there instead. |
| 7870 | Sema::AssociatedNamespaceSet AssociatedNamespaces; |
| 7871 | Sema::AssociatedClassSet AssociatedClasses; |
| 7872 | SemaRef.FindAssociatedClassesAndNamespaces(Args, NumArgs, |
| 7873 | AssociatedNamespaces, |
| 7874 | AssociatedClasses); |
| 7875 | // Never suggest declaring a function within namespace 'std'. |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 7876 | Sema::AssociatedNamespaceSet SuggestedNamespaces; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 7877 | if (DeclContext *Std = SemaRef.getStdNamespace()) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 7878 | for (Sema::AssociatedNamespaceSet::iterator |
| 7879 | it = AssociatedNamespaces.begin(), |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 7880 | end = AssociatedNamespaces.end(); it != end; ++it) { |
| 7881 | if (!Std->Encloses(*it)) |
| 7882 | SuggestedNamespaces.insert(*it); |
| 7883 | } |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 7884 | } |
| 7885 | |
| 7886 | SemaRef.Diag(R.getNameLoc(), diag::err_not_found_by_two_phase_lookup) |
| 7887 | << R.getLookupName(); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 7888 | if (SuggestedNamespaces.empty()) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 7889 | SemaRef.Diag(Best->Function->getLocation(), |
| 7890 | diag::note_not_found_by_two_phase_lookup) |
| 7891 | << R.getLookupName() << 0; |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 7892 | } else if (SuggestedNamespaces.size() == 1) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 7893 | SemaRef.Diag(Best->Function->getLocation(), |
| 7894 | diag::note_not_found_by_two_phase_lookup) |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 7895 | << R.getLookupName() << 1 << *SuggestedNamespaces.begin(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 7896 | } else { |
| 7897 | // FIXME: It would be useful to list the associated namespaces here, |
| 7898 | // but the diagnostics infrastructure doesn't provide a way to produce |
| 7899 | // a localized representation of a list of items. |
| 7900 | SemaRef.Diag(Best->Function->getLocation(), |
| 7901 | diag::note_not_found_by_two_phase_lookup) |
| 7902 | << R.getLookupName() << 2; |
| 7903 | } |
| 7904 | |
| 7905 | // Try to recover by calling this function. |
| 7906 | return true; |
| 7907 | } |
| 7908 | |
| 7909 | R.clear(); |
| 7910 | } |
| 7911 | |
| 7912 | return false; |
| 7913 | } |
| 7914 | |
| 7915 | /// Attempt to recover from ill-formed use of a non-dependent operator in a |
| 7916 | /// template, where the non-dependent operator was declared after the template |
| 7917 | /// was defined. |
| 7918 | /// |
| 7919 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 7920 | static bool |
| 7921 | DiagnoseTwoPhaseOperatorLookup(Sema &SemaRef, OverloadedOperatorKind Op, |
| 7922 | SourceLocation OpLoc, |
| 7923 | Expr **Args, unsigned NumArgs) { |
| 7924 | DeclarationName OpName = |
| 7925 | SemaRef.Context.DeclarationNames.getCXXOperatorName(Op); |
| 7926 | LookupResult R(SemaRef, OpName, OpLoc, Sema::LookupOperatorName); |
| 7927 | return DiagnoseTwoPhaseLookup(SemaRef, OpLoc, CXXScopeSpec(), R, |
| 7928 | /*ExplicitTemplateArgs=*/0, Args, NumArgs); |
| 7929 | } |
| 7930 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7931 | /// Attempts to recover from a call where no functions were found. |
| 7932 | /// |
| 7933 | /// Returns true if new candidates were found. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7934 | static ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 7935 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7936 | UnresolvedLookupExpr *ULE, |
| 7937 | SourceLocation LParenLoc, |
| 7938 | Expr **Args, unsigned NumArgs, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 7939 | SourceLocation RParenLoc, |
| 7940 | bool EmptyLookup) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7941 | |
| 7942 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 7943 | SS.Adopt(ULE->getQualifierLoc()); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7944 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7945 | TemplateArgumentListInfo TABuffer; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 7946 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7947 | if (ULE->hasExplicitTemplateArgs()) { |
| 7948 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 7949 | ExplicitTemplateArgs = &TABuffer; |
| 7950 | } |
| 7951 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7952 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 7953 | Sema::LookupOrdinaryName); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 7954 | if (!DiagnoseTwoPhaseLookup(SemaRef, Fn->getExprLoc(), SS, R, |
| 7955 | ExplicitTemplateArgs, Args, NumArgs) && |
| 7956 | (!EmptyLookup || |
| 7957 | SemaRef.DiagnoseEmptyLookup(S, SS, R, Sema::CTC_Expression))) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7958 | return ExprError(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7959 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7960 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 7961 | |
| 7962 | // Build an implicit member call if appropriate. Just drop the |
| 7963 | // casts and such from the call, we don't really care. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7964 | ExprResult NewFn = ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7965 | if ((*R.begin())->isCXXClassMember()) |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7966 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, |
| 7967 | ExplicitTemplateArgs); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7968 | else if (ExplicitTemplateArgs) |
| 7969 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 7970 | else |
| 7971 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 7972 | |
| 7973 | if (NewFn.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7974 | return ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7975 | |
| 7976 | // This shouldn't cause an infinite loop because we're giving it |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 7977 | // an expression with viable lookup results, which should never |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7978 | // end up here. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7979 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, NewFn.take(), LParenLoc, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 7980 | MultiExprArg(Args, NumArgs), RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7981 | } |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 7982 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7983 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7984 | /// (which eventually refers to the declaration Func) and the call |
| 7985 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 7986 | /// to a specific function. If overload resolution succeeds, returns |
| 7987 | /// the function declaration produced by overload |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 7988 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7989 | /// arguments and Fn, and returns NULL. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7990 | ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 7991 | Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, UnresolvedLookupExpr *ULE, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7992 | SourceLocation LParenLoc, |
| 7993 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 7994 | SourceLocation RParenLoc, |
| 7995 | Expr *ExecConfig) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7996 | #ifndef NDEBUG |
| 7997 | if (ULE->requiresADL()) { |
| 7998 | // To do ADL, we must have found an unqualified name. |
| 7999 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 8000 | |
| 8001 | // We don't perform ADL for implicit declarations of builtins. |
| 8002 | // Verify that this was correctly set up. |
| 8003 | FunctionDecl *F; |
| 8004 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 8005 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 8006 | F->getBuiltinID() && F->isImplicit()) |
| 8007 | assert(0 && "performing ADL for builtin"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8008 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8009 | // We don't perform ADL in C. |
| 8010 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 8011 | } else |
| 8012 | assert(!ULE->isStdAssociatedNamespace() && |
| 8013 | "std is associated namespace but not doing ADL"); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8014 | #endif |
| 8015 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8016 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 8017 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8018 | // Add the functions denoted by the callee to the set of candidate |
| 8019 | // functions, including those from argument-dependent lookup. |
| 8020 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 8021 | |
| 8022 | // If we found nothing, try to recover. |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8023 | // BuildRecoveryCallExpr diagnoses the error itself, so we just bail |
| 8024 | // out if it fails. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8025 | if (CandidateSet.empty()) |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 8026 | return BuildRecoveryCallExpr(*this, S, Fn, ULE, LParenLoc, Args, NumArgs, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8027 | RParenLoc, /*EmptyLookup=*/true); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 8028 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8029 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8030 | switch (CandidateSet.BestViableFunction(*this, Fn->getLocStart(), Best)) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8031 | case OR_Success: { |
| 8032 | FunctionDecl *FDecl = Best->Function; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8033 | MarkDeclarationReferenced(Fn->getExprLoc(), FDecl); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8034 | CheckUnresolvedLookupAccess(ULE, Best->FoundDecl); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8035 | DiagnoseUseOfDecl(FDecl? FDecl : Best->FoundDecl.getDecl(), |
| 8036 | ULE->getNameLoc()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8037 | Fn = FixOverloadedFunctionReference(Fn, Best->FoundDecl, FDecl); |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 8038 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 8039 | ExecConfig); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8040 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8041 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8042 | case OR_No_Viable_Function: { |
| 8043 | // Try to recover by looking for viable functions which the user might |
| 8044 | // have meant to call. |
| 8045 | ExprResult Recovery = BuildRecoveryCallExpr(*this, S, Fn, ULE, LParenLoc, |
| 8046 | Args, NumArgs, RParenLoc, |
| 8047 | /*EmptyLookup=*/false); |
| 8048 | if (!Recovery.isInvalid()) |
| 8049 | return Recovery; |
| 8050 | |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 8051 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8052 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8053 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8054 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8055 | break; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8056 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8057 | |
| 8058 | case OR_Ambiguous: |
| 8059 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8060 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8061 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8062 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8063 | |
| 8064 | case OR_Deleted: |
Fariborz Jahanian | 2b982b7 | 2011-02-25 18:38:59 +0000 | [diff] [blame] | 8065 | { |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 8066 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 8067 | << Best->Function->isDeleted() |
| 8068 | << ULE->getName() |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 8069 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 8070 | << Fn->getSourceRange(); |
Fariborz Jahanian | 2b982b7 | 2011-02-25 18:38:59 +0000 | [diff] [blame] | 8071 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
| 8072 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8073 | break; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8074 | } |
| 8075 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 8076 | // Overload resolution failed. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8077 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8078 | } |
| 8079 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8080 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 8081 | return Functions.size() > 1 || |
| 8082 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 8083 | } |
| 8084 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8085 | /// \brief Create a unary operation that may resolve to an overloaded |
| 8086 | /// operator. |
| 8087 | /// |
| 8088 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 8089 | /// |
| 8090 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 8091 | /// operator. |
| 8092 | /// |
| 8093 | /// \param Functions The set of non-member functions that will be |
| 8094 | /// considered by overload resolution. The caller needs to build this |
| 8095 | /// set based on the context using, e.g., |
| 8096 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 8097 | /// set should not contain any member functions; those will be added |
| 8098 | /// by CreateOverloadedUnaryOp(). |
| 8099 | /// |
| 8100 | /// \param input The input argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8101 | ExprResult |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8102 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 8103 | const UnresolvedSetImpl &Fns, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8104 | Expr *Input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8105 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8106 | |
| 8107 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 8108 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 8109 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8110 | // TODO: provide better source location info. |
| 8111 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8112 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8113 | if (Input->getObjectKind() == OK_ObjCProperty) { |
| 8114 | ExprResult Result = ConvertPropertyForRValue(Input); |
| 8115 | if (Result.isInvalid()) |
| 8116 | return ExprError(); |
| 8117 | Input = Result.take(); |
| 8118 | } |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8119 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8120 | Expr *Args[2] = { Input, 0 }; |
| 8121 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8122 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8123 | // For post-increment and post-decrement, add the implicit '0' as |
| 8124 | // the second argument, so that we know this is a post-increment or |
| 8125 | // post-decrement. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8126 | if (Opc == UO_PostInc || Opc == UO_PostDec) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8127 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 8128 | Args[1] = IntegerLiteral::Create(Context, Zero, Context.IntTy, |
| 8129 | SourceLocation()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8130 | NumArgs = 2; |
| 8131 | } |
| 8132 | |
| 8133 | if (Input->isTypeDependent()) { |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 8134 | if (Fns.empty()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8135 | return Owned(new (Context) UnaryOperator(Input, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8136 | Opc, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 8137 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8138 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 8139 | OpLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8140 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8141 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8142 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 8143 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 8144 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 8145 | /*ADL*/ true, IsOverloaded(Fns), |
| 8146 | Fns.begin(), Fns.end()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8147 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 8148 | &Args[0], NumArgs, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8149 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8150 | VK_RValue, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8151 | OpLoc)); |
| 8152 | } |
| 8153 | |
| 8154 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8155 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8156 | |
| 8157 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8158 | AddFunctionCandidates(Fns, &Args[0], NumArgs, CandidateSet, false); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8159 | |
| 8160 | // Add operator candidates that are member functions. |
| 8161 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 8162 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8163 | // Add candidates from ADL. |
| 8164 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Douglas Gregor | dc81c88 | 2010-02-05 05:15:43 +0000 | [diff] [blame] | 8165 | Args, NumArgs, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8166 | /*ExplicitTemplateArgs*/ 0, |
| 8167 | CandidateSet); |
| 8168 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8169 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 8170 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8171 | |
| 8172 | // Perform overload resolution. |
| 8173 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8174 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8175 | case OR_Success: { |
| 8176 | // We found a built-in operator or an overloaded operator. |
| 8177 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8178 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8179 | if (FnDecl) { |
| 8180 | // We matched an overloaded operator. Build a call to that |
| 8181 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8182 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8183 | MarkDeclarationReferenced(OpLoc, FnDecl); |
| 8184 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8185 | // Convert the arguments. |
| 8186 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8187 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 8188 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8189 | ExprResult InputRes = |
| 8190 | PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 8191 | Best->FoundDecl, Method); |
| 8192 | if (InputRes.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8193 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8194 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8195 | } else { |
| 8196 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8197 | ExprResult InputInit |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 8198 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 8199 | Context, |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 8200 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8201 | SourceLocation(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8202 | Input); |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 8203 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8204 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8205 | Input = InputInit.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8206 | } |
| 8207 | |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8208 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 8209 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8210 | // Determine the result type. |
| 8211 | QualType ResultTy = FnDecl->getResultType(); |
| 8212 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 8213 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8214 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8215 | // Build the actual expression node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8216 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl); |
| 8217 | if (FnExpr.isInvalid()) |
| 8218 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8219 | |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 8220 | Args[0] = Input; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8221 | CallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8222 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8223 | Args, NumArgs, ResultTy, VK, OpLoc); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8224 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8225 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 8226 | FnDecl)) |
| 8227 | return ExprError(); |
| 8228 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8229 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8230 | } else { |
| 8231 | // We matched a built-in operator. Convert the arguments, then |
| 8232 | // break out so that we will build the appropriate built-in |
| 8233 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8234 | ExprResult InputRes = |
| 8235 | PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 8236 | Best->Conversions[0], AA_Passing); |
| 8237 | if (InputRes.isInvalid()) |
| 8238 | return ExprError(); |
| 8239 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8240 | break; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8241 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8242 | } |
| 8243 | |
| 8244 | case OR_No_Viable_Function: |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8245 | // This is an erroneous use of an operator which can be overloaded by |
| 8246 | // a non-member function. Check for non-member operators which were |
| 8247 | // defined too late to be candidates. |
| 8248 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, Args, NumArgs)) |
| 8249 | // FIXME: Recover by calling the found function. |
| 8250 | return ExprError(); |
| 8251 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8252 | // No viable function; fall through to handling this as a |
| 8253 | // built-in operator, which will produce an error message for us. |
| 8254 | break; |
| 8255 | |
| 8256 | case OR_Ambiguous: |
| 8257 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 8258 | << UnaryOperator::getOpcodeStr(Opc) |
| 8259 | << Input->getType() |
| 8260 | << Input->getSourceRange(); |
| 8261 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, |
| 8262 | Args, NumArgs, |
| 8263 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
| 8264 | return ExprError(); |
| 8265 | |
| 8266 | case OR_Deleted: |
| 8267 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 8268 | << Best->Function->isDeleted() |
| 8269 | << UnaryOperator::getOpcodeStr(Opc) |
| 8270 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 8271 | << Input->getSourceRange(); |
| 8272 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
| 8273 | return ExprError(); |
| 8274 | } |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8275 | |
| 8276 | // Either we found no viable overloaded operator or we matched a |
| 8277 | // built-in operator. In either case, fall through to trying to |
| 8278 | // build a built-in operation. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8279 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8280 | } |
| 8281 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8282 | /// \brief Create a binary operation that may resolve to an overloaded |
| 8283 | /// operator. |
| 8284 | /// |
| 8285 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 8286 | /// |
| 8287 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 8288 | /// operator. |
| 8289 | /// |
| 8290 | /// \param Functions The set of non-member functions that will be |
| 8291 | /// considered by overload resolution. The caller needs to build this |
| 8292 | /// set based on the context using, e.g., |
| 8293 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 8294 | /// set should not contain any member functions; those will be added |
| 8295 | /// by CreateOverloadedBinOp(). |
| 8296 | /// |
| 8297 | /// \param LHS Left-hand argument. |
| 8298 | /// \param RHS Right-hand argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8299 | ExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8300 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8301 | unsigned OpcIn, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8302 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8303 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8304 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8305 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8306 | |
| 8307 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 8308 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 8309 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 8310 | |
| 8311 | // If either side is type-dependent, create an appropriate dependent |
| 8312 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8313 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8314 | if (Fns.empty()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8315 | // If there are no functions to store, just build a dependent |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8316 | // BinaryOperator or CompoundAssignment. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8317 | if (Opc <= BO_Assign || Opc > BO_OrAssign) |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8318 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8319 | Context.DependentTy, |
| 8320 | VK_RValue, OK_Ordinary, |
| 8321 | OpLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8322 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8323 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 8324 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8325 | VK_LValue, |
| 8326 | OK_Ordinary, |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8327 | Context.DependentTy, |
| 8328 | Context.DependentTy, |
| 8329 | OpLoc)); |
| 8330 | } |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8331 | |
| 8332 | // FIXME: save results of ADL from here? |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8333 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8334 | // TODO: provide better source location info in DNLoc component. |
| 8335 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8336 | UnresolvedLookupExpr *Fn |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 8337 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
| 8338 | NestedNameSpecifierLoc(), OpNameInfo, |
| 8339 | /*ADL*/ true, IsOverloaded(Fns), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 8340 | Fns.begin(), Fns.end()); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8341 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8342 | Args, 2, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8343 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8344 | VK_RValue, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8345 | OpLoc)); |
| 8346 | } |
| 8347 | |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8348 | // Always do property rvalue conversions on the RHS. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8349 | if (Args[1]->getObjectKind() == OK_ObjCProperty) { |
| 8350 | ExprResult Result = ConvertPropertyForRValue(Args[1]); |
| 8351 | if (Result.isInvalid()) |
| 8352 | return ExprError(); |
| 8353 | Args[1] = Result.take(); |
| 8354 | } |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8355 | |
| 8356 | // The LHS is more complicated. |
| 8357 | if (Args[0]->getObjectKind() == OK_ObjCProperty) { |
| 8358 | |
| 8359 | // There's a tension for assignment operators between primitive |
| 8360 | // property assignment and the overloaded operators. |
| 8361 | if (BinaryOperator::isAssignmentOp(Opc)) { |
| 8362 | const ObjCPropertyRefExpr *PRE = LHS->getObjCProperty(); |
| 8363 | |
| 8364 | // Is the property "logically" settable? |
| 8365 | bool Settable = (PRE->isExplicitProperty() || |
| 8366 | PRE->getImplicitPropertySetter()); |
| 8367 | |
| 8368 | // To avoid gratuitously inventing semantics, use the primitive |
| 8369 | // unless it isn't. Thoughts in case we ever really care: |
| 8370 | // - If the property isn't logically settable, we have to |
| 8371 | // load and hope. |
| 8372 | // - If the property is settable and this is simple assignment, |
| 8373 | // we really should use the primitive. |
| 8374 | // - If the property is settable, then we could try overloading |
| 8375 | // on a generic lvalue of the appropriate type; if it works |
| 8376 | // out to a builtin candidate, we would do that same operation |
| 8377 | // on the property, and otherwise just error. |
| 8378 | if (Settable) |
| 8379 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 8380 | } |
| 8381 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8382 | ExprResult Result = ConvertPropertyForRValue(Args[0]); |
| 8383 | if (Result.isInvalid()) |
| 8384 | return ExprError(); |
| 8385 | Args[0] = Result.take(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8386 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8387 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 8388 | // If this is the assignment operator, we only perform overload resolution |
| 8389 | // if the left-hand side is a class or enumeration type. This is actually |
| 8390 | // a hack. The standard requires that we do overload resolution between the |
| 8391 | // various built-in candidates, but as DR507 points out, this can lead to |
| 8392 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 8393 | // Note that we go the traditional code path for compound assignment forms. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8394 | if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8395 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8396 | |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8397 | // If this is the .* operator, which is not overloadable, just |
| 8398 | // create a built-in binary operator. |
| 8399 | if (Opc == BO_PtrMemD) |
| 8400 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 8401 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8402 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8403 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8404 | |
| 8405 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8406 | AddFunctionCandidates(Fns, Args, 2, CandidateSet, false); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8407 | |
| 8408 | // Add operator candidates that are member functions. |
| 8409 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 8410 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8411 | // Add candidates from ADL. |
| 8412 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
| 8413 | Args, 2, |
| 8414 | /*ExplicitTemplateArgs*/ 0, |
| 8415 | CandidateSet); |
| 8416 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8417 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 8418 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8419 | |
| 8420 | // Perform overload resolution. |
| 8421 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8422 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 8423 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8424 | // We found a built-in operator or an overloaded operator. |
| 8425 | FunctionDecl *FnDecl = Best->Function; |
| 8426 | |
| 8427 | if (FnDecl) { |
| 8428 | // We matched an overloaded operator. Build a call to that |
| 8429 | // operator. |
| 8430 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8431 | MarkDeclarationReferenced(OpLoc, FnDecl); |
| 8432 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8433 | // Convert the arguments. |
| 8434 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 8435 | // Best->Access is only meaningful for class members. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8436 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 8437 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8438 | ExprResult Arg1 = |
| 8439 | PerformCopyInitialization( |
| 8440 | InitializedEntity::InitializeParameter(Context, |
| 8441 | FnDecl->getParamDecl(0)), |
| 8442 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8443 | if (Arg1.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8444 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8445 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8446 | ExprResult Arg0 = |
| 8447 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 8448 | Best->FoundDecl, Method); |
| 8449 | if (Arg0.isInvalid()) |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8450 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8451 | Args[0] = Arg0.takeAs<Expr>(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8452 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8453 | } else { |
| 8454 | // Convert the arguments. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8455 | ExprResult Arg0 = PerformCopyInitialization( |
| 8456 | InitializedEntity::InitializeParameter(Context, |
| 8457 | FnDecl->getParamDecl(0)), |
| 8458 | SourceLocation(), Owned(Args[0])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8459 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8460 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8461 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8462 | ExprResult Arg1 = |
| 8463 | PerformCopyInitialization( |
| 8464 | InitializedEntity::InitializeParameter(Context, |
| 8465 | FnDecl->getParamDecl(1)), |
| 8466 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8467 | if (Arg1.isInvalid()) |
| 8468 | return ExprError(); |
| 8469 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 8470 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8471 | } |
| 8472 | |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8473 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 8474 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8475 | // Determine the result type. |
| 8476 | QualType ResultTy = FnDecl->getResultType(); |
| 8477 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 8478 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8479 | |
| 8480 | // Build the actual expression node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8481 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, OpLoc); |
| 8482 | if (FnExpr.isInvalid()) |
| 8483 | return ExprError(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8484 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8485 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8486 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8487 | Args, 2, ResultTy, VK, OpLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8488 | |
| 8489 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 8490 | FnDecl)) |
| 8491 | return ExprError(); |
| 8492 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8493 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8494 | } else { |
| 8495 | // We matched a built-in operator. Convert the arguments, then |
| 8496 | // break out so that we will build the appropriate built-in |
| 8497 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8498 | ExprResult ArgsRes0 = |
| 8499 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 8500 | Best->Conversions[0], AA_Passing); |
| 8501 | if (ArgsRes0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8502 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8503 | Args[0] = ArgsRes0.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8504 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8505 | ExprResult ArgsRes1 = |
| 8506 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 8507 | Best->Conversions[1], AA_Passing); |
| 8508 | if (ArgsRes1.isInvalid()) |
| 8509 | return ExprError(); |
| 8510 | Args[1] = ArgsRes1.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8511 | break; |
| 8512 | } |
| 8513 | } |
| 8514 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8515 | case OR_No_Viable_Function: { |
| 8516 | // C++ [over.match.oper]p9: |
| 8517 | // If the operator is the operator , [...] and there are no |
| 8518 | // viable functions, then the operator is assumed to be the |
| 8519 | // built-in operator and interpreted according to clause 5. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8520 | if (Opc == BO_Comma) |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8521 | break; |
| 8522 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8523 | // For class as left operand for assignment or compound assigment |
| 8524 | // operator do not fall through to handling in built-in, but report that |
| 8525 | // no overloaded assignment operator found |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8526 | ExprResult Result = ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8527 | if (Args[0]->getType()->isRecordType() && |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8528 | Opc >= BO_Assign && Opc <= BO_OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 8529 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 8530 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8531 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8532 | } else { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8533 | // This is an erroneous use of an operator which can be overloaded by |
| 8534 | // a non-member function. Check for non-member operators which were |
| 8535 | // defined too late to be candidates. |
| 8536 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, Args, 2)) |
| 8537 | // FIXME: Recover by calling the found function. |
| 8538 | return ExprError(); |
| 8539 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8540 | // No viable function; try to create a built-in operation, which will |
| 8541 | // produce an error. Then, show the non-viable candidates. |
| 8542 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 8543 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8544 | assert(Result.isInvalid() && |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8545 | "C++ binary operator overloading is missing candidates!"); |
| 8546 | if (Result.isInvalid()) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8547 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 8548 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8549 | return move(Result); |
| 8550 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8551 | |
| 8552 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 8553 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_binary) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8554 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 8555 | << Args[0]->getType() << Args[1]->getType() |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8556 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8557 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, 2, |
| 8558 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8559 | return ExprError(); |
| 8560 | |
| 8561 | case OR_Deleted: |
| 8562 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 8563 | << Best->Function->isDeleted() |
| 8564 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 8565 | << getDeletedOrUnavailableSuffix(Best->Function) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8566 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8567 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8568 | return ExprError(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8569 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8570 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8571 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8572 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8573 | } |
| 8574 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8575 | ExprResult |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8576 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 8577 | SourceLocation RLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8578 | Expr *Base, Expr *Idx) { |
| 8579 | Expr *Args[2] = { Base, Idx }; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8580 | DeclarationName OpName = |
| 8581 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 8582 | |
| 8583 | // If either side is type-dependent, create an appropriate dependent |
| 8584 | // expression. |
| 8585 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 8586 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8587 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8588 | // CHECKME: no 'operator' keyword? |
| 8589 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
| 8590 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8591 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 8592 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 8593 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 8594 | /*ADL*/ true, /*Overloaded*/ false, |
| 8595 | UnresolvedSetIterator(), |
| 8596 | UnresolvedSetIterator()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 8597 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8598 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8599 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 8600 | Args, 2, |
| 8601 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8602 | VK_RValue, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8603 | RLoc)); |
| 8604 | } |
| 8605 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8606 | if (Args[0]->getObjectKind() == OK_ObjCProperty) { |
| 8607 | ExprResult Result = ConvertPropertyForRValue(Args[0]); |
| 8608 | if (Result.isInvalid()) |
| 8609 | return ExprError(); |
| 8610 | Args[0] = Result.take(); |
| 8611 | } |
| 8612 | if (Args[1]->getObjectKind() == OK_ObjCProperty) { |
| 8613 | ExprResult Result = ConvertPropertyForRValue(Args[1]); |
| 8614 | if (Result.isInvalid()) |
| 8615 | return ExprError(); |
| 8616 | Args[1] = Result.take(); |
| 8617 | } |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8618 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8619 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8620 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8621 | |
| 8622 | // Subscript can only be overloaded as a member function. |
| 8623 | |
| 8624 | // Add operator candidates that are member functions. |
| 8625 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 8626 | |
| 8627 | // Add builtin operator candidates. |
| 8628 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 8629 | |
| 8630 | // Perform overload resolution. |
| 8631 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8632 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8633 | case OR_Success: { |
| 8634 | // We found a built-in operator or an overloaded operator. |
| 8635 | FunctionDecl *FnDecl = Best->Function; |
| 8636 | |
| 8637 | if (FnDecl) { |
| 8638 | // We matched an overloaded operator. Build a call to that |
| 8639 | // operator. |
| 8640 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8641 | MarkDeclarationReferenced(LLoc, FnDecl); |
| 8642 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8643 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8644 | DiagnoseUseOfDecl(Best->FoundDecl, LLoc); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8645 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8646 | // Convert the arguments. |
| 8647 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8648 | ExprResult Arg0 = |
| 8649 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 8650 | Best->FoundDecl, Method); |
| 8651 | if (Arg0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8652 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8653 | Args[0] = Arg0.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8654 | |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 8655 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8656 | ExprResult InputInit |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 8657 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 8658 | Context, |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 8659 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8660 | SourceLocation(), |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 8661 | Owned(Args[1])); |
| 8662 | if (InputInit.isInvalid()) |
| 8663 | return ExprError(); |
| 8664 | |
| 8665 | Args[1] = InputInit.takeAs<Expr>(); |
| 8666 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8667 | // Determine the result type |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8668 | QualType ResultTy = FnDecl->getResultType(); |
| 8669 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 8670 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8671 | |
| 8672 | // Build the actual expression node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8673 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, LLoc); |
| 8674 | if (FnExpr.isInvalid()) |
| 8675 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8676 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8677 | CXXOperatorCallExpr *TheCall = |
| 8678 | new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8679 | FnExpr.take(), Args, 2, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8680 | ResultTy, VK, RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8681 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8682 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8683 | FnDecl)) |
| 8684 | return ExprError(); |
| 8685 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8686 | return MaybeBindToTemporary(TheCall); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8687 | } else { |
| 8688 | // We matched a built-in operator. Convert the arguments, then |
| 8689 | // break out so that we will build the appropriate built-in |
| 8690 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8691 | ExprResult ArgsRes0 = |
| 8692 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 8693 | Best->Conversions[0], AA_Passing); |
| 8694 | if (ArgsRes0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8695 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8696 | Args[0] = ArgsRes0.take(); |
| 8697 | |
| 8698 | ExprResult ArgsRes1 = |
| 8699 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 8700 | Best->Conversions[1], AA_Passing); |
| 8701 | if (ArgsRes1.isInvalid()) |
| 8702 | return ExprError(); |
| 8703 | Args[1] = ArgsRes1.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8704 | |
| 8705 | break; |
| 8706 | } |
| 8707 | } |
| 8708 | |
| 8709 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 8710 | if (CandidateSet.empty()) |
| 8711 | Diag(LLoc, diag::err_ovl_no_oper) |
| 8712 | << Args[0]->getType() << /*subscript*/ 0 |
| 8713 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 8714 | else |
| 8715 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 8716 | << Args[0]->getType() |
| 8717 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8718 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 8719 | "[]", LLoc); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 8720 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8721 | } |
| 8722 | |
| 8723 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 8724 | Diag(LLoc, diag::err_ovl_ambiguous_oper_binary) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8725 | << "[]" |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 8726 | << Args[0]->getType() << Args[1]->getType() |
| 8727 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8728 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, 2, |
| 8729 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8730 | return ExprError(); |
| 8731 | |
| 8732 | case OR_Deleted: |
| 8733 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 8734 | << Best->Function->isDeleted() << "[]" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 8735 | << getDeletedOrUnavailableSuffix(Best->Function) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8736 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8737 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 8738 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8739 | return ExprError(); |
| 8740 | } |
| 8741 | |
| 8742 | // We matched a built-in operator; build it. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8743 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8744 | } |
| 8745 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8746 | /// BuildCallToMemberFunction - Build a call to a member |
| 8747 | /// function. MemExpr is the expression that refers to the member |
| 8748 | /// function (and includes the object parameter), Args/NumArgs are the |
| 8749 | /// arguments to the function call (not including the object |
| 8750 | /// parameter). The caller needs to validate that the member |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8751 | /// expression refers to a non-static member function or an overloaded |
| 8752 | /// member function. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8753 | ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8754 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 8755 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 8756 | unsigned NumArgs, SourceLocation RParenLoc) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8757 | assert(MemExprE->getType() == Context.BoundMemberTy || |
| 8758 | MemExprE->getType() == Context.OverloadTy); |
| 8759 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8760 | // Dig out the member expression. This holds both the object |
| 8761 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8762 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8763 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8764 | // Determine whether this is a call to a pointer-to-member function. |
| 8765 | if (BinaryOperator *op = dyn_cast<BinaryOperator>(NakedMemExpr)) { |
| 8766 | assert(op->getType() == Context.BoundMemberTy); |
| 8767 | assert(op->getOpcode() == BO_PtrMemD || op->getOpcode() == BO_PtrMemI); |
| 8768 | |
| 8769 | QualType fnType = |
| 8770 | op->getRHS()->getType()->castAs<MemberPointerType>()->getPointeeType(); |
| 8771 | |
| 8772 | const FunctionProtoType *proto = fnType->castAs<FunctionProtoType>(); |
| 8773 | QualType resultType = proto->getCallResultType(Context); |
| 8774 | ExprValueKind valueKind = Expr::getValueKindForType(proto->getResultType()); |
| 8775 | |
| 8776 | // Check that the object type isn't more qualified than the |
| 8777 | // member function we're calling. |
| 8778 | Qualifiers funcQuals = Qualifiers::fromCVRMask(proto->getTypeQuals()); |
| 8779 | |
| 8780 | QualType objectType = op->getLHS()->getType(); |
| 8781 | if (op->getOpcode() == BO_PtrMemI) |
| 8782 | objectType = objectType->castAs<PointerType>()->getPointeeType(); |
| 8783 | Qualifiers objectQuals = objectType.getQualifiers(); |
| 8784 | |
| 8785 | Qualifiers difference = objectQuals - funcQuals; |
| 8786 | difference.removeObjCGCAttr(); |
| 8787 | difference.removeAddressSpace(); |
| 8788 | if (difference) { |
| 8789 | std::string qualsString = difference.getAsString(); |
| 8790 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
| 8791 | << fnType.getUnqualifiedType() |
| 8792 | << qualsString |
| 8793 | << (qualsString.find(' ') == std::string::npos ? 1 : 2); |
| 8794 | } |
| 8795 | |
| 8796 | CXXMemberCallExpr *call |
| 8797 | = new (Context) CXXMemberCallExpr(Context, MemExprE, Args, NumArgs, |
| 8798 | resultType, valueKind, RParenLoc); |
| 8799 | |
| 8800 | if (CheckCallReturnType(proto->getResultType(), |
| 8801 | op->getRHS()->getSourceRange().getBegin(), |
| 8802 | call, 0)) |
| 8803 | return ExprError(); |
| 8804 | |
| 8805 | if (ConvertArgumentsForCall(call, op, 0, proto, Args, NumArgs, RParenLoc)) |
| 8806 | return ExprError(); |
| 8807 | |
| 8808 | return MaybeBindToTemporary(call); |
| 8809 | } |
| 8810 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8811 | MemberExpr *MemExpr; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8812 | CXXMethodDecl *Method = 0; |
John McCall | bb6fb46 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 8813 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 8814 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8815 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 8816 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8817 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8818 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 8819 | Qualifier = MemExpr->getQualifier(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8820 | } else { |
| 8821 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 8822 | Qualifier = UnresExpr->getQualifier(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8823 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 8824 | QualType ObjectType = UnresExpr->getBaseType(); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 8825 | Expr::Classification ObjectClassification |
| 8826 | = UnresExpr->isArrow()? Expr::Classification::makeSimpleLValue() |
| 8827 | : UnresExpr->getBase()->Classify(Context); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8828 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8829 | // Add overload candidates |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8830 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8831 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8832 | // FIXME: avoid copy. |
| 8833 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 8834 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 8835 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 8836 | TemplateArgs = &TemplateArgsBuffer; |
| 8837 | } |
| 8838 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8839 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 8840 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 8841 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 8842 | NamedDecl *Func = *I; |
| 8843 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 8844 | if (isa<UsingShadowDecl>(Func)) |
| 8845 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 8846 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 8847 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 8848 | // Microsoft supports direct constructor calls. |
| 8849 | if (getLangOptions().Microsoft && isa<CXXConstructorDecl>(Func)) { |
| 8850 | AddOverloadCandidate(cast<CXXConstructorDecl>(Func), I.getPair(), Args, NumArgs, |
| 8851 | CandidateSet); |
| 8852 | } else if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 8853 | // If explicit template arguments were provided, we can't call a |
| 8854 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8855 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 8856 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8857 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8858 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8859 | ObjectClassification, |
| 8860 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 8861 | /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8862 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8863 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8864 | I.getPair(), ActingDC, TemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8865 | ObjectType, ObjectClassification, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 8866 | Args, NumArgs, CandidateSet, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 8867 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8868 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 8869 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8870 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8871 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 8872 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8873 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8874 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getLocStart(), |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 8875 | Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8876 | case OR_Success: |
| 8877 | Method = cast<CXXMethodDecl>(Best->Function); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8878 | MarkDeclarationReferenced(UnresExpr->getMemberLoc(), Method); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8879 | FoundDecl = Best->FoundDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8880 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8881 | DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8882 | break; |
| 8883 | |
| 8884 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8885 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8886 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 8887 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8888 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8889 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8890 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8891 | |
| 8892 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8893 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 8894 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8895 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8896 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8897 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8898 | |
| 8899 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8900 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8901 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 8902 | << DeclName |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 8903 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 8904 | << MemExprE->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8905 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8906 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8907 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8908 | } |
| 8909 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8910 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8911 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8912 | // If overload resolution picked a static member, build a |
| 8913 | // non-member call based on that function. |
| 8914 | if (Method->isStatic()) { |
| 8915 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 8916 | Args, NumArgs, RParenLoc); |
| 8917 | } |
| 8918 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8919 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8920 | } |
| 8921 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8922 | QualType ResultType = Method->getResultType(); |
| 8923 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 8924 | ResultType = ResultType.getNonLValueExprType(Context); |
| 8925 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8926 | assert(Method && "Member call to something that isn't a method?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8927 | CXXMemberCallExpr *TheCall = |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8928 | new (Context) CXXMemberCallExpr(Context, MemExprE, Args, NumArgs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8929 | ResultType, VK, RParenLoc); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8930 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 8931 | // Check for a valid return type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8932 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8933 | TheCall, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8934 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8935 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8936 | // Convert the object argument (for a non-static member function call). |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8937 | // We only need to do this if there was actually an overload; otherwise |
| 8938 | // it was done at lookup. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8939 | if (!Method->isStatic()) { |
| 8940 | ExprResult ObjectArg = |
| 8941 | PerformObjectArgumentInitialization(MemExpr->getBase(), Qualifier, |
| 8942 | FoundDecl, Method); |
| 8943 | if (ObjectArg.isInvalid()) |
| 8944 | return ExprError(); |
| 8945 | MemExpr->setBase(ObjectArg.take()); |
| 8946 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8947 | |
| 8948 | // Convert the rest of the arguments |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8949 | const FunctionProtoType *Proto = |
| 8950 | Method->getType()->getAs<FunctionProtoType>(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8951 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8952 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8953 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8954 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8955 | if (CheckFunctionCall(Method, TheCall)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8956 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 8957 | |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 8958 | if ((isa<CXXConstructorDecl>(CurContext) || |
| 8959 | isa<CXXDestructorDecl>(CurContext)) && |
| 8960 | TheCall->getMethodDecl()->isPure()) { |
| 8961 | const CXXMethodDecl *MD = TheCall->getMethodDecl(); |
| 8962 | |
| 8963 | if (isa<CXXThisExpr>(MemExpr->getBase()->IgnoreParenCasts())) |
| 8964 | Diag(MemExpr->getLocStart(), |
| 8965 | diag::warn_call_to_pure_virtual_member_function_from_ctor_dtor) |
| 8966 | << MD->getDeclName() << isa<CXXDestructorDecl>(CurContext) |
| 8967 | << MD->getParent()->getDeclName(); |
| 8968 | |
| 8969 | Diag(MD->getLocStart(), diag::note_previous_decl) << MD->getDeclName(); |
| 8970 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8971 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8972 | } |
| 8973 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8974 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 8975 | /// type (C++ [over.call.object]), which can end up invoking an |
| 8976 | /// overloaded function call operator (@c operator()) or performing a |
| 8977 | /// user-defined conversion on the object argument. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8978 | ExprResult |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8979 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 8980 | SourceLocation LParenLoc, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8981 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8982 | SourceLocation RParenLoc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8983 | ExprResult Object = Owned(Obj); |
| 8984 | if (Object.get()->getObjectKind() == OK_ObjCProperty) { |
| 8985 | Object = ConvertPropertyForRValue(Object.take()); |
| 8986 | if (Object.isInvalid()) |
| 8987 | return ExprError(); |
| 8988 | } |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8989 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8990 | assert(Object.get()->getType()->isRecordType() && "Requires object type argument"); |
| 8991 | const RecordType *Record = Object.get()->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8992 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8993 | // C++ [over.call.object]p1: |
| 8994 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 8995 | // 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] | 8996 | // candidate functions includes at least the function call |
| 8997 | // operators of T. The function call operators of T are obtained by |
| 8998 | // ordinary lookup of the name operator() in the context of |
| 8999 | // (E).operator(). |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9000 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 9001 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 9002 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9003 | if (RequireCompleteType(LParenLoc, Object.get()->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 9004 | PDiag(diag::err_incomplete_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9005 | << Object.get()->getSourceRange())) |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 9006 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9007 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9008 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 9009 | LookupQualifiedName(R, Record->getDecl()); |
| 9010 | R.suppressDiagnostics(); |
| 9011 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 9012 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 9013 | Oper != OperEnd; ++Oper) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9014 | AddMethodCandidate(Oper.getPair(), Object.get()->getType(), |
| 9015 | Object.get()->Classify(Context), Args, NumArgs, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 9016 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 9017 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9018 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9019 | // C++ [over.call.object]p2: |
| 9020 | // In addition, for each conversion function declared in T of the |
| 9021 | // form |
| 9022 | // |
| 9023 | // operator conversion-type-id () cv-qualifier; |
| 9024 | // |
| 9025 | // where cv-qualifier is the same cv-qualification as, or a |
| 9026 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 9027 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 9028 | // R", or the type "reference to pointer to function of |
| 9029 | // (P1,...,Pn) returning R", or the type "reference to function |
| 9030 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9031 | // is also considered as a candidate function. Similarly, |
| 9032 | // surrogate call functions are added to the set of candidate |
| 9033 | // functions for each conversion function declared in an |
| 9034 | // accessible base class provided the function is not hidden |
| 9035 | // within T by another intervening declaration. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 9036 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 9037 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 9038 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9039 | E = Conversions->end(); I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 9040 | NamedDecl *D = *I; |
| 9041 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 9042 | if (isa<UsingShadowDecl>(D)) |
| 9043 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9044 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 9045 | // Skip over templated conversion functions; they aren't |
| 9046 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 9047 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 9048 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 9049 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 9050 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9051 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 9052 | // Strip the reference type (if any) and then the pointer type (if |
| 9053 | // any) to get down to what might be a function type. |
| 9054 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 9055 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 9056 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9057 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 9058 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9059 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9060 | Object.get(), Args, NumArgs, CandidateSet); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9061 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9062 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9063 | // Perform overload resolution. |
| 9064 | OverloadCandidateSet::iterator Best; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9065 | switch (CandidateSet.BestViableFunction(*this, Object.get()->getLocStart(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9066 | Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9067 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9068 | // Overload resolution succeeded; we'll build the appropriate call |
| 9069 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9070 | break; |
| 9071 | |
| 9072 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 9073 | if (CandidateSet.empty()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9074 | Diag(Object.get()->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 9075 | << Object.get()->getType() << /*call*/ 1 |
| 9076 | << Object.get()->getSourceRange(); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 9077 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9078 | Diag(Object.get()->getSourceRange().getBegin(), |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 9079 | diag::err_ovl_no_viable_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9080 | << Object.get()->getType() << Object.get()->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9081 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9082 | break; |
| 9083 | |
| 9084 | case OR_Ambiguous: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9085 | Diag(Object.get()->getSourceRange().getBegin(), |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9086 | diag::err_ovl_ambiguous_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9087 | << Object.get()->getType() << Object.get()->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9088 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9089 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9090 | |
| 9091 | case OR_Deleted: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9092 | Diag(Object.get()->getSourceRange().getBegin(), |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9093 | diag::err_ovl_deleted_object_call) |
| 9094 | << Best->Function->isDeleted() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9095 | << Object.get()->getType() |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 9096 | << getDeletedOrUnavailableSuffix(Best->Function) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9097 | << Object.get()->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9098 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9099 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9100 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9101 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 9102 | if (Best == CandidateSet.end()) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9103 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9104 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9105 | if (Best->Function == 0) { |
| 9106 | // Since there is no function declaration, this is one of the |
| 9107 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9108 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9109 | = cast<CXXConversionDecl>( |
| 9110 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 9111 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9112 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 9113 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 9114 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9115 | // We selected one of the surrogate functions that converts the |
| 9116 | // object parameter to a function pointer. Perform the conversion |
| 9117 | // on the object argument, then let ActOnCallExpr finish the job. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9118 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 9119 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 9120 | // and then call it. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9121 | ExprResult Call = BuildCXXMemberCallExpr(Object.get(), Best->FoundDecl, Conv); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 9122 | if (Call.isInvalid()) |
| 9123 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9124 | |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 9125 | return ActOnCallExpr(S, Call.get(), LParenLoc, MultiExprArg(Args, NumArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 9126 | RParenLoc); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9127 | } |
| 9128 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 9129 | MarkDeclarationReferenced(LParenLoc, Best->Function); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9130 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 9131 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 9132 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9133 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 9134 | // that calls this method, using Object for the implicit object |
| 9135 | // parameter and passing along the remaining arguments. |
| 9136 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 9137 | const FunctionProtoType *Proto = |
| 9138 | Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9139 | |
| 9140 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 9141 | unsigned NumArgsToCheck = NumArgs; |
| 9142 | |
| 9143 | // Build the full argument list for the method call (the |
| 9144 | // implicit object parameter is placed at the beginning of the |
| 9145 | // list). |
| 9146 | Expr **MethodArgs; |
| 9147 | if (NumArgs < NumArgsInProto) { |
| 9148 | NumArgsToCheck = NumArgsInProto; |
| 9149 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 9150 | } else { |
| 9151 | MethodArgs = new Expr*[NumArgs + 1]; |
| 9152 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9153 | MethodArgs[0] = Object.get(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9154 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 9155 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9156 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9157 | ExprResult NewFn = CreateFunctionRefExpr(*this, Method); |
| 9158 | if (NewFn.isInvalid()) |
| 9159 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9160 | |
| 9161 | // Once we've built TheCall, all of the expressions are properly |
| 9162 | // owned. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9163 | QualType ResultTy = Method->getResultType(); |
| 9164 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 9165 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 9166 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9167 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9168 | new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn.take(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9169 | MethodArgs, NumArgs + 1, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9170 | ResultTy, VK, RParenLoc); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9171 | delete [] MethodArgs; |
| 9172 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9173 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall, |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 9174 | Method)) |
| 9175 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9176 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 9177 | // We may have default arguments. If so, we need to allocate more |
| 9178 | // slots in the call for them. |
| 9179 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 9180 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 9181 | else if (NumArgs > NumArgsInProto) |
| 9182 | NumArgsToCheck = NumArgsInProto; |
| 9183 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 9184 | bool IsError = false; |
| 9185 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9186 | // Initialize the implicit object parameter. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9187 | ExprResult ObjRes = |
| 9188 | PerformObjectArgumentInitialization(Object.get(), /*Qualifier=*/0, |
| 9189 | Best->FoundDecl, Method); |
| 9190 | if (ObjRes.isInvalid()) |
| 9191 | IsError = true; |
| 9192 | else |
| 9193 | Object = move(ObjRes); |
| 9194 | TheCall->setArg(0, Object.take()); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 9195 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9196 | // Check the argument types. |
| 9197 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9198 | Expr *Arg; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 9199 | if (i < NumArgs) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9200 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9201 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 9202 | // Pass the argument. |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 9203 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9204 | ExprResult InputInit |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 9205 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 9206 | Context, |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 9207 | Method->getParamDecl(i)), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9208 | SourceLocation(), Arg); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9209 | |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 9210 | IsError |= InputInit.isInvalid(); |
| 9211 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 9212 | } else { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9213 | ExprResult DefArg |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 9214 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 9215 | if (DefArg.isInvalid()) { |
| 9216 | IsError = true; |
| 9217 | break; |
| 9218 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9219 | |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 9220 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 9221 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9222 | |
| 9223 | TheCall->setArg(i + 1, Arg); |
| 9224 | } |
| 9225 | |
| 9226 | // If this is a variadic call, handle args passed through "...". |
| 9227 | if (Proto->isVariadic()) { |
| 9228 | // Promote the arguments (C99 6.5.2.2p7). |
| 9229 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9230 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0); |
| 9231 | IsError |= Arg.isInvalid(); |
| 9232 | TheCall->setArg(i + 1, Arg.take()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9233 | } |
| 9234 | } |
| 9235 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 9236 | if (IsError) return true; |
| 9237 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9238 | if (CheckFunctionCall(Method, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 9239 | return true; |
| 9240 | |
John McCall | 182f709 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 9241 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9242 | } |
| 9243 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9244 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9245 | /// (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] | 9246 | /// @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] | 9247 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9248 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc) { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 9249 | assert(Base->getType()->isRecordType() && |
| 9250 | "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9251 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9252 | if (Base->getObjectKind() == OK_ObjCProperty) { |
| 9253 | ExprResult Result = ConvertPropertyForRValue(Base); |
| 9254 | if (Result.isInvalid()) |
| 9255 | return ExprError(); |
| 9256 | Base = Result.take(); |
| 9257 | } |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 9258 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9259 | SourceLocation Loc = Base->getExprLoc(); |
| 9260 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9261 | // C++ [over.ref]p1: |
| 9262 | // |
| 9263 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 9264 | // for a class object x of type T if T::operator->() exists and if |
| 9265 | // the operator is selected as the best match function by the |
| 9266 | // overload resolution mechanism (13.3). |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 9267 | DeclarationName OpName = |
| 9268 | Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9269 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 9270 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 9271 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9272 | if (RequireCompleteType(Loc, Base->getType(), |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 9273 | PDiag(diag::err_typecheck_incomplete_tag) |
| 9274 | << Base->getSourceRange())) |
| 9275 | return ExprError(); |
| 9276 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9277 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 9278 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 9279 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 9280 | |
| 9281 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 9282 | Oper != OperEnd; ++Oper) { |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 9283 | AddMethodCandidate(Oper.getPair(), Base->getType(), Base->Classify(Context), |
| 9284 | 0, 0, CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 9285 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9286 | |
| 9287 | // Perform overload resolution. |
| 9288 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9289 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9290 | case OR_Success: |
| 9291 | // Overload resolution succeeded; we'll build the call below. |
| 9292 | break; |
| 9293 | |
| 9294 | case OR_No_Viable_Function: |
| 9295 | if (CandidateSet.empty()) |
| 9296 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 9297 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9298 | else |
| 9299 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 9300 | << "operator->" << Base->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9301 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 9302 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9303 | |
| 9304 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 9305 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 9306 | << "->" << Base->getType() << Base->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9307 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 9308 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9309 | |
| 9310 | case OR_Deleted: |
| 9311 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 9312 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 9313 | << "->" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 9314 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 9315 | << Base->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9316 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 9317 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9318 | } |
| 9319 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 9320 | MarkDeclarationReferenced(OpLoc, Best->Function); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9321 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 9322 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9323 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9324 | // Convert the object parameter. |
| 9325 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9326 | ExprResult BaseResult = |
| 9327 | PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 9328 | Best->FoundDecl, Method); |
| 9329 | if (BaseResult.isInvalid()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 9330 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9331 | Base = BaseResult.take(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 9332 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9333 | // Build the operator call. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9334 | ExprResult FnExpr = CreateFunctionRefExpr(*this, Method); |
| 9335 | if (FnExpr.isInvalid()) |
| 9336 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9337 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9338 | QualType ResultTy = Method->getResultType(); |
| 9339 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 9340 | ResultTy = ResultTy.getNonLValueExprType(Context); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9341 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9342 | new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr.take(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9343 | &Base, 1, ResultTy, VK, OpLoc); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 9344 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9345 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 9346 | Method)) |
| 9347 | return ExprError(); |
Eli Friedman | d593190 | 2011-04-04 01:18:25 +0000 | [diff] [blame] | 9348 | |
| 9349 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9350 | } |
| 9351 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9352 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 9353 | /// a C++ overloaded function (possibly with some parentheses and |
| 9354 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 9355 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 9356 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 9357 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9358 | FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9359 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9360 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 9361 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9362 | if (SubExpr == PE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9363 | return PE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9364 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9365 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9366 | } |
| 9367 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9368 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9369 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 9370 | Found, Fn); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9371 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9372 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 9373 | "Implicit cast type cannot be determined from overload"); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 9374 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9375 | if (SubExpr == ICE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9376 | return ICE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9377 | |
| 9378 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 9379 | ICE->getCastKind(), |
| 9380 | SubExpr, 0, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 9381 | ICE->getValueKind()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9382 | } |
| 9383 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9384 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9385 | assert(UnOp->getOpcode() == UO_AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9386 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 9387 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 9388 | if (Method->isStatic()) { |
| 9389 | // Do nothing: static member functions aren't any different |
| 9390 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9391 | } else { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 9392 | // Fix the sub expression, which really has to be an |
| 9393 | // UnresolvedLookupExpr holding an overloaded member function |
| 9394 | // or template. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9395 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 9396 | Found, Fn); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9397 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9398 | return UnOp; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9399 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9400 | assert(isa<DeclRefExpr>(SubExpr) |
| 9401 | && "fixed to something other than a decl ref"); |
| 9402 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 9403 | && "fixed to a member ref with no nested name qualifier"); |
| 9404 | |
| 9405 | // We have taken the address of a pointer to member |
| 9406 | // function. Perform the computation here so that we get the |
| 9407 | // appropriate pointer to member type. |
| 9408 | QualType ClassType |
| 9409 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 9410 | QualType MemPtrType |
| 9411 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 9412 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9413 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, MemPtrType, |
| 9414 | VK_RValue, OK_Ordinary, |
| 9415 | UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 9416 | } |
| 9417 | } |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9418 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 9419 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9420 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9421 | return UnOp; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9422 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9423 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9424 | Context.getPointerType(SubExpr->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9425 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9426 | UnOp->getOperatorLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9427 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9428 | |
| 9429 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9430 | // FIXME: avoid copy. |
| 9431 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 9432 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9433 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 9434 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 9435 | } |
| 9436 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9437 | return DeclRefExpr::Create(Context, |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 9438 | ULE->getQualifierLoc(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9439 | Fn, |
| 9440 | ULE->getNameLoc(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9441 | Fn->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9442 | VK_LValue, |
Chandler Carruth | 3aa8140 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 9443 | Found.getDecl(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9444 | TemplateArgs); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9445 | } |
| 9446 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9447 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 9448 | // FIXME: avoid copy. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9449 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 9450 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 9451 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 9452 | TemplateArgs = &TemplateArgsBuffer; |
| 9453 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 9454 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9455 | Expr *Base; |
| 9456 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9457 | // If we're filling in a static method where we used to have an |
| 9458 | // implicit member access, rewrite to a simple decl ref. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9459 | if (MemExpr->isImplicitAccess()) { |
| 9460 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 9461 | return DeclRefExpr::Create(Context, |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 9462 | MemExpr->getQualifierLoc(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9463 | Fn, |
| 9464 | MemExpr->getMemberLoc(), |
| 9465 | Fn->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9466 | VK_LValue, |
Chandler Carruth | 3aa8140 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 9467 | Found.getDecl(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9468 | TemplateArgs); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 9469 | } else { |
| 9470 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 9471 | if (MemExpr->getQualifier()) |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 9472 | Loc = MemExpr->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 9473 | Base = new (Context) CXXThisExpr(Loc, |
| 9474 | MemExpr->getBaseType(), |
| 9475 | /*isImplicit=*/true); |
| 9476 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9477 | } else |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9478 | Base = MemExpr->getBase(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9479 | |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9480 | ExprValueKind valueKind; |
| 9481 | QualType type; |
| 9482 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 9483 | valueKind = VK_LValue; |
| 9484 | type = Fn->getType(); |
| 9485 | } else { |
| 9486 | valueKind = VK_RValue; |
| 9487 | type = Context.BoundMemberTy; |
| 9488 | } |
| 9489 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9490 | return MemberExpr::Create(Context, Base, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9491 | MemExpr->isArrow(), |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 9492 | MemExpr->getQualifierLoc(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9493 | Fn, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9494 | Found, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9495 | MemExpr->getMemberNameInfo(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9496 | TemplateArgs, |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9497 | type, valueKind, OK_Ordinary); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9498 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9499 | |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9500 | llvm_unreachable("Invalid reference to overloaded function"); |
| 9501 | return E; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9502 | } |
| 9503 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9504 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9505 | DeclAccessPair Found, |
| 9506 | FunctionDecl *Fn) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9507 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 9508 | } |
| 9509 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9510 | } // end namespace clang |