Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1 | //===--- SemaOverload.cpp - C++ Overloading ---------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides Sema routines for C++ overloading. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Lookup.h" |
| 16 | #include "clang/Sema/Initialization.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Template.h" |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 18 | #include "clang/Sema/TemplateDeduction.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 22 | #include "clang/AST/CXXInheritance.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 24 | #include "clang/AST/Expr.h" |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeOrdering.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | 661b493 | 2010-09-12 04:28:07 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseSet.h" |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 32 | #include <algorithm> |
| 33 | |
| 34 | namespace clang { |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 35 | using namespace sema; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 36 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 37 | /// A convenience routine for creating a decayed reference to a |
| 38 | /// function. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 39 | static ExprResult |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 40 | CreateFunctionRefExpr(Sema &S, FunctionDecl *Fn, bool HadMultipleCandidates, |
Douglas Gregor | 5b8968c | 2011-07-15 16:25:15 +0000 | [diff] [blame] | 41 | SourceLocation Loc = SourceLocation(), |
| 42 | const DeclarationNameLoc &LocInfo = DeclarationNameLoc()){ |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 43 | DeclRefExpr *DRE = new (S.Context) DeclRefExpr(Fn, Fn->getType(), |
| 44 | VK_LValue, Loc, LocInfo); |
| 45 | if (HadMultipleCandidates) |
| 46 | DRE->setHadMultipleCandidates(true); |
| 47 | ExprResult E = S.Owned(DRE); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 48 | E = S.DefaultFunctionArrayConversion(E.take()); |
| 49 | if (E.isInvalid()) |
| 50 | return ExprError(); |
| 51 | return move(E); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 52 | } |
| 53 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 54 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 55 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 56 | StandardConversionSequence &SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 57 | bool CStyle, |
| 58 | bool AllowObjCWritebackConversion); |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 59 | |
| 60 | static bool IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 61 | QualType &ToType, |
| 62 | bool InOverloadResolution, |
| 63 | StandardConversionSequence &SCS, |
| 64 | bool CStyle); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 65 | static OverloadingResult |
| 66 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 67 | UserDefinedConversionSequence& User, |
| 68 | OverloadCandidateSet& Conversions, |
| 69 | bool AllowExplicit); |
| 70 | |
| 71 | |
| 72 | static ImplicitConversionSequence::CompareKind |
| 73 | CompareStandardConversionSequences(Sema &S, |
| 74 | const StandardConversionSequence& SCS1, |
| 75 | const StandardConversionSequence& SCS2); |
| 76 | |
| 77 | static ImplicitConversionSequence::CompareKind |
| 78 | CompareQualificationConversions(Sema &S, |
| 79 | const StandardConversionSequence& SCS1, |
| 80 | const StandardConversionSequence& SCS2); |
| 81 | |
| 82 | static ImplicitConversionSequence::CompareKind |
| 83 | CompareDerivedToBaseConversions(Sema &S, |
| 84 | const StandardConversionSequence& SCS1, |
| 85 | const StandardConversionSequence& SCS2); |
| 86 | |
| 87 | |
| 88 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 89 | /// GetConversionCategory - Retrieve the implicit conversion |
| 90 | /// category corresponding to the given implicit conversion kind. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | ImplicitConversionCategory |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 92 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 93 | static const ImplicitConversionCategory |
| 94 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 95 | ICC_Identity, |
| 96 | ICC_Lvalue_Transformation, |
| 97 | ICC_Lvalue_Transformation, |
| 98 | ICC_Lvalue_Transformation, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 99 | ICC_Identity, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 100 | ICC_Qualification_Adjustment, |
| 101 | ICC_Promotion, |
| 102 | ICC_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 103 | ICC_Promotion, |
| 104 | ICC_Conversion, |
| 105 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 106 | ICC_Conversion, |
| 107 | ICC_Conversion, |
| 108 | ICC_Conversion, |
| 109 | ICC_Conversion, |
| 110 | ICC_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 111 | ICC_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 112 | ICC_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 113 | ICC_Conversion, |
| 114 | ICC_Conversion, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 115 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 116 | ICC_Conversion |
| 117 | }; |
| 118 | return Category[(int)Kind]; |
| 119 | } |
| 120 | |
| 121 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 122 | /// corresponding to the given implicit conversion kind. |
| 123 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 124 | static const ImplicitConversionRank |
| 125 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 126 | ICR_Exact_Match, |
| 127 | ICR_Exact_Match, |
| 128 | ICR_Exact_Match, |
| 129 | ICR_Exact_Match, |
| 130 | ICR_Exact_Match, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 131 | ICR_Exact_Match, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 132 | ICR_Promotion, |
| 133 | ICR_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 134 | ICR_Promotion, |
| 135 | ICR_Conversion, |
| 136 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 137 | ICR_Conversion, |
| 138 | ICR_Conversion, |
| 139 | ICR_Conversion, |
| 140 | ICR_Conversion, |
| 141 | ICR_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 142 | ICR_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 143 | ICR_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 144 | ICR_Conversion, |
| 145 | ICR_Conversion, |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 146 | ICR_Complex_Real_Conversion, |
| 147 | ICR_Conversion, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 148 | ICR_Conversion, |
| 149 | ICR_Writeback_Conversion |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 150 | }; |
| 151 | return Rank[(int)Kind]; |
| 152 | } |
| 153 | |
| 154 | /// GetImplicitConversionName - Return the name of this kind of |
| 155 | /// implicit conversion. |
| 156 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | 2550d70 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 157 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 158 | "No conversion", |
| 159 | "Lvalue-to-rvalue", |
| 160 | "Array-to-pointer", |
| 161 | "Function-to-pointer", |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 162 | "Noreturn adjustment", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 163 | "Qualification", |
| 164 | "Integral promotion", |
| 165 | "Floating point promotion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 166 | "Complex promotion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 167 | "Integral conversion", |
| 168 | "Floating conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 169 | "Complex conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 170 | "Floating-integral conversion", |
| 171 | "Pointer conversion", |
| 172 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 173 | "Boolean conversion", |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 174 | "Compatible-types conversion", |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 175 | "Derived-to-base conversion", |
| 176 | "Vector conversion", |
| 177 | "Vector splat", |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 178 | "Complex-real conversion", |
| 179 | "Block Pointer conversion", |
| 180 | "Transparent Union Conversion" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 181 | "Writeback conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 182 | }; |
| 183 | return Name[Kind]; |
| 184 | } |
| 185 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 186 | /// StandardConversionSequence - Set the standard conversion |
| 187 | /// sequence to the identity conversion. |
| 188 | void StandardConversionSequence::setAsIdentityConversion() { |
| 189 | First = ICK_Identity; |
| 190 | Second = ICK_Identity; |
| 191 | Third = ICK_Identity; |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 192 | DeprecatedStringLiteralToCharPtr = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 193 | QualificationIncludesObjCLifetime = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 194 | ReferenceBinding = false; |
| 195 | DirectBinding = false; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 196 | IsLvalueReference = true; |
| 197 | BindsToFunctionLvalue = false; |
| 198 | BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 199 | BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 200 | ObjCLifetimeConversionBinding = false; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 201 | CopyConstructor = 0; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 204 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 205 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 206 | /// implicit conversions. |
| 207 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 208 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 209 | if (GetConversionRank(First) > Rank) |
| 210 | Rank = GetConversionRank(First); |
| 211 | if (GetConversionRank(Second) > Rank) |
| 212 | Rank = GetConversionRank(Second); |
| 213 | if (GetConversionRank(Third) > Rank) |
| 214 | Rank = GetConversionRank(Third); |
| 215 | return Rank; |
| 216 | } |
| 217 | |
| 218 | /// isPointerConversionToBool - Determines whether this conversion is |
| 219 | /// a conversion of a pointer or pointer-to-member to bool. This is |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 221 | /// (C++ 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 222 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 223 | // Note that FromType has not necessarily been transformed by the |
| 224 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 225 | // check for their presence as well as checking whether FromType is |
| 226 | // a pointer. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 227 | if (getToType(1)->isBooleanType() && |
John McCall | ddb0ce7 | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 228 | (getFromType()->isPointerType() || |
| 229 | getFromType()->isObjCObjectPointerType() || |
| 230 | getFromType()->isBlockPointerType() || |
Anders Carlsson | c8df0b6 | 2010-11-05 00:12:09 +0000 | [diff] [blame] | 231 | getFromType()->isNullPtrType() || |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 232 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 233 | return true; |
| 234 | |
| 235 | return false; |
| 236 | } |
| 237 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 238 | /// isPointerConversionToVoidPointer - Determines whether this |
| 239 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 240 | /// used as part of the ranking of standard conversion sequences (C++ |
| 241 | /// 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | bool |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 243 | StandardConversionSequence:: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 245 | QualType FromType = getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 246 | QualType ToType = getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 247 | |
| 248 | // Note that FromType has not necessarily been transformed by the |
| 249 | // array-to-pointer implicit conversion, so check for its presence |
| 250 | // and redo the conversion to get a pointer. |
| 251 | if (First == ICK_Array_To_Pointer) |
| 252 | FromType = Context.getArrayDecayedType(FromType); |
| 253 | |
Douglas Gregor | f9af524 | 2011-04-15 20:45:44 +0000 | [diff] [blame] | 254 | if (Second == ICK_Pointer_Conversion && FromType->isAnyPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 255 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 256 | return ToPtrType->getPointeeType()->isVoidType(); |
| 257 | |
| 258 | return false; |
| 259 | } |
| 260 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 261 | /// DebugPrint - Print this standard conversion sequence to standard |
| 262 | /// error. Useful for debugging overloading issues. |
| 263 | void StandardConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 264 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 265 | bool PrintedSomething = false; |
| 266 | if (First != ICK_Identity) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 267 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 268 | PrintedSomething = true; |
| 269 | } |
| 270 | |
| 271 | if (Second != ICK_Identity) { |
| 272 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 273 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 274 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 275 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 276 | |
| 277 | if (CopyConstructor) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 278 | OS << " (by copy constructor)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 279 | } else if (DirectBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 280 | OS << " (direct reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 281 | } else if (ReferenceBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 282 | OS << " (reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 283 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 284 | PrintedSomething = true; |
| 285 | } |
| 286 | |
| 287 | if (Third != ICK_Identity) { |
| 288 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 289 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 290 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 291 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 292 | PrintedSomething = true; |
| 293 | } |
| 294 | |
| 295 | if (!PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 296 | OS << "No conversions required"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 301 | /// error. Useful for debugging overloading issues. |
| 302 | void UserDefinedConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 303 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 304 | if (Before.First || Before.Second || Before.Third) { |
| 305 | Before.DebugPrint(); |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 306 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 307 | } |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 308 | if (ConversionFunction) |
| 309 | OS << '\'' << *ConversionFunction << '\''; |
| 310 | else |
| 311 | OS << "aggregate initialization"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 312 | if (After.First || After.Second || After.Third) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 313 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 314 | After.DebugPrint(); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 319 | /// error. Useful for debugging overloading issues. |
| 320 | void ImplicitConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 321 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 322 | switch (ConversionKind) { |
| 323 | case StandardConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 324 | OS << "Standard conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 325 | Standard.DebugPrint(); |
| 326 | break; |
| 327 | case UserDefinedConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 328 | OS << "User-defined conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 329 | UserDefined.DebugPrint(); |
| 330 | break; |
| 331 | case EllipsisConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 332 | OS << "Ellipsis conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 333 | break; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 334 | case AmbiguousConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 335 | OS << "Ambiguous conversion"; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 336 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 337 | case BadConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 338 | OS << "Bad conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 339 | break; |
| 340 | } |
| 341 | |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 342 | OS << "\n"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 343 | } |
| 344 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 345 | void AmbiguousConversionSequence::construct() { |
| 346 | new (&conversions()) ConversionSet(); |
| 347 | } |
| 348 | |
| 349 | void AmbiguousConversionSequence::destruct() { |
| 350 | conversions().~ConversionSet(); |
| 351 | } |
| 352 | |
| 353 | void |
| 354 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 355 | FromTypePtr = O.FromTypePtr; |
| 356 | ToTypePtr = O.ToTypePtr; |
| 357 | new (&conversions()) ConversionSet(O.conversions()); |
| 358 | } |
| 359 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 360 | namespace { |
| 361 | // Structure used by OverloadCandidate::DeductionFailureInfo to store |
| 362 | // template parameter and template argument information. |
| 363 | struct DFIParamWithArguments { |
| 364 | TemplateParameter Param; |
| 365 | TemplateArgument FirstArg; |
| 366 | TemplateArgument SecondArg; |
| 367 | }; |
| 368 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 369 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 370 | /// \brief Convert from Sema's representation of template deduction information |
| 371 | /// to the form used in overload-candidate information. |
| 372 | OverloadCandidate::DeductionFailureInfo |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 373 | static MakeDeductionFailureInfo(ASTContext &Context, |
| 374 | Sema::TemplateDeductionResult TDK, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 375 | TemplateDeductionInfo &Info) { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 376 | OverloadCandidate::DeductionFailureInfo Result; |
| 377 | Result.Result = static_cast<unsigned>(TDK); |
| 378 | Result.Data = 0; |
| 379 | switch (TDK) { |
| 380 | case Sema::TDK_Success: |
| 381 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 382 | case Sema::TDK_TooManyArguments: |
| 383 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 384 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 385 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 386 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 387 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 388 | Result.Data = Info.Param.getOpaqueValue(); |
| 389 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 390 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 391 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 392 | case Sema::TDK_Underqualified: { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 393 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 394 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 395 | Saved->Param = Info.Param; |
| 396 | Saved->FirstArg = Info.FirstArg; |
| 397 | Saved->SecondArg = Info.SecondArg; |
| 398 | Result.Data = Saved; |
| 399 | break; |
| 400 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 401 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 402 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 403 | Result.Data = Info.take(); |
| 404 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 405 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 406 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 407 | case Sema::TDK_FailedOverloadResolution: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 408 | break; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 409 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 410 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 411 | return Result; |
| 412 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 413 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 414 | void OverloadCandidate::DeductionFailureInfo::Destroy() { |
| 415 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 416 | case Sema::TDK_Success: |
| 417 | case Sema::TDK_InstantiationDepth: |
| 418 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 419 | case Sema::TDK_TooManyArguments: |
| 420 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 421 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 422 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 423 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 424 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 425 | case Sema::TDK_Underqualified: |
Douglas Gregor | aaa045d | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 426 | // FIXME: Destroy the data? |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 427 | Data = 0; |
| 428 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 429 | |
| 430 | case Sema::TDK_SubstitutionFailure: |
| 431 | // FIXME: Destroy the template arugment list? |
| 432 | Data = 0; |
| 433 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 434 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 435 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 436 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 437 | case Sema::TDK_FailedOverloadResolution: |
| 438 | break; |
| 439 | } |
| 440 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 441 | |
| 442 | TemplateParameter |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 443 | OverloadCandidate::DeductionFailureInfo::getTemplateParameter() { |
| 444 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 445 | case Sema::TDK_Success: |
| 446 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 447 | case Sema::TDK_TooManyArguments: |
| 448 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 449 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 450 | return TemplateParameter(); |
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 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 453 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 454 | return TemplateParameter::getFromOpaqueValue(Data); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 455 | |
| 456 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 457 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 458 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 459 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 460 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 461 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 462 | case Sema::TDK_FailedOverloadResolution: |
| 463 | break; |
| 464 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 465 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 466 | return TemplateParameter(); |
| 467 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 468 | |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 469 | TemplateArgumentList * |
| 470 | OverloadCandidate::DeductionFailureInfo::getTemplateArgumentList() { |
| 471 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 472 | case Sema::TDK_Success: |
| 473 | case Sema::TDK_InstantiationDepth: |
| 474 | case Sema::TDK_TooManyArguments: |
| 475 | case Sema::TDK_TooFewArguments: |
| 476 | case Sema::TDK_Incomplete: |
| 477 | case Sema::TDK_InvalidExplicitArguments: |
| 478 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 479 | case Sema::TDK_Underqualified: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 480 | return 0; |
| 481 | |
| 482 | case Sema::TDK_SubstitutionFailure: |
| 483 | return static_cast<TemplateArgumentList*>(Data); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 484 | |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 485 | // Unhandled |
| 486 | case Sema::TDK_NonDeducedMismatch: |
| 487 | case Sema::TDK_FailedOverloadResolution: |
| 488 | break; |
| 489 | } |
| 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 494 | const TemplateArgument *OverloadCandidate::DeductionFailureInfo::getFirstArg() { |
| 495 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 496 | case Sema::TDK_Success: |
| 497 | case Sema::TDK_InstantiationDepth: |
| 498 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 499 | case Sema::TDK_TooManyArguments: |
| 500 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 501 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 502 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 503 | return 0; |
| 504 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 505 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 506 | case Sema::TDK_Underqualified: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 507 | return &static_cast<DFIParamWithArguments*>(Data)->FirstArg; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 508 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 509 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 510 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 511 | case Sema::TDK_FailedOverloadResolution: |
| 512 | break; |
| 513 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 514 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 515 | return 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 516 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 517 | |
| 518 | const TemplateArgument * |
| 519 | OverloadCandidate::DeductionFailureInfo::getSecondArg() { |
| 520 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 521 | case Sema::TDK_Success: |
| 522 | case Sema::TDK_InstantiationDepth: |
| 523 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 524 | case Sema::TDK_TooManyArguments: |
| 525 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 526 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 527 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 528 | return 0; |
| 529 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 530 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 531 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 532 | return &static_cast<DFIParamWithArguments*>(Data)->SecondArg; |
| 533 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 534 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 535 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 536 | case Sema::TDK_FailedOverloadResolution: |
| 537 | break; |
| 538 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 539 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | void OverloadCandidateSet::clear() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 544 | inherited::clear(); |
| 545 | Functions.clear(); |
| 546 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 547 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 548 | namespace { |
| 549 | class UnbridgedCastsSet { |
| 550 | struct Entry { |
| 551 | Expr **Addr; |
| 552 | Expr *Saved; |
| 553 | }; |
| 554 | SmallVector<Entry, 2> Entries; |
| 555 | |
| 556 | public: |
| 557 | void save(Sema &S, Expr *&E) { |
| 558 | assert(E->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 559 | Entry entry = { &E, E }; |
| 560 | Entries.push_back(entry); |
| 561 | E = S.stripARCUnbridgedCast(E); |
| 562 | } |
| 563 | |
| 564 | void restore() { |
| 565 | for (SmallVectorImpl<Entry>::iterator |
| 566 | i = Entries.begin(), e = Entries.end(); i != e; ++i) |
| 567 | *i->Addr = i->Saved; |
| 568 | } |
| 569 | }; |
| 570 | } |
| 571 | |
| 572 | /// checkPlaceholderForOverload - Do any interesting placeholder-like |
| 573 | /// preprocessing on the given expression. |
| 574 | /// |
| 575 | /// \param unbridgedCasts a collection to which to add unbridged casts; |
| 576 | /// without this, they will be immediately diagnosed as errors |
| 577 | /// |
| 578 | /// Return true on unrecoverable error. |
| 579 | static bool checkPlaceholderForOverload(Sema &S, Expr *&E, |
| 580 | UnbridgedCastsSet *unbridgedCasts = 0) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 581 | if (const BuiltinType *placeholder = E->getType()->getAsPlaceholderType()) { |
| 582 | // We can't handle overloaded expressions here because overload |
| 583 | // resolution might reasonably tweak them. |
| 584 | if (placeholder->getKind() == BuiltinType::Overload) return false; |
| 585 | |
| 586 | // If the context potentially accepts unbridged ARC casts, strip |
| 587 | // the unbridged cast and add it to the collection for later restoration. |
| 588 | if (placeholder->getKind() == BuiltinType::ARCUnbridgedCast && |
| 589 | unbridgedCasts) { |
| 590 | unbridgedCasts->save(S, E); |
| 591 | return false; |
| 592 | } |
| 593 | |
| 594 | // Go ahead and check everything else. |
| 595 | ExprResult result = S.CheckPlaceholderExpr(E); |
| 596 | if (result.isInvalid()) |
| 597 | return true; |
| 598 | |
| 599 | E = result.take(); |
| 600 | return false; |
| 601 | } |
| 602 | |
| 603 | // Nothing to do. |
| 604 | return false; |
| 605 | } |
| 606 | |
| 607 | /// checkArgPlaceholdersForOverload - Check a set of call operands for |
| 608 | /// placeholders. |
| 609 | static bool checkArgPlaceholdersForOverload(Sema &S, Expr **args, |
| 610 | unsigned numArgs, |
| 611 | UnbridgedCastsSet &unbridged) { |
| 612 | for (unsigned i = 0; i != numArgs; ++i) |
| 613 | if (checkPlaceholderForOverload(S, args[i], &unbridged)) |
| 614 | return true; |
| 615 | |
| 616 | return false; |
| 617 | } |
| 618 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 619 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 620 | // overload of the declarations in Old. This routine returns false if |
| 621 | // New and Old cannot be overloaded, e.g., if New has the same |
| 622 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 623 | // declarations aren't functions (or function templates) at all. When |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 624 | // it does return false, MatchedDecl will point to the decl that New |
| 625 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 626 | // top of the underlying declaration. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 627 | // |
| 628 | // Example: Given the following input: |
| 629 | // |
| 630 | // void f(int, float); // #1 |
| 631 | // void f(int, int); // #2 |
| 632 | // int f(int, int); // #3 |
| 633 | // |
| 634 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | // so IsOverload will not be used. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 636 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 637 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 638 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 639 | // (since they have different signatures), so this routine returns |
| 640 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 641 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 642 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 643 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 644 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 645 | // identical (return types of functions are not part of the |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 646 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 647 | // point to the FunctionDecl for #2. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 648 | // |
| 649 | // 'NewIsUsingShadowDecl' indicates that 'New' is being introduced |
| 650 | // into a class by a using declaration. The rules for whether to hide |
| 651 | // shadow declarations ignore some properties which otherwise figure |
| 652 | // into a function template's signature. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 653 | Sema::OverloadKind |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 654 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
| 655 | NamedDecl *&Match, bool NewIsUsingDecl) { |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 656 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 657 | I != E; ++I) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 658 | NamedDecl *OldD = *I; |
| 659 | |
| 660 | bool OldIsUsingDecl = false; |
| 661 | if (isa<UsingShadowDecl>(OldD)) { |
| 662 | OldIsUsingDecl = true; |
| 663 | |
| 664 | // We can always introduce two using declarations into the same |
| 665 | // context, even if they have identical signatures. |
| 666 | if (NewIsUsingDecl) continue; |
| 667 | |
| 668 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 669 | } |
| 670 | |
| 671 | // If either declaration was introduced by a using declaration, |
| 672 | // we'll need to use slightly different rules for matching. |
| 673 | // Essentially, these rules are the normal rules, except that |
| 674 | // function templates hide function templates with different |
| 675 | // return types or template parameter lists. |
| 676 | bool UseMemberUsingDeclRules = |
| 677 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord(); |
| 678 | |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 679 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 680 | if (!IsOverload(New, OldT->getTemplatedDecl(), UseMemberUsingDeclRules)) { |
| 681 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 682 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 683 | continue; |
| 684 | } |
| 685 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 686 | Match = *I; |
| 687 | return Ovl_Match; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 688 | } |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 689 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 690 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
| 691 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 692 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 693 | continue; |
| 694 | } |
| 695 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 696 | Match = *I; |
| 697 | return Ovl_Match; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 698 | } |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 699 | } else if (isa<UsingDecl>(OldD)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 700 | // We can overload with these, which can show up when doing |
| 701 | // redeclaration checks for UsingDecls. |
| 702 | assert(Old.getLookupKind() == LookupUsingDeclName); |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 703 | } else if (isa<TagDecl>(OldD)) { |
| 704 | // We can always overload with tags by hiding them. |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 705 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 706 | // Optimistically assume that an unresolved using decl will |
| 707 | // overload; if it doesn't, we'll have to diagnose during |
| 708 | // template instantiation. |
| 709 | } else { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 710 | // (C++ 13p1): |
| 711 | // Only function declarations can be overloaded; object and type |
| 712 | // declarations cannot be overloaded. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 713 | Match = *I; |
| 714 | return Ovl_NonFunction; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 715 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 716 | } |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 717 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 718 | return Ovl_Overload; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 719 | } |
| 720 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 721 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| 722 | bool UseUsingDeclRules) { |
John McCall | 7b49202 | 2010-08-12 07:09:11 +0000 | [diff] [blame] | 723 | // If both of the functions are extern "C", then they are not |
| 724 | // overloads. |
| 725 | if (Old->isExternC() && New->isExternC()) |
| 726 | return false; |
| 727 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 728 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 729 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 730 | |
| 731 | // C++ [temp.fct]p2: |
| 732 | // A function template can be overloaded with other function templates |
| 733 | // and with normal (non-template) functions. |
| 734 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 735 | return true; |
| 736 | |
| 737 | // Is the function New an overload of the function Old? |
| 738 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 739 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 740 | |
| 741 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 742 | // determine whether they are overloads. If we find any mismatch |
| 743 | // in the signature, they are overloads. |
| 744 | |
| 745 | // If either of these functions is a K&R-style function (no |
| 746 | // prototype), then we consider them to have matching signatures. |
| 747 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 748 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 749 | return false; |
| 750 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 751 | const FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 752 | const FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 753 | |
| 754 | // The signature of a function includes the types of its |
| 755 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 756 | // of the ellipsis; see C++ DR 357). |
| 757 | if (OldQType != NewQType && |
| 758 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 759 | OldType->isVariadic() != NewType->isVariadic() || |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 760 | !FunctionArgTypesAreEqual(OldType, NewType))) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 761 | return true; |
| 762 | |
| 763 | // C++ [temp.over.link]p4: |
| 764 | // The signature of a function template consists of its function |
| 765 | // signature, its return type and its template parameter list. The names |
| 766 | // of the template parameters are significant only for establishing the |
| 767 | // relationship between the template parameters and the rest of the |
| 768 | // signature. |
| 769 | // |
| 770 | // We check the return type and template parameter lists for function |
| 771 | // templates first; the remaining checks follow. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 772 | // |
| 773 | // However, we don't consider either of these when deciding whether |
| 774 | // a member introduced by a shadow declaration is hidden. |
| 775 | if (!UseUsingDeclRules && NewTemplate && |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 776 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 777 | OldTemplate->getTemplateParameters(), |
| 778 | false, TPL_TemplateMatch) || |
| 779 | OldType->getResultType() != NewType->getResultType())) |
| 780 | return true; |
| 781 | |
| 782 | // If the function is a class member, its signature includes the |
Douglas Gregor | 57c9f4f | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 783 | // 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] | 784 | // |
| 785 | // As part of this, also check whether one of the member functions |
| 786 | // is static, in which case they are not overloads (C++ |
| 787 | // 13.1p2). While not part of the definition of the signature, |
| 788 | // this check is important to determine whether these functions |
| 789 | // can be overloaded. |
| 790 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 791 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 792 | if (OldMethod && NewMethod && |
| 793 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
Douglas Gregor | 57c9f4f | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 794 | (OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers() || |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 795 | OldMethod->getRefQualifier() != NewMethod->getRefQualifier())) { |
| 796 | if (!UseUsingDeclRules && |
| 797 | OldMethod->getRefQualifier() != NewMethod->getRefQualifier() && |
| 798 | (OldMethod->getRefQualifier() == RQ_None || |
| 799 | NewMethod->getRefQualifier() == RQ_None)) { |
| 800 | // C++0x [over.load]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 801 | // - Member function declarations with the same name and the same |
| 802 | // parameter-type-list as well as member function template |
| 803 | // declarations with the same name, the same parameter-type-list, and |
| 804 | // the same template parameter lists cannot be overloaded if any of |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 805 | // them, but not all, have a ref-qualifier (8.3.5). |
| 806 | Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) |
| 807 | << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); |
| 808 | Diag(OldMethod->getLocation(), diag::note_previous_declaration); |
| 809 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 810 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 811 | return true; |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 812 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 813 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 814 | // The signatures match; this is not an overload. |
| 815 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 818 | /// \brief Checks availability of the function depending on the current |
| 819 | /// function context. Inside an unavailable function, unavailability is ignored. |
| 820 | /// |
| 821 | /// \returns true if \arg FD is unavailable and current context is inside |
| 822 | /// an available function, false otherwise. |
| 823 | bool Sema::isFunctionConsideredUnavailable(FunctionDecl *FD) { |
| 824 | return FD->isUnavailable() && !cast<Decl>(CurContext)->isUnavailable(); |
| 825 | } |
| 826 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 827 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 828 | /// from the given expression (Expr) to the given type (ToType). This |
| 829 | /// function returns an implicit conversion sequence that can be used |
| 830 | /// to perform the initialization. Given |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 831 | /// |
| 832 | /// void f(float f); |
| 833 | /// void g(int i) { f(i); } |
| 834 | /// |
| 835 | /// this routine would produce an implicit conversion sequence to |
| 836 | /// describe the initialization of f from i, which will be a standard |
| 837 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 838 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 839 | // |
| 840 | /// Note that this routine only determines how the conversion can be |
| 841 | /// performed; it does not actually perform the conversion. As such, |
| 842 | /// it will not produce any diagnostics if no conversion is available, |
| 843 | /// but will instead return an implicit conversion sequence of kind |
| 844 | /// "BadConversion". |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 845 | /// |
| 846 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 847 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 848 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 849 | /// permitted. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 850 | /// |
| 851 | /// \param AllowObjCWritebackConversion Whether we allow the Objective-C |
| 852 | /// writeback conversion, which allows __autoreleasing id* parameters to |
| 853 | /// be initialized with __strong id* or __weak id* arguments. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 854 | static ImplicitConversionSequence |
| 855 | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
| 856 | bool SuppressUserConversions, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 857 | bool AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 858 | bool InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 859 | bool CStyle, |
| 860 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 861 | ImplicitConversionSequence ICS; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 862 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 863 | ICS.Standard, CStyle, AllowObjCWritebackConversion)){ |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 864 | ICS.setStandard(); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 865 | return ICS; |
| 866 | } |
| 867 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 868 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 869 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 870 | return ICS; |
| 871 | } |
| 872 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 873 | // C++ [over.ics.user]p4: |
| 874 | // A conversion of an expression of class type to the same class |
| 875 | // type is given Exact Match rank, and a conversion of an |
| 876 | // expression of class type to a base class of that type is |
| 877 | // given Conversion rank, in spite of the fact that a copy/move |
| 878 | // constructor (i.e., a user-defined conversion function) is |
| 879 | // called for those cases. |
| 880 | QualType FromType = From->getType(); |
| 881 | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 882 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
| 883 | S.IsDerivedFrom(FromType, ToType))) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 884 | ICS.setStandard(); |
| 885 | ICS.Standard.setAsIdentityConversion(); |
| 886 | ICS.Standard.setFromType(FromType); |
| 887 | ICS.Standard.setAllToTypes(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 888 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 889 | // We don't actually check at this point whether there is a valid |
| 890 | // copy/move constructor, since overloading just assumes that it |
| 891 | // exists. When we actually perform initialization, we'll find the |
| 892 | // appropriate constructor to copy the returned object, if needed. |
| 893 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 894 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 895 | // Determine whether this is considered a derived-to-base conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 896 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 897 | ICS.Standard.Second = ICK_Derived_To_Base; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 898 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 899 | return ICS; |
| 900 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 901 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 902 | if (SuppressUserConversions) { |
| 903 | // We're not in the case above, so there is no conversion that |
| 904 | // we can perform. |
| 905 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 906 | return ICS; |
| 907 | } |
| 908 | |
| 909 | // Attempt user-defined conversion. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 910 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 911 | OverloadingResult UserDefResult |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 912 | = IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, Conversions, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 913 | AllowExplicit); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 914 | |
| 915 | if (UserDefResult == OR_Success) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 916 | ICS.setUserDefined(); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 917 | // C++ [over.ics.user]p4: |
| 918 | // A conversion of an expression of class type to the same class |
| 919 | // type is given Exact Match rank, and a conversion of an |
| 920 | // expression of class type to a base class of that type is |
| 921 | // given Conversion rank, in spite of the fact that a copy |
| 922 | // constructor (i.e., a user-defined conversion function) is |
| 923 | // called for those cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 924 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 925 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 926 | QualType FromCanon |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 927 | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 928 | QualType ToCanon |
| 929 | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 930 | if (Constructor->isCopyConstructor() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 931 | (FromCanon == ToCanon || S.IsDerivedFrom(FromCanon, ToCanon))) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 932 | // Turn this into a "standard" conversion sequence, so that it |
| 933 | // gets ranked with standard conversion sequences. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 934 | ICS.setStandard(); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 935 | ICS.Standard.setAsIdentityConversion(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 936 | ICS.Standard.setFromType(From->getType()); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 937 | ICS.Standard.setAllToTypes(ToType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 938 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 939 | if (ToCanon != FromCanon) |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 940 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 941 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 942 | } |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 943 | |
| 944 | // C++ [over.best.ics]p4: |
| 945 | // However, when considering the argument of a user-defined |
| 946 | // conversion function that is a candidate by 13.3.1.3 when |
| 947 | // invoked for the copying of the temporary in the second step |
| 948 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 949 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 950 | // ellipsis conversion sequences are allowed. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 951 | if (SuppressUserConversions && ICS.isUserDefined()) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 952 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 953 | } |
John McCall | cefd3ad | 2010-01-13 22:30:33 +0000 | [diff] [blame] | 954 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 955 | ICS.setAmbiguous(); |
| 956 | ICS.Ambiguous.setFromType(From->getType()); |
| 957 | ICS.Ambiguous.setToType(ToType); |
| 958 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 959 | Cand != Conversions.end(); ++Cand) |
| 960 | if (Cand->Viable) |
| 961 | ICS.Ambiguous.addConversion(Cand->Function); |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 962 | } else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 963 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 964 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 965 | |
| 966 | return ICS; |
| 967 | } |
| 968 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 969 | ImplicitConversionSequence |
| 970 | Sema::TryImplicitConversion(Expr *From, QualType ToType, |
| 971 | bool SuppressUserConversions, |
| 972 | bool AllowExplicit, |
| 973 | bool InOverloadResolution, |
| 974 | bool CStyle, |
| 975 | bool AllowObjCWritebackConversion) { |
| 976 | return clang::TryImplicitConversion(*this, From, ToType, |
| 977 | SuppressUserConversions, AllowExplicit, |
| 978 | InOverloadResolution, CStyle, |
| 979 | AllowObjCWritebackConversion); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 982 | /// PerformImplicitConversion - Perform an implicit conversion of the |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 983 | /// expression From to the type ToType. Returns the |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 984 | /// converted expression. Flavor is the kind of conversion we're |
| 985 | /// performing, used in the error message. If @p AllowExplicit, |
| 986 | /// explicit user-defined conversions are permitted. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 987 | ExprResult |
| 988 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 989 | AssignmentAction Action, bool AllowExplicit) { |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 990 | ImplicitConversionSequence ICS; |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 991 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 992 | } |
| 993 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 994 | ExprResult |
| 995 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 996 | AssignmentAction Action, bool AllowExplicit, |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 997 | ImplicitConversionSequence& ICS) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 998 | if (checkPlaceholderForOverload(*this, From)) |
| 999 | return ExprError(); |
| 1000 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1001 | // Objective-C ARC: Determine whether we will allow the writeback conversion. |
| 1002 | bool AllowObjCWritebackConversion |
| 1003 | = getLangOptions().ObjCAutoRefCount && |
| 1004 | (Action == AA_Passing || Action == AA_Sending); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1005 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1006 | ICS = clang::TryImplicitConversion(*this, From, ToType, |
| 1007 | /*SuppressUserConversions=*/false, |
| 1008 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1009 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1010 | /*CStyle=*/false, |
| 1011 | AllowObjCWritebackConversion); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 1012 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 1013 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1014 | |
| 1015 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1016 | /// conversion that strips "noreturn" off the nested function type. |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1017 | bool Sema::IsNoReturnConversion(QualType FromType, QualType ToType, |
| 1018 | QualType &ResultTy) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1019 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1020 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1021 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1022 | // Permit the conversion F(t __attribute__((noreturn))) -> F(t) |
| 1023 | // where F adds one of the following at most once: |
| 1024 | // - a pointer |
| 1025 | // - a member pointer |
| 1026 | // - a block pointer |
| 1027 | CanQualType CanTo = Context.getCanonicalType(ToType); |
| 1028 | CanQualType CanFrom = Context.getCanonicalType(FromType); |
| 1029 | Type::TypeClass TyClass = CanTo->getTypeClass(); |
| 1030 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1031 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) { |
| 1032 | if (TyClass == Type::Pointer) { |
| 1033 | CanTo = CanTo.getAs<PointerType>()->getPointeeType(); |
| 1034 | CanFrom = CanFrom.getAs<PointerType>()->getPointeeType(); |
| 1035 | } else if (TyClass == Type::BlockPointer) { |
| 1036 | CanTo = CanTo.getAs<BlockPointerType>()->getPointeeType(); |
| 1037 | CanFrom = CanFrom.getAs<BlockPointerType>()->getPointeeType(); |
| 1038 | } else if (TyClass == Type::MemberPointer) { |
| 1039 | CanTo = CanTo.getAs<MemberPointerType>()->getPointeeType(); |
| 1040 | CanFrom = CanFrom.getAs<MemberPointerType>()->getPointeeType(); |
| 1041 | } else { |
| 1042 | return false; |
| 1043 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1044 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 1045 | TyClass = CanTo->getTypeClass(); |
| 1046 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 1047 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) |
| 1048 | return false; |
| 1049 | } |
| 1050 | |
| 1051 | const FunctionType *FromFn = cast<FunctionType>(CanFrom); |
| 1052 | FunctionType::ExtInfo EInfo = FromFn->getExtInfo(); |
| 1053 | if (!EInfo.getNoReturn()) return false; |
| 1054 | |
| 1055 | FromFn = Context.adjustFunctionType(FromFn, EInfo.withNoReturn(false)); |
| 1056 | assert(QualType(FromFn, 0).isCanonical()); |
| 1057 | if (QualType(FromFn, 0) != CanTo) return false; |
| 1058 | |
| 1059 | ResultTy = ToType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1060 | return true; |
| 1061 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1062 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1063 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 1064 | /// vector conversion. |
| 1065 | /// |
| 1066 | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
| 1067 | /// conversion. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1068 | static bool IsVectorConversion(ASTContext &Context, QualType FromType, |
| 1069 | QualType ToType, ImplicitConversionKind &ICK) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1070 | // We need at least one of these types to be a vector type to have a vector |
| 1071 | // conversion. |
| 1072 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
| 1073 | return false; |
| 1074 | |
| 1075 | // Identical types require no conversions. |
| 1076 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1077 | return false; |
| 1078 | |
| 1079 | // There are no conversions between extended vector types, only identity. |
| 1080 | if (ToType->isExtVectorType()) { |
| 1081 | // There are no conversions between extended vector types other than the |
| 1082 | // identity conversion. |
| 1083 | if (FromType->isExtVectorType()) |
| 1084 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1085 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1086 | // Vector splat from any arithmetic type to a vector. |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 1087 | if (FromType->isArithmeticType()) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1088 | ICK = ICK_Vector_Splat; |
| 1089 | return true; |
| 1090 | } |
| 1091 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1092 | |
| 1093 | // We can perform the conversion between vector types in the following cases: |
| 1094 | // 1)vector types are equivalent AltiVec and GCC vector types |
| 1095 | // 2)lax vector conversions are permitted and the vector types are of the |
| 1096 | // same size |
| 1097 | if (ToType->isVectorType() && FromType->isVectorType()) { |
| 1098 | if (Context.areCompatibleVectorTypes(FromType, ToType) || |
Chandler Carruth | c45eb9c | 2010-08-08 05:02:51 +0000 | [diff] [blame] | 1099 | (Context.getLangOptions().LaxVectorConversions && |
| 1100 | (Context.getTypeSize(FromType) == Context.getTypeSize(ToType)))) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1101 | ICK = ICK_Vector_Conversion; |
| 1102 | return true; |
| 1103 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1104 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1105 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1106 | return false; |
| 1107 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1108 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1109 | /// IsStandardConversion - Determines whether there is a standard |
| 1110 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 1111 | /// expression From to the type ToType. Standard conversion sequences |
| 1112 | /// only consider non-class types; for conversions that involve class |
| 1113 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 1114 | /// contain the standard conversion sequence required to perform this |
| 1115 | /// conversion and this routine will return true. Otherwise, this |
| 1116 | /// routine will return false and the value of SCS is unspecified. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1117 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 1118 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1119 | StandardConversionSequence &SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1120 | bool CStyle, |
| 1121 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1122 | QualType FromType = From->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1123 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1124 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1125 | SCS.setAsIdentityConversion(); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1126 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1127 | SCS.IncompatibleObjC = false; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1128 | SCS.setFromType(FromType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1129 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1130 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1131 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1132 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1133 | if (FromType->isRecordType() || ToType->isRecordType()) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1134 | if (S.getLangOptions().CPlusPlus) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1135 | return false; |
| 1136 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1137 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1140 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 1141 | // array-to-pointer conversion, or function-to-pointer conversion |
| 1142 | // (C++ 4p1). |
| 1143 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1144 | if (FromType == S.Context.OverloadTy) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1145 | DeclAccessPair AccessPair; |
| 1146 | if (FunctionDecl *Fn |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1147 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1148 | AccessPair)) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1149 | // We were able to resolve the address of the overloaded function, |
| 1150 | // so we can convert to the type of that function. |
| 1151 | FromType = Fn->getType(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1152 | |
| 1153 | // we can sometimes resolve &foo<int> regardless of ToType, so check |
| 1154 | // if the type matches (identity) or we are converting to bool |
| 1155 | if (!S.Context.hasSameUnqualifiedType( |
| 1156 | S.ExtractUnqualifiedFunctionType(ToType), FromType)) { |
| 1157 | QualType resultTy; |
| 1158 | // if the function type matches except for [[noreturn]], it's ok |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1159 | if (!S.IsNoReturnConversion(FromType, |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1160 | S.ExtractUnqualifiedFunctionType(ToType), resultTy)) |
| 1161 | // otherwise, only a boolean conversion is standard |
| 1162 | if (!ToType->isBooleanType()) |
| 1163 | return false; |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1164 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1165 | |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1166 | // Check if the "from" expression is taking the address of an overloaded |
| 1167 | // function and recompute the FromType accordingly. Take advantage of the |
| 1168 | // fact that non-static member functions *must* have such an address-of |
| 1169 | // expression. |
| 1170 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn); |
| 1171 | if (Method && !Method->isStatic()) { |
| 1172 | assert(isa<UnaryOperator>(From->IgnoreParens()) && |
| 1173 | "Non-unary operator on non-static member address"); |
| 1174 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() |
| 1175 | == UO_AddrOf && |
| 1176 | "Non-address-of operator on non-static member address"); |
| 1177 | const Type *ClassType |
| 1178 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 1179 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
Chandler Carruth | fc5c8fc | 2011-03-29 18:38:10 +0000 | [diff] [blame] | 1180 | } else if (isa<UnaryOperator>(From->IgnoreParens())) { |
| 1181 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() == |
| 1182 | UO_AddrOf && |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1183 | "Non-address-of operator for overloaded function expression"); |
| 1184 | FromType = S.Context.getPointerType(FromType); |
| 1185 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1186 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1187 | // Check that we've computed the proper type after overload resolution. |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1188 | assert(S.Context.hasSameType( |
| 1189 | FromType, |
| 1190 | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1191 | } else { |
| 1192 | return false; |
| 1193 | } |
Anders Carlsson | 2bd6250 | 2010-11-04 05:28:09 +0000 | [diff] [blame] | 1194 | } |
John McCall | 2148011 | 2011-08-30 00:57:29 +0000 | [diff] [blame] | 1195 | // Lvalue-to-rvalue conversion (C++11 4.1): |
| 1196 | // A glvalue (3.10) of a non-function, non-array type T can |
| 1197 | // be converted to a prvalue. |
| 1198 | bool argIsLValue = From->isGLValue(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1199 | if (argIsLValue && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1200 | !FromType->isFunctionType() && !FromType->isArrayType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1201 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1202 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1203 | |
| 1204 | // If T is a non-class type, the type of the rvalue is the |
| 1205 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1206 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 1207 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1208 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1209 | } else if (FromType->isArrayType()) { |
| 1210 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1211 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1212 | |
| 1213 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 1214 | // bound of T" can be converted to an rvalue of type "pointer to |
| 1215 | // T" (C++ 4.2p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1216 | FromType = S.Context.getArrayDecayedType(FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1217 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1218 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1219 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1220 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1221 | |
| 1222 | // For the purpose of ranking in overload resolution |
| 1223 | // (13.3.3.1.1), this conversion is considered an |
| 1224 | // array-to-pointer conversion followed by a qualification |
| 1225 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1226 | SCS.Second = ICK_Identity; |
| 1227 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1228 | SCS.QualificationIncludesObjCLifetime = false; |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1229 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1230 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1231 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1232 | } else if (FromType->isFunctionType() && argIsLValue) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1233 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1234 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1235 | |
| 1236 | // An lvalue of function type T can be converted to an rvalue of |
| 1237 | // type "pointer to T." The result is a pointer to the |
| 1238 | // function. (C++ 4.3p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1239 | FromType = S.Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1240 | } else { |
| 1241 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1242 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1243 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1244 | SCS.setToType(0, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1245 | |
| 1246 | // The second conversion can be an integral promotion, floating |
| 1247 | // point promotion, integral conversion, floating point conversion, |
| 1248 | // floating-integral conversion, pointer conversion, |
| 1249 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1250 | // For overloading in C, this can also be a "compatible-type" |
| 1251 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1252 | bool IncompatibleObjC = false; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1253 | ImplicitConversionKind SecondICK = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1254 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1255 | // The unqualified versions of the types are the same: there's no |
| 1256 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1257 | SCS.Second = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1258 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1259 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1260 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1261 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1262 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1263 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1264 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1265 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1266 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1267 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1268 | SCS.Second = ICK_Complex_Promotion; |
| 1269 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1270 | } else if (ToType->isBooleanType() && |
| 1271 | (FromType->isArithmeticType() || |
| 1272 | FromType->isAnyPointerType() || |
| 1273 | FromType->isBlockPointerType() || |
| 1274 | FromType->isMemberPointerType() || |
| 1275 | FromType->isNullPtrType())) { |
| 1276 | // Boolean conversions (C++ 4.12). |
| 1277 | SCS.Second = ICK_Boolean_Conversion; |
| 1278 | FromType = S.Context.BoolTy; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1279 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1280 | ToType->isIntegralType(S.Context)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1281 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1282 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1283 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1284 | } else if (FromType->isAnyComplexType() && ToType->isComplexType()) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1285 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1286 | SCS.Second = ICK_Complex_Conversion; |
| 1287 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1288 | } else if ((FromType->isAnyComplexType() && ToType->isArithmeticType()) || |
| 1289 | (ToType->isAnyComplexType() && FromType->isArithmeticType())) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1290 | // Complex-real conversions (C99 6.3.1.7) |
| 1291 | SCS.Second = ICK_Complex_Real; |
| 1292 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1293 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1294 | // Floating point conversions (C++ 4.8). |
| 1295 | SCS.Second = ICK_Floating_Conversion; |
| 1296 | FromType = ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1297 | } else if ((FromType->isRealFloatingType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1298 | ToType->isIntegralType(S.Context)) || |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1299 | (FromType->isIntegralOrUnscopedEnumerationType() && |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1300 | ToType->isRealFloatingType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1301 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1302 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1303 | FromType = ToType.getUnqualifiedType(); |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1304 | } else if (S.IsBlockPointerConversion(FromType, ToType, FromType)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1305 | SCS.Second = ICK_Block_Pointer_Conversion; |
| 1306 | } else if (AllowObjCWritebackConversion && |
| 1307 | S.isObjCWritebackConversion(FromType, ToType, FromType)) { |
| 1308 | SCS.Second = ICK_Writeback_Conversion; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1309 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1310 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1311 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1312 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1313 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1314 | FromType = FromType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1315 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1316 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1317 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1318 | SCS.Second = ICK_Pointer_Member; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1319 | } else if (IsVectorConversion(S.Context, FromType, ToType, SecondICK)) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1320 | SCS.Second = SecondICK; |
| 1321 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1322 | } else if (!S.getLangOptions().CPlusPlus && |
| 1323 | S.Context.typesAreCompatible(ToType, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1324 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1325 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1326 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1327 | } else if (S.IsNoReturnConversion(FromType, ToType, FromType)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1328 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1329 | SCS.Second = ICK_NoReturn_Adjustment; |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1330 | } else if (IsTransparentUnionStandardConversion(S, From, ToType, |
| 1331 | InOverloadResolution, |
| 1332 | SCS, CStyle)) { |
| 1333 | SCS.Second = ICK_TransparentUnionConversion; |
| 1334 | FromType = ToType; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1335 | } else { |
| 1336 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1337 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1338 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1339 | SCS.setToType(1, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1340 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1341 | QualType CanonFrom; |
| 1342 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1343 | // The third conversion can be a qualification conversion (C++ 4p1). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1344 | bool ObjCLifetimeConversion; |
| 1345 | if (S.IsQualificationConversion(FromType, ToType, CStyle, |
| 1346 | ObjCLifetimeConversion)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1347 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1348 | SCS.QualificationIncludesObjCLifetime = ObjCLifetimeConversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1349 | FromType = ToType; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1350 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1351 | CanonTo = S.Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1352 | } else { |
| 1353 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1354 | SCS.Third = ICK_Identity; |
| 1355 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1356 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1357 | // [...] Any difference in top-level cv-qualification is |
| 1358 | // subsumed by the initialization itself and does not constitute |
| 1359 | // a conversion. [...] |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1360 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1361 | CanonTo = S.Context.getCanonicalType(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1362 | if (CanonFrom.getLocalUnqualifiedType() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1363 | == CanonTo.getLocalUnqualifiedType() && |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 1364 | (CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers() |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1365 | || CanonFrom.getObjCGCAttr() != CanonTo.getObjCGCAttr() |
| 1366 | || CanonFrom.getObjCLifetime() != CanonTo.getObjCLifetime())) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1367 | FromType = ToType; |
| 1368 | CanonFrom = CanonTo; |
| 1369 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1370 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1371 | SCS.setToType(2, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1372 | |
| 1373 | // If we have not converted the argument type to the parameter type, |
| 1374 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1375 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1376 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1377 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1378 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1379 | } |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1380 | |
| 1381 | static bool |
| 1382 | IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 1383 | QualType &ToType, |
| 1384 | bool InOverloadResolution, |
| 1385 | StandardConversionSequence &SCS, |
| 1386 | bool CStyle) { |
| 1387 | |
| 1388 | const RecordType *UT = ToType->getAsUnionType(); |
| 1389 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 1390 | return false; |
| 1391 | // The field to initialize within the transparent union. |
| 1392 | RecordDecl *UD = UT->getDecl(); |
| 1393 | // It's compatible if the expression matches any of the fields. |
| 1394 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1395 | itend = UD->field_end(); |
| 1396 | it != itend; ++it) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1397 | if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS, |
| 1398 | CStyle, /*ObjCWritebackConversion=*/false)) { |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1399 | ToType = it->getType(); |
| 1400 | return true; |
| 1401 | } |
| 1402 | } |
| 1403 | return false; |
| 1404 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1405 | |
| 1406 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1407 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1408 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1409 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1411 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1412 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1413 | if (!To) { |
| 1414 | return false; |
| 1415 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1416 | |
| 1417 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1418 | // unsigned short int can be converted to an rvalue of type int if |
| 1419 | // int can represent all the values of the source type; otherwise, |
| 1420 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1421 | // int (C++ 4.5p1). |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1422 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1423 | !FromType->isEnumeralType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1424 | if (// We can promote any signed, promotable integer type to an int |
| 1425 | (FromType->isSignedIntegerType() || |
| 1426 | // We can promote any unsigned integer type whose size is |
| 1427 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1429 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1430 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1433 | return To->getKind() == BuiltinType::UInt; |
| 1434 | } |
| 1435 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1436 | // C++0x [conv.prom]p3: |
| 1437 | // A prvalue of an unscoped enumeration type whose underlying type is not |
| 1438 | // fixed (7.2) can be converted to an rvalue a prvalue of the first of the |
| 1439 | // following types that can represent all the values of the enumeration |
| 1440 | // (i.e., the values in the range bmin to bmax as described in 7.2): int, |
| 1441 | // unsigned int, long int, unsigned long int, long long int, or unsigned |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1442 | // 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] | 1443 | // values of the enumeration, an rvalue a prvalue of an unscoped enumeration |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1444 | // 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] | 1445 | // with lowest integer conversion rank (4.13) greater than the rank of long |
| 1446 | // 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] | 1447 | // there are two such extended types, the signed one is chosen. |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1448 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
| 1449 | // C++0x 7.2p9: Note that this implicit enum to int conversion is not |
| 1450 | // provided for a scoped enumeration. |
| 1451 | if (FromEnumType->getDecl()->isScoped()) |
| 1452 | return false; |
| 1453 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1454 | // We have already pre-calculated the promotion type, so this is trivial. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1455 | if (ToType->isIntegerType() && |
Douglas Gregor | 5dde160 | 2010-09-12 03:38:25 +0000 | [diff] [blame] | 1456 | !RequireCompleteType(From->getLocStart(), FromType, PDiag())) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1457 | return Context.hasSameUnqualifiedType(ToType, |
| 1458 | FromEnumType->getDecl()->getPromotionType()); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1459 | } |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1460 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1461 | // C++0x [conv.prom]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1462 | // A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted |
| 1463 | // to an rvalue a prvalue of the first of the following types that can |
| 1464 | // represent all the values of its underlying type: int, unsigned int, |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1465 | // 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] | 1466 | // 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] | 1467 | // underlying type, an rvalue a prvalue of type char16_t, char32_t, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1468 | // 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] | 1469 | // type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1470 | if (FromType->isAnyCharacterType() && !FromType->isCharType() && |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1471 | ToType->isIntegerType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1472 | // Determine whether the type we're converting from is signed or |
| 1473 | // unsigned. |
David Majnemer | 0ad9231 | 2011-07-22 21:09:04 +0000 | [diff] [blame] | 1474 | bool FromIsSigned = FromType->isSignedIntegerType(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1475 | uint64_t FromSize = Context.getTypeSize(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1476 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1477 | // The types we'll try to promote to, in the appropriate |
| 1478 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1479 | QualType PromoteTypes[6] = { |
| 1480 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1481 | Context.LongTy, Context.UnsignedLongTy , |
| 1482 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1483 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1484 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1485 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1486 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1488 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1489 | // We found the type that we can promote to. If this is the |
| 1490 | // type we wanted, we have a promotion. Otherwise, no |
| 1491 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1492 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1493 | } |
| 1494 | } |
| 1495 | } |
| 1496 | |
| 1497 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1498 | // rvalue of type int if int can represent all the values of the |
| 1499 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1500 | // unsigned int can represent all the values of the bit-field. If |
| 1501 | // the bit-field is larger yet, no integral promotion applies to |
| 1502 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1503 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1504 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1505 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1506 | using llvm::APSInt; |
| 1507 | if (From) |
| 1508 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1509 | APSInt BitWidth; |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1510 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1511 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1512 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1513 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1514 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1515 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1516 | if (BitWidth < ToSize || |
| 1517 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1518 | return To->getKind() == BuiltinType::Int; |
| 1519 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1520 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1521 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1522 | // that fits into an unsigned int? |
| 1523 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1524 | return To->getKind() == BuiltinType::UInt; |
| 1525 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1526 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1527 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1528 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1529 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1530 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1531 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1532 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1533 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1534 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1535 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1536 | |
| 1537 | return false; |
| 1538 | } |
| 1539 | |
| 1540 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1541 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1542 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1543 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1544 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1545 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1546 | /// An rvalue of type float can be converted to an rvalue of type |
| 1547 | /// double. (C++ 4.6p1). |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1548 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1549 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1550 | return true; |
| 1551 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1552 | // C99 6.3.1.5p1: |
| 1553 | // When a float is promoted to double or long double, or a |
| 1554 | // double is promoted to long double [...]. |
| 1555 | if (!getLangOptions().CPlusPlus && |
| 1556 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1557 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1558 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1559 | return true; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1560 | |
| 1561 | // Half can be promoted to float. |
| 1562 | if (FromBuiltin->getKind() == BuiltinType::Half && |
| 1563 | ToBuiltin->getKind() == BuiltinType::Float) |
| 1564 | return true; |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1567 | return false; |
| 1568 | } |
| 1569 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1570 | /// \brief Determine if a conversion is a complex promotion. |
| 1571 | /// |
| 1572 | /// A complex promotion is defined as a complex -> complex conversion |
| 1573 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1574 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1575 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1576 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1577 | if (!FromComplex) |
| 1578 | return false; |
| 1579 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1580 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1581 | if (!ToComplex) |
| 1582 | return false; |
| 1583 | |
| 1584 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1585 | ToComplex->getElementType()) || |
| 1586 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1587 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1590 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1591 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1592 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1593 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1594 | /// the right set of qualifiers on its pointee. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1595 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1596 | static QualType |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1597 | BuildSimilarlyQualifiedPointerType(const Type *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1598 | QualType ToPointee, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1599 | ASTContext &Context, |
| 1600 | bool StripObjCLifetime = false) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1601 | assert((FromPtr->getTypeClass() == Type::Pointer || |
| 1602 | FromPtr->getTypeClass() == Type::ObjCObjectPointer) && |
| 1603 | "Invalid similarly-qualified pointer type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1604 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1605 | /// Conversions to 'id' subsume cv-qualifier conversions. |
| 1606 | if (ToType->isObjCIdType() || ToType->isObjCQualifiedIdType()) |
Douglas Gregor | 143c7ac | 2010-12-06 22:09:19 +0000 | [diff] [blame] | 1607 | return ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1608 | |
| 1609 | QualType CanonFromPointee |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1610 | = Context.getCanonicalType(FromPtr->getPointeeType()); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1611 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1612 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1613 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1614 | if (StripObjCLifetime) |
| 1615 | Quals.removeObjCLifetime(); |
| 1616 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1617 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1618 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1619 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1620 | if (!ToType.isNull()) |
Douglas Gregor | af7bea5 | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1621 | return ToType.getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1622 | |
| 1623 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1624 | // already. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1625 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1626 | return Context.getObjCObjectPointerType(ToPointee); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1627 | return Context.getPointerType(ToPointee); |
| 1628 | } |
| 1629 | |
| 1630 | // Just build a canonical type that has the right qualifiers. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1631 | QualType QualifiedCanonToPointee |
| 1632 | = Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1633 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1634 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1635 | return Context.getObjCObjectPointerType(QualifiedCanonToPointee); |
| 1636 | return Context.getPointerType(QualifiedCanonToPointee); |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1637 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1638 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1639 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1640 | bool InOverloadResolution, |
| 1641 | ASTContext &Context) { |
| 1642 | // Handle value-dependent integral null pointer constants correctly. |
| 1643 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1644 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1645 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1646 | return !InOverloadResolution; |
| 1647 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1648 | return Expr->isNullPointerConstant(Context, |
| 1649 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1650 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1651 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1652 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1653 | /// IsPointerConversion - Determines whether the conversion of the |
| 1654 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1655 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1656 | /// 4.10). If so, returns true and places the converted type (that |
| 1657 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1658 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1659 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1660 | /// This routine also supports conversions to and from block pointers |
| 1661 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1662 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1663 | /// appropriate overloading rules for Objective-C, we may want to |
| 1664 | /// split the Objective-C checks into a different routine; however, |
| 1665 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1666 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1667 | /// set if the conversion is an allowed Objective-C conversion that |
| 1668 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1669 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1670 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1671 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1672 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1673 | IncompatibleObjC = false; |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1674 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, |
| 1675 | IncompatibleObjC)) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1676 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1677 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1679 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1680 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1681 | ConvertedType = ToType; |
| 1682 | return true; |
| 1683 | } |
| 1684 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1685 | // Blocks: Block pointers can be converted to void*. |
| 1686 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1687 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1688 | ConvertedType = ToType; |
| 1689 | return true; |
| 1690 | } |
| 1691 | // Blocks: A null pointer constant can be converted to a block |
| 1692 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1693 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1694 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1695 | ConvertedType = ToType; |
| 1696 | return true; |
| 1697 | } |
| 1698 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1699 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1700 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1701 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1702 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1703 | ConvertedType = ToType; |
| 1704 | return true; |
| 1705 | } |
| 1706 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1707 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1708 | if (!ToTypePtr) |
| 1709 | return false; |
| 1710 | |
| 1711 | // 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] | 1712 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1713 | ConvertedType = ToType; |
| 1714 | return true; |
| 1715 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1716 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1717 | // Beyond this point, both types need to be pointers |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1718 | // , including objective-c pointers. |
| 1719 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1720 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType() && |
| 1721 | !getLangOptions().ObjCAutoRefCount) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1722 | ConvertedType = BuildSimilarlyQualifiedPointerType( |
| 1723 | FromType->getAs<ObjCObjectPointerType>(), |
| 1724 | ToPointeeType, |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1725 | ToType, Context); |
| 1726 | return true; |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1727 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1728 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1729 | if (!FromTypePtr) |
| 1730 | return false; |
| 1731 | |
| 1732 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1733 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1734 | // 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] | 1735 | // pointer conversion, so don't do all of the work below. |
| 1736 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
| 1737 | return false; |
| 1738 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1739 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1740 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1741 | // 4.10p2). |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1742 | if (FromPointeeType->isIncompleteOrObjectType() && |
| 1743 | ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1744 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1745 | ToPointeeType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1746 | ToType, Context, |
| 1747 | /*StripObjCLifetime=*/true); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1748 | return true; |
| 1749 | } |
| 1750 | |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 1751 | // MSVC allows implicit function to void* type conversion. |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 1752 | if (getLangOptions().MicrosoftExt && FromPointeeType->isFunctionType() && |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 1753 | ToPointeeType->isVoidType()) { |
| 1754 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 1755 | ToPointeeType, |
| 1756 | ToType, Context); |
| 1757 | return true; |
| 1758 | } |
| 1759 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1760 | // When we're overloading in C, we allow a special kind of pointer |
| 1761 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1762 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1763 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1764 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1765 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1766 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1767 | return true; |
| 1768 | } |
| 1769 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1770 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1771 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1772 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1773 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1774 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1775 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1776 | // necessitates this conversion is ill-formed. The result of the |
| 1777 | // conversion is a pointer to the base class sub-object of the |
| 1778 | // derived class object. The null pointer value is converted to |
| 1779 | // the null pointer value of the destination type. |
| 1780 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1781 | // Note that we do not check for ambiguity or inaccessibility |
| 1782 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1783 | if (getLangOptions().CPlusPlus && |
| 1784 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | bf1764c | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 1785 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | 2685eab | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1786 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1787 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1788 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1789 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1790 | ToType, Context); |
| 1791 | return true; |
| 1792 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1793 | |
Fariborz Jahanian | 5da3c08 | 2011-04-14 20:33:36 +0000 | [diff] [blame] | 1794 | if (FromPointeeType->isVectorType() && ToPointeeType->isVectorType() && |
| 1795 | Context.areCompatibleVectorTypes(FromPointeeType, ToPointeeType)) { |
| 1796 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 1797 | ToPointeeType, |
| 1798 | ToType, Context); |
| 1799 | return true; |
| 1800 | } |
| 1801 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1802 | return false; |
| 1803 | } |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1804 | |
| 1805 | /// \brief Adopt the given qualifiers for the given type. |
| 1806 | static QualType AdoptQualifiers(ASTContext &Context, QualType T, Qualifiers Qs){ |
| 1807 | Qualifiers TQs = T.getQualifiers(); |
| 1808 | |
| 1809 | // Check whether qualifiers already match. |
| 1810 | if (TQs == Qs) |
| 1811 | return T; |
| 1812 | |
| 1813 | if (Qs.compatiblyIncludes(TQs)) |
| 1814 | return Context.getQualifiedType(T, Qs); |
| 1815 | |
| 1816 | return Context.getQualifiedType(T.getUnqualifiedType(), Qs); |
| 1817 | } |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1818 | |
| 1819 | /// isObjCPointerConversion - Determines whether this is an |
| 1820 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1821 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1822 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1823 | QualType& ConvertedType, |
| 1824 | bool &IncompatibleObjC) { |
| 1825 | if (!getLangOptions().ObjC1) |
| 1826 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1827 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1828 | // The set of qualifiers on the type we're converting from. |
| 1829 | Qualifiers FromQualifiers = FromType.getQualifiers(); |
| 1830 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1831 | // First, we handle all conversions on ObjC object pointer types. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1832 | const ObjCObjectPointerType* ToObjCPtr = |
| 1833 | ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1834 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1835 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1836 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1837 | if (ToObjCPtr && FromObjCPtr) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1838 | // If the pointee types are the same (ignoring qualifications), |
| 1839 | // then this is not a pointer conversion. |
| 1840 | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
| 1841 | FromObjCPtr->getPointeeType())) |
| 1842 | return false; |
| 1843 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1844 | // Check for compatible |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1845 | // 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] | 1846 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1847 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1848 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1849 | return true; |
| 1850 | } |
| 1851 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1852 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1853 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1854 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1855 | /*compare=*/false)) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1856 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1857 | return true; |
| 1858 | } |
| 1859 | // Objective C++: We're able to convert from a pointer to an |
| 1860 | // interface to a pointer to a different interface. |
| 1861 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 1862 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 1863 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
| 1864 | if (getLangOptions().CPlusPlus && LHS && RHS && |
| 1865 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 1866 | FromObjCPtr->getPointeeType())) |
| 1867 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1868 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1869 | ToObjCPtr->getPointeeType(), |
| 1870 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1871 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1872 | return true; |
| 1873 | } |
| 1874 | |
| 1875 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1876 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1877 | // interfaces, which is permitted. However, we're going to |
| 1878 | // complain about it. |
| 1879 | IncompatibleObjC = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1880 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1881 | ToObjCPtr->getPointeeType(), |
| 1882 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1883 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1884 | return true; |
| 1885 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1886 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1887 | // 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] | 1888 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1889 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1890 | ToPointeeType = ToCPtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1891 | else if (const BlockPointerType *ToBlockPtr = |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1892 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 1893 | // 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] | 1894 | // to a block pointer type. |
| 1895 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1896 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1897 | return true; |
| 1898 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1899 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1900 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1901 | else if (FromType->getAs<BlockPointerType>() && |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1902 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1903 | // 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] | 1904 | // pointer to any object. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1905 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1906 | return true; |
| 1907 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1908 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1909 | return false; |
| 1910 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1911 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1912 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1913 | FromPointeeType = FromCPtr->getPointeeType(); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1914 | else if (const BlockPointerType *FromBlockPtr = |
| 1915 | FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1916 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1917 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1918 | return false; |
| 1919 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1920 | // If we have pointers to pointers, recursively check whether this |
| 1921 | // is an Objective-C conversion. |
| 1922 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1923 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1924 | IncompatibleObjC)) { |
| 1925 | // We always complain about this conversion. |
| 1926 | IncompatibleObjC = true; |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1927 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1928 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1929 | return true; |
| 1930 | } |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1931 | // Allow conversion of pointee being objective-c pointer to another one; |
| 1932 | // as in I* to id. |
| 1933 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 1934 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 1935 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1936 | IncompatibleObjC)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1937 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1938 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1939 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1940 | return true; |
| 1941 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1942 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1943 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1944 | // differences in the argument and result types are in Objective-C |
| 1945 | // pointer conversions. If so, we permit the conversion (but |
| 1946 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1947 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1948 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1949 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1950 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1951 | if (FromFunctionType && ToFunctionType) { |
| 1952 | // If the function types are exactly the same, this isn't an |
| 1953 | // Objective-C pointer conversion. |
| 1954 | if (Context.getCanonicalType(FromPointeeType) |
| 1955 | == Context.getCanonicalType(ToPointeeType)) |
| 1956 | return false; |
| 1957 | |
| 1958 | // Perform the quick checks that will tell us whether these |
| 1959 | // function types are obviously different. |
| 1960 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1961 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1962 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1963 | return false; |
| 1964 | |
| 1965 | bool HasObjCConversion = false; |
| 1966 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1967 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1968 | // Okay, the types match exactly. Nothing to do. |
| 1969 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1970 | ToFunctionType->getResultType(), |
| 1971 | ConvertedType, IncompatibleObjC)) { |
| 1972 | // Okay, we have an Objective-C pointer conversion. |
| 1973 | HasObjCConversion = true; |
| 1974 | } else { |
| 1975 | // Function types are too different. Abort. |
| 1976 | return false; |
| 1977 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1978 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1979 | // Check argument types. |
| 1980 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1981 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1982 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1983 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1984 | if (Context.getCanonicalType(FromArgType) |
| 1985 | == Context.getCanonicalType(ToArgType)) { |
| 1986 | // Okay, the types match exactly. Nothing to do. |
| 1987 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1988 | ConvertedType, IncompatibleObjC)) { |
| 1989 | // Okay, we have an Objective-C pointer conversion. |
| 1990 | HasObjCConversion = true; |
| 1991 | } else { |
| 1992 | // Argument types are too different. Abort. |
| 1993 | return false; |
| 1994 | } |
| 1995 | } |
| 1996 | |
| 1997 | if (HasObjCConversion) { |
| 1998 | // We had an Objective-C conversion. Allow this pointer |
| 1999 | // conversion, but complain about it. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 2000 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 2001 | IncompatibleObjC = true; |
| 2002 | return true; |
| 2003 | } |
| 2004 | } |
| 2005 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2006 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2007 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2008 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2009 | /// \brief Determine whether this is an Objective-C writeback conversion, |
| 2010 | /// used for parameter passing when performing automatic reference counting. |
| 2011 | /// |
| 2012 | /// \param FromType The type we're converting form. |
| 2013 | /// |
| 2014 | /// \param ToType The type we're converting to. |
| 2015 | /// |
| 2016 | /// \param ConvertedType The type that will be produced after applying |
| 2017 | /// this conversion. |
| 2018 | bool Sema::isObjCWritebackConversion(QualType FromType, QualType ToType, |
| 2019 | QualType &ConvertedType) { |
| 2020 | if (!getLangOptions().ObjCAutoRefCount || |
| 2021 | Context.hasSameUnqualifiedType(FromType, ToType)) |
| 2022 | return false; |
| 2023 | |
| 2024 | // Parameter must be a pointer to __autoreleasing (with no other qualifiers). |
| 2025 | QualType ToPointee; |
| 2026 | if (const PointerType *ToPointer = ToType->getAs<PointerType>()) |
| 2027 | ToPointee = ToPointer->getPointeeType(); |
| 2028 | else |
| 2029 | return false; |
| 2030 | |
| 2031 | Qualifiers ToQuals = ToPointee.getQualifiers(); |
| 2032 | if (!ToPointee->isObjCLifetimeType() || |
| 2033 | ToQuals.getObjCLifetime() != Qualifiers::OCL_Autoreleasing || |
| 2034 | !ToQuals.withoutObjCGLifetime().empty()) |
| 2035 | return false; |
| 2036 | |
| 2037 | // Argument must be a pointer to __strong to __weak. |
| 2038 | QualType FromPointee; |
| 2039 | if (const PointerType *FromPointer = FromType->getAs<PointerType>()) |
| 2040 | FromPointee = FromPointer->getPointeeType(); |
| 2041 | else |
| 2042 | return false; |
| 2043 | |
| 2044 | Qualifiers FromQuals = FromPointee.getQualifiers(); |
| 2045 | if (!FromPointee->isObjCLifetimeType() || |
| 2046 | (FromQuals.getObjCLifetime() != Qualifiers::OCL_Strong && |
| 2047 | FromQuals.getObjCLifetime() != Qualifiers::OCL_Weak)) |
| 2048 | return false; |
| 2049 | |
| 2050 | // Make sure that we have compatible qualifiers. |
| 2051 | FromQuals.setObjCLifetime(Qualifiers::OCL_Autoreleasing); |
| 2052 | if (!ToQuals.compatiblyIncludes(FromQuals)) |
| 2053 | return false; |
| 2054 | |
| 2055 | // Remove qualifiers from the pointee type we're converting from; they |
| 2056 | // aren't used in the compatibility check belong, and we'll be adding back |
| 2057 | // qualifiers (with __autoreleasing) if the compatibility check succeeds. |
| 2058 | FromPointee = FromPointee.getUnqualifiedType(); |
| 2059 | |
| 2060 | // The unqualified form of the pointee types must be compatible. |
| 2061 | ToPointee = ToPointee.getUnqualifiedType(); |
| 2062 | bool IncompatibleObjC; |
| 2063 | if (Context.typesAreCompatible(FromPointee, ToPointee)) |
| 2064 | FromPointee = ToPointee; |
| 2065 | else if (!isObjCPointerConversion(FromPointee, ToPointee, FromPointee, |
| 2066 | IncompatibleObjC)) |
| 2067 | return false; |
| 2068 | |
| 2069 | /// \brief Construct the type we're converting to, which is a pointer to |
| 2070 | /// __autoreleasing pointee. |
| 2071 | FromPointee = Context.getQualifiedType(FromPointee, FromQuals); |
| 2072 | ConvertedType = Context.getPointerType(FromPointee); |
| 2073 | return true; |
| 2074 | } |
| 2075 | |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2076 | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
| 2077 | QualType& ConvertedType) { |
| 2078 | QualType ToPointeeType; |
| 2079 | if (const BlockPointerType *ToBlockPtr = |
| 2080 | ToType->getAs<BlockPointerType>()) |
| 2081 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 2082 | else |
| 2083 | return false; |
| 2084 | |
| 2085 | QualType FromPointeeType; |
| 2086 | if (const BlockPointerType *FromBlockPtr = |
| 2087 | FromType->getAs<BlockPointerType>()) |
| 2088 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2089 | else |
| 2090 | return false; |
| 2091 | // We have pointer to blocks, check whether the only |
| 2092 | // differences in the argument and result types are in Objective-C |
| 2093 | // pointer conversions. If so, we permit the conversion. |
| 2094 | |
| 2095 | const FunctionProtoType *FromFunctionType |
| 2096 | = FromPointeeType->getAs<FunctionProtoType>(); |
| 2097 | const FunctionProtoType *ToFunctionType |
| 2098 | = ToPointeeType->getAs<FunctionProtoType>(); |
| 2099 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2100 | if (!FromFunctionType || !ToFunctionType) |
| 2101 | return false; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2102 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2103 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2104 | return true; |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2105 | |
| 2106 | // Perform the quick checks that will tell us whether these |
| 2107 | // function types are obviously different. |
| 2108 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2109 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
| 2110 | return false; |
| 2111 | |
| 2112 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
| 2113 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
| 2114 | if (FromEInfo != ToEInfo) |
| 2115 | return false; |
| 2116 | |
| 2117 | bool IncompatibleObjC = false; |
Fariborz Jahanian | 462dae5 | 2011-02-13 20:11:42 +0000 | [diff] [blame] | 2118 | if (Context.hasSameType(FromFunctionType->getResultType(), |
| 2119 | ToFunctionType->getResultType())) { |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2120 | // Okay, the types match exactly. Nothing to do. |
| 2121 | } else { |
| 2122 | QualType RHS = FromFunctionType->getResultType(); |
| 2123 | QualType LHS = ToFunctionType->getResultType(); |
| 2124 | if ((!getLangOptions().CPlusPlus || !RHS->isRecordType()) && |
| 2125 | !RHS.hasQualifiers() && LHS.hasQualifiers()) |
| 2126 | LHS = LHS.getUnqualifiedType(); |
| 2127 | |
| 2128 | if (Context.hasSameType(RHS,LHS)) { |
| 2129 | // OK exact match. |
| 2130 | } else if (isObjCPointerConversion(RHS, LHS, |
| 2131 | ConvertedType, IncompatibleObjC)) { |
| 2132 | if (IncompatibleObjC) |
| 2133 | return false; |
| 2134 | // Okay, we have an Objective-C pointer conversion. |
| 2135 | } |
| 2136 | else |
| 2137 | return false; |
| 2138 | } |
| 2139 | |
| 2140 | // Check argument types. |
| 2141 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2142 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2143 | IncompatibleObjC = false; |
| 2144 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2145 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2146 | if (Context.hasSameType(FromArgType, ToArgType)) { |
| 2147 | // Okay, the types match exactly. Nothing to do. |
| 2148 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
| 2149 | ConvertedType, IncompatibleObjC)) { |
| 2150 | if (IncompatibleObjC) |
| 2151 | return false; |
| 2152 | // Okay, we have an Objective-C pointer conversion. |
| 2153 | } else |
| 2154 | // Argument types are too different. Abort. |
| 2155 | return false; |
| 2156 | } |
Fariborz Jahanian | 78213e4 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 2157 | if (LangOpts.ObjCAutoRefCount && |
| 2158 | !Context.FunctionTypesMatchOnNSConsumedAttrs(FromFunctionType, |
| 2159 | ToFunctionType)) |
| 2160 | return false; |
Fariborz Jahanian | f9d9527 | 2011-09-28 20:22:05 +0000 | [diff] [blame] | 2161 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2162 | ConvertedType = ToType; |
| 2163 | return true; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2166 | enum { |
| 2167 | ft_default, |
| 2168 | ft_different_class, |
| 2169 | ft_parameter_arity, |
| 2170 | ft_parameter_mismatch, |
| 2171 | ft_return_type, |
| 2172 | ft_qualifer_mismatch |
| 2173 | }; |
| 2174 | |
| 2175 | /// HandleFunctionTypeMismatch - Gives diagnostic information for differeing |
| 2176 | /// function types. Catches different number of parameter, mismatch in |
| 2177 | /// parameter types, and different return types. |
| 2178 | void Sema::HandleFunctionTypeMismatch(PartialDiagnostic &PDiag, |
| 2179 | QualType FromType, QualType ToType) { |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2180 | // If either type is not valid, include no extra info. |
| 2181 | if (FromType.isNull() || ToType.isNull()) { |
| 2182 | PDiag << ft_default; |
| 2183 | return; |
| 2184 | } |
| 2185 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2186 | // Get the function type from the pointers. |
| 2187 | if (FromType->isMemberPointerType() && ToType->isMemberPointerType()) { |
| 2188 | const MemberPointerType *FromMember = FromType->getAs<MemberPointerType>(), |
| 2189 | *ToMember = ToType->getAs<MemberPointerType>(); |
| 2190 | if (FromMember->getClass() != ToMember->getClass()) { |
| 2191 | PDiag << ft_different_class << QualType(ToMember->getClass(), 0) |
| 2192 | << QualType(FromMember->getClass(), 0); |
| 2193 | return; |
| 2194 | } |
| 2195 | FromType = FromMember->getPointeeType(); |
| 2196 | ToType = ToMember->getPointeeType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2199 | if (FromType->isPointerType()) |
| 2200 | FromType = FromType->getPointeeType(); |
| 2201 | if (ToType->isPointerType()) |
| 2202 | ToType = ToType->getPointeeType(); |
| 2203 | |
| 2204 | // Remove references. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2205 | FromType = FromType.getNonReferenceType(); |
| 2206 | ToType = ToType.getNonReferenceType(); |
| 2207 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2208 | // Don't print extra info for non-specialized template functions. |
| 2209 | if (FromType->isInstantiationDependentType() && |
| 2210 | !FromType->getAs<TemplateSpecializationType>()) { |
| 2211 | PDiag << ft_default; |
| 2212 | return; |
| 2213 | } |
| 2214 | |
Richard Trieu | a6dc7ef | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 2215 | // No extra info for same types. |
| 2216 | if (Context.hasSameType(FromType, ToType)) { |
| 2217 | PDiag << ft_default; |
| 2218 | return; |
| 2219 | } |
| 2220 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2221 | const FunctionProtoType *FromFunction = FromType->getAs<FunctionProtoType>(), |
| 2222 | *ToFunction = ToType->getAs<FunctionProtoType>(); |
| 2223 | |
| 2224 | // Both types need to be function types. |
| 2225 | if (!FromFunction || !ToFunction) { |
| 2226 | PDiag << ft_default; |
| 2227 | return; |
| 2228 | } |
| 2229 | |
| 2230 | if (FromFunction->getNumArgs() != ToFunction->getNumArgs()) { |
| 2231 | PDiag << ft_parameter_arity << ToFunction->getNumArgs() |
| 2232 | << FromFunction->getNumArgs(); |
| 2233 | return; |
| 2234 | } |
| 2235 | |
| 2236 | // Handle different parameter types. |
| 2237 | unsigned ArgPos; |
| 2238 | if (!FunctionArgTypesAreEqual(FromFunction, ToFunction, &ArgPos)) { |
| 2239 | PDiag << ft_parameter_mismatch << ArgPos + 1 |
| 2240 | << ToFunction->getArgType(ArgPos) |
| 2241 | << FromFunction->getArgType(ArgPos); |
| 2242 | return; |
| 2243 | } |
| 2244 | |
| 2245 | // Handle different return type. |
| 2246 | if (!Context.hasSameType(FromFunction->getResultType(), |
| 2247 | ToFunction->getResultType())) { |
| 2248 | PDiag << ft_return_type << ToFunction->getResultType() |
| 2249 | << FromFunction->getResultType(); |
| 2250 | return; |
| 2251 | } |
| 2252 | |
| 2253 | unsigned FromQuals = FromFunction->getTypeQuals(), |
| 2254 | ToQuals = ToFunction->getTypeQuals(); |
| 2255 | if (FromQuals != ToQuals) { |
| 2256 | PDiag << ft_qualifer_mismatch << ToQuals << FromQuals; |
| 2257 | return; |
| 2258 | } |
| 2259 | |
| 2260 | // Unable to find a difference, so add no extra info. |
| 2261 | PDiag << ft_default; |
| 2262 | } |
| 2263 | |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2264 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
Douglas Gregor | dec1cc4 | 2011-12-15 17:15:07 +0000 | [diff] [blame] | 2265 | /// for equality of their argument types. Caller has already checked that |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2266 | /// they have same number of arguments. This routine assumes that Objective-C |
| 2267 | /// pointer types which only differ in their protocol qualifiers are equal. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2268 | /// If the parameters are different, ArgPos will have the the parameter index |
| 2269 | /// of the first different parameter. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2270 | bool Sema::FunctionArgTypesAreEqual(const FunctionProtoType *OldType, |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2271 | const FunctionProtoType *NewType, |
| 2272 | unsigned *ArgPos) { |
| 2273 | if (!getLangOptions().ObjC1) { |
| 2274 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 2275 | N = NewType->arg_type_begin(), |
| 2276 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
| 2277 | if (!Context.hasSameType(*O, *N)) { |
| 2278 | if (ArgPos) *ArgPos = O - OldType->arg_type_begin(); |
| 2279 | return false; |
| 2280 | } |
| 2281 | } |
| 2282 | return true; |
| 2283 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2284 | |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2285 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 2286 | N = NewType->arg_type_begin(), |
| 2287 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
| 2288 | QualType ToType = (*O); |
| 2289 | QualType FromType = (*N); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2290 | if (!Context.hasSameType(ToType, FromType)) { |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2291 | if (const PointerType *PTTo = ToType->getAs<PointerType>()) { |
| 2292 | if (const PointerType *PTFr = FromType->getAs<PointerType>()) |
Chandler Carruth | 0ee93de | 2010-05-06 00:15:06 +0000 | [diff] [blame] | 2293 | if ((PTTo->getPointeeType()->isObjCQualifiedIdType() && |
| 2294 | PTFr->getPointeeType()->isObjCQualifiedIdType()) || |
| 2295 | (PTTo->getPointeeType()->isObjCQualifiedClassType() && |
| 2296 | PTFr->getPointeeType()->isObjCQualifiedClassType())) |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2297 | continue; |
| 2298 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2299 | else if (const ObjCObjectPointerType *PTTo = |
| 2300 | ToType->getAs<ObjCObjectPointerType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2301 | if (const ObjCObjectPointerType *PTFr = |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2302 | FromType->getAs<ObjCObjectPointerType>()) |
Douglas Gregor | dec1cc4 | 2011-12-15 17:15:07 +0000 | [diff] [blame] | 2303 | if (Context.hasSameUnqualifiedType( |
| 2304 | PTTo->getObjectType()->getBaseType(), |
| 2305 | PTFr->getObjectType()->getBaseType())) |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2306 | continue; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2307 | } |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 2308 | if (ArgPos) *ArgPos = O - OldType->arg_type_begin(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2309 | return false; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2310 | } |
| 2311 | } |
| 2312 | return true; |
| 2313 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2314 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2315 | /// CheckPointerConversion - Check the pointer conversion from the |
| 2316 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2317 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2318 | /// conversions for which IsPointerConversion has already returned |
| 2319 | /// true. It returns true and produces a diagnostic if there was an |
| 2320 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2321 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2322 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2323 | CXXCastPath& BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2324 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2325 | QualType FromType = From->getType(); |
Argyrios Kyrtzidis | b335872 | 2010-09-28 14:54:11 +0000 | [diff] [blame] | 2326 | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2327 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2328 | Kind = CK_BitCast; |
| 2329 | |
Chandler Carruth | 88f0aed | 2011-04-09 07:32:05 +0000 | [diff] [blame] | 2330 | if (!IsCStyleOrFunctionalCast && |
| 2331 | Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy) && |
| 2332 | From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 2333 | DiagRuntimeBehavior(From->getExprLoc(), From, |
Chandler Carruth | b600669 | 2011-04-09 07:48:17 +0000 | [diff] [blame] | 2334 | PDiag(diag::warn_impcast_bool_to_null_pointer) |
| 2335 | << ToType << From->getSourceRange()); |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 2336 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2337 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
| 2338 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2339 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 2340 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 2341 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2342 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 2343 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2344 | // We must have a derived-to-base conversion. Check an |
| 2345 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2346 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 2347 | From->getExprLoc(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2348 | From->getSourceRange(), &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2349 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2350 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2351 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2352 | // The conversion was successful. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2353 | Kind = CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2354 | } |
| 2355 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2356 | } else if (const ObjCObjectPointerType *ToPtrType = |
| 2357 | ToType->getAs<ObjCObjectPointerType>()) { |
| 2358 | if (const ObjCObjectPointerType *FromPtrType = |
| 2359 | FromType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2360 | // Objective-C++ conversions are always okay. |
| 2361 | // FIXME: We should have a different class of conversions for the |
| 2362 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2363 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2364 | return false; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2365 | } else if (FromType->isBlockPointerType()) { |
| 2366 | Kind = CK_BlockPointerToObjCPointerCast; |
| 2367 | } else { |
| 2368 | Kind = CK_CPointerToObjCPointerCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2369 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2370 | } else if (ToType->isBlockPointerType()) { |
| 2371 | if (!FromType->isBlockPointerType()) |
| 2372 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2373 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2374 | |
| 2375 | // We shouldn't fall into this case unless it's valid for other |
| 2376 | // reasons. |
| 2377 | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 2378 | Kind = CK_NullToPointer; |
| 2379 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2380 | return false; |
| 2381 | } |
| 2382 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2383 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 2384 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 2385 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 2386 | /// If so, returns true and places the converted type (that might differ from |
| 2387 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 2388 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2389 | QualType ToType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2390 | bool InOverloadResolution, |
| 2391 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2392 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2393 | if (!ToTypePtr) |
| 2394 | return false; |
| 2395 | |
| 2396 | // 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] | 2397 | if (From->isNullPointerConstant(Context, |
| 2398 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 2399 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2400 | ConvertedType = ToType; |
| 2401 | return true; |
| 2402 | } |
| 2403 | |
| 2404 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2405 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2406 | if (!FromTypePtr) |
| 2407 | return false; |
| 2408 | |
| 2409 | // A pointer to member of B can be converted to a pointer to member of D, |
| 2410 | // where D is derived from B (C++ 4.11p2). |
| 2411 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 2412 | QualType ToClass(ToTypePtr->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2413 | |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2414 | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
| 2415 | !RequireCompleteType(From->getLocStart(), ToClass, PDiag()) && |
| 2416 | IsDerivedFrom(ToClass, FromClass)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2417 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 2418 | ToClass.getTypePtr()); |
| 2419 | return true; |
| 2420 | } |
| 2421 | |
| 2422 | return false; |
| 2423 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2424 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2425 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 2426 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2427 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2428 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 2429 | /// true and produces a diagnostic if there was an error, or returns false |
| 2430 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2431 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2432 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2433 | CXXCastPath &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2434 | bool IgnoreBaseAccess) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2435 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2436 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2437 | if (!FromPtrType) { |
| 2438 | // This must be a null pointer to member pointer conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2439 | assert(From->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2440 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2441 | "Expr must be null pointer constant!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2442 | Kind = CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2443 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2444 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2445 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2446 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2447 | assert(ToPtrType && "No member pointer cast has a target type " |
| 2448 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2449 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2450 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 2451 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2452 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2453 | // FIXME: What about dependent types? |
| 2454 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 2455 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2456 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2457 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 2458 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2459 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 2460 | assert(DerivationOkay && |
| 2461 | "Should not have been called if derivation isn't OK."); |
| 2462 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2463 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2464 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 2465 | getUnqualifiedType())) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2466 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 2467 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 2468 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 2469 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2470 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2471 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2472 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2473 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 2474 | << FromClass << ToClass << QualType(VBase, 0) |
| 2475 | << From->getSourceRange(); |
| 2476 | return true; |
| 2477 | } |
| 2478 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2479 | if (!IgnoreBaseAccess) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2480 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 2481 | Paths.front(), |
| 2482 | diag::err_downcast_from_inaccessible_base); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2483 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2484 | // Must be a base to derived member conversion. |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2485 | BuildBasePathArray(Paths, BasePath); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2486 | Kind = CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2487 | return false; |
| 2488 | } |
| 2489 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2490 | /// IsQualificationConversion - Determines whether the conversion from |
| 2491 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 2492 | /// (C++ 4.4). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2493 | /// |
| 2494 | /// \param ObjCLifetimeConversion Output parameter that will be set to indicate |
| 2495 | /// when the qualification conversion involves a change in the Objective-C |
| 2496 | /// object lifetime. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2497 | bool |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2498 | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2499 | bool CStyle, bool &ObjCLifetimeConversion) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2500 | FromType = Context.getCanonicalType(FromType); |
| 2501 | ToType = Context.getCanonicalType(ToType); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2502 | ObjCLifetimeConversion = false; |
| 2503 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2504 | // If FromType and ToType are the same type, this is not a |
| 2505 | // qualification conversion. |
Sebastian Redl | 22c9240 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 2506 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2507 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2508 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2509 | // (C++ 4.4p4): |
| 2510 | // A conversion can add cv-qualifiers at levels other than the first |
| 2511 | // in multi-level pointers, subject to the following rules: [...] |
| 2512 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2513 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2514 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2515 | // Within each iteration of the loop, we check the qualifiers to |
| 2516 | // determine if this still looks like a qualification |
| 2517 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2518 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2519 | // until there are no more pointers or pointers-to-members left to |
| 2520 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2521 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2522 | |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2523 | Qualifiers FromQuals = FromType.getQualifiers(); |
| 2524 | Qualifiers ToQuals = ToType.getQualifiers(); |
| 2525 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2526 | // Objective-C ARC: |
| 2527 | // Check Objective-C lifetime conversions. |
| 2528 | if (FromQuals.getObjCLifetime() != ToQuals.getObjCLifetime() && |
| 2529 | UnwrappedAnyPointer) { |
| 2530 | if (ToQuals.compatiblyIncludesObjCLifetime(FromQuals)) { |
| 2531 | ObjCLifetimeConversion = true; |
| 2532 | FromQuals.removeObjCLifetime(); |
| 2533 | ToQuals.removeObjCLifetime(); |
| 2534 | } else { |
| 2535 | // Qualification conversions cannot cast between different |
| 2536 | // Objective-C lifetime qualifiers. |
| 2537 | return false; |
| 2538 | } |
| 2539 | } |
| 2540 | |
Douglas Gregor | 377e1bd | 2011-05-08 06:09:53 +0000 | [diff] [blame] | 2541 | // Allow addition/removal of GC attributes but not changing GC attributes. |
| 2542 | if (FromQuals.getObjCGCAttr() != ToQuals.getObjCGCAttr() && |
| 2543 | (!FromQuals.hasObjCGCAttr() || !ToQuals.hasObjCGCAttr())) { |
| 2544 | FromQuals.removeObjCGCAttr(); |
| 2545 | ToQuals.removeObjCGCAttr(); |
| 2546 | } |
| 2547 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2548 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 2549 | // 2,j, and similarly for volatile. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2550 | if (!CStyle && !ToQuals.compatiblyIncludes(FromQuals)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2551 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2552 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2553 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 2554 | // every cv for 0 < k < j. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2555 | if (!CStyle && FromQuals.getCVRQualifiers() != ToQuals.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2556 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2557 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2558 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2559 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 2560 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2561 | PreviousToQualsIncludeConst |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2562 | = PreviousToQualsIncludeConst && ToQuals.hasConst(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2563 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2564 | |
| 2565 | // We are left with FromType and ToType being the pointee types |
| 2566 | // after unwrapping the original FromType and ToType the same number |
| 2567 | // of types. If we unwrapped any pointers, and if FromType and |
| 2568 | // ToType have the same unqualified type (since we checked |
| 2569 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2570 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2573 | /// Determines whether there is a user-defined conversion sequence |
| 2574 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 2575 | /// ToType. If such a conversion exists, User will contain the |
| 2576 | /// user-defined conversion sequence that performs such a conversion |
| 2577 | /// and this routine will return true. Otherwise, this routine returns |
| 2578 | /// false and User is unspecified. |
| 2579 | /// |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2580 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 2581 | /// "explicit" conversion functions as well as non-explicit conversion |
| 2582 | /// functions (C++0x [class.conv.fct]p2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2583 | static OverloadingResult |
| 2584 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 2585 | UserDefinedConversionSequence& User, |
| 2586 | OverloadCandidateSet& CandidateSet, |
| 2587 | bool AllowExplicit) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2588 | // Whether we will only visit constructors. |
| 2589 | bool ConstructorsOnly = false; |
| 2590 | |
| 2591 | // If the type we are conversion to is a class type, enumerate its |
| 2592 | // constructors. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2593 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2594 | // C++ [over.match.ctor]p1: |
| 2595 | // When objects of class type are direct-initialized (8.5), or |
| 2596 | // copy-initialized from an expression of the same or a |
| 2597 | // derived class type (8.5), overload resolution selects the |
| 2598 | // constructor. [...] For copy-initialization, the candidate |
| 2599 | // functions are all the converting constructors (12.3.1) of |
| 2600 | // that class. The argument list is the expression-list within |
| 2601 | // the parentheses of the initializer. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2602 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2603 | (From->getType()->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2604 | S.IsDerivedFrom(From->getType(), ToType))) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2605 | ConstructorsOnly = true; |
| 2606 | |
Argyrios Kyrtzidis | e36bca6 | 2011-04-22 17:45:37 +0000 | [diff] [blame] | 2607 | S.RequireCompleteType(From->getLocStart(), ToType, S.PDiag()); |
| 2608 | // RequireCompleteType may have returned true due to some invalid decl |
| 2609 | // during template instantiation, but ToType may be complete enough now |
| 2610 | // to try to recover. |
| 2611 | if (ToType->isIncompleteType()) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 2612 | // We're not going to find any constructors. |
| 2613 | } else if (CXXRecordDecl *ToRecordDecl |
| 2614 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2615 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2616 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(ToRecordDecl); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2617 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2618 | NamedDecl *D = *Con; |
| 2619 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2620 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2621 | // Find the constructor (which may be a template). |
| 2622 | CXXConstructorDecl *Constructor = 0; |
| 2623 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2624 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2625 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2626 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2627 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 2628 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2629 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2630 | |
Fariborz Jahanian | 52ab92b | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 2631 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 2632 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2633 | if (ConstructorTmpl) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2634 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2635 | /*ExplicitArgs*/ 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2636 | &From, 1, CandidateSet, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2637 | /*SuppressUserConversions=*/ |
| 2638 | !ConstructorsOnly); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2639 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 2640 | // Allow one user-defined conversion when user specifies a |
| 2641 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2642 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 2643 | &From, 1, CandidateSet, |
| 2644 | /*SuppressUserConversions=*/ |
| 2645 | !ConstructorsOnly); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2646 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2647 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2648 | } |
| 2649 | } |
| 2650 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2651 | // Enumerate conversion functions, if we're allowed to. |
| 2652 | if (ConstructorsOnly) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2653 | } else if (S.RequireCompleteType(From->getLocStart(), From->getType(), |
| 2654 | S.PDiag(0) << From->getSourceRange())) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2655 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2656 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2657 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2658 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2659 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 2660 | // Add all of the conversion functions as candidates. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2661 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 2662 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2663 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2664 | E = Conversions->end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2665 | DeclAccessPair FoundDecl = I.getPair(); |
| 2666 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2667 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2668 | if (isa<UsingShadowDecl>(D)) |
| 2669 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2670 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2671 | CXXConversionDecl *Conv; |
| 2672 | FunctionTemplateDecl *ConvTemplate; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2673 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 2674 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2675 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2676 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2677 | |
| 2678 | if (AllowExplicit || !Conv->isExplicit()) { |
| 2679 | if (ConvTemplate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2680 | S.AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
| 2681 | ActingContext, From, ToType, |
| 2682 | CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2683 | else |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2684 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, |
| 2685 | From, ToType, CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2686 | } |
| 2687 | } |
| 2688 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2689 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2690 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2691 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 2692 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2693 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 2694 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2695 | case OR_Success: |
| 2696 | // Record the standard conversion we used and the conversion function. |
| 2697 | if (CXXConstructorDecl *Constructor |
| 2698 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2699 | S.MarkDeclarationReferenced(From->getLocStart(), Constructor); |
| 2700 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2701 | // C++ [over.ics.user]p1: |
| 2702 | // If the user-defined conversion is specified by a |
| 2703 | // constructor (12.3.1), the initial standard conversion |
| 2704 | // sequence converts the source type to the type required by |
| 2705 | // the argument of the constructor. |
| 2706 | // |
| 2707 | QualType ThisType = Constructor->getThisType(S.Context); |
| 2708 | if (Best->Conversions[0].isEllipsis()) |
| 2709 | User.EllipsisConversion = true; |
| 2710 | else { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2711 | User.Before = Best->Conversions[0].Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2712 | User.EllipsisConversion = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2713 | } |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2714 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2715 | User.ConversionFunction = Constructor; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 2716 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2717 | User.After.setAsIdentityConversion(); |
| 2718 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 2719 | User.After.setAllToTypes(ToType); |
| 2720 | return OR_Success; |
| 2721 | } else if (CXXConversionDecl *Conversion |
| 2722 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2723 | S.MarkDeclarationReferenced(From->getLocStart(), Conversion); |
| 2724 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2725 | // C++ [over.ics.user]p1: |
| 2726 | // |
| 2727 | // [...] If the user-defined conversion is specified by a |
| 2728 | // conversion function (12.3.2), the initial standard |
| 2729 | // conversion sequence converts the source type to the |
| 2730 | // implicit object parameter of the conversion function. |
| 2731 | User.Before = Best->Conversions[0].Standard; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2732 | User.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2733 | User.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 2734 | User.FoundConversionFunction = Best->FoundDecl; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2735 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2736 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2737 | // C++ [over.ics.user]p2: |
| 2738 | // The second standard conversion sequence converts the |
| 2739 | // result of the user-defined conversion to the target type |
| 2740 | // for the sequence. Since an implicit conversion sequence |
| 2741 | // is an initialization, the special rules for |
| 2742 | // initialization by user-defined conversion apply when |
| 2743 | // selecting the best user-defined conversion for a |
| 2744 | // user-defined conversion sequence (see 13.3.3 and |
| 2745 | // 13.3.3.1). |
| 2746 | User.After = Best->FinalConversion; |
| 2747 | return OR_Success; |
| 2748 | } else { |
| 2749 | llvm_unreachable("Not a constructor or conversion function?"); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2750 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2753 | case OR_No_Viable_Function: |
| 2754 | return OR_No_Viable_Function; |
| 2755 | case OR_Deleted: |
| 2756 | // No conversion here! We're done. |
| 2757 | return OR_Deleted; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2758 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2759 | case OR_Ambiguous: |
| 2760 | return OR_Ambiguous; |
| 2761 | } |
| 2762 | |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2763 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2764 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2765 | |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2766 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2767 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2768 | ImplicitConversionSequence ICS; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2769 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2770 | OverloadingResult OvResult = |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2771 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2772 | CandidateSet, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2773 | if (OvResult == OR_Ambiguous) |
| 2774 | Diag(From->getSourceRange().getBegin(), |
| 2775 | diag::err_typecheck_ambiguous_condition) |
| 2776 | << From->getType() << ToType << From->getSourceRange(); |
| 2777 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 2778 | Diag(From->getSourceRange().getBegin(), |
| 2779 | diag::err_typecheck_nonviable_condition) |
| 2780 | << From->getType() << ToType << From->getSourceRange(); |
| 2781 | else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2782 | return false; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2783 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &From, 1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2784 | return true; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2785 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2786 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2787 | /// CompareImplicitConversionSequences - Compare two implicit |
| 2788 | /// conversion sequences to determine whether one is better than the |
| 2789 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2790 | static ImplicitConversionSequence::CompareKind |
| 2791 | CompareImplicitConversionSequences(Sema &S, |
| 2792 | const ImplicitConversionSequence& ICS1, |
| 2793 | const ImplicitConversionSequence& ICS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2794 | { |
| 2795 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 2796 | // conversion sequences (as defined in 13.3.3.1) |
| 2797 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 2798 | // conversion sequence than a user-defined conversion sequence or |
| 2799 | // an ellipsis conversion sequence, and |
| 2800 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 2801 | // conversion sequence than an ellipsis conversion sequence |
| 2802 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2803 | // |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2804 | // C++0x [over.best.ics]p10: |
| 2805 | // For the purpose of ranking implicit conversion sequences as |
| 2806 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 2807 | // treated as a user-defined sequence that is indistinguishable |
| 2808 | // from any other user-defined conversion sequence. |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2809 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 2810 | return ImplicitConversionSequence::Better; |
| 2811 | else if (ICS2.getKindRank() < ICS1.getKindRank()) |
| 2812 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2813 | |
Benjamin Kramer | b6eee07 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 2814 | // The following checks require both conversion sequences to be of |
| 2815 | // the same kind. |
| 2816 | if (ICS1.getKind() != ICS2.getKind()) |
| 2817 | return ImplicitConversionSequence::Indistinguishable; |
| 2818 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 2819 | ImplicitConversionSequence::CompareKind Result = |
| 2820 | ImplicitConversionSequence::Indistinguishable; |
| 2821 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2822 | // Two implicit conversion sequences of the same form are |
| 2823 | // indistinguishable conversion sequences unless one of the |
| 2824 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2825 | if (ICS1.isStandard()) |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 2826 | Result = CompareStandardConversionSequences(S, |
| 2827 | ICS1.Standard, ICS2.Standard); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2828 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2829 | // User-defined conversion sequence U1 is a better conversion |
| 2830 | // sequence than another user-defined conversion sequence U2 if |
| 2831 | // they contain the same user-defined conversion function or |
| 2832 | // constructor and if the second standard conversion sequence of |
| 2833 | // U1 is better than the second standard conversion sequence of |
| 2834 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2835 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2836 | ICS2.UserDefined.ConversionFunction) |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 2837 | Result = CompareStandardConversionSequences(S, |
| 2838 | ICS1.UserDefined.After, |
| 2839 | ICS2.UserDefined.After); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2840 | } |
| 2841 | |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 2842 | // List-initialization sequence L1 is a better conversion sequence than |
| 2843 | // list-initialization sequence L2 if L1 converts to std::initializer_list<X> |
| 2844 | // for some X and L2 does not. |
| 2845 | if (Result == ImplicitConversionSequence::Indistinguishable && |
| 2846 | ICS1.isListInitializationSequence() && |
| 2847 | ICS2.isListInitializationSequence()) { |
| 2848 | // FIXME: Find out if ICS1 converts to initializer_list and ICS2 doesn't. |
| 2849 | } |
| 2850 | |
| 2851 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2852 | } |
| 2853 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2854 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 2855 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 2856 | Qualifiers Quals; |
| 2857 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2858 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2859 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2860 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2861 | return Context.hasSameUnqualifiedType(T1, T2); |
| 2862 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2863 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2864 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 2865 | // determine if one is a proper subset of the other. |
| 2866 | static ImplicitConversionSequence::CompareKind |
| 2867 | compareStandardConversionSubsets(ASTContext &Context, |
| 2868 | const StandardConversionSequence& SCS1, |
| 2869 | const StandardConversionSequence& SCS2) { |
| 2870 | ImplicitConversionSequence::CompareKind Result |
| 2871 | = ImplicitConversionSequence::Indistinguishable; |
| 2872 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2873 | // the identity conversion sequence is considered to be a subsequence of |
Douglas Gregor | ae65f4b | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 2874 | // any non-identity conversion sequence |
Douglas Gregor | 4ae5b72 | 2011-06-05 06:15:20 +0000 | [diff] [blame] | 2875 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 2876 | return ImplicitConversionSequence::Better; |
| 2877 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 2878 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2879 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2880 | if (SCS1.Second != SCS2.Second) { |
| 2881 | if (SCS1.Second == ICK_Identity) |
| 2882 | Result = ImplicitConversionSequence::Better; |
| 2883 | else if (SCS2.Second == ICK_Identity) |
| 2884 | Result = ImplicitConversionSequence::Worse; |
| 2885 | else |
| 2886 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2887 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2888 | return ImplicitConversionSequence::Indistinguishable; |
| 2889 | |
| 2890 | if (SCS1.Third == SCS2.Third) { |
| 2891 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 2892 | : ImplicitConversionSequence::Indistinguishable; |
| 2893 | } |
| 2894 | |
| 2895 | if (SCS1.Third == ICK_Identity) |
| 2896 | return Result == ImplicitConversionSequence::Worse |
| 2897 | ? ImplicitConversionSequence::Indistinguishable |
| 2898 | : ImplicitConversionSequence::Better; |
| 2899 | |
| 2900 | if (SCS2.Third == ICK_Identity) |
| 2901 | return Result == ImplicitConversionSequence::Better |
| 2902 | ? ImplicitConversionSequence::Indistinguishable |
| 2903 | : ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2904 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2905 | return ImplicitConversionSequence::Indistinguishable; |
| 2906 | } |
| 2907 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2908 | /// \brief Determine whether one of the given reference bindings is better |
| 2909 | /// than the other based on what kind of bindings they are. |
| 2910 | static bool isBetterReferenceBindingKind(const StandardConversionSequence &SCS1, |
| 2911 | const StandardConversionSequence &SCS2) { |
| 2912 | // C++0x [over.ics.rank]p3b4: |
| 2913 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 2914 | // implicit object parameter of a non-static member function declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2915 | // without a ref-qualifier, and *either* S1 binds an rvalue reference |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2916 | // 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] | 2917 | // lvalue reference to a function lvalue and S2 binds an rvalue |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2918 | // reference*. |
| 2919 | // |
| 2920 | // FIXME: Rvalue references. We're going rogue with the above edits, |
| 2921 | // because the semantics in the current C++0x working paper (N3225 at the |
| 2922 | // time of this writing) break the standard definition of std::forward |
| 2923 | // and std::reference_wrapper when dealing with references to functions. |
| 2924 | // Proposed wording changes submitted to CWG for consideration. |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 2925 | if (SCS1.BindsImplicitObjectArgumentWithoutRefQualifier || |
| 2926 | SCS2.BindsImplicitObjectArgumentWithoutRefQualifier) |
| 2927 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2928 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2929 | return (!SCS1.IsLvalueReference && SCS1.BindsToRvalue && |
| 2930 | SCS2.IsLvalueReference) || |
| 2931 | (SCS1.IsLvalueReference && SCS1.BindsToFunctionLvalue && |
| 2932 | !SCS2.IsLvalueReference); |
| 2933 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2934 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2935 | /// CompareStandardConversionSequences - Compare two standard |
| 2936 | /// conversion sequences to determine whether one is better than the |
| 2937 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2938 | static ImplicitConversionSequence::CompareKind |
| 2939 | CompareStandardConversionSequences(Sema &S, |
| 2940 | const StandardConversionSequence& SCS1, |
| 2941 | const StandardConversionSequence& SCS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2942 | { |
| 2943 | // Standard conversion sequence S1 is a better conversion sequence |
| 2944 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 2945 | |
| 2946 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 2947 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 2948 | // excluding any Lvalue Transformation; the identity conversion |
| 2949 | // sequence is considered to be a subsequence of any |
| 2950 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2951 | if (ImplicitConversionSequence::CompareKind CK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2952 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2953 | return CK; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2954 | |
| 2955 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 2956 | // defined below), or, if not that, |
| 2957 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 2958 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 2959 | if (Rank1 < Rank2) |
| 2960 | return ImplicitConversionSequence::Better; |
| 2961 | else if (Rank2 < Rank1) |
| 2962 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2963 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2964 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 2965 | // are indistinguishable unless one of the following rules |
| 2966 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2967 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2968 | // A conversion that is not a conversion of a pointer, or |
| 2969 | // pointer to member, to bool is better than another conversion |
| 2970 | // that is such a conversion. |
| 2971 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 2972 | return SCS2.isPointerConversionToBool() |
| 2973 | ? ImplicitConversionSequence::Better |
| 2974 | : ImplicitConversionSequence::Worse; |
| 2975 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2976 | // C++ [over.ics.rank]p4b2: |
| 2977 | // |
| 2978 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2979 | // conversion of B* to A* is better than conversion of B* to |
| 2980 | // void*, and conversion of A* to void* is better than conversion |
| 2981 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2982 | bool SCS1ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2983 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2984 | bool SCS2ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2985 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2986 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 2987 | // Exactly one of the conversion sequences is a conversion to |
| 2988 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2989 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 2990 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2991 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 2992 | // Neither conversion sequence converts to a void pointer; compare |
| 2993 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2994 | if (ImplicitConversionSequence::CompareKind DerivedCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2995 | = CompareDerivedToBaseConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2996 | return DerivedCK; |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 2997 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid && |
| 2998 | !S.Context.hasSameType(SCS1.getFromType(), SCS2.getFromType())) { |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2999 | // Both conversion sequences are conversions to void |
| 3000 | // pointers. Compare the source types to determine if there's an |
| 3001 | // inheritance relationship in their sources. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3002 | QualType FromType1 = SCS1.getFromType(); |
| 3003 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3004 | |
| 3005 | // Adjust the types we're converting from via the array-to-pointer |
| 3006 | // conversion, if we need to. |
| 3007 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3008 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3009 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3010 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3011 | |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3012 | QualType FromPointee1 = FromType1->getPointeeType().getUnqualifiedType(); |
| 3013 | QualType FromPointee2 = FromType2->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3014 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3015 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3016 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3017 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3018 | return ImplicitConversionSequence::Worse; |
| 3019 | |
| 3020 | // Objective-C++: If one interface is more specific than the |
| 3021 | // other, it is the better one. |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 3022 | const ObjCObjectPointerType* FromObjCPtr1 |
| 3023 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3024 | const ObjCObjectPointerType* FromObjCPtr2 |
| 3025 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3026 | if (FromObjCPtr1 && FromObjCPtr2) { |
| 3027 | bool AssignLeft = S.Context.canAssignObjCInterfaces(FromObjCPtr1, |
| 3028 | FromObjCPtr2); |
| 3029 | bool AssignRight = S.Context.canAssignObjCInterfaces(FromObjCPtr2, |
| 3030 | FromObjCPtr1); |
| 3031 | if (AssignLeft != AssignRight) { |
| 3032 | return AssignLeft? ImplicitConversionSequence::Better |
| 3033 | : ImplicitConversionSequence::Worse; |
| 3034 | } |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 3035 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3036 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3037 | |
| 3038 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 3039 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3040 | if (ImplicitConversionSequence::CompareKind QualCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3041 | = CompareQualificationConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3042 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3043 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3044 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3045 | // Check for a better reference binding based on the kind of bindings. |
| 3046 | if (isBetterReferenceBindingKind(SCS1, SCS2)) |
| 3047 | return ImplicitConversionSequence::Better; |
| 3048 | else if (isBetterReferenceBindingKind(SCS2, SCS1)) |
| 3049 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3050 | |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 3051 | // C++ [over.ics.rank]p3b4: |
| 3052 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 3053 | // which the references refer are the same type except for |
| 3054 | // top-level cv-qualifiers, and the type to which the reference |
| 3055 | // initialized by S2 refers is more cv-qualified than the type |
| 3056 | // to which the reference initialized by S1 refers. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3057 | QualType T1 = SCS1.getToType(2); |
| 3058 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3059 | T1 = S.Context.getCanonicalType(T1); |
| 3060 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3061 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3062 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3063 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3064 | if (UnqualT1 == UnqualT2) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3065 | // Objective-C++ ARC: If the references refer to objects with different |
| 3066 | // lifetimes, prefer bindings that don't change lifetime. |
| 3067 | if (SCS1.ObjCLifetimeConversionBinding != |
| 3068 | SCS2.ObjCLifetimeConversionBinding) { |
| 3069 | return SCS1.ObjCLifetimeConversionBinding |
| 3070 | ? ImplicitConversionSequence::Worse |
| 3071 | : ImplicitConversionSequence::Better; |
| 3072 | } |
| 3073 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3074 | // If the type is an array type, promote the element qualifiers to the |
| 3075 | // type for comparison. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3076 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3077 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3078 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3079 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3080 | if (T2.isMoreQualifiedThan(T1)) |
| 3081 | return ImplicitConversionSequence::Better; |
| 3082 | else if (T1.isMoreQualifiedThan(T2)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3083 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3084 | } |
| 3085 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3086 | |
Francois Pichet | 1c98d62 | 2011-09-18 21:37:37 +0000 | [diff] [blame] | 3087 | // In Microsoft mode, prefer an integral conversion to a |
| 3088 | // floating-to-integral conversion if the integral conversion |
| 3089 | // is between types of the same size. |
| 3090 | // For example: |
| 3091 | // void f(float); |
| 3092 | // void f(int); |
| 3093 | // int main { |
| 3094 | // long a; |
| 3095 | // f(a); |
| 3096 | // } |
| 3097 | // Here, MSVC will call f(int) instead of generating a compile error |
| 3098 | // as clang will do in standard mode. |
| 3099 | if (S.getLangOptions().MicrosoftMode && |
| 3100 | SCS1.Second == ICK_Integral_Conversion && |
| 3101 | SCS2.Second == ICK_Floating_Integral && |
| 3102 | S.Context.getTypeSize(SCS1.getFromType()) == |
| 3103 | S.Context.getTypeSize(SCS1.getToType(2))) |
| 3104 | return ImplicitConversionSequence::Better; |
| 3105 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3106 | return ImplicitConversionSequence::Indistinguishable; |
| 3107 | } |
| 3108 | |
| 3109 | /// CompareQualificationConversions - Compares two standard conversion |
| 3110 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3111 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 3112 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3113 | CompareQualificationConversions(Sema &S, |
| 3114 | const StandardConversionSequence& SCS1, |
| 3115 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 3116 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3117 | // -- S1 and S2 differ only in their qualification conversion and |
| 3118 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 3119 | // cv-qualification signature of type T1 is a proper subset of |
| 3120 | // the cv-qualification signature of type T2, and S1 is not the |
| 3121 | // deprecated string literal array-to-pointer conversion (4.2). |
| 3122 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 3123 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 3124 | return ImplicitConversionSequence::Indistinguishable; |
| 3125 | |
| 3126 | // FIXME: the example in the standard doesn't use a qualification |
| 3127 | // conversion (!) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3128 | QualType T1 = SCS1.getToType(2); |
| 3129 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3130 | T1 = S.Context.getCanonicalType(T1); |
| 3131 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3132 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3133 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3134 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3135 | |
| 3136 | // If the types are the same, we won't learn anything by unwrapped |
| 3137 | // them. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3138 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3139 | return ImplicitConversionSequence::Indistinguishable; |
| 3140 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3141 | // If the type is an array type, promote the element qualifiers to the type |
| 3142 | // for comparison. |
| 3143 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3144 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3145 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3146 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3147 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3148 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3149 | = ImplicitConversionSequence::Indistinguishable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3150 | |
| 3151 | // Objective-C++ ARC: |
| 3152 | // Prefer qualification conversions not involving a change in lifetime |
| 3153 | // to qualification conversions that do not change lifetime. |
| 3154 | if (SCS1.QualificationIncludesObjCLifetime != |
| 3155 | SCS2.QualificationIncludesObjCLifetime) { |
| 3156 | Result = SCS1.QualificationIncludesObjCLifetime |
| 3157 | ? ImplicitConversionSequence::Worse |
| 3158 | : ImplicitConversionSequence::Better; |
| 3159 | } |
| 3160 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3161 | while (S.Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3162 | // Within each iteration of the loop, we check the qualifiers to |
| 3163 | // determine if this still looks like a qualification |
| 3164 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 3165 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3166 | // until there are no more pointers or pointers-to-members left |
| 3167 | // to unwrap. This essentially mimics what |
| 3168 | // IsQualificationConversion does, but here we're checking for a |
| 3169 | // strict subset of qualifiers. |
| 3170 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 3171 | // The qualifiers are the same, so this doesn't tell us anything |
| 3172 | // about how the sequences rank. |
| 3173 | ; |
| 3174 | else if (T2.isMoreQualifiedThan(T1)) { |
| 3175 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 3176 | if (Result == ImplicitConversionSequence::Worse) |
| 3177 | // Neither has qualifiers that are a subset of the other's |
| 3178 | // qualifiers. |
| 3179 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3180 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3181 | Result = ImplicitConversionSequence::Better; |
| 3182 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 3183 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 3184 | if (Result == ImplicitConversionSequence::Better) |
| 3185 | // Neither has qualifiers that are a subset of the other's |
| 3186 | // qualifiers. |
| 3187 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3188 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3189 | Result = ImplicitConversionSequence::Worse; |
| 3190 | } else { |
| 3191 | // Qualifiers are disjoint. |
| 3192 | return ImplicitConversionSequence::Indistinguishable; |
| 3193 | } |
| 3194 | |
| 3195 | // If the types after this point are equivalent, we're done. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3196 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3197 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3200 | // Check that the winning standard conversion sequence isn't using |
| 3201 | // the deprecated string literal array to pointer conversion. |
| 3202 | switch (Result) { |
| 3203 | case ImplicitConversionSequence::Better: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3204 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3205 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3206 | break; |
| 3207 | |
| 3208 | case ImplicitConversionSequence::Indistinguishable: |
| 3209 | break; |
| 3210 | |
| 3211 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 3212 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 3213 | Result = ImplicitConversionSequence::Indistinguishable; |
| 3214 | break; |
| 3215 | } |
| 3216 | |
| 3217 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3218 | } |
| 3219 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3220 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 3221 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3222 | /// various kinds of derived-to-base conversions (C++ |
| 3223 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 3224 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3225 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3226 | CompareDerivedToBaseConversions(Sema &S, |
| 3227 | const StandardConversionSequence& SCS1, |
| 3228 | const StandardConversionSequence& SCS2) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3229 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3230 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3231 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3232 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3233 | |
| 3234 | // Adjust the types we're converting from via the array-to-pointer |
| 3235 | // conversion, if we need to. |
| 3236 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3237 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3238 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3239 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3240 | |
| 3241 | // Canonicalize all of the types. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3242 | FromType1 = S.Context.getCanonicalType(FromType1); |
| 3243 | ToType1 = S.Context.getCanonicalType(ToType1); |
| 3244 | FromType2 = S.Context.getCanonicalType(FromType2); |
| 3245 | ToType2 = S.Context.getCanonicalType(ToType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3246 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3247 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3248 | // |
| 3249 | // If class B is derived directly or indirectly from class A and |
| 3250 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3251 | // |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3252 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3253 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 3254 | SCS2.Second == ICK_Pointer_Conversion && |
| 3255 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 3256 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 3257 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3258 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3259 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3260 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3261 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3262 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3263 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3264 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3265 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3266 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3267 | // -- 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] | 3268 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3269 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3270 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3271 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3272 | return ImplicitConversionSequence::Worse; |
| 3273 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3274 | |
| 3275 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 3276 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3277 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3278 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3279 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3280 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3281 | } |
| 3282 | } else if (SCS1.Second == ICK_Pointer_Conversion && |
| 3283 | SCS2.Second == ICK_Pointer_Conversion) { |
| 3284 | const ObjCObjectPointerType *FromPtr1 |
| 3285 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3286 | const ObjCObjectPointerType *FromPtr2 |
| 3287 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3288 | const ObjCObjectPointerType *ToPtr1 |
| 3289 | = ToType1->getAs<ObjCObjectPointerType>(); |
| 3290 | const ObjCObjectPointerType *ToPtr2 |
| 3291 | = ToType2->getAs<ObjCObjectPointerType>(); |
| 3292 | |
| 3293 | if (FromPtr1 && FromPtr2 && ToPtr1 && ToPtr2) { |
| 3294 | // Apply the same conversion ranking rules for Objective-C pointer types |
| 3295 | // that we do for C++ pointers to class types. However, we employ the |
| 3296 | // Objective-C pseudo-subtyping relationship used for assignment of |
| 3297 | // Objective-C pointer types. |
| 3298 | bool FromAssignLeft |
| 3299 | = S.Context.canAssignObjCInterfaces(FromPtr1, FromPtr2); |
| 3300 | bool FromAssignRight |
| 3301 | = S.Context.canAssignObjCInterfaces(FromPtr2, FromPtr1); |
| 3302 | bool ToAssignLeft |
| 3303 | = S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2); |
| 3304 | bool ToAssignRight |
| 3305 | = S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1); |
| 3306 | |
| 3307 | // A conversion to an a non-id object pointer type or qualified 'id' |
| 3308 | // type is better than a conversion to 'id'. |
| 3309 | if (ToPtr1->isObjCIdType() && |
| 3310 | (ToPtr2->isObjCQualifiedIdType() || ToPtr2->getInterfaceDecl())) |
| 3311 | return ImplicitConversionSequence::Worse; |
| 3312 | if (ToPtr2->isObjCIdType() && |
| 3313 | (ToPtr1->isObjCQualifiedIdType() || ToPtr1->getInterfaceDecl())) |
| 3314 | return ImplicitConversionSequence::Better; |
| 3315 | |
| 3316 | // A conversion to a non-id object pointer type is better than a |
| 3317 | // conversion to a qualified 'id' type |
| 3318 | if (ToPtr1->isObjCQualifiedIdType() && ToPtr2->getInterfaceDecl()) |
| 3319 | return ImplicitConversionSequence::Worse; |
| 3320 | if (ToPtr2->isObjCQualifiedIdType() && ToPtr1->getInterfaceDecl()) |
| 3321 | return ImplicitConversionSequence::Better; |
| 3322 | |
| 3323 | // A conversion to an a non-Class object pointer type or qualified 'Class' |
| 3324 | // type is better than a conversion to 'Class'. |
| 3325 | if (ToPtr1->isObjCClassType() && |
| 3326 | (ToPtr2->isObjCQualifiedClassType() || ToPtr2->getInterfaceDecl())) |
| 3327 | return ImplicitConversionSequence::Worse; |
| 3328 | if (ToPtr2->isObjCClassType() && |
| 3329 | (ToPtr1->isObjCQualifiedClassType() || ToPtr1->getInterfaceDecl())) |
| 3330 | return ImplicitConversionSequence::Better; |
| 3331 | |
| 3332 | // A conversion to a non-Class object pointer type is better than a |
| 3333 | // conversion to a qualified 'Class' type. |
| 3334 | if (ToPtr1->isObjCQualifiedClassType() && ToPtr2->getInterfaceDecl()) |
| 3335 | return ImplicitConversionSequence::Worse; |
| 3336 | if (ToPtr2->isObjCQualifiedClassType() && ToPtr1->getInterfaceDecl()) |
| 3337 | return ImplicitConversionSequence::Better; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3338 | |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3339 | // -- "conversion of C* to B* is better than conversion of C* to A*," |
| 3340 | if (S.Context.hasSameType(FromType1, FromType2) && |
| 3341 | !FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType() && |
| 3342 | (ToAssignLeft != ToAssignRight)) |
| 3343 | return ToAssignLeft? ImplicitConversionSequence::Worse |
| 3344 | : ImplicitConversionSequence::Better; |
| 3345 | |
| 3346 | // -- "conversion of B* to A* is better than conversion of C* to A*," |
| 3347 | if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) && |
| 3348 | (FromAssignLeft != FromAssignRight)) |
| 3349 | return FromAssignLeft? ImplicitConversionSequence::Better |
| 3350 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3351 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3352 | } |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3353 | |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3354 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3355 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 3356 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 3357 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3358 | const MemberPointerType * FromMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3359 | FromType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3360 | const MemberPointerType * ToMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3361 | ToType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3362 | const MemberPointerType * FromMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3363 | FromType2->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3364 | const MemberPointerType * ToMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3365 | ToType2->getAs<MemberPointerType>(); |
| 3366 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 3367 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 3368 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 3369 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 3370 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 3371 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 3372 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 3373 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3374 | // 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] | 3375 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3376 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3377 | return ImplicitConversionSequence::Worse; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3378 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3379 | return ImplicitConversionSequence::Better; |
| 3380 | } |
| 3381 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 3382 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3383 | if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3384 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3385 | else if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3386 | return ImplicitConversionSequence::Worse; |
| 3387 | } |
| 3388 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3389 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3390 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3391 | // -- 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] | 3392 | // -- binding of an expression of type C to a reference of type |
| 3393 | // B& is better than binding an expression of type C to a |
| 3394 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3395 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3396 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3397 | if (S.IsDerivedFrom(ToType1, ToType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3398 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3399 | else if (S.IsDerivedFrom(ToType2, ToType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3400 | return ImplicitConversionSequence::Worse; |
| 3401 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3402 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3403 | // -- 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] | 3404 | // -- binding of an expression of type B to a reference of type |
| 3405 | // A& is better than binding an expression of type C to a |
| 3406 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3407 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3408 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3409 | if (S.IsDerivedFrom(FromType2, FromType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3410 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3411 | else if (S.IsDerivedFrom(FromType1, FromType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3412 | return ImplicitConversionSequence::Worse; |
| 3413 | } |
| 3414 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3415 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3416 | return ImplicitConversionSequence::Indistinguishable; |
| 3417 | } |
| 3418 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3419 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 3420 | /// determine whether they are reference-related, |
| 3421 | /// reference-compatible, reference-compatible with added |
| 3422 | /// qualification, or incompatible, for use in C++ initialization by |
| 3423 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 3424 | /// type, and the first type (T1) is the pointee type of the reference |
| 3425 | /// type being initialized. |
| 3426 | Sema::ReferenceCompareResult |
| 3427 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 3428 | QualType OrigT1, QualType OrigT2, |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3429 | bool &DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3430 | bool &ObjCConversion, |
| 3431 | bool &ObjCLifetimeConversion) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3432 | assert(!OrigT1->isReferenceType() && |
| 3433 | "T1 must be the pointee type of the reference type"); |
| 3434 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 3435 | |
| 3436 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 3437 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 3438 | Qualifiers T1Quals, T2Quals; |
| 3439 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3440 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 3441 | |
| 3442 | // C++ [dcl.init.ref]p4: |
| 3443 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 3444 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 3445 | // T1 is a base class of T2. |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3446 | DerivedToBase = false; |
| 3447 | ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3448 | ObjCLifetimeConversion = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3449 | if (UnqualT1 == UnqualT2) { |
| 3450 | // Nothing to do. |
| 3451 | } else if (!RequireCompleteType(Loc, OrigT2, PDiag()) && |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3452 | IsDerivedFrom(UnqualT2, UnqualT1)) |
| 3453 | DerivedToBase = true; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3454 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
| 3455 | UnqualT2->isObjCObjectOrInterfaceType() && |
| 3456 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
| 3457 | ObjCConversion = true; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3458 | else |
| 3459 | return Ref_Incompatible; |
| 3460 | |
| 3461 | // At this point, we know that T1 and T2 are reference-related (at |
| 3462 | // least). |
| 3463 | |
| 3464 | // If the type is an array type, promote the element qualifiers to the type |
| 3465 | // for comparison. |
| 3466 | if (isa<ArrayType>(T1) && T1Quals) |
| 3467 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 3468 | if (isa<ArrayType>(T2) && T2Quals) |
| 3469 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 3470 | |
| 3471 | // C++ [dcl.init.ref]p4: |
| 3472 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 3473 | // reference-related to T2 and cv1 is the same cv-qualification |
| 3474 | // as, or greater cv-qualification than, cv2. For purposes of |
| 3475 | // overload resolution, cases for which cv1 is greater |
| 3476 | // cv-qualification than cv2 are identified as |
| 3477 | // reference-compatible with added qualification (see 13.3.3.2). |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 3478 | // |
| 3479 | // Note that we also require equivalence of Objective-C GC and address-space |
| 3480 | // qualifiers when performing these computations, so that e.g., an int in |
| 3481 | // address space 1 is not reference-compatible with an int in address |
| 3482 | // space 2. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3483 | if (T1Quals.getObjCLifetime() != T2Quals.getObjCLifetime() && |
| 3484 | T1Quals.compatiblyIncludesObjCLifetime(T2Quals)) { |
| 3485 | T1Quals.removeObjCLifetime(); |
| 3486 | T2Quals.removeObjCLifetime(); |
| 3487 | ObjCLifetimeConversion = true; |
| 3488 | } |
| 3489 | |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 3490 | if (T1Quals == T2Quals) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3491 | return Ref_Compatible; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3492 | else if (T1Quals.compatiblyIncludes(T2Quals)) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3493 | return Ref_Compatible_With_Added_Qualification; |
| 3494 | else |
| 3495 | return Ref_Related; |
| 3496 | } |
| 3497 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3498 | /// \brief Look for a user-defined conversion to an value reference-compatible |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3499 | /// with DeclType. Return true if something definite is found. |
| 3500 | static bool |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3501 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
| 3502 | QualType DeclType, SourceLocation DeclLoc, |
| 3503 | Expr *Init, QualType T2, bool AllowRvalues, |
| 3504 | bool AllowExplicit) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3505 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 3506 | CXXRecordDecl *T2RecordDecl |
| 3507 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 3508 | |
| 3509 | OverloadCandidateSet CandidateSet(DeclLoc); |
| 3510 | const UnresolvedSetImpl *Conversions |
| 3511 | = T2RecordDecl->getVisibleConversionFunctions(); |
| 3512 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 3513 | E = Conversions->end(); I != E; ++I) { |
| 3514 | NamedDecl *D = *I; |
| 3515 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3516 | if (isa<UsingShadowDecl>(D)) |
| 3517 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3518 | |
| 3519 | FunctionTemplateDecl *ConvTemplate |
| 3520 | = dyn_cast<FunctionTemplateDecl>(D); |
| 3521 | CXXConversionDecl *Conv; |
| 3522 | if (ConvTemplate) |
| 3523 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3524 | else |
| 3525 | Conv = cast<CXXConversionDecl>(D); |
| 3526 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3527 | // 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] | 3528 | // explicit conversions, skip it. |
| 3529 | if (!AllowExplicit && Conv->isExplicit()) |
| 3530 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3531 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3532 | if (AllowRvalues) { |
| 3533 | bool DerivedToBase = false; |
| 3534 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3535 | bool ObjCLifetimeConversion = false; |
Douglas Gregor | 203050c | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 3536 | |
| 3537 | // If we are initializing an rvalue reference, don't permit conversion |
| 3538 | // functions that return lvalues. |
| 3539 | if (!ConvTemplate && DeclType->isRValueReferenceType()) { |
| 3540 | const ReferenceType *RefType |
| 3541 | = Conv->getConversionType()->getAs<LValueReferenceType>(); |
| 3542 | if (RefType && !RefType->getPointeeType()->isFunctionType()) |
| 3543 | continue; |
| 3544 | } |
| 3545 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3546 | if (!ConvTemplate && |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3547 | S.CompareReferenceRelationship( |
| 3548 | DeclLoc, |
| 3549 | Conv->getConversionType().getNonReferenceType() |
| 3550 | .getUnqualifiedType(), |
| 3551 | DeclType.getNonReferenceType().getUnqualifiedType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3552 | DerivedToBase, ObjCConversion, ObjCLifetimeConversion) == |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3553 | Sema::Ref_Incompatible) |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3554 | continue; |
| 3555 | } else { |
| 3556 | // If the conversion function doesn't return a reference type, |
| 3557 | // it can't be considered for this conversion. An rvalue reference |
| 3558 | // is only acceptable if its referencee is a function type. |
| 3559 | |
| 3560 | const ReferenceType *RefType = |
| 3561 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 3562 | if (!RefType || |
| 3563 | (!RefType->isLValueReferenceType() && |
| 3564 | !RefType->getPointeeType()->isFunctionType())) |
| 3565 | continue; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3566 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3567 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3568 | if (ConvTemplate) |
| 3569 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3570 | Init, DeclType, CandidateSet); |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3571 | else |
| 3572 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3573 | DeclType, CandidateSet); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3576 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 3577 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3578 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3579 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3580 | case OR_Success: |
| 3581 | // C++ [over.ics.ref]p1: |
| 3582 | // |
| 3583 | // [...] If the parameter binds directly to the result of |
| 3584 | // applying a conversion function to the argument |
| 3585 | // expression, the implicit conversion sequence is a |
| 3586 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 3587 | // second standard conversion sequence either an identity |
| 3588 | // conversion or, if the conversion function returns an |
| 3589 | // entity of a type that is a derived class of the parameter |
| 3590 | // type, a derived-to-base Conversion. |
| 3591 | if (!Best->FinalConversion.DirectBinding) |
| 3592 | return false; |
| 3593 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3594 | if (Best->Function) |
| 3595 | S.MarkDeclarationReferenced(DeclLoc, Best->Function); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3596 | ICS.setUserDefined(); |
| 3597 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 3598 | ICS.UserDefined.After = Best->FinalConversion; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 3599 | ICS.UserDefined.HadMultipleCandidates = HadMultipleCandidates; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3600 | ICS.UserDefined.ConversionFunction = Best->Function; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 3601 | ICS.UserDefined.FoundConversionFunction = Best->FoundDecl; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3602 | ICS.UserDefined.EllipsisConversion = false; |
| 3603 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 3604 | ICS.UserDefined.After.DirectBinding && |
| 3605 | "Expected a direct reference binding!"); |
| 3606 | return true; |
| 3607 | |
| 3608 | case OR_Ambiguous: |
| 3609 | ICS.setAmbiguous(); |
| 3610 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3611 | Cand != CandidateSet.end(); ++Cand) |
| 3612 | if (Cand->Viable) |
| 3613 | ICS.Ambiguous.addConversion(Cand->Function); |
| 3614 | return true; |
| 3615 | |
| 3616 | case OR_No_Viable_Function: |
| 3617 | case OR_Deleted: |
| 3618 | // There was no suitable conversion, or we found a deleted |
| 3619 | // conversion; continue with other checks. |
| 3620 | return false; |
| 3621 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3622 | |
Eric Christopher | 1c3d502 | 2010-06-30 18:36:32 +0000 | [diff] [blame] | 3623 | return false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3624 | } |
| 3625 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3626 | /// \brief Compute an implicit conversion sequence for reference |
| 3627 | /// initialization. |
| 3628 | static ImplicitConversionSequence |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 3629 | TryReferenceInit(Sema &S, Expr *Init, QualType DeclType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3630 | SourceLocation DeclLoc, |
| 3631 | bool SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 3632 | bool AllowExplicit) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3633 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 3634 | |
| 3635 | // Most paths end in a failed conversion. |
| 3636 | ImplicitConversionSequence ICS; |
| 3637 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 3638 | |
| 3639 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 3640 | QualType T2 = Init->getType(); |
| 3641 | |
| 3642 | // If the initializer is the address of an overloaded function, try |
| 3643 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3644 | // type of the resulting function. |
| 3645 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 3646 | DeclAccessPair Found; |
| 3647 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 3648 | false, Found)) |
| 3649 | T2 = Fn->getType(); |
| 3650 | } |
| 3651 | |
| 3652 | // Compute some basic properties of the types and the initializer. |
| 3653 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 3654 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3655 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3656 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3657 | Expr::Classification InitCategory = Init->Classify(S.Context); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3658 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3659 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3660 | ObjCConversion, ObjCLifetimeConversion); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3661 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3662 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3663 | // C++0x [dcl.init.ref]p5: |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 3664 | // A reference to type "cv1 T1" is initialized by an expression |
| 3665 | // of type "cv2 T2" as follows: |
| 3666 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3667 | // -- If reference is an lvalue reference and the initializer expression |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3668 | if (!isRValRef) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3669 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 3670 | // reference-compatible with "cv2 T2," or |
| 3671 | // |
| 3672 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 3673 | if (InitCategory.isLValue() && |
| 3674 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3675 | // C++ [over.ics.ref]p1: |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3676 | // When a parameter of reference type binds directly (8.5.3) |
| 3677 | // to an argument expression, the implicit conversion sequence |
| 3678 | // is the identity conversion, unless the argument expression |
| 3679 | // has a type that is a derived class of the parameter type, |
| 3680 | // in which case the implicit conversion sequence is a |
| 3681 | // derived-to-base Conversion (13.3.3.1). |
| 3682 | ICS.setStandard(); |
| 3683 | ICS.Standard.First = ICK_Identity; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3684 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 3685 | : ObjCConversion? ICK_Compatible_Conversion |
| 3686 | : ICK_Identity; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3687 | ICS.Standard.Third = ICK_Identity; |
| 3688 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 3689 | ICS.Standard.setToType(0, T2); |
| 3690 | ICS.Standard.setToType(1, T1); |
| 3691 | ICS.Standard.setToType(2, T1); |
| 3692 | ICS.Standard.ReferenceBinding = true; |
| 3693 | ICS.Standard.DirectBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3694 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3695 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 3696 | ICS.Standard.BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3697 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3698 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3699 | ICS.Standard.CopyConstructor = 0; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3700 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3701 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 3702 | // derived-to-base conversions is suppressed when we're |
| 3703 | // computing the implicit conversion sequence (C++ |
| 3704 | // [over.best.ics]p2). |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3705 | return ICS; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3706 | } |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3707 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3708 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 3709 | // not reference-related to T2, and can be implicitly |
| 3710 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 3711 | // is reference-compatible with "cv3 T3" 92) (this |
| 3712 | // conversion is selected by enumerating the applicable |
| 3713 | // conversion functions (13.3.1.6) and choosing the best |
| 3714 | // one through overload resolution (13.3)), |
| 3715 | if (!SuppressUserConversions && T2->isRecordType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3716 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3717 | RefRelationship == Sema::Ref_Incompatible) { |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3718 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 3719 | Init, T2, /*AllowRvalues=*/false, |
| 3720 | AllowExplicit)) |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3721 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3722 | } |
| 3723 | } |
| 3724 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3725 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 3726 | // 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] | 3727 | // shall be an rvalue reference. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3728 | // |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 3729 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 3730 | // point, which is that, due to p2 (which short-circuits reference |
| 3731 | // binding by only attempting a simple conversion for non-direct |
| 3732 | // bindings) and p3's strange wording, we allow a const volatile |
| 3733 | // reference to bind to an rvalue. Hence the check for the presence |
| 3734 | // of "const" rather than checking for "const" being the only |
| 3735 | // qualifier. |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3736 | // This is also the point where rvalue references and lvalue inits no longer |
| 3737 | // go together. |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 3738 | if (!isRValRef && !T1.isConstQualified()) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3739 | return ICS; |
| 3740 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3741 | // -- If the initializer expression |
| 3742 | // |
| 3743 | // -- is an xvalue, class prvalue, array prvalue or function |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3744 | // lvalue and "cv1 T1" is reference-compatible with "cv2 T2", or |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3745 | if (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification && |
| 3746 | (InitCategory.isXValue() || |
| 3747 | (InitCategory.isPRValue() && (T2->isRecordType() || T2->isArrayType())) || |
| 3748 | (InitCategory.isLValue() && T2->isFunctionType()))) { |
| 3749 | ICS.setStandard(); |
| 3750 | ICS.Standard.First = ICK_Identity; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3751 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3752 | : ObjCConversion? ICK_Compatible_Conversion |
| 3753 | : ICK_Identity; |
| 3754 | ICS.Standard.Third = ICK_Identity; |
| 3755 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 3756 | ICS.Standard.setToType(0, T2); |
| 3757 | ICS.Standard.setToType(1, T1); |
| 3758 | ICS.Standard.setToType(2, T1); |
| 3759 | ICS.Standard.ReferenceBinding = true; |
| 3760 | // In C++0x, this is always a direct binding. In C++98/03, it's a direct |
| 3761 | // binding unless we're binding to a class prvalue. |
| 3762 | // Note: Although xvalues wouldn't normally show up in C++98/03 code, we |
| 3763 | // allow the use of rvalue references in C++98/03 for the benefit of |
| 3764 | // standard library implementors; therefore, we need the xvalue check here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3765 | ICS.Standard.DirectBinding = |
| 3766 | S.getLangOptions().CPlusPlus0x || |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3767 | (InitCategory.isPRValue() && !T2->isRecordType()); |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3768 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3769 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3770 | ICS.Standard.BindsToRvalue = InitCategory.isRValue(); |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3771 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3772 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3773 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3774 | return ICS; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3775 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3776 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3777 | // -- has a class type (i.e., T2 is a class type), where T1 is not |
| 3778 | // reference-related to T2, and can be implicitly converted to |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3779 | // an xvalue, class prvalue, or function lvalue of type |
| 3780 | // "cv3 T3", where "cv1 T1" is reference-compatible with |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3781 | // "cv3 T3", |
| 3782 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3783 | // then the reference is bound to the value of the initializer |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3784 | // expression in the first case and to the result of the conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3785 | // in the second case (or, in either case, to an appropriate base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3786 | // class subobject). |
| 3787 | if (!SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3788 | T2->isRecordType() && !S.RequireCompleteType(DeclLoc, T2, 0) && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3789 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 3790 | Init, T2, /*AllowRvalues=*/true, |
| 3791 | AllowExplicit)) { |
| 3792 | // In the second case, if the reference is an rvalue reference |
| 3793 | // and the second standard conversion sequence of the |
| 3794 | // user-defined conversion sequence includes an lvalue-to-rvalue |
| 3795 | // conversion, the program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3796 | if (ICS.isUserDefined() && isRValRef && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3797 | ICS.UserDefined.After.First == ICK_Lvalue_To_Rvalue) |
| 3798 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 3799 | |
Douglas Gregor | 68ed68b | 2011-01-21 16:36:05 +0000 | [diff] [blame] | 3800 | return ICS; |
Rafael Espindola | aa5952c | 2011-01-22 15:32:35 +0000 | [diff] [blame] | 3801 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3802 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3803 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 3804 | // initialized from the initializer expression using the |
| 3805 | // rules for a non-reference copy initialization (8.5). The |
| 3806 | // reference is then bound to the temporary. If T1 is |
| 3807 | // reference-related to T2, cv1 must be the same |
| 3808 | // cv-qualification as, or greater cv-qualification than, |
| 3809 | // cv2; otherwise, the program is ill-formed. |
| 3810 | if (RefRelationship == Sema::Ref_Related) { |
| 3811 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 3812 | // we would be reference-compatible or reference-compatible with |
| 3813 | // added qualification. But that wasn't the case, so the reference |
| 3814 | // initialization fails. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3815 | // |
| 3816 | // Note that we only want to check address spaces and cvr-qualifiers here. |
| 3817 | // ObjC GC and lifetime qualifiers aren't important. |
| 3818 | Qualifiers T1Quals = T1.getQualifiers(); |
| 3819 | Qualifiers T2Quals = T2.getQualifiers(); |
| 3820 | T1Quals.removeObjCGCAttr(); |
| 3821 | T1Quals.removeObjCLifetime(); |
| 3822 | T2Quals.removeObjCGCAttr(); |
| 3823 | T2Quals.removeObjCLifetime(); |
| 3824 | if (!T1Quals.compatiblyIncludes(T2Quals)) |
| 3825 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3826 | } |
| 3827 | |
| 3828 | // If at least one of the types is a class type, the types are not |
| 3829 | // related, and we aren't allowed any user conversions, the |
| 3830 | // reference binding fails. This case is important for breaking |
| 3831 | // recursion, since TryImplicitConversion below will attempt to |
| 3832 | // create a temporary through the use of a copy constructor. |
| 3833 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 3834 | (T1->isRecordType() || T2->isRecordType())) |
| 3835 | return ICS; |
| 3836 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 3837 | // If T1 is reference-related to T2 and the reference is an rvalue |
| 3838 | // reference, the initializer expression shall not be an lvalue. |
| 3839 | if (RefRelationship >= Sema::Ref_Related && |
| 3840 | isRValRef && Init->Classify(S.Context).isLValue()) |
| 3841 | return ICS; |
| 3842 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3843 | // C++ [over.ics.ref]p2: |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3844 | // When a parameter of reference type is not bound directly to |
| 3845 | // an argument expression, the conversion sequence is the one |
| 3846 | // required to convert the argument expression to the |
| 3847 | // underlying type of the reference according to |
| 3848 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 3849 | // to copy-initializing a temporary of the underlying type with |
| 3850 | // the argument expression. Any difference in top-level |
| 3851 | // cv-qualification is subsumed by the initialization itself |
| 3852 | // and does not constitute a conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3853 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
| 3854 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3855 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3856 | /*CStyle=*/false, |
| 3857 | /*AllowObjCWritebackConversion=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3858 | |
| 3859 | // Of course, that's still a reference binding. |
| 3860 | if (ICS.isStandard()) { |
| 3861 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3862 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3863 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 3864 | ICS.Standard.BindsToRvalue = true; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3865 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3866 | ICS.Standard.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3867 | } else if (ICS.isUserDefined()) { |
Douglas Gregor | 203050c | 2011-10-04 23:59:32 +0000 | [diff] [blame] | 3868 | // Don't allow rvalue references to bind to lvalues. |
| 3869 | if (DeclType->isRValueReferenceType()) { |
| 3870 | if (const ReferenceType *RefType |
| 3871 | = ICS.UserDefined.ConversionFunction->getResultType() |
| 3872 | ->getAs<LValueReferenceType>()) { |
| 3873 | if (!RefType->getPointeeType()->isFunctionType()) { |
| 3874 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, Init, |
| 3875 | DeclType); |
| 3876 | return ICS; |
| 3877 | } |
| 3878 | } |
| 3879 | } |
| 3880 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3881 | ICS.UserDefined.After.ReferenceBinding = true; |
Douglas Gregor | f20d272 | 2011-08-15 13:59:46 +0000 | [diff] [blame] | 3882 | ICS.UserDefined.After.IsLvalueReference = !isRValRef; |
| 3883 | ICS.UserDefined.After.BindsToFunctionLvalue = T2->isFunctionType(); |
| 3884 | ICS.UserDefined.After.BindsToRvalue = true; |
| 3885 | ICS.UserDefined.After.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 3886 | ICS.UserDefined.After.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3887 | } |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 3888 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3889 | return ICS; |
| 3890 | } |
| 3891 | |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 3892 | static ImplicitConversionSequence |
| 3893 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
| 3894 | bool SuppressUserConversions, |
| 3895 | bool InOverloadResolution, |
| 3896 | bool AllowObjCWritebackConversion); |
| 3897 | |
| 3898 | /// TryListConversion - Try to copy-initialize a value of type ToType from the |
| 3899 | /// initializer list From. |
| 3900 | static ImplicitConversionSequence |
| 3901 | TryListConversion(Sema &S, InitListExpr *From, QualType ToType, |
| 3902 | bool SuppressUserConversions, |
| 3903 | bool InOverloadResolution, |
| 3904 | bool AllowObjCWritebackConversion) { |
| 3905 | // C++11 [over.ics.list]p1: |
| 3906 | // When an argument is an initializer list, it is not an expression and |
| 3907 | // special rules apply for converting it to a parameter type. |
| 3908 | |
| 3909 | ImplicitConversionSequence Result; |
| 3910 | Result.setBad(BadConversionSequence::no_conversion, From, ToType); |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3911 | Result.setListInitializationSequence(); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 3912 | |
| 3913 | // C++11 [over.ics.list]p2: |
| 3914 | // If the parameter type is std::initializer_list<X> or "array of X" and |
| 3915 | // all the elements can be implicitly converted to X, the implicit |
| 3916 | // conversion sequence is the worst conversion necessary to convert an |
| 3917 | // element of the list to X. |
| 3918 | // FIXME: Recognize std::initializer_list. |
| 3919 | // FIXME: Arrays don't make sense until we can deal with references. |
| 3920 | if (ToType->isArrayType()) |
| 3921 | return Result; |
| 3922 | |
| 3923 | // C++11 [over.ics.list]p3: |
| 3924 | // Otherwise, if the parameter is a non-aggregate class X and overload |
| 3925 | // resolution chooses a single best constructor [...] the implicit |
| 3926 | // conversion sequence is a user-defined conversion sequence. If multiple |
| 3927 | // constructors are viable but none is better than the others, the |
| 3928 | // implicit conversion sequence is a user-defined conversion sequence. |
| 3929 | // FIXME: Implement this. |
| 3930 | if (ToType->isRecordType() && !ToType->isAggregateType()) |
| 3931 | return Result; |
| 3932 | |
| 3933 | // C++11 [over.ics.list]p4: |
| 3934 | // Otherwise, if the parameter has an aggregate type which can be |
| 3935 | // initialized from the initializer list [...] the implicit conversion |
| 3936 | // sequence is a user-defined conversion sequence. |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 3937 | if (ToType->isAggregateType()) { |
Sebastian Redl | cc7a648 | 2011-11-01 15:53:09 +0000 | [diff] [blame] | 3938 | // Type is an aggregate, argument is an init list. At this point it comes |
| 3939 | // down to checking whether the initialization works. |
| 3940 | // FIXME: Find out whether this parameter is consumed or not. |
| 3941 | InitializedEntity Entity = |
| 3942 | InitializedEntity::InitializeParameter(S.Context, ToType, |
| 3943 | /*Consumed=*/false); |
| 3944 | if (S.CanPerformCopyInitialization(Entity, S.Owned(From))) { |
| 3945 | Result.setUserDefined(); |
| 3946 | Result.UserDefined.Before.setAsIdentityConversion(); |
| 3947 | // Initializer lists don't have a type. |
| 3948 | Result.UserDefined.Before.setFromType(QualType()); |
| 3949 | Result.UserDefined.Before.setAllToTypes(QualType()); |
| 3950 | |
| 3951 | Result.UserDefined.After.setAsIdentityConversion(); |
| 3952 | Result.UserDefined.After.setFromType(ToType); |
| 3953 | Result.UserDefined.After.setAllToTypes(ToType); |
| 3954 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 3955 | return Result; |
| 3956 | } |
| 3957 | |
| 3958 | // C++11 [over.ics.list]p5: |
| 3959 | // Otherwise, if the parameter is a reference, see 13.3.3.1.4. |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 3960 | if (ToType->isReferenceType()) { |
| 3961 | // The standard is notoriously unclear here, since 13.3.3.1.4 doesn't |
| 3962 | // mention initializer lists in any way. So we go by what list- |
| 3963 | // initialization would do and try to extrapolate from that. |
| 3964 | |
| 3965 | QualType T1 = ToType->getAs<ReferenceType>()->getPointeeType(); |
| 3966 | |
| 3967 | // If the initializer list has a single element that is reference-related |
| 3968 | // to the parameter type, we initialize the reference from that. |
| 3969 | if (From->getNumInits() == 1) { |
| 3970 | Expr *Init = From->getInit(0); |
| 3971 | |
| 3972 | QualType T2 = Init->getType(); |
| 3973 | |
| 3974 | // If the initializer is the address of an overloaded function, try |
| 3975 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3976 | // type of the resulting function. |
| 3977 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 3978 | DeclAccessPair Found; |
| 3979 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction( |
| 3980 | Init, ToType, false, Found)) |
| 3981 | T2 = Fn->getType(); |
| 3982 | } |
| 3983 | |
| 3984 | // Compute some basic properties of the types and the initializer. |
| 3985 | bool dummy1 = false; |
| 3986 | bool dummy2 = false; |
| 3987 | bool dummy3 = false; |
| 3988 | Sema::ReferenceCompareResult RefRelationship |
| 3989 | = S.CompareReferenceRelationship(From->getLocStart(), T1, T2, dummy1, |
| 3990 | dummy2, dummy3); |
| 3991 | |
| 3992 | if (RefRelationship >= Sema::Ref_Related) |
| 3993 | return TryReferenceInit(S, Init, ToType, |
| 3994 | /*FIXME:*/From->getLocStart(), |
| 3995 | SuppressUserConversions, |
| 3996 | /*AllowExplicit=*/false); |
| 3997 | } |
| 3998 | |
| 3999 | // Otherwise, we bind the reference to a temporary created from the |
| 4000 | // initializer list. |
| 4001 | Result = TryListConversion(S, From, T1, SuppressUserConversions, |
| 4002 | InOverloadResolution, |
| 4003 | AllowObjCWritebackConversion); |
| 4004 | if (Result.isFailure()) |
| 4005 | return Result; |
| 4006 | assert(!Result.isEllipsis() && |
| 4007 | "Sub-initialization cannot result in ellipsis conversion."); |
| 4008 | |
| 4009 | // Can we even bind to a temporary? |
| 4010 | if (ToType->isRValueReferenceType() || |
| 4011 | (T1.isConstQualified() && !T1.isVolatileQualified())) { |
| 4012 | StandardConversionSequence &SCS = Result.isStandard() ? Result.Standard : |
| 4013 | Result.UserDefined.After; |
| 4014 | SCS.ReferenceBinding = true; |
| 4015 | SCS.IsLvalueReference = ToType->isLValueReferenceType(); |
| 4016 | SCS.BindsToRvalue = true; |
| 4017 | SCS.BindsToFunctionLvalue = false; |
| 4018 | SCS.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 4019 | SCS.ObjCLifetimeConversionBinding = false; |
| 4020 | } else |
| 4021 | Result.setBad(BadConversionSequence::lvalue_ref_to_rvalue, |
| 4022 | From, ToType); |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4023 | return Result; |
Sebastian Redl | 1cdb70b | 2011-12-03 14:54:30 +0000 | [diff] [blame] | 4024 | } |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4025 | |
| 4026 | // C++11 [over.ics.list]p6: |
| 4027 | // Otherwise, if the parameter type is not a class: |
| 4028 | if (!ToType->isRecordType()) { |
| 4029 | // - if the initializer list has one element, the implicit conversion |
| 4030 | // sequence is the one required to convert the element to the |
| 4031 | // parameter type. |
| 4032 | // FIXME: Catch narrowing here? |
| 4033 | unsigned NumInits = From->getNumInits(); |
| 4034 | if (NumInits == 1) |
| 4035 | Result = TryCopyInitialization(S, From->getInit(0), ToType, |
| 4036 | SuppressUserConversions, |
| 4037 | InOverloadResolution, |
| 4038 | AllowObjCWritebackConversion); |
| 4039 | // - if the initializer list has no elements, the implicit conversion |
| 4040 | // sequence is the identity conversion. |
| 4041 | else if (NumInits == 0) { |
| 4042 | Result.setStandard(); |
| 4043 | Result.Standard.setAsIdentityConversion(); |
| 4044 | } |
| 4045 | return Result; |
| 4046 | } |
| 4047 | |
| 4048 | // C++11 [over.ics.list]p7: |
| 4049 | // In all cases other than those enumerated above, no conversion is possible |
| 4050 | return Result; |
| 4051 | } |
| 4052 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4053 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 4054 | /// ToType from the expression From. Return the implicit conversion |
| 4055 | /// sequence required to pass this argument, which may be a bad |
| 4056 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4057 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | 74e386e | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 4058 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4059 | static ImplicitConversionSequence |
| 4060 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4061 | bool SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4062 | bool InOverloadResolution, |
| 4063 | bool AllowObjCWritebackConversion) { |
Sebastian Redl | 5405b81 | 2011-10-16 18:19:34 +0000 | [diff] [blame] | 4064 | if (InitListExpr *FromInitList = dyn_cast<InitListExpr>(From)) |
| 4065 | return TryListConversion(S, FromInitList, ToType, SuppressUserConversions, |
| 4066 | InOverloadResolution,AllowObjCWritebackConversion); |
| 4067 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4068 | if (ToType->isReferenceType()) |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4069 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4070 | /*FIXME:*/From->getLocStart(), |
| 4071 | SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 4072 | /*AllowExplicit=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 4073 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4074 | return TryImplicitConversion(S, From, ToType, |
| 4075 | SuppressUserConversions, |
| 4076 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4077 | InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4078 | /*CStyle=*/false, |
| 4079 | AllowObjCWritebackConversion); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4080 | } |
| 4081 | |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 4082 | static bool TryCopyInitialization(const CanQualType FromQTy, |
| 4083 | const CanQualType ToQTy, |
| 4084 | Sema &S, |
| 4085 | SourceLocation Loc, |
| 4086 | ExprValueKind FromVK) { |
| 4087 | OpaqueValueExpr TmpExpr(Loc, FromQTy, FromVK); |
| 4088 | ImplicitConversionSequence ICS = |
| 4089 | TryCopyInitialization(S, &TmpExpr, ToQTy, true, true, false); |
| 4090 | |
| 4091 | return !ICS.isBad(); |
| 4092 | } |
| 4093 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4094 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 4095 | /// parameter of the given member function (@c Method) from the |
| 4096 | /// expression @p From. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4097 | static ImplicitConversionSequence |
| 4098 | TryObjectArgumentInitialization(Sema &S, QualType OrigFromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4099 | Expr::Classification FromClassification, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4100 | CXXMethodDecl *Method, |
| 4101 | CXXRecordDecl *ActingContext) { |
| 4102 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 4103 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 4104 | // const volatile object. |
| 4105 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 4106 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4107 | QualType ImplicitParamType = S.Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4108 | |
| 4109 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 4110 | // to exit early. |
| 4111 | ImplicitConversionSequence ICS; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4112 | |
| 4113 | // We need to have an object of class type. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 4114 | QualType FromType = OrigFromType; |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4115 | if (const PointerType *PT = FromType->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4116 | FromType = PT->getPointeeType(); |
| 4117 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4118 | // When we had a pointer, it's implicitly dereferenced, so we |
| 4119 | // better have an lvalue. |
| 4120 | assert(FromClassification.isLValue()); |
| 4121 | } |
| 4122 | |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4123 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4124 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4125 | // C++0x [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4126 | // For non-static member functions, the type of the implicit object |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4127 | // parameter is |
| 4128 | // |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4129 | // - "lvalue reference to cv X" for functions declared without a |
| 4130 | // ref-qualifier or with the & ref-qualifier |
| 4131 | // - "rvalue reference to cv X" for functions declared with the && |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4132 | // ref-qualifier |
| 4133 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4134 | // 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] | 4135 | // cv-qualification on the member function declaration. |
| 4136 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4137 | // However, when finding an implicit conversion sequence for the argument, we |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4138 | // are not allowed to create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4139 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 4140 | // reference binding here, that allows class rvalues to bind to |
| 4141 | // non-constant references. |
| 4142 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4143 | // First check the qualifiers. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4144 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4145 | if (ImplicitParamType.getCVRQualifiers() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4146 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4147 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4148 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
| 4149 | OrigFromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4150 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4151 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4152 | |
| 4153 | // Check that we have either the same type or a derived type. It |
| 4154 | // affects the conversion rank. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4155 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4156 | ImplicitConversionKind SecondKind; |
| 4157 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 4158 | SecondKind = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4159 | } else if (S.IsDerivedFrom(FromType, ClassType)) |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4160 | SecondKind = ICK_Derived_To_Base; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4161 | else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4162 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 4163 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4164 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4165 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4166 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4167 | // Check the ref-qualifier. |
| 4168 | switch (Method->getRefQualifier()) { |
| 4169 | case RQ_None: |
| 4170 | // Do nothing; we don't care about lvalueness or rvalueness. |
| 4171 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4172 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4173 | case RQ_LValue: |
| 4174 | if (!FromClassification.isLValue() && Quals != Qualifiers::Const) { |
| 4175 | // non-const lvalue reference cannot bind to an rvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4176 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4177 | ImplicitParamType); |
| 4178 | return ICS; |
| 4179 | } |
| 4180 | break; |
| 4181 | |
| 4182 | case RQ_RValue: |
| 4183 | if (!FromClassification.isRValue()) { |
| 4184 | // rvalue reference cannot bind to an lvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4185 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4186 | ImplicitParamType); |
| 4187 | return ICS; |
| 4188 | } |
| 4189 | break; |
| 4190 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4191 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4192 | // Success. Mark this as a reference binding. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4193 | ICS.setStandard(); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4194 | ICS.Standard.setAsIdentityConversion(); |
| 4195 | ICS.Standard.Second = SecondKind; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4196 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 4197 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4198 | ICS.Standard.ReferenceBinding = true; |
| 4199 | ICS.Standard.DirectBinding = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4200 | ICS.Standard.IsLvalueReference = Method->getRefQualifier() != RQ_RValue; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 4201 | ICS.Standard.BindsToFunctionLvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 4202 | ICS.Standard.BindsToRvalue = FromClassification.isRValue(); |
| 4203 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier |
| 4204 | = (Method->getRefQualifier() == RQ_None); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4205 | return ICS; |
| 4206 | } |
| 4207 | |
| 4208 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 4209 | /// the implicit object parameter for the given Method with the given |
| 4210 | /// expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4211 | ExprResult |
| 4212 | Sema::PerformObjectArgumentInitialization(Expr *From, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4213 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4214 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4215 | CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4216 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4217 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4218 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4219 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4220 | Expr::Classification FromClassification; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4221 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4222 | FromRecordType = PT->getPointeeType(); |
| 4223 | DestType = Method->getThisType(Context); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4224 | FromClassification = Expr::Classification::makeSimpleLValue(); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4225 | } else { |
| 4226 | FromRecordType = From->getType(); |
| 4227 | DestType = ImplicitParamRecordType; |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4228 | FromClassification = From->Classify(Context); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4229 | } |
| 4230 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4231 | // Note that we always use the true parent context when performing |
| 4232 | // the actual argument initialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4233 | ImplicitConversionSequence ICS |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4234 | = TryObjectArgumentInitialization(*this, From->getType(), FromClassification, |
| 4235 | Method, Method->getParent()); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4236 | if (ICS.isBad()) { |
| 4237 | if (ICS.Bad.Kind == BadConversionSequence::bad_qualifiers) { |
| 4238 | Qualifiers FromQs = FromRecordType.getQualifiers(); |
| 4239 | Qualifiers ToQs = DestType.getQualifiers(); |
| 4240 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 4241 | if (CVR) { |
| 4242 | Diag(From->getSourceRange().getBegin(), |
| 4243 | diag::err_member_function_call_bad_cvr) |
| 4244 | << Method->getDeclName() << FromRecordType << (CVR - 1) |
| 4245 | << From->getSourceRange(); |
| 4246 | Diag(Method->getLocation(), diag::note_previous_decl) |
| 4247 | << Method->getDeclName(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4248 | return ExprError(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4249 | } |
| 4250 | } |
| 4251 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4252 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4253 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4254 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 4255 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4256 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4257 | if (ICS.Standard.Second == ICK_Derived_To_Base) { |
| 4258 | ExprResult FromRes = |
| 4259 | PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
| 4260 | if (FromRes.isInvalid()) |
| 4261 | return ExprError(); |
| 4262 | From = FromRes.take(); |
| 4263 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4264 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 4265 | if (!Context.hasSameType(From->getType(), DestType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4266 | From = ImpCastExprToType(From, DestType, CK_NoOp, |
Richard Smith | acdfa4d | 2011-11-10 23:32:36 +0000 | [diff] [blame] | 4267 | From->getValueKind()).take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4268 | return Owned(From); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4269 | } |
| 4270 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4271 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 4272 | /// expression From to bool (C++0x [conv]p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4273 | static ImplicitConversionSequence |
| 4274 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
Douglas Gregor | c6dfe19 | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 4275 | // FIXME: This is pretty broken. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4276 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 4277 | // FIXME: Are these flags correct? |
| 4278 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4279 | /*AllowExplicit=*/true, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4280 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4281 | /*CStyle=*/false, |
| 4282 | /*AllowObjCWritebackConversion=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4283 | } |
| 4284 | |
| 4285 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 4286 | /// of the expression From to bool (C++0x [conv]p3). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4287 | ExprResult Sema::PerformContextuallyConvertToBool(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4288 | if (checkPlaceholderForOverload(*this, From)) |
| 4289 | return ExprError(); |
| 4290 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4291 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4292 | if (!ICS.isBad()) |
| 4293 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4294 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 4295 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4296 | return Diag(From->getSourceRange().getBegin(), |
| 4297 | diag::err_typecheck_bool_condition) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 4298 | << From->getType() << From->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4299 | return ExprError(); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4300 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4301 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 4302 | /// dropPointerConversions - If the given standard conversion sequence |
| 4303 | /// involves any pointer conversions, remove them. This may change |
| 4304 | /// the result type of the conversion sequence. |
| 4305 | static void dropPointerConversion(StandardConversionSequence &SCS) { |
| 4306 | if (SCS.Second == ICK_Pointer_Conversion) { |
| 4307 | SCS.Second = ICK_Identity; |
| 4308 | SCS.Third = ICK_Identity; |
| 4309 | SCS.ToTypePtrs[2] = SCS.ToTypePtrs[1] = SCS.ToTypePtrs[0]; |
| 4310 | } |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 4311 | } |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4312 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 4313 | /// TryContextuallyConvertToObjCPointer - Attempt to contextually |
| 4314 | /// convert the expression From to an Objective-C pointer type. |
| 4315 | static ImplicitConversionSequence |
| 4316 | TryContextuallyConvertToObjCPointer(Sema &S, Expr *From) { |
| 4317 | // Do an implicit conversion to 'id'. |
| 4318 | QualType Ty = S.Context.getObjCIdType(); |
| 4319 | ImplicitConversionSequence ICS |
| 4320 | = TryImplicitConversion(S, From, Ty, |
| 4321 | // FIXME: Are these flags correct? |
| 4322 | /*SuppressUserConversions=*/false, |
| 4323 | /*AllowExplicit=*/true, |
| 4324 | /*InOverloadResolution=*/false, |
| 4325 | /*CStyle=*/false, |
| 4326 | /*AllowObjCWritebackConversion=*/false); |
| 4327 | |
| 4328 | // Strip off any final conversions to 'id'. |
| 4329 | switch (ICS.getKind()) { |
| 4330 | case ImplicitConversionSequence::BadConversion: |
| 4331 | case ImplicitConversionSequence::AmbiguousConversion: |
| 4332 | case ImplicitConversionSequence::EllipsisConversion: |
| 4333 | break; |
| 4334 | |
| 4335 | case ImplicitConversionSequence::UserDefinedConversion: |
| 4336 | dropPointerConversion(ICS.UserDefined.After); |
| 4337 | break; |
| 4338 | |
| 4339 | case ImplicitConversionSequence::StandardConversion: |
| 4340 | dropPointerConversion(ICS.Standard); |
| 4341 | break; |
| 4342 | } |
| 4343 | |
| 4344 | return ICS; |
| 4345 | } |
| 4346 | |
| 4347 | /// PerformContextuallyConvertToObjCPointer - Perform a contextual |
| 4348 | /// conversion of the expression From to an Objective-C pointer type. |
| 4349 | ExprResult Sema::PerformContextuallyConvertToObjCPointer(Expr *From) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4350 | if (checkPlaceholderForOverload(*this, From)) |
| 4351 | return ExprError(); |
| 4352 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4353 | QualType Ty = Context.getObjCIdType(); |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 4354 | ImplicitConversionSequence ICS = |
| 4355 | TryContextuallyConvertToObjCPointer(*this, From); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 4356 | if (!ICS.isBad()) |
| 4357 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4358 | return ExprError(); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 4359 | } |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4360 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4361 | /// \brief Attempt to convert the given expression to an integral or |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4362 | /// enumeration type. |
| 4363 | /// |
| 4364 | /// This routine will attempt to convert an expression of class type to an |
| 4365 | /// integral or enumeration type, if that class type only has a single |
| 4366 | /// conversion to an integral or enumeration type. |
| 4367 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 4368 | /// \param Loc The source location of the construct that requires the |
| 4369 | /// conversion. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4370 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 4371 | /// \param FromE The expression we're converting from. |
| 4372 | /// |
| 4373 | /// \param NotIntDiag The diagnostic to be emitted if the expression does not |
| 4374 | /// have integral or enumeration type. |
| 4375 | /// |
| 4376 | /// \param IncompleteDiag The diagnostic to be emitted if the expression has |
| 4377 | /// incomplete class type. |
| 4378 | /// |
| 4379 | /// \param ExplicitConvDiag The diagnostic to be emitted if we're calling an |
| 4380 | /// explicit conversion function (because no implicit conversion functions |
| 4381 | /// were available). This is a recovery mode. |
| 4382 | /// |
| 4383 | /// \param ExplicitConvNote The note to be emitted with \p ExplicitConvDiag, |
| 4384 | /// showing which conversion was picked. |
| 4385 | /// |
| 4386 | /// \param AmbigDiag The diagnostic to be emitted if there is more than one |
| 4387 | /// conversion function that could convert to integral or enumeration type. |
| 4388 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4389 | /// \param AmbigNote The note to be emitted with \p AmbigDiag for each |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 4390 | /// usable conversion function. |
| 4391 | /// |
| 4392 | /// \param ConvDiag The diagnostic to be emitted if we are calling a conversion |
| 4393 | /// function, which may be an extension in this case. |
| 4394 | /// |
| 4395 | /// \returns The expression, converted to an integral or enumeration type if |
| 4396 | /// successful. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4397 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4398 | Sema::ConvertToIntegralOrEnumerationType(SourceLocation Loc, Expr *From, |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4399 | const PartialDiagnostic &NotIntDiag, |
| 4400 | const PartialDiagnostic &IncompleteDiag, |
| 4401 | const PartialDiagnostic &ExplicitConvDiag, |
| 4402 | const PartialDiagnostic &ExplicitConvNote, |
| 4403 | const PartialDiagnostic &AmbigDiag, |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 4404 | const PartialDiagnostic &AmbigNote, |
| 4405 | const PartialDiagnostic &ConvDiag) { |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4406 | // We can't perform any more checking for type-dependent expressions. |
| 4407 | if (From->isTypeDependent()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4408 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4409 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4410 | // If the expression already has integral or enumeration type, we're golden. |
| 4411 | QualType T = From->getType(); |
| 4412 | if (T->isIntegralOrEnumerationType()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4413 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4414 | |
| 4415 | // FIXME: Check for missing '()' if T is a function type? |
| 4416 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4417 | // 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] | 4418 | // expression of integral or enumeration type. |
| 4419 | const RecordType *RecordTy = T->getAs<RecordType>(); |
| 4420 | if (!RecordTy || !getLangOptions().CPlusPlus) { |
| 4421 | Diag(Loc, NotIntDiag) |
| 4422 | << T << From->getSourceRange(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4423 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4424 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4425 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4426 | // We must have a complete class type. |
| 4427 | if (RequireCompleteType(Loc, T, IncompleteDiag)) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4428 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4429 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4430 | // Look for a conversion to an integral or enumeration type. |
| 4431 | UnresolvedSet<4> ViableConversions; |
| 4432 | UnresolvedSet<4> ExplicitConversions; |
| 4433 | const UnresolvedSetImpl *Conversions |
| 4434 | = cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4435 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4436 | bool HadMultipleCandidates = (Conversions->size() > 1); |
| 4437 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4438 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4439 | E = Conversions->end(); |
| 4440 | I != E; |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4441 | ++I) { |
| 4442 | if (CXXConversionDecl *Conversion |
| 4443 | = dyn_cast<CXXConversionDecl>((*I)->getUnderlyingDecl())) |
| 4444 | if (Conversion->getConversionType().getNonReferenceType() |
| 4445 | ->isIntegralOrEnumerationType()) { |
| 4446 | if (Conversion->isExplicit()) |
| 4447 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
| 4448 | else |
| 4449 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
| 4450 | } |
| 4451 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4452 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4453 | switch (ViableConversions.size()) { |
| 4454 | case 0: |
| 4455 | if (ExplicitConversions.size() == 1) { |
| 4456 | DeclAccessPair Found = ExplicitConversions[0]; |
| 4457 | CXXConversionDecl *Conversion |
| 4458 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4459 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4460 | // The user probably meant to invoke the given explicit |
| 4461 | // conversion; use it. |
| 4462 | QualType ConvTy |
| 4463 | = Conversion->getConversionType().getNonReferenceType(); |
| 4464 | std::string TypeStr; |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 4465 | ConvTy.getAsStringInternal(TypeStr, getPrintingPolicy()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4466 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4467 | Diag(Loc, ExplicitConvDiag) |
| 4468 | << T << ConvTy |
| 4469 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 4470 | "static_cast<" + TypeStr + ">(") |
| 4471 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(From->getLocEnd()), |
| 4472 | ")"); |
| 4473 | Diag(Conversion->getLocation(), ExplicitConvNote) |
| 4474 | << ConvTy->isEnumeralType() << ConvTy; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4475 | |
| 4476 | // 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] | 4477 | // explicit conversion function. |
| 4478 | if (isSFINAEContext()) |
| 4479 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4480 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4481 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4482 | ExprResult Result = BuildCXXMemberCallExpr(From, Found, Conversion, |
| 4483 | HadMultipleCandidates); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 4484 | if (Result.isInvalid()) |
| 4485 | return ExprError(); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 4486 | // Record usage of conversion in an implicit cast. |
| 4487 | From = ImplicitCastExpr::Create(Context, Result.get()->getType(), |
| 4488 | CK_UserDefinedConversion, |
| 4489 | Result.get(), 0, |
| 4490 | Result.get()->getValueKind()); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4491 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4492 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4493 | // We'll complain below about a non-integral condition type. |
| 4494 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4495 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4496 | case 1: { |
| 4497 | // Apply this conversion. |
| 4498 | DeclAccessPair Found = ViableConversions[0]; |
| 4499 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4500 | |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 4501 | CXXConversionDecl *Conversion |
| 4502 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 4503 | QualType ConvTy |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4504 | = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 4505 | if (ConvDiag.getDiagID()) { |
| 4506 | if (isSFINAEContext()) |
| 4507 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4508 | |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 4509 | Diag(Loc, ConvDiag) |
| 4510 | << T << ConvTy->isEnumeralType() << ConvTy << From->getSourceRange(); |
| 4511 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4512 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4513 | ExprResult Result = BuildCXXMemberCallExpr(From, Found, Conversion, |
| 4514 | HadMultipleCandidates); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 4515 | if (Result.isInvalid()) |
| 4516 | return ExprError(); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 4517 | // Record usage of conversion in an implicit cast. |
| 4518 | From = ImplicitCastExpr::Create(Context, Result.get()->getType(), |
| 4519 | CK_UserDefinedConversion, |
| 4520 | Result.get(), 0, |
| 4521 | Result.get()->getValueKind()); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4522 | break; |
| 4523 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4524 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4525 | default: |
| 4526 | Diag(Loc, AmbigDiag) |
| 4527 | << T << From->getSourceRange(); |
| 4528 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 4529 | CXXConversionDecl *Conv |
| 4530 | = cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 4531 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 4532 | Diag(Conv->getLocation(), AmbigNote) |
| 4533 | << ConvTy->isEnumeralType() << ConvTy; |
| 4534 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4535 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4536 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4537 | |
Douglas Gregor | acb0bd8 | 2010-06-29 23:25:20 +0000 | [diff] [blame] | 4538 | if (!From->getType()->isIntegralOrEnumerationType()) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4539 | Diag(Loc, NotIntDiag) |
| 4540 | << From->getType() << From->getSourceRange(); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4541 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4542 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4543 | } |
| 4544 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4545 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4546 | /// candidate functions, using the given function call arguments. If |
| 4547 | /// @p SuppressUserConversions, then don't allow user-defined |
| 4548 | /// conversions via constructors or conversion operators. |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4549 | /// |
| 4550 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 4551 | /// based on an incomplete set of function arguments. This feature is used by |
| 4552 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4553 | void |
| 4554 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4555 | DeclAccessPair FoundDecl, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4556 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4557 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 4558 | bool SuppressUserConversions, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4559 | bool PartialOverloading) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4560 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4561 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4562 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4563 | assert(!Function->getDescribedFunctionTemplate() && |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4564 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4565 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4566 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4567 | if (!isa<CXXConstructorDecl>(Method)) { |
| 4568 | // If we get here, it's because we're calling a member function |
| 4569 | // that is named without a member access expression (e.g., |
| 4570 | // "this->f") that was either written explicitly or created |
| 4571 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4572 | // function, e.g., X::f(). We use an empty type for the implied |
| 4573 | // object argument (C++ [over.call.func]p3), and the acting context |
| 4574 | // is irrelevant. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4575 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4576 | QualType(), Expr::Classification::makeSimpleLValue(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4577 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4578 | SuppressUserConversions); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4579 | return; |
| 4580 | } |
| 4581 | // We treat a constructor like a non-member function, since its object |
| 4582 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4583 | } |
| 4584 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 4585 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4586 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 4587 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4588 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4589 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4590 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 4591 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 4592 | // C++ [class.copy]p3: |
| 4593 | // A member function template is never instantiated to perform the copy |
| 4594 | // of a class object to an object of its class type. |
| 4595 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4596 | if (NumArgs == 1 && |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4597 | Constructor->isSpecializationCopyingObject() && |
Douglas Gregor | 1211606 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 4598 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 4599 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 4600 | return; |
| 4601 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4602 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4603 | // Add this candidate |
| 4604 | CandidateSet.push_back(OverloadCandidate()); |
| 4605 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4606 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4607 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4608 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4609 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4610 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4611 | Candidate.ExplicitCallArguments = NumArgs; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4612 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4613 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4614 | |
| 4615 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 4616 | // parameters is viable only if it has an ellipsis in its parameter |
| 4617 | // list (8.3.5). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4618 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 4619 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4620 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4621 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4622 | return; |
| 4623 | } |
| 4624 | |
| 4625 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 4626 | // is viable only if the (m+1)st parameter has a default argument |
| 4627 | // (8.3.6). For the purposes of overload resolution, the |
| 4628 | // parameter list is truncated on the right, so that there are |
| 4629 | // exactly m parameters. |
| 4630 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4631 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4632 | // Not enough arguments. |
| 4633 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4634 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4635 | return; |
| 4636 | } |
| 4637 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 4638 | // (CUDA B.1): Check for invalid calls between targets. |
| 4639 | if (getLangOptions().CUDA) |
| 4640 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
| 4641 | if (CheckCUDATarget(Caller, Function)) { |
| 4642 | Candidate.Viable = false; |
| 4643 | Candidate.FailureKind = ovl_fail_bad_target; |
| 4644 | return; |
| 4645 | } |
| 4646 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4647 | // Determine the implicit conversion sequences for each of the |
| 4648 | // arguments. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4649 | Candidate.Conversions.resize(NumArgs); |
| 4650 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 4651 | if (ArgIdx < NumArgsInProto) { |
| 4652 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 4653 | // exist for each argument an implicit conversion sequence |
| 4654 | // (13.3.3.1) that converts that argument to the corresponding |
| 4655 | // parameter of F. |
| 4656 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4657 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4658 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4659 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4660 | /*InOverloadResolution=*/true, |
| 4661 | /*AllowObjCWritebackConversion=*/ |
| 4662 | getLangOptions().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4663 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 4664 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4665 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4666 | break; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4667 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4668 | } else { |
| 4669 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 4670 | // argument for which there is no corresponding parameter is |
| 4671 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4672 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4673 | } |
| 4674 | } |
| 4675 | } |
| 4676 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4677 | /// \brief Add all of the function declarations in the given function set to |
| 4678 | /// the overload canddiate set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4679 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4680 | Expr **Args, unsigned NumArgs, |
| 4681 | OverloadCandidateSet& CandidateSet, |
| 4682 | bool SuppressUserConversions) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4683 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4684 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 4685 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4686 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4687 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4688 | cast<CXXMethodDecl>(FD)->getParent(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4689 | Args[0]->getType(), Args[0]->Classify(Context), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4690 | Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4691 | CandidateSet, SuppressUserConversions); |
| 4692 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4693 | AddOverloadCandidate(FD, F.getPair(), Args, NumArgs, CandidateSet, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4694 | SuppressUserConversions); |
| 4695 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4696 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4697 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 4698 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4699 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4700 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4701 | /*FIXME: explicit args */ 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4702 | Args[0]->getType(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4703 | Args[0]->Classify(Context), |
| 4704 | Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4705 | CandidateSet, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4706 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4707 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4708 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4709 | /*FIXME: explicit args */ 0, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4710 | Args, NumArgs, CandidateSet, |
| 4711 | SuppressUserConversions); |
| 4712 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4713 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4714 | } |
| 4715 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4716 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 4717 | /// method) as a method candidate to the given overload set. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4718 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4719 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4720 | Expr::Classification ObjectClassification, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4721 | Expr **Args, unsigned NumArgs, |
| 4722 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4723 | bool SuppressUserConversions) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4724 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4725 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4726 | |
| 4727 | if (isa<UsingShadowDecl>(Decl)) |
| 4728 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4729 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4730 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 4731 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 4732 | "Expected a member function template"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4733 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 4734 | /*ExplicitArgs*/ 0, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4735 | ObjectType, ObjectClassification, Args, NumArgs, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4736 | CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4737 | SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4738 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4739 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4740 | ObjectType, ObjectClassification, Args, NumArgs, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4741 | CandidateSet, SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4742 | } |
| 4743 | } |
| 4744 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4745 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 4746 | /// of candidate functions, using the given function call arguments |
| 4747 | /// and the object argument (@c Object). For example, in a call |
| 4748 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 4749 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 4750 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4751 | /// operators. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4752 | void |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4753 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4754 | CXXRecordDecl *ActingContext, QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4755 | Expr::Classification ObjectClassification, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4756 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4757 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4758 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4759 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4760 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4761 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4762 | assert(!isa<CXXConstructorDecl>(Method) && |
| 4763 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4764 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4765 | if (!CandidateSet.isNewCandidate(Method)) |
| 4766 | return; |
| 4767 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4768 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4769 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4770 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4771 | // Add this candidate |
| 4772 | CandidateSet.push_back(OverloadCandidate()); |
| 4773 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4774 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4775 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4776 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4777 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4778 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4779 | |
| 4780 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4781 | |
| 4782 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 4783 | // parameters is viable only if it has an ellipsis in its parameter |
| 4784 | // list (8.3.5). |
| 4785 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 4786 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4787 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4788 | return; |
| 4789 | } |
| 4790 | |
| 4791 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 4792 | // is viable only if the (m+1)st parameter has a default argument |
| 4793 | // (8.3.6). For the purposes of overload resolution, the |
| 4794 | // parameter list is truncated on the right, so that there are |
| 4795 | // exactly m parameters. |
| 4796 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 4797 | if (NumArgs < MinRequiredArgs) { |
| 4798 | // Not enough arguments. |
| 4799 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4800 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4801 | return; |
| 4802 | } |
| 4803 | |
| 4804 | Candidate.Viable = true; |
| 4805 | Candidate.Conversions.resize(NumArgs + 1); |
| 4806 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4807 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4808 | // The implicit object argument is ignored. |
| 4809 | Candidate.IgnoreObjectArgument = true; |
| 4810 | else { |
| 4811 | // Determine the implicit conversion sequence for the object |
| 4812 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4813 | Candidate.Conversions[0] |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4814 | = TryObjectArgumentInitialization(*this, ObjectType, ObjectClassification, |
| 4815 | Method, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4816 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4817 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4818 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4819 | return; |
| 4820 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4821 | } |
| 4822 | |
| 4823 | // Determine the implicit conversion sequences for each of the |
| 4824 | // arguments. |
| 4825 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 4826 | if (ArgIdx < NumArgsInProto) { |
| 4827 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 4828 | // exist for each argument an implicit conversion sequence |
| 4829 | // (13.3.3.1) that converts that argument to the corresponding |
| 4830 | // parameter of F. |
| 4831 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4832 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4833 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4834 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4835 | /*InOverloadResolution=*/true, |
| 4836 | /*AllowObjCWritebackConversion=*/ |
| 4837 | getLangOptions().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4838 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4839 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4840 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4841 | break; |
| 4842 | } |
| 4843 | } else { |
| 4844 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 4845 | // argument for which there is no corresponding parameter is |
| 4846 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4847 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4848 | } |
| 4849 | } |
| 4850 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4851 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4852 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 4853 | /// set, using template argument deduction to produce an appropriate member |
| 4854 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4855 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4856 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4857 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4858 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4859 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4860 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4861 | Expr::Classification ObjectClassification, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4862 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4863 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4864 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4865 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 4866 | return; |
| 4867 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4868 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4869 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4870 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4871 | // 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] | 4872 | // candidate functions in the usual way.113) A given name can refer to one |
| 4873 | // or more function templates and also to a set of overloaded non-template |
| 4874 | // functions. In such a case, the candidate functions generated from each |
| 4875 | // function template are combined with the set of non-template candidate |
| 4876 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4877 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4878 | FunctionDecl *Specialization = 0; |
| 4879 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4880 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4881 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4882 | CandidateSet.push_back(OverloadCandidate()); |
| 4883 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 4884 | Candidate.FoundDecl = FoundDecl; |
| 4885 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 4886 | Candidate.Viable = false; |
| 4887 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 4888 | Candidate.IsSurrogate = false; |
| 4889 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4890 | Candidate.ExplicitCallArguments = NumArgs; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4891 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4892 | Info); |
| 4893 | return; |
| 4894 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4895 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4896 | // Add the function template specialization produced by template argument |
| 4897 | // deduction as a candidate. |
| 4898 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4899 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4900 | "Specialization is not a member function?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4901 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4902 | ActingContext, ObjectType, ObjectClassification, |
| 4903 | Args, NumArgs, CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4906 | /// \brief Add a C++ function template specialization as a candidate |
| 4907 | /// in the candidate set, using template argument deduction to produce |
| 4908 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4909 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4910 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4911 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4912 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4913 | Expr **Args, unsigned NumArgs, |
| 4914 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4915 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4916 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 4917 | return; |
| 4918 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4919 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4920 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4921 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4922 | // 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] | 4923 | // candidate functions in the usual way.113) A given name can refer to one |
| 4924 | // or more function templates and also to a set of overloaded non-template |
| 4925 | // functions. In such a case, the candidate functions generated from each |
| 4926 | // function template are combined with the set of non-template candidate |
| 4927 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4928 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4929 | FunctionDecl *Specialization = 0; |
| 4930 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4931 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4932 | Args, NumArgs, Specialization, Info)) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4933 | CandidateSet.push_back(OverloadCandidate()); |
| 4934 | OverloadCandidate &Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4935 | Candidate.FoundDecl = FoundDecl; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4936 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 4937 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4938 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4939 | Candidate.IsSurrogate = false; |
| 4940 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4941 | Candidate.ExplicitCallArguments = NumArgs; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4942 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4943 | Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4944 | return; |
| 4945 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4946 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4947 | // Add the function template specialization produced by template argument |
| 4948 | // deduction as a candidate. |
| 4949 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4950 | AddOverloadCandidate(Specialization, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4951 | SuppressUserConversions); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4952 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4953 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4954 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4955 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4956 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4957 | /// 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] | 4958 | /// (which may or may not be the same type as the type that the |
| 4959 | /// conversion function produces). |
| 4960 | void |
| 4961 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4962 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4963 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4964 | Expr *From, QualType ToType, |
| 4965 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4966 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 4967 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4968 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4969 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 4970 | return; |
| 4971 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4972 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4973 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4974 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4975 | // Add this candidate |
| 4976 | CandidateSet.push_back(OverloadCandidate()); |
| 4977 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4978 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4979 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4980 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4981 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4982 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4983 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 4984 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4985 | Candidate.Viable = true; |
| 4986 | Candidate.Conversions.resize(1); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4987 | Candidate.ExplicitCallArguments = 1; |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4988 | |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 4989 | // C++ [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4990 | // For conversion functions, the function is considered to be a member of |
| 4991 | // the class of the implicit implied object argument for the purpose of |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 4992 | // defining the type of the implicit object parameter. |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4993 | // |
| 4994 | // Determine the implicit conversion sequence for the implicit |
| 4995 | // object parameter. |
| 4996 | QualType ImplicitParamType = From->getType(); |
| 4997 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
| 4998 | ImplicitParamType = FromPtrType->getPointeeType(); |
| 4999 | CXXRecordDecl *ConversionContext |
| 5000 | = cast<CXXRecordDecl>(ImplicitParamType->getAs<RecordType>()->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5001 | |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5002 | Candidate.Conversions[0] |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5003 | = TryObjectArgumentInitialization(*this, From->getType(), |
| 5004 | From->Classify(Context), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5005 | Conversion, ConversionContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5006 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5007 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5008 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5009 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5010 | return; |
| 5011 | } |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 5012 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5013 | // 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] | 5014 | // derived to base as such conversions are given Conversion Rank. They only |
| 5015 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 5016 | QualType FromCanon |
| 5017 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 5018 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 5019 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 5020 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5021 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 5022 | return; |
| 5023 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5024 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5025 | // To determine what the conversion from the result of calling the |
| 5026 | // conversion function to the type we're eventually trying to |
| 5027 | // convert to (ToType), we need to synthesize a call to the |
| 5028 | // conversion function and attempt copy initialization from it. This |
| 5029 | // makes sure that we get the right semantics with respect to |
| 5030 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 5031 | // call on the stack and we don't need its arguments to be |
| 5032 | // well-formed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5033 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5034 | VK_LValue, From->getLocStart()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5035 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 5036 | Context.getPointerType(Conversion->getType()), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5037 | CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5038 | &ConversionRef, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5039 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5040 | QualType ConversionType = Conversion->getConversionType(); |
| 5041 | if (RequireCompleteType(From->getLocStart(), ConversionType, 0)) { |
Douglas Gregor | 7d14d38 | 2010-11-13 19:36:57 +0000 | [diff] [blame] | 5042 | Candidate.Viable = false; |
| 5043 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 5044 | return; |
| 5045 | } |
| 5046 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5047 | ExprValueKind VK = Expr::getValueKindForType(ConversionType); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5048 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5049 | // 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] | 5050 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 5051 | // allocator). |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 5052 | QualType CallResultType = ConversionType.getNonLValueExprType(Context); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5053 | CallExpr Call(Context, &ConversionFn, 0, 0, CallResultType, VK, |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 5054 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5055 | ImplicitConversionSequence ICS = |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5056 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 5057 | /*SuppressUserConversions=*/true, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5058 | /*InOverloadResolution=*/false, |
| 5059 | /*AllowObjCWritebackConversion=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5060 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5061 | switch (ICS.getKind()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5062 | case ImplicitConversionSequence::StandardConversion: |
| 5063 | Candidate.FinalConversion = ICS.Standard; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5064 | |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5065 | // C++ [over.ics.user]p3: |
| 5066 | // If the user-defined conversion is specified by a specialization of a |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5067 | // conversion function template, the second standard conversion sequence |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5068 | // shall have exact match rank. |
| 5069 | if (Conversion->getPrimaryTemplate() && |
| 5070 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 5071 | Candidate.Viable = false; |
| 5072 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 5073 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5074 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 5075 | // C++0x [dcl.init.ref]p5: |
| 5076 | // In the second case, if the reference is an rvalue reference and |
| 5077 | // the second standard conversion sequence of the user-defined |
| 5078 | // conversion sequence includes an lvalue-to-rvalue conversion, the |
| 5079 | // program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5080 | if (ToType->isRValueReferenceType() && |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 5081 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 5082 | Candidate.Viable = false; |
| 5083 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 5084 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5085 | break; |
| 5086 | |
| 5087 | case ImplicitConversionSequence::BadConversion: |
| 5088 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5089 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5090 | break; |
| 5091 | |
| 5092 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5093 | llvm_unreachable( |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5094 | "Can only end up with a standard conversion sequence or failure"); |
| 5095 | } |
| 5096 | } |
| 5097 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5098 | /// \brief Adds a conversion function template specialization |
| 5099 | /// candidate to the overload set, using template argument deduction |
| 5100 | /// to deduce the template arguments of the conversion function |
| 5101 | /// template from the type that we are converting to (C++ |
| 5102 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5103 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5104 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5105 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5106 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5107 | Expr *From, QualType ToType, |
| 5108 | OverloadCandidateSet &CandidateSet) { |
| 5109 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 5110 | "Only conversion function templates permitted here"); |
| 5111 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5112 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 5113 | return; |
| 5114 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5115 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5116 | CXXConversionDecl *Specialization = 0; |
| 5117 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5118 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5119 | Specialization, Info)) { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5120 | CandidateSet.push_back(OverloadCandidate()); |
| 5121 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 5122 | Candidate.FoundDecl = FoundDecl; |
| 5123 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 5124 | Candidate.Viable = false; |
| 5125 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 5126 | Candidate.IsSurrogate = false; |
| 5127 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5128 | Candidate.ExplicitCallArguments = 1; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5129 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 5130 | Info); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5131 | return; |
| 5132 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5133 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5134 | // Add the conversion function template specialization produced by |
| 5135 | // template argument deduction as a candidate. |
| 5136 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5137 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5138 | CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5139 | } |
| 5140 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5141 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 5142 | /// converts the given @c Object to a function pointer via the |
| 5143 | /// conversion function @c Conversion, and then attempts to call it |
| 5144 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 5145 | /// the type of function that we'll eventually be calling. |
| 5146 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5147 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5148 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5149 | const FunctionProtoType *Proto, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5150 | Expr *Object, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5151 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5152 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 5153 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 5154 | return; |
| 5155 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5156 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5157 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5158 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5159 | CandidateSet.push_back(OverloadCandidate()); |
| 5160 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5161 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5162 | Candidate.Function = 0; |
| 5163 | Candidate.Surrogate = Conversion; |
| 5164 | Candidate.Viable = true; |
| 5165 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5166 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5167 | Candidate.Conversions.resize(NumArgs + 1); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5168 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5169 | |
| 5170 | // Determine the implicit conversion sequence for the implicit |
| 5171 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5172 | ImplicitConversionSequence ObjectInit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5173 | = TryObjectArgumentInitialization(*this, Object->getType(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5174 | Object->Classify(Context), |
| 5175 | Conversion, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5176 | if (ObjectInit.isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5177 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5178 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5179 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5180 | return; |
| 5181 | } |
| 5182 | |
| 5183 | // The first conversion is actually a user-defined conversion whose |
| 5184 | // first conversion is ObjectInit's standard conversion (which is |
| 5185 | // effectively a reference binding). Record it as such. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5186 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5187 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 5188 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5189 | Candidate.Conversions[0].UserDefined.HadMultipleCandidates = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5190 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 5191 | Candidate.Conversions[0].UserDefined.FoundConversionFunction = FoundDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5192 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5193 | = Candidate.Conversions[0].UserDefined.Before; |
| 5194 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 5195 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5196 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5197 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5198 | |
| 5199 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 5200 | // parameters is viable only if it has an ellipsis in its parameter |
| 5201 | // list (8.3.5). |
| 5202 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 5203 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5204 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5205 | return; |
| 5206 | } |
| 5207 | |
| 5208 | // Function types don't have any default arguments, so just check if |
| 5209 | // we have enough arguments. |
| 5210 | if (NumArgs < NumArgsInProto) { |
| 5211 | // Not enough arguments. |
| 5212 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5213 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5214 | return; |
| 5215 | } |
| 5216 | |
| 5217 | // Determine the implicit conversion sequences for each of the |
| 5218 | // arguments. |
| 5219 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 5220 | if (ArgIdx < NumArgsInProto) { |
| 5221 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 5222 | // exist for each argument an implicit conversion sequence |
| 5223 | // (13.3.3.1) that converts that argument to the corresponding |
| 5224 | // parameter of F. |
| 5225 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5226 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5227 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 5228 | /*SuppressUserConversions=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5229 | /*InOverloadResolution=*/false, |
| 5230 | /*AllowObjCWritebackConversion=*/ |
| 5231 | getLangOptions().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5232 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5233 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5234 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5235 | break; |
| 5236 | } |
| 5237 | } else { |
| 5238 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 5239 | // argument for which there is no corresponding parameter is |
| 5240 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5241 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5242 | } |
| 5243 | } |
| 5244 | } |
| 5245 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5246 | /// \brief Add overload candidates for overloaded operators that are |
| 5247 | /// member functions. |
| 5248 | /// |
| 5249 | /// Add the overloaded operator candidates that are member functions |
| 5250 | /// for the operator Op that was used in an operator expression such |
| 5251 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 5252 | /// CandidateSet will store the added overload candidates. (C++ |
| 5253 | /// [over.match.oper]). |
| 5254 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 5255 | SourceLocation OpLoc, |
| 5256 | Expr **Args, unsigned NumArgs, |
| 5257 | OverloadCandidateSet& CandidateSet, |
| 5258 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5259 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5260 | |
| 5261 | // C++ [over.match.oper]p3: |
| 5262 | // For a unary operator @ with an operand of a type whose |
| 5263 | // cv-unqualified version is T1, and for a binary operator @ with |
| 5264 | // a left operand of a type whose cv-unqualified version is T1 and |
| 5265 | // a right operand of a type whose cv-unqualified version is T2, |
| 5266 | // three sets of candidate functions, designated member |
| 5267 | // candidates, non-member candidates and built-in candidates, are |
| 5268 | // constructed as follows: |
| 5269 | QualType T1 = Args[0]->getType(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5270 | |
| 5271 | // -- If T1 is a class type, the set of member candidates is the |
| 5272 | // result of the qualified lookup of T1::operator@ |
| 5273 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 5274 | // empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5275 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 5276 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 5277 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 5278 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5279 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5280 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 5281 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 5282 | Operators.suppressDiagnostics(); |
| 5283 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5284 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 5285 | OperEnd = Operators.end(); |
| 5286 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5287 | ++Oper) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5288 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5289 | Args[0]->Classify(Context), Args + 1, NumArgs - 1, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5290 | CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5291 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5292 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5293 | } |
| 5294 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5295 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 5296 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 5297 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 5298 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 5299 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5300 | /// operator. NumContextualBoolArguments is the number of arguments |
| 5301 | /// (at the beginning of the argument list) that will be contextually |
| 5302 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5303 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5304 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 5305 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5306 | bool IsAssignmentOperator, |
| 5307 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5308 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5309 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5310 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5311 | // Add this candidate |
| 5312 | CandidateSet.push_back(OverloadCandidate()); |
| 5313 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5314 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5315 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 5316 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5317 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5318 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 5319 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 5320 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 5321 | |
| 5322 | // Determine the implicit conversion sequences for each of the |
| 5323 | // arguments. |
| 5324 | Candidate.Viable = true; |
| 5325 | Candidate.Conversions.resize(NumArgs); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 5326 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5327 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 5328 | // C++ [over.match.oper]p4: |
| 5329 | // For the built-in assignment operators, conversions of the |
| 5330 | // left operand are restricted as follows: |
| 5331 | // -- no temporaries are introduced to hold the left operand, and |
| 5332 | // -- no user-defined conversions are applied to the left |
| 5333 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5334 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 5335 | // |
| 5336 | // We block these conversions by turning off user-defined |
| 5337 | // conversions, since that is the only way that initialization of |
| 5338 | // a reference to a non-class type can occur from something that |
| 5339 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5340 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5341 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5342 | "Contextual conversion to bool requires bool type"); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5343 | Candidate.Conversions[ArgIdx] |
| 5344 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5345 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5346 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5347 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 5348 | ArgIdx == 0 && IsAssignmentOperator, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5349 | /*InOverloadResolution=*/false, |
| 5350 | /*AllowObjCWritebackConversion=*/ |
| 5351 | getLangOptions().ObjCAutoRefCount); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5352 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5353 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5354 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5355 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 5356 | break; |
| 5357 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5358 | } |
| 5359 | } |
| 5360 | |
| 5361 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 5362 | /// candidate operator functions for built-in operators (C++ |
| 5363 | /// [over.built]). The types are separated into pointer types and |
| 5364 | /// enumeration types. |
| 5365 | class BuiltinCandidateTypeSet { |
| 5366 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 5367 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5368 | |
| 5369 | /// PointerTypes - The set of pointer types that will be used in the |
| 5370 | /// built-in candidates. |
| 5371 | TypeSet PointerTypes; |
| 5372 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5373 | /// MemberPointerTypes - The set of member pointer types that will be |
| 5374 | /// used in the built-in candidates. |
| 5375 | TypeSet MemberPointerTypes; |
| 5376 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5377 | /// EnumerationTypes - The set of enumeration types that will be |
| 5378 | /// used in the built-in candidates. |
| 5379 | TypeSet EnumerationTypes; |
| 5380 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5381 | /// \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] | 5382 | /// candidates. |
| 5383 | TypeSet VectorTypes; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5384 | |
| 5385 | /// \brief A flag indicating non-record types are viable candidates |
| 5386 | bool HasNonRecordTypes; |
| 5387 | |
| 5388 | /// \brief A flag indicating whether either arithmetic or enumeration types |
| 5389 | /// were present in the candidate set. |
| 5390 | bool HasArithmeticOrEnumeralTypes; |
| 5391 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 5392 | /// \brief A flag indicating whether the nullptr type was present in the |
| 5393 | /// candidate set. |
| 5394 | bool HasNullPtrType; |
| 5395 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 5396 | /// Sema - The semantic analysis instance where we are building the |
| 5397 | /// candidate type set. |
| 5398 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5399 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5400 | /// Context - The AST context in which we will build the type sets. |
| 5401 | ASTContext &Context; |
| 5402 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 5403 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 5404 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5405 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5406 | |
| 5407 | public: |
| 5408 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 5409 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5410 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5411 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5412 | : HasNonRecordTypes(false), |
| 5413 | HasArithmeticOrEnumeralTypes(false), |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 5414 | HasNullPtrType(false), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5415 | SemaRef(SemaRef), |
| 5416 | Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5417 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5418 | void AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5419 | SourceLocation Loc, |
| 5420 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5421 | bool AllowExplicitConversions, |
| 5422 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5423 | |
| 5424 | /// pointer_begin - First pointer type found; |
| 5425 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 5426 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5427 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5428 | iterator pointer_end() { return PointerTypes.end(); } |
| 5429 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5430 | /// member_pointer_begin - First member pointer type found; |
| 5431 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 5432 | |
| 5433 | /// member_pointer_end - Past the last member pointer type found; |
| 5434 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 5435 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5436 | /// enumeration_begin - First enumeration type found; |
| 5437 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 5438 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5439 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5440 | iterator enumeration_end() { return EnumerationTypes.end(); } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5441 | |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 5442 | iterator vector_begin() { return VectorTypes.begin(); } |
| 5443 | iterator vector_end() { return VectorTypes.end(); } |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5444 | |
| 5445 | bool hasNonRecordTypes() { return HasNonRecordTypes; } |
| 5446 | bool hasArithmeticOrEnumeralTypes() { return HasArithmeticOrEnumeralTypes; } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 5447 | bool hasNullPtrType() const { return HasNullPtrType; } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5448 | }; |
| 5449 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5450 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5451 | /// the set of pointer types along with any more-qualified variants of |
| 5452 | /// that type. For example, if @p Ty is "int const *", this routine |
| 5453 | /// will add "int const *", "int const volatile *", "int const |
| 5454 | /// restrict *", and "int const volatile restrict *" to the set of |
| 5455 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 5456 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5457 | /// |
| 5458 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5459 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5460 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 5461 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5462 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5463 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 5464 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5465 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5466 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 5467 | QualType PointeeTy; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5468 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 5469 | bool buildObjCPtr = false; |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 5470 | if (!PointerTy) { |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 5471 | if (const ObjCObjectPointerType *PTy = Ty->getAs<ObjCObjectPointerType>()) { |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 5472 | PointeeTy = PTy->getPointeeType(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 5473 | buildObjCPtr = true; |
| 5474 | } |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 5475 | else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5476 | llvm_unreachable("type was not a pointer type!"); |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 5477 | } |
| 5478 | else |
| 5479 | PointeeTy = PointerTy->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5480 | |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 5481 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 5482 | // (the qualifier would sink to the element type), and for another, the |
| 5483 | // only overload situation where it matters is subscript or pointer +- int, |
| 5484 | // and those shouldn't have qualifier variants anyway. |
| 5485 | if (PointeeTy->isArrayType()) |
| 5486 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5487 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 5488 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | d411b3f | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 5489 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 5490 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 5491 | bool hasRestrict = VisibleQuals.hasRestrict(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5492 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5493 | // Iterate through all strict supersets of BaseCVR. |
| 5494 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 5495 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 5496 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 5497 | // in the types. |
| 5498 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 5499 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5500 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 5501 | if (!buildObjCPtr) |
| 5502 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
| 5503 | else |
| 5504 | PointerTypes.insert(Context.getObjCObjectPointerType(QPointeeTy)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5505 | } |
| 5506 | |
| 5507 | return true; |
| 5508 | } |
| 5509 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5510 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 5511 | /// to the set of pointer types along with any more-qualified variants of |
| 5512 | /// that type. For example, if @p Ty is "int const *", this routine |
| 5513 | /// will add "int const *", "int const volatile *", "int const |
| 5514 | /// restrict *", and "int const volatile restrict *" to the set of |
| 5515 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 5516 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5517 | /// |
| 5518 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5519 | bool |
| 5520 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 5521 | QualType Ty) { |
| 5522 | // Insert this type. |
| 5523 | if (!MemberPointerTypes.insert(Ty)) |
| 5524 | return false; |
| 5525 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5526 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 5527 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5528 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5529 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 5530 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 5531 | // (the qualifier would sink to the element type), and for another, the |
| 5532 | // only overload situation where it matters is subscript or pointer +- int, |
| 5533 | // and those shouldn't have qualifier variants anyway. |
| 5534 | if (PointeeTy->isArrayType()) |
| 5535 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5536 | const Type *ClassTy = PointerTy->getClass(); |
| 5537 | |
| 5538 | // Iterate through all strict supersets of the pointee type's CVR |
| 5539 | // qualifiers. |
| 5540 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 5541 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 5542 | if ((CVR | BaseCVR) != CVR) continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5543 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5544 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 5545 | MemberPointerTypes.insert( |
| 5546 | Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5547 | } |
| 5548 | |
| 5549 | return true; |
| 5550 | } |
| 5551 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5552 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 5553 | /// 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] | 5554 | /// primarily interested in pointer types and enumeration types. We also |
| 5555 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5556 | /// AllowUserConversions is true if we should look at the conversion |
| 5557 | /// functions of a class type, and AllowExplicitConversions if we |
| 5558 | /// should also include the explicit conversion functions of a class |
| 5559 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5560 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5561 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5562 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5563 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5564 | bool AllowExplicitConversions, |
| 5565 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5566 | // Only deal with canonical types. |
| 5567 | Ty = Context.getCanonicalType(Ty); |
| 5568 | |
| 5569 | // Look through reference types; they aren't part of the type of an |
| 5570 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5571 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5572 | Ty = RefTy->getPointeeType(); |
| 5573 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5574 | // If we're dealing with an array type, decay to the pointer. |
| 5575 | if (Ty->isArrayType()) |
| 5576 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 5577 | |
| 5578 | // Otherwise, we don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5579 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5580 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5581 | // Flag if we ever add a non-record type. |
| 5582 | const RecordType *TyRec = Ty->getAs<RecordType>(); |
| 5583 | HasNonRecordTypes = HasNonRecordTypes || !TyRec; |
| 5584 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5585 | // Flag if we encounter an arithmetic type. |
| 5586 | HasArithmeticOrEnumeralTypes = |
| 5587 | HasArithmeticOrEnumeralTypes || Ty->isArithmeticType(); |
| 5588 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 5589 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
| 5590 | PointerTypes.insert(Ty); |
| 5591 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5592 | // Insert our type, and its more-qualified variants, into the set |
| 5593 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 5594 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5595 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5596 | } else if (Ty->isMemberPointerType()) { |
| 5597 | // Member pointers are far easier, since the pointee can't be converted. |
| 5598 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 5599 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5600 | } else if (Ty->isEnumeralType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5601 | HasArithmeticOrEnumeralTypes = true; |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 5602 | EnumerationTypes.insert(Ty); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 5603 | } else if (Ty->isVectorType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5604 | // We treat vector types as arithmetic types in many contexts as an |
| 5605 | // extension. |
| 5606 | HasArithmeticOrEnumeralTypes = true; |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 5607 | VectorTypes.insert(Ty); |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 5608 | } else if (Ty->isNullPtrType()) { |
| 5609 | HasNullPtrType = true; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5610 | } else if (AllowUserConversions && TyRec) { |
| 5611 | // No conversion functions in incomplete types. |
| 5612 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) |
| 5613 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5614 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5615 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| 5616 | const UnresolvedSetImpl *Conversions |
| 5617 | = ClassDecl->getVisibleConversionFunctions(); |
| 5618 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 5619 | E = Conversions->end(); I != E; ++I) { |
| 5620 | NamedDecl *D = I.getDecl(); |
| 5621 | if (isa<UsingShadowDecl>(D)) |
| 5622 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5623 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5624 | // Skip conversion function templates; they don't tell us anything |
| 5625 | // about which builtin types we can convert to. |
| 5626 | if (isa<FunctionTemplateDecl>(D)) |
| 5627 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5628 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5629 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
| 5630 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
| 5631 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
| 5632 | VisibleQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5633 | } |
| 5634 | } |
| 5635 | } |
| 5636 | } |
| 5637 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 5638 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 5639 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 5640 | /// given type to the candidate set. |
| 5641 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 5642 | QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5643 | Expr **Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 5644 | unsigned NumArgs, |
| 5645 | OverloadCandidateSet &CandidateSet) { |
| 5646 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5647 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 5648 | // T& operator=(T&, T) |
| 5649 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 5650 | ParamTypes[1] = T; |
| 5651 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5652 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5653 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 5654 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 5655 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5656 | ParamTypes[0] |
| 5657 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 5658 | ParamTypes[1] = T; |
| 5659 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5660 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 5661 | } |
| 5662 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5663 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 5664 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 5665 | /// 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] | 5666 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 5667 | Qualifiers VRQuals; |
| 5668 | const RecordType *TyRec; |
| 5669 | if (const MemberPointerType *RHSMPType = |
| 5670 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 5671 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5672 | else |
| 5673 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 5674 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 5675 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5676 | VRQuals.addVolatile(); |
| 5677 | VRQuals.addRestrict(); |
| 5678 | return VRQuals; |
| 5679 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5680 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5681 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 5682 | if (!ClassDecl->hasDefinition()) |
| 5683 | return VRQuals; |
| 5684 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 5685 | const UnresolvedSetImpl *Conversions = |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 5686 | ClassDecl->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5687 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 5688 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5689 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 5690 | NamedDecl *D = I.getDecl(); |
| 5691 | if (isa<UsingShadowDecl>(D)) |
| 5692 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 5693 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5694 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 5695 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 5696 | CanTy = ResTypeRef->getPointeeType(); |
| 5697 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 5698 | // as see them. |
| 5699 | bool done = false; |
| 5700 | while (!done) { |
| 5701 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 5702 | CanTy = ResTypePtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5703 | else if (const MemberPointerType *ResTypeMPtr = |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5704 | CanTy->getAs<MemberPointerType>()) |
| 5705 | CanTy = ResTypeMPtr->getPointeeType(); |
| 5706 | else |
| 5707 | done = true; |
| 5708 | if (CanTy.isVolatileQualified()) |
| 5709 | VRQuals.addVolatile(); |
| 5710 | if (CanTy.isRestrictQualified()) |
| 5711 | VRQuals.addRestrict(); |
| 5712 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 5713 | return VRQuals; |
| 5714 | } |
| 5715 | } |
| 5716 | } |
| 5717 | return VRQuals; |
| 5718 | } |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 5719 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5720 | namespace { |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 5721 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5722 | /// \brief Helper class to manage the addition of builtin operator overload |
| 5723 | /// candidates. It provides shared state and utility methods used throughout |
| 5724 | /// the process, as well as a helper method to add each group of builtin |
| 5725 | /// operator overloads from the standard to a candidate set. |
| 5726 | class BuiltinOperatorOverloadBuilder { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5727 | // Common instance state available to all overload candidate addition methods. |
| 5728 | Sema &S; |
| 5729 | Expr **Args; |
| 5730 | unsigned NumArgs; |
| 5731 | Qualifiers VisibleTypeConversionsQuals; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5732 | bool HasArithmeticOrEnumeralCandidateType; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5733 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5734 | OverloadCandidateSet &CandidateSet; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5735 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5736 | // Define some constants used to index and iterate over the arithemetic types |
| 5737 | // provided via the getArithmeticType() method below. |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 5738 | // The "promoted arithmetic types" are the arithmetic |
| 5739 | // types are that preserved by promotion (C++ [over.built]p2). |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 5740 | static const unsigned FirstIntegralType = 3; |
| 5741 | static const unsigned LastIntegralType = 18; |
| 5742 | static const unsigned FirstPromotedIntegralType = 3, |
| 5743 | LastPromotedIntegralType = 9; |
| 5744 | static const unsigned FirstPromotedArithmeticType = 0, |
| 5745 | LastPromotedArithmeticType = 9; |
| 5746 | static const unsigned NumArithmeticTypes = 18; |
| 5747 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5748 | /// \brief Get the canonical type for a given arithmetic type index. |
| 5749 | CanQualType getArithmeticType(unsigned index) { |
| 5750 | assert(index < NumArithmeticTypes); |
| 5751 | static CanQualType ASTContext::* const |
| 5752 | ArithmeticTypes[NumArithmeticTypes] = { |
| 5753 | // Start of promoted types. |
| 5754 | &ASTContext::FloatTy, |
| 5755 | &ASTContext::DoubleTy, |
| 5756 | &ASTContext::LongDoubleTy, |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 5757 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5758 | // Start of integral types. |
| 5759 | &ASTContext::IntTy, |
| 5760 | &ASTContext::LongTy, |
| 5761 | &ASTContext::LongLongTy, |
| 5762 | &ASTContext::UnsignedIntTy, |
| 5763 | &ASTContext::UnsignedLongTy, |
| 5764 | &ASTContext::UnsignedLongLongTy, |
| 5765 | // End of promoted types. |
| 5766 | |
| 5767 | &ASTContext::BoolTy, |
| 5768 | &ASTContext::CharTy, |
| 5769 | &ASTContext::WCharTy, |
| 5770 | &ASTContext::Char16Ty, |
| 5771 | &ASTContext::Char32Ty, |
| 5772 | &ASTContext::SignedCharTy, |
| 5773 | &ASTContext::ShortTy, |
| 5774 | &ASTContext::UnsignedCharTy, |
| 5775 | &ASTContext::UnsignedShortTy, |
| 5776 | // End of integral types. |
| 5777 | // FIXME: What about complex? |
| 5778 | }; |
| 5779 | return S.Context.*ArithmeticTypes[index]; |
| 5780 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5781 | |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5782 | /// \brief Gets the canonical type resulting from the usual arithemetic |
| 5783 | /// converions for the given arithmetic types. |
| 5784 | CanQualType getUsualArithmeticConversions(unsigned L, unsigned R) { |
| 5785 | // Accelerator table for performing the usual arithmetic conversions. |
| 5786 | // The rules are basically: |
| 5787 | // - if either is floating-point, use the wider floating-point |
| 5788 | // - if same signedness, use the higher rank |
| 5789 | // - if same size, use unsigned of the higher rank |
| 5790 | // - use the larger type |
| 5791 | // These rules, together with the axiom that higher ranks are |
| 5792 | // never smaller, are sufficient to precompute all of these results |
| 5793 | // *except* when dealing with signed types of higher rank. |
| 5794 | // (we could precompute SLL x UI for all known platforms, but it's |
| 5795 | // better not to make any assumptions). |
| 5796 | enum PromotedType { |
| 5797 | Flt, Dbl, LDbl, SI, SL, SLL, UI, UL, ULL, Dep=-1 |
| 5798 | }; |
| 5799 | static PromotedType ConversionsTable[LastPromotedArithmeticType] |
| 5800 | [LastPromotedArithmeticType] = { |
| 5801 | /* Flt*/ { Flt, Dbl, LDbl, Flt, Flt, Flt, Flt, Flt, Flt }, |
| 5802 | /* Dbl*/ { Dbl, Dbl, LDbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl }, |
| 5803 | /*LDbl*/ { LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl }, |
| 5804 | /* SI*/ { Flt, Dbl, LDbl, SI, SL, SLL, UI, UL, ULL }, |
| 5805 | /* SL*/ { Flt, Dbl, LDbl, SL, SL, SLL, Dep, UL, ULL }, |
| 5806 | /* SLL*/ { Flt, Dbl, LDbl, SLL, SLL, SLL, Dep, Dep, ULL }, |
| 5807 | /* UI*/ { Flt, Dbl, LDbl, UI, Dep, Dep, UI, UL, ULL }, |
| 5808 | /* UL*/ { Flt, Dbl, LDbl, UL, UL, Dep, UL, UL, ULL }, |
| 5809 | /* ULL*/ { Flt, Dbl, LDbl, ULL, ULL, ULL, ULL, ULL, ULL }, |
| 5810 | }; |
| 5811 | |
| 5812 | assert(L < LastPromotedArithmeticType); |
| 5813 | assert(R < LastPromotedArithmeticType); |
| 5814 | int Idx = ConversionsTable[L][R]; |
| 5815 | |
| 5816 | // Fast path: the table gives us a concrete answer. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5817 | if (Idx != Dep) return getArithmeticType(Idx); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5818 | |
| 5819 | // Slow path: we need to compare widths. |
| 5820 | // An invariant is that the signed type has higher rank. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5821 | CanQualType LT = getArithmeticType(L), |
| 5822 | RT = getArithmeticType(R); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5823 | unsigned LW = S.Context.getIntWidth(LT), |
| 5824 | RW = S.Context.getIntWidth(RT); |
| 5825 | |
| 5826 | // If they're different widths, use the signed type. |
| 5827 | if (LW > RW) return LT; |
| 5828 | else if (LW < RW) return RT; |
| 5829 | |
| 5830 | // Otherwise, use the unsigned type of the signed type's rank. |
| 5831 | if (L == SL || R == SL) return S.Context.UnsignedLongTy; |
| 5832 | assert(L == SLL || R == SLL); |
| 5833 | return S.Context.UnsignedLongLongTy; |
| 5834 | } |
| 5835 | |
Chandler Carruth | 3c69dc4 | 2010-12-12 09:22:45 +0000 | [diff] [blame] | 5836 | /// \brief Helper method to factor out the common pattern of adding overloads |
| 5837 | /// for '++' and '--' builtin operators. |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5838 | void addPlusPlusMinusMinusStyleOverloads(QualType CandidateTy, |
| 5839 | bool HasVolatile) { |
| 5840 | QualType ParamTypes[2] = { |
| 5841 | S.Context.getLValueReferenceType(CandidateTy), |
| 5842 | S.Context.IntTy |
| 5843 | }; |
| 5844 | |
| 5845 | // Non-volatile version. |
| 5846 | if (NumArgs == 1) |
| 5847 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 5848 | else |
| 5849 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 5850 | |
| 5851 | // Use a heuristic to reduce number of builtin candidates in the set: |
| 5852 | // add volatile version only if there are conversions to a volatile type. |
| 5853 | if (HasVolatile) { |
| 5854 | ParamTypes[0] = |
| 5855 | S.Context.getLValueReferenceType( |
| 5856 | S.Context.getVolatileType(CandidateTy)); |
| 5857 | if (NumArgs == 1) |
| 5858 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 5859 | else |
| 5860 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 5861 | } |
| 5862 | } |
| 5863 | |
| 5864 | public: |
| 5865 | BuiltinOperatorOverloadBuilder( |
| 5866 | Sema &S, Expr **Args, unsigned NumArgs, |
| 5867 | Qualifiers VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5868 | bool HasArithmeticOrEnumeralCandidateType, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5869 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5870 | OverloadCandidateSet &CandidateSet) |
| 5871 | : S(S), Args(Args), NumArgs(NumArgs), |
| 5872 | VisibleTypeConversionsQuals(VisibleTypeConversionsQuals), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5873 | HasArithmeticOrEnumeralCandidateType( |
| 5874 | HasArithmeticOrEnumeralCandidateType), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5875 | CandidateTypes(CandidateTypes), |
| 5876 | CandidateSet(CandidateSet) { |
| 5877 | // Validate some of our static helper constants in debug builds. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5878 | assert(getArithmeticType(FirstPromotedIntegralType) == S.Context.IntTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5879 | "Invalid first promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5880 | assert(getArithmeticType(LastPromotedIntegralType - 1) |
| 5881 | == S.Context.UnsignedLongLongTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5882 | "Invalid last promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5883 | assert(getArithmeticType(FirstPromotedArithmeticType) |
| 5884 | == S.Context.FloatTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5885 | "Invalid first promoted arithmetic type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5886 | assert(getArithmeticType(LastPromotedArithmeticType - 1) |
| 5887 | == S.Context.UnsignedLongLongTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5888 | "Invalid last promoted arithmetic type"); |
| 5889 | } |
| 5890 | |
| 5891 | // C++ [over.built]p3: |
| 5892 | // |
| 5893 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 5894 | // is either volatile or empty, there exist candidate operator |
| 5895 | // functions of the form |
| 5896 | // |
| 5897 | // VQ T& operator++(VQ T&); |
| 5898 | // T operator++(VQ T&, int); |
| 5899 | // |
| 5900 | // C++ [over.built]p4: |
| 5901 | // |
| 5902 | // For every pair (T, VQ), where T is an arithmetic type other |
| 5903 | // than bool, and VQ is either volatile or empty, there exist |
| 5904 | // candidate operator functions of the form |
| 5905 | // |
| 5906 | // VQ T& operator--(VQ T&); |
| 5907 | // T operator--(VQ T&, int); |
| 5908 | void addPlusPlusMinusMinusArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5909 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5910 | return; |
| 5911 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5912 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 5913 | Arith < NumArithmeticTypes; ++Arith) { |
| 5914 | addPlusPlusMinusMinusStyleOverloads( |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5915 | getArithmeticType(Arith), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5916 | VisibleTypeConversionsQuals.hasVolatile()); |
| 5917 | } |
| 5918 | } |
| 5919 | |
| 5920 | // C++ [over.built]p5: |
| 5921 | // |
| 5922 | // For every pair (T, VQ), where T is a cv-qualified or |
| 5923 | // cv-unqualified object type, and VQ is either volatile or |
| 5924 | // empty, there exist candidate operator functions of the form |
| 5925 | // |
| 5926 | // T*VQ& operator++(T*VQ&); |
| 5927 | // T*VQ& operator--(T*VQ&); |
| 5928 | // T* operator++(T*VQ&, int); |
| 5929 | // T* operator--(T*VQ&, int); |
| 5930 | void addPlusPlusMinusMinusPointerOverloads() { |
| 5931 | for (BuiltinCandidateTypeSet::iterator |
| 5932 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5933 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5934 | Ptr != PtrEnd; ++Ptr) { |
| 5935 | // Skip pointer types that aren't pointers to object types. |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5936 | if (!(*Ptr)->getPointeeType()->isObjectType()) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5937 | continue; |
| 5938 | |
| 5939 | addPlusPlusMinusMinusStyleOverloads(*Ptr, |
| 5940 | (!S.Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 5941 | VisibleTypeConversionsQuals.hasVolatile())); |
| 5942 | } |
| 5943 | } |
| 5944 | |
| 5945 | // C++ [over.built]p6: |
| 5946 | // For every cv-qualified or cv-unqualified object type T, there |
| 5947 | // exist candidate operator functions of the form |
| 5948 | // |
| 5949 | // T& operator*(T*); |
| 5950 | // |
| 5951 | // C++ [over.built]p7: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5952 | // 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] | 5953 | // ref-qualifier, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5954 | // T& operator*(T*); |
| 5955 | void addUnaryStarPointerOverloads() { |
| 5956 | for (BuiltinCandidateTypeSet::iterator |
| 5957 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5958 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5959 | Ptr != PtrEnd; ++Ptr) { |
| 5960 | QualType ParamTy = *Ptr; |
| 5961 | QualType PointeeTy = ParamTy->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5962 | if (!PointeeTy->isObjectType() && !PointeeTy->isFunctionType()) |
| 5963 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5964 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5965 | if (const FunctionProtoType *Proto =PointeeTy->getAs<FunctionProtoType>()) |
| 5966 | if (Proto->getTypeQuals() || Proto->getRefQualifier()) |
| 5967 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5968 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5969 | S.AddBuiltinCandidate(S.Context.getLValueReferenceType(PointeeTy), |
| 5970 | &ParamTy, Args, 1, CandidateSet); |
| 5971 | } |
| 5972 | } |
| 5973 | |
| 5974 | // C++ [over.built]p9: |
| 5975 | // For every promoted arithmetic type T, there exist candidate |
| 5976 | // operator functions of the form |
| 5977 | // |
| 5978 | // T operator+(T); |
| 5979 | // T operator-(T); |
| 5980 | void addUnaryPlusOrMinusArithmeticOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5981 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5982 | return; |
| 5983 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5984 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 5985 | Arith < LastPromotedArithmeticType; ++Arith) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5986 | QualType ArithTy = getArithmeticType(Arith); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5987 | S.AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 5988 | } |
| 5989 | |
| 5990 | // Extension: We also add these operators for vector types. |
| 5991 | for (BuiltinCandidateTypeSet::iterator |
| 5992 | Vec = CandidateTypes[0].vector_begin(), |
| 5993 | VecEnd = CandidateTypes[0].vector_end(); |
| 5994 | Vec != VecEnd; ++Vec) { |
| 5995 | QualType VecTy = *Vec; |
| 5996 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 5997 | } |
| 5998 | } |
| 5999 | |
| 6000 | // C++ [over.built]p8: |
| 6001 | // For every type T, there exist candidate operator functions of |
| 6002 | // the form |
| 6003 | // |
| 6004 | // T* operator+(T*); |
| 6005 | void addUnaryPlusPointerOverloads() { |
| 6006 | for (BuiltinCandidateTypeSet::iterator |
| 6007 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6008 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6009 | Ptr != PtrEnd; ++Ptr) { |
| 6010 | QualType ParamTy = *Ptr; |
| 6011 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 6012 | } |
| 6013 | } |
| 6014 | |
| 6015 | // C++ [over.built]p10: |
| 6016 | // For every promoted integral type T, there exist candidate |
| 6017 | // operator functions of the form |
| 6018 | // |
| 6019 | // T operator~(T); |
| 6020 | void addUnaryTildePromotedIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6021 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6022 | return; |
| 6023 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6024 | for (unsigned Int = FirstPromotedIntegralType; |
| 6025 | Int < LastPromotedIntegralType; ++Int) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6026 | QualType IntTy = getArithmeticType(Int); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6027 | S.AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 6028 | } |
| 6029 | |
| 6030 | // Extension: We also add this operator for vector types. |
| 6031 | for (BuiltinCandidateTypeSet::iterator |
| 6032 | Vec = CandidateTypes[0].vector_begin(), |
| 6033 | VecEnd = CandidateTypes[0].vector_end(); |
| 6034 | Vec != VecEnd; ++Vec) { |
| 6035 | QualType VecTy = *Vec; |
| 6036 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 6037 | } |
| 6038 | } |
| 6039 | |
| 6040 | // C++ [over.match.oper]p16: |
| 6041 | // For every pointer to member type T, there exist candidate operator |
| 6042 | // functions of the form |
| 6043 | // |
| 6044 | // bool operator==(T,T); |
| 6045 | // bool operator!=(T,T); |
| 6046 | void addEqualEqualOrNotEqualMemberPointerOverloads() { |
| 6047 | /// Set of (canonical) types that we've already handled. |
| 6048 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6049 | |
| 6050 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 6051 | for (BuiltinCandidateTypeSet::iterator |
| 6052 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 6053 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 6054 | MemPtr != MemPtrEnd; |
| 6055 | ++MemPtr) { |
| 6056 | // Don't add the same builtin candidate twice. |
| 6057 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 6058 | continue; |
| 6059 | |
| 6060 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 6061 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 6062 | CandidateSet); |
| 6063 | } |
| 6064 | } |
| 6065 | } |
| 6066 | |
| 6067 | // C++ [over.built]p15: |
| 6068 | // |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6069 | // For every T, where T is an enumeration type, a pointer type, or |
| 6070 | // std::nullptr_t, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6071 | // |
| 6072 | // bool operator<(T, T); |
| 6073 | // bool operator>(T, T); |
| 6074 | // bool operator<=(T, T); |
| 6075 | // bool operator>=(T, T); |
| 6076 | // bool operator==(T, T); |
| 6077 | // bool operator!=(T, T); |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6078 | void addRelationalPointerOrEnumeralOverloads() { |
| 6079 | // C++ [over.built]p1: |
| 6080 | // If there is a user-written candidate with the same name and parameter |
| 6081 | // types as a built-in candidate operator function, the built-in operator |
| 6082 | // function is hidden and is not included in the set of candidate |
| 6083 | // functions. |
| 6084 | // |
| 6085 | // The text is actually in a note, but if we don't implement it then we end |
| 6086 | // up with ambiguities when the user provides an overloaded operator for |
| 6087 | // an enumeration type. Note that only enumeration types have this problem, |
| 6088 | // so we track which enumeration types we've seen operators for. Also, the |
| 6089 | // only other overloaded operator with enumeration argumenst, operator=, |
| 6090 | // cannot be overloaded for enumeration types, so this is the only place |
| 6091 | // where we must suppress candidates like this. |
| 6092 | llvm::DenseSet<std::pair<CanQualType, CanQualType> > |
| 6093 | UserDefinedBinaryOperators; |
| 6094 | |
| 6095 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 6096 | if (CandidateTypes[ArgIdx].enumeration_begin() != |
| 6097 | CandidateTypes[ArgIdx].enumeration_end()) { |
| 6098 | for (OverloadCandidateSet::iterator C = CandidateSet.begin(), |
| 6099 | CEnd = CandidateSet.end(); |
| 6100 | C != CEnd; ++C) { |
| 6101 | if (!C->Viable || !C->Function || C->Function->getNumParams() != 2) |
| 6102 | continue; |
| 6103 | |
| 6104 | QualType FirstParamType = |
| 6105 | C->Function->getParamDecl(0)->getType().getUnqualifiedType(); |
| 6106 | QualType SecondParamType = |
| 6107 | C->Function->getParamDecl(1)->getType().getUnqualifiedType(); |
| 6108 | |
| 6109 | // Skip if either parameter isn't of enumeral type. |
| 6110 | if (!FirstParamType->isEnumeralType() || |
| 6111 | !SecondParamType->isEnumeralType()) |
| 6112 | continue; |
| 6113 | |
| 6114 | // Add this operator to the set of known user-defined operators. |
| 6115 | UserDefinedBinaryOperators.insert( |
| 6116 | std::make_pair(S.Context.getCanonicalType(FirstParamType), |
| 6117 | S.Context.getCanonicalType(SecondParamType))); |
| 6118 | } |
| 6119 | } |
| 6120 | } |
| 6121 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6122 | /// Set of (canonical) types that we've already handled. |
| 6123 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6124 | |
| 6125 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 6126 | for (BuiltinCandidateTypeSet::iterator |
| 6127 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 6128 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 6129 | Ptr != PtrEnd; ++Ptr) { |
| 6130 | // Don't add the same builtin candidate twice. |
| 6131 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 6132 | continue; |
| 6133 | |
| 6134 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 6135 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 6136 | CandidateSet); |
| 6137 | } |
| 6138 | for (BuiltinCandidateTypeSet::iterator |
| 6139 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 6140 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 6141 | Enum != EnumEnd; ++Enum) { |
| 6142 | CanQualType CanonType = S.Context.getCanonicalType(*Enum); |
| 6143 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6144 | // Don't add the same builtin candidate twice, or if a user defined |
| 6145 | // candidate exists. |
| 6146 | if (!AddedTypes.insert(CanonType) || |
| 6147 | UserDefinedBinaryOperators.count(std::make_pair(CanonType, |
| 6148 | CanonType))) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6149 | continue; |
| 6150 | |
| 6151 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6152 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 6153 | CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6154 | } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 6155 | |
| 6156 | if (CandidateTypes[ArgIdx].hasNullPtrType()) { |
| 6157 | CanQualType NullPtrTy = S.Context.getCanonicalType(S.Context.NullPtrTy); |
| 6158 | if (AddedTypes.insert(NullPtrTy) && |
| 6159 | !UserDefinedBinaryOperators.count(std::make_pair(NullPtrTy, |
| 6160 | NullPtrTy))) { |
| 6161 | QualType ParamTypes[2] = { NullPtrTy, NullPtrTy }; |
| 6162 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 6163 | CandidateSet); |
| 6164 | } |
| 6165 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6166 | } |
| 6167 | } |
| 6168 | |
| 6169 | // C++ [over.built]p13: |
| 6170 | // |
| 6171 | // For every cv-qualified or cv-unqualified object type T |
| 6172 | // there exist candidate operator functions of the form |
| 6173 | // |
| 6174 | // T* operator+(T*, ptrdiff_t); |
| 6175 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 6176 | // T* operator-(T*, ptrdiff_t); |
| 6177 | // T* operator+(ptrdiff_t, T*); |
| 6178 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 6179 | // |
| 6180 | // C++ [over.built]p14: |
| 6181 | // |
| 6182 | // For every T, where T is a pointer to object type, there |
| 6183 | // exist candidate operator functions of the form |
| 6184 | // |
| 6185 | // ptrdiff_t operator-(T, T); |
| 6186 | void addBinaryPlusOrMinusPointerOverloads(OverloadedOperatorKind Op) { |
| 6187 | /// Set of (canonical) types that we've already handled. |
| 6188 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6189 | |
| 6190 | for (int Arg = 0; Arg < 2; ++Arg) { |
| 6191 | QualType AsymetricParamTypes[2] = { |
| 6192 | S.Context.getPointerDiffType(), |
| 6193 | S.Context.getPointerDiffType(), |
| 6194 | }; |
| 6195 | for (BuiltinCandidateTypeSet::iterator |
| 6196 | Ptr = CandidateTypes[Arg].pointer_begin(), |
| 6197 | PtrEnd = CandidateTypes[Arg].pointer_end(); |
| 6198 | Ptr != PtrEnd; ++Ptr) { |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6199 | QualType PointeeTy = (*Ptr)->getPointeeType(); |
| 6200 | if (!PointeeTy->isObjectType()) |
| 6201 | continue; |
| 6202 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6203 | AsymetricParamTypes[Arg] = *Ptr; |
| 6204 | if (Arg == 0 || Op == OO_Plus) { |
| 6205 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 6206 | // T* operator+(ptrdiff_t, T*); |
| 6207 | S.AddBuiltinCandidate(*Ptr, AsymetricParamTypes, Args, 2, |
| 6208 | CandidateSet); |
| 6209 | } |
| 6210 | if (Op == OO_Minus) { |
| 6211 | // ptrdiff_t operator-(T, T); |
| 6212 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 6213 | continue; |
| 6214 | |
| 6215 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 6216 | S.AddBuiltinCandidate(S.Context.getPointerDiffType(), ParamTypes, |
| 6217 | Args, 2, CandidateSet); |
| 6218 | } |
| 6219 | } |
| 6220 | } |
| 6221 | } |
| 6222 | |
| 6223 | // C++ [over.built]p12: |
| 6224 | // |
| 6225 | // For every pair of promoted arithmetic types L and R, there |
| 6226 | // exist candidate operator functions of the form |
| 6227 | // |
| 6228 | // LR operator*(L, R); |
| 6229 | // LR operator/(L, R); |
| 6230 | // LR operator+(L, R); |
| 6231 | // LR operator-(L, R); |
| 6232 | // bool operator<(L, R); |
| 6233 | // bool operator>(L, R); |
| 6234 | // bool operator<=(L, R); |
| 6235 | // bool operator>=(L, R); |
| 6236 | // bool operator==(L, R); |
| 6237 | // bool operator!=(L, R); |
| 6238 | // |
| 6239 | // where LR is the result of the usual arithmetic conversions |
| 6240 | // between types L and R. |
| 6241 | // |
| 6242 | // C++ [over.built]p24: |
| 6243 | // |
| 6244 | // For every pair of promoted arithmetic types L and R, there exist |
| 6245 | // candidate operator functions of the form |
| 6246 | // |
| 6247 | // LR operator?(bool, L, R); |
| 6248 | // |
| 6249 | // where LR is the result of the usual arithmetic conversions |
| 6250 | // between types L and R. |
| 6251 | // Our candidates ignore the first parameter. |
| 6252 | void addGenericBinaryArithmeticOverloads(bool isComparison) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6253 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6254 | return; |
| 6255 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6256 | for (unsigned Left = FirstPromotedArithmeticType; |
| 6257 | Left < LastPromotedArithmeticType; ++Left) { |
| 6258 | for (unsigned Right = FirstPromotedArithmeticType; |
| 6259 | Right < LastPromotedArithmeticType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6260 | QualType LandR[2] = { getArithmeticType(Left), |
| 6261 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6262 | QualType Result = |
| 6263 | isComparison ? S.Context.BoolTy |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6264 | : getUsualArithmeticConversions(Left, Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6265 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 6266 | } |
| 6267 | } |
| 6268 | |
| 6269 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 6270 | // conditional operator for vector types. |
| 6271 | for (BuiltinCandidateTypeSet::iterator |
| 6272 | Vec1 = CandidateTypes[0].vector_begin(), |
| 6273 | Vec1End = CandidateTypes[0].vector_end(); |
| 6274 | Vec1 != Vec1End; ++Vec1) { |
| 6275 | for (BuiltinCandidateTypeSet::iterator |
| 6276 | Vec2 = CandidateTypes[1].vector_begin(), |
| 6277 | Vec2End = CandidateTypes[1].vector_end(); |
| 6278 | Vec2 != Vec2End; ++Vec2) { |
| 6279 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 6280 | QualType Result = S.Context.BoolTy; |
| 6281 | if (!isComparison) { |
| 6282 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 6283 | Result = *Vec1; |
| 6284 | else |
| 6285 | Result = *Vec2; |
| 6286 | } |
| 6287 | |
| 6288 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 6289 | } |
| 6290 | } |
| 6291 | } |
| 6292 | |
| 6293 | // C++ [over.built]p17: |
| 6294 | // |
| 6295 | // For every pair of promoted integral types L and R, there |
| 6296 | // exist candidate operator functions of the form |
| 6297 | // |
| 6298 | // LR operator%(L, R); |
| 6299 | // LR operator&(L, R); |
| 6300 | // LR operator^(L, R); |
| 6301 | // LR operator|(L, R); |
| 6302 | // L operator<<(L, R); |
| 6303 | // L operator>>(L, R); |
| 6304 | // |
| 6305 | // where LR is the result of the usual arithmetic conversions |
| 6306 | // between types L and R. |
| 6307 | void addBinaryBitwiseArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6308 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6309 | return; |
| 6310 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6311 | for (unsigned Left = FirstPromotedIntegralType; |
| 6312 | Left < LastPromotedIntegralType; ++Left) { |
| 6313 | for (unsigned Right = FirstPromotedIntegralType; |
| 6314 | Right < LastPromotedIntegralType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6315 | QualType LandR[2] = { getArithmeticType(Left), |
| 6316 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6317 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 6318 | ? LandR[0] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 6319 | : getUsualArithmeticConversions(Left, Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6320 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 6321 | } |
| 6322 | } |
| 6323 | } |
| 6324 | |
| 6325 | // C++ [over.built]p20: |
| 6326 | // |
| 6327 | // For every pair (T, VQ), where T is an enumeration or |
| 6328 | // pointer to member type and VQ is either volatile or |
| 6329 | // empty, there exist candidate operator functions of the form |
| 6330 | // |
| 6331 | // VQ T& operator=(VQ T&, T); |
| 6332 | void addAssignmentMemberPointerOrEnumeralOverloads() { |
| 6333 | /// Set of (canonical) types that we've already handled. |
| 6334 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6335 | |
| 6336 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 6337 | for (BuiltinCandidateTypeSet::iterator |
| 6338 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 6339 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 6340 | Enum != EnumEnd; ++Enum) { |
| 6341 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 6342 | continue; |
| 6343 | |
| 6344 | AddBuiltinAssignmentOperatorCandidates(S, *Enum, Args, 2, |
| 6345 | CandidateSet); |
| 6346 | } |
| 6347 | |
| 6348 | for (BuiltinCandidateTypeSet::iterator |
| 6349 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 6350 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 6351 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 6352 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 6353 | continue; |
| 6354 | |
| 6355 | AddBuiltinAssignmentOperatorCandidates(S, *MemPtr, Args, 2, |
| 6356 | CandidateSet); |
| 6357 | } |
| 6358 | } |
| 6359 | } |
| 6360 | |
| 6361 | // C++ [over.built]p19: |
| 6362 | // |
| 6363 | // For every pair (T, VQ), where T is any type and VQ is either |
| 6364 | // volatile or empty, there exist candidate operator functions |
| 6365 | // of the form |
| 6366 | // |
| 6367 | // T*VQ& operator=(T*VQ&, T*); |
| 6368 | // |
| 6369 | // C++ [over.built]p21: |
| 6370 | // |
| 6371 | // For every pair (T, VQ), where T is a cv-qualified or |
| 6372 | // cv-unqualified object type and VQ is either volatile or |
| 6373 | // empty, there exist candidate operator functions of the form |
| 6374 | // |
| 6375 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 6376 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 6377 | void addAssignmentPointerOverloads(bool isEqualOp) { |
| 6378 | /// Set of (canonical) types that we've already handled. |
| 6379 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6380 | |
| 6381 | for (BuiltinCandidateTypeSet::iterator |
| 6382 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6383 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6384 | Ptr != PtrEnd; ++Ptr) { |
| 6385 | // If this is operator=, keep track of the builtin candidates we added. |
| 6386 | if (isEqualOp) |
| 6387 | AddedTypes.insert(S.Context.getCanonicalType(*Ptr)); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6388 | else if (!(*Ptr)->getPointeeType()->isObjectType()) |
| 6389 | continue; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6390 | |
| 6391 | // non-volatile version |
| 6392 | QualType ParamTypes[2] = { |
| 6393 | S.Context.getLValueReferenceType(*Ptr), |
| 6394 | isEqualOp ? *Ptr : S.Context.getPointerDiffType(), |
| 6395 | }; |
| 6396 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 6397 | /*IsAssigmentOperator=*/ isEqualOp); |
| 6398 | |
| 6399 | if (!S.Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 6400 | VisibleTypeConversionsQuals.hasVolatile()) { |
| 6401 | // volatile version |
| 6402 | ParamTypes[0] = |
| 6403 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
| 6404 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 6405 | /*IsAssigmentOperator=*/isEqualOp); |
| 6406 | } |
| 6407 | } |
| 6408 | |
| 6409 | if (isEqualOp) { |
| 6410 | for (BuiltinCandidateTypeSet::iterator |
| 6411 | Ptr = CandidateTypes[1].pointer_begin(), |
| 6412 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 6413 | Ptr != PtrEnd; ++Ptr) { |
| 6414 | // Make sure we don't add the same candidate twice. |
| 6415 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 6416 | continue; |
| 6417 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6418 | QualType ParamTypes[2] = { |
| 6419 | S.Context.getLValueReferenceType(*Ptr), |
| 6420 | *Ptr, |
| 6421 | }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6422 | |
| 6423 | // non-volatile version |
| 6424 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 6425 | /*IsAssigmentOperator=*/true); |
| 6426 | |
| 6427 | if (!S.Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 6428 | VisibleTypeConversionsQuals.hasVolatile()) { |
| 6429 | // volatile version |
| 6430 | ParamTypes[0] = |
| 6431 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6432 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 6433 | CandidateSet, /*IsAssigmentOperator=*/true); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6434 | } |
| 6435 | } |
| 6436 | } |
| 6437 | } |
| 6438 | |
| 6439 | // C++ [over.built]p18: |
| 6440 | // |
| 6441 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 6442 | // VQ is either volatile or empty, and R is a promoted |
| 6443 | // arithmetic type, there exist candidate operator functions of |
| 6444 | // the form |
| 6445 | // |
| 6446 | // VQ L& operator=(VQ L&, R); |
| 6447 | // VQ L& operator*=(VQ L&, R); |
| 6448 | // VQ L& operator/=(VQ L&, R); |
| 6449 | // VQ L& operator+=(VQ L&, R); |
| 6450 | // VQ L& operator-=(VQ L&, R); |
| 6451 | void addAssignmentArithmeticOverloads(bool isEqualOp) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6452 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6453 | return; |
| 6454 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6455 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 6456 | for (unsigned Right = FirstPromotedArithmeticType; |
| 6457 | Right < LastPromotedArithmeticType; ++Right) { |
| 6458 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6459 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6460 | |
| 6461 | // Add this built-in operator as a candidate (VQ is empty). |
| 6462 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6463 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6464 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 6465 | /*IsAssigmentOperator=*/isEqualOp); |
| 6466 | |
| 6467 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 6468 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 6469 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6470 | S.Context.getVolatileType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6471 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6472 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 6473 | CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6474 | /*IsAssigmentOperator=*/isEqualOp); |
| 6475 | } |
| 6476 | } |
| 6477 | } |
| 6478 | |
| 6479 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 6480 | for (BuiltinCandidateTypeSet::iterator |
| 6481 | Vec1 = CandidateTypes[0].vector_begin(), |
| 6482 | Vec1End = CandidateTypes[0].vector_end(); |
| 6483 | Vec1 != Vec1End; ++Vec1) { |
| 6484 | for (BuiltinCandidateTypeSet::iterator |
| 6485 | Vec2 = CandidateTypes[1].vector_begin(), |
| 6486 | Vec2End = CandidateTypes[1].vector_end(); |
| 6487 | Vec2 != Vec2End; ++Vec2) { |
| 6488 | QualType ParamTypes[2]; |
| 6489 | ParamTypes[1] = *Vec2; |
| 6490 | // Add this built-in operator as a candidate (VQ is empty). |
| 6491 | ParamTypes[0] = S.Context.getLValueReferenceType(*Vec1); |
| 6492 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 6493 | /*IsAssigmentOperator=*/isEqualOp); |
| 6494 | |
| 6495 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 6496 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 6497 | ParamTypes[0] = S.Context.getVolatileType(*Vec1); |
| 6498 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6499 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 6500 | CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6501 | /*IsAssigmentOperator=*/isEqualOp); |
| 6502 | } |
| 6503 | } |
| 6504 | } |
| 6505 | } |
| 6506 | |
| 6507 | // C++ [over.built]p22: |
| 6508 | // |
| 6509 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 6510 | // is either volatile or empty, and R is a promoted integral |
| 6511 | // type, there exist candidate operator functions of the form |
| 6512 | // |
| 6513 | // VQ L& operator%=(VQ L&, R); |
| 6514 | // VQ L& operator<<=(VQ L&, R); |
| 6515 | // VQ L& operator>>=(VQ L&, R); |
| 6516 | // VQ L& operator&=(VQ L&, R); |
| 6517 | // VQ L& operator^=(VQ L&, R); |
| 6518 | // VQ L& operator|=(VQ L&, R); |
| 6519 | void addAssignmentIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6520 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6521 | return; |
| 6522 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6523 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 6524 | for (unsigned Right = FirstPromotedIntegralType; |
| 6525 | Right < LastPromotedIntegralType; ++Right) { |
| 6526 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6527 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6528 | |
| 6529 | // Add this built-in operator as a candidate (VQ is empty). |
| 6530 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6531 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6532 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 6533 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 6534 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6535 | ParamTypes[0] = getArithmeticType(Left); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6536 | ParamTypes[0] = S.Context.getVolatileType(ParamTypes[0]); |
| 6537 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
| 6538 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 6539 | CandidateSet); |
| 6540 | } |
| 6541 | } |
| 6542 | } |
| 6543 | } |
| 6544 | |
| 6545 | // C++ [over.operator]p23: |
| 6546 | // |
| 6547 | // There also exist candidate operator functions of the form |
| 6548 | // |
| 6549 | // bool operator!(bool); |
| 6550 | // bool operator&&(bool, bool); |
| 6551 | // bool operator||(bool, bool); |
| 6552 | void addExclaimOverload() { |
| 6553 | QualType ParamTy = S.Context.BoolTy; |
| 6554 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 6555 | /*IsAssignmentOperator=*/false, |
| 6556 | /*NumContextualBoolArguments=*/1); |
| 6557 | } |
| 6558 | void addAmpAmpOrPipePipeOverload() { |
| 6559 | QualType ParamTypes[2] = { S.Context.BoolTy, S.Context.BoolTy }; |
| 6560 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 6561 | /*IsAssignmentOperator=*/false, |
| 6562 | /*NumContextualBoolArguments=*/2); |
| 6563 | } |
| 6564 | |
| 6565 | // C++ [over.built]p13: |
| 6566 | // |
| 6567 | // For every cv-qualified or cv-unqualified object type T there |
| 6568 | // exist candidate operator functions of the form |
| 6569 | // |
| 6570 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 6571 | // T& operator[](T*, ptrdiff_t); |
| 6572 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 6573 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 6574 | // T& operator[](ptrdiff_t, T*); |
| 6575 | void addSubscriptOverloads() { |
| 6576 | for (BuiltinCandidateTypeSet::iterator |
| 6577 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6578 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6579 | Ptr != PtrEnd; ++Ptr) { |
| 6580 | QualType ParamTypes[2] = { *Ptr, S.Context.getPointerDiffType() }; |
| 6581 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6582 | if (!PointeeType->isObjectType()) |
| 6583 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6584 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6585 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 6586 | |
| 6587 | // T& operator[](T*, ptrdiff_t) |
| 6588 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 6589 | } |
| 6590 | |
| 6591 | for (BuiltinCandidateTypeSet::iterator |
| 6592 | Ptr = CandidateTypes[1].pointer_begin(), |
| 6593 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 6594 | Ptr != PtrEnd; ++Ptr) { |
| 6595 | QualType ParamTypes[2] = { S.Context.getPointerDiffType(), *Ptr }; |
| 6596 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6597 | if (!PointeeType->isObjectType()) |
| 6598 | continue; |
| 6599 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6600 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 6601 | |
| 6602 | // T& operator[](ptrdiff_t, T*) |
| 6603 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 6604 | } |
| 6605 | } |
| 6606 | |
| 6607 | // C++ [over.built]p11: |
| 6608 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 6609 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 6610 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 6611 | // there exist candidate operator functions of the form |
| 6612 | // |
| 6613 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 6614 | // |
| 6615 | // where CV12 is the union of CV1 and CV2. |
| 6616 | void addArrowStarOverloads() { |
| 6617 | for (BuiltinCandidateTypeSet::iterator |
| 6618 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6619 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6620 | Ptr != PtrEnd; ++Ptr) { |
| 6621 | QualType C1Ty = (*Ptr); |
| 6622 | QualType C1; |
| 6623 | QualifierCollector Q1; |
| 6624 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
| 6625 | if (!isa<RecordType>(C1)) |
| 6626 | continue; |
| 6627 | // heuristic to reduce number of builtin candidates in the set. |
| 6628 | // Add volatile/restrict version only if there are conversions to a |
| 6629 | // volatile/restrict type. |
| 6630 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 6631 | continue; |
| 6632 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 6633 | continue; |
| 6634 | for (BuiltinCandidateTypeSet::iterator |
| 6635 | MemPtr = CandidateTypes[1].member_pointer_begin(), |
| 6636 | MemPtrEnd = CandidateTypes[1].member_pointer_end(); |
| 6637 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 6638 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 6639 | QualType C2 = QualType(mptr->getClass(), 0); |
| 6640 | C2 = C2.getUnqualifiedType(); |
| 6641 | if (C1 != C2 && !S.IsDerivedFrom(C1, C2)) |
| 6642 | break; |
| 6643 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 6644 | // build CV12 T& |
| 6645 | QualType T = mptr->getPointeeType(); |
| 6646 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 6647 | T.isVolatileQualified()) |
| 6648 | continue; |
| 6649 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 6650 | T.isRestrictQualified()) |
| 6651 | continue; |
| 6652 | T = Q1.apply(S.Context, T); |
| 6653 | QualType ResultTy = S.Context.getLValueReferenceType(T); |
| 6654 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 6655 | } |
| 6656 | } |
| 6657 | } |
| 6658 | |
| 6659 | // Note that we don't consider the first argument, since it has been |
| 6660 | // contextually converted to bool long ago. The candidates below are |
| 6661 | // therefore added as binary. |
| 6662 | // |
| 6663 | // C++ [over.built]p25: |
| 6664 | // For every type T, where T is a pointer, pointer-to-member, or scoped |
| 6665 | // enumeration type, there exist candidate operator functions of the form |
| 6666 | // |
| 6667 | // T operator?(bool, T, T); |
| 6668 | // |
| 6669 | void addConditionalOperatorOverloads() { |
| 6670 | /// Set of (canonical) types that we've already handled. |
| 6671 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6672 | |
| 6673 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 6674 | for (BuiltinCandidateTypeSet::iterator |
| 6675 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 6676 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 6677 | Ptr != PtrEnd; ++Ptr) { |
| 6678 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 6679 | continue; |
| 6680 | |
| 6681 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 6682 | S.AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 6683 | } |
| 6684 | |
| 6685 | for (BuiltinCandidateTypeSet::iterator |
| 6686 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 6687 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 6688 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 6689 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 6690 | continue; |
| 6691 | |
| 6692 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 6693 | S.AddBuiltinCandidate(*MemPtr, ParamTypes, Args, 2, CandidateSet); |
| 6694 | } |
| 6695 | |
| 6696 | if (S.getLangOptions().CPlusPlus0x) { |
| 6697 | for (BuiltinCandidateTypeSet::iterator |
| 6698 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 6699 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 6700 | Enum != EnumEnd; ++Enum) { |
| 6701 | if (!(*Enum)->getAs<EnumType>()->getDecl()->isScoped()) |
| 6702 | continue; |
| 6703 | |
| 6704 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 6705 | continue; |
| 6706 | |
| 6707 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 6708 | S.AddBuiltinCandidate(*Enum, ParamTypes, Args, 2, CandidateSet); |
| 6709 | } |
| 6710 | } |
| 6711 | } |
| 6712 | } |
| 6713 | }; |
| 6714 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6715 | } // end anonymous namespace |
| 6716 | |
| 6717 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 6718 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 6719 | /// on the operator @p Op and the arguments given. For example, if the |
| 6720 | /// operator is a binary '+', this routine might add "int |
| 6721 | /// operator+(int, int)" to cover integer addition. |
| 6722 | void |
| 6723 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 6724 | SourceLocation OpLoc, |
| 6725 | Expr **Args, unsigned NumArgs, |
| 6726 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6727 | // Find all of the types that the arguments can convert to, but only |
| 6728 | // if the operator we're looking at has built-in operator candidates |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6729 | // that make use of these types. Also record whether we encounter non-record |
| 6730 | // candidate types or either arithmetic or enumeral candidate types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6731 | Qualifiers VisibleTypeConversionsQuals; |
| 6732 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 6733 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 6734 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6735 | |
| 6736 | bool HasNonRecordCandidateType = false; |
| 6737 | bool HasArithmeticOrEnumeralCandidateType = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6738 | SmallVector<BuiltinCandidateTypeSet, 2> CandidateTypes; |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 6739 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 6740 | CandidateTypes.push_back(BuiltinCandidateTypeSet(*this)); |
| 6741 | CandidateTypes[ArgIdx].AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 6742 | OpLoc, |
| 6743 | true, |
| 6744 | (Op == OO_Exclaim || |
| 6745 | Op == OO_AmpAmp || |
| 6746 | Op == OO_PipePipe), |
| 6747 | VisibleTypeConversionsQuals); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6748 | HasNonRecordCandidateType = HasNonRecordCandidateType || |
| 6749 | CandidateTypes[ArgIdx].hasNonRecordTypes(); |
| 6750 | HasArithmeticOrEnumeralCandidateType = |
| 6751 | HasArithmeticOrEnumeralCandidateType || |
| 6752 | CandidateTypes[ArgIdx].hasArithmeticOrEnumeralTypes(); |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 6753 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6754 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6755 | // Exit early when no non-record types have been added to the candidate set |
| 6756 | // for any of the arguments to the operator. |
Douglas Gregor | 25aaff9 | 2011-10-10 14:05:31 +0000 | [diff] [blame] | 6757 | // |
| 6758 | // We can't exit early for !, ||, or &&, since there we have always have |
| 6759 | // 'bool' overloads. |
| 6760 | if (!HasNonRecordCandidateType && |
| 6761 | !(Op == OO_Exclaim || Op == OO_AmpAmp || Op == OO_PipePipe)) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6762 | return; |
| 6763 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6764 | // Setup an object to manage the common state for building overloads. |
| 6765 | BuiltinOperatorOverloadBuilder OpBuilder(*this, Args, NumArgs, |
| 6766 | VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6767 | HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6768 | CandidateTypes, CandidateSet); |
| 6769 | |
| 6770 | // Dispatch over the operation to add in only those overloads which apply. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6771 | switch (Op) { |
| 6772 | case OO_None: |
| 6773 | case NUM_OVERLOADED_OPERATORS: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6774 | llvm_unreachable("Expected an overloaded operator"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6775 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6776 | case OO_New: |
| 6777 | case OO_Delete: |
| 6778 | case OO_Array_New: |
| 6779 | case OO_Array_Delete: |
| 6780 | case OO_Call: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6781 | llvm_unreachable( |
| 6782 | "Special operators don't use AddBuiltinOperatorCandidates"); |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6783 | |
| 6784 | case OO_Comma: |
| 6785 | case OO_Arrow: |
| 6786 | // C++ [over.match.oper]p3: |
| 6787 | // -- For the operator ',', the unary operator '&', or the |
| 6788 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6789 | break; |
| 6790 | |
| 6791 | case OO_Plus: // '+' is either unary or binary |
Chandler Carruth | 32fe0d0 | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 6792 | if (NumArgs == 1) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6793 | OpBuilder.addUnaryPlusPointerOverloads(); |
Chandler Carruth | 32fe0d0 | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 6794 | // Fall through. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6795 | |
| 6796 | case OO_Minus: // '-' is either unary or binary |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 6797 | if (NumArgs == 1) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6798 | OpBuilder.addUnaryPlusOrMinusArithmeticOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 6799 | } else { |
| 6800 | OpBuilder.addBinaryPlusOrMinusPointerOverloads(Op); |
| 6801 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 6802 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6803 | break; |
| 6804 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6805 | case OO_Star: // '*' is either unary or binary |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6806 | if (NumArgs == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6807 | OpBuilder.addUnaryStarPointerOverloads(); |
| 6808 | else |
| 6809 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 6810 | break; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6811 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6812 | case OO_Slash: |
| 6813 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 6814 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6815 | |
| 6816 | case OO_PlusPlus: |
| 6817 | case OO_MinusMinus: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6818 | OpBuilder.addPlusPlusMinusMinusArithmeticOverloads(Op); |
| 6819 | OpBuilder.addPlusPlusMinusMinusPointerOverloads(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6820 | break; |
| 6821 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6822 | case OO_EqualEqual: |
| 6823 | case OO_ExclaimEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6824 | OpBuilder.addEqualEqualOrNotEqualMemberPointerOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 6825 | // Fall through. |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 6826 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6827 | case OO_Less: |
| 6828 | case OO_Greater: |
| 6829 | case OO_LessEqual: |
| 6830 | case OO_GreaterEqual: |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6831 | OpBuilder.addRelationalPointerOrEnumeralOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 6832 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/true); |
| 6833 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6834 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6835 | case OO_Percent: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6836 | case OO_Caret: |
| 6837 | case OO_Pipe: |
| 6838 | case OO_LessLess: |
| 6839 | case OO_GreaterGreater: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6840 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6841 | break; |
| 6842 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6843 | case OO_Amp: // '&' is either unary or binary |
| 6844 | if (NumArgs == 1) |
| 6845 | // C++ [over.match.oper]p3: |
| 6846 | // -- For the operator ',', the unary operator '&', or the |
| 6847 | // operator '->', the built-in candidates set is empty. |
| 6848 | break; |
| 6849 | |
| 6850 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
| 6851 | break; |
| 6852 | |
| 6853 | case OO_Tilde: |
| 6854 | OpBuilder.addUnaryTildePromotedIntegralOverloads(); |
| 6855 | break; |
| 6856 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6857 | case OO_Equal: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6858 | OpBuilder.addAssignmentMemberPointerOrEnumeralOverloads(); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6859 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6860 | |
| 6861 | case OO_PlusEqual: |
| 6862 | case OO_MinusEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6863 | OpBuilder.addAssignmentPointerOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6864 | // Fall through. |
| 6865 | |
| 6866 | case OO_StarEqual: |
| 6867 | case OO_SlashEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6868 | OpBuilder.addAssignmentArithmeticOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6869 | break; |
| 6870 | |
| 6871 | case OO_PercentEqual: |
| 6872 | case OO_LessLessEqual: |
| 6873 | case OO_GreaterGreaterEqual: |
| 6874 | case OO_AmpEqual: |
| 6875 | case OO_CaretEqual: |
| 6876 | case OO_PipeEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6877 | OpBuilder.addAssignmentIntegralOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6878 | break; |
| 6879 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6880 | case OO_Exclaim: |
| 6881 | OpBuilder.addExclaimOverload(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6882 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6883 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6884 | case OO_AmpAmp: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6885 | case OO_PipePipe: |
| 6886 | OpBuilder.addAmpAmpOrPipePipeOverload(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6887 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6888 | |
| 6889 | case OO_Subscript: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6890 | OpBuilder.addSubscriptOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6891 | break; |
| 6892 | |
| 6893 | case OO_ArrowStar: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6894 | OpBuilder.addArrowStarOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6895 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 6896 | |
| 6897 | case OO_Conditional: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6898 | OpBuilder.addConditionalOperatorOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 6899 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 6900 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6901 | } |
| 6902 | } |
| 6903 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6904 | /// \brief Add function candidates found via argument-dependent lookup |
| 6905 | /// to the set of overloading candidates. |
| 6906 | /// |
| 6907 | /// This routine performs argument-dependent name lookup based on the |
| 6908 | /// given function name (which may also be an operator name) and adds |
| 6909 | /// all of the overload candidates found by ADL to the overload |
| 6910 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6911 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6912 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6913 | bool Operator, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6914 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 6915 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6916 | OverloadCandidateSet& CandidateSet, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 6917 | bool PartialOverloading, |
| 6918 | bool StdNamespaceIsAssociated) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6919 | ADLResult Fns; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6920 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 6921 | // FIXME: This approach for uniquing ADL results (and removing |
| 6922 | // redundant candidates from the set) relies on pointer-equality, |
| 6923 | // which means we need to key off the canonical decl. However, |
| 6924 | // always going back to the canonical decl might not get us the |
| 6925 | // right set of default arguments. What default arguments are |
| 6926 | // we supposed to consider on ADL candidates, anyway? |
| 6927 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6928 | // FIXME: Pass in the explicit template arguments? |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 6929 | ArgumentDependentLookup(Name, Operator, Args, NumArgs, Fns, |
| 6930 | StdNamespaceIsAssociated); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6931 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6932 | // Erase all of the candidates we already knew about. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6933 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 6934 | CandEnd = CandidateSet.end(); |
| 6935 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6936 | if (Cand->Function) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6937 | Fns.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6938 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6939 | Fns.erase(FunTmpl); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6940 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6941 | |
| 6942 | // For each of the ADL candidates we found, add it to the overload |
| 6943 | // set. |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6944 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6945 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6946 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6947 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6948 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6949 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6950 | AddOverloadCandidate(FD, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 6951 | false, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6952 | } else |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6953 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6954 | FoundDecl, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 6955 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6956 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6957 | } |
| 6958 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6959 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 6960 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6961 | bool |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6962 | isBetterOverloadCandidate(Sema &S, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 6963 | const OverloadCandidate &Cand1, |
| 6964 | const OverloadCandidate &Cand2, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6965 | SourceLocation Loc, |
| 6966 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6967 | // Define viable functions to be better candidates than non-viable |
| 6968 | // functions. |
| 6969 | if (!Cand2.Viable) |
| 6970 | return Cand1.Viable; |
| 6971 | else if (!Cand1.Viable) |
| 6972 | return false; |
| 6973 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6974 | // C++ [over.match.best]p1: |
| 6975 | // |
| 6976 | // -- if F is a static member function, ICS1(F) is defined such |
| 6977 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 6978 | // any function G, and, symmetrically, ICS1(G) is neither |
| 6979 | // better nor worse than ICS1(F). |
| 6980 | unsigned StartArg = 0; |
| 6981 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 6982 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6983 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6984 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6985 | // A viable function F1 is defined to be a better function than another |
| 6986 | // 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] | 6987 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6988 | unsigned NumArgs = Cand1.Conversions.size(); |
| 6989 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 6990 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6991 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6992 | switch (CompareImplicitConversionSequences(S, |
| 6993 | Cand1.Conversions[ArgIdx], |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6994 | Cand2.Conversions[ArgIdx])) { |
| 6995 | case ImplicitConversionSequence::Better: |
| 6996 | // Cand1 has a better conversion sequence. |
| 6997 | HasBetterConversion = true; |
| 6998 | break; |
| 6999 | |
| 7000 | case ImplicitConversionSequence::Worse: |
| 7001 | // Cand1 can't be better than Cand2. |
| 7002 | return false; |
| 7003 | |
| 7004 | case ImplicitConversionSequence::Indistinguishable: |
| 7005 | // Do nothing. |
| 7006 | break; |
| 7007 | } |
| 7008 | } |
| 7009 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7010 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7011 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7012 | if (HasBetterConversion) |
| 7013 | return true; |
| 7014 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7015 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7016 | // specialization, or, if not that, |
Douglas Gregor | ccd4713 | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 7017 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 7018 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 7019 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7020 | |
| 7021 | // -- F1 and F2 are function template specializations, and the function |
| 7022 | // template for F1 is more specialized than the template for F2 |
| 7023 | // 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] | 7024 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 7025 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7026 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7027 | if (FunctionTemplateDecl *BetterTemplate |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7028 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 7029 | Cand2.Function->getPrimaryTemplate(), |
| 7030 | Loc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7031 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 7032 | : TPOC_Call, |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7033 | Cand1.ExplicitCallArguments)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7034 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 7035 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7036 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7037 | // -- the context is an initialization by user-defined conversion |
| 7038 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 7039 | // from the return type of F1 to the destination type (i.e., |
| 7040 | // the type of the entity being initialized) is a better |
| 7041 | // conversion sequence than the standard conversion sequence |
| 7042 | // from the return type of F2 to the destination type. |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7043 | if (UserDefinedConversion && Cand1.Function && Cand2.Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7044 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7045 | isa<CXXConversionDecl>(Cand2.Function)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7046 | switch (CompareStandardConversionSequences(S, |
| 7047 | Cand1.FinalConversion, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 7048 | Cand2.FinalConversion)) { |
| 7049 | case ImplicitConversionSequence::Better: |
| 7050 | // Cand1 has a better conversion sequence. |
| 7051 | return true; |
| 7052 | |
| 7053 | case ImplicitConversionSequence::Worse: |
| 7054 | // Cand1 can't be better than Cand2. |
| 7055 | return false; |
| 7056 | |
| 7057 | case ImplicitConversionSequence::Indistinguishable: |
| 7058 | // Do nothing |
| 7059 | break; |
| 7060 | } |
| 7061 | } |
| 7062 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7063 | return false; |
| 7064 | } |
| 7065 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7066 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7067 | /// within an overload candidate set. |
| 7068 | /// |
| 7069 | /// \param CandidateSet the set of candidate functions. |
| 7070 | /// |
| 7071 | /// \param Loc the location of the function name (or operator symbol) for |
| 7072 | /// which overload resolution occurs. |
| 7073 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7074 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7075 | /// function, Best points to the candidate function found. |
| 7076 | /// |
| 7077 | /// \returns The result of overload resolution. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7078 | OverloadingResult |
| 7079 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 7080 | iterator &Best, |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 7081 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7082 | // Find the best viable function. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7083 | Best = end(); |
| 7084 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
| 7085 | if (Cand->Viable) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7086 | if (Best == end() || isBetterOverloadCandidate(S, *Cand, *Best, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7087 | UserDefinedConversion)) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7088 | Best = Cand; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7089 | } |
| 7090 | |
| 7091 | // If we didn't find any viable functions, abort. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7092 | if (Best == end()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7093 | return OR_No_Viable_Function; |
| 7094 | |
| 7095 | // Make sure that this function is better than every other viable |
| 7096 | // function. If not, we have an ambiguity. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7097 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7098 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7099 | Cand != Best && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7100 | !isBetterOverloadCandidate(S, *Best, *Cand, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 7101 | UserDefinedConversion)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7102 | Best = end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7103 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7104 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7105 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7106 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7107 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7108 | if (Best->Function && |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 7109 | (Best->Function->isDeleted() || |
| 7110 | S.isFunctionConsideredUnavailable(Best->Function))) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7111 | return OR_Deleted; |
| 7112 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7113 | return OR_Success; |
| 7114 | } |
| 7115 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7116 | namespace { |
| 7117 | |
| 7118 | enum OverloadCandidateKind { |
| 7119 | oc_function, |
| 7120 | oc_method, |
| 7121 | oc_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7122 | oc_function_template, |
| 7123 | oc_method_template, |
| 7124 | oc_constructor_template, |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7125 | oc_implicit_default_constructor, |
| 7126 | oc_implicit_copy_constructor, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 7127 | oc_implicit_move_constructor, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7128 | oc_implicit_copy_assignment, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 7129 | oc_implicit_move_assignment, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7130 | oc_implicit_inherited_constructor |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7131 | }; |
| 7132 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7133 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 7134 | FunctionDecl *Fn, |
| 7135 | std::string &Description) { |
| 7136 | bool isTemplate = false; |
| 7137 | |
| 7138 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 7139 | isTemplate = true; |
| 7140 | Description = S.getTemplateArgumentBindingsText( |
| 7141 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 7142 | } |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7143 | |
| 7144 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7145 | if (!Ctor->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7146 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7147 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7148 | if (Ctor->getInheritedConstructor()) |
| 7149 | return oc_implicit_inherited_constructor; |
| 7150 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 7151 | if (Ctor->isDefaultConstructor()) |
| 7152 | return oc_implicit_default_constructor; |
| 7153 | |
| 7154 | if (Ctor->isMoveConstructor()) |
| 7155 | return oc_implicit_move_constructor; |
| 7156 | |
| 7157 | assert(Ctor->isCopyConstructor() && |
| 7158 | "unexpected sort of implicit constructor"); |
| 7159 | return oc_implicit_copy_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7160 | } |
| 7161 | |
| 7162 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 7163 | // This actually gets spelled 'candidate function' for now, but |
| 7164 | // it doesn't hurt to split it out. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7165 | if (!Meth->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7166 | return isTemplate ? oc_method_template : oc_method; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7167 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 7168 | if (Meth->isMoveAssignmentOperator()) |
| 7169 | return oc_implicit_move_assignment; |
| 7170 | |
Douglas Gregor | 3e9438b | 2010-09-27 22:37:28 +0000 | [diff] [blame] | 7171 | assert(Meth->isCopyAssignmentOperator() |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7172 | && "implicit method is not copy assignment operator?"); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7173 | return oc_implicit_copy_assignment; |
| 7174 | } |
| 7175 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7176 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7177 | } |
| 7178 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7179 | void MaybeEmitInheritedConstructorNote(Sema &S, FunctionDecl *Fn) { |
| 7180 | const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn); |
| 7181 | if (!Ctor) return; |
| 7182 | |
| 7183 | Ctor = Ctor->getInheritedConstructor(); |
| 7184 | if (!Ctor) return; |
| 7185 | |
| 7186 | S.Diag(Ctor->getLocation(), diag::note_ovl_candidate_inherited_constructor); |
| 7187 | } |
| 7188 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7189 | } // end anonymous namespace |
| 7190 | |
| 7191 | // Notes the location of an overload candidate. |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 7192 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn, QualType DestType) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7193 | std::string FnDesc; |
| 7194 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 7195 | PartialDiagnostic PD = PDiag(diag::note_ovl_candidate) |
| 7196 | << (unsigned) K << FnDesc; |
| 7197 | HandleFunctionTypeMismatch(PD, Fn->getType(), DestType); |
| 7198 | Diag(Fn->getLocation(), PD); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7199 | MaybeEmitInheritedConstructorNote(*this, Fn); |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 7200 | } |
| 7201 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7202 | //Notes the location of all overload candidates designated through |
| 7203 | // OverloadedExpr |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 7204 | void Sema::NoteAllOverloadCandidates(Expr* OverloadedExpr, QualType DestType) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7205 | assert(OverloadedExpr->getType() == Context.OverloadTy); |
| 7206 | |
| 7207 | OverloadExpr::FindResult Ovl = OverloadExpr::find(OverloadedExpr); |
| 7208 | OverloadExpr *OvlExpr = Ovl.Expression; |
| 7209 | |
| 7210 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 7211 | IEnd = OvlExpr->decls_end(); |
| 7212 | I != IEnd; ++I) { |
| 7213 | if (FunctionTemplateDecl *FunTmpl = |
| 7214 | dyn_cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 7215 | NoteOverloadCandidate(FunTmpl->getTemplatedDecl(), DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7216 | } else if (FunctionDecl *Fun |
| 7217 | = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl()) ) { |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 7218 | NoteOverloadCandidate(Fun, DestType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7219 | } |
| 7220 | } |
| 7221 | } |
| 7222 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7223 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 7224 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 7225 | /// target types of the conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7226 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
| 7227 | Sema &S, |
| 7228 | SourceLocation CaretLoc, |
| 7229 | const PartialDiagnostic &PDiag) const { |
| 7230 | S.Diag(CaretLoc, PDiag) |
| 7231 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7232 | for (AmbiguousConversionSequence::const_iterator |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7233 | I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
| 7234 | S.NoteOverloadCandidate(*I); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7235 | } |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7236 | } |
| 7237 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7238 | namespace { |
| 7239 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7240 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 7241 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 7242 | assert(Conv.isBad()); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7243 | assert(Cand->Function && "for now, candidate must be a function"); |
| 7244 | FunctionDecl *Fn = Cand->Function; |
| 7245 | |
| 7246 | // There's a conversion slot for the object argument if this is a |
| 7247 | // non-constructor method. Note that 'I' corresponds the |
| 7248 | // conversion-slot index. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7249 | bool isObjectArgument = false; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7250 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7251 | if (I == 0) |
| 7252 | isObjectArgument = true; |
| 7253 | else |
| 7254 | I--; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7255 | } |
| 7256 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7257 | std::string FnDesc; |
| 7258 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 7259 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7260 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 7261 | QualType FromTy = Conv.Bad.getFromType(); |
| 7262 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7263 | |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 7264 | if (FromTy == S.Context.OverloadTy) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 7265 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 7266 | Expr *E = FromExpr->IgnoreParens(); |
| 7267 | if (isa<UnaryOperator>(E)) |
| 7268 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 7269 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 7270 | |
| 7271 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 7272 | << (unsigned) FnKind << FnDesc |
| 7273 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 7274 | << ToTy << Name << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7275 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 7276 | return; |
| 7277 | } |
| 7278 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 7279 | // Do some hand-waving analysis to see if the non-viability is due |
| 7280 | // to a qualifier mismatch. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 7281 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 7282 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 7283 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 7284 | CToTy = RT->getPointeeType(); |
| 7285 | else { |
| 7286 | // TODO: detect and diagnose the full richness of const mismatches. |
| 7287 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 7288 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 7289 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 7290 | } |
| 7291 | |
| 7292 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 7293 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
| 7294 | // It is dumb that we have to do this here. |
| 7295 | while (isa<ArrayType>(CFromTy)) |
| 7296 | CFromTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 7297 | while (isa<ArrayType>(CToTy)) |
| 7298 | CToTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 7299 | |
| 7300 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 7301 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 7302 | |
| 7303 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 7304 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 7305 | << (unsigned) FnKind << FnDesc |
| 7306 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 7307 | << FromTy |
| 7308 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 7309 | << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7310 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 7311 | return; |
| 7312 | } |
| 7313 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7314 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 7315 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7316 | << (unsigned) FnKind << FnDesc |
| 7317 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 7318 | << FromTy |
| 7319 | << FromQs.getObjCLifetime() << ToQs.getObjCLifetime() |
| 7320 | << (unsigned) isObjectArgument << I+1; |
| 7321 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 7322 | return; |
| 7323 | } |
| 7324 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 7325 | if (FromQs.getObjCGCAttr() != ToQs.getObjCGCAttr()) { |
| 7326 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_gc) |
| 7327 | << (unsigned) FnKind << FnDesc |
| 7328 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 7329 | << FromTy |
| 7330 | << FromQs.getObjCGCAttr() << ToQs.getObjCGCAttr() |
| 7331 | << (unsigned) isObjectArgument << I+1; |
| 7332 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 7333 | return; |
| 7334 | } |
| 7335 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 7336 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 7337 | assert(CVR && "unexpected qualifiers mismatch"); |
| 7338 | |
| 7339 | if (isObjectArgument) { |
| 7340 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 7341 | << (unsigned) FnKind << FnDesc |
| 7342 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 7343 | << FromTy << (CVR - 1); |
| 7344 | } else { |
| 7345 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 7346 | << (unsigned) FnKind << FnDesc |
| 7347 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 7348 | << FromTy << (CVR - 1) << I+1; |
| 7349 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7350 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 7351 | return; |
| 7352 | } |
| 7353 | |
Sebastian Redl | fd2a00a | 2011-09-24 17:48:32 +0000 | [diff] [blame] | 7354 | // Special diagnostic for failure to convert an initializer list, since |
| 7355 | // telling the user that it has type void is not useful. |
| 7356 | if (FromExpr && isa<InitListExpr>(FromExpr)) { |
| 7357 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_list_argument) |
| 7358 | << (unsigned) FnKind << FnDesc |
| 7359 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 7360 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 7361 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 7362 | return; |
| 7363 | } |
| 7364 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 7365 | // Diagnose references or pointers to incomplete types differently, |
| 7366 | // since it's far from impossible that the incompleteness triggered |
| 7367 | // the failure. |
| 7368 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 7369 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 7370 | TempFromTy = PTy->getPointeeType(); |
| 7371 | if (TempFromTy->isIncompleteType()) { |
| 7372 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 7373 | << (unsigned) FnKind << FnDesc |
| 7374 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 7375 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7376 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 7377 | return; |
| 7378 | } |
| 7379 | |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 7380 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 7381 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 7382 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 7383 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 7384 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 7385 | FromPtrTy->getPointeeType()) && |
| 7386 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 7387 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7388 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 7389 | FromPtrTy->getPointeeType())) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 7390 | BaseToDerivedConversion = 1; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 7391 | } |
| 7392 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 7393 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 7394 | if (const ObjCObjectPointerType *ToPtrTy |
| 7395 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 7396 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 7397 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 7398 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 7399 | FromPtrTy->getPointeeType()) && |
| 7400 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 7401 | BaseToDerivedConversion = 2; |
| 7402 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
| 7403 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 7404 | !FromTy->isIncompleteType() && |
| 7405 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 7406 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) |
| 7407 | BaseToDerivedConversion = 3; |
| 7408 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7409 | |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 7410 | if (BaseToDerivedConversion) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7411 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 7412 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 7413 | << (unsigned) FnKind << FnDesc |
| 7414 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 7415 | << (BaseToDerivedConversion - 1) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7416 | << FromTy << ToTy << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7417 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 7418 | return; |
| 7419 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7420 | |
Fariborz Jahanian | 909bcb3 | 2011-07-20 17:14:09 +0000 | [diff] [blame] | 7421 | if (isa<ObjCObjectPointerType>(CFromTy) && |
| 7422 | isa<PointerType>(CToTy)) { |
| 7423 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 7424 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 7425 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
| 7426 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_arc_conv) |
| 7427 | << (unsigned) FnKind << FnDesc |
| 7428 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 7429 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 7430 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 7431 | return; |
| 7432 | } |
| 7433 | } |
| 7434 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7435 | // Emit the generic diagnostic and, optionally, add the hints to it. |
| 7436 | PartialDiagnostic FDiag = S.PDiag(diag::note_ovl_candidate_bad_conv); |
| 7437 | FDiag << (unsigned) FnKind << FnDesc |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7438 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7439 | << FromTy << ToTy << (unsigned) isObjectArgument << I + 1 |
| 7440 | << (unsigned) (Cand->Fix.Kind); |
| 7441 | |
| 7442 | // If we can fix the conversion, suggest the FixIts. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7443 | for (SmallVector<FixItHint, 1>::iterator |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7444 | HI = Cand->Fix.Hints.begin(), HE = Cand->Fix.Hints.end(); |
| 7445 | HI != HE; ++HI) |
| 7446 | FDiag << *HI; |
| 7447 | S.Diag(Fn->getLocation(), FDiag); |
| 7448 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7449 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7450 | } |
| 7451 | |
| 7452 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 7453 | unsigned NumFormalArgs) { |
| 7454 | // TODO: treat calls to a missing default constructor as a special case |
| 7455 | |
| 7456 | FunctionDecl *Fn = Cand->Function; |
| 7457 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 7458 | |
| 7459 | unsigned MinParams = Fn->getMinRequiredArguments(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7460 | |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 7461 | // With invalid overloaded operators, it's possible that we think we |
| 7462 | // have an arity mismatch when it fact it looks like we have the |
| 7463 | // right number of arguments, because only overloaded operators have |
| 7464 | // the weird behavior of overloading member and non-member functions. |
| 7465 | // Just don't report anything. |
| 7466 | if (Fn->isInvalidDecl() && |
| 7467 | Fn->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 7468 | return; |
| 7469 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7470 | // at least / at most / exactly |
| 7471 | unsigned mode, modeCount; |
| 7472 | if (NumFormalArgs < MinParams) { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 7473 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 7474 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 7475 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7476 | if (MinParams != FnTy->getNumArgs() || |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 7477 | FnTy->isVariadic() || FnTy->isTemplateVariadic()) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7478 | mode = 0; // "at least" |
| 7479 | else |
| 7480 | mode = 2; // "exactly" |
| 7481 | modeCount = MinParams; |
| 7482 | } else { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 7483 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 7484 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 7485 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7486 | if (MinParams != FnTy->getNumArgs()) |
| 7487 | mode = 1; // "at most" |
| 7488 | else |
| 7489 | mode = 2; // "exactly" |
| 7490 | modeCount = FnTy->getNumArgs(); |
| 7491 | } |
| 7492 | |
| 7493 | std::string Description; |
| 7494 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 7495 | |
| 7496 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7497 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 7498 | << modeCount << NumFormalArgs; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7499 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7500 | } |
| 7501 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7502 | /// Diagnose a failed template-argument deduction. |
| 7503 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
| 7504 | Expr **Args, unsigned NumArgs) { |
| 7505 | FunctionDecl *Fn = Cand->Function; // pattern |
| 7506 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7507 | TemplateParameter Param = Cand->DeductionFailure.getTemplateParameter(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7508 | NamedDecl *ParamD; |
| 7509 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 7510 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 7511 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7512 | switch (Cand->DeductionFailure.Result) { |
| 7513 | case Sema::TDK_Success: |
| 7514 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 7515 | |
| 7516 | case Sema::TDK_Incomplete: { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7517 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| 7518 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_incomplete_deduction) |
| 7519 | << ParamD->getDeclName(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7520 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7521 | return; |
| 7522 | } |
| 7523 | |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 7524 | case Sema::TDK_Underqualified: { |
| 7525 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 7526 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 7527 | |
| 7528 | QualType Param = Cand->DeductionFailure.getFirstArg()->getAsType(); |
| 7529 | |
| 7530 | // Param will have been canonicalized, but it should just be a |
| 7531 | // qualified version of ParamD, so move the qualifiers to that. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 7532 | QualifierCollector Qs; |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 7533 | Qs.strip(Param); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 7534 | QualType NonCanonParam = Qs.apply(S.Context, TParam->getTypeForDecl()); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 7535 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 7536 | |
| 7537 | // Arg has also been canonicalized, but there's nothing we can do |
| 7538 | // about that. It also doesn't matter as much, because it won't |
| 7539 | // have any template parameters in it (because deduction isn't |
| 7540 | // done on dependent types). |
| 7541 | QualType Arg = Cand->DeductionFailure.getSecondArg()->getAsType(); |
| 7542 | |
| 7543 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_underqualified) |
| 7544 | << ParamD->getDeclName() << Arg << NonCanonParam; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7545 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 7546 | return; |
| 7547 | } |
| 7548 | |
| 7549 | case Sema::TDK_Inconsistent: { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7550 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7551 | int which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7552 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7553 | which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7554 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7555 | which = 1; |
| 7556 | else { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7557 | which = 2; |
| 7558 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7559 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7560 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_inconsistent_deduction) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7561 | << which << ParamD->getDeclName() |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7562 | << *Cand->DeductionFailure.getFirstArg() |
| 7563 | << *Cand->DeductionFailure.getSecondArg(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7564 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7565 | return; |
| 7566 | } |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 7567 | |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7568 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7569 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7570 | if (ParamD->getDeclName()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7571 | S.Diag(Fn->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7572 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
| 7573 | << ParamD->getDeclName(); |
| 7574 | else { |
| 7575 | int index = 0; |
| 7576 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 7577 | index = TTP->getIndex(); |
| 7578 | else if (NonTypeTemplateParmDecl *NTTP |
| 7579 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 7580 | index = NTTP->getIndex(); |
| 7581 | else |
| 7582 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7583 | S.Diag(Fn->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7584 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
| 7585 | << (index + 1); |
| 7586 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7587 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7588 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7589 | |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 7590 | case Sema::TDK_TooManyArguments: |
| 7591 | case Sema::TDK_TooFewArguments: |
| 7592 | DiagnoseArityMismatch(S, Cand, NumArgs); |
| 7593 | return; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 7594 | |
| 7595 | case Sema::TDK_InstantiationDepth: |
| 7596 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_instantiation_depth); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7597 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 7598 | return; |
| 7599 | |
| 7600 | case Sema::TDK_SubstitutionFailure: { |
| 7601 | std::string ArgString; |
| 7602 | if (TemplateArgumentList *Args |
| 7603 | = Cand->DeductionFailure.getTemplateArgumentList()) |
| 7604 | ArgString = S.getTemplateArgumentBindingsText( |
| 7605 | Fn->getDescribedFunctionTemplate()->getTemplateParameters(), |
| 7606 | *Args); |
| 7607 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_substitution_failure) |
| 7608 | << ArgString; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7609 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 7610 | return; |
| 7611 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7612 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7613 | // TODO: diagnose these individually, then kill off |
| 7614 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7615 | case Sema::TDK_NonDeducedMismatch: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7616 | case Sema::TDK_FailedOverloadResolution: |
| 7617 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7618 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7619 | return; |
| 7620 | } |
| 7621 | } |
| 7622 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 7623 | /// CUDA: diagnose an invalid call across targets. |
| 7624 | void DiagnoseBadTarget(Sema &S, OverloadCandidate *Cand) { |
| 7625 | FunctionDecl *Caller = cast<FunctionDecl>(S.CurContext); |
| 7626 | FunctionDecl *Callee = Cand->Function; |
| 7627 | |
| 7628 | Sema::CUDAFunctionTarget CallerTarget = S.IdentifyCUDATarget(Caller), |
| 7629 | CalleeTarget = S.IdentifyCUDATarget(Callee); |
| 7630 | |
| 7631 | std::string FnDesc; |
| 7632 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Callee, FnDesc); |
| 7633 | |
| 7634 | S.Diag(Callee->getLocation(), diag::note_ovl_candidate_bad_target) |
| 7635 | << (unsigned) FnKind << CalleeTarget << CallerTarget; |
| 7636 | } |
| 7637 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7638 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 7639 | /// already generated a primary error at the call site. |
| 7640 | /// |
| 7641 | /// It really does need to be a single diagnostic with its caret |
| 7642 | /// pointed at the candidate declaration. Yes, this creates some |
| 7643 | /// major challenges of technical writing. Yes, this makes pointing |
| 7644 | /// out problems with specific arguments quite awkward. It's still |
| 7645 | /// better than generating twenty screens of text for every failed |
| 7646 | /// overload. |
| 7647 | /// |
| 7648 | /// It would be great to be able to express per-candidate problems |
| 7649 | /// more richly for those diagnostic clients that cared, but we'd |
| 7650 | /// still have to be just as careful with the default diagnostics. |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7651 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| 7652 | Expr **Args, unsigned NumArgs) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7653 | FunctionDecl *Fn = Cand->Function; |
| 7654 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7655 | // Note deleted candidates, but only if they're viable. |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 7656 | if (Cand->Viable && (Fn->isDeleted() || |
| 7657 | S.isFunctionConsideredUnavailable(Fn))) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7658 | std::string FnDesc; |
| 7659 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7660 | |
| 7661 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7662 | << FnKind << FnDesc << Fn->isDeleted(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7663 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7664 | return; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7665 | } |
| 7666 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7667 | // We don't really have anything else to say about viable candidates. |
| 7668 | if (Cand->Viable) { |
| 7669 | S.NoteOverloadCandidate(Fn); |
| 7670 | return; |
| 7671 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7672 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7673 | switch (Cand->FailureKind) { |
| 7674 | case ovl_fail_too_many_arguments: |
| 7675 | case ovl_fail_too_few_arguments: |
| 7676 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7677 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7678 | case ovl_fail_bad_deduction: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7679 | return DiagnoseBadDeduction(S, Cand, Args, NumArgs); |
| 7680 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7681 | case ovl_fail_trivial_conversion: |
| 7682 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 7683 | case ovl_fail_final_conversion_not_exact: |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7684 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7685 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 7686 | case ovl_fail_bad_conversion: { |
| 7687 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
| 7688 | for (unsigned N = Cand->Conversions.size(); I != N; ++I) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7689 | if (Cand->Conversions[I].isBad()) |
| 7690 | return DiagnoseBadConversion(S, Cand, I); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7691 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7692 | // FIXME: this currently happens when we're called from SemaInit |
| 7693 | // when user-conversion overload fails. Figure out how to handle |
| 7694 | // those conditions and diagnose them well. |
| 7695 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7696 | } |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 7697 | |
| 7698 | case ovl_fail_bad_target: |
| 7699 | return DiagnoseBadTarget(S, Cand); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 7700 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7701 | } |
| 7702 | |
| 7703 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 7704 | // Desugar the type of the surrogate down to a function type, |
| 7705 | // retaining as many typedefs as possible while still showing |
| 7706 | // the function type (and, therefore, its parameter types). |
| 7707 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 7708 | bool isLValueReference = false; |
| 7709 | bool isRValueReference = false; |
| 7710 | bool isPointer = false; |
| 7711 | if (const LValueReferenceType *FnTypeRef = |
| 7712 | FnType->getAs<LValueReferenceType>()) { |
| 7713 | FnType = FnTypeRef->getPointeeType(); |
| 7714 | isLValueReference = true; |
| 7715 | } else if (const RValueReferenceType *FnTypeRef = |
| 7716 | FnType->getAs<RValueReferenceType>()) { |
| 7717 | FnType = FnTypeRef->getPointeeType(); |
| 7718 | isRValueReference = true; |
| 7719 | } |
| 7720 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 7721 | FnType = FnTypePtr->getPointeeType(); |
| 7722 | isPointer = true; |
| 7723 | } |
| 7724 | // Desugar down to a function type. |
| 7725 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 7726 | // Reconstruct the pointer/reference as appropriate. |
| 7727 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 7728 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 7729 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 7730 | |
| 7731 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 7732 | << FnType; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7733 | MaybeEmitInheritedConstructorNote(S, Cand->Surrogate); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7734 | } |
| 7735 | |
| 7736 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 7737 | const char *Opc, |
| 7738 | SourceLocation OpLoc, |
| 7739 | OverloadCandidate *Cand) { |
| 7740 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
| 7741 | std::string TypeStr("operator"); |
| 7742 | TypeStr += Opc; |
| 7743 | TypeStr += "("; |
| 7744 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 7745 | if (Cand->Conversions.size() == 1) { |
| 7746 | TypeStr += ")"; |
| 7747 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 7748 | } else { |
| 7749 | TypeStr += ", "; |
| 7750 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 7751 | TypeStr += ")"; |
| 7752 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 7753 | } |
| 7754 | } |
| 7755 | |
| 7756 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 7757 | OverloadCandidate *Cand) { |
| 7758 | unsigned NoOperands = Cand->Conversions.size(); |
| 7759 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 7760 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7761 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 7762 | if (!ICS.isAmbiguous()) continue; |
| 7763 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7764 | ICS.DiagnoseAmbiguousConversion(S, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7765 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7766 | } |
| 7767 | } |
| 7768 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 7769 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 7770 | if (Cand->Function) |
| 7771 | return Cand->Function->getLocation(); |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 7772 | if (Cand->IsSurrogate) |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 7773 | return Cand->Surrogate->getLocation(); |
| 7774 | return SourceLocation(); |
| 7775 | } |
| 7776 | |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 7777 | static unsigned |
| 7778 | RankDeductionFailure(const OverloadCandidate::DeductionFailureInfo &DFI) { |
Chandler Carruth | 78bf680 | 2011-09-10 00:51:24 +0000 | [diff] [blame] | 7779 | switch ((Sema::TemplateDeductionResult)DFI.Result) { |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 7780 | case Sema::TDK_Success: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7781 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 7782 | |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 7783 | case Sema::TDK_Incomplete: |
| 7784 | return 1; |
| 7785 | |
| 7786 | case Sema::TDK_Underqualified: |
| 7787 | case Sema::TDK_Inconsistent: |
| 7788 | return 2; |
| 7789 | |
| 7790 | case Sema::TDK_SubstitutionFailure: |
| 7791 | case Sema::TDK_NonDeducedMismatch: |
| 7792 | return 3; |
| 7793 | |
| 7794 | case Sema::TDK_InstantiationDepth: |
| 7795 | case Sema::TDK_FailedOverloadResolution: |
| 7796 | return 4; |
| 7797 | |
| 7798 | case Sema::TDK_InvalidExplicitArguments: |
| 7799 | return 5; |
| 7800 | |
| 7801 | case Sema::TDK_TooManyArguments: |
| 7802 | case Sema::TDK_TooFewArguments: |
| 7803 | return 6; |
| 7804 | } |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 7805 | llvm_unreachable("Unhandled deduction result"); |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 7806 | } |
| 7807 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 7808 | struct CompareOverloadCandidatesForDisplay { |
| 7809 | Sema &S; |
| 7810 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7811 | |
| 7812 | bool operator()(const OverloadCandidate *L, |
| 7813 | const OverloadCandidate *R) { |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 7814 | // Fast-path this check. |
| 7815 | if (L == R) return false; |
| 7816 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7817 | // Order first by viability. |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 7818 | if (L->Viable) { |
| 7819 | if (!R->Viable) return true; |
| 7820 | |
| 7821 | // TODO: introduce a tri-valued comparison for overload |
| 7822 | // candidates. Would be more worthwhile if we had a sort |
| 7823 | // that could exploit it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7824 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation())) return true; |
| 7825 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation())) return false; |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 7826 | } else if (R->Viable) |
| 7827 | return false; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7828 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 7829 | assert(L->Viable == R->Viable); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7830 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 7831 | // Criteria by which we can sort non-viable candidates: |
| 7832 | if (!L->Viable) { |
| 7833 | // 1. Arity mismatches come after other candidates. |
| 7834 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 7835 | L->FailureKind == ovl_fail_too_few_arguments) |
| 7836 | return false; |
| 7837 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 7838 | R->FailureKind == ovl_fail_too_few_arguments) |
| 7839 | return true; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7840 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7841 | // 2. Bad conversions come first and are ordered by the number |
| 7842 | // of bad conversions and quality of good conversions. |
| 7843 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 7844 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 7845 | return true; |
| 7846 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7847 | // The conversion that can be fixed with a smaller number of changes, |
| 7848 | // comes first. |
| 7849 | unsigned numLFixes = L->Fix.NumConversionsFixed; |
| 7850 | unsigned numRFixes = R->Fix.NumConversionsFixed; |
| 7851 | numLFixes = (numLFixes == 0) ? UINT_MAX : numLFixes; |
| 7852 | numRFixes = (numRFixes == 0) ? UINT_MAX : numRFixes; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 7853 | if (numLFixes != numRFixes) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7854 | if (numLFixes < numRFixes) |
| 7855 | return true; |
| 7856 | else |
| 7857 | return false; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 7858 | } |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7859 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7860 | // If there's any ordering between the defined conversions... |
| 7861 | // FIXME: this might not be transitive. |
| 7862 | assert(L->Conversions.size() == R->Conversions.size()); |
| 7863 | |
| 7864 | int leftBetter = 0; |
John McCall | 3a81337 | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 7865 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
| 7866 | for (unsigned E = L->Conversions.size(); I != E; ++I) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7867 | switch (CompareImplicitConversionSequences(S, |
| 7868 | L->Conversions[I], |
| 7869 | R->Conversions[I])) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7870 | case ImplicitConversionSequence::Better: |
| 7871 | leftBetter++; |
| 7872 | break; |
| 7873 | |
| 7874 | case ImplicitConversionSequence::Worse: |
| 7875 | leftBetter--; |
| 7876 | break; |
| 7877 | |
| 7878 | case ImplicitConversionSequence::Indistinguishable: |
| 7879 | break; |
| 7880 | } |
| 7881 | } |
| 7882 | if (leftBetter > 0) return true; |
| 7883 | if (leftBetter < 0) return false; |
| 7884 | |
| 7885 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 7886 | return false; |
| 7887 | |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 7888 | if (L->FailureKind == ovl_fail_bad_deduction) { |
| 7889 | if (R->FailureKind != ovl_fail_bad_deduction) |
| 7890 | return true; |
| 7891 | |
| 7892 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 7893 | return RankDeductionFailure(L->DeductionFailure) |
Eli Friedman | ce1846e | 2011-10-14 23:10:30 +0000 | [diff] [blame] | 7894 | < RankDeductionFailure(R->DeductionFailure); |
Eli Friedman | 1c522f7 | 2011-10-14 21:52:24 +0000 | [diff] [blame] | 7895 | } else if (R->FailureKind == ovl_fail_bad_deduction) |
| 7896 | return false; |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 7897 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 7898 | // TODO: others? |
| 7899 | } |
| 7900 | |
| 7901 | // Sort everything else by location. |
| 7902 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 7903 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 7904 | |
| 7905 | // Put candidates without locations (e.g. builtins) at the end. |
| 7906 | if (LLoc.isInvalid()) return false; |
| 7907 | if (RLoc.isInvalid()) return true; |
| 7908 | |
| 7909 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7910 | } |
| 7911 | }; |
| 7912 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7913 | /// CompleteNonViableCandidate - Normally, overload resolution only |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7914 | /// computes up to the first. Produces the FixIt set if possible. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7915 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
| 7916 | Expr **Args, unsigned NumArgs) { |
| 7917 | assert(!Cand->Viable); |
| 7918 | |
| 7919 | // Don't do anything on failures other than bad conversion. |
| 7920 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 7921 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7922 | // We only want the FixIts if all the arguments can be corrected. |
| 7923 | bool Unfixable = false; |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 7924 | // Use a implicit copy initialization to check conversion fixes. |
| 7925 | Cand->Fix.setConversionChecker(TryCopyInitialization); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7926 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7927 | // Skip forward to the first bad conversion. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 7928 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7929 | unsigned ConvCount = Cand->Conversions.size(); |
| 7930 | while (true) { |
| 7931 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 7932 | ConvIdx++; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7933 | if (Cand->Conversions[ConvIdx - 1].isBad()) { |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 7934 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx - 1, S); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7935 | break; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7936 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7937 | } |
| 7938 | |
| 7939 | if (ConvIdx == ConvCount) |
| 7940 | return; |
| 7941 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 7942 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 7943 | "remaining conversion is initialized?"); |
| 7944 | |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 7945 | // FIXME: this should probably be preserved from the overload |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7946 | // operation somehow. |
| 7947 | bool SuppressUserConversions = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7948 | |
| 7949 | const FunctionProtoType* Proto; |
| 7950 | unsigned ArgIdx = ConvIdx; |
| 7951 | |
| 7952 | if (Cand->IsSurrogate) { |
| 7953 | QualType ConvType |
| 7954 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 7955 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 7956 | ConvType = ConvPtrType->getPointeeType(); |
| 7957 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 7958 | ArgIdx--; |
| 7959 | } else if (Cand->Function) { |
| 7960 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 7961 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 7962 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 7963 | ArgIdx--; |
| 7964 | } else { |
| 7965 | // Builtin binary operator with a bad first conversion. |
| 7966 | assert(ConvCount <= 3); |
| 7967 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 7968 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7969 | = TryCopyInitialization(S, Args[ConvIdx], |
| 7970 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7971 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7972 | /*InOverloadResolution*/ true, |
| 7973 | /*AllowObjCWritebackConversion=*/ |
| 7974 | S.getLangOptions().ObjCAutoRefCount); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7975 | return; |
| 7976 | } |
| 7977 | |
| 7978 | // Fill in the rest of the conversions. |
| 7979 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 7980 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7981 | if (ArgIdx < NumArgsInProto) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7982 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7983 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7984 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7985 | /*InOverloadResolution=*/true, |
| 7986 | /*AllowObjCWritebackConversion=*/ |
| 7987 | S.getLangOptions().ObjCAutoRefCount); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7988 | // Store the FixIt in the candidate if it exists. |
| 7989 | if (!Unfixable && Cand->Conversions[ConvIdx].isBad()) |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 7990 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx, S); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7991 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7992 | else |
| 7993 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 7994 | } |
| 7995 | } |
| 7996 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7997 | } // end anonymous namespace |
| 7998 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7999 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 8000 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8001 | /// set. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8002 | void OverloadCandidateSet::NoteCandidates(Sema &S, |
| 8003 | OverloadCandidateDisplayKind OCD, |
| 8004 | Expr **Args, unsigned NumArgs, |
| 8005 | const char *Opc, |
| 8006 | SourceLocation OpLoc) { |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8007 | // Sort the candidates by viability and position. Sorting directly would |
| 8008 | // be prohibitive, so we make a set of pointers and sort those. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8009 | SmallVector<OverloadCandidate*, 32> Cands; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8010 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
| 8011 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8012 | if (Cand->Viable) |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8013 | Cands.push_back(Cand); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8014 | else if (OCD == OCD_AllCandidates) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8015 | CompleteNonViableCandidate(S, Cand, Args, NumArgs); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8016 | if (Cand->Function || Cand->IsSurrogate) |
| 8017 | Cands.push_back(Cand); |
| 8018 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 8019 | // want to list every possible builtin candidate. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 8020 | } |
| 8021 | } |
| 8022 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 8023 | std::sort(Cands.begin(), Cands.end(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8024 | CompareOverloadCandidatesForDisplay(S)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8025 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8026 | bool ReportedAmbiguousConversions = false; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8027 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8028 | SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 8029 | const DiagnosticsEngine::OverloadsShown ShowOverloads = |
| 8030 | S.Diags.getShowOverloads(); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8031 | unsigned CandsShown = 0; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 8032 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 8033 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 8034 | |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8035 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 8036 | // the user with. FIXME: This limit should depend on details of the |
| 8037 | // candidate list. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 8038 | if (CandsShown >= 4 && ShowOverloads == DiagnosticsEngine::Ovl_Best) { |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8039 | break; |
| 8040 | } |
| 8041 | ++CandsShown; |
| 8042 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8043 | if (Cand->Function) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8044 | NoteFunctionCandidate(S, Cand, Args, NumArgs); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8045 | else if (Cand->IsSurrogate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8046 | NoteSurrogateCandidate(S, Cand); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8047 | else { |
| 8048 | assert(Cand->Viable && |
| 8049 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8050 | // Generally we only see ambiguities including viable builtin |
| 8051 | // operators if overload resolution got screwed up by an |
| 8052 | // ambiguous user-defined conversion. |
| 8053 | // |
| 8054 | // FIXME: It's quite possible for different conversions to see |
| 8055 | // different ambiguities, though. |
| 8056 | if (!ReportedAmbiguousConversions) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8057 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8058 | ReportedAmbiguousConversions = true; |
| 8059 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 8060 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8061 | // If this is a viable builtin, print it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8062 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 8063 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8064 | } |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 8065 | |
| 8066 | if (I != E) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8067 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 8068 | } |
| 8069 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8070 | // [PossiblyAFunctionType] --> [Return] |
| 8071 | // NonFunctionType --> NonFunctionType |
| 8072 | // R (A) --> R(A) |
| 8073 | // R (*)(A) --> R (A) |
| 8074 | // R (&)(A) --> R (A) |
| 8075 | // R (S::*)(A) --> R (A) |
| 8076 | QualType Sema::ExtractUnqualifiedFunctionType(QualType PossiblyAFunctionType) { |
| 8077 | QualType Ret = PossiblyAFunctionType; |
| 8078 | if (const PointerType *ToTypePtr = |
| 8079 | PossiblyAFunctionType->getAs<PointerType>()) |
| 8080 | Ret = ToTypePtr->getPointeeType(); |
| 8081 | else if (const ReferenceType *ToTypeRef = |
| 8082 | PossiblyAFunctionType->getAs<ReferenceType>()) |
| 8083 | Ret = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 8084 | else if (const MemberPointerType *MemTypePtr = |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8085 | PossiblyAFunctionType->getAs<MemberPointerType>()) |
| 8086 | Ret = MemTypePtr->getPointeeType(); |
| 8087 | Ret = |
| 8088 | Context.getCanonicalType(Ret).getUnqualifiedType(); |
| 8089 | return Ret; |
| 8090 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 8091 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8092 | // A helper class to help with address of function resolution |
| 8093 | // - allows us to avoid passing around all those ugly parameters |
| 8094 | class AddressOfFunctionResolver |
| 8095 | { |
| 8096 | Sema& S; |
| 8097 | Expr* SourceExpr; |
| 8098 | const QualType& TargetType; |
| 8099 | QualType TargetFunctionType; // Extracted function type from target type |
| 8100 | |
| 8101 | bool Complain; |
| 8102 | //DeclAccessPair& ResultFunctionAccessPair; |
| 8103 | ASTContext& Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8104 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8105 | bool TargetTypeIsNonStaticMemberFunction; |
| 8106 | bool FoundNonTemplateFunction; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8107 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8108 | OverloadExpr::FindResult OvlExprInfo; |
| 8109 | OverloadExpr *OvlExpr; |
| 8110 | TemplateArgumentListInfo OvlExplicitTemplateArgs; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8111 | SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8112 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8113 | public: |
| 8114 | AddressOfFunctionResolver(Sema &S, Expr* SourceExpr, |
| 8115 | const QualType& TargetType, bool Complain) |
| 8116 | : S(S), SourceExpr(SourceExpr), TargetType(TargetType), |
| 8117 | Complain(Complain), Context(S.getASTContext()), |
| 8118 | TargetTypeIsNonStaticMemberFunction( |
| 8119 | !!TargetType->getAs<MemberPointerType>()), |
| 8120 | FoundNonTemplateFunction(false), |
| 8121 | OvlExprInfo(OverloadExpr::find(SourceExpr)), |
| 8122 | OvlExpr(OvlExprInfo.Expression) |
| 8123 | { |
| 8124 | ExtractUnqualifiedFunctionTypeFromTargetType(); |
| 8125 | |
| 8126 | if (!TargetFunctionType->isFunctionType()) { |
| 8127 | if (OvlExpr->hasExplicitTemplateArgs()) { |
| 8128 | DeclAccessPair dap; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8129 | if (FunctionDecl* Fn = S.ResolveSingleFunctionTemplateSpecialization( |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8130 | OvlExpr, false, &dap) ) { |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 8131 | |
| 8132 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 8133 | if (!Method->isStatic()) { |
| 8134 | // If the target type is a non-function type and the function |
| 8135 | // found is a non-static member function, pretend as if that was |
| 8136 | // the target, it's the only possible type to end up with. |
| 8137 | TargetTypeIsNonStaticMemberFunction = true; |
| 8138 | |
| 8139 | // And skip adding the function if its not in the proper form. |
| 8140 | // We'll diagnose this due to an empty set of functions. |
| 8141 | if (!OvlExprInfo.HasFormOfMemberPointer) |
| 8142 | return; |
| 8143 | } |
| 8144 | } |
| 8145 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8146 | Matches.push_back(std::make_pair(dap,Fn)); |
| 8147 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 8148 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8149 | return; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 8150 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8151 | |
| 8152 | if (OvlExpr->hasExplicitTemplateArgs()) |
| 8153 | OvlExpr->getExplicitTemplateArgs().copyInto(OvlExplicitTemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8154 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8155 | if (FindAllFunctionsThatMatchTargetTypeExactly()) { |
| 8156 | // C++ [over.over]p4: |
| 8157 | // If more than one function is selected, [...] |
| 8158 | if (Matches.size() > 1) { |
| 8159 | if (FoundNonTemplateFunction) |
| 8160 | EliminateAllTemplateMatches(); |
| 8161 | else |
| 8162 | EliminateAllExceptMostSpecializedTemplate(); |
| 8163 | } |
| 8164 | } |
| 8165 | } |
| 8166 | |
| 8167 | private: |
| 8168 | bool isTargetTypeAFunction() const { |
| 8169 | return TargetFunctionType->isFunctionType(); |
| 8170 | } |
| 8171 | |
| 8172 | // [ToType] [Return] |
| 8173 | |
| 8174 | // R (*)(A) --> R (A), IsNonStaticMemberFunction = false |
| 8175 | // R (&)(A) --> R (A), IsNonStaticMemberFunction = false |
| 8176 | // R (S::*)(A) --> R (A), IsNonStaticMemberFunction = true |
| 8177 | void inline ExtractUnqualifiedFunctionTypeFromTargetType() { |
| 8178 | TargetFunctionType = S.ExtractUnqualifiedFunctionType(TargetType); |
| 8179 | } |
| 8180 | |
| 8181 | // return true if any matching specializations were found |
| 8182 | bool AddMatchingTemplateFunction(FunctionTemplateDecl* FunctionTemplate, |
| 8183 | const DeclAccessPair& CurAccessFunPair) { |
| 8184 | if (CXXMethodDecl *Method |
| 8185 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
| 8186 | // Skip non-static function templates when converting to pointer, and |
| 8187 | // static when converting to member pointer. |
| 8188 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 8189 | return false; |
| 8190 | } |
| 8191 | else if (TargetTypeIsNonStaticMemberFunction) |
| 8192 | return false; |
| 8193 | |
| 8194 | // C++ [over.over]p2: |
| 8195 | // If the name is a function template, template argument deduction is |
| 8196 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 8197 | // resulting template argument list is used to generate a single |
| 8198 | // function template specialization, which is added to the set of |
| 8199 | // overloaded functions considered. |
| 8200 | FunctionDecl *Specialization = 0; |
| 8201 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
| 8202 | if (Sema::TemplateDeductionResult Result |
| 8203 | = S.DeduceTemplateArguments(FunctionTemplate, |
| 8204 | &OvlExplicitTemplateArgs, |
| 8205 | TargetFunctionType, Specialization, |
| 8206 | Info)) { |
| 8207 | // FIXME: make a note of the failed deduction for diagnostics. |
| 8208 | (void)Result; |
| 8209 | return false; |
| 8210 | } |
| 8211 | |
| 8212 | // Template argument deduction ensures that we have an exact match. |
| 8213 | // This function template specicalization works. |
| 8214 | Specialization = cast<FunctionDecl>(Specialization->getCanonicalDecl()); |
| 8215 | assert(TargetFunctionType |
| 8216 | == Context.getCanonicalType(Specialization->getType())); |
| 8217 | Matches.push_back(std::make_pair(CurAccessFunPair, Specialization)); |
| 8218 | return true; |
| 8219 | } |
| 8220 | |
| 8221 | bool AddMatchingNonTemplateFunction(NamedDecl* Fn, |
| 8222 | const DeclAccessPair& CurAccessFunPair) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 8223 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 8224 | // Skip non-static functions when converting to pointer, and static |
| 8225 | // when converting to member pointer. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8226 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 8227 | return false; |
| 8228 | } |
| 8229 | else if (TargetTypeIsNonStaticMemberFunction) |
| 8230 | return false; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 8231 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 8232 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 8233 | if (S.getLangOptions().CUDA) |
| 8234 | if (FunctionDecl *Caller = dyn_cast<FunctionDecl>(S.CurContext)) |
| 8235 | if (S.CheckCUDATarget(Caller, FunDecl)) |
| 8236 | return false; |
| 8237 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 8238 | QualType ResultTy; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8239 | if (Context.hasSameUnqualifiedType(TargetFunctionType, |
| 8240 | FunDecl->getType()) || |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 8241 | S.IsNoReturnConversion(FunDecl->getType(), TargetFunctionType, |
| 8242 | ResultTy)) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8243 | Matches.push_back(std::make_pair(CurAccessFunPair, |
| 8244 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 8245 | FoundNonTemplateFunction = true; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8246 | return true; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 8247 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8248 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8249 | |
| 8250 | return false; |
| 8251 | } |
| 8252 | |
| 8253 | bool FindAllFunctionsThatMatchTargetTypeExactly() { |
| 8254 | bool Ret = false; |
| 8255 | |
| 8256 | // If the overload expression doesn't have the form of a pointer to |
| 8257 | // member, don't try to convert it to a pointer-to-member type. |
| 8258 | if (IsInvalidFormOfPointerToMemberFunction()) |
| 8259 | return false; |
| 8260 | |
| 8261 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 8262 | E = OvlExpr->decls_end(); |
| 8263 | I != E; ++I) { |
| 8264 | // Look through any using declarations to find the underlying function. |
| 8265 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 8266 | |
| 8267 | // C++ [over.over]p3: |
| 8268 | // Non-member functions and static member functions match |
| 8269 | // targets of type "pointer-to-function" or "reference-to-function." |
| 8270 | // Nonstatic member functions match targets of |
| 8271 | // type "pointer-to-member-function." |
| 8272 | // Note that according to DR 247, the containing class does not matter. |
| 8273 | if (FunctionTemplateDecl *FunctionTemplate |
| 8274 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
| 8275 | if (AddMatchingTemplateFunction(FunctionTemplate, I.getPair())) |
| 8276 | Ret = true; |
| 8277 | } |
| 8278 | // If we have explicit template arguments supplied, skip non-templates. |
| 8279 | else if (!OvlExpr->hasExplicitTemplateArgs() && |
| 8280 | AddMatchingNonTemplateFunction(Fn, I.getPair())) |
| 8281 | Ret = true; |
| 8282 | } |
| 8283 | assert(Ret || Matches.empty()); |
| 8284 | return Ret; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 8285 | } |
| 8286 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8287 | void EliminateAllExceptMostSpecializedTemplate() { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 8288 | // [...] and any given function template specialization F1 is |
| 8289 | // eliminated if the set contains a second function template |
| 8290 | // specialization whose function template is more specialized |
| 8291 | // than the function template of F1 according to the partial |
| 8292 | // ordering rules of 14.5.5.2. |
| 8293 | |
| 8294 | // The algorithm specified above is quadratic. We instead use a |
| 8295 | // two-pass algorithm (similar to the one used to identify the |
| 8296 | // best viable function in an overload set) that identifies the |
| 8297 | // best function template (if it exists). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8298 | |
| 8299 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 8300 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 8301 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8302 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8303 | UnresolvedSetIterator Result = |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8304 | S.getMostSpecialized(MatchesCopy.begin(), MatchesCopy.end(), |
| 8305 | TPOC_Other, 0, SourceExpr->getLocStart(), |
| 8306 | S.PDiag(), |
| 8307 | S.PDiag(diag::err_addr_ovl_ambiguous) |
| 8308 | << Matches[0].second->getDeclName(), |
| 8309 | S.PDiag(diag::note_ovl_candidate) |
| 8310 | << (unsigned) oc_function_template, |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8311 | Complain, TargetFunctionType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8312 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8313 | if (Result != MatchesCopy.end()) { |
| 8314 | // Make it the first and only element |
| 8315 | Matches[0].first = Matches[Result - MatchesCopy.begin()].first; |
| 8316 | Matches[0].second = cast<FunctionDecl>(*Result); |
| 8317 | Matches.resize(1); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8318 | } |
| 8319 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8320 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8321 | void EliminateAllTemplateMatches() { |
| 8322 | // [...] any function template specializations in the set are |
| 8323 | // eliminated if the set also contains a non-template function, [...] |
| 8324 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
| 8325 | if (Matches[I].second->getPrimaryTemplate() == 0) |
| 8326 | ++I; |
| 8327 | else { |
| 8328 | Matches[I] = Matches[--N]; |
| 8329 | Matches.set_size(N); |
| 8330 | } |
| 8331 | } |
| 8332 | } |
| 8333 | |
| 8334 | public: |
| 8335 | void ComplainNoMatchesFound() const { |
| 8336 | assert(Matches.empty()); |
| 8337 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_no_viable) |
| 8338 | << OvlExpr->getName() << TargetFunctionType |
| 8339 | << OvlExpr->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8340 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8341 | } |
| 8342 | |
| 8343 | bool IsInvalidFormOfPointerToMemberFunction() const { |
| 8344 | return TargetTypeIsNonStaticMemberFunction && |
| 8345 | !OvlExprInfo.HasFormOfMemberPointer; |
| 8346 | } |
| 8347 | |
| 8348 | void ComplainIsInvalidFormOfPointerToMemberFunction() const { |
| 8349 | // TODO: Should we condition this on whether any functions might |
| 8350 | // have matched, or is it more appropriate to do that in callers? |
| 8351 | // TODO: a fixit wouldn't hurt. |
| 8352 | S.Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
| 8353 | << TargetType << OvlExpr->getSourceRange(); |
| 8354 | } |
| 8355 | |
| 8356 | void ComplainOfInvalidConversion() const { |
| 8357 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 8358 | << OvlExpr->getName() << TargetType; |
| 8359 | } |
| 8360 | |
| 8361 | void ComplainMultipleMatchesFound() const { |
| 8362 | assert(Matches.size() > 1); |
| 8363 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 8364 | << OvlExpr->getName() |
| 8365 | << OvlExpr->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 8366 | S.NoteAllOverloadCandidates(OvlExpr, TargetFunctionType); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8367 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 8368 | |
| 8369 | bool hadMultipleCandidates() const { return (OvlExpr->getNumDecls() > 1); } |
| 8370 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8371 | int getNumMatches() const { return Matches.size(); } |
| 8372 | |
| 8373 | FunctionDecl* getMatchingFunctionDecl() const { |
| 8374 | if (Matches.size() != 1) return 0; |
| 8375 | return Matches[0].second; |
| 8376 | } |
| 8377 | |
| 8378 | const DeclAccessPair* getMatchingFunctionAccessPair() const { |
| 8379 | if (Matches.size() != 1) return 0; |
| 8380 | return &Matches[0].first; |
| 8381 | } |
| 8382 | }; |
| 8383 | |
| 8384 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 8385 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 8386 | /// expression with overloaded function type and @p ToType is the type |
| 8387 | /// we're trying to resolve to. For example: |
| 8388 | /// |
| 8389 | /// @code |
| 8390 | /// int f(double); |
| 8391 | /// int f(int); |
| 8392 | /// |
| 8393 | /// int (*pfd)(double) = f; // selects f(double) |
| 8394 | /// @endcode |
| 8395 | /// |
| 8396 | /// This routine returns the resulting FunctionDecl if it could be |
| 8397 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 8398 | /// routine will emit diagnostics if there is an error. |
| 8399 | FunctionDecl * |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 8400 | Sema::ResolveAddressOfOverloadedFunction(Expr *AddressOfExpr, |
| 8401 | QualType TargetType, |
| 8402 | bool Complain, |
| 8403 | DeclAccessPair &FoundResult, |
| 8404 | bool *pHadMultipleCandidates) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8405 | assert(AddressOfExpr->getType() == Context.OverloadTy); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 8406 | |
| 8407 | AddressOfFunctionResolver Resolver(*this, AddressOfExpr, TargetType, |
| 8408 | Complain); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8409 | int NumMatches = Resolver.getNumMatches(); |
| 8410 | FunctionDecl* Fn = 0; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 8411 | if (NumMatches == 0 && Complain) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8412 | if (Resolver.IsInvalidFormOfPointerToMemberFunction()) |
| 8413 | Resolver.ComplainIsInvalidFormOfPointerToMemberFunction(); |
| 8414 | else |
| 8415 | Resolver.ComplainNoMatchesFound(); |
| 8416 | } |
| 8417 | else if (NumMatches > 1 && Complain) |
| 8418 | Resolver.ComplainMultipleMatchesFound(); |
| 8419 | else if (NumMatches == 1) { |
| 8420 | Fn = Resolver.getMatchingFunctionDecl(); |
| 8421 | assert(Fn); |
| 8422 | FoundResult = *Resolver.getMatchingFunctionAccessPair(); |
| 8423 | MarkDeclarationReferenced(AddressOfExpr->getLocStart(), Fn); |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 8424 | if (Complain) |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8425 | CheckAddressOfMemberAccess(AddressOfExpr, FoundResult); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 8426 | } |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 8427 | |
| 8428 | if (pHadMultipleCandidates) |
| 8429 | *pHadMultipleCandidates = Resolver.hadMultipleCandidates(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8430 | return Fn; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 8431 | } |
| 8432 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8433 | /// \brief Given an expression that refers to an overloaded function, try to |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8434 | /// resolve that overloaded function expression down to a single function. |
| 8435 | /// |
| 8436 | /// This routine can only resolve template-ids that refer to a single function |
| 8437 | /// template, where that template-id refers to a single template whose template |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8438 | /// arguments are either provided by the template-id or have defaults, |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8439 | /// as described in C++0x [temp.arg.explicit]p3. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8440 | FunctionDecl * |
| 8441 | Sema::ResolveSingleFunctionTemplateSpecialization(OverloadExpr *ovl, |
| 8442 | bool Complain, |
| 8443 | DeclAccessPair *FoundResult) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8444 | // C++ [over.over]p1: |
| 8445 | // [...] [Note: any redundant set of parentheses surrounding the |
| 8446 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8447 | // C++ [over.over]p1: |
| 8448 | // [...] The overloaded function name can be preceded by the & |
| 8449 | // operator. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8450 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8451 | // If we didn't actually find any template-ids, we're done. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8452 | if (!ovl->hasExplicitTemplateArgs()) |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8453 | return 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 8454 | |
| 8455 | TemplateArgumentListInfo ExplicitTemplateArgs; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8456 | ovl->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8457 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8458 | // Look through all of the overloaded functions, searching for one |
| 8459 | // whose type matches exactly. |
| 8460 | FunctionDecl *Matched = 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8461 | for (UnresolvedSetIterator I = ovl->decls_begin(), |
| 8462 | E = ovl->decls_end(); I != E; ++I) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8463 | // C++0x [temp.arg.explicit]p3: |
| 8464 | // [...] In contexts where deduction is done and fails, or in contexts |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8465 | // where deduction is not done, if a template argument list is |
| 8466 | // specified and it, along with any default template arguments, |
| 8467 | // identifies a single function template specialization, then the |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8468 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | 66a8c9a | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 8469 | FunctionTemplateDecl *FunctionTemplate |
| 8470 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8471 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8472 | // C++ [over.over]p2: |
| 8473 | // If the name is a function template, template argument deduction is |
| 8474 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 8475 | // resulting template argument list is used to generate a single |
| 8476 | // function template specialization, which is added to the set of |
| 8477 | // overloaded functions considered. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8478 | FunctionDecl *Specialization = 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8479 | TemplateDeductionInfo Info(Context, ovl->getNameLoc()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8480 | if (TemplateDeductionResult Result |
| 8481 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 8482 | Specialization, Info)) { |
| 8483 | // FIXME: make a note of the failed deduction for diagnostics. |
| 8484 | (void)Result; |
| 8485 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8486 | } |
| 8487 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8488 | assert(Specialization && "no specialization and no error?"); |
| 8489 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8490 | // Multiple matches; we can't resolve to a single declaration. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8491 | if (Matched) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8492 | if (Complain) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8493 | Diag(ovl->getExprLoc(), diag::err_addr_ovl_ambiguous) |
| 8494 | << ovl->getName(); |
| 8495 | NoteAllOverloadCandidates(ovl); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8496 | } |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8497 | return 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8498 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8499 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8500 | Matched = Specialization; |
| 8501 | if (FoundResult) *FoundResult = I.getPair(); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8502 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8503 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8504 | return Matched; |
| 8505 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8506 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8507 | |
| 8508 | |
| 8509 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8510 | // Resolve and fix an overloaded expression that can be resolved |
| 8511 | // because it identifies a single function template specialization. |
| 8512 | // |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8513 | // Last three arguments should only be supplied if Complain = true |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8514 | // |
| 8515 | // Return true if it was logically possible to so resolve the |
| 8516 | // expression, regardless of whether or not it succeeded. Always |
| 8517 | // returns true if 'complain' is set. |
| 8518 | bool Sema::ResolveAndFixSingleFunctionTemplateSpecialization( |
| 8519 | ExprResult &SrcExpr, bool doFunctionPointerConverion, |
| 8520 | bool complain, const SourceRange& OpRangeForComplaining, |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8521 | QualType DestTypeForComplaining, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8522 | unsigned DiagIDForComplaining) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8523 | assert(SrcExpr.get()->getType() == Context.OverloadTy); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8524 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8525 | OverloadExpr::FindResult ovl = OverloadExpr::find(SrcExpr.get()); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8526 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8527 | DeclAccessPair found; |
| 8528 | ExprResult SingleFunctionExpression; |
| 8529 | if (FunctionDecl *fn = ResolveSingleFunctionTemplateSpecialization( |
| 8530 | ovl.Expression, /*complain*/ false, &found)) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8531 | if (DiagnoseUseOfDecl(fn, SrcExpr.get()->getSourceRange().getBegin())) { |
| 8532 | SrcExpr = ExprError(); |
| 8533 | return true; |
| 8534 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8535 | |
| 8536 | // It is only correct to resolve to an instance method if we're |
| 8537 | // resolving a form that's permitted to be a pointer to member. |
| 8538 | // Otherwise we'll end up making a bound member expression, which |
| 8539 | // is illegal in all the contexts we resolve like this. |
| 8540 | if (!ovl.HasFormOfMemberPointer && |
| 8541 | isa<CXXMethodDecl>(fn) && |
| 8542 | cast<CXXMethodDecl>(fn)->isInstance()) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8543 | if (!complain) return false; |
| 8544 | |
| 8545 | Diag(ovl.Expression->getExprLoc(), |
| 8546 | diag::err_bound_member_function) |
| 8547 | << 0 << ovl.Expression->getSourceRange(); |
| 8548 | |
| 8549 | // TODO: I believe we only end up here if there's a mix of |
| 8550 | // static and non-static candidates (otherwise the expression |
| 8551 | // would have 'bound member' type, not 'overload' type). |
| 8552 | // Ideally we would note which candidate was chosen and why |
| 8553 | // the static candidates were rejected. |
| 8554 | SrcExpr = ExprError(); |
| 8555 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8556 | } |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 8557 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8558 | // Fix the expresion to refer to 'fn'. |
| 8559 | SingleFunctionExpression = |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8560 | Owned(FixOverloadedFunctionReference(SrcExpr.take(), found, fn)); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8561 | |
| 8562 | // If desired, do function-to-pointer decay. |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8563 | if (doFunctionPointerConverion) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8564 | SingleFunctionExpression = |
| 8565 | DefaultFunctionArrayLvalueConversion(SingleFunctionExpression.take()); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8566 | if (SingleFunctionExpression.isInvalid()) { |
| 8567 | SrcExpr = ExprError(); |
| 8568 | return true; |
| 8569 | } |
| 8570 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8571 | } |
| 8572 | |
| 8573 | if (!SingleFunctionExpression.isUsable()) { |
| 8574 | if (complain) { |
| 8575 | Diag(OpRangeForComplaining.getBegin(), DiagIDForComplaining) |
| 8576 | << ovl.Expression->getName() |
| 8577 | << DestTypeForComplaining |
| 8578 | << OpRangeForComplaining |
| 8579 | << ovl.Expression->getQualifierLoc().getSourceRange(); |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8580 | NoteAllOverloadCandidates(SrcExpr.get()); |
| 8581 | |
| 8582 | SrcExpr = ExprError(); |
| 8583 | return true; |
| 8584 | } |
| 8585 | |
| 8586 | return false; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8587 | } |
| 8588 | |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 8589 | SrcExpr = SingleFunctionExpression; |
| 8590 | return true; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8591 | } |
| 8592 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8593 | /// \brief Add a single candidate to the overload set. |
| 8594 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8595 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 8596 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8597 | Expr **Args, unsigned NumArgs, |
| 8598 | OverloadCandidateSet &CandidateSet, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 8599 | bool PartialOverloading, |
| 8600 | bool KnownValid) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8601 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8602 | if (isa<UsingShadowDecl>(Callee)) |
| 8603 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 8604 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8605 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 8606 | if (ExplicitTemplateArgs) { |
| 8607 | assert(!KnownValid && "Explicit template arguments?"); |
| 8608 | return; |
| 8609 | } |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8610 | S.AddOverloadCandidate(Func, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 8611 | false, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8612 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8613 | } |
| 8614 | |
| 8615 | if (FunctionTemplateDecl *FuncTemplate |
| 8616 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8617 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
| 8618 | ExplicitTemplateArgs, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8619 | Args, NumArgs, CandidateSet); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8620 | return; |
| 8621 | } |
| 8622 | |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 8623 | assert(!KnownValid && "unhandled case in overloaded call candidate"); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8624 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8625 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8626 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 8627 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8628 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8629 | Expr **Args, unsigned NumArgs, |
| 8630 | OverloadCandidateSet &CandidateSet, |
| 8631 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8632 | |
| 8633 | #ifndef NDEBUG |
| 8634 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 8635 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8636 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8637 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 8638 | // and let Y be the lookup set produced by argument dependent |
| 8639 | // lookup (defined as follows). If X contains |
| 8640 | // |
| 8641 | // -- a declaration of a class member, or |
| 8642 | // |
| 8643 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8644 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8645 | // |
| 8646 | // -- a declaration that is neither a function or a function |
| 8647 | // template |
| 8648 | // |
| 8649 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8650 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8651 | if (ULE->requiresADL()) { |
| 8652 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 8653 | E = ULE->decls_end(); I != E; ++I) { |
| 8654 | assert(!(*I)->getDeclContext()->isRecord()); |
| 8655 | assert(isa<UsingShadowDecl>(*I) || |
| 8656 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 8657 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8658 | } |
| 8659 | } |
| 8660 | #endif |
| 8661 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8662 | // It would be nice to avoid this copy. |
| 8663 | TemplateArgumentListInfo TABuffer; |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 8664 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8665 | if (ULE->hasExplicitTemplateArgs()) { |
| 8666 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 8667 | ExplicitTemplateArgs = &TABuffer; |
| 8668 | } |
| 8669 | |
| 8670 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 8671 | E = ULE->decls_end(); I != E; ++I) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8672 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8673 | Args, NumArgs, CandidateSet, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 8674 | PartialOverloading, /*KnownValid*/ true); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8675 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8676 | if (ULE->requiresADL()) |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8677 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
| 8678 | Args, NumArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8679 | ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8680 | CandidateSet, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 8681 | PartialOverloading, |
| 8682 | ULE->isStdAssociatedNamespace()); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8683 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 8684 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8685 | /// Attempt to recover from an ill-formed use of a non-dependent name in a |
| 8686 | /// template, where the non-dependent name was declared after the template |
| 8687 | /// was defined. This is common in code written for a compilers which do not |
| 8688 | /// correctly implement two-stage name lookup. |
| 8689 | /// |
| 8690 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 8691 | static bool |
| 8692 | DiagnoseTwoPhaseLookup(Sema &SemaRef, SourceLocation FnLoc, |
| 8693 | const CXXScopeSpec &SS, LookupResult &R, |
| 8694 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 8695 | Expr **Args, unsigned NumArgs) { |
| 8696 | if (SemaRef.ActiveTemplateInstantiations.empty() || !SS.isEmpty()) |
| 8697 | return false; |
| 8698 | |
| 8699 | for (DeclContext *DC = SemaRef.CurContext; DC; DC = DC->getParent()) { |
| 8700 | SemaRef.LookupQualifiedName(R, DC); |
| 8701 | |
| 8702 | if (!R.empty()) { |
| 8703 | R.suppressDiagnostics(); |
| 8704 | |
| 8705 | if (isa<CXXRecordDecl>(DC)) { |
| 8706 | // Don't diagnose names we find in classes; we get much better |
| 8707 | // diagnostics for these from DiagnoseEmptyLookup. |
| 8708 | R.clear(); |
| 8709 | return false; |
| 8710 | } |
| 8711 | |
| 8712 | OverloadCandidateSet Candidates(FnLoc); |
| 8713 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 8714 | AddOverloadedCallCandidate(SemaRef, I.getPair(), |
| 8715 | ExplicitTemplateArgs, Args, NumArgs, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 8716 | Candidates, false, /*KnownValid*/ false); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8717 | |
| 8718 | OverloadCandidateSet::iterator Best; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 8719 | if (Candidates.BestViableFunction(SemaRef, FnLoc, Best) != OR_Success) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8720 | // No viable functions. Don't bother the user with notes for functions |
| 8721 | // which don't work and shouldn't be found anyway. |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 8722 | R.clear(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8723 | return false; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 8724 | } |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8725 | |
| 8726 | // Find the namespaces where ADL would have looked, and suggest |
| 8727 | // declaring the function there instead. |
| 8728 | Sema::AssociatedNamespaceSet AssociatedNamespaces; |
| 8729 | Sema::AssociatedClassSet AssociatedClasses; |
| 8730 | SemaRef.FindAssociatedClassesAndNamespaces(Args, NumArgs, |
| 8731 | AssociatedNamespaces, |
| 8732 | AssociatedClasses); |
| 8733 | // Never suggest declaring a function within namespace 'std'. |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 8734 | Sema::AssociatedNamespaceSet SuggestedNamespaces; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8735 | if (DeclContext *Std = SemaRef.getStdNamespace()) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8736 | for (Sema::AssociatedNamespaceSet::iterator |
| 8737 | it = AssociatedNamespaces.begin(), |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 8738 | end = AssociatedNamespaces.end(); it != end; ++it) { |
| 8739 | if (!Std->Encloses(*it)) |
| 8740 | SuggestedNamespaces.insert(*it); |
| 8741 | } |
Chandler Carruth | 45cad4a | 2011-06-08 10:13:17 +0000 | [diff] [blame] | 8742 | } else { |
| 8743 | // Lacking the 'std::' namespace, use all of the associated namespaces. |
| 8744 | SuggestedNamespaces = AssociatedNamespaces; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8745 | } |
| 8746 | |
| 8747 | SemaRef.Diag(R.getNameLoc(), diag::err_not_found_by_two_phase_lookup) |
| 8748 | << R.getLookupName(); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 8749 | if (SuggestedNamespaces.empty()) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8750 | SemaRef.Diag(Best->Function->getLocation(), |
| 8751 | diag::note_not_found_by_two_phase_lookup) |
| 8752 | << R.getLookupName() << 0; |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 8753 | } else if (SuggestedNamespaces.size() == 1) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8754 | SemaRef.Diag(Best->Function->getLocation(), |
| 8755 | diag::note_not_found_by_two_phase_lookup) |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 8756 | << R.getLookupName() << 1 << *SuggestedNamespaces.begin(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8757 | } else { |
| 8758 | // FIXME: It would be useful to list the associated namespaces here, |
| 8759 | // but the diagnostics infrastructure doesn't provide a way to produce |
| 8760 | // a localized representation of a list of items. |
| 8761 | SemaRef.Diag(Best->Function->getLocation(), |
| 8762 | diag::note_not_found_by_two_phase_lookup) |
| 8763 | << R.getLookupName() << 2; |
| 8764 | } |
| 8765 | |
| 8766 | // Try to recover by calling this function. |
| 8767 | return true; |
| 8768 | } |
| 8769 | |
| 8770 | R.clear(); |
| 8771 | } |
| 8772 | |
| 8773 | return false; |
| 8774 | } |
| 8775 | |
| 8776 | /// Attempt to recover from ill-formed use of a non-dependent operator in a |
| 8777 | /// template, where the non-dependent operator was declared after the template |
| 8778 | /// was defined. |
| 8779 | /// |
| 8780 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 8781 | static bool |
| 8782 | DiagnoseTwoPhaseOperatorLookup(Sema &SemaRef, OverloadedOperatorKind Op, |
| 8783 | SourceLocation OpLoc, |
| 8784 | Expr **Args, unsigned NumArgs) { |
| 8785 | DeclarationName OpName = |
| 8786 | SemaRef.Context.DeclarationNames.getCXXOperatorName(Op); |
| 8787 | LookupResult R(SemaRef, OpName, OpLoc, Sema::LookupOperatorName); |
| 8788 | return DiagnoseTwoPhaseLookup(SemaRef, OpLoc, CXXScopeSpec(), R, |
| 8789 | /*ExplicitTemplateArgs=*/0, Args, NumArgs); |
| 8790 | } |
| 8791 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 8792 | /// Attempts to recover from a call where no functions were found. |
| 8793 | /// |
| 8794 | /// Returns true if new candidates were found. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8795 | static ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 8796 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8797 | UnresolvedLookupExpr *ULE, |
| 8798 | SourceLocation LParenLoc, |
| 8799 | Expr **Args, unsigned NumArgs, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8800 | SourceLocation RParenLoc, |
| 8801 | bool EmptyLookup) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 8802 | |
| 8803 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 8804 | SS.Adopt(ULE->getQualifierLoc()); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 8805 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8806 | TemplateArgumentListInfo TABuffer; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8807 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8808 | if (ULE->hasExplicitTemplateArgs()) { |
| 8809 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 8810 | ExplicitTemplateArgs = &TABuffer; |
| 8811 | } |
| 8812 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 8813 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 8814 | Sema::LookupOrdinaryName); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8815 | if (!DiagnoseTwoPhaseLookup(SemaRef, Fn->getExprLoc(), SS, R, |
| 8816 | ExplicitTemplateArgs, Args, NumArgs) && |
| 8817 | (!EmptyLookup || |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 8818 | SemaRef.DiagnoseEmptyLookup(S, SS, R, Sema::CTC_Expression, |
Kaelyn Uhrain | ace5e76 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 8819 | ExplicitTemplateArgs, Args, NumArgs))) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8820 | return ExprError(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 8821 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8822 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 8823 | |
| 8824 | // Build an implicit member call if appropriate. Just drop the |
| 8825 | // casts and such from the call, we don't really care. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8826 | ExprResult NewFn = ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8827 | if ((*R.begin())->isCXXClassMember()) |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8828 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, |
| 8829 | ExplicitTemplateArgs); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8830 | else if (ExplicitTemplateArgs) |
| 8831 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 8832 | else |
| 8833 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 8834 | |
| 8835 | if (NewFn.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8836 | return ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8837 | |
| 8838 | // This shouldn't cause an infinite loop because we're giving it |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8839 | // an expression with viable lookup results, which should never |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8840 | // end up here. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8841 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, NewFn.take(), LParenLoc, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 8842 | MultiExprArg(Args, NumArgs), RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 8843 | } |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 8844 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8845 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 8846 | /// (which eventually refers to the declaration Func) and the call |
| 8847 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 8848 | /// to a specific function. If overload resolution succeeds, returns |
| 8849 | /// the function declaration produced by overload |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 8850 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8851 | /// arguments and Fn, and returns NULL. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8852 | ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 8853 | Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, UnresolvedLookupExpr *ULE, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8854 | SourceLocation LParenLoc, |
| 8855 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 8856 | SourceLocation RParenLoc, |
| 8857 | Expr *ExecConfig) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8858 | #ifndef NDEBUG |
| 8859 | if (ULE->requiresADL()) { |
| 8860 | // To do ADL, we must have found an unqualified name. |
| 8861 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 8862 | |
| 8863 | // We don't perform ADL for implicit declarations of builtins. |
| 8864 | // Verify that this was correctly set up. |
| 8865 | FunctionDecl *F; |
| 8866 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 8867 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 8868 | F->getBuiltinID() && F->isImplicit()) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8869 | llvm_unreachable("performing ADL for builtin"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8870 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8871 | // We don't perform ADL in C. |
| 8872 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 8873 | } else |
| 8874 | assert(!ULE->isStdAssociatedNamespace() && |
| 8875 | "std is associated namespace but not doing ADL"); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8876 | #endif |
| 8877 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 8878 | UnbridgedCastsSet UnbridgedCasts; |
| 8879 | if (checkArgPlaceholdersForOverload(*this, Args, NumArgs, UnbridgedCasts)) |
| 8880 | return ExprError(); |
| 8881 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8882 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 8883 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8884 | // Add the functions denoted by the callee to the set of candidate |
| 8885 | // functions, including those from argument-dependent lookup. |
| 8886 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 8887 | |
| 8888 | // If we found nothing, try to recover. |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8889 | // BuildRecoveryCallExpr diagnoses the error itself, so we just bail |
| 8890 | // out if it fails. |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 8891 | if (CandidateSet.empty()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8892 | // In Microsoft mode, if we are inside a template class member function then |
| 8893 | // create a type dependent CallExpr. The goal is to postpone name lookup |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 8894 | // to instantiation time to be able to search into type dependent base |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8895 | // classes. |
Francois Pichet | ef04ecf | 2011-11-11 00:12:11 +0000 | [diff] [blame] | 8896 | if (getLangOptions().MicrosoftMode && CurContext->isDependentContext() && |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 8897 | (isa<FunctionDecl>(CurContext) || isa<CXXRecordDecl>(CurContext))) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 8898 | CallExpr *CE = new (Context) CallExpr(Context, Fn, Args, NumArgs, |
| 8899 | Context.DependentTy, VK_RValue, |
| 8900 | RParenLoc); |
| 8901 | CE->setTypeDependent(true); |
| 8902 | return Owned(CE); |
| 8903 | } |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 8904 | return BuildRecoveryCallExpr(*this, S, Fn, ULE, LParenLoc, Args, NumArgs, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8905 | RParenLoc, /*EmptyLookup=*/true); |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 8906 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 8907 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 8908 | UnbridgedCasts.restore(); |
| 8909 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8910 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8911 | switch (CandidateSet.BestViableFunction(*this, Fn->getLocStart(), Best)) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8912 | case OR_Success: { |
| 8913 | FunctionDecl *FDecl = Best->Function; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8914 | MarkDeclarationReferenced(Fn->getExprLoc(), FDecl); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8915 | CheckUnresolvedLookupAccess(ULE, Best->FoundDecl); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 8916 | DiagnoseUseOfDecl(FDecl, ULE->getNameLoc()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8917 | Fn = FixOverloadedFunctionReference(Fn, Best->FoundDecl, FDecl); |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 8918 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 8919 | ExecConfig); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8920 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8921 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8922 | case OR_No_Viable_Function: { |
| 8923 | // Try to recover by looking for viable functions which the user might |
| 8924 | // have meant to call. |
| 8925 | ExprResult Recovery = BuildRecoveryCallExpr(*this, S, Fn, ULE, LParenLoc, |
| 8926 | Args, NumArgs, RParenLoc, |
| 8927 | /*EmptyLookup=*/false); |
| 8928 | if (!Recovery.isInvalid()) |
| 8929 | return Recovery; |
| 8930 | |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 8931 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8932 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8933 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8934 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8935 | break; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8936 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8937 | |
| 8938 | case OR_Ambiguous: |
| 8939 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8940 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8941 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8942 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8943 | |
| 8944 | case OR_Deleted: |
Fariborz Jahanian | 2b982b7 | 2011-02-25 18:38:59 +0000 | [diff] [blame] | 8945 | { |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 8946 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 8947 | << Best->Function->isDeleted() |
| 8948 | << ULE->getName() |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 8949 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 8950 | << Fn->getSourceRange(); |
Fariborz Jahanian | 2b982b7 | 2011-02-25 18:38:59 +0000 | [diff] [blame] | 8951 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Argyrios Kyrtzidis | 0d579b6 | 2011-11-04 15:58:13 +0000 | [diff] [blame] | 8952 | |
| 8953 | // We emitted an error for the unvailable/deleted function call but keep |
| 8954 | // the call in the AST. |
| 8955 | FunctionDecl *FDecl = Best->Function; |
| 8956 | Fn = FixOverloadedFunctionReference(Fn, Best->FoundDecl, FDecl); |
| 8957 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, |
| 8958 | RParenLoc, ExecConfig); |
Fariborz Jahanian | 2b982b7 | 2011-02-25 18:38:59 +0000 | [diff] [blame] | 8959 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8960 | break; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8961 | } |
| 8962 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 8963 | // Overload resolution failed. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8964 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8965 | } |
| 8966 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8967 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 8968 | return Functions.size() > 1 || |
| 8969 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 8970 | } |
| 8971 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8972 | /// \brief Create a unary operation that may resolve to an overloaded |
| 8973 | /// operator. |
| 8974 | /// |
| 8975 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 8976 | /// |
| 8977 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 8978 | /// operator. |
| 8979 | /// |
| 8980 | /// \param Functions The set of non-member functions that will be |
| 8981 | /// considered by overload resolution. The caller needs to build this |
| 8982 | /// set based on the context using, e.g., |
| 8983 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 8984 | /// set should not contain any member functions; those will be added |
| 8985 | /// by CreateOverloadedUnaryOp(). |
| 8986 | /// |
| 8987 | /// \param input The input argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8988 | ExprResult |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8989 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 8990 | const UnresolvedSetImpl &Fns, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8991 | Expr *Input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8992 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8993 | |
| 8994 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 8995 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 8996 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8997 | // TODO: provide better source location info. |
| 8998 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8999 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9000 | if (checkPlaceholderForOverload(*this, Input)) |
| 9001 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 9002 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9003 | Expr *Args[2] = { Input, 0 }; |
| 9004 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9005 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9006 | // For post-increment and post-decrement, add the implicit '0' as |
| 9007 | // the second argument, so that we know this is a post-increment or |
| 9008 | // post-decrement. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9009 | if (Opc == UO_PostInc || Opc == UO_PostDec) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9010 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 9011 | Args[1] = IntegerLiteral::Create(Context, Zero, Context.IntTy, |
| 9012 | SourceLocation()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9013 | NumArgs = 2; |
| 9014 | } |
| 9015 | |
| 9016 | if (Input->isTypeDependent()) { |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 9017 | if (Fns.empty()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9018 | return Owned(new (Context) UnaryOperator(Input, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9019 | Opc, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 9020 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9021 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 9022 | OpLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9023 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9024 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9025 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 9026 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 9027 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 9028 | /*ADL*/ true, IsOverloaded(Fns), |
| 9029 | Fns.begin(), Fns.end()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9030 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 9031 | &Args[0], NumArgs, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9032 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9033 | VK_RValue, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9034 | OpLoc)); |
| 9035 | } |
| 9036 | |
| 9037 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9038 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9039 | |
| 9040 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9041 | AddFunctionCandidates(Fns, &Args[0], NumArgs, CandidateSet, false); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9042 | |
| 9043 | // Add operator candidates that are member functions. |
| 9044 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 9045 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9046 | // Add candidates from ADL. |
| 9047 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Douglas Gregor | dc81c88 | 2010-02-05 05:15:43 +0000 | [diff] [blame] | 9048 | Args, NumArgs, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9049 | /*ExplicitTemplateArgs*/ 0, |
| 9050 | CandidateSet); |
| 9051 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9052 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 9053 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9054 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9055 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 9056 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9057 | // Perform overload resolution. |
| 9058 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9059 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9060 | case OR_Success: { |
| 9061 | // We found a built-in operator or an overloaded operator. |
| 9062 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9063 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9064 | if (FnDecl) { |
| 9065 | // We matched an overloaded operator. Build a call to that |
| 9066 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9067 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 9068 | MarkDeclarationReferenced(OpLoc, FnDecl); |
| 9069 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9070 | // Convert the arguments. |
| 9071 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9072 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 9073 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9074 | ExprResult InputRes = |
| 9075 | PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 9076 | Best->FoundDecl, Method); |
| 9077 | if (InputRes.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9078 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9079 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9080 | } else { |
| 9081 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9082 | ExprResult InputInit |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 9083 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 9084 | Context, |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 9085 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9086 | SourceLocation(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9087 | Input); |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 9088 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9089 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9090 | Input = InputInit.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9091 | } |
| 9092 | |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 9093 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 9094 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9095 | // Determine the result type. |
| 9096 | QualType ResultTy = FnDecl->getResultType(); |
| 9097 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 9098 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9099 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9100 | // Build the actual expression node. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9101 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
| 9102 | HadMultipleCandidates); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9103 | if (FnExpr.isInvalid()) |
| 9104 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9105 | |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 9106 | Args[0] = Input; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9107 | CallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9108 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9109 | Args, NumArgs, ResultTy, VK, OpLoc); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 9110 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9111 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 9112 | FnDecl)) |
| 9113 | return ExprError(); |
| 9114 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9115 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9116 | } else { |
| 9117 | // We matched a built-in operator. Convert the arguments, then |
| 9118 | // break out so that we will build the appropriate built-in |
| 9119 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9120 | ExprResult InputRes = |
| 9121 | PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 9122 | Best->Conversions[0], AA_Passing); |
| 9123 | if (InputRes.isInvalid()) |
| 9124 | return ExprError(); |
| 9125 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9126 | break; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9127 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9128 | } |
| 9129 | |
| 9130 | case OR_No_Viable_Function: |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9131 | // This is an erroneous use of an operator which can be overloaded by |
| 9132 | // a non-member function. Check for non-member operators which were |
| 9133 | // defined too late to be candidates. |
| 9134 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, Args, NumArgs)) |
| 9135 | // FIXME: Recover by calling the found function. |
| 9136 | return ExprError(); |
| 9137 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9138 | // No viable function; fall through to handling this as a |
| 9139 | // built-in operator, which will produce an error message for us. |
| 9140 | break; |
| 9141 | |
| 9142 | case OR_Ambiguous: |
| 9143 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 9144 | << UnaryOperator::getOpcodeStr(Opc) |
| 9145 | << Input->getType() |
| 9146 | << Input->getSourceRange(); |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 9147 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9148 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
| 9149 | return ExprError(); |
| 9150 | |
| 9151 | case OR_Deleted: |
| 9152 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 9153 | << Best->Function->isDeleted() |
| 9154 | << UnaryOperator::getOpcodeStr(Opc) |
| 9155 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 9156 | << Input->getSourceRange(); |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 9157 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs, |
| 9158 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9159 | return ExprError(); |
| 9160 | } |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9161 | |
| 9162 | // Either we found no viable overloaded operator or we matched a |
| 9163 | // built-in operator. In either case, fall through to trying to |
| 9164 | // build a built-in operation. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9165 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9166 | } |
| 9167 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9168 | /// \brief Create a binary operation that may resolve to an overloaded |
| 9169 | /// operator. |
| 9170 | /// |
| 9171 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 9172 | /// |
| 9173 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 9174 | /// operator. |
| 9175 | /// |
| 9176 | /// \param Functions The set of non-member functions that will be |
| 9177 | /// considered by overload resolution. The caller needs to build this |
| 9178 | /// set based on the context using, e.g., |
| 9179 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 9180 | /// set should not contain any member functions; those will be added |
| 9181 | /// by CreateOverloadedBinOp(). |
| 9182 | /// |
| 9183 | /// \param LHS Left-hand argument. |
| 9184 | /// \param RHS Right-hand argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9185 | ExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9186 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9187 | unsigned OpcIn, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9188 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9189 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9190 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 9191 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9192 | |
| 9193 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 9194 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 9195 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 9196 | |
| 9197 | // If either side is type-dependent, create an appropriate dependent |
| 9198 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 9199 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9200 | if (Fns.empty()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9201 | // If there are no functions to store, just build a dependent |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9202 | // BinaryOperator or CompoundAssignment. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9203 | if (Opc <= BO_Assign || Opc > BO_OrAssign) |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9204 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9205 | Context.DependentTy, |
| 9206 | VK_RValue, OK_Ordinary, |
| 9207 | OpLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9208 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9209 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 9210 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9211 | VK_LValue, |
| 9212 | OK_Ordinary, |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9213 | Context.DependentTy, |
| 9214 | Context.DependentTy, |
| 9215 | OpLoc)); |
| 9216 | } |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9217 | |
| 9218 | // FIXME: save results of ADL from here? |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9219 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9220 | // TODO: provide better source location info in DNLoc component. |
| 9221 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9222 | UnresolvedLookupExpr *Fn |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 9223 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
| 9224 | NestedNameSpecifierLoc(), OpNameInfo, |
| 9225 | /*ADL*/ true, IsOverloaded(Fns), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 9226 | Fns.begin(), Fns.end()); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9227 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9228 | Args, 2, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9229 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9230 | VK_RValue, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9231 | OpLoc)); |
| 9232 | } |
| 9233 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9234 | // Always do placeholder-like conversions on the RHS. |
| 9235 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 9236 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 9237 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 9238 | // Do placeholder-like conversion on the LHS; note that we should |
| 9239 | // not get here with a PseudoObject LHS. |
| 9240 | assert(Args[0]->getObjectKind() != OK_ObjCProperty); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9241 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 9242 | return ExprError(); |
| 9243 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 9244 | // If this is the assignment operator, we only perform overload resolution |
| 9245 | // if the left-hand side is a class or enumeration type. This is actually |
| 9246 | // a hack. The standard requires that we do overload resolution between the |
| 9247 | // various built-in candidates, but as DR507 points out, this can lead to |
| 9248 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 9249 | // Note that we go the traditional code path for compound assignment forms. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9250 | if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 9251 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9252 | |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 9253 | // If this is the .* operator, which is not overloadable, just |
| 9254 | // create a built-in binary operator. |
| 9255 | if (Opc == BO_PtrMemD) |
| 9256 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 9257 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9258 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9259 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9260 | |
| 9261 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9262 | AddFunctionCandidates(Fns, Args, 2, CandidateSet, false); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9263 | |
| 9264 | // Add operator candidates that are member functions. |
| 9265 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 9266 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9267 | // Add candidates from ADL. |
| 9268 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
| 9269 | Args, 2, |
| 9270 | /*ExplicitTemplateArgs*/ 0, |
| 9271 | CandidateSet); |
| 9272 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9273 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 9274 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9275 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9276 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 9277 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9278 | // Perform overload resolution. |
| 9279 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9280 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 9281 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9282 | // We found a built-in operator or an overloaded operator. |
| 9283 | FunctionDecl *FnDecl = Best->Function; |
| 9284 | |
| 9285 | if (FnDecl) { |
| 9286 | // We matched an overloaded operator. Build a call to that |
| 9287 | // operator. |
| 9288 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 9289 | MarkDeclarationReferenced(OpLoc, FnDecl); |
| 9290 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9291 | // Convert the arguments. |
| 9292 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 9293 | // Best->Access is only meaningful for class members. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9294 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 9295 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 9296 | ExprResult Arg1 = |
| 9297 | PerformCopyInitialization( |
| 9298 | InitializedEntity::InitializeParameter(Context, |
| 9299 | FnDecl->getParamDecl(0)), |
| 9300 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 9301 | if (Arg1.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9302 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 9303 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9304 | ExprResult Arg0 = |
| 9305 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 9306 | Best->FoundDecl, Method); |
| 9307 | if (Arg0.isInvalid()) |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 9308 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9309 | Args[0] = Arg0.takeAs<Expr>(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 9310 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9311 | } else { |
| 9312 | // Convert the arguments. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 9313 | ExprResult Arg0 = PerformCopyInitialization( |
| 9314 | InitializedEntity::InitializeParameter(Context, |
| 9315 | FnDecl->getParamDecl(0)), |
| 9316 | SourceLocation(), Owned(Args[0])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 9317 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9318 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 9319 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 9320 | ExprResult Arg1 = |
| 9321 | PerformCopyInitialization( |
| 9322 | InitializedEntity::InitializeParameter(Context, |
| 9323 | FnDecl->getParamDecl(1)), |
| 9324 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 9325 | if (Arg1.isInvalid()) |
| 9326 | return ExprError(); |
| 9327 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 9328 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9329 | } |
| 9330 | |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 9331 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 9332 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9333 | // Determine the result type. |
| 9334 | QualType ResultTy = FnDecl->getResultType(); |
| 9335 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 9336 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9337 | |
| 9338 | // Build the actual expression node. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9339 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
| 9340 | HadMultipleCandidates, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9341 | if (FnExpr.isInvalid()) |
| 9342 | return ExprError(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9343 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9344 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9345 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9346 | Args, 2, ResultTy, VK, OpLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9347 | |
| 9348 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 9349 | FnDecl)) |
| 9350 | return ExprError(); |
| 9351 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9352 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9353 | } else { |
| 9354 | // We matched a built-in operator. Convert the arguments, then |
| 9355 | // break out so that we will build the appropriate built-in |
| 9356 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9357 | ExprResult ArgsRes0 = |
| 9358 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 9359 | Best->Conversions[0], AA_Passing); |
| 9360 | if (ArgsRes0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9361 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9362 | Args[0] = ArgsRes0.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9363 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9364 | ExprResult ArgsRes1 = |
| 9365 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 9366 | Best->Conversions[1], AA_Passing); |
| 9367 | if (ArgsRes1.isInvalid()) |
| 9368 | return ExprError(); |
| 9369 | Args[1] = ArgsRes1.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9370 | break; |
| 9371 | } |
| 9372 | } |
| 9373 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 9374 | case OR_No_Viable_Function: { |
| 9375 | // C++ [over.match.oper]p9: |
| 9376 | // If the operator is the operator , [...] and there are no |
| 9377 | // viable functions, then the operator is assumed to be the |
| 9378 | // built-in operator and interpreted according to clause 5. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9379 | if (Opc == BO_Comma) |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 9380 | break; |
| 9381 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 9382 | // For class as left operand for assignment or compound assigment |
| 9383 | // operator do not fall through to handling in built-in, but report that |
| 9384 | // no overloaded assignment operator found |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9385 | ExprResult Result = ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9386 | if (Args[0]->getType()->isRecordType() && |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9387 | Opc >= BO_Assign && Opc <= BO_OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 9388 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 9389 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 9390 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 9391 | } else { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 9392 | // This is an erroneous use of an operator which can be overloaded by |
| 9393 | // a non-member function. Check for non-member operators which were |
| 9394 | // defined too late to be candidates. |
| 9395 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, Args, 2)) |
| 9396 | // FIXME: Recover by calling the found function. |
| 9397 | return ExprError(); |
| 9398 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 9399 | // No viable function; try to create a built-in operation, which will |
| 9400 | // produce an error. Then, show the non-viable candidates. |
| 9401 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 9402 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9403 | assert(Result.isInvalid() && |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 9404 | "C++ binary operator overloading is missing candidates!"); |
| 9405 | if (Result.isInvalid()) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9406 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 9407 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 9408 | return move(Result); |
| 9409 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9410 | |
| 9411 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 9412 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_binary) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9413 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 9414 | << Args[0]->getType() << Args[1]->getType() |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 9415 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9416 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, 2, |
| 9417 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9418 | return ExprError(); |
| 9419 | |
| 9420 | case OR_Deleted: |
| 9421 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 9422 | << Best->Function->isDeleted() |
| 9423 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 9424 | << getDeletedOrUnavailableSuffix(Best->Function) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 9425 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 9426 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 9427 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9428 | return ExprError(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 9429 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9430 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 9431 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 9432 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 9433 | } |
| 9434 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9435 | ExprResult |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9436 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 9437 | SourceLocation RLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9438 | Expr *Base, Expr *Idx) { |
| 9439 | Expr *Args[2] = { Base, Idx }; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9440 | DeclarationName OpName = |
| 9441 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 9442 | |
| 9443 | // If either side is type-dependent, create an appropriate dependent |
| 9444 | // expression. |
| 9445 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 9446 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9447 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9448 | // CHECKME: no 'operator' keyword? |
| 9449 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
| 9450 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9451 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 9452 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 9453 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 9454 | /*ADL*/ true, /*Overloaded*/ false, |
| 9455 | UnresolvedSetIterator(), |
| 9456 | UnresolvedSetIterator()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 9457 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9458 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9459 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 9460 | Args, 2, |
| 9461 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9462 | VK_RValue, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9463 | RLoc)); |
| 9464 | } |
| 9465 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9466 | // Handle placeholders on both operands. |
| 9467 | if (checkPlaceholderForOverload(*this, Args[0])) |
| 9468 | return ExprError(); |
| 9469 | if (checkPlaceholderForOverload(*this, Args[1])) |
| 9470 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 9471 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9472 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9473 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9474 | |
| 9475 | // Subscript can only be overloaded as a member function. |
| 9476 | |
| 9477 | // Add operator candidates that are member functions. |
| 9478 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 9479 | |
| 9480 | // Add builtin operator candidates. |
| 9481 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 9482 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9483 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 9484 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9485 | // Perform overload resolution. |
| 9486 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9487 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9488 | case OR_Success: { |
| 9489 | // We found a built-in operator or an overloaded operator. |
| 9490 | FunctionDecl *FnDecl = Best->Function; |
| 9491 | |
| 9492 | if (FnDecl) { |
| 9493 | // We matched an overloaded operator. Build a call to that |
| 9494 | // operator. |
| 9495 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 9496 | MarkDeclarationReferenced(LLoc, FnDecl); |
| 9497 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9498 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 9499 | DiagnoseUseOfDecl(Best->FoundDecl, LLoc); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9500 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9501 | // Convert the arguments. |
| 9502 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9503 | ExprResult Arg0 = |
| 9504 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 9505 | Best->FoundDecl, Method); |
| 9506 | if (Arg0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9507 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9508 | Args[0] = Arg0.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9509 | |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 9510 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9511 | ExprResult InputInit |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 9512 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 9513 | Context, |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 9514 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9515 | SourceLocation(), |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 9516 | Owned(Args[1])); |
| 9517 | if (InputInit.isInvalid()) |
| 9518 | return ExprError(); |
| 9519 | |
| 9520 | Args[1] = InputInit.takeAs<Expr>(); |
| 9521 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9522 | // Determine the result type |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9523 | QualType ResultTy = FnDecl->getResultType(); |
| 9524 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 9525 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9526 | |
| 9527 | // Build the actual expression node. |
Douglas Gregor | 5b8968c | 2011-07-15 16:25:15 +0000 | [diff] [blame] | 9528 | DeclarationNameLoc LocInfo; |
| 9529 | LocInfo.CXXOperatorName.BeginOpNameLoc = LLoc.getRawEncoding(); |
| 9530 | LocInfo.CXXOperatorName.EndOpNameLoc = RLoc.getRawEncoding(); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9531 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, |
| 9532 | HadMultipleCandidates, |
| 9533 | LLoc, LocInfo); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9534 | if (FnExpr.isInvalid()) |
| 9535 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9536 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9537 | CXXOperatorCallExpr *TheCall = |
| 9538 | new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9539 | FnExpr.take(), Args, 2, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9540 | ResultTy, VK, RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9541 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9542 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9543 | FnDecl)) |
| 9544 | return ExprError(); |
| 9545 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9546 | return MaybeBindToTemporary(TheCall); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9547 | } else { |
| 9548 | // We matched a built-in operator. Convert the arguments, then |
| 9549 | // break out so that we will build the appropriate built-in |
| 9550 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9551 | ExprResult ArgsRes0 = |
| 9552 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 9553 | Best->Conversions[0], AA_Passing); |
| 9554 | if (ArgsRes0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9555 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9556 | Args[0] = ArgsRes0.take(); |
| 9557 | |
| 9558 | ExprResult ArgsRes1 = |
| 9559 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 9560 | Best->Conversions[1], AA_Passing); |
| 9561 | if (ArgsRes1.isInvalid()) |
| 9562 | return ExprError(); |
| 9563 | Args[1] = ArgsRes1.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9564 | |
| 9565 | break; |
| 9566 | } |
| 9567 | } |
| 9568 | |
| 9569 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 9570 | if (CandidateSet.empty()) |
| 9571 | Diag(LLoc, diag::err_ovl_no_oper) |
| 9572 | << Args[0]->getType() << /*subscript*/ 0 |
| 9573 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 9574 | else |
| 9575 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 9576 | << Args[0]->getType() |
| 9577 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9578 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 9579 | "[]", LLoc); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 9580 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9581 | } |
| 9582 | |
| 9583 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 9584 | Diag(LLoc, diag::err_ovl_ambiguous_oper_binary) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9585 | << "[]" |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 9586 | << Args[0]->getType() << Args[1]->getType() |
| 9587 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9588 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, 2, |
| 9589 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9590 | return ExprError(); |
| 9591 | |
| 9592 | case OR_Deleted: |
| 9593 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 9594 | << Best->Function->isDeleted() << "[]" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 9595 | << getDeletedOrUnavailableSuffix(Best->Function) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9596 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9597 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 9598 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9599 | return ExprError(); |
| 9600 | } |
| 9601 | |
| 9602 | // We matched a built-in operator; build it. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9603 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9604 | } |
| 9605 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9606 | /// BuildCallToMemberFunction - Build a call to a member |
| 9607 | /// function. MemExpr is the expression that refers to the member |
| 9608 | /// function (and includes the object parameter), Args/NumArgs are the |
| 9609 | /// arguments to the function call (not including the object |
| 9610 | /// parameter). The caller needs to validate that the member |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9611 | /// expression refers to a non-static member function or an overloaded |
| 9612 | /// member function. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9613 | ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9614 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 9615 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 9616 | unsigned NumArgs, SourceLocation RParenLoc) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9617 | assert(MemExprE->getType() == Context.BoundMemberTy || |
| 9618 | MemExprE->getType() == Context.OverloadTy); |
| 9619 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9620 | // Dig out the member expression. This holds both the object |
| 9621 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9622 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9623 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9624 | // Determine whether this is a call to a pointer-to-member function. |
| 9625 | if (BinaryOperator *op = dyn_cast<BinaryOperator>(NakedMemExpr)) { |
| 9626 | assert(op->getType() == Context.BoundMemberTy); |
| 9627 | assert(op->getOpcode() == BO_PtrMemD || op->getOpcode() == BO_PtrMemI); |
| 9628 | |
| 9629 | QualType fnType = |
| 9630 | op->getRHS()->getType()->castAs<MemberPointerType>()->getPointeeType(); |
| 9631 | |
| 9632 | const FunctionProtoType *proto = fnType->castAs<FunctionProtoType>(); |
| 9633 | QualType resultType = proto->getCallResultType(Context); |
| 9634 | ExprValueKind valueKind = Expr::getValueKindForType(proto->getResultType()); |
| 9635 | |
| 9636 | // Check that the object type isn't more qualified than the |
| 9637 | // member function we're calling. |
| 9638 | Qualifiers funcQuals = Qualifiers::fromCVRMask(proto->getTypeQuals()); |
| 9639 | |
| 9640 | QualType objectType = op->getLHS()->getType(); |
| 9641 | if (op->getOpcode() == BO_PtrMemI) |
| 9642 | objectType = objectType->castAs<PointerType>()->getPointeeType(); |
| 9643 | Qualifiers objectQuals = objectType.getQualifiers(); |
| 9644 | |
| 9645 | Qualifiers difference = objectQuals - funcQuals; |
| 9646 | difference.removeObjCGCAttr(); |
| 9647 | difference.removeAddressSpace(); |
| 9648 | if (difference) { |
| 9649 | std::string qualsString = difference.getAsString(); |
| 9650 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
| 9651 | << fnType.getUnqualifiedType() |
| 9652 | << qualsString |
| 9653 | << (qualsString.find(' ') == std::string::npos ? 1 : 2); |
| 9654 | } |
| 9655 | |
| 9656 | CXXMemberCallExpr *call |
| 9657 | = new (Context) CXXMemberCallExpr(Context, MemExprE, Args, NumArgs, |
| 9658 | resultType, valueKind, RParenLoc); |
| 9659 | |
| 9660 | if (CheckCallReturnType(proto->getResultType(), |
| 9661 | op->getRHS()->getSourceRange().getBegin(), |
| 9662 | call, 0)) |
| 9663 | return ExprError(); |
| 9664 | |
| 9665 | if (ConvertArgumentsForCall(call, op, 0, proto, Args, NumArgs, RParenLoc)) |
| 9666 | return ExprError(); |
| 9667 | |
| 9668 | return MaybeBindToTemporary(call); |
| 9669 | } |
| 9670 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9671 | UnbridgedCastsSet UnbridgedCasts; |
| 9672 | if (checkArgPlaceholdersForOverload(*this, Args, NumArgs, UnbridgedCasts)) |
| 9673 | return ExprError(); |
| 9674 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9675 | MemberExpr *MemExpr; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9676 | CXXMethodDecl *Method = 0; |
John McCall | bb6fb46 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 9677 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 9678 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9679 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 9680 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9681 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9682 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 9683 | Qualifier = MemExpr->getQualifier(); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9684 | UnbridgedCasts.restore(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9685 | } else { |
| 9686 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 9687 | Qualifier = UnresExpr->getQualifier(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9688 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 9689 | QualType ObjectType = UnresExpr->getBaseType(); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 9690 | Expr::Classification ObjectClassification |
| 9691 | = UnresExpr->isArrow()? Expr::Classification::makeSimpleLValue() |
| 9692 | : UnresExpr->getBase()->Classify(Context); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9693 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9694 | // Add overload candidates |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9695 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9696 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9697 | // FIXME: avoid copy. |
| 9698 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 9699 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 9700 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 9701 | TemplateArgs = &TemplateArgsBuffer; |
| 9702 | } |
| 9703 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9704 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 9705 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 9706 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 9707 | NamedDecl *Func = *I; |
| 9708 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 9709 | if (isa<UsingShadowDecl>(Func)) |
| 9710 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 9711 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 9712 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 9713 | // Microsoft supports direct constructor calls. |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 9714 | if (getLangOptions().MicrosoftExt && isa<CXXConstructorDecl>(Func)) { |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 9715 | AddOverloadCandidate(cast<CXXConstructorDecl>(Func), I.getPair(), Args, NumArgs, |
| 9716 | CandidateSet); |
| 9717 | } else if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 9718 | // If explicit template arguments were provided, we can't call a |
| 9719 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9720 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 9721 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9722 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9723 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9724 | ObjectClassification, |
| 9725 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 9726 | /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 9727 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9728 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9729 | I.getPair(), ActingDC, TemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9730 | ObjectType, ObjectClassification, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 9731 | Args, NumArgs, CandidateSet, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 9732 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 9733 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 9734 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9735 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9736 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 9737 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9738 | UnbridgedCasts.restore(); |
| 9739 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9740 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9741 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getLocStart(), |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 9742 | Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9743 | case OR_Success: |
| 9744 | Method = cast<CXXMethodDecl>(Best->Function); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 9745 | MarkDeclarationReferenced(UnresExpr->getMemberLoc(), Method); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9746 | FoundDecl = Best->FoundDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9747 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 9748 | DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9749 | break; |
| 9750 | |
| 9751 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9752 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9753 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 9754 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9755 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9756 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9757 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9758 | |
| 9759 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9760 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 9761 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9762 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9763 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9764 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9765 | |
| 9766 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9767 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9768 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 9769 | << DeclName |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 9770 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 9771 | << MemExprE->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9772 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9773 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9774 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9775 | } |
| 9776 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9777 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9778 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9779 | // If overload resolution picked a static member, build a |
| 9780 | // non-member call based on that function. |
| 9781 | if (Method->isStatic()) { |
| 9782 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 9783 | Args, NumArgs, RParenLoc); |
| 9784 | } |
| 9785 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9786 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9787 | } |
| 9788 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9789 | QualType ResultType = Method->getResultType(); |
| 9790 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 9791 | ResultType = ResultType.getNonLValueExprType(Context); |
| 9792 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9793 | assert(Method && "Member call to something that isn't a method?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9794 | CXXMemberCallExpr *TheCall = |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9795 | new (Context) CXXMemberCallExpr(Context, MemExprE, Args, NumArgs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9796 | ResultType, VK, RParenLoc); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9797 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 9798 | // Check for a valid return type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9799 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9800 | TheCall, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9801 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9802 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9803 | // Convert the object argument (for a non-static member function call). |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9804 | // We only need to do this if there was actually an overload; otherwise |
| 9805 | // it was done at lookup. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9806 | if (!Method->isStatic()) { |
| 9807 | ExprResult ObjectArg = |
| 9808 | PerformObjectArgumentInitialization(MemExpr->getBase(), Qualifier, |
| 9809 | FoundDecl, Method); |
| 9810 | if (ObjectArg.isInvalid()) |
| 9811 | return ExprError(); |
| 9812 | MemExpr->setBase(ObjectArg.take()); |
| 9813 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9814 | |
| 9815 | // Convert the rest of the arguments |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 9816 | const FunctionProtoType *Proto = |
| 9817 | Method->getType()->getAs<FunctionProtoType>(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9818 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9819 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9820 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9821 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9822 | if (CheckFunctionCall(Method, TheCall)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9823 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 9824 | |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 9825 | if ((isa<CXXConstructorDecl>(CurContext) || |
| 9826 | isa<CXXDestructorDecl>(CurContext)) && |
| 9827 | TheCall->getMethodDecl()->isPure()) { |
| 9828 | const CXXMethodDecl *MD = TheCall->getMethodDecl(); |
| 9829 | |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 9830 | if (isa<CXXThisExpr>(MemExpr->getBase()->IgnoreParenCasts())) { |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 9831 | Diag(MemExpr->getLocStart(), |
| 9832 | diag::warn_call_to_pure_virtual_member_function_from_ctor_dtor) |
| 9833 | << MD->getDeclName() << isa<CXXDestructorDecl>(CurContext) |
| 9834 | << MD->getParent()->getDeclName(); |
| 9835 | |
| 9836 | Diag(MD->getLocStart(), diag::note_previous_decl) << MD->getDeclName(); |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 9837 | } |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 9838 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9839 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9840 | } |
| 9841 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9842 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 9843 | /// type (C++ [over.call.object]), which can end up invoking an |
| 9844 | /// overloaded function call operator (@c operator()) or performing a |
| 9845 | /// user-defined conversion on the object argument. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9846 | ExprResult |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9847 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 9848 | SourceLocation LParenLoc, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9849 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9850 | SourceLocation RParenLoc) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9851 | if (checkPlaceholderForOverload(*this, Obj)) |
| 9852 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9853 | ExprResult Object = Owned(Obj); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9854 | |
| 9855 | UnbridgedCastsSet UnbridgedCasts; |
| 9856 | if (checkArgPlaceholdersForOverload(*this, Args, NumArgs, UnbridgedCasts)) |
| 9857 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 9858 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9859 | assert(Object.get()->getType()->isRecordType() && "Requires object type argument"); |
| 9860 | const RecordType *Record = Object.get()->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9861 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9862 | // C++ [over.call.object]p1: |
| 9863 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 9864 | // 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] | 9865 | // candidate functions includes at least the function call |
| 9866 | // operators of T. The function call operators of T are obtained by |
| 9867 | // ordinary lookup of the name operator() in the context of |
| 9868 | // (E).operator(). |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9869 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 9870 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 9871 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9872 | if (RequireCompleteType(LParenLoc, Object.get()->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 9873 | PDiag(diag::err_incomplete_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9874 | << Object.get()->getSourceRange())) |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 9875 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9876 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9877 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 9878 | LookupQualifiedName(R, Record->getDecl()); |
| 9879 | R.suppressDiagnostics(); |
| 9880 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 9881 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 9882 | Oper != OperEnd; ++Oper) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9883 | AddMethodCandidate(Oper.getPair(), Object.get()->getType(), |
| 9884 | Object.get()->Classify(Context), Args, NumArgs, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 9885 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 9886 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9887 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9888 | // C++ [over.call.object]p2: |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 9889 | // In addition, for each (non-explicit in C++0x) conversion function |
| 9890 | // declared in T of the form |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9891 | // |
| 9892 | // operator conversion-type-id () cv-qualifier; |
| 9893 | // |
| 9894 | // where cv-qualifier is the same cv-qualification as, or a |
| 9895 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 9896 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 9897 | // R", or the type "reference to pointer to function of |
| 9898 | // (P1,...,Pn) returning R", or the type "reference to function |
| 9899 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9900 | // is also considered as a candidate function. Similarly, |
| 9901 | // surrogate call functions are added to the set of candidate |
| 9902 | // functions for each conversion function declared in an |
| 9903 | // accessible base class provided the function is not hidden |
| 9904 | // within T by another intervening declaration. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 9905 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 9906 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 9907 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9908 | E = Conversions->end(); I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 9909 | NamedDecl *D = *I; |
| 9910 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 9911 | if (isa<UsingShadowDecl>(D)) |
| 9912 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9913 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 9914 | // Skip over templated conversion functions; they aren't |
| 9915 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 9916 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 9917 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 9918 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 9919 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 9920 | if (!Conv->isExplicit()) { |
| 9921 | // Strip the reference type (if any) and then the pointer type (if |
| 9922 | // any) to get down to what might be a function type. |
| 9923 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 9924 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 9925 | ConvType = ConvPtrType->getPointeeType(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9926 | |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 9927 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
| 9928 | { |
| 9929 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
| 9930 | Object.get(), Args, NumArgs, CandidateSet); |
| 9931 | } |
| 9932 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9933 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9934 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9935 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 9936 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9937 | // Perform overload resolution. |
| 9938 | OverloadCandidateSet::iterator Best; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9939 | switch (CandidateSet.BestViableFunction(*this, Object.get()->getLocStart(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9940 | Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9941 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9942 | // Overload resolution succeeded; we'll build the appropriate call |
| 9943 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9944 | break; |
| 9945 | |
| 9946 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 9947 | if (CandidateSet.empty()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9948 | Diag(Object.get()->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 9949 | << Object.get()->getType() << /*call*/ 1 |
| 9950 | << Object.get()->getSourceRange(); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 9951 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9952 | Diag(Object.get()->getSourceRange().getBegin(), |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 9953 | diag::err_ovl_no_viable_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9954 | << Object.get()->getType() << Object.get()->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9955 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9956 | break; |
| 9957 | |
| 9958 | case OR_Ambiguous: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9959 | Diag(Object.get()->getSourceRange().getBegin(), |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9960 | diag::err_ovl_ambiguous_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9961 | << Object.get()->getType() << Object.get()->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9962 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9963 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9964 | |
| 9965 | case OR_Deleted: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9966 | Diag(Object.get()->getSourceRange().getBegin(), |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9967 | diag::err_ovl_deleted_object_call) |
| 9968 | << Best->Function->isDeleted() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9969 | << Object.get()->getType() |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 9970 | << getDeletedOrUnavailableSuffix(Best->Function) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9971 | << Object.get()->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9972 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9973 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9974 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9975 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 9976 | if (Best == CandidateSet.end()) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9977 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9978 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 9979 | UnbridgedCasts.restore(); |
| 9980 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9981 | if (Best->Function == 0) { |
| 9982 | // Since there is no function declaration, this is one of the |
| 9983 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9984 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9985 | = cast<CXXConversionDecl>( |
| 9986 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 9987 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9988 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 9989 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 9990 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9991 | // We selected one of the surrogate functions that converts the |
| 9992 | // object parameter to a function pointer. Perform the conversion |
| 9993 | // on the object argument, then let ActOnCallExpr finish the job. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9994 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 9995 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 9996 | // and then call it. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9997 | ExprResult Call = BuildCXXMemberCallExpr(Object.get(), Best->FoundDecl, |
| 9998 | Conv, HadMultipleCandidates); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 9999 | if (Call.isInvalid()) |
| 10000 | return ExprError(); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 10001 | // Record usage of conversion in an implicit cast. |
| 10002 | Call = Owned(ImplicitCastExpr::Create(Context, Call.get()->getType(), |
| 10003 | CK_UserDefinedConversion, |
| 10004 | Call.get(), 0, VK_RValue)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10005 | |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 10006 | return ActOnCallExpr(S, Call.get(), LParenLoc, MultiExprArg(Args, NumArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 10007 | RParenLoc); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10008 | } |
| 10009 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 10010 | MarkDeclarationReferenced(LParenLoc, Best->Function); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10011 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10012 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 10013 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 10014 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 10015 | // that calls this method, using Object for the implicit object |
| 10016 | // parameter and passing along the remaining arguments. |
| 10017 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10018 | const FunctionProtoType *Proto = |
| 10019 | Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10020 | |
| 10021 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 10022 | unsigned NumArgsToCheck = NumArgs; |
| 10023 | |
| 10024 | // Build the full argument list for the method call (the |
| 10025 | // implicit object parameter is placed at the beginning of the |
| 10026 | // list). |
| 10027 | Expr **MethodArgs; |
| 10028 | if (NumArgs < NumArgsInProto) { |
| 10029 | NumArgsToCheck = NumArgsInProto; |
| 10030 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 10031 | } else { |
| 10032 | MethodArgs = new Expr*[NumArgs + 1]; |
| 10033 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10034 | MethodArgs[0] = Object.get(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10035 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 10036 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10037 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10038 | ExprResult NewFn = CreateFunctionRefExpr(*this, Method, |
| 10039 | HadMultipleCandidates); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10040 | if (NewFn.isInvalid()) |
| 10041 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10042 | |
| 10043 | // Once we've built TheCall, all of the expressions are properly |
| 10044 | // owned. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10045 | QualType ResultTy = Method->getResultType(); |
| 10046 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10047 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 10048 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10049 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10050 | new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn.take(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10051 | MethodArgs, NumArgs + 1, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10052 | ResultTy, VK, RParenLoc); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10053 | delete [] MethodArgs; |
| 10054 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10055 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall, |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 10056 | Method)) |
| 10057 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10058 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 10059 | // We may have default arguments. If so, we need to allocate more |
| 10060 | // slots in the call for them. |
| 10061 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 10062 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 10063 | else if (NumArgs > NumArgsInProto) |
| 10064 | NumArgsToCheck = NumArgsInProto; |
| 10065 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 10066 | bool IsError = false; |
| 10067 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10068 | // Initialize the implicit object parameter. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10069 | ExprResult ObjRes = |
| 10070 | PerformObjectArgumentInitialization(Object.get(), /*Qualifier=*/0, |
| 10071 | Best->FoundDecl, Method); |
| 10072 | if (ObjRes.isInvalid()) |
| 10073 | IsError = true; |
| 10074 | else |
| 10075 | Object = move(ObjRes); |
| 10076 | TheCall->setArg(0, Object.take()); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 10077 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10078 | // Check the argument types. |
| 10079 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10080 | Expr *Arg; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 10081 | if (i < NumArgs) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10082 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10083 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 10084 | // Pass the argument. |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 10085 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10086 | ExprResult InputInit |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 10087 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 10088 | Context, |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 10089 | Method->getParamDecl(i)), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10090 | SourceLocation(), Arg); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10091 | |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 10092 | IsError |= InputInit.isInvalid(); |
| 10093 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 10094 | } else { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10095 | ExprResult DefArg |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 10096 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 10097 | if (DefArg.isInvalid()) { |
| 10098 | IsError = true; |
| 10099 | break; |
| 10100 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10101 | |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 10102 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 10103 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10104 | |
| 10105 | TheCall->setArg(i + 1, Arg); |
| 10106 | } |
| 10107 | |
| 10108 | // If this is a variadic call, handle args passed through "...". |
| 10109 | if (Proto->isVariadic()) { |
| 10110 | // Promote the arguments (C99 6.5.2.2p7). |
| 10111 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10112 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0); |
| 10113 | IsError |= Arg.isInvalid(); |
| 10114 | TheCall->setArg(i + 1, Arg.take()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10115 | } |
| 10116 | } |
| 10117 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 10118 | if (IsError) return true; |
| 10119 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10120 | if (CheckFunctionCall(Method, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 10121 | return true; |
| 10122 | |
John McCall | 182f709 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 10123 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 10124 | } |
| 10125 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10126 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10127 | /// (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] | 10128 | /// @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] | 10129 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10130 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc) { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10131 | assert(Base->getType()->isRecordType() && |
| 10132 | "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10133 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10134 | if (checkPlaceholderForOverload(*this, Base)) |
| 10135 | return ExprError(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 10136 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10137 | SourceLocation Loc = Base->getExprLoc(); |
| 10138 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10139 | // C++ [over.ref]p1: |
| 10140 | // |
| 10141 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 10142 | // for a class object x of type T if T::operator->() exists and if |
| 10143 | // the operator is selected as the best match function by the |
| 10144 | // overload resolution mechanism (13.3). |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 10145 | DeclarationName OpName = |
| 10146 | Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10147 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 10148 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 10149 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 10150 | if (RequireCompleteType(Loc, Base->getType(), |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 10151 | PDiag(diag::err_typecheck_incomplete_tag) |
| 10152 | << Base->getSourceRange())) |
| 10153 | return ExprError(); |
| 10154 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 10155 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 10156 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 10157 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 10158 | |
| 10159 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 10160 | Oper != OperEnd; ++Oper) { |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 10161 | AddMethodCandidate(Oper.getPair(), Base->getType(), Base->Classify(Context), |
| 10162 | 0, 0, CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 10163 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10164 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10165 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 10166 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10167 | // Perform overload resolution. |
| 10168 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10169 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10170 | case OR_Success: |
| 10171 | // Overload resolution succeeded; we'll build the call below. |
| 10172 | break; |
| 10173 | |
| 10174 | case OR_No_Viable_Function: |
| 10175 | if (CandidateSet.empty()) |
| 10176 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 10177 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10178 | else |
| 10179 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 10180 | << "operator->" << Base->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10181 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 10182 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10183 | |
| 10184 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 10185 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 10186 | << "->" << Base->getType() << Base->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10187 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 10188 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 10189 | |
| 10190 | case OR_Deleted: |
| 10191 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 10192 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 10193 | << "->" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 10194 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 10195 | << Base->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 10196 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 10197 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10198 | } |
| 10199 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 10200 | MarkDeclarationReferenced(OpLoc, Best->Function); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10201 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 10202 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 10203 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10204 | // Convert the object parameter. |
| 10205 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10206 | ExprResult BaseResult = |
| 10207 | PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 10208 | Best->FoundDecl, Method); |
| 10209 | if (BaseResult.isInvalid()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 10210 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10211 | Base = BaseResult.take(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 10212 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10213 | // Build the operator call. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10214 | ExprResult FnExpr = CreateFunctionRefExpr(*this, Method, |
| 10215 | HadMultipleCandidates); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10216 | if (FnExpr.isInvalid()) |
| 10217 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10218 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10219 | QualType ResultTy = Method->getResultType(); |
| 10220 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 10221 | ResultTy = ResultTy.getNonLValueExprType(Context); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10222 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10223 | new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr.take(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10224 | &Base, 1, ResultTy, VK, OpLoc); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 10225 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10226 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 10227 | Method)) |
| 10228 | return ExprError(); |
Eli Friedman | d593190 | 2011-04-04 01:18:25 +0000 | [diff] [blame] | 10229 | |
| 10230 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 10231 | } |
| 10232 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 10233 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 10234 | /// a C++ overloaded function (possibly with some parentheses and |
| 10235 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 10236 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 10237 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 10238 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10239 | FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 10240 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10241 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 10242 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10243 | if (SubExpr == PE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 10244 | return PE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10245 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10246 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10247 | } |
| 10248 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10249 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10250 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 10251 | Found, Fn); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10252 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10253 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 10254 | "Implicit cast type cannot be determined from overload"); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 10255 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10256 | if (SubExpr == ICE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 10257 | return ICE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10258 | |
| 10259 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 10260 | ICE->getCastKind(), |
| 10261 | SubExpr, 0, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 10262 | ICE->getValueKind()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10263 | } |
| 10264 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10265 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10266 | assert(UnOp->getOpcode() == UO_AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 10267 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 10268 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 10269 | if (Method->isStatic()) { |
| 10270 | // Do nothing: static member functions aren't any different |
| 10271 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10272 | } else { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 10273 | // Fix the sub expression, which really has to be an |
| 10274 | // UnresolvedLookupExpr holding an overloaded member function |
| 10275 | // or template. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10276 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 10277 | Found, Fn); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10278 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 10279 | return UnOp; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10280 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10281 | assert(isa<DeclRefExpr>(SubExpr) |
| 10282 | && "fixed to something other than a decl ref"); |
| 10283 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 10284 | && "fixed to a member ref with no nested name qualifier"); |
| 10285 | |
| 10286 | // We have taken the address of a pointer to member |
| 10287 | // function. Perform the computation here so that we get the |
| 10288 | // appropriate pointer to member type. |
| 10289 | QualType ClassType |
| 10290 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 10291 | QualType MemPtrType |
| 10292 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 10293 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10294 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, MemPtrType, |
| 10295 | VK_RValue, OK_Ordinary, |
| 10296 | UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 10297 | } |
| 10298 | } |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10299 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 10300 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10301 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 10302 | return UnOp; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10303 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10304 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10305 | Context.getPointerType(SubExpr->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10306 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10307 | UnOp->getOperatorLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10308 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10309 | |
| 10310 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10311 | // FIXME: avoid copy. |
| 10312 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 10313 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10314 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 10315 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 10316 | } |
| 10317 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10318 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 10319 | ULE->getQualifierLoc(), |
| 10320 | Fn, |
| 10321 | ULE->getNameLoc(), |
| 10322 | Fn->getType(), |
| 10323 | VK_LValue, |
| 10324 | Found.getDecl(), |
| 10325 | TemplateArgs); |
| 10326 | DRE->setHadMultipleCandidates(ULE->getNumDecls() > 1); |
| 10327 | return DRE; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 10328 | } |
| 10329 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 10330 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 10331 | // FIXME: avoid copy. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10332 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 10333 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 10334 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 10335 | TemplateArgs = &TemplateArgsBuffer; |
| 10336 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 10337 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10338 | Expr *Base; |
| 10339 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10340 | // If we're filling in a static method where we used to have an |
| 10341 | // implicit member access, rewrite to a simple decl ref. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10342 | if (MemExpr->isImplicitAccess()) { |
| 10343 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10344 | DeclRefExpr *DRE = DeclRefExpr::Create(Context, |
| 10345 | MemExpr->getQualifierLoc(), |
| 10346 | Fn, |
| 10347 | MemExpr->getMemberLoc(), |
| 10348 | Fn->getType(), |
| 10349 | VK_LValue, |
| 10350 | Found.getDecl(), |
| 10351 | TemplateArgs); |
| 10352 | DRE->setHadMultipleCandidates(MemExpr->getNumDecls() > 1); |
| 10353 | return DRE; |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 10354 | } else { |
| 10355 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 10356 | if (MemExpr->getQualifier()) |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 10357 | Loc = MemExpr->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 10358 | Base = new (Context) CXXThisExpr(Loc, |
| 10359 | MemExpr->getBaseType(), |
| 10360 | /*isImplicit=*/true); |
| 10361 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10362 | } else |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 10363 | Base = MemExpr->getBase(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 10364 | |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 10365 | ExprValueKind valueKind; |
| 10366 | QualType type; |
| 10367 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 10368 | valueKind = VK_LValue; |
| 10369 | type = Fn->getType(); |
| 10370 | } else { |
| 10371 | valueKind = VK_RValue; |
| 10372 | type = Context.BoundMemberTy; |
| 10373 | } |
| 10374 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10375 | MemberExpr *ME = MemberExpr::Create(Context, Base, |
| 10376 | MemExpr->isArrow(), |
| 10377 | MemExpr->getQualifierLoc(), |
| 10378 | Fn, |
| 10379 | Found, |
| 10380 | MemExpr->getMemberNameInfo(), |
| 10381 | TemplateArgs, |
| 10382 | type, valueKind, OK_Ordinary); |
| 10383 | ME->setHadMultipleCandidates(true); |
| 10384 | return ME; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 10385 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10386 | |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 10387 | llvm_unreachable("Invalid reference to overloaded function"); |
| 10388 | return E; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 10389 | } |
| 10390 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 10391 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10392 | DeclAccessPair Found, |
| 10393 | FunctionDecl *Fn) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 10394 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 10395 | } |
| 10396 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 10397 | } // end namespace clang |