Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1 | //===--- SemaOverload.cpp - C++ Overloading ---------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides Sema routines for C++ overloading. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Lookup.h" |
| 16 | #include "clang/Sema/Initialization.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Template.h" |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 18 | #include "clang/Sema/TemplateDeduction.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 22 | #include "clang/AST/CXXInheritance.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 24 | #include "clang/AST/Expr.h" |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeOrdering.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | 661b493 | 2010-09-12 04:28:07 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseSet.h" |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 32 | #include <algorithm> |
| 33 | |
| 34 | namespace clang { |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 35 | using namespace sema; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 36 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 37 | /// A convenience routine for creating a decayed reference to a |
| 38 | /// function. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 39 | static ExprResult |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 40 | CreateFunctionRefExpr(Sema &S, FunctionDecl *Fn, |
Douglas Gregor | 5b8968c | 2011-07-15 16:25:15 +0000 | [diff] [blame] | 41 | SourceLocation Loc = SourceLocation(), |
| 42 | const DeclarationNameLoc &LocInfo = DeclarationNameLoc()){ |
| 43 | ExprResult E = S.Owned(new (S.Context) DeclRefExpr(Fn, Fn->getType(), |
| 44 | VK_LValue, Loc, LocInfo)); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 45 | E = S.DefaultFunctionArrayConversion(E.take()); |
| 46 | if (E.isInvalid()) |
| 47 | return ExprError(); |
| 48 | return move(E); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 49 | } |
| 50 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 51 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 52 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 53 | StandardConversionSequence &SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 54 | bool CStyle, |
| 55 | bool AllowObjCWritebackConversion); |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 56 | |
| 57 | static bool IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 58 | QualType &ToType, |
| 59 | bool InOverloadResolution, |
| 60 | StandardConversionSequence &SCS, |
| 61 | bool CStyle); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 62 | static OverloadingResult |
| 63 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 64 | UserDefinedConversionSequence& User, |
| 65 | OverloadCandidateSet& Conversions, |
| 66 | bool AllowExplicit); |
| 67 | |
| 68 | |
| 69 | static ImplicitConversionSequence::CompareKind |
| 70 | CompareStandardConversionSequences(Sema &S, |
| 71 | const StandardConversionSequence& SCS1, |
| 72 | const StandardConversionSequence& SCS2); |
| 73 | |
| 74 | static ImplicitConversionSequence::CompareKind |
| 75 | CompareQualificationConversions(Sema &S, |
| 76 | const StandardConversionSequence& SCS1, |
| 77 | const StandardConversionSequence& SCS2); |
| 78 | |
| 79 | static ImplicitConversionSequence::CompareKind |
| 80 | CompareDerivedToBaseConversions(Sema &S, |
| 81 | const StandardConversionSequence& SCS1, |
| 82 | const StandardConversionSequence& SCS2); |
| 83 | |
| 84 | |
| 85 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 86 | /// GetConversionCategory - Retrieve the implicit conversion |
| 87 | /// category corresponding to the given implicit conversion kind. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | ImplicitConversionCategory |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 89 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 90 | static const ImplicitConversionCategory |
| 91 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 92 | ICC_Identity, |
| 93 | ICC_Lvalue_Transformation, |
| 94 | ICC_Lvalue_Transformation, |
| 95 | ICC_Lvalue_Transformation, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 96 | ICC_Identity, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 97 | ICC_Qualification_Adjustment, |
| 98 | ICC_Promotion, |
| 99 | ICC_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 100 | ICC_Promotion, |
| 101 | ICC_Conversion, |
| 102 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 103 | ICC_Conversion, |
| 104 | ICC_Conversion, |
| 105 | ICC_Conversion, |
| 106 | ICC_Conversion, |
| 107 | ICC_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 108 | ICC_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 109 | ICC_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 110 | ICC_Conversion, |
| 111 | ICC_Conversion, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 112 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 113 | ICC_Conversion |
| 114 | }; |
| 115 | return Category[(int)Kind]; |
| 116 | } |
| 117 | |
| 118 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 119 | /// corresponding to the given implicit conversion kind. |
| 120 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 121 | static const ImplicitConversionRank |
| 122 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 123 | ICR_Exact_Match, |
| 124 | ICR_Exact_Match, |
| 125 | ICR_Exact_Match, |
| 126 | ICR_Exact_Match, |
| 127 | ICR_Exact_Match, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 128 | ICR_Exact_Match, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 129 | ICR_Promotion, |
| 130 | ICR_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 131 | ICR_Promotion, |
| 132 | ICR_Conversion, |
| 133 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 134 | ICR_Conversion, |
| 135 | ICR_Conversion, |
| 136 | ICR_Conversion, |
| 137 | ICR_Conversion, |
| 138 | ICR_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 139 | ICR_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 140 | ICR_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 141 | ICR_Conversion, |
| 142 | ICR_Conversion, |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 143 | ICR_Complex_Real_Conversion, |
| 144 | ICR_Conversion, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 145 | ICR_Conversion, |
| 146 | ICR_Writeback_Conversion |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 147 | }; |
| 148 | return Rank[(int)Kind]; |
| 149 | } |
| 150 | |
| 151 | /// GetImplicitConversionName - Return the name of this kind of |
| 152 | /// implicit conversion. |
| 153 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | 2550d70 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 154 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 155 | "No conversion", |
| 156 | "Lvalue-to-rvalue", |
| 157 | "Array-to-pointer", |
| 158 | "Function-to-pointer", |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 159 | "Noreturn adjustment", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 160 | "Qualification", |
| 161 | "Integral promotion", |
| 162 | "Floating point promotion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 163 | "Complex promotion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 164 | "Integral conversion", |
| 165 | "Floating conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 166 | "Complex conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 167 | "Floating-integral conversion", |
| 168 | "Pointer conversion", |
| 169 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 170 | "Boolean conversion", |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 171 | "Compatible-types conversion", |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 172 | "Derived-to-base conversion", |
| 173 | "Vector conversion", |
| 174 | "Vector splat", |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 175 | "Complex-real conversion", |
| 176 | "Block Pointer conversion", |
| 177 | "Transparent Union Conversion" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 178 | "Writeback conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 179 | }; |
| 180 | return Name[Kind]; |
| 181 | } |
| 182 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 183 | /// StandardConversionSequence - Set the standard conversion |
| 184 | /// sequence to the identity conversion. |
| 185 | void StandardConversionSequence::setAsIdentityConversion() { |
| 186 | First = ICK_Identity; |
| 187 | Second = ICK_Identity; |
| 188 | Third = ICK_Identity; |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 189 | DeprecatedStringLiteralToCharPtr = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 190 | QualificationIncludesObjCLifetime = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 191 | ReferenceBinding = false; |
| 192 | DirectBinding = false; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 193 | IsLvalueReference = true; |
| 194 | BindsToFunctionLvalue = false; |
| 195 | BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 196 | BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 197 | ObjCLifetimeConversionBinding = false; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 198 | CopyConstructor = 0; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 201 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 202 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 203 | /// implicit conversions. |
| 204 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 205 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 206 | if (GetConversionRank(First) > Rank) |
| 207 | Rank = GetConversionRank(First); |
| 208 | if (GetConversionRank(Second) > Rank) |
| 209 | Rank = GetConversionRank(Second); |
| 210 | if (GetConversionRank(Third) > Rank) |
| 211 | Rank = GetConversionRank(Third); |
| 212 | return Rank; |
| 213 | } |
| 214 | |
| 215 | /// isPointerConversionToBool - Determines whether this conversion is |
| 216 | /// 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] | 217 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 218 | /// (C++ 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 220 | // Note that FromType has not necessarily been transformed by the |
| 221 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 222 | // check for their presence as well as checking whether FromType is |
| 223 | // a pointer. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 224 | if (getToType(1)->isBooleanType() && |
John McCall | ddb0ce7 | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 225 | (getFromType()->isPointerType() || |
| 226 | getFromType()->isObjCObjectPointerType() || |
| 227 | getFromType()->isBlockPointerType() || |
Anders Carlsson | c8df0b6 | 2010-11-05 00:12:09 +0000 | [diff] [blame] | 228 | getFromType()->isNullPtrType() || |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 229 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 230 | return true; |
| 231 | |
| 232 | return false; |
| 233 | } |
| 234 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 235 | /// isPointerConversionToVoidPointer - Determines whether this |
| 236 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 237 | /// used as part of the ranking of standard conversion sequences (C++ |
| 238 | /// 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | bool |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 240 | StandardConversionSequence:: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 242 | QualType FromType = getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 243 | QualType ToType = getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 244 | |
| 245 | // Note that FromType has not necessarily been transformed by the |
| 246 | // array-to-pointer implicit conversion, so check for its presence |
| 247 | // and redo the conversion to get a pointer. |
| 248 | if (First == ICK_Array_To_Pointer) |
| 249 | FromType = Context.getArrayDecayedType(FromType); |
| 250 | |
Douglas Gregor | f9af524 | 2011-04-15 20:45:44 +0000 | [diff] [blame] | 251 | if (Second == ICK_Pointer_Conversion && FromType->isAnyPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 252 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 253 | return ToPtrType->getPointeeType()->isVoidType(); |
| 254 | |
| 255 | return false; |
| 256 | } |
| 257 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 258 | /// DebugPrint - Print this standard conversion sequence to standard |
| 259 | /// error. Useful for debugging overloading issues. |
| 260 | void StandardConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 261 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 262 | bool PrintedSomething = false; |
| 263 | if (First != ICK_Identity) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 264 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 265 | PrintedSomething = true; |
| 266 | } |
| 267 | |
| 268 | if (Second != ICK_Identity) { |
| 269 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 270 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 271 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 272 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 273 | |
| 274 | if (CopyConstructor) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 275 | OS << " (by copy constructor)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 276 | } else if (DirectBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 277 | OS << " (direct reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 278 | } else if (ReferenceBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 279 | OS << " (reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 280 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 281 | PrintedSomething = true; |
| 282 | } |
| 283 | |
| 284 | if (Third != ICK_Identity) { |
| 285 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 286 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 287 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 288 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 289 | PrintedSomething = true; |
| 290 | } |
| 291 | |
| 292 | if (!PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 293 | OS << "No conversions required"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 298 | /// error. Useful for debugging overloading issues. |
| 299 | void UserDefinedConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 300 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 301 | if (Before.First || Before.Second || Before.Third) { |
| 302 | Before.DebugPrint(); |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 303 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 304 | } |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 305 | OS << '\'' << ConversionFunction << '\''; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 306 | if (After.First || After.Second || After.Third) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 307 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 308 | After.DebugPrint(); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 313 | /// error. Useful for debugging overloading issues. |
| 314 | void ImplicitConversionSequence::DebugPrint() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 315 | raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 316 | switch (ConversionKind) { |
| 317 | case StandardConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 318 | OS << "Standard conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 319 | Standard.DebugPrint(); |
| 320 | break; |
| 321 | case UserDefinedConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 322 | OS << "User-defined conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 323 | UserDefined.DebugPrint(); |
| 324 | break; |
| 325 | case EllipsisConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 326 | OS << "Ellipsis conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 327 | break; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 328 | case AmbiguousConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 329 | OS << "Ambiguous conversion"; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 330 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 331 | case BadConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 332 | OS << "Bad conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 333 | break; |
| 334 | } |
| 335 | |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 336 | OS << "\n"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 337 | } |
| 338 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 339 | void AmbiguousConversionSequence::construct() { |
| 340 | new (&conversions()) ConversionSet(); |
| 341 | } |
| 342 | |
| 343 | void AmbiguousConversionSequence::destruct() { |
| 344 | conversions().~ConversionSet(); |
| 345 | } |
| 346 | |
| 347 | void |
| 348 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 349 | FromTypePtr = O.FromTypePtr; |
| 350 | ToTypePtr = O.ToTypePtr; |
| 351 | new (&conversions()) ConversionSet(O.conversions()); |
| 352 | } |
| 353 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 354 | namespace { |
| 355 | // Structure used by OverloadCandidate::DeductionFailureInfo to store |
| 356 | // template parameter and template argument information. |
| 357 | struct DFIParamWithArguments { |
| 358 | TemplateParameter Param; |
| 359 | TemplateArgument FirstArg; |
| 360 | TemplateArgument SecondArg; |
| 361 | }; |
| 362 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 363 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 364 | /// \brief Convert from Sema's representation of template deduction information |
| 365 | /// to the form used in overload-candidate information. |
| 366 | OverloadCandidate::DeductionFailureInfo |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 367 | static MakeDeductionFailureInfo(ASTContext &Context, |
| 368 | Sema::TemplateDeductionResult TDK, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 369 | TemplateDeductionInfo &Info) { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 370 | OverloadCandidate::DeductionFailureInfo Result; |
| 371 | Result.Result = static_cast<unsigned>(TDK); |
| 372 | Result.Data = 0; |
| 373 | switch (TDK) { |
| 374 | case Sema::TDK_Success: |
| 375 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 376 | case Sema::TDK_TooManyArguments: |
| 377 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 378 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 379 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 380 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 381 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 382 | Result.Data = Info.Param.getOpaqueValue(); |
| 383 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 384 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 385 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 386 | case Sema::TDK_Underqualified: { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 387 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 388 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 389 | Saved->Param = Info.Param; |
| 390 | Saved->FirstArg = Info.FirstArg; |
| 391 | Saved->SecondArg = Info.SecondArg; |
| 392 | Result.Data = Saved; |
| 393 | break; |
| 394 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 395 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 396 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 397 | Result.Data = Info.take(); |
| 398 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 399 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 400 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 401 | case Sema::TDK_FailedOverloadResolution: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 402 | break; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 403 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 404 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 405 | return Result; |
| 406 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 407 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 408 | void OverloadCandidate::DeductionFailureInfo::Destroy() { |
| 409 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 410 | case Sema::TDK_Success: |
| 411 | case Sema::TDK_InstantiationDepth: |
| 412 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 413 | case Sema::TDK_TooManyArguments: |
| 414 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 415 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 416 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 417 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 418 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 419 | case Sema::TDK_Underqualified: |
Douglas Gregor | aaa045d | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 420 | // FIXME: Destroy the data? |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 421 | Data = 0; |
| 422 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 423 | |
| 424 | case Sema::TDK_SubstitutionFailure: |
| 425 | // FIXME: Destroy the template arugment list? |
| 426 | Data = 0; |
| 427 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 428 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 429 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 430 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 431 | case Sema::TDK_FailedOverloadResolution: |
| 432 | break; |
| 433 | } |
| 434 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 435 | |
| 436 | TemplateParameter |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 437 | OverloadCandidate::DeductionFailureInfo::getTemplateParameter() { |
| 438 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 439 | case Sema::TDK_Success: |
| 440 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 441 | case Sema::TDK_TooManyArguments: |
| 442 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 443 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 444 | return TemplateParameter(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 445 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 446 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 447 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 448 | return TemplateParameter::getFromOpaqueValue(Data); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 449 | |
| 450 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 451 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 452 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 453 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 454 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 455 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 456 | case Sema::TDK_FailedOverloadResolution: |
| 457 | break; |
| 458 | } |
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 | return TemplateParameter(); |
| 461 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 462 | |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 463 | TemplateArgumentList * |
| 464 | OverloadCandidate::DeductionFailureInfo::getTemplateArgumentList() { |
| 465 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 466 | case Sema::TDK_Success: |
| 467 | case Sema::TDK_InstantiationDepth: |
| 468 | case Sema::TDK_TooManyArguments: |
| 469 | case Sema::TDK_TooFewArguments: |
| 470 | case Sema::TDK_Incomplete: |
| 471 | case Sema::TDK_InvalidExplicitArguments: |
| 472 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 473 | case Sema::TDK_Underqualified: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 474 | return 0; |
| 475 | |
| 476 | case Sema::TDK_SubstitutionFailure: |
| 477 | return static_cast<TemplateArgumentList*>(Data); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 478 | |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 479 | // Unhandled |
| 480 | case Sema::TDK_NonDeducedMismatch: |
| 481 | case Sema::TDK_FailedOverloadResolution: |
| 482 | break; |
| 483 | } |
| 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 488 | const TemplateArgument *OverloadCandidate::DeductionFailureInfo::getFirstArg() { |
| 489 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 490 | case Sema::TDK_Success: |
| 491 | case Sema::TDK_InstantiationDepth: |
| 492 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 493 | case Sema::TDK_TooManyArguments: |
| 494 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 495 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 496 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 497 | return 0; |
| 498 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 499 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 500 | case Sema::TDK_Underqualified: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 501 | return &static_cast<DFIParamWithArguments*>(Data)->FirstArg; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 502 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 503 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 504 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 505 | case Sema::TDK_FailedOverloadResolution: |
| 506 | break; |
| 507 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 508 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 509 | return 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 510 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 511 | |
| 512 | const TemplateArgument * |
| 513 | OverloadCandidate::DeductionFailureInfo::getSecondArg() { |
| 514 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 515 | case Sema::TDK_Success: |
| 516 | case Sema::TDK_InstantiationDepth: |
| 517 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 518 | case Sema::TDK_TooManyArguments: |
| 519 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 520 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 521 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 522 | return 0; |
| 523 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 524 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 525 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 526 | return &static_cast<DFIParamWithArguments*>(Data)->SecondArg; |
| 527 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 528 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 529 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 530 | case Sema::TDK_FailedOverloadResolution: |
| 531 | break; |
| 532 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 533 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | void OverloadCandidateSet::clear() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 538 | inherited::clear(); |
| 539 | Functions.clear(); |
| 540 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 541 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 542 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 543 | // overload of the declarations in Old. This routine returns false if |
| 544 | // New and Old cannot be overloaded, e.g., if New has the same |
| 545 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 546 | // declarations aren't functions (or function templates) at all. When |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 547 | // it does return false, MatchedDecl will point to the decl that New |
| 548 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 549 | // top of the underlying declaration. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 550 | // |
| 551 | // Example: Given the following input: |
| 552 | // |
| 553 | // void f(int, float); // #1 |
| 554 | // void f(int, int); // #2 |
| 555 | // int f(int, int); // #3 |
| 556 | // |
| 557 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | // so IsOverload will not be used. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 559 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 560 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 561 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 562 | // (since they have different signatures), so this routine returns |
| 563 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 564 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 565 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 566 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 567 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 568 | // identical (return types of functions are not part of the |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 569 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 570 | // point to the FunctionDecl for #2. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 571 | // |
| 572 | // 'NewIsUsingShadowDecl' indicates that 'New' is being introduced |
| 573 | // into a class by a using declaration. The rules for whether to hide |
| 574 | // shadow declarations ignore some properties which otherwise figure |
| 575 | // into a function template's signature. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 576 | Sema::OverloadKind |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 577 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
| 578 | NamedDecl *&Match, bool NewIsUsingDecl) { |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 579 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 580 | I != E; ++I) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 581 | NamedDecl *OldD = *I; |
| 582 | |
| 583 | bool OldIsUsingDecl = false; |
| 584 | if (isa<UsingShadowDecl>(OldD)) { |
| 585 | OldIsUsingDecl = true; |
| 586 | |
| 587 | // We can always introduce two using declarations into the same |
| 588 | // context, even if they have identical signatures. |
| 589 | if (NewIsUsingDecl) continue; |
| 590 | |
| 591 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 592 | } |
| 593 | |
| 594 | // If either declaration was introduced by a using declaration, |
| 595 | // we'll need to use slightly different rules for matching. |
| 596 | // Essentially, these rules are the normal rules, except that |
| 597 | // function templates hide function templates with different |
| 598 | // return types or template parameter lists. |
| 599 | bool UseMemberUsingDeclRules = |
| 600 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord(); |
| 601 | |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 602 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 603 | if (!IsOverload(New, OldT->getTemplatedDecl(), UseMemberUsingDeclRules)) { |
| 604 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 605 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 606 | continue; |
| 607 | } |
| 608 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 609 | Match = *I; |
| 610 | return Ovl_Match; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 611 | } |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 612 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 613 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
| 614 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 615 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 616 | continue; |
| 617 | } |
| 618 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 619 | Match = *I; |
| 620 | return Ovl_Match; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 621 | } |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 622 | } else if (isa<UsingDecl>(OldD)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 623 | // We can overload with these, which can show up when doing |
| 624 | // redeclaration checks for UsingDecls. |
| 625 | assert(Old.getLookupKind() == LookupUsingDeclName); |
John McCall | d7945c6 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 626 | } else if (isa<TagDecl>(OldD)) { |
| 627 | // We can always overload with tags by hiding them. |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 628 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 629 | // Optimistically assume that an unresolved using decl will |
| 630 | // overload; if it doesn't, we'll have to diagnose during |
| 631 | // template instantiation. |
| 632 | } else { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 633 | // (C++ 13p1): |
| 634 | // Only function declarations can be overloaded; object and type |
| 635 | // declarations cannot be overloaded. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 636 | Match = *I; |
| 637 | return Ovl_NonFunction; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 638 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 639 | } |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 640 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 641 | return Ovl_Overload; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 642 | } |
| 643 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 644 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| 645 | bool UseUsingDeclRules) { |
John McCall | 7b49202 | 2010-08-12 07:09:11 +0000 | [diff] [blame] | 646 | // If both of the functions are extern "C", then they are not |
| 647 | // overloads. |
| 648 | if (Old->isExternC() && New->isExternC()) |
| 649 | return false; |
| 650 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 651 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 652 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 653 | |
| 654 | // C++ [temp.fct]p2: |
| 655 | // A function template can be overloaded with other function templates |
| 656 | // and with normal (non-template) functions. |
| 657 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 658 | return true; |
| 659 | |
| 660 | // Is the function New an overload of the function Old? |
| 661 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 662 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 663 | |
| 664 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 665 | // determine whether they are overloads. If we find any mismatch |
| 666 | // in the signature, they are overloads. |
| 667 | |
| 668 | // If either of these functions is a K&R-style function (no |
| 669 | // prototype), then we consider them to have matching signatures. |
| 670 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 671 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 672 | return false; |
| 673 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 674 | const FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 675 | const FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 676 | |
| 677 | // The signature of a function includes the types of its |
| 678 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 679 | // of the ellipsis; see C++ DR 357). |
| 680 | if (OldQType != NewQType && |
| 681 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 682 | OldType->isVariadic() != NewType->isVariadic() || |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 683 | !FunctionArgTypesAreEqual(OldType, NewType))) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 684 | return true; |
| 685 | |
| 686 | // C++ [temp.over.link]p4: |
| 687 | // The signature of a function template consists of its function |
| 688 | // signature, its return type and its template parameter list. The names |
| 689 | // of the template parameters are significant only for establishing the |
| 690 | // relationship between the template parameters and the rest of the |
| 691 | // signature. |
| 692 | // |
| 693 | // We check the return type and template parameter lists for function |
| 694 | // templates first; the remaining checks follow. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 695 | // |
| 696 | // However, we don't consider either of these when deciding whether |
| 697 | // a member introduced by a shadow declaration is hidden. |
| 698 | if (!UseUsingDeclRules && NewTemplate && |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 699 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 700 | OldTemplate->getTemplateParameters(), |
| 701 | false, TPL_TemplateMatch) || |
| 702 | OldType->getResultType() != NewType->getResultType())) |
| 703 | return true; |
| 704 | |
| 705 | // If the function is a class member, its signature includes the |
Douglas Gregor | 57c9f4f | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 706 | // 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] | 707 | // |
| 708 | // As part of this, also check whether one of the member functions |
| 709 | // is static, in which case they are not overloads (C++ |
| 710 | // 13.1p2). While not part of the definition of the signature, |
| 711 | // this check is important to determine whether these functions |
| 712 | // can be overloaded. |
| 713 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 714 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 715 | if (OldMethod && NewMethod && |
| 716 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
Douglas Gregor | 57c9f4f | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 717 | (OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers() || |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 718 | OldMethod->getRefQualifier() != NewMethod->getRefQualifier())) { |
| 719 | if (!UseUsingDeclRules && |
| 720 | OldMethod->getRefQualifier() != NewMethod->getRefQualifier() && |
| 721 | (OldMethod->getRefQualifier() == RQ_None || |
| 722 | NewMethod->getRefQualifier() == RQ_None)) { |
| 723 | // C++0x [over.load]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 724 | // - Member function declarations with the same name and the same |
| 725 | // parameter-type-list as well as member function template |
| 726 | // declarations with the same name, the same parameter-type-list, and |
| 727 | // the same template parameter lists cannot be overloaded if any of |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 728 | // them, but not all, have a ref-qualifier (8.3.5). |
| 729 | Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) |
| 730 | << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); |
| 731 | Diag(OldMethod->getLocation(), diag::note_previous_declaration); |
| 732 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 733 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 734 | return true; |
Douglas Gregor | b145ee6 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 735 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 736 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 737 | // The signatures match; this is not an overload. |
| 738 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 741 | /// \brief Checks availability of the function depending on the current |
| 742 | /// function context. Inside an unavailable function, unavailability is ignored. |
| 743 | /// |
| 744 | /// \returns true if \arg FD is unavailable and current context is inside |
| 745 | /// an available function, false otherwise. |
| 746 | bool Sema::isFunctionConsideredUnavailable(FunctionDecl *FD) { |
| 747 | return FD->isUnavailable() && !cast<Decl>(CurContext)->isUnavailable(); |
| 748 | } |
| 749 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 750 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 751 | /// from the given expression (Expr) to the given type (ToType). This |
| 752 | /// function returns an implicit conversion sequence that can be used |
| 753 | /// to perform the initialization. Given |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 754 | /// |
| 755 | /// void f(float f); |
| 756 | /// void g(int i) { f(i); } |
| 757 | /// |
| 758 | /// this routine would produce an implicit conversion sequence to |
| 759 | /// describe the initialization of f from i, which will be a standard |
| 760 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 761 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 762 | // |
| 763 | /// Note that this routine only determines how the conversion can be |
| 764 | /// performed; it does not actually perform the conversion. As such, |
| 765 | /// it will not produce any diagnostics if no conversion is available, |
| 766 | /// but will instead return an implicit conversion sequence of kind |
| 767 | /// "BadConversion". |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 768 | /// |
| 769 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 770 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 771 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 772 | /// permitted. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 773 | /// |
| 774 | /// \param AllowObjCWritebackConversion Whether we allow the Objective-C |
| 775 | /// writeback conversion, which allows __autoreleasing id* parameters to |
| 776 | /// be initialized with __strong id* or __weak id* arguments. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 777 | static ImplicitConversionSequence |
| 778 | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
| 779 | bool SuppressUserConversions, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 780 | bool AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 781 | bool InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 782 | bool CStyle, |
| 783 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 784 | ImplicitConversionSequence ICS; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 785 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 786 | ICS.Standard, CStyle, AllowObjCWritebackConversion)){ |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 787 | ICS.setStandard(); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 788 | return ICS; |
| 789 | } |
| 790 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 791 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 792 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 793 | return ICS; |
| 794 | } |
| 795 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 796 | // C++ [over.ics.user]p4: |
| 797 | // A conversion of an expression of class type to the same class |
| 798 | // type is given Exact Match rank, and a conversion of an |
| 799 | // expression of class type to a base class of that type is |
| 800 | // given Conversion rank, in spite of the fact that a copy/move |
| 801 | // constructor (i.e., a user-defined conversion function) is |
| 802 | // called for those cases. |
| 803 | QualType FromType = From->getType(); |
| 804 | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 805 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
| 806 | S.IsDerivedFrom(FromType, ToType))) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 807 | ICS.setStandard(); |
| 808 | ICS.Standard.setAsIdentityConversion(); |
| 809 | ICS.Standard.setFromType(FromType); |
| 810 | ICS.Standard.setAllToTypes(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 811 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 812 | // We don't actually check at this point whether there is a valid |
| 813 | // copy/move constructor, since overloading just assumes that it |
| 814 | // exists. When we actually perform initialization, we'll find the |
| 815 | // appropriate constructor to copy the returned object, if needed. |
| 816 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 817 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 818 | // Determine whether this is considered a derived-to-base conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 819 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 820 | ICS.Standard.Second = ICK_Derived_To_Base; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 821 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 822 | return ICS; |
| 823 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 824 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 825 | if (SuppressUserConversions) { |
| 826 | // We're not in the case above, so there is no conversion that |
| 827 | // we can perform. |
| 828 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 829 | return ICS; |
| 830 | } |
| 831 | |
| 832 | // Attempt user-defined conversion. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 833 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 834 | OverloadingResult UserDefResult |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 835 | = IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, Conversions, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 836 | AllowExplicit); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 837 | |
| 838 | if (UserDefResult == OR_Success) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 839 | ICS.setUserDefined(); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 840 | // C++ [over.ics.user]p4: |
| 841 | // A conversion of an expression of class type to the same class |
| 842 | // type is given Exact Match rank, and a conversion of an |
| 843 | // expression of class type to a base class of that type is |
| 844 | // given Conversion rank, in spite of the fact that a copy |
| 845 | // constructor (i.e., a user-defined conversion function) is |
| 846 | // called for those cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 847 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 848 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 849 | QualType FromCanon |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 850 | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 851 | QualType ToCanon |
| 852 | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 853 | if (Constructor->isCopyConstructor() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 854 | (FromCanon == ToCanon || S.IsDerivedFrom(FromCanon, ToCanon))) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 855 | // Turn this into a "standard" conversion sequence, so that it |
| 856 | // gets ranked with standard conversion sequences. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 857 | ICS.setStandard(); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 858 | ICS.Standard.setAsIdentityConversion(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 859 | ICS.Standard.setFromType(From->getType()); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 860 | ICS.Standard.setAllToTypes(ToType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 861 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 862 | if (ToCanon != FromCanon) |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 863 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 864 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 865 | } |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 866 | |
| 867 | // C++ [over.best.ics]p4: |
| 868 | // However, when considering the argument of a user-defined |
| 869 | // conversion function that is a candidate by 13.3.1.3 when |
| 870 | // invoked for the copying of the temporary in the second step |
| 871 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 872 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 873 | // ellipsis conversion sequences are allowed. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 874 | if (SuppressUserConversions && ICS.isUserDefined()) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 875 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 876 | } |
John McCall | cefd3ad | 2010-01-13 22:30:33 +0000 | [diff] [blame] | 877 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 878 | ICS.setAmbiguous(); |
| 879 | ICS.Ambiguous.setFromType(From->getType()); |
| 880 | ICS.Ambiguous.setToType(ToType); |
| 881 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 882 | Cand != Conversions.end(); ++Cand) |
| 883 | if (Cand->Viable) |
| 884 | ICS.Ambiguous.addConversion(Cand->Function); |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 885 | } else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 886 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 887 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 888 | |
| 889 | return ICS; |
| 890 | } |
| 891 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 892 | ImplicitConversionSequence |
| 893 | Sema::TryImplicitConversion(Expr *From, QualType ToType, |
| 894 | bool SuppressUserConversions, |
| 895 | bool AllowExplicit, |
| 896 | bool InOverloadResolution, |
| 897 | bool CStyle, |
| 898 | bool AllowObjCWritebackConversion) { |
| 899 | return clang::TryImplicitConversion(*this, From, ToType, |
| 900 | SuppressUserConversions, AllowExplicit, |
| 901 | InOverloadResolution, CStyle, |
| 902 | AllowObjCWritebackConversion); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 905 | /// PerformImplicitConversion - Perform an implicit conversion of the |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 906 | /// expression From to the type ToType. Returns the |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 907 | /// converted expression. Flavor is the kind of conversion we're |
| 908 | /// performing, used in the error message. If @p AllowExplicit, |
| 909 | /// explicit user-defined conversions are permitted. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 910 | ExprResult |
| 911 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 912 | AssignmentAction Action, bool AllowExplicit) { |
| 913 | ImplicitConversionSequence ICS; |
| 914 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
| 915 | } |
| 916 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 917 | ExprResult |
| 918 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 919 | AssignmentAction Action, bool AllowExplicit, |
| 920 | ImplicitConversionSequence& ICS) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 921 | // Objective-C ARC: Determine whether we will allow the writeback conversion. |
| 922 | bool AllowObjCWritebackConversion |
| 923 | = getLangOptions().ObjCAutoRefCount && |
| 924 | (Action == AA_Passing || Action == AA_Sending); |
| 925 | |
| 926 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 927 | ICS = clang::TryImplicitConversion(*this, From, ToType, |
| 928 | /*SuppressUserConversions=*/false, |
| 929 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 930 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 931 | /*CStyle=*/false, |
| 932 | AllowObjCWritebackConversion); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 933 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 934 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 935 | |
| 936 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 937 | /// conversion that strips "noreturn" off the nested function type. |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 938 | bool Sema::IsNoReturnConversion(QualType FromType, QualType ToType, |
| 939 | QualType &ResultTy) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 940 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 941 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 942 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 943 | // Permit the conversion F(t __attribute__((noreturn))) -> F(t) |
| 944 | // where F adds one of the following at most once: |
| 945 | // - a pointer |
| 946 | // - a member pointer |
| 947 | // - a block pointer |
| 948 | CanQualType CanTo = Context.getCanonicalType(ToType); |
| 949 | CanQualType CanFrom = Context.getCanonicalType(FromType); |
| 950 | Type::TypeClass TyClass = CanTo->getTypeClass(); |
| 951 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 952 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) { |
| 953 | if (TyClass == Type::Pointer) { |
| 954 | CanTo = CanTo.getAs<PointerType>()->getPointeeType(); |
| 955 | CanFrom = CanFrom.getAs<PointerType>()->getPointeeType(); |
| 956 | } else if (TyClass == Type::BlockPointer) { |
| 957 | CanTo = CanTo.getAs<BlockPointerType>()->getPointeeType(); |
| 958 | CanFrom = CanFrom.getAs<BlockPointerType>()->getPointeeType(); |
| 959 | } else if (TyClass == Type::MemberPointer) { |
| 960 | CanTo = CanTo.getAs<MemberPointerType>()->getPointeeType(); |
| 961 | CanFrom = CanFrom.getAs<MemberPointerType>()->getPointeeType(); |
| 962 | } else { |
| 963 | return false; |
| 964 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 965 | |
John McCall | 00ccbef | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 966 | TyClass = CanTo->getTypeClass(); |
| 967 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 968 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) |
| 969 | return false; |
| 970 | } |
| 971 | |
| 972 | const FunctionType *FromFn = cast<FunctionType>(CanFrom); |
| 973 | FunctionType::ExtInfo EInfo = FromFn->getExtInfo(); |
| 974 | if (!EInfo.getNoReturn()) return false; |
| 975 | |
| 976 | FromFn = Context.adjustFunctionType(FromFn, EInfo.withNoReturn(false)); |
| 977 | assert(QualType(FromFn, 0).isCanonical()); |
| 978 | if (QualType(FromFn, 0) != CanTo) return false; |
| 979 | |
| 980 | ResultTy = ToType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 981 | return true; |
| 982 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 983 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 984 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 985 | /// vector conversion. |
| 986 | /// |
| 987 | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
| 988 | /// conversion. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 989 | static bool IsVectorConversion(ASTContext &Context, QualType FromType, |
| 990 | QualType ToType, ImplicitConversionKind &ICK) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 991 | // We need at least one of these types to be a vector type to have a vector |
| 992 | // conversion. |
| 993 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
| 994 | return false; |
| 995 | |
| 996 | // Identical types require no conversions. |
| 997 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 998 | return false; |
| 999 | |
| 1000 | // There are no conversions between extended vector types, only identity. |
| 1001 | if (ToType->isExtVectorType()) { |
| 1002 | // There are no conversions between extended vector types other than the |
| 1003 | // identity conversion. |
| 1004 | if (FromType->isExtVectorType()) |
| 1005 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1006 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1007 | // Vector splat from any arithmetic type to a vector. |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 1008 | if (FromType->isArithmeticType()) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1009 | ICK = ICK_Vector_Splat; |
| 1010 | return true; |
| 1011 | } |
| 1012 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1013 | |
| 1014 | // We can perform the conversion between vector types in the following cases: |
| 1015 | // 1)vector types are equivalent AltiVec and GCC vector types |
| 1016 | // 2)lax vector conversions are permitted and the vector types are of the |
| 1017 | // same size |
| 1018 | if (ToType->isVectorType() && FromType->isVectorType()) { |
| 1019 | if (Context.areCompatibleVectorTypes(FromType, ToType) || |
Chandler Carruth | c45eb9c | 2010-08-08 05:02:51 +0000 | [diff] [blame] | 1020 | (Context.getLangOptions().LaxVectorConversions && |
| 1021 | (Context.getTypeSize(FromType) == Context.getTypeSize(ToType)))) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1022 | ICK = ICK_Vector_Conversion; |
| 1023 | return true; |
| 1024 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1025 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1026 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1027 | return false; |
| 1028 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1029 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1030 | /// IsStandardConversion - Determines whether there is a standard |
| 1031 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 1032 | /// expression From to the type ToType. Standard conversion sequences |
| 1033 | /// only consider non-class types; for conversions that involve class |
| 1034 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 1035 | /// contain the standard conversion sequence required to perform this |
| 1036 | /// conversion and this routine will return true. Otherwise, this |
| 1037 | /// routine will return false and the value of SCS is unspecified. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1038 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 1039 | bool InOverloadResolution, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1040 | StandardConversionSequence &SCS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1041 | bool CStyle, |
| 1042 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1043 | QualType FromType = From->getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1044 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1045 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1046 | SCS.setAsIdentityConversion(); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1047 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1048 | SCS.IncompatibleObjC = false; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1049 | SCS.setFromType(FromType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1050 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1051 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1052 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1053 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1054 | if (FromType->isRecordType() || ToType->isRecordType()) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1055 | if (S.getLangOptions().CPlusPlus) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1056 | return false; |
| 1057 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1058 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1061 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 1062 | // array-to-pointer conversion, or function-to-pointer conversion |
| 1063 | // (C++ 4p1). |
| 1064 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1065 | if (FromType == S.Context.OverloadTy) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1066 | DeclAccessPair AccessPair; |
| 1067 | if (FunctionDecl *Fn |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1068 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1069 | AccessPair)) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1070 | // We were able to resolve the address of the overloaded function, |
| 1071 | // so we can convert to the type of that function. |
| 1072 | FromType = Fn->getType(); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1073 | |
| 1074 | // we can sometimes resolve &foo<int> regardless of ToType, so check |
| 1075 | // if the type matches (identity) or we are converting to bool |
| 1076 | if (!S.Context.hasSameUnqualifiedType( |
| 1077 | S.ExtractUnqualifiedFunctionType(ToType), FromType)) { |
| 1078 | QualType resultTy; |
| 1079 | // if the function type matches except for [[noreturn]], it's ok |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1080 | if (!S.IsNoReturnConversion(FromType, |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1081 | S.ExtractUnqualifiedFunctionType(ToType), resultTy)) |
| 1082 | // otherwise, only a boolean conversion is standard |
| 1083 | if (!ToType->isBooleanType()) |
| 1084 | return false; |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1085 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1086 | |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1087 | // Check if the "from" expression is taking the address of an overloaded |
| 1088 | // function and recompute the FromType accordingly. Take advantage of the |
| 1089 | // fact that non-static member functions *must* have such an address-of |
| 1090 | // expression. |
| 1091 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn); |
| 1092 | if (Method && !Method->isStatic()) { |
| 1093 | assert(isa<UnaryOperator>(From->IgnoreParens()) && |
| 1094 | "Non-unary operator on non-static member address"); |
| 1095 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() |
| 1096 | == UO_AddrOf && |
| 1097 | "Non-address-of operator on non-static member address"); |
| 1098 | const Type *ClassType |
| 1099 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 1100 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
Chandler Carruth | fc5c8fc | 2011-03-29 18:38:10 +0000 | [diff] [blame] | 1101 | } else if (isa<UnaryOperator>(From->IgnoreParens())) { |
| 1102 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() == |
| 1103 | UO_AddrOf && |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1104 | "Non-address-of operator for overloaded function expression"); |
| 1105 | FromType = S.Context.getPointerType(FromType); |
| 1106 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1107 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1108 | // Check that we've computed the proper type after overload resolution. |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1109 | assert(S.Context.hasSameType( |
| 1110 | FromType, |
| 1111 | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1112 | } else { |
| 1113 | return false; |
| 1114 | } |
Anders Carlsson | 2bd6250 | 2010-11-04 05:28:09 +0000 | [diff] [blame] | 1115 | } |
John McCall | 2148011 | 2011-08-30 00:57:29 +0000 | [diff] [blame] | 1116 | // Lvalue-to-rvalue conversion (C++11 4.1): |
| 1117 | // A glvalue (3.10) of a non-function, non-array type T can |
| 1118 | // be converted to a prvalue. |
| 1119 | bool argIsLValue = From->isGLValue(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1120 | if (argIsLValue && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1121 | !FromType->isFunctionType() && !FromType->isArrayType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1122 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1123 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1124 | |
| 1125 | // If T is a non-class type, the type of the rvalue is the |
| 1126 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1127 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 1128 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1129 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1130 | } else if (FromType->isArrayType()) { |
| 1131 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1132 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1133 | |
| 1134 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 1135 | // bound of T" can be converted to an rvalue of type "pointer to |
| 1136 | // T" (C++ 4.2p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1137 | FromType = S.Context.getArrayDecayedType(FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1138 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1139 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1140 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1141 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1142 | |
| 1143 | // For the purpose of ranking in overload resolution |
| 1144 | // (13.3.3.1.1), this conversion is considered an |
| 1145 | // array-to-pointer conversion followed by a qualification |
| 1146 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1147 | SCS.Second = ICK_Identity; |
| 1148 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1149 | SCS.QualificationIncludesObjCLifetime = false; |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1150 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1151 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1152 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1153 | } else if (FromType->isFunctionType() && argIsLValue) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1154 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1155 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1156 | |
| 1157 | // An lvalue of function type T can be converted to an rvalue of |
| 1158 | // type "pointer to T." The result is a pointer to the |
| 1159 | // function. (C++ 4.3p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1160 | FromType = S.Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1161 | } else { |
| 1162 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1163 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1164 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1165 | SCS.setToType(0, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1166 | |
| 1167 | // The second conversion can be an integral promotion, floating |
| 1168 | // point promotion, integral conversion, floating point conversion, |
| 1169 | // floating-integral conversion, pointer conversion, |
| 1170 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1171 | // For overloading in C, this can also be a "compatible-type" |
| 1172 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1173 | bool IncompatibleObjC = false; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1174 | ImplicitConversionKind SecondICK = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1175 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1176 | // The unqualified versions of the types are the same: there's no |
| 1177 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1178 | SCS.Second = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1179 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1180 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1181 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1182 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1183 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1184 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1185 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1186 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1187 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1188 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1189 | SCS.Second = ICK_Complex_Promotion; |
| 1190 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1191 | } else if (ToType->isBooleanType() && |
| 1192 | (FromType->isArithmeticType() || |
| 1193 | FromType->isAnyPointerType() || |
| 1194 | FromType->isBlockPointerType() || |
| 1195 | FromType->isMemberPointerType() || |
| 1196 | FromType->isNullPtrType())) { |
| 1197 | // Boolean conversions (C++ 4.12). |
| 1198 | SCS.Second = ICK_Boolean_Conversion; |
| 1199 | FromType = S.Context.BoolTy; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1200 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1201 | ToType->isIntegralType(S.Context)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1202 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1203 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1204 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1205 | } else if (FromType->isAnyComplexType() && ToType->isComplexType()) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1206 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1207 | SCS.Second = ICK_Complex_Conversion; |
| 1208 | FromType = ToType.getUnqualifiedType(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1209 | } else if ((FromType->isAnyComplexType() && ToType->isArithmeticType()) || |
| 1210 | (ToType->isAnyComplexType() && FromType->isArithmeticType())) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1211 | // Complex-real conversions (C99 6.3.1.7) |
| 1212 | SCS.Second = ICK_Complex_Real; |
| 1213 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1214 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1215 | // Floating point conversions (C++ 4.8). |
| 1216 | SCS.Second = ICK_Floating_Conversion; |
| 1217 | FromType = ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1218 | } else if ((FromType->isRealFloatingType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1219 | ToType->isIntegralType(S.Context)) || |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1220 | (FromType->isIntegralOrUnscopedEnumerationType() && |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1221 | ToType->isRealFloatingType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1222 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1223 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1224 | FromType = ToType.getUnqualifiedType(); |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1225 | } else if (S.IsBlockPointerConversion(FromType, ToType, FromType)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1226 | SCS.Second = ICK_Block_Pointer_Conversion; |
| 1227 | } else if (AllowObjCWritebackConversion && |
| 1228 | S.isObjCWritebackConversion(FromType, ToType, FromType)) { |
| 1229 | SCS.Second = ICK_Writeback_Conversion; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1230 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1231 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1232 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1233 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1234 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1235 | FromType = FromType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1236 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1237 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1238 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1239 | SCS.Second = ICK_Pointer_Member; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1240 | } else if (IsVectorConversion(S.Context, FromType, ToType, SecondICK)) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1241 | SCS.Second = SecondICK; |
| 1242 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1243 | } else if (!S.getLangOptions().CPlusPlus && |
| 1244 | S.Context.typesAreCompatible(ToType, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1245 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1246 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1247 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 1248 | } else if (S.IsNoReturnConversion(FromType, ToType, FromType)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1249 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1250 | SCS.Second = ICK_NoReturn_Adjustment; |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1251 | } else if (IsTransparentUnionStandardConversion(S, From, ToType, |
| 1252 | InOverloadResolution, |
| 1253 | SCS, CStyle)) { |
| 1254 | SCS.Second = ICK_TransparentUnionConversion; |
| 1255 | FromType = ToType; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1256 | } else { |
| 1257 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1258 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1259 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1260 | SCS.setToType(1, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1261 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1262 | QualType CanonFrom; |
| 1263 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1264 | // The third conversion can be a qualification conversion (C++ 4p1). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1265 | bool ObjCLifetimeConversion; |
| 1266 | if (S.IsQualificationConversion(FromType, ToType, CStyle, |
| 1267 | ObjCLifetimeConversion)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1268 | SCS.Third = ICK_Qualification; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1269 | SCS.QualificationIncludesObjCLifetime = ObjCLifetimeConversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1270 | FromType = ToType; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1271 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1272 | CanonTo = S.Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1273 | } else { |
| 1274 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1275 | SCS.Third = ICK_Identity; |
| 1276 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1277 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1278 | // [...] Any difference in top-level cv-qualification is |
| 1279 | // subsumed by the initialization itself and does not constitute |
| 1280 | // a conversion. [...] |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1281 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1282 | CanonTo = S.Context.getCanonicalType(ToType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1283 | if (CanonFrom.getLocalUnqualifiedType() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1284 | == CanonTo.getLocalUnqualifiedType() && |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 1285 | (CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers() |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1286 | || CanonFrom.getObjCGCAttr() != CanonTo.getObjCGCAttr() |
| 1287 | || CanonFrom.getObjCLifetime() != CanonTo.getObjCLifetime())) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1288 | FromType = ToType; |
| 1289 | CanonFrom = CanonTo; |
| 1290 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1291 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1292 | SCS.setToType(2, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1293 | |
| 1294 | // If we have not converted the argument type to the parameter type, |
| 1295 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1296 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1297 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1298 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1299 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1300 | } |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1301 | |
| 1302 | static bool |
| 1303 | IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 1304 | QualType &ToType, |
| 1305 | bool InOverloadResolution, |
| 1306 | StandardConversionSequence &SCS, |
| 1307 | bool CStyle) { |
| 1308 | |
| 1309 | const RecordType *UT = ToType->getAsUnionType(); |
| 1310 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 1311 | return false; |
| 1312 | // The field to initialize within the transparent union. |
| 1313 | RecordDecl *UD = UT->getDecl(); |
| 1314 | // It's compatible if the expression matches any of the fields. |
| 1315 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1316 | itend = UD->field_end(); |
| 1317 | it != itend; ++it) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1318 | if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS, |
| 1319 | CStyle, /*ObjCWritebackConversion=*/false)) { |
Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1320 | ToType = it->getType(); |
| 1321 | return true; |
| 1322 | } |
| 1323 | } |
| 1324 | return false; |
| 1325 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1326 | |
| 1327 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1328 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1329 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1330 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1331 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1332 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1333 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1334 | if (!To) { |
| 1335 | return false; |
| 1336 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1337 | |
| 1338 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1339 | // unsigned short int can be converted to an rvalue of type int if |
| 1340 | // int can represent all the values of the source type; otherwise, |
| 1341 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1342 | // int (C++ 4.5p1). |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1343 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1344 | !FromType->isEnumeralType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1345 | if (// We can promote any signed, promotable integer type to an int |
| 1346 | (FromType->isSignedIntegerType() || |
| 1347 | // We can promote any unsigned integer type whose size is |
| 1348 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1349 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1350 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1351 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1354 | return To->getKind() == BuiltinType::UInt; |
| 1355 | } |
| 1356 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1357 | // C++0x [conv.prom]p3: |
| 1358 | // A prvalue of an unscoped enumeration type whose underlying type is not |
| 1359 | // fixed (7.2) can be converted to an rvalue a prvalue of the first of the |
| 1360 | // following types that can represent all the values of the enumeration |
| 1361 | // (i.e., the values in the range bmin to bmax as described in 7.2): int, |
| 1362 | // unsigned int, long int, unsigned long int, long long int, or unsigned |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1363 | // 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] | 1364 | // values of the enumeration, an rvalue a prvalue of an unscoped enumeration |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1365 | // 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] | 1366 | // with lowest integer conversion rank (4.13) greater than the rank of long |
| 1367 | // 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] | 1368 | // there are two such extended types, the signed one is chosen. |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1369 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
| 1370 | // C++0x 7.2p9: Note that this implicit enum to int conversion is not |
| 1371 | // provided for a scoped enumeration. |
| 1372 | if (FromEnumType->getDecl()->isScoped()) |
| 1373 | return false; |
| 1374 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1375 | // We have already pre-calculated the promotion type, so this is trivial. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1376 | if (ToType->isIntegerType() && |
Douglas Gregor | 5dde160 | 2010-09-12 03:38:25 +0000 | [diff] [blame] | 1377 | !RequireCompleteType(From->getLocStart(), FromType, PDiag())) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1378 | return Context.hasSameUnqualifiedType(ToType, |
| 1379 | FromEnumType->getDecl()->getPromotionType()); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1380 | } |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1381 | |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1382 | // C++0x [conv.prom]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1383 | // A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted |
| 1384 | // to an rvalue a prvalue of the first of the following types that can |
| 1385 | // represent all the values of its underlying type: int, unsigned int, |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1386 | // 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] | 1387 | // 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] | 1388 | // underlying type, an rvalue a prvalue of type char16_t, char32_t, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1389 | // 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] | 1390 | // type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1391 | if (FromType->isAnyCharacterType() && !FromType->isCharType() && |
Douglas Gregor | 0b8ddb9 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1392 | ToType->isIntegerType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1393 | // Determine whether the type we're converting from is signed or |
| 1394 | // unsigned. |
David Majnemer | 0ad9231 | 2011-07-22 21:09:04 +0000 | [diff] [blame] | 1395 | bool FromIsSigned = FromType->isSignedIntegerType(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1396 | uint64_t FromSize = Context.getTypeSize(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1397 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1398 | // The types we'll try to promote to, in the appropriate |
| 1399 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1400 | QualType PromoteTypes[6] = { |
| 1401 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1402 | Context.LongTy, Context.UnsignedLongTy , |
| 1403 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1404 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1405 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1406 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1407 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1408 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1409 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1410 | // We found the type that we can promote to. If this is the |
| 1411 | // type we wanted, we have a promotion. Otherwise, no |
| 1412 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1413 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1414 | } |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1419 | // rvalue of type int if int can represent all the values of the |
| 1420 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1421 | // unsigned int can represent all the values of the bit-field. If |
| 1422 | // the bit-field is larger yet, no integral promotion applies to |
| 1423 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1424 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1425 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1426 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1427 | using llvm::APSInt; |
| 1428 | if (From) |
| 1429 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1430 | APSInt BitWidth; |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1431 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1432 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1433 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1434 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1435 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1436 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1437 | if (BitWidth < ToSize || |
| 1438 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1439 | return To->getKind() == BuiltinType::Int; |
| 1440 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1441 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1442 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1443 | // that fits into an unsigned int? |
| 1444 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1445 | return To->getKind() == BuiltinType::UInt; |
| 1446 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1447 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1448 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1449 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1450 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1451 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1452 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1453 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1454 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1455 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1456 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1457 | |
| 1458 | return false; |
| 1459 | } |
| 1460 | |
| 1461 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1462 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1463 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1464 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1465 | /// An rvalue of type float can be converted to an rvalue of type |
| 1466 | /// double. (C++ 4.6p1). |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1467 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1468 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1469 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1470 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1471 | return true; |
| 1472 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1473 | // C99 6.3.1.5p1: |
| 1474 | // When a float is promoted to double or long double, or a |
| 1475 | // double is promoted to long double [...]. |
| 1476 | if (!getLangOptions().CPlusPlus && |
| 1477 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1478 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1479 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1480 | return true; |
| 1481 | } |
| 1482 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1483 | return false; |
| 1484 | } |
| 1485 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1486 | /// \brief Determine if a conversion is a complex promotion. |
| 1487 | /// |
| 1488 | /// A complex promotion is defined as a complex -> complex conversion |
| 1489 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1490 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1491 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1492 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1493 | if (!FromComplex) |
| 1494 | return false; |
| 1495 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1496 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1497 | if (!ToComplex) |
| 1498 | return false; |
| 1499 | |
| 1500 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1501 | ToComplex->getElementType()) || |
| 1502 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1503 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1506 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1507 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1508 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1509 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1510 | /// the right set of qualifiers on its pointee. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1511 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1512 | static QualType |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1513 | BuildSimilarlyQualifiedPointerType(const Type *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1514 | QualType ToPointee, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1515 | ASTContext &Context, |
| 1516 | bool StripObjCLifetime = false) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1517 | assert((FromPtr->getTypeClass() == Type::Pointer || |
| 1518 | FromPtr->getTypeClass() == Type::ObjCObjectPointer) && |
| 1519 | "Invalid similarly-qualified pointer type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1520 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1521 | /// Conversions to 'id' subsume cv-qualifier conversions. |
| 1522 | if (ToType->isObjCIdType() || ToType->isObjCQualifiedIdType()) |
Douglas Gregor | 143c7ac | 2010-12-06 22:09:19 +0000 | [diff] [blame] | 1523 | return ToType.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1524 | |
| 1525 | QualType CanonFromPointee |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1526 | = Context.getCanonicalType(FromPtr->getPointeeType()); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1527 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1528 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1529 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1530 | if (StripObjCLifetime) |
| 1531 | Quals.removeObjCLifetime(); |
| 1532 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1533 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1534 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1535 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1536 | if (!ToType.isNull()) |
Douglas Gregor | af7bea5 | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1537 | return ToType.getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1538 | |
| 1539 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1540 | // already. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1541 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1542 | return Context.getObjCObjectPointerType(ToPointee); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1543 | return Context.getPointerType(ToPointee); |
| 1544 | } |
| 1545 | |
| 1546 | // Just build a canonical type that has the right qualifiers. |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1547 | QualType QualifiedCanonToPointee |
| 1548 | = Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1549 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1550 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1551 | return Context.getObjCObjectPointerType(QualifiedCanonToPointee); |
| 1552 | return Context.getPointerType(QualifiedCanonToPointee); |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1553 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1554 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1556 | bool InOverloadResolution, |
| 1557 | ASTContext &Context) { |
| 1558 | // Handle value-dependent integral null pointer constants correctly. |
| 1559 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1560 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1561 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1562 | return !InOverloadResolution; |
| 1563 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1564 | return Expr->isNullPointerConstant(Context, |
| 1565 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1566 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1567 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1568 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1569 | /// IsPointerConversion - Determines whether the conversion of the |
| 1570 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1571 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1572 | /// 4.10). If so, returns true and places the converted type (that |
| 1573 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1574 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1575 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1576 | /// This routine also supports conversions to and from block pointers |
| 1577 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1578 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1579 | /// appropriate overloading rules for Objective-C, we may want to |
| 1580 | /// split the Objective-C checks into a different routine; however, |
| 1581 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1582 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1583 | /// set if the conversion is an allowed Objective-C conversion that |
| 1584 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1585 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1586 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1587 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1588 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1589 | IncompatibleObjC = false; |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1590 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, |
| 1591 | IncompatibleObjC)) |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1592 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1593 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1594 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1595 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1596 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1597 | ConvertedType = ToType; |
| 1598 | return true; |
| 1599 | } |
| 1600 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1601 | // Blocks: Block pointers can be converted to void*. |
| 1602 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1603 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1604 | ConvertedType = ToType; |
| 1605 | return true; |
| 1606 | } |
| 1607 | // Blocks: A null pointer constant can be converted to a block |
| 1608 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1609 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1610 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1611 | ConvertedType = ToType; |
| 1612 | return true; |
| 1613 | } |
| 1614 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1615 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1616 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1617 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1618 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1619 | ConvertedType = ToType; |
| 1620 | return true; |
| 1621 | } |
| 1622 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1623 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1624 | if (!ToTypePtr) |
| 1625 | return false; |
| 1626 | |
| 1627 | // 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] | 1628 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1629 | ConvertedType = ToType; |
| 1630 | return true; |
| 1631 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1632 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1633 | // Beyond this point, both types need to be pointers |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1634 | // , including objective-c pointers. |
| 1635 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1636 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType() && |
| 1637 | !getLangOptions().ObjCAutoRefCount) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1638 | ConvertedType = BuildSimilarlyQualifiedPointerType( |
| 1639 | FromType->getAs<ObjCObjectPointerType>(), |
| 1640 | ToPointeeType, |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1641 | ToType, Context); |
| 1642 | return true; |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1643 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1644 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1645 | if (!FromTypePtr) |
| 1646 | return false; |
| 1647 | |
| 1648 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1649 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1650 | // 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] | 1651 | // pointer conversion, so don't do all of the work below. |
| 1652 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
| 1653 | return false; |
| 1654 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1655 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1656 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1657 | // 4.10p2). |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1658 | if (FromPointeeType->isIncompleteOrObjectType() && |
| 1659 | ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1660 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1661 | ToPointeeType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1662 | ToType, Context, |
| 1663 | /*StripObjCLifetime=*/true); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1664 | return true; |
| 1665 | } |
| 1666 | |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 1667 | // MSVC allows implicit function to void* type conversion. |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame^] | 1668 | if (getLangOptions().MicrosoftExt && FromPointeeType->isFunctionType() && |
Francois Pichet | a8ef3ac | 2011-05-08 22:52:41 +0000 | [diff] [blame] | 1669 | ToPointeeType->isVoidType()) { |
| 1670 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 1671 | ToPointeeType, |
| 1672 | ToType, Context); |
| 1673 | return true; |
| 1674 | } |
| 1675 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1676 | // When we're overloading in C, we allow a special kind of pointer |
| 1677 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1679 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1680 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1681 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1682 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1683 | return true; |
| 1684 | } |
| 1685 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1686 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1687 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1688 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1689 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1690 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1691 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1692 | // necessitates this conversion is ill-formed. The result of the |
| 1693 | // conversion is a pointer to the base class sub-object of the |
| 1694 | // derived class object. The null pointer value is converted to |
| 1695 | // the null pointer value of the destination type. |
| 1696 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1697 | // Note that we do not check for ambiguity or inaccessibility |
| 1698 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1699 | if (getLangOptions().CPlusPlus && |
| 1700 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | bf1764c | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 1701 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | 2685eab | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1702 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1703 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1704 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1705 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1706 | ToType, Context); |
| 1707 | return true; |
| 1708 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1709 | |
Fariborz Jahanian | 5da3c08 | 2011-04-14 20:33:36 +0000 | [diff] [blame] | 1710 | if (FromPointeeType->isVectorType() && ToPointeeType->isVectorType() && |
| 1711 | Context.areCompatibleVectorTypes(FromPointeeType, ToPointeeType)) { |
| 1712 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 1713 | ToPointeeType, |
| 1714 | ToType, Context); |
| 1715 | return true; |
| 1716 | } |
| 1717 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1718 | return false; |
| 1719 | } |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1720 | |
| 1721 | /// \brief Adopt the given qualifiers for the given type. |
| 1722 | static QualType AdoptQualifiers(ASTContext &Context, QualType T, Qualifiers Qs){ |
| 1723 | Qualifiers TQs = T.getQualifiers(); |
| 1724 | |
| 1725 | // Check whether qualifiers already match. |
| 1726 | if (TQs == Qs) |
| 1727 | return T; |
| 1728 | |
| 1729 | if (Qs.compatiblyIncludes(TQs)) |
| 1730 | return Context.getQualifiedType(T, Qs); |
| 1731 | |
| 1732 | return Context.getQualifiedType(T.getUnqualifiedType(), Qs); |
| 1733 | } |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1734 | |
| 1735 | /// isObjCPointerConversion - Determines whether this is an |
| 1736 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1737 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1738 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1739 | QualType& ConvertedType, |
| 1740 | bool &IncompatibleObjC) { |
| 1741 | if (!getLangOptions().ObjC1) |
| 1742 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1743 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1744 | // The set of qualifiers on the type we're converting from. |
| 1745 | Qualifiers FromQualifiers = FromType.getQualifiers(); |
| 1746 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1747 | // First, we handle all conversions on ObjC object pointer types. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1748 | const ObjCObjectPointerType* ToObjCPtr = |
| 1749 | ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1751 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1752 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1753 | if (ToObjCPtr && FromObjCPtr) { |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1754 | // If the pointee types are the same (ignoring qualifications), |
| 1755 | // then this is not a pointer conversion. |
| 1756 | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
| 1757 | FromObjCPtr->getPointeeType())) |
| 1758 | return false; |
| 1759 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1760 | // Check for compatible |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1761 | // 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] | 1762 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1763 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1764 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1765 | return true; |
| 1766 | } |
| 1767 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1768 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1769 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1770 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1771 | /*compare=*/false)) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1772 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1773 | return true; |
| 1774 | } |
| 1775 | // Objective C++: We're able to convert from a pointer to an |
| 1776 | // interface to a pointer to a different interface. |
| 1777 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 1778 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 1779 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
| 1780 | if (getLangOptions().CPlusPlus && LHS && RHS && |
| 1781 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 1782 | FromObjCPtr->getPointeeType())) |
| 1783 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1784 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1785 | ToObjCPtr->getPointeeType(), |
| 1786 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1787 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1788 | return true; |
| 1789 | } |
| 1790 | |
| 1791 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1792 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1793 | // interfaces, which is permitted. However, we're going to |
| 1794 | // complain about it. |
| 1795 | IncompatibleObjC = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1796 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1797 | ToObjCPtr->getPointeeType(), |
| 1798 | ToType, Context); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1799 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1800 | return true; |
| 1801 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1802 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1803 | // 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] | 1804 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1805 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1806 | ToPointeeType = ToCPtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1807 | else if (const BlockPointerType *ToBlockPtr = |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1808 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 1809 | // 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] | 1810 | // to a block pointer type. |
| 1811 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1812 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1813 | return true; |
| 1814 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1815 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1816 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1817 | else if (FromType->getAs<BlockPointerType>() && |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1818 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1819 | // 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] | 1820 | // pointer to any object. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1821 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1822 | return true; |
| 1823 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1824 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1825 | return false; |
| 1826 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1827 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1828 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1829 | FromPointeeType = FromCPtr->getPointeeType(); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1830 | else if (const BlockPointerType *FromBlockPtr = |
| 1831 | FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1832 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1833 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1834 | return false; |
| 1835 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1836 | // If we have pointers to pointers, recursively check whether this |
| 1837 | // is an Objective-C conversion. |
| 1838 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1839 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1840 | IncompatibleObjC)) { |
| 1841 | // We always complain about this conversion. |
| 1842 | IncompatibleObjC = true; |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1843 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1844 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1845 | return true; |
| 1846 | } |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1847 | // Allow conversion of pointee being objective-c pointer to another one; |
| 1848 | // as in I* to id. |
| 1849 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 1850 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 1851 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1852 | IncompatibleObjC)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1853 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1854 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1855 | ConvertedType = AdoptQualifiers(Context, ConvertedType, FromQualifiers); |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1856 | return true; |
| 1857 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1858 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1859 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1860 | // differences in the argument and result types are in Objective-C |
| 1861 | // pointer conversions. If so, we permit the conversion (but |
| 1862 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1863 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1864 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1865 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1866 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1867 | if (FromFunctionType && ToFunctionType) { |
| 1868 | // If the function types are exactly the same, this isn't an |
| 1869 | // Objective-C pointer conversion. |
| 1870 | if (Context.getCanonicalType(FromPointeeType) |
| 1871 | == Context.getCanonicalType(ToPointeeType)) |
| 1872 | return false; |
| 1873 | |
| 1874 | // Perform the quick checks that will tell us whether these |
| 1875 | // function types are obviously different. |
| 1876 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1877 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1878 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1879 | return false; |
| 1880 | |
| 1881 | bool HasObjCConversion = false; |
| 1882 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1883 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1884 | // Okay, the types match exactly. Nothing to do. |
| 1885 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1886 | ToFunctionType->getResultType(), |
| 1887 | ConvertedType, IncompatibleObjC)) { |
| 1888 | // Okay, we have an Objective-C pointer conversion. |
| 1889 | HasObjCConversion = true; |
| 1890 | } else { |
| 1891 | // Function types are too different. Abort. |
| 1892 | return false; |
| 1893 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1894 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1895 | // Check argument types. |
| 1896 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1897 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1898 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1899 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1900 | if (Context.getCanonicalType(FromArgType) |
| 1901 | == Context.getCanonicalType(ToArgType)) { |
| 1902 | // Okay, the types match exactly. Nothing to do. |
| 1903 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1904 | ConvertedType, IncompatibleObjC)) { |
| 1905 | // Okay, we have an Objective-C pointer conversion. |
| 1906 | HasObjCConversion = true; |
| 1907 | } else { |
| 1908 | // Argument types are too different. Abort. |
| 1909 | return false; |
| 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | if (HasObjCConversion) { |
| 1914 | // We had an Objective-C conversion. Allow this pointer |
| 1915 | // conversion, but complain about it. |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 1916 | ConvertedType = AdoptQualifiers(Context, ToType, FromQualifiers); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1917 | IncompatibleObjC = true; |
| 1918 | return true; |
| 1919 | } |
| 1920 | } |
| 1921 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1922 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1923 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1924 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1925 | /// \brief Determine whether this is an Objective-C writeback conversion, |
| 1926 | /// used for parameter passing when performing automatic reference counting. |
| 1927 | /// |
| 1928 | /// \param FromType The type we're converting form. |
| 1929 | /// |
| 1930 | /// \param ToType The type we're converting to. |
| 1931 | /// |
| 1932 | /// \param ConvertedType The type that will be produced after applying |
| 1933 | /// this conversion. |
| 1934 | bool Sema::isObjCWritebackConversion(QualType FromType, QualType ToType, |
| 1935 | QualType &ConvertedType) { |
| 1936 | if (!getLangOptions().ObjCAutoRefCount || |
| 1937 | Context.hasSameUnqualifiedType(FromType, ToType)) |
| 1938 | return false; |
| 1939 | |
| 1940 | // Parameter must be a pointer to __autoreleasing (with no other qualifiers). |
| 1941 | QualType ToPointee; |
| 1942 | if (const PointerType *ToPointer = ToType->getAs<PointerType>()) |
| 1943 | ToPointee = ToPointer->getPointeeType(); |
| 1944 | else |
| 1945 | return false; |
| 1946 | |
| 1947 | Qualifiers ToQuals = ToPointee.getQualifiers(); |
| 1948 | if (!ToPointee->isObjCLifetimeType() || |
| 1949 | ToQuals.getObjCLifetime() != Qualifiers::OCL_Autoreleasing || |
| 1950 | !ToQuals.withoutObjCGLifetime().empty()) |
| 1951 | return false; |
| 1952 | |
| 1953 | // Argument must be a pointer to __strong to __weak. |
| 1954 | QualType FromPointee; |
| 1955 | if (const PointerType *FromPointer = FromType->getAs<PointerType>()) |
| 1956 | FromPointee = FromPointer->getPointeeType(); |
| 1957 | else |
| 1958 | return false; |
| 1959 | |
| 1960 | Qualifiers FromQuals = FromPointee.getQualifiers(); |
| 1961 | if (!FromPointee->isObjCLifetimeType() || |
| 1962 | (FromQuals.getObjCLifetime() != Qualifiers::OCL_Strong && |
| 1963 | FromQuals.getObjCLifetime() != Qualifiers::OCL_Weak)) |
| 1964 | return false; |
| 1965 | |
| 1966 | // Make sure that we have compatible qualifiers. |
| 1967 | FromQuals.setObjCLifetime(Qualifiers::OCL_Autoreleasing); |
| 1968 | if (!ToQuals.compatiblyIncludes(FromQuals)) |
| 1969 | return false; |
| 1970 | |
| 1971 | // Remove qualifiers from the pointee type we're converting from; they |
| 1972 | // aren't used in the compatibility check belong, and we'll be adding back |
| 1973 | // qualifiers (with __autoreleasing) if the compatibility check succeeds. |
| 1974 | FromPointee = FromPointee.getUnqualifiedType(); |
| 1975 | |
| 1976 | // The unqualified form of the pointee types must be compatible. |
| 1977 | ToPointee = ToPointee.getUnqualifiedType(); |
| 1978 | bool IncompatibleObjC; |
| 1979 | if (Context.typesAreCompatible(FromPointee, ToPointee)) |
| 1980 | FromPointee = ToPointee; |
| 1981 | else if (!isObjCPointerConversion(FromPointee, ToPointee, FromPointee, |
| 1982 | IncompatibleObjC)) |
| 1983 | return false; |
| 1984 | |
| 1985 | /// \brief Construct the type we're converting to, which is a pointer to |
| 1986 | /// __autoreleasing pointee. |
| 1987 | FromPointee = Context.getQualifiedType(FromPointee, FromQuals); |
| 1988 | ConvertedType = Context.getPointerType(FromPointee); |
| 1989 | return true; |
| 1990 | } |
| 1991 | |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1992 | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
| 1993 | QualType& ConvertedType) { |
| 1994 | QualType ToPointeeType; |
| 1995 | if (const BlockPointerType *ToBlockPtr = |
| 1996 | ToType->getAs<BlockPointerType>()) |
| 1997 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 1998 | else |
| 1999 | return false; |
| 2000 | |
| 2001 | QualType FromPointeeType; |
| 2002 | if (const BlockPointerType *FromBlockPtr = |
| 2003 | FromType->getAs<BlockPointerType>()) |
| 2004 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 2005 | else |
| 2006 | return false; |
| 2007 | // We have pointer to blocks, check whether the only |
| 2008 | // differences in the argument and result types are in Objective-C |
| 2009 | // pointer conversions. If so, we permit the conversion. |
| 2010 | |
| 2011 | const FunctionProtoType *FromFunctionType |
| 2012 | = FromPointeeType->getAs<FunctionProtoType>(); |
| 2013 | const FunctionProtoType *ToFunctionType |
| 2014 | = ToPointeeType->getAs<FunctionProtoType>(); |
| 2015 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2016 | if (!FromFunctionType || !ToFunctionType) |
| 2017 | return false; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2018 | |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2019 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2020 | return true; |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2021 | |
| 2022 | // Perform the quick checks that will tell us whether these |
| 2023 | // function types are obviously different. |
| 2024 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 2025 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
| 2026 | return false; |
| 2027 | |
| 2028 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
| 2029 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
| 2030 | if (FromEInfo != ToEInfo) |
| 2031 | return false; |
| 2032 | |
| 2033 | bool IncompatibleObjC = false; |
Fariborz Jahanian | 462dae5 | 2011-02-13 20:11:42 +0000 | [diff] [blame] | 2034 | if (Context.hasSameType(FromFunctionType->getResultType(), |
| 2035 | ToFunctionType->getResultType())) { |
Fariborz Jahanian | 569bd8f | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 2036 | // Okay, the types match exactly. Nothing to do. |
| 2037 | } else { |
| 2038 | QualType RHS = FromFunctionType->getResultType(); |
| 2039 | QualType LHS = ToFunctionType->getResultType(); |
| 2040 | if ((!getLangOptions().CPlusPlus || !RHS->isRecordType()) && |
| 2041 | !RHS.hasQualifiers() && LHS.hasQualifiers()) |
| 2042 | LHS = LHS.getUnqualifiedType(); |
| 2043 | |
| 2044 | if (Context.hasSameType(RHS,LHS)) { |
| 2045 | // OK exact match. |
| 2046 | } else if (isObjCPointerConversion(RHS, LHS, |
| 2047 | ConvertedType, IncompatibleObjC)) { |
| 2048 | if (IncompatibleObjC) |
| 2049 | return false; |
| 2050 | // Okay, we have an Objective-C pointer conversion. |
| 2051 | } |
| 2052 | else |
| 2053 | return false; |
| 2054 | } |
| 2055 | |
| 2056 | // Check argument types. |
| 2057 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 2058 | ArgIdx != NumArgs; ++ArgIdx) { |
| 2059 | IncompatibleObjC = false; |
| 2060 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 2061 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 2062 | if (Context.hasSameType(FromArgType, ToArgType)) { |
| 2063 | // Okay, the types match exactly. Nothing to do. |
| 2064 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
| 2065 | ConvertedType, IncompatibleObjC)) { |
| 2066 | if (IncompatibleObjC) |
| 2067 | return false; |
| 2068 | // Okay, we have an Objective-C pointer conversion. |
| 2069 | } else |
| 2070 | // Argument types are too different. Abort. |
| 2071 | return false; |
| 2072 | } |
| 2073 | ConvertedType = ToType; |
| 2074 | return true; |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2077 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
| 2078 | /// for equlity of their argument types. Caller has already checked that |
| 2079 | /// they have same number of arguments. This routine assumes that Objective-C |
| 2080 | /// pointer types which only differ in their protocol qualifiers are equal. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2081 | bool Sema::FunctionArgTypesAreEqual(const FunctionProtoType *OldType, |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2082 | const FunctionProtoType *NewType) { |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2083 | if (!getLangOptions().ObjC1) |
| 2084 | return std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 2085 | NewType->arg_type_begin()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2086 | |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2087 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 2088 | N = NewType->arg_type_begin(), |
| 2089 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
| 2090 | QualType ToType = (*O); |
| 2091 | QualType FromType = (*N); |
| 2092 | if (ToType != FromType) { |
| 2093 | if (const PointerType *PTTo = ToType->getAs<PointerType>()) { |
| 2094 | if (const PointerType *PTFr = FromType->getAs<PointerType>()) |
Chandler Carruth | 0ee93de | 2010-05-06 00:15:06 +0000 | [diff] [blame] | 2095 | if ((PTTo->getPointeeType()->isObjCQualifiedIdType() && |
| 2096 | PTFr->getPointeeType()->isObjCQualifiedIdType()) || |
| 2097 | (PTTo->getPointeeType()->isObjCQualifiedClassType() && |
| 2098 | PTFr->getPointeeType()->isObjCQualifiedClassType())) |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2099 | continue; |
| 2100 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2101 | else if (const ObjCObjectPointerType *PTTo = |
| 2102 | ToType->getAs<ObjCObjectPointerType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2103 | if (const ObjCObjectPointerType *PTFr = |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2104 | FromType->getAs<ObjCObjectPointerType>()) |
| 2105 | if (PTTo->getInterfaceDecl() == PTFr->getInterfaceDecl()) |
| 2106 | continue; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2107 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2108 | return false; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 2109 | } |
| 2110 | } |
| 2111 | return true; |
| 2112 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2113 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2114 | /// CheckPointerConversion - Check the pointer conversion from the |
| 2115 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2116 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2117 | /// conversions for which IsPointerConversion has already returned |
| 2118 | /// true. It returns true and produces a diagnostic if there was an |
| 2119 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2120 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2121 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2122 | CXXCastPath& BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2123 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2124 | QualType FromType = From->getType(); |
Argyrios Kyrtzidis | b335872 | 2010-09-28 14:54:11 +0000 | [diff] [blame] | 2125 | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2126 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2127 | Kind = CK_BitCast; |
| 2128 | |
Chandler Carruth | 88f0aed | 2011-04-09 07:32:05 +0000 | [diff] [blame] | 2129 | if (!IsCStyleOrFunctionalCast && |
| 2130 | Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy) && |
| 2131 | From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 2132 | DiagRuntimeBehavior(From->getExprLoc(), From, |
Chandler Carruth | b600669 | 2011-04-09 07:48:17 +0000 | [diff] [blame] | 2133 | PDiag(diag::warn_impcast_bool_to_null_pointer) |
| 2134 | << ToType << From->getSourceRange()); |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 2135 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2136 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
| 2137 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2138 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 2139 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 2140 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2141 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 2142 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2143 | // We must have a derived-to-base conversion. Check an |
| 2144 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2145 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 2146 | From->getExprLoc(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2147 | From->getSourceRange(), &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2148 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2149 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2150 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2151 | // The conversion was successful. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2152 | Kind = CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2153 | } |
| 2154 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2155 | } else if (const ObjCObjectPointerType *ToPtrType = |
| 2156 | ToType->getAs<ObjCObjectPointerType>()) { |
| 2157 | if (const ObjCObjectPointerType *FromPtrType = |
| 2158 | FromType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2159 | // Objective-C++ conversions are always okay. |
| 2160 | // FIXME: We should have a different class of conversions for the |
| 2161 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2162 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2163 | return false; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2164 | } else if (FromType->isBlockPointerType()) { |
| 2165 | Kind = CK_BlockPointerToObjCPointerCast; |
| 2166 | } else { |
| 2167 | Kind = CK_CPointerToObjCPointerCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2168 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2169 | } else if (ToType->isBlockPointerType()) { |
| 2170 | if (!FromType->isBlockPointerType()) |
| 2171 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2172 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2173 | |
| 2174 | // We shouldn't fall into this case unless it's valid for other |
| 2175 | // reasons. |
| 2176 | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 2177 | Kind = CK_NullToPointer; |
| 2178 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2179 | return false; |
| 2180 | } |
| 2181 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2182 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 2183 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 2184 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 2185 | /// If so, returns true and places the converted type (that might differ from |
| 2186 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 2187 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2188 | QualType ToType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2189 | bool InOverloadResolution, |
| 2190 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2191 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2192 | if (!ToTypePtr) |
| 2193 | return false; |
| 2194 | |
| 2195 | // 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] | 2196 | if (From->isNullPointerConstant(Context, |
| 2197 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 2198 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2199 | ConvertedType = ToType; |
| 2200 | return true; |
| 2201 | } |
| 2202 | |
| 2203 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2204 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2205 | if (!FromTypePtr) |
| 2206 | return false; |
| 2207 | |
| 2208 | // A pointer to member of B can be converted to a pointer to member of D, |
| 2209 | // where D is derived from B (C++ 4.11p2). |
| 2210 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 2211 | QualType ToClass(ToTypePtr->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2212 | |
Douglas Gregor | cfddf7b | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2213 | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
| 2214 | !RequireCompleteType(From->getLocStart(), ToClass, PDiag()) && |
| 2215 | IsDerivedFrom(ToClass, FromClass)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2216 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 2217 | ToClass.getTypePtr()); |
| 2218 | return true; |
| 2219 | } |
| 2220 | |
| 2221 | return false; |
| 2222 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2223 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2224 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 2225 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2226 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2227 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 2228 | /// true and produces a diagnostic if there was an error, or returns false |
| 2229 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2230 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2231 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2232 | CXXCastPath &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2233 | bool IgnoreBaseAccess) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2234 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2235 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2236 | if (!FromPtrType) { |
| 2237 | // This must be a null pointer to member pointer conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2238 | assert(From->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2239 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2240 | "Expr must be null pointer constant!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2241 | Kind = CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2242 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2243 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2244 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2245 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2246 | assert(ToPtrType && "No member pointer cast has a target type " |
| 2247 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2248 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2249 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 2250 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2251 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2252 | // FIXME: What about dependent types? |
| 2253 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 2254 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2255 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2256 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 2257 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2258 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 2259 | assert(DerivationOkay && |
| 2260 | "Should not have been called if derivation isn't OK."); |
| 2261 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2262 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2263 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 2264 | getUnqualifiedType())) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2265 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 2266 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 2267 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 2268 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2269 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2270 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2271 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2272 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 2273 | << FromClass << ToClass << QualType(VBase, 0) |
| 2274 | << From->getSourceRange(); |
| 2275 | return true; |
| 2276 | } |
| 2277 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2278 | if (!IgnoreBaseAccess) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2279 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 2280 | Paths.front(), |
| 2281 | diag::err_downcast_from_inaccessible_base); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2282 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2283 | // Must be a base to derived member conversion. |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2284 | BuildBasePathArray(Paths, BasePath); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2285 | Kind = CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2286 | return false; |
| 2287 | } |
| 2288 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2289 | /// IsQualificationConversion - Determines whether the conversion from |
| 2290 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 2291 | /// (C++ 4.4). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2292 | /// |
| 2293 | /// \param ObjCLifetimeConversion Output parameter that will be set to indicate |
| 2294 | /// when the qualification conversion involves a change in the Objective-C |
| 2295 | /// object lifetime. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2296 | bool |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2297 | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2298 | bool CStyle, bool &ObjCLifetimeConversion) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2299 | FromType = Context.getCanonicalType(FromType); |
| 2300 | ToType = Context.getCanonicalType(ToType); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2301 | ObjCLifetimeConversion = false; |
| 2302 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2303 | // If FromType and ToType are the same type, this is not a |
| 2304 | // qualification conversion. |
Sebastian Redl | 22c9240 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 2305 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2306 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2307 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2308 | // (C++ 4.4p4): |
| 2309 | // A conversion can add cv-qualifiers at levels other than the first |
| 2310 | // in multi-level pointers, subject to the following rules: [...] |
| 2311 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2312 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2313 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2314 | // Within each iteration of the loop, we check the qualifiers to |
| 2315 | // determine if this still looks like a qualification |
| 2316 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2317 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2318 | // until there are no more pointers or pointers-to-members left to |
| 2319 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2320 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2321 | |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2322 | Qualifiers FromQuals = FromType.getQualifiers(); |
| 2323 | Qualifiers ToQuals = ToType.getQualifiers(); |
| 2324 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2325 | // Objective-C ARC: |
| 2326 | // Check Objective-C lifetime conversions. |
| 2327 | if (FromQuals.getObjCLifetime() != ToQuals.getObjCLifetime() && |
| 2328 | UnwrappedAnyPointer) { |
| 2329 | if (ToQuals.compatiblyIncludesObjCLifetime(FromQuals)) { |
| 2330 | ObjCLifetimeConversion = true; |
| 2331 | FromQuals.removeObjCLifetime(); |
| 2332 | ToQuals.removeObjCLifetime(); |
| 2333 | } else { |
| 2334 | // Qualification conversions cannot cast between different |
| 2335 | // Objective-C lifetime qualifiers. |
| 2336 | return false; |
| 2337 | } |
| 2338 | } |
| 2339 | |
Douglas Gregor | 377e1bd | 2011-05-08 06:09:53 +0000 | [diff] [blame] | 2340 | // Allow addition/removal of GC attributes but not changing GC attributes. |
| 2341 | if (FromQuals.getObjCGCAttr() != ToQuals.getObjCGCAttr() && |
| 2342 | (!FromQuals.hasObjCGCAttr() || !ToQuals.hasObjCGCAttr())) { |
| 2343 | FromQuals.removeObjCGCAttr(); |
| 2344 | ToQuals.removeObjCGCAttr(); |
| 2345 | } |
| 2346 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2347 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 2348 | // 2,j, and similarly for volatile. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2349 | if (!CStyle && !ToQuals.compatiblyIncludes(FromQuals)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2350 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2351 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2352 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 2353 | // every cv for 0 < k < j. |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2354 | if (!CStyle && FromQuals.getCVRQualifiers() != ToQuals.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2355 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2356 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2357 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2358 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 2359 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2360 | PreviousToQualsIncludeConst |
Douglas Gregor | 621c92a | 2011-04-25 18:40:17 +0000 | [diff] [blame] | 2361 | = PreviousToQualsIncludeConst && ToQuals.hasConst(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2362 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2363 | |
| 2364 | // We are left with FromType and ToType being the pointee types |
| 2365 | // after unwrapping the original FromType and ToType the same number |
| 2366 | // of types. If we unwrapped any pointers, and if FromType and |
| 2367 | // ToType have the same unqualified type (since we checked |
| 2368 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2369 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2372 | /// Determines whether there is a user-defined conversion sequence |
| 2373 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 2374 | /// ToType. If such a conversion exists, User will contain the |
| 2375 | /// user-defined conversion sequence that performs such a conversion |
| 2376 | /// and this routine will return true. Otherwise, this routine returns |
| 2377 | /// false and User is unspecified. |
| 2378 | /// |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2379 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 2380 | /// "explicit" conversion functions as well as non-explicit conversion |
| 2381 | /// functions (C++0x [class.conv.fct]p2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2382 | static OverloadingResult |
| 2383 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 2384 | UserDefinedConversionSequence& User, |
| 2385 | OverloadCandidateSet& CandidateSet, |
| 2386 | bool AllowExplicit) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2387 | // Whether we will only visit constructors. |
| 2388 | bool ConstructorsOnly = false; |
| 2389 | |
| 2390 | // If the type we are conversion to is a class type, enumerate its |
| 2391 | // constructors. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2392 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2393 | // C++ [over.match.ctor]p1: |
| 2394 | // When objects of class type are direct-initialized (8.5), or |
| 2395 | // copy-initialized from an expression of the same or a |
| 2396 | // derived class type (8.5), overload resolution selects the |
| 2397 | // constructor. [...] For copy-initialization, the candidate |
| 2398 | // functions are all the converting constructors (12.3.1) of |
| 2399 | // that class. The argument list is the expression-list within |
| 2400 | // the parentheses of the initializer. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2401 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2402 | (From->getType()->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2403 | S.IsDerivedFrom(From->getType(), ToType))) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2404 | ConstructorsOnly = true; |
| 2405 | |
Argyrios Kyrtzidis | e36bca6 | 2011-04-22 17:45:37 +0000 | [diff] [blame] | 2406 | S.RequireCompleteType(From->getLocStart(), ToType, S.PDiag()); |
| 2407 | // RequireCompleteType may have returned true due to some invalid decl |
| 2408 | // during template instantiation, but ToType may be complete enough now |
| 2409 | // to try to recover. |
| 2410 | if (ToType->isIncompleteType()) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 2411 | // We're not going to find any constructors. |
| 2412 | } else if (CXXRecordDecl *ToRecordDecl |
| 2413 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2414 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2415 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(ToRecordDecl); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2416 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2417 | NamedDecl *D = *Con; |
| 2418 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2419 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2420 | // Find the constructor (which may be a template). |
| 2421 | CXXConstructorDecl *Constructor = 0; |
| 2422 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2423 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2424 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2425 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2426 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 2427 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2428 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2429 | |
Fariborz Jahanian | 52ab92b | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 2430 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 2431 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2432 | if (ConstructorTmpl) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2433 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2434 | /*ExplicitArgs*/ 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2435 | &From, 1, CandidateSet, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2436 | /*SuppressUserConversions=*/ |
| 2437 | !ConstructorsOnly); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2438 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 2439 | // Allow one user-defined conversion when user specifies a |
| 2440 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2441 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 2442 | &From, 1, CandidateSet, |
| 2443 | /*SuppressUserConversions=*/ |
| 2444 | !ConstructorsOnly); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2445 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2446 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2447 | } |
| 2448 | } |
| 2449 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2450 | // Enumerate conversion functions, if we're allowed to. |
| 2451 | if (ConstructorsOnly) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2452 | } else if (S.RequireCompleteType(From->getLocStart(), From->getType(), |
| 2453 | S.PDiag(0) << From->getSourceRange())) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2454 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2455 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2456 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2457 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2458 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 2459 | // Add all of the conversion functions as candidates. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2460 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 2461 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2462 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2463 | E = Conversions->end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2464 | DeclAccessPair FoundDecl = I.getPair(); |
| 2465 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2466 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2467 | if (isa<UsingShadowDecl>(D)) |
| 2468 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2469 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2470 | CXXConversionDecl *Conv; |
| 2471 | FunctionTemplateDecl *ConvTemplate; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2472 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 2473 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2474 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2475 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2476 | |
| 2477 | if (AllowExplicit || !Conv->isExplicit()) { |
| 2478 | if (ConvTemplate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2479 | S.AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
| 2480 | ActingContext, From, ToType, |
| 2481 | CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2482 | else |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2483 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, |
| 2484 | From, ToType, CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2485 | } |
| 2486 | } |
| 2487 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2488 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2489 | |
| 2490 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 2491 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2492 | case OR_Success: |
| 2493 | // Record the standard conversion we used and the conversion function. |
| 2494 | if (CXXConstructorDecl *Constructor |
| 2495 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2496 | S.MarkDeclarationReferenced(From->getLocStart(), Constructor); |
| 2497 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2498 | // C++ [over.ics.user]p1: |
| 2499 | // If the user-defined conversion is specified by a |
| 2500 | // constructor (12.3.1), the initial standard conversion |
| 2501 | // sequence converts the source type to the type required by |
| 2502 | // the argument of the constructor. |
| 2503 | // |
| 2504 | QualType ThisType = Constructor->getThisType(S.Context); |
| 2505 | if (Best->Conversions[0].isEllipsis()) |
| 2506 | User.EllipsisConversion = true; |
| 2507 | else { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2508 | User.Before = Best->Conversions[0].Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2509 | User.EllipsisConversion = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2510 | } |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2511 | User.ConversionFunction = Constructor; |
Douglas Gregor | 83eecbe | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 2512 | User.FoundConversionFunction = Best->FoundDecl.getDecl(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2513 | User.After.setAsIdentityConversion(); |
| 2514 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 2515 | User.After.setAllToTypes(ToType); |
| 2516 | return OR_Success; |
| 2517 | } else if (CXXConversionDecl *Conversion |
| 2518 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2519 | S.MarkDeclarationReferenced(From->getLocStart(), Conversion); |
| 2520 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2521 | // C++ [over.ics.user]p1: |
| 2522 | // |
| 2523 | // [...] If the user-defined conversion is specified by a |
| 2524 | // conversion function (12.3.2), the initial standard |
| 2525 | // conversion sequence converts the source type to the |
| 2526 | // implicit object parameter of the conversion function. |
| 2527 | User.Before = Best->Conversions[0].Standard; |
| 2528 | User.ConversionFunction = Conversion; |
Douglas Gregor | 83eecbe | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 2529 | User.FoundConversionFunction = Best->FoundDecl.getDecl(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2530 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2531 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2532 | // C++ [over.ics.user]p2: |
| 2533 | // The second standard conversion sequence converts the |
| 2534 | // result of the user-defined conversion to the target type |
| 2535 | // for the sequence. Since an implicit conversion sequence |
| 2536 | // is an initialization, the special rules for |
| 2537 | // initialization by user-defined conversion apply when |
| 2538 | // selecting the best user-defined conversion for a |
| 2539 | // user-defined conversion sequence (see 13.3.3 and |
| 2540 | // 13.3.3.1). |
| 2541 | User.After = Best->FinalConversion; |
| 2542 | return OR_Success; |
| 2543 | } else { |
| 2544 | llvm_unreachable("Not a constructor or conversion function?"); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2545 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2548 | case OR_No_Viable_Function: |
| 2549 | return OR_No_Viable_Function; |
| 2550 | case OR_Deleted: |
| 2551 | // No conversion here! We're done. |
| 2552 | return OR_Deleted; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2553 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2554 | case OR_Ambiguous: |
| 2555 | return OR_Ambiguous; |
| 2556 | } |
| 2557 | |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2558 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2559 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2560 | |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2561 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2562 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2563 | ImplicitConversionSequence ICS; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2564 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2565 | OverloadingResult OvResult = |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2566 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2567 | CandidateSet, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2568 | if (OvResult == OR_Ambiguous) |
| 2569 | Diag(From->getSourceRange().getBegin(), |
| 2570 | diag::err_typecheck_ambiguous_condition) |
| 2571 | << From->getType() << ToType << From->getSourceRange(); |
| 2572 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 2573 | Diag(From->getSourceRange().getBegin(), |
| 2574 | diag::err_typecheck_nonviable_condition) |
| 2575 | << From->getType() << ToType << From->getSourceRange(); |
| 2576 | else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2577 | return false; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2578 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &From, 1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2579 | return true; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2580 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2581 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2582 | /// CompareImplicitConversionSequences - Compare two implicit |
| 2583 | /// conversion sequences to determine whether one is better than the |
| 2584 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2585 | static ImplicitConversionSequence::CompareKind |
| 2586 | CompareImplicitConversionSequences(Sema &S, |
| 2587 | const ImplicitConversionSequence& ICS1, |
| 2588 | const ImplicitConversionSequence& ICS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2589 | { |
| 2590 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 2591 | // conversion sequences (as defined in 13.3.3.1) |
| 2592 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 2593 | // conversion sequence than a user-defined conversion sequence or |
| 2594 | // an ellipsis conversion sequence, and |
| 2595 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 2596 | // conversion sequence than an ellipsis conversion sequence |
| 2597 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2598 | // |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2599 | // C++0x [over.best.ics]p10: |
| 2600 | // For the purpose of ranking implicit conversion sequences as |
| 2601 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 2602 | // treated as a user-defined sequence that is indistinguishable |
| 2603 | // from any other user-defined conversion sequence. |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2604 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 2605 | return ImplicitConversionSequence::Better; |
| 2606 | else if (ICS2.getKindRank() < ICS1.getKindRank()) |
| 2607 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2608 | |
Benjamin Kramer | b6eee07 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 2609 | // The following checks require both conversion sequences to be of |
| 2610 | // the same kind. |
| 2611 | if (ICS1.getKind() != ICS2.getKind()) |
| 2612 | return ImplicitConversionSequence::Indistinguishable; |
| 2613 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2614 | // Two implicit conversion sequences of the same form are |
| 2615 | // indistinguishable conversion sequences unless one of the |
| 2616 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2617 | if (ICS1.isStandard()) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2618 | return CompareStandardConversionSequences(S, ICS1.Standard, ICS2.Standard); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2619 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2620 | // User-defined conversion sequence U1 is a better conversion |
| 2621 | // sequence than another user-defined conversion sequence U2 if |
| 2622 | // they contain the same user-defined conversion function or |
| 2623 | // constructor and if the second standard conversion sequence of |
| 2624 | // U1 is better than the second standard conversion sequence of |
| 2625 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2626 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2627 | ICS2.UserDefined.ConversionFunction) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2628 | return CompareStandardConversionSequences(S, |
| 2629 | ICS1.UserDefined.After, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2630 | ICS2.UserDefined.After); |
| 2631 | } |
| 2632 | |
| 2633 | return ImplicitConversionSequence::Indistinguishable; |
| 2634 | } |
| 2635 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2636 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 2637 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 2638 | Qualifiers Quals; |
| 2639 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2640 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2641 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2642 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2643 | return Context.hasSameUnqualifiedType(T1, T2); |
| 2644 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2645 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2646 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 2647 | // determine if one is a proper subset of the other. |
| 2648 | static ImplicitConversionSequence::CompareKind |
| 2649 | compareStandardConversionSubsets(ASTContext &Context, |
| 2650 | const StandardConversionSequence& SCS1, |
| 2651 | const StandardConversionSequence& SCS2) { |
| 2652 | ImplicitConversionSequence::CompareKind Result |
| 2653 | = ImplicitConversionSequence::Indistinguishable; |
| 2654 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2655 | // the identity conversion sequence is considered to be a subsequence of |
Douglas Gregor | ae65f4b | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 2656 | // any non-identity conversion sequence |
Douglas Gregor | 4ae5b72 | 2011-06-05 06:15:20 +0000 | [diff] [blame] | 2657 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 2658 | return ImplicitConversionSequence::Better; |
| 2659 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 2660 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2661 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2662 | if (SCS1.Second != SCS2.Second) { |
| 2663 | if (SCS1.Second == ICK_Identity) |
| 2664 | Result = ImplicitConversionSequence::Better; |
| 2665 | else if (SCS2.Second == ICK_Identity) |
| 2666 | Result = ImplicitConversionSequence::Worse; |
| 2667 | else |
| 2668 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2669 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2670 | return ImplicitConversionSequence::Indistinguishable; |
| 2671 | |
| 2672 | if (SCS1.Third == SCS2.Third) { |
| 2673 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 2674 | : ImplicitConversionSequence::Indistinguishable; |
| 2675 | } |
| 2676 | |
| 2677 | if (SCS1.Third == ICK_Identity) |
| 2678 | return Result == ImplicitConversionSequence::Worse |
| 2679 | ? ImplicitConversionSequence::Indistinguishable |
| 2680 | : ImplicitConversionSequence::Better; |
| 2681 | |
| 2682 | if (SCS2.Third == ICK_Identity) |
| 2683 | return Result == ImplicitConversionSequence::Better |
| 2684 | ? ImplicitConversionSequence::Indistinguishable |
| 2685 | : ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2686 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2687 | return ImplicitConversionSequence::Indistinguishable; |
| 2688 | } |
| 2689 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2690 | /// \brief Determine whether one of the given reference bindings is better |
| 2691 | /// than the other based on what kind of bindings they are. |
| 2692 | static bool isBetterReferenceBindingKind(const StandardConversionSequence &SCS1, |
| 2693 | const StandardConversionSequence &SCS2) { |
| 2694 | // C++0x [over.ics.rank]p3b4: |
| 2695 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 2696 | // implicit object parameter of a non-static member function declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2697 | // without a ref-qualifier, and *either* S1 binds an rvalue reference |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2698 | // 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] | 2699 | // lvalue reference to a function lvalue and S2 binds an rvalue |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2700 | // reference*. |
| 2701 | // |
| 2702 | // FIXME: Rvalue references. We're going rogue with the above edits, |
| 2703 | // because the semantics in the current C++0x working paper (N3225 at the |
| 2704 | // time of this writing) break the standard definition of std::forward |
| 2705 | // and std::reference_wrapper when dealing with references to functions. |
| 2706 | // Proposed wording changes submitted to CWG for consideration. |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 2707 | if (SCS1.BindsImplicitObjectArgumentWithoutRefQualifier || |
| 2708 | SCS2.BindsImplicitObjectArgumentWithoutRefQualifier) |
| 2709 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2710 | |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2711 | return (!SCS1.IsLvalueReference && SCS1.BindsToRvalue && |
| 2712 | SCS2.IsLvalueReference) || |
| 2713 | (SCS1.IsLvalueReference && SCS1.BindsToFunctionLvalue && |
| 2714 | !SCS2.IsLvalueReference); |
| 2715 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2716 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2717 | /// CompareStandardConversionSequences - Compare two standard |
| 2718 | /// conversion sequences to determine whether one is better than the |
| 2719 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2720 | static ImplicitConversionSequence::CompareKind |
| 2721 | CompareStandardConversionSequences(Sema &S, |
| 2722 | const StandardConversionSequence& SCS1, |
| 2723 | const StandardConversionSequence& SCS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2724 | { |
| 2725 | // Standard conversion sequence S1 is a better conversion sequence |
| 2726 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 2727 | |
| 2728 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 2729 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 2730 | // excluding any Lvalue Transformation; the identity conversion |
| 2731 | // sequence is considered to be a subsequence of any |
| 2732 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2733 | if (ImplicitConversionSequence::CompareKind CK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2734 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2735 | return CK; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2736 | |
| 2737 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 2738 | // defined below), or, if not that, |
| 2739 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 2740 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 2741 | if (Rank1 < Rank2) |
| 2742 | return ImplicitConversionSequence::Better; |
| 2743 | else if (Rank2 < Rank1) |
| 2744 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2745 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2746 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 2747 | // are indistinguishable unless one of the following rules |
| 2748 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2749 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2750 | // A conversion that is not a conversion of a pointer, or |
| 2751 | // pointer to member, to bool is better than another conversion |
| 2752 | // that is such a conversion. |
| 2753 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 2754 | return SCS2.isPointerConversionToBool() |
| 2755 | ? ImplicitConversionSequence::Better |
| 2756 | : ImplicitConversionSequence::Worse; |
| 2757 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2758 | // C++ [over.ics.rank]p4b2: |
| 2759 | // |
| 2760 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2761 | // conversion of B* to A* is better than conversion of B* to |
| 2762 | // void*, and conversion of A* to void* is better than conversion |
| 2763 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2764 | bool SCS1ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2765 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2766 | bool SCS2ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2767 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2768 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 2769 | // Exactly one of the conversion sequences is a conversion to |
| 2770 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2771 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 2772 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2773 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 2774 | // Neither conversion sequence converts to a void pointer; compare |
| 2775 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2776 | if (ImplicitConversionSequence::CompareKind DerivedCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2777 | = CompareDerivedToBaseConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2778 | return DerivedCK; |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 2779 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid && |
| 2780 | !S.Context.hasSameType(SCS1.getFromType(), SCS2.getFromType())) { |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2781 | // Both conversion sequences are conversions to void |
| 2782 | // pointers. Compare the source types to determine if there's an |
| 2783 | // inheritance relationship in their sources. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2784 | QualType FromType1 = SCS1.getFromType(); |
| 2785 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2786 | |
| 2787 | // Adjust the types we're converting from via the array-to-pointer |
| 2788 | // conversion, if we need to. |
| 2789 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2790 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2791 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2792 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2793 | |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 2794 | QualType FromPointee1 = FromType1->getPointeeType().getUnqualifiedType(); |
| 2795 | QualType FromPointee2 = FromType2->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2796 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2797 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2798 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2799 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2800 | return ImplicitConversionSequence::Worse; |
| 2801 | |
| 2802 | // Objective-C++: If one interface is more specific than the |
| 2803 | // other, it is the better one. |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 2804 | const ObjCObjectPointerType* FromObjCPtr1 |
| 2805 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 2806 | const ObjCObjectPointerType* FromObjCPtr2 |
| 2807 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 2808 | if (FromObjCPtr1 && FromObjCPtr2) { |
| 2809 | bool AssignLeft = S.Context.canAssignObjCInterfaces(FromObjCPtr1, |
| 2810 | FromObjCPtr2); |
| 2811 | bool AssignRight = S.Context.canAssignObjCInterfaces(FromObjCPtr2, |
| 2812 | FromObjCPtr1); |
| 2813 | if (AssignLeft != AssignRight) { |
| 2814 | return AssignLeft? ImplicitConversionSequence::Better |
| 2815 | : ImplicitConversionSequence::Worse; |
| 2816 | } |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2817 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2818 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2819 | |
| 2820 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 2821 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2822 | if (ImplicitConversionSequence::CompareKind QualCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2823 | = CompareQualificationConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2824 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2825 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2826 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2827 | // Check for a better reference binding based on the kind of bindings. |
| 2828 | if (isBetterReferenceBindingKind(SCS1, SCS2)) |
| 2829 | return ImplicitConversionSequence::Better; |
| 2830 | else if (isBetterReferenceBindingKind(SCS2, SCS1)) |
| 2831 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2832 | |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 2833 | // C++ [over.ics.rank]p3b4: |
| 2834 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 2835 | // which the references refer are the same type except for |
| 2836 | // top-level cv-qualifiers, and the type to which the reference |
| 2837 | // initialized by S2 refers is more cv-qualified than the type |
| 2838 | // to which the reference initialized by S1 refers. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2839 | QualType T1 = SCS1.getToType(2); |
| 2840 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2841 | T1 = S.Context.getCanonicalType(T1); |
| 2842 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2843 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2844 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2845 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2846 | if (UnqualT1 == UnqualT2) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2847 | // Objective-C++ ARC: If the references refer to objects with different |
| 2848 | // lifetimes, prefer bindings that don't change lifetime. |
| 2849 | if (SCS1.ObjCLifetimeConversionBinding != |
| 2850 | SCS2.ObjCLifetimeConversionBinding) { |
| 2851 | return SCS1.ObjCLifetimeConversionBinding |
| 2852 | ? ImplicitConversionSequence::Worse |
| 2853 | : ImplicitConversionSequence::Better; |
| 2854 | } |
| 2855 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2856 | // If the type is an array type, promote the element qualifiers to the |
| 2857 | // type for comparison. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2858 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2859 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2860 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2861 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2862 | if (T2.isMoreQualifiedThan(T1)) |
| 2863 | return ImplicitConversionSequence::Better; |
| 2864 | else if (T1.isMoreQualifiedThan(T2)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2865 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2866 | } |
| 2867 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2868 | |
| 2869 | return ImplicitConversionSequence::Indistinguishable; |
| 2870 | } |
| 2871 | |
| 2872 | /// CompareQualificationConversions - Compares two standard conversion |
| 2873 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2874 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 2875 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2876 | CompareQualificationConversions(Sema &S, |
| 2877 | const StandardConversionSequence& SCS1, |
| 2878 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 2879 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2880 | // -- S1 and S2 differ only in their qualification conversion and |
| 2881 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 2882 | // cv-qualification signature of type T1 is a proper subset of |
| 2883 | // the cv-qualification signature of type T2, and S1 is not the |
| 2884 | // deprecated string literal array-to-pointer conversion (4.2). |
| 2885 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 2886 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 2887 | return ImplicitConversionSequence::Indistinguishable; |
| 2888 | |
| 2889 | // FIXME: the example in the standard doesn't use a qualification |
| 2890 | // conversion (!) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2891 | QualType T1 = SCS1.getToType(2); |
| 2892 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2893 | T1 = S.Context.getCanonicalType(T1); |
| 2894 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2895 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2896 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2897 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2898 | |
| 2899 | // If the types are the same, we won't learn anything by unwrapped |
| 2900 | // them. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2901 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2902 | return ImplicitConversionSequence::Indistinguishable; |
| 2903 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2904 | // If the type is an array type, promote the element qualifiers to the type |
| 2905 | // for comparison. |
| 2906 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2907 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2908 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2909 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2910 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2911 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2912 | = ImplicitConversionSequence::Indistinguishable; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2913 | |
| 2914 | // Objective-C++ ARC: |
| 2915 | // Prefer qualification conversions not involving a change in lifetime |
| 2916 | // to qualification conversions that do not change lifetime. |
| 2917 | if (SCS1.QualificationIncludesObjCLifetime != |
| 2918 | SCS2.QualificationIncludesObjCLifetime) { |
| 2919 | Result = SCS1.QualificationIncludesObjCLifetime |
| 2920 | ? ImplicitConversionSequence::Worse |
| 2921 | : ImplicitConversionSequence::Better; |
| 2922 | } |
| 2923 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2924 | while (S.Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2925 | // Within each iteration of the loop, we check the qualifiers to |
| 2926 | // determine if this still looks like a qualification |
| 2927 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2928 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2929 | // until there are no more pointers or pointers-to-members left |
| 2930 | // to unwrap. This essentially mimics what |
| 2931 | // IsQualificationConversion does, but here we're checking for a |
| 2932 | // strict subset of qualifiers. |
| 2933 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 2934 | // The qualifiers are the same, so this doesn't tell us anything |
| 2935 | // about how the sequences rank. |
| 2936 | ; |
| 2937 | else if (T2.isMoreQualifiedThan(T1)) { |
| 2938 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 2939 | if (Result == ImplicitConversionSequence::Worse) |
| 2940 | // Neither has qualifiers that are a subset of the other's |
| 2941 | // qualifiers. |
| 2942 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2943 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2944 | Result = ImplicitConversionSequence::Better; |
| 2945 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 2946 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 2947 | if (Result == ImplicitConversionSequence::Better) |
| 2948 | // Neither has qualifiers that are a subset of the other's |
| 2949 | // qualifiers. |
| 2950 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2951 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2952 | Result = ImplicitConversionSequence::Worse; |
| 2953 | } else { |
| 2954 | // Qualifiers are disjoint. |
| 2955 | return ImplicitConversionSequence::Indistinguishable; |
| 2956 | } |
| 2957 | |
| 2958 | // If the types after this point are equivalent, we're done. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2959 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2960 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2963 | // Check that the winning standard conversion sequence isn't using |
| 2964 | // the deprecated string literal array to pointer conversion. |
| 2965 | switch (Result) { |
| 2966 | case ImplicitConversionSequence::Better: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2967 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2968 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2969 | break; |
| 2970 | |
| 2971 | case ImplicitConversionSequence::Indistinguishable: |
| 2972 | break; |
| 2973 | |
| 2974 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2975 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2976 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2977 | break; |
| 2978 | } |
| 2979 | |
| 2980 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2983 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 2984 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2985 | /// various kinds of derived-to-base conversions (C++ |
| 2986 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 2987 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2988 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2989 | CompareDerivedToBaseConversions(Sema &S, |
| 2990 | const StandardConversionSequence& SCS1, |
| 2991 | const StandardConversionSequence& SCS2) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2992 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2993 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2994 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2995 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2996 | |
| 2997 | // Adjust the types we're converting from via the array-to-pointer |
| 2998 | // conversion, if we need to. |
| 2999 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3000 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3001 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3002 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3003 | |
| 3004 | // Canonicalize all of the types. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3005 | FromType1 = S.Context.getCanonicalType(FromType1); |
| 3006 | ToType1 = S.Context.getCanonicalType(ToType1); |
| 3007 | FromType2 = S.Context.getCanonicalType(FromType2); |
| 3008 | ToType2 = S.Context.getCanonicalType(ToType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3009 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3010 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3011 | // |
| 3012 | // If class B is derived directly or indirectly from class A and |
| 3013 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3014 | // |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3015 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3016 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 3017 | SCS2.Second == ICK_Pointer_Conversion && |
| 3018 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 3019 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 3020 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3021 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3022 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3023 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3024 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3025 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3026 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3027 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3028 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 3029 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3030 | // -- 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] | 3031 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3032 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3033 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3034 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3035 | return ImplicitConversionSequence::Worse; |
| 3036 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3037 | |
| 3038 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 3039 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3040 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3041 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3042 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3043 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3044 | } |
| 3045 | } else if (SCS1.Second == ICK_Pointer_Conversion && |
| 3046 | SCS2.Second == ICK_Pointer_Conversion) { |
| 3047 | const ObjCObjectPointerType *FromPtr1 |
| 3048 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 3049 | const ObjCObjectPointerType *FromPtr2 |
| 3050 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 3051 | const ObjCObjectPointerType *ToPtr1 |
| 3052 | = ToType1->getAs<ObjCObjectPointerType>(); |
| 3053 | const ObjCObjectPointerType *ToPtr2 |
| 3054 | = ToType2->getAs<ObjCObjectPointerType>(); |
| 3055 | |
| 3056 | if (FromPtr1 && FromPtr2 && ToPtr1 && ToPtr2) { |
| 3057 | // Apply the same conversion ranking rules for Objective-C pointer types |
| 3058 | // that we do for C++ pointers to class types. However, we employ the |
| 3059 | // Objective-C pseudo-subtyping relationship used for assignment of |
| 3060 | // Objective-C pointer types. |
| 3061 | bool FromAssignLeft |
| 3062 | = S.Context.canAssignObjCInterfaces(FromPtr1, FromPtr2); |
| 3063 | bool FromAssignRight |
| 3064 | = S.Context.canAssignObjCInterfaces(FromPtr2, FromPtr1); |
| 3065 | bool ToAssignLeft |
| 3066 | = S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2); |
| 3067 | bool ToAssignRight |
| 3068 | = S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1); |
| 3069 | |
| 3070 | // A conversion to an a non-id object pointer type or qualified 'id' |
| 3071 | // type is better than a conversion to 'id'. |
| 3072 | if (ToPtr1->isObjCIdType() && |
| 3073 | (ToPtr2->isObjCQualifiedIdType() || ToPtr2->getInterfaceDecl())) |
| 3074 | return ImplicitConversionSequence::Worse; |
| 3075 | if (ToPtr2->isObjCIdType() && |
| 3076 | (ToPtr1->isObjCQualifiedIdType() || ToPtr1->getInterfaceDecl())) |
| 3077 | return ImplicitConversionSequence::Better; |
| 3078 | |
| 3079 | // A conversion to a non-id object pointer type is better than a |
| 3080 | // conversion to a qualified 'id' type |
| 3081 | if (ToPtr1->isObjCQualifiedIdType() && ToPtr2->getInterfaceDecl()) |
| 3082 | return ImplicitConversionSequence::Worse; |
| 3083 | if (ToPtr2->isObjCQualifiedIdType() && ToPtr1->getInterfaceDecl()) |
| 3084 | return ImplicitConversionSequence::Better; |
| 3085 | |
| 3086 | // A conversion to an a non-Class object pointer type or qualified 'Class' |
| 3087 | // type is better than a conversion to 'Class'. |
| 3088 | if (ToPtr1->isObjCClassType() && |
| 3089 | (ToPtr2->isObjCQualifiedClassType() || ToPtr2->getInterfaceDecl())) |
| 3090 | return ImplicitConversionSequence::Worse; |
| 3091 | if (ToPtr2->isObjCClassType() && |
| 3092 | (ToPtr1->isObjCQualifiedClassType() || ToPtr1->getInterfaceDecl())) |
| 3093 | return ImplicitConversionSequence::Better; |
| 3094 | |
| 3095 | // A conversion to a non-Class object pointer type is better than a |
| 3096 | // conversion to a qualified 'Class' type. |
| 3097 | if (ToPtr1->isObjCQualifiedClassType() && ToPtr2->getInterfaceDecl()) |
| 3098 | return ImplicitConversionSequence::Worse; |
| 3099 | if (ToPtr2->isObjCQualifiedClassType() && ToPtr1->getInterfaceDecl()) |
| 3100 | return ImplicitConversionSequence::Better; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3101 | |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3102 | // -- "conversion of C* to B* is better than conversion of C* to A*," |
| 3103 | if (S.Context.hasSameType(FromType1, FromType2) && |
| 3104 | !FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType() && |
| 3105 | (ToAssignLeft != ToAssignRight)) |
| 3106 | return ToAssignLeft? ImplicitConversionSequence::Worse |
| 3107 | : ImplicitConversionSequence::Better; |
| 3108 | |
| 3109 | // -- "conversion of B* to A* is better than conversion of C* to A*," |
| 3110 | if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) && |
| 3111 | (FromAssignLeft != FromAssignRight)) |
| 3112 | return FromAssignLeft? ImplicitConversionSequence::Better |
| 3113 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3114 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3115 | } |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 3116 | |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3117 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3118 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 3119 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 3120 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3121 | const MemberPointerType * FromMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3122 | FromType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3123 | const MemberPointerType * ToMemPointer1 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3124 | ToType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3125 | const MemberPointerType * FromMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3126 | FromType2->getAs<MemberPointerType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3127 | const MemberPointerType * ToMemPointer2 = |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3128 | ToType2->getAs<MemberPointerType>(); |
| 3129 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 3130 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 3131 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 3132 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 3133 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 3134 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 3135 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 3136 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 3137 | // 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] | 3138 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3139 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3140 | return ImplicitConversionSequence::Worse; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3141 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3142 | return ImplicitConversionSequence::Better; |
| 3143 | } |
| 3144 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 3145 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3146 | if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3147 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3148 | else if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 3149 | return ImplicitConversionSequence::Worse; |
| 3150 | } |
| 3151 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3152 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3153 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3154 | // -- 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] | 3155 | // -- binding of an expression of type C to a reference of type |
| 3156 | // B& is better than binding an expression of type C to a |
| 3157 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3158 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3159 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3160 | if (S.IsDerivedFrom(ToType1, ToType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3161 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3162 | else if (S.IsDerivedFrom(ToType2, ToType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3163 | return ImplicitConversionSequence::Worse; |
| 3164 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3165 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3166 | // -- 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] | 3167 | // -- binding of an expression of type B to a reference of type |
| 3168 | // A& is better than binding an expression of type C to a |
| 3169 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3170 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 3171 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 3172 | if (S.IsDerivedFrom(FromType2, FromType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3173 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3174 | else if (S.IsDerivedFrom(FromType1, FromType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3175 | return ImplicitConversionSequence::Worse; |
| 3176 | } |
| 3177 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3178 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 3179 | return ImplicitConversionSequence::Indistinguishable; |
| 3180 | } |
| 3181 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3182 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 3183 | /// determine whether they are reference-related, |
| 3184 | /// reference-compatible, reference-compatible with added |
| 3185 | /// qualification, or incompatible, for use in C++ initialization by |
| 3186 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 3187 | /// type, and the first type (T1) is the pointee type of the reference |
| 3188 | /// type being initialized. |
| 3189 | Sema::ReferenceCompareResult |
| 3190 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 3191 | QualType OrigT1, QualType OrigT2, |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3192 | bool &DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3193 | bool &ObjCConversion, |
| 3194 | bool &ObjCLifetimeConversion) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3195 | assert(!OrigT1->isReferenceType() && |
| 3196 | "T1 must be the pointee type of the reference type"); |
| 3197 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 3198 | |
| 3199 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 3200 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 3201 | Qualifiers T1Quals, T2Quals; |
| 3202 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 3203 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 3204 | |
| 3205 | // C++ [dcl.init.ref]p4: |
| 3206 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 3207 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 3208 | // T1 is a base class of T2. |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3209 | DerivedToBase = false; |
| 3210 | ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3211 | ObjCLifetimeConversion = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3212 | if (UnqualT1 == UnqualT2) { |
| 3213 | // Nothing to do. |
| 3214 | } else if (!RequireCompleteType(Loc, OrigT2, PDiag()) && |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3215 | IsDerivedFrom(UnqualT2, UnqualT1)) |
| 3216 | DerivedToBase = true; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3217 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
| 3218 | UnqualT2->isObjCObjectOrInterfaceType() && |
| 3219 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
| 3220 | ObjCConversion = true; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3221 | else |
| 3222 | return Ref_Incompatible; |
| 3223 | |
| 3224 | // At this point, we know that T1 and T2 are reference-related (at |
| 3225 | // least). |
| 3226 | |
| 3227 | // If the type is an array type, promote the element qualifiers to the type |
| 3228 | // for comparison. |
| 3229 | if (isa<ArrayType>(T1) && T1Quals) |
| 3230 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 3231 | if (isa<ArrayType>(T2) && T2Quals) |
| 3232 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 3233 | |
| 3234 | // C++ [dcl.init.ref]p4: |
| 3235 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 3236 | // reference-related to T2 and cv1 is the same cv-qualification |
| 3237 | // as, or greater cv-qualification than, cv2. For purposes of |
| 3238 | // overload resolution, cases for which cv1 is greater |
| 3239 | // cv-qualification than cv2 are identified as |
| 3240 | // reference-compatible with added qualification (see 13.3.3.2). |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 3241 | // |
| 3242 | // Note that we also require equivalence of Objective-C GC and address-space |
| 3243 | // qualifiers when performing these computations, so that e.g., an int in |
| 3244 | // address space 1 is not reference-compatible with an int in address |
| 3245 | // space 2. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3246 | if (T1Quals.getObjCLifetime() != T2Quals.getObjCLifetime() && |
| 3247 | T1Quals.compatiblyIncludesObjCLifetime(T2Quals)) { |
| 3248 | T1Quals.removeObjCLifetime(); |
| 3249 | T2Quals.removeObjCLifetime(); |
| 3250 | ObjCLifetimeConversion = true; |
| 3251 | } |
| 3252 | |
Douglas Gregor | a6ce3e6 | 2011-04-28 17:56:11 +0000 | [diff] [blame] | 3253 | if (T1Quals == T2Quals) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3254 | return Ref_Compatible; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3255 | else if (T1Quals.compatiblyIncludes(T2Quals)) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3256 | return Ref_Compatible_With_Added_Qualification; |
| 3257 | else |
| 3258 | return Ref_Related; |
| 3259 | } |
| 3260 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3261 | /// \brief Look for a user-defined conversion to an value reference-compatible |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3262 | /// with DeclType. Return true if something definite is found. |
| 3263 | static bool |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3264 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
| 3265 | QualType DeclType, SourceLocation DeclLoc, |
| 3266 | Expr *Init, QualType T2, bool AllowRvalues, |
| 3267 | bool AllowExplicit) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3268 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 3269 | CXXRecordDecl *T2RecordDecl |
| 3270 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 3271 | |
| 3272 | OverloadCandidateSet CandidateSet(DeclLoc); |
| 3273 | const UnresolvedSetImpl *Conversions |
| 3274 | = T2RecordDecl->getVisibleConversionFunctions(); |
| 3275 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 3276 | E = Conversions->end(); I != E; ++I) { |
| 3277 | NamedDecl *D = *I; |
| 3278 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3279 | if (isa<UsingShadowDecl>(D)) |
| 3280 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3281 | |
| 3282 | FunctionTemplateDecl *ConvTemplate |
| 3283 | = dyn_cast<FunctionTemplateDecl>(D); |
| 3284 | CXXConversionDecl *Conv; |
| 3285 | if (ConvTemplate) |
| 3286 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3287 | else |
| 3288 | Conv = cast<CXXConversionDecl>(D); |
| 3289 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3290 | // 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] | 3291 | // explicit conversions, skip it. |
| 3292 | if (!AllowExplicit && Conv->isExplicit()) |
| 3293 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3294 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3295 | if (AllowRvalues) { |
| 3296 | bool DerivedToBase = false; |
| 3297 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3298 | bool ObjCLifetimeConversion = false; |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3299 | if (!ConvTemplate && |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3300 | S.CompareReferenceRelationship( |
| 3301 | DeclLoc, |
| 3302 | Conv->getConversionType().getNonReferenceType() |
| 3303 | .getUnqualifiedType(), |
| 3304 | DeclType.getNonReferenceType().getUnqualifiedType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3305 | DerivedToBase, ObjCConversion, ObjCLifetimeConversion) == |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3306 | Sema::Ref_Incompatible) |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3307 | continue; |
| 3308 | } else { |
| 3309 | // If the conversion function doesn't return a reference type, |
| 3310 | // it can't be considered for this conversion. An rvalue reference |
| 3311 | // is only acceptable if its referencee is a function type. |
| 3312 | |
| 3313 | const ReferenceType *RefType = |
| 3314 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 3315 | if (!RefType || |
| 3316 | (!RefType->isLValueReferenceType() && |
| 3317 | !RefType->getPointeeType()->isFunctionType())) |
| 3318 | continue; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3319 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3320 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3321 | if (ConvTemplate) |
| 3322 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3323 | Init, DeclType, CandidateSet); |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3324 | else |
| 3325 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3326 | DeclType, CandidateSet); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
| 3329 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3330 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3331 | case OR_Success: |
| 3332 | // C++ [over.ics.ref]p1: |
| 3333 | // |
| 3334 | // [...] If the parameter binds directly to the result of |
| 3335 | // applying a conversion function to the argument |
| 3336 | // expression, the implicit conversion sequence is a |
| 3337 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 3338 | // second standard conversion sequence either an identity |
| 3339 | // conversion or, if the conversion function returns an |
| 3340 | // entity of a type that is a derived class of the parameter |
| 3341 | // type, a derived-to-base Conversion. |
| 3342 | if (!Best->FinalConversion.DirectBinding) |
| 3343 | return false; |
| 3344 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3345 | if (Best->Function) |
| 3346 | S.MarkDeclarationReferenced(DeclLoc, Best->Function); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3347 | ICS.setUserDefined(); |
| 3348 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 3349 | ICS.UserDefined.After = Best->FinalConversion; |
| 3350 | ICS.UserDefined.ConversionFunction = Best->Function; |
Douglas Gregor | 83eecbe | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 3351 | ICS.UserDefined.FoundConversionFunction = Best->FoundDecl.getDecl(); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3352 | ICS.UserDefined.EllipsisConversion = false; |
| 3353 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 3354 | ICS.UserDefined.After.DirectBinding && |
| 3355 | "Expected a direct reference binding!"); |
| 3356 | return true; |
| 3357 | |
| 3358 | case OR_Ambiguous: |
| 3359 | ICS.setAmbiguous(); |
| 3360 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3361 | Cand != CandidateSet.end(); ++Cand) |
| 3362 | if (Cand->Viable) |
| 3363 | ICS.Ambiguous.addConversion(Cand->Function); |
| 3364 | return true; |
| 3365 | |
| 3366 | case OR_No_Viable_Function: |
| 3367 | case OR_Deleted: |
| 3368 | // There was no suitable conversion, or we found a deleted |
| 3369 | // conversion; continue with other checks. |
| 3370 | return false; |
| 3371 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3372 | |
Eric Christopher | 1c3d502 | 2010-06-30 18:36:32 +0000 | [diff] [blame] | 3373 | return false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3374 | } |
| 3375 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3376 | /// \brief Compute an implicit conversion sequence for reference |
| 3377 | /// initialization. |
| 3378 | static ImplicitConversionSequence |
| 3379 | TryReferenceInit(Sema &S, Expr *&Init, QualType DeclType, |
| 3380 | SourceLocation DeclLoc, |
| 3381 | bool SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 3382 | bool AllowExplicit) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3383 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 3384 | |
| 3385 | // Most paths end in a failed conversion. |
| 3386 | ImplicitConversionSequence ICS; |
| 3387 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 3388 | |
| 3389 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 3390 | QualType T2 = Init->getType(); |
| 3391 | |
| 3392 | // If the initializer is the address of an overloaded function, try |
| 3393 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3394 | // type of the resulting function. |
| 3395 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 3396 | DeclAccessPair Found; |
| 3397 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 3398 | false, Found)) |
| 3399 | T2 = Fn->getType(); |
| 3400 | } |
| 3401 | |
| 3402 | // Compute some basic properties of the types and the initializer. |
| 3403 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 3404 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3405 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3406 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3407 | Expr::Classification InitCategory = Init->Classify(S.Context); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3408 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3409 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3410 | ObjCConversion, ObjCLifetimeConversion); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3411 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3412 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3413 | // C++0x [dcl.init.ref]p5: |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 3414 | // A reference to type "cv1 T1" is initialized by an expression |
| 3415 | // of type "cv2 T2" as follows: |
| 3416 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3417 | // -- If reference is an lvalue reference and the initializer expression |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3418 | if (!isRValRef) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3419 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 3420 | // reference-compatible with "cv2 T2," or |
| 3421 | // |
| 3422 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 3423 | if (InitCategory.isLValue() && |
| 3424 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3425 | // C++ [over.ics.ref]p1: |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3426 | // When a parameter of reference type binds directly (8.5.3) |
| 3427 | // to an argument expression, the implicit conversion sequence |
| 3428 | // is the identity conversion, unless the argument expression |
| 3429 | // has a type that is a derived class of the parameter type, |
| 3430 | // in which case the implicit conversion sequence is a |
| 3431 | // derived-to-base Conversion (13.3.3.1). |
| 3432 | ICS.setStandard(); |
| 3433 | ICS.Standard.First = ICK_Identity; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3434 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 3435 | : ObjCConversion? ICK_Compatible_Conversion |
| 3436 | : ICK_Identity; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3437 | ICS.Standard.Third = ICK_Identity; |
| 3438 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 3439 | ICS.Standard.setToType(0, T2); |
| 3440 | ICS.Standard.setToType(1, T1); |
| 3441 | ICS.Standard.setToType(2, T1); |
| 3442 | ICS.Standard.ReferenceBinding = true; |
| 3443 | ICS.Standard.DirectBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3444 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3445 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 3446 | ICS.Standard.BindsToRvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3447 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3448 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3449 | ICS.Standard.CopyConstructor = 0; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3450 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3451 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 3452 | // derived-to-base conversions is suppressed when we're |
| 3453 | // computing the implicit conversion sequence (C++ |
| 3454 | // [over.best.ics]p2). |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3455 | return ICS; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3456 | } |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3457 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3458 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 3459 | // not reference-related to T2, and can be implicitly |
| 3460 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 3461 | // is reference-compatible with "cv3 T3" 92) (this |
| 3462 | // conversion is selected by enumerating the applicable |
| 3463 | // conversion functions (13.3.1.6) and choosing the best |
| 3464 | // one through overload resolution (13.3)), |
| 3465 | if (!SuppressUserConversions && T2->isRecordType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3466 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3467 | RefRelationship == Sema::Ref_Incompatible) { |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3468 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 3469 | Init, T2, /*AllowRvalues=*/false, |
| 3470 | AllowExplicit)) |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3471 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3472 | } |
| 3473 | } |
| 3474 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3475 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 3476 | // 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] | 3477 | // shall be an rvalue reference. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3478 | // |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 3479 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 3480 | // point, which is that, due to p2 (which short-circuits reference |
| 3481 | // binding by only attempting a simple conversion for non-direct |
| 3482 | // bindings) and p3's strange wording, we allow a const volatile |
| 3483 | // reference to bind to an rvalue. Hence the check for the presence |
| 3484 | // of "const" rather than checking for "const" being the only |
| 3485 | // qualifier. |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3486 | // This is also the point where rvalue references and lvalue inits no longer |
| 3487 | // go together. |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 3488 | if (!isRValRef && !T1.isConstQualified()) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3489 | return ICS; |
| 3490 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3491 | // -- If the initializer expression |
| 3492 | // |
| 3493 | // -- is an xvalue, class prvalue, array prvalue or function |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3494 | // lvalue and "cv1 T1" is reference-compatible with "cv2 T2", or |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3495 | if (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification && |
| 3496 | (InitCategory.isXValue() || |
| 3497 | (InitCategory.isPRValue() && (T2->isRecordType() || T2->isArrayType())) || |
| 3498 | (InitCategory.isLValue() && T2->isFunctionType()))) { |
| 3499 | ICS.setStandard(); |
| 3500 | ICS.Standard.First = ICK_Identity; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3501 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3502 | : ObjCConversion? ICK_Compatible_Conversion |
| 3503 | : ICK_Identity; |
| 3504 | ICS.Standard.Third = ICK_Identity; |
| 3505 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 3506 | ICS.Standard.setToType(0, T2); |
| 3507 | ICS.Standard.setToType(1, T1); |
| 3508 | ICS.Standard.setToType(2, T1); |
| 3509 | ICS.Standard.ReferenceBinding = true; |
| 3510 | // In C++0x, this is always a direct binding. In C++98/03, it's a direct |
| 3511 | // binding unless we're binding to a class prvalue. |
| 3512 | // Note: Although xvalues wouldn't normally show up in C++98/03 code, we |
| 3513 | // allow the use of rvalue references in C++98/03 for the benefit of |
| 3514 | // standard library implementors; therefore, we need the xvalue check here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3515 | ICS.Standard.DirectBinding = |
| 3516 | S.getLangOptions().CPlusPlus0x || |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3517 | (InitCategory.isPRValue() && !T2->isRecordType()); |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3518 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3519 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3520 | ICS.Standard.BindsToRvalue = InitCategory.isRValue(); |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3521 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3522 | ICS.Standard.ObjCLifetimeConversionBinding = ObjCLifetimeConversion; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3523 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3524 | return ICS; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3525 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3526 | |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3527 | // -- has a class type (i.e., T2 is a class type), where T1 is not |
| 3528 | // reference-related to T2, and can be implicitly converted to |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3529 | // an xvalue, class prvalue, or function lvalue of type |
| 3530 | // "cv3 T3", where "cv1 T1" is reference-compatible with |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3531 | // "cv3 T3", |
| 3532 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3533 | // then the reference is bound to the value of the initializer |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3534 | // expression in the first case and to the result of the conversion |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3535 | // in the second case (or, in either case, to an appropriate base |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3536 | // class subobject). |
| 3537 | if (!SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3538 | T2->isRecordType() && !S.RequireCompleteType(DeclLoc, T2, 0) && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3539 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 3540 | Init, T2, /*AllowRvalues=*/true, |
| 3541 | AllowExplicit)) { |
| 3542 | // In the second case, if the reference is an rvalue reference |
| 3543 | // and the second standard conversion sequence of the |
| 3544 | // user-defined conversion sequence includes an lvalue-to-rvalue |
| 3545 | // conversion, the program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3546 | if (ICS.isUserDefined() && isRValRef && |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3547 | ICS.UserDefined.After.First == ICK_Lvalue_To_Rvalue) |
| 3548 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 3549 | |
Douglas Gregor | 68ed68b | 2011-01-21 16:36:05 +0000 | [diff] [blame] | 3550 | return ICS; |
Rafael Espindola | aa5952c | 2011-01-22 15:32:35 +0000 | [diff] [blame] | 3551 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3552 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3553 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 3554 | // initialized from the initializer expression using the |
| 3555 | // rules for a non-reference copy initialization (8.5). The |
| 3556 | // reference is then bound to the temporary. If T1 is |
| 3557 | // reference-related to T2, cv1 must be the same |
| 3558 | // cv-qualification as, or greater cv-qualification than, |
| 3559 | // cv2; otherwise, the program is ill-formed. |
| 3560 | if (RefRelationship == Sema::Ref_Related) { |
| 3561 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 3562 | // we would be reference-compatible or reference-compatible with |
| 3563 | // added qualification. But that wasn't the case, so the reference |
| 3564 | // initialization fails. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3565 | // |
| 3566 | // Note that we only want to check address spaces and cvr-qualifiers here. |
| 3567 | // ObjC GC and lifetime qualifiers aren't important. |
| 3568 | Qualifiers T1Quals = T1.getQualifiers(); |
| 3569 | Qualifiers T2Quals = T2.getQualifiers(); |
| 3570 | T1Quals.removeObjCGCAttr(); |
| 3571 | T1Quals.removeObjCLifetime(); |
| 3572 | T2Quals.removeObjCGCAttr(); |
| 3573 | T2Quals.removeObjCLifetime(); |
| 3574 | if (!T1Quals.compatiblyIncludes(T2Quals)) |
| 3575 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3576 | } |
| 3577 | |
| 3578 | // If at least one of the types is a class type, the types are not |
| 3579 | // related, and we aren't allowed any user conversions, the |
| 3580 | // reference binding fails. This case is important for breaking |
| 3581 | // recursion, since TryImplicitConversion below will attempt to |
| 3582 | // create a temporary through the use of a copy constructor. |
| 3583 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 3584 | (T1->isRecordType() || T2->isRecordType())) |
| 3585 | return ICS; |
| 3586 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 3587 | // If T1 is reference-related to T2 and the reference is an rvalue |
| 3588 | // reference, the initializer expression shall not be an lvalue. |
| 3589 | if (RefRelationship >= Sema::Ref_Related && |
| 3590 | isRValRef && Init->Classify(S.Context).isLValue()) |
| 3591 | return ICS; |
| 3592 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3593 | // C++ [over.ics.ref]p2: |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3594 | // When a parameter of reference type is not bound directly to |
| 3595 | // an argument expression, the conversion sequence is the one |
| 3596 | // required to convert the argument expression to the |
| 3597 | // underlying type of the reference according to |
| 3598 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 3599 | // to copy-initializing a temporary of the underlying type with |
| 3600 | // the argument expression. Any difference in top-level |
| 3601 | // cv-qualification is subsumed by the initialization itself |
| 3602 | // and does not constitute a conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3603 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
| 3604 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3605 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3606 | /*CStyle=*/false, |
| 3607 | /*AllowObjCWritebackConversion=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3608 | |
| 3609 | // Of course, that's still a reference binding. |
| 3610 | if (ICS.isStandard()) { |
| 3611 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3612 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3613 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 3614 | ICS.Standard.BindsToRvalue = true; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3615 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3616 | ICS.Standard.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3617 | } else if (ICS.isUserDefined()) { |
| 3618 | ICS.UserDefined.After.ReferenceBinding = true; |
Douglas Gregor | f20d272 | 2011-08-15 13:59:46 +0000 | [diff] [blame] | 3619 | ICS.UserDefined.After.IsLvalueReference = !isRValRef; |
| 3620 | ICS.UserDefined.After.BindsToFunctionLvalue = T2->isFunctionType(); |
| 3621 | ICS.UserDefined.After.BindsToRvalue = true; |
| 3622 | ICS.UserDefined.After.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
| 3623 | ICS.UserDefined.After.ObjCLifetimeConversionBinding = false; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3624 | } |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 3625 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3626 | return ICS; |
| 3627 | } |
| 3628 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3629 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 3630 | /// ToType from the expression From. Return the implicit conversion |
| 3631 | /// sequence required to pass this argument, which may be a bad |
| 3632 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3633 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | 74e386e | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 3634 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3635 | static ImplicitConversionSequence |
| 3636 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3637 | bool SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3638 | bool InOverloadResolution, |
| 3639 | bool AllowObjCWritebackConversion) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3640 | if (ToType->isReferenceType()) |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3641 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3642 | /*FIXME:*/From->getLocStart(), |
| 3643 | SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 3644 | /*AllowExplicit=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3645 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3646 | return TryImplicitConversion(S, From, ToType, |
| 3647 | SuppressUserConversions, |
| 3648 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3649 | InOverloadResolution, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3650 | /*CStyle=*/false, |
| 3651 | AllowObjCWritebackConversion); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 3654 | static bool TryCopyInitialization(const CanQualType FromQTy, |
| 3655 | const CanQualType ToQTy, |
| 3656 | Sema &S, |
| 3657 | SourceLocation Loc, |
| 3658 | ExprValueKind FromVK) { |
| 3659 | OpaqueValueExpr TmpExpr(Loc, FromQTy, FromVK); |
| 3660 | ImplicitConversionSequence ICS = |
| 3661 | TryCopyInitialization(S, &TmpExpr, ToQTy, true, true, false); |
| 3662 | |
| 3663 | return !ICS.isBad(); |
| 3664 | } |
| 3665 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3666 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 3667 | /// parameter of the given member function (@c Method) from the |
| 3668 | /// expression @p From. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3669 | static ImplicitConversionSequence |
| 3670 | TryObjectArgumentInitialization(Sema &S, QualType OrigFromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3671 | Expr::Classification FromClassification, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3672 | CXXMethodDecl *Method, |
| 3673 | CXXRecordDecl *ActingContext) { |
| 3674 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 3675 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 3676 | // const volatile object. |
| 3677 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 3678 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3679 | QualType ImplicitParamType = S.Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3680 | |
| 3681 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 3682 | // to exit early. |
| 3683 | ImplicitConversionSequence ICS; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3684 | |
| 3685 | // We need to have an object of class type. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 3686 | QualType FromType = OrigFromType; |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3687 | if (const PointerType *PT = FromType->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3688 | FromType = PT->getPointeeType(); |
| 3689 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3690 | // When we had a pointer, it's implicitly dereferenced, so we |
| 3691 | // better have an lvalue. |
| 3692 | assert(FromClassification.isLValue()); |
| 3693 | } |
| 3694 | |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3695 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3696 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3697 | // C++0x [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3698 | // For non-static member functions, the type of the implicit object |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3699 | // parameter is |
| 3700 | // |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3701 | // - "lvalue reference to cv X" for functions declared without a |
| 3702 | // ref-qualifier or with the & ref-qualifier |
| 3703 | // - "rvalue reference to cv X" for functions declared with the && |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3704 | // ref-qualifier |
| 3705 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3706 | // 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] | 3707 | // cv-qualification on the member function declaration. |
| 3708 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3709 | // However, when finding an implicit conversion sequence for the argument, we |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3710 | // are not allowed to create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3711 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 3712 | // reference binding here, that allows class rvalues to bind to |
| 3713 | // non-constant references. |
| 3714 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3715 | // First check the qualifiers. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3716 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3717 | if (ImplicitParamType.getCVRQualifiers() |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3718 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3719 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3720 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
| 3721 | OrigFromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3722 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3723 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3724 | |
| 3725 | // Check that we have either the same type or a derived type. It |
| 3726 | // affects the conversion rank. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3727 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3728 | ImplicitConversionKind SecondKind; |
| 3729 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 3730 | SecondKind = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3731 | } else if (S.IsDerivedFrom(FromType, ClassType)) |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3732 | SecondKind = ICK_Derived_To_Base; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3733 | else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3734 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 3735 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3736 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3737 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3738 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3739 | // Check the ref-qualifier. |
| 3740 | switch (Method->getRefQualifier()) { |
| 3741 | case RQ_None: |
| 3742 | // Do nothing; we don't care about lvalueness or rvalueness. |
| 3743 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3744 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3745 | case RQ_LValue: |
| 3746 | if (!FromClassification.isLValue() && Quals != Qualifiers::Const) { |
| 3747 | // non-const lvalue reference cannot bind to an rvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3748 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3749 | ImplicitParamType); |
| 3750 | return ICS; |
| 3751 | } |
| 3752 | break; |
| 3753 | |
| 3754 | case RQ_RValue: |
| 3755 | if (!FromClassification.isRValue()) { |
| 3756 | // rvalue reference cannot bind to an lvalue |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3757 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, FromType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3758 | ImplicitParamType); |
| 3759 | return ICS; |
| 3760 | } |
| 3761 | break; |
| 3762 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3763 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3764 | // Success. Mark this as a reference binding. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3765 | ICS.setStandard(); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3766 | ICS.Standard.setAsIdentityConversion(); |
| 3767 | ICS.Standard.Second = SecondKind; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3768 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3769 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3770 | ICS.Standard.ReferenceBinding = true; |
| 3771 | ICS.Standard.DirectBinding = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3772 | ICS.Standard.IsLvalueReference = Method->getRefQualifier() != RQ_RValue; |
Douglas Gregor | 440a483 | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3773 | ICS.Standard.BindsToFunctionLvalue = false; |
Douglas Gregor | fcab48b | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3774 | ICS.Standard.BindsToRvalue = FromClassification.isRValue(); |
| 3775 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier |
| 3776 | = (Method->getRefQualifier() == RQ_None); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3777 | return ICS; |
| 3778 | } |
| 3779 | |
| 3780 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 3781 | /// the implicit object parameter for the given Method with the given |
| 3782 | /// expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3783 | ExprResult |
| 3784 | Sema::PerformObjectArgumentInitialization(Expr *From, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3785 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3786 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3787 | CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3788 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3789 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3790 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3791 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3792 | Expr::Classification FromClassification; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3793 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3794 | FromRecordType = PT->getPointeeType(); |
| 3795 | DestType = Method->getThisType(Context); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3796 | FromClassification = Expr::Classification::makeSimpleLValue(); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3797 | } else { |
| 3798 | FromRecordType = From->getType(); |
| 3799 | DestType = ImplicitParamRecordType; |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3800 | FromClassification = From->Classify(Context); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3801 | } |
| 3802 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3803 | // Note that we always use the true parent context when performing |
| 3804 | // the actual argument initialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3805 | ImplicitConversionSequence ICS |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3806 | = TryObjectArgumentInitialization(*this, From->getType(), FromClassification, |
| 3807 | Method, Method->getParent()); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 3808 | if (ICS.isBad()) { |
| 3809 | if (ICS.Bad.Kind == BadConversionSequence::bad_qualifiers) { |
| 3810 | Qualifiers FromQs = FromRecordType.getQualifiers(); |
| 3811 | Qualifiers ToQs = DestType.getQualifiers(); |
| 3812 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 3813 | if (CVR) { |
| 3814 | Diag(From->getSourceRange().getBegin(), |
| 3815 | diag::err_member_function_call_bad_cvr) |
| 3816 | << Method->getDeclName() << FromRecordType << (CVR - 1) |
| 3817 | << From->getSourceRange(); |
| 3818 | Diag(Method->getLocation(), diag::note_previous_decl) |
| 3819 | << Method->getDeclName(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3820 | return ExprError(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 3821 | } |
| 3822 | } |
| 3823 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3824 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3825 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3826 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Argyrios Kyrtzidis | 64ccf24 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 3827 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3828 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3829 | if (ICS.Standard.Second == ICK_Derived_To_Base) { |
| 3830 | ExprResult FromRes = |
| 3831 | PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
| 3832 | if (FromRes.isInvalid()) |
| 3833 | return ExprError(); |
| 3834 | From = FromRes.take(); |
| 3835 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3836 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3837 | if (!Context.hasSameType(From->getType(), DestType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3838 | From = ImpCastExprToType(From, DestType, CK_NoOp, |
| 3839 | From->getType()->isPointerType() ? VK_RValue : VK_LValue).take(); |
| 3840 | return Owned(From); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3843 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 3844 | /// expression From to bool (C++0x [conv]p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3845 | static ImplicitConversionSequence |
| 3846 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
Douglas Gregor | c6dfe19 | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 3847 | // FIXME: This is pretty broken. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3848 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 3849 | // FIXME: Are these flags correct? |
| 3850 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3851 | /*AllowExplicit=*/true, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3852 | /*InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3853 | /*CStyle=*/false, |
| 3854 | /*AllowObjCWritebackConversion=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3855 | } |
| 3856 | |
| 3857 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 3858 | /// of the expression From to bool (C++0x [conv]p3). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3859 | ExprResult Sema::PerformContextuallyConvertToBool(Expr *From) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3860 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3861 | if (!ICS.isBad()) |
| 3862 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3863 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3864 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3865 | return Diag(From->getSourceRange().getBegin(), |
| 3866 | diag::err_typecheck_bool_condition) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3867 | << From->getType() << From->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3868 | return ExprError(); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3869 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3870 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 3871 | /// dropPointerConversions - If the given standard conversion sequence |
| 3872 | /// involves any pointer conversions, remove them. This may change |
| 3873 | /// the result type of the conversion sequence. |
| 3874 | static void dropPointerConversion(StandardConversionSequence &SCS) { |
| 3875 | if (SCS.Second == ICK_Pointer_Conversion) { |
| 3876 | SCS.Second = ICK_Identity; |
| 3877 | SCS.Third = ICK_Identity; |
| 3878 | SCS.ToTypePtrs[2] = SCS.ToTypePtrs[1] = SCS.ToTypePtrs[0]; |
| 3879 | } |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3880 | } |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3881 | |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 3882 | /// TryContextuallyConvertToObjCPointer - Attempt to contextually |
| 3883 | /// convert the expression From to an Objective-C pointer type. |
| 3884 | static ImplicitConversionSequence |
| 3885 | TryContextuallyConvertToObjCPointer(Sema &S, Expr *From) { |
| 3886 | // Do an implicit conversion to 'id'. |
| 3887 | QualType Ty = S.Context.getObjCIdType(); |
| 3888 | ImplicitConversionSequence ICS |
| 3889 | = TryImplicitConversion(S, From, Ty, |
| 3890 | // FIXME: Are these flags correct? |
| 3891 | /*SuppressUserConversions=*/false, |
| 3892 | /*AllowExplicit=*/true, |
| 3893 | /*InOverloadResolution=*/false, |
| 3894 | /*CStyle=*/false, |
| 3895 | /*AllowObjCWritebackConversion=*/false); |
| 3896 | |
| 3897 | // Strip off any final conversions to 'id'. |
| 3898 | switch (ICS.getKind()) { |
| 3899 | case ImplicitConversionSequence::BadConversion: |
| 3900 | case ImplicitConversionSequence::AmbiguousConversion: |
| 3901 | case ImplicitConversionSequence::EllipsisConversion: |
| 3902 | break; |
| 3903 | |
| 3904 | case ImplicitConversionSequence::UserDefinedConversion: |
| 3905 | dropPointerConversion(ICS.UserDefined.After); |
| 3906 | break; |
| 3907 | |
| 3908 | case ImplicitConversionSequence::StandardConversion: |
| 3909 | dropPointerConversion(ICS.Standard); |
| 3910 | break; |
| 3911 | } |
| 3912 | |
| 3913 | return ICS; |
| 3914 | } |
| 3915 | |
| 3916 | /// PerformContextuallyConvertToObjCPointer - Perform a contextual |
| 3917 | /// conversion of the expression From to an Objective-C pointer type. |
| 3918 | ExprResult Sema::PerformContextuallyConvertToObjCPointer(Expr *From) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3919 | QualType Ty = Context.getObjCIdType(); |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 3920 | ImplicitConversionSequence ICS = |
| 3921 | TryContextuallyConvertToObjCPointer(*this, From); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3922 | if (!ICS.isBad()) |
| 3923 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3924 | return ExprError(); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3925 | } |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3926 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3927 | /// \brief Attempt to convert the given expression to an integral or |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3928 | /// enumeration type. |
| 3929 | /// |
| 3930 | /// This routine will attempt to convert an expression of class type to an |
| 3931 | /// integral or enumeration type, if that class type only has a single |
| 3932 | /// conversion to an integral or enumeration type. |
| 3933 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3934 | /// \param Loc The source location of the construct that requires the |
| 3935 | /// conversion. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3936 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3937 | /// \param FromE The expression we're converting from. |
| 3938 | /// |
| 3939 | /// \param NotIntDiag The diagnostic to be emitted if the expression does not |
| 3940 | /// have integral or enumeration type. |
| 3941 | /// |
| 3942 | /// \param IncompleteDiag The diagnostic to be emitted if the expression has |
| 3943 | /// incomplete class type. |
| 3944 | /// |
| 3945 | /// \param ExplicitConvDiag The diagnostic to be emitted if we're calling an |
| 3946 | /// explicit conversion function (because no implicit conversion functions |
| 3947 | /// were available). This is a recovery mode. |
| 3948 | /// |
| 3949 | /// \param ExplicitConvNote The note to be emitted with \p ExplicitConvDiag, |
| 3950 | /// showing which conversion was picked. |
| 3951 | /// |
| 3952 | /// \param AmbigDiag The diagnostic to be emitted if there is more than one |
| 3953 | /// conversion function that could convert to integral or enumeration type. |
| 3954 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3955 | /// \param AmbigNote The note to be emitted with \p AmbigDiag for each |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3956 | /// usable conversion function. |
| 3957 | /// |
| 3958 | /// \param ConvDiag The diagnostic to be emitted if we are calling a conversion |
| 3959 | /// function, which may be an extension in this case. |
| 3960 | /// |
| 3961 | /// \returns The expression, converted to an integral or enumeration type if |
| 3962 | /// successful. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3963 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3964 | Sema::ConvertToIntegralOrEnumerationType(SourceLocation Loc, Expr *From, |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3965 | const PartialDiagnostic &NotIntDiag, |
| 3966 | const PartialDiagnostic &IncompleteDiag, |
| 3967 | const PartialDiagnostic &ExplicitConvDiag, |
| 3968 | const PartialDiagnostic &ExplicitConvNote, |
| 3969 | const PartialDiagnostic &AmbigDiag, |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3970 | const PartialDiagnostic &AmbigNote, |
| 3971 | const PartialDiagnostic &ConvDiag) { |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3972 | // We can't perform any more checking for type-dependent expressions. |
| 3973 | if (From->isTypeDependent()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3974 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3975 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3976 | // If the expression already has integral or enumeration type, we're golden. |
| 3977 | QualType T = From->getType(); |
| 3978 | if (T->isIntegralOrEnumerationType()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3979 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3980 | |
| 3981 | // FIXME: Check for missing '()' if T is a function type? |
| 3982 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3983 | // 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] | 3984 | // expression of integral or enumeration type. |
| 3985 | const RecordType *RecordTy = T->getAs<RecordType>(); |
| 3986 | if (!RecordTy || !getLangOptions().CPlusPlus) { |
| 3987 | Diag(Loc, NotIntDiag) |
| 3988 | << T << From->getSourceRange(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3989 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3990 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3991 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3992 | // We must have a complete class type. |
| 3993 | if (RequireCompleteType(Loc, T, IncompleteDiag)) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3994 | return Owned(From); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3995 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3996 | // Look for a conversion to an integral or enumeration type. |
| 3997 | UnresolvedSet<4> ViableConversions; |
| 3998 | UnresolvedSet<4> ExplicitConversions; |
| 3999 | const UnresolvedSetImpl *Conversions |
| 4000 | = cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4001 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4002 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4003 | E = Conversions->end(); |
| 4004 | I != E; |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4005 | ++I) { |
| 4006 | if (CXXConversionDecl *Conversion |
| 4007 | = dyn_cast<CXXConversionDecl>((*I)->getUnderlyingDecl())) |
| 4008 | if (Conversion->getConversionType().getNonReferenceType() |
| 4009 | ->isIntegralOrEnumerationType()) { |
| 4010 | if (Conversion->isExplicit()) |
| 4011 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
| 4012 | else |
| 4013 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
| 4014 | } |
| 4015 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4016 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4017 | switch (ViableConversions.size()) { |
| 4018 | case 0: |
| 4019 | if (ExplicitConversions.size() == 1) { |
| 4020 | DeclAccessPair Found = ExplicitConversions[0]; |
| 4021 | CXXConversionDecl *Conversion |
| 4022 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4023 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4024 | // The user probably meant to invoke the given explicit |
| 4025 | // conversion; use it. |
| 4026 | QualType ConvTy |
| 4027 | = Conversion->getConversionType().getNonReferenceType(); |
| 4028 | std::string TypeStr; |
| 4029 | ConvTy.getAsStringInternal(TypeStr, Context.PrintingPolicy); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4030 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4031 | Diag(Loc, ExplicitConvDiag) |
| 4032 | << T << ConvTy |
| 4033 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 4034 | "static_cast<" + TypeStr + ">(") |
| 4035 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(From->getLocEnd()), |
| 4036 | ")"); |
| 4037 | Diag(Conversion->getLocation(), ExplicitConvNote) |
| 4038 | << ConvTy->isEnumeralType() << ConvTy; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4039 | |
| 4040 | // 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] | 4041 | // explicit conversion function. |
| 4042 | if (isSFINAEContext()) |
| 4043 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4044 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4045 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 4046 | ExprResult Result = BuildCXXMemberCallExpr(From, Found, Conversion); |
| 4047 | if (Result.isInvalid()) |
| 4048 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4049 | |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 4050 | From = Result.get(); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4051 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4052 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4053 | // We'll complain below about a non-integral condition type. |
| 4054 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4055 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4056 | case 1: { |
| 4057 | // Apply this conversion. |
| 4058 | DeclAccessPair Found = ViableConversions[0]; |
| 4059 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4060 | |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 4061 | CXXConversionDecl *Conversion |
| 4062 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 4063 | QualType ConvTy |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4064 | = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 4065 | if (ConvDiag.getDiagID()) { |
| 4066 | if (isSFINAEContext()) |
| 4067 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4068 | |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 4069 | Diag(Loc, ConvDiag) |
| 4070 | << T << ConvTy->isEnumeralType() << ConvTy << From->getSourceRange(); |
| 4071 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4072 | |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 4073 | ExprResult Result = BuildCXXMemberCallExpr(From, Found, |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4074 | cast<CXXConversionDecl>(Found->getUnderlyingDecl())); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 4075 | if (Result.isInvalid()) |
| 4076 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4077 | |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 4078 | From = Result.get(); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4079 | break; |
| 4080 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4081 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4082 | default: |
| 4083 | Diag(Loc, AmbigDiag) |
| 4084 | << T << From->getSourceRange(); |
| 4085 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 4086 | CXXConversionDecl *Conv |
| 4087 | = cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 4088 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 4089 | Diag(Conv->getLocation(), AmbigNote) |
| 4090 | << ConvTy->isEnumeralType() << ConvTy; |
| 4091 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4092 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4093 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4094 | |
Douglas Gregor | acb0bd8 | 2010-06-29 23:25:20 +0000 | [diff] [blame] | 4095 | if (!From->getType()->isIntegralOrEnumerationType()) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4096 | Diag(Loc, NotIntDiag) |
| 4097 | << From->getType() << From->getSourceRange(); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4098 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4099 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 4100 | } |
| 4101 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4102 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4103 | /// candidate functions, using the given function call arguments. If |
| 4104 | /// @p SuppressUserConversions, then don't allow user-defined |
| 4105 | /// conversions via constructors or conversion operators. |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4106 | /// |
| 4107 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 4108 | /// based on an incomplete set of function arguments. This feature is used by |
| 4109 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4110 | void |
| 4111 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4112 | DeclAccessPair FoundDecl, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4113 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4114 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 4115 | bool SuppressUserConversions, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4116 | bool PartialOverloading) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4117 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4118 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4119 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4120 | assert(!Function->getDescribedFunctionTemplate() && |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4121 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4122 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4123 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4124 | if (!isa<CXXConstructorDecl>(Method)) { |
| 4125 | // If we get here, it's because we're calling a member function |
| 4126 | // that is named without a member access expression (e.g., |
| 4127 | // "this->f") that was either written explicitly or created |
| 4128 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4129 | // function, e.g., X::f(). We use an empty type for the implied |
| 4130 | // object argument (C++ [over.call.func]p3), and the acting context |
| 4131 | // is irrelevant. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4132 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4133 | QualType(), Expr::Classification::makeSimpleLValue(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4134 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4135 | SuppressUserConversions); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4136 | return; |
| 4137 | } |
| 4138 | // We treat a constructor like a non-member function, since its object |
| 4139 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4140 | } |
| 4141 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 4142 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4143 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 4144 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4145 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4146 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4147 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 4148 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 4149 | // C++ [class.copy]p3: |
| 4150 | // A member function template is never instantiated to perform the copy |
| 4151 | // of a class object to an object of its class type. |
| 4152 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4153 | if (NumArgs == 1 && |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4154 | Constructor->isSpecializationCopyingObject() && |
Douglas Gregor | 1211606 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 4155 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 4156 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 4157 | return; |
| 4158 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4159 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4160 | // Add this candidate |
| 4161 | CandidateSet.push_back(OverloadCandidate()); |
| 4162 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4163 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4164 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4165 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4166 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4167 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4168 | Candidate.ExplicitCallArguments = NumArgs; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4169 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4170 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4171 | |
| 4172 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 4173 | // parameters is viable only if it has an ellipsis in its parameter |
| 4174 | // list (8.3.5). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4175 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 4176 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4177 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4178 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4179 | return; |
| 4180 | } |
| 4181 | |
| 4182 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 4183 | // is viable only if the (m+1)st parameter has a default argument |
| 4184 | // (8.3.6). For the purposes of overload resolution, the |
| 4185 | // parameter list is truncated on the right, so that there are |
| 4186 | // exactly m parameters. |
| 4187 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4188 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4189 | // Not enough arguments. |
| 4190 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4191 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4192 | return; |
| 4193 | } |
| 4194 | |
| 4195 | // Determine the implicit conversion sequences for each of the |
| 4196 | // arguments. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4197 | Candidate.Conversions.resize(NumArgs); |
| 4198 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 4199 | if (ArgIdx < NumArgsInProto) { |
| 4200 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 4201 | // exist for each argument an implicit conversion sequence |
| 4202 | // (13.3.3.1) that converts that argument to the corresponding |
| 4203 | // parameter of F. |
| 4204 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4205 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4206 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4207 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4208 | /*InOverloadResolution=*/true, |
| 4209 | /*AllowObjCWritebackConversion=*/ |
| 4210 | getLangOptions().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4211 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 4212 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4213 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4214 | break; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4215 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4216 | } else { |
| 4217 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 4218 | // argument for which there is no corresponding parameter is |
| 4219 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4220 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4221 | } |
| 4222 | } |
| 4223 | } |
| 4224 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4225 | /// \brief Add all of the function declarations in the given function set to |
| 4226 | /// the overload canddiate set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4227 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4228 | Expr **Args, unsigned NumArgs, |
| 4229 | OverloadCandidateSet& CandidateSet, |
| 4230 | bool SuppressUserConversions) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4231 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4232 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 4233 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4234 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4235 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4236 | cast<CXXMethodDecl>(FD)->getParent(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4237 | Args[0]->getType(), Args[0]->Classify(Context), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4238 | Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4239 | CandidateSet, SuppressUserConversions); |
| 4240 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4241 | AddOverloadCandidate(FD, F.getPair(), Args, NumArgs, CandidateSet, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4242 | SuppressUserConversions); |
| 4243 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4244 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4245 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 4246 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4247 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4248 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4249 | /*FIXME: explicit args */ 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4250 | Args[0]->getType(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4251 | Args[0]->Classify(Context), |
| 4252 | Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4253 | CandidateSet, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4254 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4255 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4256 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4257 | /*FIXME: explicit args */ 0, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4258 | Args, NumArgs, CandidateSet, |
| 4259 | SuppressUserConversions); |
| 4260 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4261 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4262 | } |
| 4263 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4264 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 4265 | /// method) as a method candidate to the given overload set. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4266 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4267 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4268 | Expr::Classification ObjectClassification, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4269 | Expr **Args, unsigned NumArgs, |
| 4270 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4271 | bool SuppressUserConversions) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4272 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4273 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4274 | |
| 4275 | if (isa<UsingShadowDecl>(Decl)) |
| 4276 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4277 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4278 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 4279 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 4280 | "Expected a member function template"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4281 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 4282 | /*ExplicitArgs*/ 0, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4283 | ObjectType, ObjectClassification, Args, NumArgs, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4284 | CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4285 | SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4286 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4287 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4288 | ObjectType, ObjectClassification, Args, NumArgs, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4289 | CandidateSet, SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4290 | } |
| 4291 | } |
| 4292 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4293 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 4294 | /// of candidate functions, using the given function call arguments |
| 4295 | /// and the object argument (@c Object). For example, in a call |
| 4296 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 4297 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 4298 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4299 | /// operators. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4300 | void |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4301 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4302 | CXXRecordDecl *ActingContext, QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4303 | Expr::Classification ObjectClassification, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4304 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4305 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4306 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4307 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4308 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4309 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4310 | assert(!isa<CXXConstructorDecl>(Method) && |
| 4311 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4312 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4313 | if (!CandidateSet.isNewCandidate(Method)) |
| 4314 | return; |
| 4315 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4316 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4317 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4318 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4319 | // Add this candidate |
| 4320 | CandidateSet.push_back(OverloadCandidate()); |
| 4321 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4322 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4323 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4324 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4325 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4326 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4327 | |
| 4328 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4329 | |
| 4330 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 4331 | // parameters is viable only if it has an ellipsis in its parameter |
| 4332 | // list (8.3.5). |
| 4333 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 4334 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4335 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4336 | return; |
| 4337 | } |
| 4338 | |
| 4339 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 4340 | // is viable only if the (m+1)st parameter has a default argument |
| 4341 | // (8.3.6). For the purposes of overload resolution, the |
| 4342 | // parameter list is truncated on the right, so that there are |
| 4343 | // exactly m parameters. |
| 4344 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 4345 | if (NumArgs < MinRequiredArgs) { |
| 4346 | // Not enough arguments. |
| 4347 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4348 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4349 | return; |
| 4350 | } |
| 4351 | |
| 4352 | Candidate.Viable = true; |
| 4353 | Candidate.Conversions.resize(NumArgs + 1); |
| 4354 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4355 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4356 | // The implicit object argument is ignored. |
| 4357 | Candidate.IgnoreObjectArgument = true; |
| 4358 | else { |
| 4359 | // Determine the implicit conversion sequence for the object |
| 4360 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4361 | Candidate.Conversions[0] |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4362 | = TryObjectArgumentInitialization(*this, ObjectType, ObjectClassification, |
| 4363 | Method, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4364 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4365 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4366 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4367 | return; |
| 4368 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4369 | } |
| 4370 | |
| 4371 | // Determine the implicit conversion sequences for each of the |
| 4372 | // arguments. |
| 4373 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 4374 | if (ArgIdx < NumArgsInProto) { |
| 4375 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 4376 | // exist for each argument an implicit conversion sequence |
| 4377 | // (13.3.3.1) that converts that argument to the corresponding |
| 4378 | // parameter of F. |
| 4379 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4380 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4381 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4382 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4383 | /*InOverloadResolution=*/true, |
| 4384 | /*AllowObjCWritebackConversion=*/ |
| 4385 | getLangOptions().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4386 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4387 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4388 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4389 | break; |
| 4390 | } |
| 4391 | } else { |
| 4392 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 4393 | // argument for which there is no corresponding parameter is |
| 4394 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4395 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4396 | } |
| 4397 | } |
| 4398 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4399 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4400 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 4401 | /// set, using template argument deduction to produce an appropriate member |
| 4402 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4403 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4404 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4405 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4406 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4407 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4408 | QualType ObjectType, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4409 | Expr::Classification ObjectClassification, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4410 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4411 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4412 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4413 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 4414 | return; |
| 4415 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4416 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4417 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4418 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4419 | // 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] | 4420 | // candidate functions in the usual way.113) A given name can refer to one |
| 4421 | // or more function templates and also to a set of overloaded non-template |
| 4422 | // functions. In such a case, the candidate functions generated from each |
| 4423 | // function template are combined with the set of non-template candidate |
| 4424 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4425 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4426 | FunctionDecl *Specialization = 0; |
| 4427 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4428 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4429 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4430 | CandidateSet.push_back(OverloadCandidate()); |
| 4431 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 4432 | Candidate.FoundDecl = FoundDecl; |
| 4433 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 4434 | Candidate.Viable = false; |
| 4435 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 4436 | Candidate.IsSurrogate = false; |
| 4437 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4438 | Candidate.ExplicitCallArguments = NumArgs; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4439 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4440 | Info); |
| 4441 | return; |
| 4442 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4443 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4444 | // Add the function template specialization produced by template argument |
| 4445 | // deduction as a candidate. |
| 4446 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4447 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4448 | "Specialization is not a member function?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4449 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4450 | ActingContext, ObjectType, ObjectClassification, |
| 4451 | Args, NumArgs, CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4452 | } |
| 4453 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4454 | /// \brief Add a C++ function template specialization as a candidate |
| 4455 | /// in the candidate set, using template argument deduction to produce |
| 4456 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4457 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4458 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4459 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4460 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4461 | Expr **Args, unsigned NumArgs, |
| 4462 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4463 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4464 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 4465 | return; |
| 4466 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4467 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4468 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4469 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4470 | // 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] | 4471 | // candidate functions in the usual way.113) A given name can refer to one |
| 4472 | // or more function templates and also to a set of overloaded non-template |
| 4473 | // functions. In such a case, the candidate functions generated from each |
| 4474 | // function template are combined with the set of non-template candidate |
| 4475 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4476 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4477 | FunctionDecl *Specialization = 0; |
| 4478 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4479 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4480 | Args, NumArgs, Specialization, Info)) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4481 | CandidateSet.push_back(OverloadCandidate()); |
| 4482 | OverloadCandidate &Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4483 | Candidate.FoundDecl = FoundDecl; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4484 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 4485 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4486 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4487 | Candidate.IsSurrogate = false; |
| 4488 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4489 | Candidate.ExplicitCallArguments = NumArgs; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4490 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4491 | Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4492 | return; |
| 4493 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4494 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4495 | // Add the function template specialization produced by template argument |
| 4496 | // deduction as a candidate. |
| 4497 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4498 | AddOverloadCandidate(Specialization, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4499 | SuppressUserConversions); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4500 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4501 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4502 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4503 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4504 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4505 | /// 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] | 4506 | /// (which may or may not be the same type as the type that the |
| 4507 | /// conversion function produces). |
| 4508 | void |
| 4509 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4510 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4511 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4512 | Expr *From, QualType ToType, |
| 4513 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4514 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 4515 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4516 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4517 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 4518 | return; |
| 4519 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4520 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4521 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4522 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4523 | // Add this candidate |
| 4524 | CandidateSet.push_back(OverloadCandidate()); |
| 4525 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4526 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4527 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4528 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4529 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4530 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4531 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 4532 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4533 | Candidate.Viable = true; |
| 4534 | Candidate.Conversions.resize(1); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4535 | Candidate.ExplicitCallArguments = 1; |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4536 | |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 4537 | // C++ [over.match.funcs]p4: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4538 | // For conversion functions, the function is considered to be a member of |
| 4539 | // the class of the implicit implied object argument for the purpose of |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 4540 | // defining the type of the implicit object parameter. |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4541 | // |
| 4542 | // Determine the implicit conversion sequence for the implicit |
| 4543 | // object parameter. |
| 4544 | QualType ImplicitParamType = From->getType(); |
| 4545 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
| 4546 | ImplicitParamType = FromPtrType->getPointeeType(); |
| 4547 | CXXRecordDecl *ConversionContext |
| 4548 | = cast<CXXRecordDecl>(ImplicitParamType->getAs<RecordType>()->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4549 | |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4550 | Candidate.Conversions[0] |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4551 | = TryObjectArgumentInitialization(*this, From->getType(), |
| 4552 | From->Classify(Context), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4553 | Conversion, ConversionContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4554 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4555 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4556 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4557 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4558 | return; |
| 4559 | } |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4560 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4561 | // 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] | 4562 | // derived to base as such conversions are given Conversion Rank. They only |
| 4563 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 4564 | QualType FromCanon |
| 4565 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 4566 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 4567 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 4568 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4569 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 4570 | return; |
| 4571 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4572 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4573 | // To determine what the conversion from the result of calling the |
| 4574 | // conversion function to the type we're eventually trying to |
| 4575 | // convert to (ToType), we need to synthesize a call to the |
| 4576 | // conversion function and attempt copy initialization from it. This |
| 4577 | // makes sure that we get the right semantics with respect to |
| 4578 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 4579 | // call on the stack and we don't need its arguments to be |
| 4580 | // well-formed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4581 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4582 | VK_LValue, From->getLocStart()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4583 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 4584 | Context.getPointerType(Conversion->getType()), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4585 | CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4586 | &ConversionRef, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4587 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 4588 | QualType ConversionType = Conversion->getConversionType(); |
| 4589 | if (RequireCompleteType(From->getLocStart(), ConversionType, 0)) { |
Douglas Gregor | 7d14d38 | 2010-11-13 19:36:57 +0000 | [diff] [blame] | 4590 | Candidate.Viable = false; |
| 4591 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 4592 | return; |
| 4593 | } |
| 4594 | |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 4595 | ExprValueKind VK = Expr::getValueKindForType(ConversionType); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4596 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4597 | // 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] | 4598 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 4599 | // allocator). |
Richard Smith | 87c1f1f | 2011-07-13 22:53:21 +0000 | [diff] [blame] | 4600 | QualType CallResultType = ConversionType.getNonLValueExprType(Context); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4601 | CallExpr Call(Context, &ConversionFn, 0, 0, CallResultType, VK, |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 4602 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4603 | ImplicitConversionSequence ICS = |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4604 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 4605 | /*SuppressUserConversions=*/true, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4606 | /*InOverloadResolution=*/false, |
| 4607 | /*AllowObjCWritebackConversion=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4608 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4609 | switch (ICS.getKind()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4610 | case ImplicitConversionSequence::StandardConversion: |
| 4611 | Candidate.FinalConversion = ICS.Standard; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4612 | |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 4613 | // C++ [over.ics.user]p3: |
| 4614 | // If the user-defined conversion is specified by a specialization of a |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4615 | // conversion function template, the second standard conversion sequence |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 4616 | // shall have exact match rank. |
| 4617 | if (Conversion->getPrimaryTemplate() && |
| 4618 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 4619 | Candidate.Viable = false; |
| 4620 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 4621 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4622 | |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4623 | // C++0x [dcl.init.ref]p5: |
| 4624 | // In the second case, if the reference is an rvalue reference and |
| 4625 | // the second standard conversion sequence of the user-defined |
| 4626 | // conversion sequence includes an lvalue-to-rvalue conversion, the |
| 4627 | // program is ill-formed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4628 | if (ToType->isRValueReferenceType() && |
Douglas Gregor | 2ad746a | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4629 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 4630 | Candidate.Viable = false; |
| 4631 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 4632 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4633 | break; |
| 4634 | |
| 4635 | case ImplicitConversionSequence::BadConversion: |
| 4636 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4637 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4638 | break; |
| 4639 | |
| 4640 | default: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4641 | assert(false && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4642 | "Can only end up with a standard conversion sequence or failure"); |
| 4643 | } |
| 4644 | } |
| 4645 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4646 | /// \brief Adds a conversion function template specialization |
| 4647 | /// candidate to the overload set, using template argument deduction |
| 4648 | /// to deduce the template arguments of the conversion function |
| 4649 | /// template from the type that we are converting to (C++ |
| 4650 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4651 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4652 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4653 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4654 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4655 | Expr *From, QualType ToType, |
| 4656 | OverloadCandidateSet &CandidateSet) { |
| 4657 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 4658 | "Only conversion function templates permitted here"); |
| 4659 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4660 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 4661 | return; |
| 4662 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4663 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4664 | CXXConversionDecl *Specialization = 0; |
| 4665 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4666 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4667 | Specialization, Info)) { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4668 | CandidateSet.push_back(OverloadCandidate()); |
| 4669 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 4670 | Candidate.FoundDecl = FoundDecl; |
| 4671 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 4672 | Candidate.Viable = false; |
| 4673 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 4674 | Candidate.IsSurrogate = false; |
| 4675 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4676 | Candidate.ExplicitCallArguments = 1; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4677 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4678 | Info); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4679 | return; |
| 4680 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4681 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4682 | // Add the conversion function template specialization produced by |
| 4683 | // template argument deduction as a candidate. |
| 4684 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4685 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4686 | CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4687 | } |
| 4688 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4689 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 4690 | /// converts the given @c Object to a function pointer via the |
| 4691 | /// conversion function @c Conversion, and then attempts to call it |
| 4692 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 4693 | /// the type of function that we'll eventually be calling. |
| 4694 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4695 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4696 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4697 | const FunctionProtoType *Proto, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4698 | Expr *Object, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4699 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4700 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4701 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 4702 | return; |
| 4703 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4704 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4705 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4706 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4707 | CandidateSet.push_back(OverloadCandidate()); |
| 4708 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4709 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4710 | Candidate.Function = 0; |
| 4711 | Candidate.Surrogate = Conversion; |
| 4712 | Candidate.Viable = true; |
| 4713 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4714 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4715 | Candidate.Conversions.resize(NumArgs + 1); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4716 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4717 | |
| 4718 | // Determine the implicit conversion sequence for the implicit |
| 4719 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4720 | ImplicitConversionSequence ObjectInit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4721 | = TryObjectArgumentInitialization(*this, Object->getType(), |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4722 | Object->Classify(Context), |
| 4723 | Conversion, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4724 | if (ObjectInit.isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4725 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4726 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4727 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4728 | return; |
| 4729 | } |
| 4730 | |
| 4731 | // The first conversion is actually a user-defined conversion whose |
| 4732 | // first conversion is ObjectInit's standard conversion (which is |
| 4733 | // effectively a reference binding). Record it as such. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4734 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4735 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 4736 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4737 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4738 | Candidate.Conversions[0].UserDefined.FoundConversionFunction |
Douglas Gregor | 83eecbe | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 4739 | = FoundDecl.getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4740 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4741 | = Candidate.Conversions[0].UserDefined.Before; |
| 4742 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 4743 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4744 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4745 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4746 | |
| 4747 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 4748 | // parameters is viable only if it has an ellipsis in its parameter |
| 4749 | // list (8.3.5). |
| 4750 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 4751 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4752 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4753 | return; |
| 4754 | } |
| 4755 | |
| 4756 | // Function types don't have any default arguments, so just check if |
| 4757 | // we have enough arguments. |
| 4758 | if (NumArgs < NumArgsInProto) { |
| 4759 | // Not enough arguments. |
| 4760 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4761 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4762 | return; |
| 4763 | } |
| 4764 | |
| 4765 | // Determine the implicit conversion sequences for each of the |
| 4766 | // arguments. |
| 4767 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 4768 | if (ArgIdx < NumArgsInProto) { |
| 4769 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 4770 | // exist for each argument an implicit conversion sequence |
| 4771 | // (13.3.3.1) that converts that argument to the corresponding |
| 4772 | // parameter of F. |
| 4773 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4774 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4775 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 4776 | /*SuppressUserConversions=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4777 | /*InOverloadResolution=*/false, |
| 4778 | /*AllowObjCWritebackConversion=*/ |
| 4779 | getLangOptions().ObjCAutoRefCount); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4780 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4781 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4782 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4783 | break; |
| 4784 | } |
| 4785 | } else { |
| 4786 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 4787 | // argument for which there is no corresponding parameter is |
| 4788 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4789 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4790 | } |
| 4791 | } |
| 4792 | } |
| 4793 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4794 | /// \brief Add overload candidates for overloaded operators that are |
| 4795 | /// member functions. |
| 4796 | /// |
| 4797 | /// Add the overloaded operator candidates that are member functions |
| 4798 | /// for the operator Op that was used in an operator expression such |
| 4799 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 4800 | /// CandidateSet will store the added overload candidates. (C++ |
| 4801 | /// [over.match.oper]). |
| 4802 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 4803 | SourceLocation OpLoc, |
| 4804 | Expr **Args, unsigned NumArgs, |
| 4805 | OverloadCandidateSet& CandidateSet, |
| 4806 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4807 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4808 | |
| 4809 | // C++ [over.match.oper]p3: |
| 4810 | // For a unary operator @ with an operand of a type whose |
| 4811 | // cv-unqualified version is T1, and for a binary operator @ with |
| 4812 | // a left operand of a type whose cv-unqualified version is T1 and |
| 4813 | // a right operand of a type whose cv-unqualified version is T2, |
| 4814 | // three sets of candidate functions, designated member |
| 4815 | // candidates, non-member candidates and built-in candidates, are |
| 4816 | // constructed as follows: |
| 4817 | QualType T1 = Args[0]->getType(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4818 | |
| 4819 | // -- If T1 is a class type, the set of member candidates is the |
| 4820 | // result of the qualified lookup of T1::operator@ |
| 4821 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 4822 | // empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4823 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 4824 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 4825 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 4826 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4827 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4828 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 4829 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 4830 | Operators.suppressDiagnostics(); |
| 4831 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4832 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 4833 | OperEnd = Operators.end(); |
| 4834 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4835 | ++Oper) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4836 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4837 | Args[0]->Classify(Context), Args + 1, NumArgs - 1, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4838 | CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4839 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4840 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4841 | } |
| 4842 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4843 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 4844 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 4845 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4846 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 4847 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4848 | /// operator. NumContextualBoolArguments is the number of arguments |
| 4849 | /// (at the beginning of the argument list) that will be contextually |
| 4850 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4851 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4852 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4853 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4854 | bool IsAssignmentOperator, |
| 4855 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4856 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4857 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4858 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4859 | // Add this candidate |
| 4860 | CandidateSet.push_back(OverloadCandidate()); |
| 4861 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4862 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4863 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 4864 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4865 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4866 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 4867 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4868 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 4869 | |
| 4870 | // Determine the implicit conversion sequences for each of the |
| 4871 | // arguments. |
| 4872 | Candidate.Viable = true; |
| 4873 | Candidate.Conversions.resize(NumArgs); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4874 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4875 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4876 | // C++ [over.match.oper]p4: |
| 4877 | // For the built-in assignment operators, conversions of the |
| 4878 | // left operand are restricted as follows: |
| 4879 | // -- no temporaries are introduced to hold the left operand, and |
| 4880 | // -- no user-defined conversions are applied to the left |
| 4881 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4882 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4883 | // |
| 4884 | // We block these conversions by turning off user-defined |
| 4885 | // conversions, since that is the only way that initialization of |
| 4886 | // a reference to a non-class type can occur from something that |
| 4887 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4888 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4889 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4890 | "Contextual conversion to bool requires bool type"); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4891 | Candidate.Conversions[ArgIdx] |
| 4892 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4893 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4894 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4895 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 4896 | ArgIdx == 0 && IsAssignmentOperator, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4897 | /*InOverloadResolution=*/false, |
| 4898 | /*AllowObjCWritebackConversion=*/ |
| 4899 | getLangOptions().ObjCAutoRefCount); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4900 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4901 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4902 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4903 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4904 | break; |
| 4905 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4906 | } |
| 4907 | } |
| 4908 | |
| 4909 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 4910 | /// candidate operator functions for built-in operators (C++ |
| 4911 | /// [over.built]). The types are separated into pointer types and |
| 4912 | /// enumeration types. |
| 4913 | class BuiltinCandidateTypeSet { |
| 4914 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4915 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4916 | |
| 4917 | /// PointerTypes - The set of pointer types that will be used in the |
| 4918 | /// built-in candidates. |
| 4919 | TypeSet PointerTypes; |
| 4920 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4921 | /// MemberPointerTypes - The set of member pointer types that will be |
| 4922 | /// used in the built-in candidates. |
| 4923 | TypeSet MemberPointerTypes; |
| 4924 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4925 | /// EnumerationTypes - The set of enumeration types that will be |
| 4926 | /// used in the built-in candidates. |
| 4927 | TypeSet EnumerationTypes; |
| 4928 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4929 | /// \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] | 4930 | /// candidates. |
| 4931 | TypeSet VectorTypes; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4932 | |
| 4933 | /// \brief A flag indicating non-record types are viable candidates |
| 4934 | bool HasNonRecordTypes; |
| 4935 | |
| 4936 | /// \brief A flag indicating whether either arithmetic or enumeration types |
| 4937 | /// were present in the candidate set. |
| 4938 | bool HasArithmeticOrEnumeralTypes; |
| 4939 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4940 | /// \brief A flag indicating whether the nullptr type was present in the |
| 4941 | /// candidate set. |
| 4942 | bool HasNullPtrType; |
| 4943 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4944 | /// Sema - The semantic analysis instance where we are building the |
| 4945 | /// candidate type set. |
| 4946 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4947 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4948 | /// Context - The AST context in which we will build the type sets. |
| 4949 | ASTContext &Context; |
| 4950 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4951 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 4952 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4953 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4954 | |
| 4955 | public: |
| 4956 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4957 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4958 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4959 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4960 | : HasNonRecordTypes(false), |
| 4961 | HasArithmeticOrEnumeralTypes(false), |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4962 | HasNullPtrType(false), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4963 | SemaRef(SemaRef), |
| 4964 | Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4965 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4966 | void AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4967 | SourceLocation Loc, |
| 4968 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4969 | bool AllowExplicitConversions, |
| 4970 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4971 | |
| 4972 | /// pointer_begin - First pointer type found; |
| 4973 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 4974 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4975 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4976 | iterator pointer_end() { return PointerTypes.end(); } |
| 4977 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4978 | /// member_pointer_begin - First member pointer type found; |
| 4979 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 4980 | |
| 4981 | /// member_pointer_end - Past the last member pointer type found; |
| 4982 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 4983 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4984 | /// enumeration_begin - First enumeration type found; |
| 4985 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 4986 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4987 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4988 | iterator enumeration_end() { return EnumerationTypes.end(); } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4989 | |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4990 | iterator vector_begin() { return VectorTypes.begin(); } |
| 4991 | iterator vector_end() { return VectorTypes.end(); } |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4992 | |
| 4993 | bool hasNonRecordTypes() { return HasNonRecordTypes; } |
| 4994 | bool hasArithmeticOrEnumeralTypes() { return HasArithmeticOrEnumeralTypes; } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4995 | bool hasNullPtrType() const { return HasNullPtrType; } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4996 | }; |
| 4997 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4998 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4999 | /// the set of pointer types along with any more-qualified variants of |
| 5000 | /// that type. For example, if @p Ty is "int const *", this routine |
| 5001 | /// will add "int const *", "int const volatile *", "int const |
| 5002 | /// restrict *", and "int const volatile restrict *" to the set of |
| 5003 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 5004 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5005 | /// |
| 5006 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5007 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5008 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 5009 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5010 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5011 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 5012 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5013 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5014 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 5015 | QualType PointeeTy; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5016 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 5017 | bool buildObjCPtr = false; |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 5018 | if (!PointerTy) { |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 5019 | if (const ObjCObjectPointerType *PTy = Ty->getAs<ObjCObjectPointerType>()) { |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 5020 | PointeeTy = PTy->getPointeeType(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 5021 | buildObjCPtr = true; |
| 5022 | } |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 5023 | else |
| 5024 | assert(false && "type was not a pointer type!"); |
| 5025 | } |
| 5026 | else |
| 5027 | PointeeTy = PointerTy->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5028 | |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 5029 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 5030 | // (the qualifier would sink to the element type), and for another, the |
| 5031 | // only overload situation where it matters is subscript or pointer +- int, |
| 5032 | // and those shouldn't have qualifier variants anyway. |
| 5033 | if (PointeeTy->isArrayType()) |
| 5034 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5035 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 5036 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | d411b3f | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 5037 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 5038 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 5039 | bool hasRestrict = VisibleQuals.hasRestrict(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5040 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5041 | // Iterate through all strict supersets of BaseCVR. |
| 5042 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 5043 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 5044 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 5045 | // in the types. |
| 5046 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 5047 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5048 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 5049 | if (!buildObjCPtr) |
| 5050 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
| 5051 | else |
| 5052 | PointerTypes.insert(Context.getObjCObjectPointerType(QPointeeTy)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5053 | } |
| 5054 | |
| 5055 | return true; |
| 5056 | } |
| 5057 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5058 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 5059 | /// to the set of pointer types along with any more-qualified variants of |
| 5060 | /// that type. For example, if @p Ty is "int const *", this routine |
| 5061 | /// will add "int const *", "int const volatile *", "int const |
| 5062 | /// restrict *", and "int const volatile restrict *" to the set of |
| 5063 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 5064 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5065 | /// |
| 5066 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5067 | bool |
| 5068 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 5069 | QualType Ty) { |
| 5070 | // Insert this type. |
| 5071 | if (!MemberPointerTypes.insert(Ty)) |
| 5072 | return false; |
| 5073 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5074 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 5075 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5076 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5077 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 5078 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 5079 | // (the qualifier would sink to the element type), and for another, the |
| 5080 | // only overload situation where it matters is subscript or pointer +- int, |
| 5081 | // and those shouldn't have qualifier variants anyway. |
| 5082 | if (PointeeTy->isArrayType()) |
| 5083 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5084 | const Type *ClassTy = PointerTy->getClass(); |
| 5085 | |
| 5086 | // Iterate through all strict supersets of the pointee type's CVR |
| 5087 | // qualifiers. |
| 5088 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 5089 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 5090 | if ((CVR | BaseCVR) != CVR) continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5091 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5092 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 5093 | MemberPointerTypes.insert( |
| 5094 | Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5095 | } |
| 5096 | |
| 5097 | return true; |
| 5098 | } |
| 5099 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5100 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 5101 | /// 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] | 5102 | /// primarily interested in pointer types and enumeration types. We also |
| 5103 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5104 | /// AllowUserConversions is true if we should look at the conversion |
| 5105 | /// functions of a class type, and AllowExplicitConversions if we |
| 5106 | /// should also include the explicit conversion functions of a class |
| 5107 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5108 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5109 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5110 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5111 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5112 | bool AllowExplicitConversions, |
| 5113 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5114 | // Only deal with canonical types. |
| 5115 | Ty = Context.getCanonicalType(Ty); |
| 5116 | |
| 5117 | // Look through reference types; they aren't part of the type of an |
| 5118 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5119 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5120 | Ty = RefTy->getPointeeType(); |
| 5121 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5122 | // If we're dealing with an array type, decay to the pointer. |
| 5123 | if (Ty->isArrayType()) |
| 5124 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 5125 | |
| 5126 | // Otherwise, we don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5127 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5128 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5129 | // Flag if we ever add a non-record type. |
| 5130 | const RecordType *TyRec = Ty->getAs<RecordType>(); |
| 5131 | HasNonRecordTypes = HasNonRecordTypes || !TyRec; |
| 5132 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5133 | // Flag if we encounter an arithmetic type. |
| 5134 | HasArithmeticOrEnumeralTypes = |
| 5135 | HasArithmeticOrEnumeralTypes || Ty->isArithmeticType(); |
| 5136 | |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 5137 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
| 5138 | PointerTypes.insert(Ty); |
| 5139 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5140 | // Insert our type, and its more-qualified variants, into the set |
| 5141 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 5142 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5143 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5144 | } else if (Ty->isMemberPointerType()) { |
| 5145 | // Member pointers are far easier, since the pointee can't be converted. |
| 5146 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 5147 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5148 | } else if (Ty->isEnumeralType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5149 | HasArithmeticOrEnumeralTypes = true; |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 5150 | EnumerationTypes.insert(Ty); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 5151 | } else if (Ty->isVectorType()) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5152 | // We treat vector types as arithmetic types in many contexts as an |
| 5153 | // extension. |
| 5154 | HasArithmeticOrEnumeralTypes = true; |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 5155 | VectorTypes.insert(Ty); |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 5156 | } else if (Ty->isNullPtrType()) { |
| 5157 | HasNullPtrType = true; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5158 | } else if (AllowUserConversions && TyRec) { |
| 5159 | // No conversion functions in incomplete types. |
| 5160 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) |
| 5161 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5162 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5163 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| 5164 | const UnresolvedSetImpl *Conversions |
| 5165 | = ClassDecl->getVisibleConversionFunctions(); |
| 5166 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 5167 | E = Conversions->end(); I != E; ++I) { |
| 5168 | NamedDecl *D = I.getDecl(); |
| 5169 | if (isa<UsingShadowDecl>(D)) |
| 5170 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5171 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5172 | // Skip conversion function templates; they don't tell us anything |
| 5173 | // about which builtin types we can convert to. |
| 5174 | if (isa<FunctionTemplateDecl>(D)) |
| 5175 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5176 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5177 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
| 5178 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
| 5179 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
| 5180 | VisibleQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5181 | } |
| 5182 | } |
| 5183 | } |
| 5184 | } |
| 5185 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 5186 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 5187 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 5188 | /// given type to the candidate set. |
| 5189 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 5190 | QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5191 | Expr **Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 5192 | unsigned NumArgs, |
| 5193 | OverloadCandidateSet &CandidateSet) { |
| 5194 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5195 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 5196 | // T& operator=(T&, T) |
| 5197 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 5198 | ParamTypes[1] = T; |
| 5199 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5200 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5201 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 5202 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 5203 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5204 | ParamTypes[0] |
| 5205 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 5206 | ParamTypes[1] = T; |
| 5207 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5208 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 5209 | } |
| 5210 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5211 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 5212 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 5213 | /// 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] | 5214 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 5215 | Qualifiers VRQuals; |
| 5216 | const RecordType *TyRec; |
| 5217 | if (const MemberPointerType *RHSMPType = |
| 5218 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 5219 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5220 | else |
| 5221 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 5222 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 5223 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5224 | VRQuals.addVolatile(); |
| 5225 | VRQuals.addRestrict(); |
| 5226 | return VRQuals; |
| 5227 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5228 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5229 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 5230 | if (!ClassDecl->hasDefinition()) |
| 5231 | return VRQuals; |
| 5232 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 5233 | const UnresolvedSetImpl *Conversions = |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 5234 | ClassDecl->getVisibleConversionFunctions(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5235 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 5236 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5237 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 5238 | NamedDecl *D = I.getDecl(); |
| 5239 | if (isa<UsingShadowDecl>(D)) |
| 5240 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 5241 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5242 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 5243 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 5244 | CanTy = ResTypeRef->getPointeeType(); |
| 5245 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 5246 | // as see them. |
| 5247 | bool done = false; |
| 5248 | while (!done) { |
| 5249 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 5250 | CanTy = ResTypePtr->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5251 | else if (const MemberPointerType *ResTypeMPtr = |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5252 | CanTy->getAs<MemberPointerType>()) |
| 5253 | CanTy = ResTypeMPtr->getPointeeType(); |
| 5254 | else |
| 5255 | done = true; |
| 5256 | if (CanTy.isVolatileQualified()) |
| 5257 | VRQuals.addVolatile(); |
| 5258 | if (CanTy.isRestrictQualified()) |
| 5259 | VRQuals.addRestrict(); |
| 5260 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 5261 | return VRQuals; |
| 5262 | } |
| 5263 | } |
| 5264 | } |
| 5265 | return VRQuals; |
| 5266 | } |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 5267 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5268 | namespace { |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 5269 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5270 | /// \brief Helper class to manage the addition of builtin operator overload |
| 5271 | /// candidates. It provides shared state and utility methods used throughout |
| 5272 | /// the process, as well as a helper method to add each group of builtin |
| 5273 | /// operator overloads from the standard to a candidate set. |
| 5274 | class BuiltinOperatorOverloadBuilder { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5275 | // Common instance state available to all overload candidate addition methods. |
| 5276 | Sema &S; |
| 5277 | Expr **Args; |
| 5278 | unsigned NumArgs; |
| 5279 | Qualifiers VisibleTypeConversionsQuals; |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5280 | bool HasArithmeticOrEnumeralCandidateType; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5281 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5282 | OverloadCandidateSet &CandidateSet; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5283 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5284 | // Define some constants used to index and iterate over the arithemetic types |
| 5285 | // provided via the getArithmeticType() method below. |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 5286 | // The "promoted arithmetic types" are the arithmetic |
| 5287 | // types are that preserved by promotion (C++ [over.built]p2). |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 5288 | static const unsigned FirstIntegralType = 3; |
| 5289 | static const unsigned LastIntegralType = 18; |
| 5290 | static const unsigned FirstPromotedIntegralType = 3, |
| 5291 | LastPromotedIntegralType = 9; |
| 5292 | static const unsigned FirstPromotedArithmeticType = 0, |
| 5293 | LastPromotedArithmeticType = 9; |
| 5294 | static const unsigned NumArithmeticTypes = 18; |
| 5295 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5296 | /// \brief Get the canonical type for a given arithmetic type index. |
| 5297 | CanQualType getArithmeticType(unsigned index) { |
| 5298 | assert(index < NumArithmeticTypes); |
| 5299 | static CanQualType ASTContext::* const |
| 5300 | ArithmeticTypes[NumArithmeticTypes] = { |
| 5301 | // Start of promoted types. |
| 5302 | &ASTContext::FloatTy, |
| 5303 | &ASTContext::DoubleTy, |
| 5304 | &ASTContext::LongDoubleTy, |
John McCall | 00071ec | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 5305 | |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5306 | // Start of integral types. |
| 5307 | &ASTContext::IntTy, |
| 5308 | &ASTContext::LongTy, |
| 5309 | &ASTContext::LongLongTy, |
| 5310 | &ASTContext::UnsignedIntTy, |
| 5311 | &ASTContext::UnsignedLongTy, |
| 5312 | &ASTContext::UnsignedLongLongTy, |
| 5313 | // End of promoted types. |
| 5314 | |
| 5315 | &ASTContext::BoolTy, |
| 5316 | &ASTContext::CharTy, |
| 5317 | &ASTContext::WCharTy, |
| 5318 | &ASTContext::Char16Ty, |
| 5319 | &ASTContext::Char32Ty, |
| 5320 | &ASTContext::SignedCharTy, |
| 5321 | &ASTContext::ShortTy, |
| 5322 | &ASTContext::UnsignedCharTy, |
| 5323 | &ASTContext::UnsignedShortTy, |
| 5324 | // End of integral types. |
| 5325 | // FIXME: What about complex? |
| 5326 | }; |
| 5327 | return S.Context.*ArithmeticTypes[index]; |
| 5328 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5329 | |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5330 | /// \brief Gets the canonical type resulting from the usual arithemetic |
| 5331 | /// converions for the given arithmetic types. |
| 5332 | CanQualType getUsualArithmeticConversions(unsigned L, unsigned R) { |
| 5333 | // Accelerator table for performing the usual arithmetic conversions. |
| 5334 | // The rules are basically: |
| 5335 | // - if either is floating-point, use the wider floating-point |
| 5336 | // - if same signedness, use the higher rank |
| 5337 | // - if same size, use unsigned of the higher rank |
| 5338 | // - use the larger type |
| 5339 | // These rules, together with the axiom that higher ranks are |
| 5340 | // never smaller, are sufficient to precompute all of these results |
| 5341 | // *except* when dealing with signed types of higher rank. |
| 5342 | // (we could precompute SLL x UI for all known platforms, but it's |
| 5343 | // better not to make any assumptions). |
| 5344 | enum PromotedType { |
| 5345 | Flt, Dbl, LDbl, SI, SL, SLL, UI, UL, ULL, Dep=-1 |
| 5346 | }; |
| 5347 | static PromotedType ConversionsTable[LastPromotedArithmeticType] |
| 5348 | [LastPromotedArithmeticType] = { |
| 5349 | /* Flt*/ { Flt, Dbl, LDbl, Flt, Flt, Flt, Flt, Flt, Flt }, |
| 5350 | /* Dbl*/ { Dbl, Dbl, LDbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl }, |
| 5351 | /*LDbl*/ { LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl }, |
| 5352 | /* SI*/ { Flt, Dbl, LDbl, SI, SL, SLL, UI, UL, ULL }, |
| 5353 | /* SL*/ { Flt, Dbl, LDbl, SL, SL, SLL, Dep, UL, ULL }, |
| 5354 | /* SLL*/ { Flt, Dbl, LDbl, SLL, SLL, SLL, Dep, Dep, ULL }, |
| 5355 | /* UI*/ { Flt, Dbl, LDbl, UI, Dep, Dep, UI, UL, ULL }, |
| 5356 | /* UL*/ { Flt, Dbl, LDbl, UL, UL, Dep, UL, UL, ULL }, |
| 5357 | /* ULL*/ { Flt, Dbl, LDbl, ULL, ULL, ULL, ULL, ULL, ULL }, |
| 5358 | }; |
| 5359 | |
| 5360 | assert(L < LastPromotedArithmeticType); |
| 5361 | assert(R < LastPromotedArithmeticType); |
| 5362 | int Idx = ConversionsTable[L][R]; |
| 5363 | |
| 5364 | // Fast path: the table gives us a concrete answer. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5365 | if (Idx != Dep) return getArithmeticType(Idx); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5366 | |
| 5367 | // Slow path: we need to compare widths. |
| 5368 | // An invariant is that the signed type has higher rank. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5369 | CanQualType LT = getArithmeticType(L), |
| 5370 | RT = getArithmeticType(R); |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5371 | unsigned LW = S.Context.getIntWidth(LT), |
| 5372 | RW = S.Context.getIntWidth(RT); |
| 5373 | |
| 5374 | // If they're different widths, use the signed type. |
| 5375 | if (LW > RW) return LT; |
| 5376 | else if (LW < RW) return RT; |
| 5377 | |
| 5378 | // Otherwise, use the unsigned type of the signed type's rank. |
| 5379 | if (L == SL || R == SL) return S.Context.UnsignedLongTy; |
| 5380 | assert(L == SLL || R == SLL); |
| 5381 | return S.Context.UnsignedLongLongTy; |
| 5382 | } |
| 5383 | |
Chandler Carruth | 3c69dc4 | 2010-12-12 09:22:45 +0000 | [diff] [blame] | 5384 | /// \brief Helper method to factor out the common pattern of adding overloads |
| 5385 | /// for '++' and '--' builtin operators. |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5386 | void addPlusPlusMinusMinusStyleOverloads(QualType CandidateTy, |
| 5387 | bool HasVolatile) { |
| 5388 | QualType ParamTypes[2] = { |
| 5389 | S.Context.getLValueReferenceType(CandidateTy), |
| 5390 | S.Context.IntTy |
| 5391 | }; |
| 5392 | |
| 5393 | // Non-volatile version. |
| 5394 | if (NumArgs == 1) |
| 5395 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 5396 | else |
| 5397 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 5398 | |
| 5399 | // Use a heuristic to reduce number of builtin candidates in the set: |
| 5400 | // add volatile version only if there are conversions to a volatile type. |
| 5401 | if (HasVolatile) { |
| 5402 | ParamTypes[0] = |
| 5403 | S.Context.getLValueReferenceType( |
| 5404 | S.Context.getVolatileType(CandidateTy)); |
| 5405 | if (NumArgs == 1) |
| 5406 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 5407 | else |
| 5408 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 5409 | } |
| 5410 | } |
| 5411 | |
| 5412 | public: |
| 5413 | BuiltinOperatorOverloadBuilder( |
| 5414 | Sema &S, Expr **Args, unsigned NumArgs, |
| 5415 | Qualifiers VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5416 | bool HasArithmeticOrEnumeralCandidateType, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5417 | SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5418 | OverloadCandidateSet &CandidateSet) |
| 5419 | : S(S), Args(Args), NumArgs(NumArgs), |
| 5420 | VisibleTypeConversionsQuals(VisibleTypeConversionsQuals), |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5421 | HasArithmeticOrEnumeralCandidateType( |
| 5422 | HasArithmeticOrEnumeralCandidateType), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5423 | CandidateTypes(CandidateTypes), |
| 5424 | CandidateSet(CandidateSet) { |
| 5425 | // Validate some of our static helper constants in debug builds. |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5426 | assert(getArithmeticType(FirstPromotedIntegralType) == S.Context.IntTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5427 | "Invalid first promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5428 | assert(getArithmeticType(LastPromotedIntegralType - 1) |
| 5429 | == S.Context.UnsignedLongLongTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5430 | "Invalid last promoted integral type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5431 | assert(getArithmeticType(FirstPromotedArithmeticType) |
| 5432 | == S.Context.FloatTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5433 | "Invalid first promoted arithmetic type"); |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5434 | assert(getArithmeticType(LastPromotedArithmeticType - 1) |
| 5435 | == S.Context.UnsignedLongLongTy && |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5436 | "Invalid last promoted arithmetic type"); |
| 5437 | } |
| 5438 | |
| 5439 | // C++ [over.built]p3: |
| 5440 | // |
| 5441 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 5442 | // is either volatile or empty, there exist candidate operator |
| 5443 | // functions of the form |
| 5444 | // |
| 5445 | // VQ T& operator++(VQ T&); |
| 5446 | // T operator++(VQ T&, int); |
| 5447 | // |
| 5448 | // C++ [over.built]p4: |
| 5449 | // |
| 5450 | // For every pair (T, VQ), where T is an arithmetic type other |
| 5451 | // than bool, and VQ is either volatile or empty, there exist |
| 5452 | // candidate operator functions of the form |
| 5453 | // |
| 5454 | // VQ T& operator--(VQ T&); |
| 5455 | // T operator--(VQ T&, int); |
| 5456 | void addPlusPlusMinusMinusArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5457 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5458 | return; |
| 5459 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5460 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 5461 | Arith < NumArithmeticTypes; ++Arith) { |
| 5462 | addPlusPlusMinusMinusStyleOverloads( |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5463 | getArithmeticType(Arith), |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5464 | VisibleTypeConversionsQuals.hasVolatile()); |
| 5465 | } |
| 5466 | } |
| 5467 | |
| 5468 | // C++ [over.built]p5: |
| 5469 | // |
| 5470 | // For every pair (T, VQ), where T is a cv-qualified or |
| 5471 | // cv-unqualified object type, and VQ is either volatile or |
| 5472 | // empty, there exist candidate operator functions of the form |
| 5473 | // |
| 5474 | // T*VQ& operator++(T*VQ&); |
| 5475 | // T*VQ& operator--(T*VQ&); |
| 5476 | // T* operator++(T*VQ&, int); |
| 5477 | // T* operator--(T*VQ&, int); |
| 5478 | void addPlusPlusMinusMinusPointerOverloads() { |
| 5479 | for (BuiltinCandidateTypeSet::iterator |
| 5480 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5481 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5482 | Ptr != PtrEnd; ++Ptr) { |
| 5483 | // Skip pointer types that aren't pointers to object types. |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5484 | if (!(*Ptr)->getPointeeType()->isObjectType()) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5485 | continue; |
| 5486 | |
| 5487 | addPlusPlusMinusMinusStyleOverloads(*Ptr, |
| 5488 | (!S.Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 5489 | VisibleTypeConversionsQuals.hasVolatile())); |
| 5490 | } |
| 5491 | } |
| 5492 | |
| 5493 | // C++ [over.built]p6: |
| 5494 | // For every cv-qualified or cv-unqualified object type T, there |
| 5495 | // exist candidate operator functions of the form |
| 5496 | // |
| 5497 | // T& operator*(T*); |
| 5498 | // |
| 5499 | // C++ [over.built]p7: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5500 | // 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] | 5501 | // ref-qualifier, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5502 | // T& operator*(T*); |
| 5503 | void addUnaryStarPointerOverloads() { |
| 5504 | for (BuiltinCandidateTypeSet::iterator |
| 5505 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5506 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5507 | Ptr != PtrEnd; ++Ptr) { |
| 5508 | QualType ParamTy = *Ptr; |
| 5509 | QualType PointeeTy = ParamTy->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5510 | if (!PointeeTy->isObjectType() && !PointeeTy->isFunctionType()) |
| 5511 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5512 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5513 | if (const FunctionProtoType *Proto =PointeeTy->getAs<FunctionProtoType>()) |
| 5514 | if (Proto->getTypeQuals() || Proto->getRefQualifier()) |
| 5515 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5516 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5517 | S.AddBuiltinCandidate(S.Context.getLValueReferenceType(PointeeTy), |
| 5518 | &ParamTy, Args, 1, CandidateSet); |
| 5519 | } |
| 5520 | } |
| 5521 | |
| 5522 | // C++ [over.built]p9: |
| 5523 | // For every promoted arithmetic type T, there exist candidate |
| 5524 | // operator functions of the form |
| 5525 | // |
| 5526 | // T operator+(T); |
| 5527 | // T operator-(T); |
| 5528 | void addUnaryPlusOrMinusArithmeticOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5529 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5530 | return; |
| 5531 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5532 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 5533 | Arith < LastPromotedArithmeticType; ++Arith) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5534 | QualType ArithTy = getArithmeticType(Arith); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5535 | S.AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 5536 | } |
| 5537 | |
| 5538 | // Extension: We also add these operators for vector types. |
| 5539 | for (BuiltinCandidateTypeSet::iterator |
| 5540 | Vec = CandidateTypes[0].vector_begin(), |
| 5541 | VecEnd = CandidateTypes[0].vector_end(); |
| 5542 | Vec != VecEnd; ++Vec) { |
| 5543 | QualType VecTy = *Vec; |
| 5544 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 5545 | } |
| 5546 | } |
| 5547 | |
| 5548 | // C++ [over.built]p8: |
| 5549 | // For every type T, there exist candidate operator functions of |
| 5550 | // the form |
| 5551 | // |
| 5552 | // T* operator+(T*); |
| 5553 | void addUnaryPlusPointerOverloads() { |
| 5554 | for (BuiltinCandidateTypeSet::iterator |
| 5555 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5556 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5557 | Ptr != PtrEnd; ++Ptr) { |
| 5558 | QualType ParamTy = *Ptr; |
| 5559 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 5560 | } |
| 5561 | } |
| 5562 | |
| 5563 | // C++ [over.built]p10: |
| 5564 | // For every promoted integral type T, there exist candidate |
| 5565 | // operator functions of the form |
| 5566 | // |
| 5567 | // T operator~(T); |
| 5568 | void addUnaryTildePromotedIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5569 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5570 | return; |
| 5571 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5572 | for (unsigned Int = FirstPromotedIntegralType; |
| 5573 | Int < LastPromotedIntegralType; ++Int) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5574 | QualType IntTy = getArithmeticType(Int); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5575 | S.AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 5576 | } |
| 5577 | |
| 5578 | // Extension: We also add this operator for vector types. |
| 5579 | for (BuiltinCandidateTypeSet::iterator |
| 5580 | Vec = CandidateTypes[0].vector_begin(), |
| 5581 | VecEnd = CandidateTypes[0].vector_end(); |
| 5582 | Vec != VecEnd; ++Vec) { |
| 5583 | QualType VecTy = *Vec; |
| 5584 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 5585 | } |
| 5586 | } |
| 5587 | |
| 5588 | // C++ [over.match.oper]p16: |
| 5589 | // For every pointer to member type T, there exist candidate operator |
| 5590 | // functions of the form |
| 5591 | // |
| 5592 | // bool operator==(T,T); |
| 5593 | // bool operator!=(T,T); |
| 5594 | void addEqualEqualOrNotEqualMemberPointerOverloads() { |
| 5595 | /// Set of (canonical) types that we've already handled. |
| 5596 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5597 | |
| 5598 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 5599 | for (BuiltinCandidateTypeSet::iterator |
| 5600 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 5601 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 5602 | MemPtr != MemPtrEnd; |
| 5603 | ++MemPtr) { |
| 5604 | // Don't add the same builtin candidate twice. |
| 5605 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 5606 | continue; |
| 5607 | |
| 5608 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 5609 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 5610 | CandidateSet); |
| 5611 | } |
| 5612 | } |
| 5613 | } |
| 5614 | |
| 5615 | // C++ [over.built]p15: |
| 5616 | // |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 5617 | // For every T, where T is an enumeration type, a pointer type, or |
| 5618 | // std::nullptr_t, there exist candidate operator functions of the form |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5619 | // |
| 5620 | // bool operator<(T, T); |
| 5621 | // bool operator>(T, T); |
| 5622 | // bool operator<=(T, T); |
| 5623 | // bool operator>=(T, T); |
| 5624 | // bool operator==(T, T); |
| 5625 | // bool operator!=(T, T); |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 5626 | void addRelationalPointerOrEnumeralOverloads() { |
| 5627 | // C++ [over.built]p1: |
| 5628 | // If there is a user-written candidate with the same name and parameter |
| 5629 | // types as a built-in candidate operator function, the built-in operator |
| 5630 | // function is hidden and is not included in the set of candidate |
| 5631 | // functions. |
| 5632 | // |
| 5633 | // The text is actually in a note, but if we don't implement it then we end |
| 5634 | // up with ambiguities when the user provides an overloaded operator for |
| 5635 | // an enumeration type. Note that only enumeration types have this problem, |
| 5636 | // so we track which enumeration types we've seen operators for. Also, the |
| 5637 | // only other overloaded operator with enumeration argumenst, operator=, |
| 5638 | // cannot be overloaded for enumeration types, so this is the only place |
| 5639 | // where we must suppress candidates like this. |
| 5640 | llvm::DenseSet<std::pair<CanQualType, CanQualType> > |
| 5641 | UserDefinedBinaryOperators; |
| 5642 | |
| 5643 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 5644 | if (CandidateTypes[ArgIdx].enumeration_begin() != |
| 5645 | CandidateTypes[ArgIdx].enumeration_end()) { |
| 5646 | for (OverloadCandidateSet::iterator C = CandidateSet.begin(), |
| 5647 | CEnd = CandidateSet.end(); |
| 5648 | C != CEnd; ++C) { |
| 5649 | if (!C->Viable || !C->Function || C->Function->getNumParams() != 2) |
| 5650 | continue; |
| 5651 | |
| 5652 | QualType FirstParamType = |
| 5653 | C->Function->getParamDecl(0)->getType().getUnqualifiedType(); |
| 5654 | QualType SecondParamType = |
| 5655 | C->Function->getParamDecl(1)->getType().getUnqualifiedType(); |
| 5656 | |
| 5657 | // Skip if either parameter isn't of enumeral type. |
| 5658 | if (!FirstParamType->isEnumeralType() || |
| 5659 | !SecondParamType->isEnumeralType()) |
| 5660 | continue; |
| 5661 | |
| 5662 | // Add this operator to the set of known user-defined operators. |
| 5663 | UserDefinedBinaryOperators.insert( |
| 5664 | std::make_pair(S.Context.getCanonicalType(FirstParamType), |
| 5665 | S.Context.getCanonicalType(SecondParamType))); |
| 5666 | } |
| 5667 | } |
| 5668 | } |
| 5669 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5670 | /// Set of (canonical) types that we've already handled. |
| 5671 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5672 | |
| 5673 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 5674 | for (BuiltinCandidateTypeSet::iterator |
| 5675 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 5676 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 5677 | Ptr != PtrEnd; ++Ptr) { |
| 5678 | // Don't add the same builtin candidate twice. |
| 5679 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 5680 | continue; |
| 5681 | |
| 5682 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5683 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 5684 | CandidateSet); |
| 5685 | } |
| 5686 | for (BuiltinCandidateTypeSet::iterator |
| 5687 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 5688 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 5689 | Enum != EnumEnd; ++Enum) { |
| 5690 | CanQualType CanonType = S.Context.getCanonicalType(*Enum); |
| 5691 | |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 5692 | // Don't add the same builtin candidate twice, or if a user defined |
| 5693 | // candidate exists. |
| 5694 | if (!AddedTypes.insert(CanonType) || |
| 5695 | UserDefinedBinaryOperators.count(std::make_pair(CanonType, |
| 5696 | CanonType))) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5697 | continue; |
| 5698 | |
| 5699 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 5700 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 5701 | CandidateSet); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5702 | } |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 5703 | |
| 5704 | if (CandidateTypes[ArgIdx].hasNullPtrType()) { |
| 5705 | CanQualType NullPtrTy = S.Context.getCanonicalType(S.Context.NullPtrTy); |
| 5706 | if (AddedTypes.insert(NullPtrTy) && |
| 5707 | !UserDefinedBinaryOperators.count(std::make_pair(NullPtrTy, |
| 5708 | NullPtrTy))) { |
| 5709 | QualType ParamTypes[2] = { NullPtrTy, NullPtrTy }; |
| 5710 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 5711 | CandidateSet); |
| 5712 | } |
| 5713 | } |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5714 | } |
| 5715 | } |
| 5716 | |
| 5717 | // C++ [over.built]p13: |
| 5718 | // |
| 5719 | // For every cv-qualified or cv-unqualified object type T |
| 5720 | // there exist candidate operator functions of the form |
| 5721 | // |
| 5722 | // T* operator+(T*, ptrdiff_t); |
| 5723 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 5724 | // T* operator-(T*, ptrdiff_t); |
| 5725 | // T* operator+(ptrdiff_t, T*); |
| 5726 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 5727 | // |
| 5728 | // C++ [over.built]p14: |
| 5729 | // |
| 5730 | // For every T, where T is a pointer to object type, there |
| 5731 | // exist candidate operator functions of the form |
| 5732 | // |
| 5733 | // ptrdiff_t operator-(T, T); |
| 5734 | void addBinaryPlusOrMinusPointerOverloads(OverloadedOperatorKind Op) { |
| 5735 | /// Set of (canonical) types that we've already handled. |
| 5736 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5737 | |
| 5738 | for (int Arg = 0; Arg < 2; ++Arg) { |
| 5739 | QualType AsymetricParamTypes[2] = { |
| 5740 | S.Context.getPointerDiffType(), |
| 5741 | S.Context.getPointerDiffType(), |
| 5742 | }; |
| 5743 | for (BuiltinCandidateTypeSet::iterator |
| 5744 | Ptr = CandidateTypes[Arg].pointer_begin(), |
| 5745 | PtrEnd = CandidateTypes[Arg].pointer_end(); |
| 5746 | Ptr != PtrEnd; ++Ptr) { |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5747 | QualType PointeeTy = (*Ptr)->getPointeeType(); |
| 5748 | if (!PointeeTy->isObjectType()) |
| 5749 | continue; |
| 5750 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5751 | AsymetricParamTypes[Arg] = *Ptr; |
| 5752 | if (Arg == 0 || Op == OO_Plus) { |
| 5753 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 5754 | // T* operator+(ptrdiff_t, T*); |
| 5755 | S.AddBuiltinCandidate(*Ptr, AsymetricParamTypes, Args, 2, |
| 5756 | CandidateSet); |
| 5757 | } |
| 5758 | if (Op == OO_Minus) { |
| 5759 | // ptrdiff_t operator-(T, T); |
| 5760 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 5761 | continue; |
| 5762 | |
| 5763 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5764 | S.AddBuiltinCandidate(S.Context.getPointerDiffType(), ParamTypes, |
| 5765 | Args, 2, CandidateSet); |
| 5766 | } |
| 5767 | } |
| 5768 | } |
| 5769 | } |
| 5770 | |
| 5771 | // C++ [over.built]p12: |
| 5772 | // |
| 5773 | // For every pair of promoted arithmetic types L and R, there |
| 5774 | // exist candidate operator functions of the form |
| 5775 | // |
| 5776 | // LR operator*(L, R); |
| 5777 | // LR operator/(L, R); |
| 5778 | // LR operator+(L, R); |
| 5779 | // LR operator-(L, R); |
| 5780 | // bool operator<(L, R); |
| 5781 | // bool operator>(L, R); |
| 5782 | // bool operator<=(L, R); |
| 5783 | // bool operator>=(L, R); |
| 5784 | // bool operator==(L, R); |
| 5785 | // bool operator!=(L, R); |
| 5786 | // |
| 5787 | // where LR is the result of the usual arithmetic conversions |
| 5788 | // between types L and R. |
| 5789 | // |
| 5790 | // C++ [over.built]p24: |
| 5791 | // |
| 5792 | // For every pair of promoted arithmetic types L and R, there exist |
| 5793 | // candidate operator functions of the form |
| 5794 | // |
| 5795 | // LR operator?(bool, L, R); |
| 5796 | // |
| 5797 | // where LR is the result of the usual arithmetic conversions |
| 5798 | // between types L and R. |
| 5799 | // Our candidates ignore the first parameter. |
| 5800 | void addGenericBinaryArithmeticOverloads(bool isComparison) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5801 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5802 | return; |
| 5803 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5804 | for (unsigned Left = FirstPromotedArithmeticType; |
| 5805 | Left < LastPromotedArithmeticType; ++Left) { |
| 5806 | for (unsigned Right = FirstPromotedArithmeticType; |
| 5807 | Right < LastPromotedArithmeticType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5808 | QualType LandR[2] = { getArithmeticType(Left), |
| 5809 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5810 | QualType Result = |
| 5811 | isComparison ? S.Context.BoolTy |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5812 | : getUsualArithmeticConversions(Left, Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5813 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 5814 | } |
| 5815 | } |
| 5816 | |
| 5817 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 5818 | // conditional operator for vector types. |
| 5819 | for (BuiltinCandidateTypeSet::iterator |
| 5820 | Vec1 = CandidateTypes[0].vector_begin(), |
| 5821 | Vec1End = CandidateTypes[0].vector_end(); |
| 5822 | Vec1 != Vec1End; ++Vec1) { |
| 5823 | for (BuiltinCandidateTypeSet::iterator |
| 5824 | Vec2 = CandidateTypes[1].vector_begin(), |
| 5825 | Vec2End = CandidateTypes[1].vector_end(); |
| 5826 | Vec2 != Vec2End; ++Vec2) { |
| 5827 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 5828 | QualType Result = S.Context.BoolTy; |
| 5829 | if (!isComparison) { |
| 5830 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 5831 | Result = *Vec1; |
| 5832 | else |
| 5833 | Result = *Vec2; |
| 5834 | } |
| 5835 | |
| 5836 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 5837 | } |
| 5838 | } |
| 5839 | } |
| 5840 | |
| 5841 | // C++ [over.built]p17: |
| 5842 | // |
| 5843 | // For every pair of promoted integral types L and R, there |
| 5844 | // exist candidate operator functions of the form |
| 5845 | // |
| 5846 | // LR operator%(L, R); |
| 5847 | // LR operator&(L, R); |
| 5848 | // LR operator^(L, R); |
| 5849 | // LR operator|(L, R); |
| 5850 | // L operator<<(L, R); |
| 5851 | // L operator>>(L, R); |
| 5852 | // |
| 5853 | // where LR is the result of the usual arithmetic conversions |
| 5854 | // between types L and R. |
| 5855 | void addBinaryBitwiseArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5856 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5857 | return; |
| 5858 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5859 | for (unsigned Left = FirstPromotedIntegralType; |
| 5860 | Left < LastPromotedIntegralType; ++Left) { |
| 5861 | for (unsigned Right = FirstPromotedIntegralType; |
| 5862 | Right < LastPromotedIntegralType; ++Right) { |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5863 | QualType LandR[2] = { getArithmeticType(Left), |
| 5864 | getArithmeticType(Right) }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5865 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 5866 | ? LandR[0] |
Chandler Carruth | 38ca8d1 | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5867 | : getUsualArithmeticConversions(Left, Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5868 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 5869 | } |
| 5870 | } |
| 5871 | } |
| 5872 | |
| 5873 | // C++ [over.built]p20: |
| 5874 | // |
| 5875 | // For every pair (T, VQ), where T is an enumeration or |
| 5876 | // pointer to member type and VQ is either volatile or |
| 5877 | // empty, there exist candidate operator functions of the form |
| 5878 | // |
| 5879 | // VQ T& operator=(VQ T&, T); |
| 5880 | void addAssignmentMemberPointerOrEnumeralOverloads() { |
| 5881 | /// Set of (canonical) types that we've already handled. |
| 5882 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5883 | |
| 5884 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 5885 | for (BuiltinCandidateTypeSet::iterator |
| 5886 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 5887 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 5888 | Enum != EnumEnd; ++Enum) { |
| 5889 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 5890 | continue; |
| 5891 | |
| 5892 | AddBuiltinAssignmentOperatorCandidates(S, *Enum, Args, 2, |
| 5893 | CandidateSet); |
| 5894 | } |
| 5895 | |
| 5896 | for (BuiltinCandidateTypeSet::iterator |
| 5897 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 5898 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 5899 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 5900 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 5901 | continue; |
| 5902 | |
| 5903 | AddBuiltinAssignmentOperatorCandidates(S, *MemPtr, Args, 2, |
| 5904 | CandidateSet); |
| 5905 | } |
| 5906 | } |
| 5907 | } |
| 5908 | |
| 5909 | // C++ [over.built]p19: |
| 5910 | // |
| 5911 | // For every pair (T, VQ), where T is any type and VQ is either |
| 5912 | // volatile or empty, there exist candidate operator functions |
| 5913 | // of the form |
| 5914 | // |
| 5915 | // T*VQ& operator=(T*VQ&, T*); |
| 5916 | // |
| 5917 | // C++ [over.built]p21: |
| 5918 | // |
| 5919 | // For every pair (T, VQ), where T is a cv-qualified or |
| 5920 | // cv-unqualified object type and VQ is either volatile or |
| 5921 | // empty, there exist candidate operator functions of the form |
| 5922 | // |
| 5923 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 5924 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 5925 | void addAssignmentPointerOverloads(bool isEqualOp) { |
| 5926 | /// Set of (canonical) types that we've already handled. |
| 5927 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5928 | |
| 5929 | for (BuiltinCandidateTypeSet::iterator |
| 5930 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5931 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5932 | Ptr != PtrEnd; ++Ptr) { |
| 5933 | // If this is operator=, keep track of the builtin candidates we added. |
| 5934 | if (isEqualOp) |
| 5935 | AddedTypes.insert(S.Context.getCanonicalType(*Ptr)); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5936 | else if (!(*Ptr)->getPointeeType()->isObjectType()) |
| 5937 | continue; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5938 | |
| 5939 | // non-volatile version |
| 5940 | QualType ParamTypes[2] = { |
| 5941 | S.Context.getLValueReferenceType(*Ptr), |
| 5942 | isEqualOp ? *Ptr : S.Context.getPointerDiffType(), |
| 5943 | }; |
| 5944 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5945 | /*IsAssigmentOperator=*/ isEqualOp); |
| 5946 | |
| 5947 | if (!S.Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 5948 | VisibleTypeConversionsQuals.hasVolatile()) { |
| 5949 | // volatile version |
| 5950 | ParamTypes[0] = |
| 5951 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
| 5952 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5953 | /*IsAssigmentOperator=*/isEqualOp); |
| 5954 | } |
| 5955 | } |
| 5956 | |
| 5957 | if (isEqualOp) { |
| 5958 | for (BuiltinCandidateTypeSet::iterator |
| 5959 | Ptr = CandidateTypes[1].pointer_begin(), |
| 5960 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 5961 | Ptr != PtrEnd; ++Ptr) { |
| 5962 | // Make sure we don't add the same candidate twice. |
| 5963 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 5964 | continue; |
| 5965 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 5966 | QualType ParamTypes[2] = { |
| 5967 | S.Context.getLValueReferenceType(*Ptr), |
| 5968 | *Ptr, |
| 5969 | }; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5970 | |
| 5971 | // non-volatile version |
| 5972 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5973 | /*IsAssigmentOperator=*/true); |
| 5974 | |
| 5975 | if (!S.Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 5976 | VisibleTypeConversionsQuals.hasVolatile()) { |
| 5977 | // volatile version |
| 5978 | ParamTypes[0] = |
| 5979 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 5980 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 5981 | CandidateSet, /*IsAssigmentOperator=*/true); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5982 | } |
| 5983 | } |
| 5984 | } |
| 5985 | } |
| 5986 | |
| 5987 | // C++ [over.built]p18: |
| 5988 | // |
| 5989 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 5990 | // VQ is either volatile or empty, and R is a promoted |
| 5991 | // arithmetic type, there exist candidate operator functions of |
| 5992 | // the form |
| 5993 | // |
| 5994 | // VQ L& operator=(VQ L&, R); |
| 5995 | // VQ L& operator*=(VQ L&, R); |
| 5996 | // VQ L& operator/=(VQ L&, R); |
| 5997 | // VQ L& operator+=(VQ L&, R); |
| 5998 | // VQ L& operator-=(VQ L&, R); |
| 5999 | void addAssignmentArithmeticOverloads(bool isEqualOp) { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6000 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6001 | return; |
| 6002 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6003 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 6004 | for (unsigned Right = FirstPromotedArithmeticType; |
| 6005 | Right < LastPromotedArithmeticType; ++Right) { |
| 6006 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6007 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6008 | |
| 6009 | // Add this built-in operator as a candidate (VQ is empty). |
| 6010 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6011 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6012 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 6013 | /*IsAssigmentOperator=*/isEqualOp); |
| 6014 | |
| 6015 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 6016 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 6017 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6018 | S.Context.getVolatileType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6019 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6020 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 6021 | CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6022 | /*IsAssigmentOperator=*/isEqualOp); |
| 6023 | } |
| 6024 | } |
| 6025 | } |
| 6026 | |
| 6027 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 6028 | for (BuiltinCandidateTypeSet::iterator |
| 6029 | Vec1 = CandidateTypes[0].vector_begin(), |
| 6030 | Vec1End = CandidateTypes[0].vector_end(); |
| 6031 | Vec1 != Vec1End; ++Vec1) { |
| 6032 | for (BuiltinCandidateTypeSet::iterator |
| 6033 | Vec2 = CandidateTypes[1].vector_begin(), |
| 6034 | Vec2End = CandidateTypes[1].vector_end(); |
| 6035 | Vec2 != Vec2End; ++Vec2) { |
| 6036 | QualType ParamTypes[2]; |
| 6037 | ParamTypes[1] = *Vec2; |
| 6038 | // Add this built-in operator as a candidate (VQ is empty). |
| 6039 | ParamTypes[0] = S.Context.getLValueReferenceType(*Vec1); |
| 6040 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 6041 | /*IsAssigmentOperator=*/isEqualOp); |
| 6042 | |
| 6043 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 6044 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 6045 | ParamTypes[0] = S.Context.getVolatileType(*Vec1); |
| 6046 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6047 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 6048 | CandidateSet, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6049 | /*IsAssigmentOperator=*/isEqualOp); |
| 6050 | } |
| 6051 | } |
| 6052 | } |
| 6053 | } |
| 6054 | |
| 6055 | // C++ [over.built]p22: |
| 6056 | // |
| 6057 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 6058 | // is either volatile or empty, and R is a promoted integral |
| 6059 | // type, there exist candidate operator functions of the form |
| 6060 | // |
| 6061 | // VQ L& operator%=(VQ L&, R); |
| 6062 | // VQ L& operator<<=(VQ L&, R); |
| 6063 | // VQ L& operator>>=(VQ L&, R); |
| 6064 | // VQ L& operator&=(VQ L&, R); |
| 6065 | // VQ L& operator^=(VQ L&, R); |
| 6066 | // VQ L& operator|=(VQ L&, R); |
| 6067 | void addAssignmentIntegralOverloads() { |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6068 | if (!HasArithmeticOrEnumeralCandidateType) |
| 6069 | return; |
| 6070 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6071 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 6072 | for (unsigned Right = FirstPromotedIntegralType; |
| 6073 | Right < LastPromotedIntegralType; ++Right) { |
| 6074 | QualType ParamTypes[2]; |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6075 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6076 | |
| 6077 | // Add this built-in operator as a candidate (VQ is empty). |
| 6078 | ParamTypes[0] = |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6079 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6080 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 6081 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 6082 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Chandler Carruth | 6d69558 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 6083 | ParamTypes[0] = getArithmeticType(Left); |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6084 | ParamTypes[0] = S.Context.getVolatileType(ParamTypes[0]); |
| 6085 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
| 6086 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 6087 | CandidateSet); |
| 6088 | } |
| 6089 | } |
| 6090 | } |
| 6091 | } |
| 6092 | |
| 6093 | // C++ [over.operator]p23: |
| 6094 | // |
| 6095 | // There also exist candidate operator functions of the form |
| 6096 | // |
| 6097 | // bool operator!(bool); |
| 6098 | // bool operator&&(bool, bool); |
| 6099 | // bool operator||(bool, bool); |
| 6100 | void addExclaimOverload() { |
| 6101 | QualType ParamTy = S.Context.BoolTy; |
| 6102 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 6103 | /*IsAssignmentOperator=*/false, |
| 6104 | /*NumContextualBoolArguments=*/1); |
| 6105 | } |
| 6106 | void addAmpAmpOrPipePipeOverload() { |
| 6107 | QualType ParamTypes[2] = { S.Context.BoolTy, S.Context.BoolTy }; |
| 6108 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 6109 | /*IsAssignmentOperator=*/false, |
| 6110 | /*NumContextualBoolArguments=*/2); |
| 6111 | } |
| 6112 | |
| 6113 | // C++ [over.built]p13: |
| 6114 | // |
| 6115 | // For every cv-qualified or cv-unqualified object type T there |
| 6116 | // exist candidate operator functions of the form |
| 6117 | // |
| 6118 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 6119 | // T& operator[](T*, ptrdiff_t); |
| 6120 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 6121 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 6122 | // T& operator[](ptrdiff_t, T*); |
| 6123 | void addSubscriptOverloads() { |
| 6124 | for (BuiltinCandidateTypeSet::iterator |
| 6125 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6126 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6127 | Ptr != PtrEnd; ++Ptr) { |
| 6128 | QualType ParamTypes[2] = { *Ptr, S.Context.getPointerDiffType() }; |
| 6129 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6130 | if (!PointeeType->isObjectType()) |
| 6131 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6132 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6133 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 6134 | |
| 6135 | // T& operator[](T*, ptrdiff_t) |
| 6136 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 6137 | } |
| 6138 | |
| 6139 | for (BuiltinCandidateTypeSet::iterator |
| 6140 | Ptr = CandidateTypes[1].pointer_begin(), |
| 6141 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 6142 | Ptr != PtrEnd; ++Ptr) { |
| 6143 | QualType ParamTypes[2] = { S.Context.getPointerDiffType(), *Ptr }; |
| 6144 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 6145 | if (!PointeeType->isObjectType()) |
| 6146 | continue; |
| 6147 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6148 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 6149 | |
| 6150 | // T& operator[](ptrdiff_t, T*) |
| 6151 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 6152 | } |
| 6153 | } |
| 6154 | |
| 6155 | // C++ [over.built]p11: |
| 6156 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 6157 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 6158 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 6159 | // there exist candidate operator functions of the form |
| 6160 | // |
| 6161 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 6162 | // |
| 6163 | // where CV12 is the union of CV1 and CV2. |
| 6164 | void addArrowStarOverloads() { |
| 6165 | for (BuiltinCandidateTypeSet::iterator |
| 6166 | Ptr = CandidateTypes[0].pointer_begin(), |
| 6167 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 6168 | Ptr != PtrEnd; ++Ptr) { |
| 6169 | QualType C1Ty = (*Ptr); |
| 6170 | QualType C1; |
| 6171 | QualifierCollector Q1; |
| 6172 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
| 6173 | if (!isa<RecordType>(C1)) |
| 6174 | continue; |
| 6175 | // heuristic to reduce number of builtin candidates in the set. |
| 6176 | // Add volatile/restrict version only if there are conversions to a |
| 6177 | // volatile/restrict type. |
| 6178 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 6179 | continue; |
| 6180 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 6181 | continue; |
| 6182 | for (BuiltinCandidateTypeSet::iterator |
| 6183 | MemPtr = CandidateTypes[1].member_pointer_begin(), |
| 6184 | MemPtrEnd = CandidateTypes[1].member_pointer_end(); |
| 6185 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 6186 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 6187 | QualType C2 = QualType(mptr->getClass(), 0); |
| 6188 | C2 = C2.getUnqualifiedType(); |
| 6189 | if (C1 != C2 && !S.IsDerivedFrom(C1, C2)) |
| 6190 | break; |
| 6191 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 6192 | // build CV12 T& |
| 6193 | QualType T = mptr->getPointeeType(); |
| 6194 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 6195 | T.isVolatileQualified()) |
| 6196 | continue; |
| 6197 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 6198 | T.isRestrictQualified()) |
| 6199 | continue; |
| 6200 | T = Q1.apply(S.Context, T); |
| 6201 | QualType ResultTy = S.Context.getLValueReferenceType(T); |
| 6202 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 6203 | } |
| 6204 | } |
| 6205 | } |
| 6206 | |
| 6207 | // Note that we don't consider the first argument, since it has been |
| 6208 | // contextually converted to bool long ago. The candidates below are |
| 6209 | // therefore added as binary. |
| 6210 | // |
| 6211 | // C++ [over.built]p25: |
| 6212 | // For every type T, where T is a pointer, pointer-to-member, or scoped |
| 6213 | // enumeration type, there exist candidate operator functions of the form |
| 6214 | // |
| 6215 | // T operator?(bool, T, T); |
| 6216 | // |
| 6217 | void addConditionalOperatorOverloads() { |
| 6218 | /// Set of (canonical) types that we've already handled. |
| 6219 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 6220 | |
| 6221 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 6222 | for (BuiltinCandidateTypeSet::iterator |
| 6223 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 6224 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 6225 | Ptr != PtrEnd; ++Ptr) { |
| 6226 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 6227 | continue; |
| 6228 | |
| 6229 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 6230 | S.AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 6231 | } |
| 6232 | |
| 6233 | for (BuiltinCandidateTypeSet::iterator |
| 6234 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 6235 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 6236 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 6237 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 6238 | continue; |
| 6239 | |
| 6240 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 6241 | S.AddBuiltinCandidate(*MemPtr, ParamTypes, Args, 2, CandidateSet); |
| 6242 | } |
| 6243 | |
| 6244 | if (S.getLangOptions().CPlusPlus0x) { |
| 6245 | for (BuiltinCandidateTypeSet::iterator |
| 6246 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 6247 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 6248 | Enum != EnumEnd; ++Enum) { |
| 6249 | if (!(*Enum)->getAs<EnumType>()->getDecl()->isScoped()) |
| 6250 | continue; |
| 6251 | |
| 6252 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 6253 | continue; |
| 6254 | |
| 6255 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 6256 | S.AddBuiltinCandidate(*Enum, ParamTypes, Args, 2, CandidateSet); |
| 6257 | } |
| 6258 | } |
| 6259 | } |
| 6260 | } |
| 6261 | }; |
| 6262 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6263 | } // end anonymous namespace |
| 6264 | |
| 6265 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 6266 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 6267 | /// on the operator @p Op and the arguments given. For example, if the |
| 6268 | /// operator is a binary '+', this routine might add "int |
| 6269 | /// operator+(int, int)" to cover integer addition. |
| 6270 | void |
| 6271 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 6272 | SourceLocation OpLoc, |
| 6273 | Expr **Args, unsigned NumArgs, |
| 6274 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6275 | // Find all of the types that the arguments can convert to, but only |
| 6276 | // if the operator we're looking at has built-in operator candidates |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6277 | // that make use of these types. Also record whether we encounter non-record |
| 6278 | // candidate types or either arithmetic or enumeral candidate types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 6279 | Qualifiers VisibleTypeConversionsQuals; |
| 6280 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 6281 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 6282 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6283 | |
| 6284 | bool HasNonRecordCandidateType = false; |
| 6285 | bool HasArithmeticOrEnumeralCandidateType = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6286 | SmallVector<BuiltinCandidateTypeSet, 2> CandidateTypes; |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 6287 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 6288 | CandidateTypes.push_back(BuiltinCandidateTypeSet(*this)); |
| 6289 | CandidateTypes[ArgIdx].AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 6290 | OpLoc, |
| 6291 | true, |
| 6292 | (Op == OO_Exclaim || |
| 6293 | Op == OO_AmpAmp || |
| 6294 | Op == OO_PipePipe), |
| 6295 | VisibleTypeConversionsQuals); |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6296 | HasNonRecordCandidateType = HasNonRecordCandidateType || |
| 6297 | CandidateTypes[ArgIdx].hasNonRecordTypes(); |
| 6298 | HasArithmeticOrEnumeralCandidateType = |
| 6299 | HasArithmeticOrEnumeralCandidateType || |
| 6300 | CandidateTypes[ArgIdx].hasArithmeticOrEnumeralTypes(); |
Douglas Gregor | fec56e7 | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 6301 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6302 | |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6303 | // Exit early when no non-record types have been added to the candidate set |
| 6304 | // for any of the arguments to the operator. |
| 6305 | if (!HasNonRecordCandidateType) |
| 6306 | return; |
| 6307 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6308 | // Setup an object to manage the common state for building overloads. |
| 6309 | BuiltinOperatorOverloadBuilder OpBuilder(*this, Args, NumArgs, |
| 6310 | VisibleTypeConversionsQuals, |
Chandler Carruth | 6a57746 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 6311 | HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6312 | CandidateTypes, CandidateSet); |
| 6313 | |
| 6314 | // Dispatch over the operation to add in only those overloads which apply. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6315 | switch (Op) { |
| 6316 | case OO_None: |
| 6317 | case NUM_OVERLOADED_OPERATORS: |
| 6318 | assert(false && "Expected an overloaded operator"); |
| 6319 | break; |
| 6320 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6321 | case OO_New: |
| 6322 | case OO_Delete: |
| 6323 | case OO_Array_New: |
| 6324 | case OO_Array_Delete: |
| 6325 | case OO_Call: |
| 6326 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
| 6327 | break; |
| 6328 | |
| 6329 | case OO_Comma: |
| 6330 | case OO_Arrow: |
| 6331 | // C++ [over.match.oper]p3: |
| 6332 | // -- For the operator ',', the unary operator '&', or the |
| 6333 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6334 | break; |
| 6335 | |
| 6336 | case OO_Plus: // '+' is either unary or binary |
Chandler Carruth | 32fe0d0 | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 6337 | if (NumArgs == 1) |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6338 | OpBuilder.addUnaryPlusPointerOverloads(); |
Chandler Carruth | 32fe0d0 | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 6339 | // Fall through. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6340 | |
| 6341 | case OO_Minus: // '-' is either unary or binary |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 6342 | if (NumArgs == 1) { |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6343 | OpBuilder.addUnaryPlusOrMinusArithmeticOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 6344 | } else { |
| 6345 | OpBuilder.addBinaryPlusOrMinusPointerOverloads(Op); |
| 6346 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 6347 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6348 | break; |
| 6349 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6350 | case OO_Star: // '*' is either unary or binary |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6351 | if (NumArgs == 1) |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6352 | OpBuilder.addUnaryStarPointerOverloads(); |
| 6353 | else |
| 6354 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 6355 | break; |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6356 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6357 | case OO_Slash: |
| 6358 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 6359 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6360 | |
| 6361 | case OO_PlusPlus: |
| 6362 | case OO_MinusMinus: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6363 | OpBuilder.addPlusPlusMinusMinusArithmeticOverloads(Op); |
| 6364 | OpBuilder.addPlusPlusMinusMinusPointerOverloads(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6365 | break; |
| 6366 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6367 | case OO_EqualEqual: |
| 6368 | case OO_ExclaimEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6369 | OpBuilder.addEqualEqualOrNotEqualMemberPointerOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 6370 | // Fall through. |
Chandler Carruth | c140946 | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 6371 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6372 | case OO_Less: |
| 6373 | case OO_Greater: |
| 6374 | case OO_LessEqual: |
| 6375 | case OO_GreaterEqual: |
Chandler Carruth | 7b80b4b | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6376 | OpBuilder.addRelationalPointerOrEnumeralOverloads(); |
Chandler Carruth | daf55d3 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 6377 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/true); |
| 6378 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6379 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6380 | case OO_Percent: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6381 | case OO_Caret: |
| 6382 | case OO_Pipe: |
| 6383 | case OO_LessLess: |
| 6384 | case OO_GreaterGreater: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6385 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6386 | break; |
| 6387 | |
Chandler Carruth | abb7184 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6388 | case OO_Amp: // '&' is either unary or binary |
| 6389 | if (NumArgs == 1) |
| 6390 | // C++ [over.match.oper]p3: |
| 6391 | // -- For the operator ',', the unary operator '&', or the |
| 6392 | // operator '->', the built-in candidates set is empty. |
| 6393 | break; |
| 6394 | |
| 6395 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
| 6396 | break; |
| 6397 | |
| 6398 | case OO_Tilde: |
| 6399 | OpBuilder.addUnaryTildePromotedIntegralOverloads(); |
| 6400 | break; |
| 6401 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6402 | case OO_Equal: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6403 | OpBuilder.addAssignmentMemberPointerOrEnumeralOverloads(); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6404 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6405 | |
| 6406 | case OO_PlusEqual: |
| 6407 | case OO_MinusEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6408 | OpBuilder.addAssignmentPointerOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6409 | // Fall through. |
| 6410 | |
| 6411 | case OO_StarEqual: |
| 6412 | case OO_SlashEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6413 | OpBuilder.addAssignmentArithmeticOverloads(Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6414 | break; |
| 6415 | |
| 6416 | case OO_PercentEqual: |
| 6417 | case OO_LessLessEqual: |
| 6418 | case OO_GreaterGreaterEqual: |
| 6419 | case OO_AmpEqual: |
| 6420 | case OO_CaretEqual: |
| 6421 | case OO_PipeEqual: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6422 | OpBuilder.addAssignmentIntegralOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6423 | break; |
| 6424 | |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6425 | case OO_Exclaim: |
| 6426 | OpBuilder.addExclaimOverload(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6427 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6428 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6429 | case OO_AmpAmp: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6430 | case OO_PipePipe: |
| 6431 | OpBuilder.addAmpAmpOrPipePipeOverload(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6432 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6433 | |
| 6434 | case OO_Subscript: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6435 | OpBuilder.addSubscriptOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6436 | break; |
| 6437 | |
| 6438 | case OO_ArrowStar: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6439 | OpBuilder.addArrowStarOverloads(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6440 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 6441 | |
| 6442 | case OO_Conditional: |
Chandler Carruth | 3a0f3ef | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6443 | OpBuilder.addConditionalOperatorOverloads(); |
Chandler Carruth | fe62274 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 6444 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 6445 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6446 | } |
| 6447 | } |
| 6448 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6449 | /// \brief Add function candidates found via argument-dependent lookup |
| 6450 | /// to the set of overloading candidates. |
| 6451 | /// |
| 6452 | /// This routine performs argument-dependent name lookup based on the |
| 6453 | /// given function name (which may also be an operator name) and adds |
| 6454 | /// all of the overload candidates found by ADL to the overload |
| 6455 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6456 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6457 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6458 | bool Operator, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6459 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 6460 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6461 | OverloadCandidateSet& CandidateSet, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 6462 | bool PartialOverloading, |
| 6463 | bool StdNamespaceIsAssociated) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6464 | ADLResult Fns; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6465 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 6466 | // FIXME: This approach for uniquing ADL results (and removing |
| 6467 | // redundant candidates from the set) relies on pointer-equality, |
| 6468 | // which means we need to key off the canonical decl. However, |
| 6469 | // always going back to the canonical decl might not get us the |
| 6470 | // right set of default arguments. What default arguments are |
| 6471 | // we supposed to consider on ADL candidates, anyway? |
| 6472 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6473 | // FIXME: Pass in the explicit template arguments? |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 6474 | ArgumentDependentLookup(Name, Operator, Args, NumArgs, Fns, |
| 6475 | StdNamespaceIsAssociated); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6476 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6477 | // Erase all of the candidates we already knew about. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6478 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 6479 | CandEnd = CandidateSet.end(); |
| 6480 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6481 | if (Cand->Function) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6482 | Fns.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6483 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6484 | Fns.erase(FunTmpl); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6485 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6486 | |
| 6487 | // For each of the ADL candidates we found, add it to the overload |
| 6488 | // set. |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6489 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6490 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6491 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6492 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6493 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6494 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6495 | AddOverloadCandidate(FD, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 6496 | false, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6497 | } else |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6498 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6499 | FoundDecl, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 6500 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6501 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6502 | } |
| 6503 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6504 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 6505 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6506 | bool |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6507 | isBetterOverloadCandidate(Sema &S, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 6508 | const OverloadCandidate &Cand1, |
| 6509 | const OverloadCandidate &Cand2, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6510 | SourceLocation Loc, |
| 6511 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6512 | // Define viable functions to be better candidates than non-viable |
| 6513 | // functions. |
| 6514 | if (!Cand2.Viable) |
| 6515 | return Cand1.Viable; |
| 6516 | else if (!Cand1.Viable) |
| 6517 | return false; |
| 6518 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6519 | // C++ [over.match.best]p1: |
| 6520 | // |
| 6521 | // -- if F is a static member function, ICS1(F) is defined such |
| 6522 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 6523 | // any function G, and, symmetrically, ICS1(G) is neither |
| 6524 | // better nor worse than ICS1(F). |
| 6525 | unsigned StartArg = 0; |
| 6526 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 6527 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6528 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6529 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6530 | // A viable function F1 is defined to be a better function than another |
| 6531 | // 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] | 6532 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6533 | unsigned NumArgs = Cand1.Conversions.size(); |
| 6534 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 6535 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6536 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6537 | switch (CompareImplicitConversionSequences(S, |
| 6538 | Cand1.Conversions[ArgIdx], |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6539 | Cand2.Conversions[ArgIdx])) { |
| 6540 | case ImplicitConversionSequence::Better: |
| 6541 | // Cand1 has a better conversion sequence. |
| 6542 | HasBetterConversion = true; |
| 6543 | break; |
| 6544 | |
| 6545 | case ImplicitConversionSequence::Worse: |
| 6546 | // Cand1 can't be better than Cand2. |
| 6547 | return false; |
| 6548 | |
| 6549 | case ImplicitConversionSequence::Indistinguishable: |
| 6550 | // Do nothing. |
| 6551 | break; |
| 6552 | } |
| 6553 | } |
| 6554 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6555 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6556 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6557 | if (HasBetterConversion) |
| 6558 | return true; |
| 6559 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6560 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6561 | // specialization, or, if not that, |
Douglas Gregor | ccd4713 | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 6562 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6563 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 6564 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6565 | |
| 6566 | // -- F1 and F2 are function template specializations, and the function |
| 6567 | // template for F1 is more specialized than the template for F2 |
| 6568 | // 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] | 6569 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 6570 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6571 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6572 | if (FunctionTemplateDecl *BetterTemplate |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6573 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 6574 | Cand2.Function->getPrimaryTemplate(), |
| 6575 | Loc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6576 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 6577 | : TPOC_Call, |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6578 | Cand1.ExplicitCallArguments)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6579 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | dfc331e | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6580 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6581 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6582 | // -- the context is an initialization by user-defined conversion |
| 6583 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 6584 | // from the return type of F1 to the destination type (i.e., |
| 6585 | // the type of the entity being initialized) is a better |
| 6586 | // conversion sequence than the standard conversion sequence |
| 6587 | // from the return type of F2 to the destination type. |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6588 | if (UserDefinedConversion && Cand1.Function && Cand2.Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6589 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6590 | isa<CXXConversionDecl>(Cand2.Function)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6591 | switch (CompareStandardConversionSequences(S, |
| 6592 | Cand1.FinalConversion, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6593 | Cand2.FinalConversion)) { |
| 6594 | case ImplicitConversionSequence::Better: |
| 6595 | // Cand1 has a better conversion sequence. |
| 6596 | return true; |
| 6597 | |
| 6598 | case ImplicitConversionSequence::Worse: |
| 6599 | // Cand1 can't be better than Cand2. |
| 6600 | return false; |
| 6601 | |
| 6602 | case ImplicitConversionSequence::Indistinguishable: |
| 6603 | // Do nothing |
| 6604 | break; |
| 6605 | } |
| 6606 | } |
| 6607 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6608 | return false; |
| 6609 | } |
| 6610 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6611 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6612 | /// within an overload candidate set. |
| 6613 | /// |
| 6614 | /// \param CandidateSet the set of candidate functions. |
| 6615 | /// |
| 6616 | /// \param Loc the location of the function name (or operator symbol) for |
| 6617 | /// which overload resolution occurs. |
| 6618 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6619 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6620 | /// function, Best points to the candidate function found. |
| 6621 | /// |
| 6622 | /// \returns The result of overload resolution. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6623 | OverloadingResult |
| 6624 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 6625 | iterator &Best, |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 6626 | bool UserDefinedConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6627 | // Find the best viable function. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6628 | Best = end(); |
| 6629 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
| 6630 | if (Cand->Viable) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6631 | if (Best == end() || isBetterOverloadCandidate(S, *Cand, *Best, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6632 | UserDefinedConversion)) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6633 | Best = Cand; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6634 | } |
| 6635 | |
| 6636 | // If we didn't find any viable functions, abort. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6637 | if (Best == end()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6638 | return OR_No_Viable_Function; |
| 6639 | |
| 6640 | // Make sure that this function is better than every other viable |
| 6641 | // function. If not, we have an ambiguity. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6642 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6643 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6644 | Cand != Best && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6645 | !isBetterOverloadCandidate(S, *Best, *Cand, Loc, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6646 | UserDefinedConversion)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6647 | Best = end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6648 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6649 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6650 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6651 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6652 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6653 | if (Best->Function && |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 6654 | (Best->Function->isDeleted() || |
| 6655 | S.isFunctionConsideredUnavailable(Best->Function))) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6656 | return OR_Deleted; |
| 6657 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6658 | return OR_Success; |
| 6659 | } |
| 6660 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6661 | namespace { |
| 6662 | |
| 6663 | enum OverloadCandidateKind { |
| 6664 | oc_function, |
| 6665 | oc_method, |
| 6666 | oc_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6667 | oc_function_template, |
| 6668 | oc_method_template, |
| 6669 | oc_constructor_template, |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6670 | oc_implicit_default_constructor, |
| 6671 | oc_implicit_copy_constructor, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 6672 | oc_implicit_move_constructor, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6673 | oc_implicit_copy_assignment, |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 6674 | oc_implicit_move_assignment, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6675 | oc_implicit_inherited_constructor |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6676 | }; |
| 6677 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6678 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 6679 | FunctionDecl *Fn, |
| 6680 | std::string &Description) { |
| 6681 | bool isTemplate = false; |
| 6682 | |
| 6683 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 6684 | isTemplate = true; |
| 6685 | Description = S.getTemplateArgumentBindingsText( |
| 6686 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 6687 | } |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6688 | |
| 6689 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6690 | if (!Ctor->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6691 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6692 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6693 | if (Ctor->getInheritedConstructor()) |
| 6694 | return oc_implicit_inherited_constructor; |
| 6695 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 6696 | if (Ctor->isDefaultConstructor()) |
| 6697 | return oc_implicit_default_constructor; |
| 6698 | |
| 6699 | if (Ctor->isMoveConstructor()) |
| 6700 | return oc_implicit_move_constructor; |
| 6701 | |
| 6702 | assert(Ctor->isCopyConstructor() && |
| 6703 | "unexpected sort of implicit constructor"); |
| 6704 | return oc_implicit_copy_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6705 | } |
| 6706 | |
| 6707 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 6708 | // This actually gets spelled 'candidate function' for now, but |
| 6709 | // it doesn't hurt to split it out. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6710 | if (!Meth->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6711 | return isTemplate ? oc_method_template : oc_method; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6712 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 6713 | if (Meth->isMoveAssignmentOperator()) |
| 6714 | return oc_implicit_move_assignment; |
| 6715 | |
Douglas Gregor | 3e9438b | 2010-09-27 22:37:28 +0000 | [diff] [blame] | 6716 | assert(Meth->isCopyAssignmentOperator() |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6717 | && "implicit method is not copy assignment operator?"); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6718 | return oc_implicit_copy_assignment; |
| 6719 | } |
| 6720 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6721 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6722 | } |
| 6723 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6724 | void MaybeEmitInheritedConstructorNote(Sema &S, FunctionDecl *Fn) { |
| 6725 | const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn); |
| 6726 | if (!Ctor) return; |
| 6727 | |
| 6728 | Ctor = Ctor->getInheritedConstructor(); |
| 6729 | if (!Ctor) return; |
| 6730 | |
| 6731 | S.Diag(Ctor->getLocation(), diag::note_ovl_candidate_inherited_constructor); |
| 6732 | } |
| 6733 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6734 | } // end anonymous namespace |
| 6735 | |
| 6736 | // Notes the location of an overload candidate. |
| 6737 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6738 | std::string FnDesc; |
| 6739 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
| 6740 | Diag(Fn->getLocation(), diag::note_ovl_candidate) |
| 6741 | << (unsigned) K << FnDesc; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6742 | MaybeEmitInheritedConstructorNote(*this, Fn); |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6743 | } |
| 6744 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6745 | //Notes the location of all overload candidates designated through |
| 6746 | // OverloadedExpr |
| 6747 | void Sema::NoteAllOverloadCandidates(Expr* OverloadedExpr) { |
| 6748 | assert(OverloadedExpr->getType() == Context.OverloadTy); |
| 6749 | |
| 6750 | OverloadExpr::FindResult Ovl = OverloadExpr::find(OverloadedExpr); |
| 6751 | OverloadExpr *OvlExpr = Ovl.Expression; |
| 6752 | |
| 6753 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 6754 | IEnd = OvlExpr->decls_end(); |
| 6755 | I != IEnd; ++I) { |
| 6756 | if (FunctionTemplateDecl *FunTmpl = |
| 6757 | dyn_cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()) ) { |
| 6758 | NoteOverloadCandidate(FunTmpl->getTemplatedDecl()); |
| 6759 | } else if (FunctionDecl *Fun |
| 6760 | = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl()) ) { |
| 6761 | NoteOverloadCandidate(Fun); |
| 6762 | } |
| 6763 | } |
| 6764 | } |
| 6765 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6766 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 6767 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 6768 | /// target types of the conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6769 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
| 6770 | Sema &S, |
| 6771 | SourceLocation CaretLoc, |
| 6772 | const PartialDiagnostic &PDiag) const { |
| 6773 | S.Diag(CaretLoc, PDiag) |
| 6774 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6775 | for (AmbiguousConversionSequence::const_iterator |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6776 | I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
| 6777 | S.NoteOverloadCandidate(*I); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6778 | } |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6779 | } |
| 6780 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6781 | namespace { |
| 6782 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6783 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 6784 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 6785 | assert(Conv.isBad()); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6786 | assert(Cand->Function && "for now, candidate must be a function"); |
| 6787 | FunctionDecl *Fn = Cand->Function; |
| 6788 | |
| 6789 | // There's a conversion slot for the object argument if this is a |
| 6790 | // non-constructor method. Note that 'I' corresponds the |
| 6791 | // conversion-slot index. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6792 | bool isObjectArgument = false; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6793 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6794 | if (I == 0) |
| 6795 | isObjectArgument = true; |
| 6796 | else |
| 6797 | I--; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6798 | } |
| 6799 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6800 | std::string FnDesc; |
| 6801 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 6802 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6803 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 6804 | QualType FromTy = Conv.Bad.getFromType(); |
| 6805 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6806 | |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 6807 | if (FromTy == S.Context.OverloadTy) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 6808 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 6809 | Expr *E = FromExpr->IgnoreParens(); |
| 6810 | if (isa<UnaryOperator>(E)) |
| 6811 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6812 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 6813 | |
| 6814 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 6815 | << (unsigned) FnKind << FnDesc |
| 6816 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6817 | << ToTy << Name << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6818 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 6819 | return; |
| 6820 | } |
| 6821 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 6822 | // Do some hand-waving analysis to see if the non-viability is due |
| 6823 | // to a qualifier mismatch. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6824 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 6825 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 6826 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 6827 | CToTy = RT->getPointeeType(); |
| 6828 | else { |
| 6829 | // TODO: detect and diagnose the full richness of const mismatches. |
| 6830 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 6831 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 6832 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 6833 | } |
| 6834 | |
| 6835 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 6836 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
| 6837 | // It is dumb that we have to do this here. |
| 6838 | while (isa<ArrayType>(CFromTy)) |
| 6839 | CFromTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 6840 | while (isa<ArrayType>(CToTy)) |
| 6841 | CToTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 6842 | |
| 6843 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 6844 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 6845 | |
| 6846 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 6847 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 6848 | << (unsigned) FnKind << FnDesc |
| 6849 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6850 | << FromTy |
| 6851 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 6852 | << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6853 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6854 | return; |
| 6855 | } |
| 6856 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6857 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 6858 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6859 | << (unsigned) FnKind << FnDesc |
| 6860 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6861 | << FromTy |
| 6862 | << FromQs.getObjCLifetime() << ToQs.getObjCLifetime() |
| 6863 | << (unsigned) isObjectArgument << I+1; |
| 6864 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 6865 | return; |
| 6866 | } |
| 6867 | |
Douglas Gregor | 028ea4b | 2011-04-26 23:16:46 +0000 | [diff] [blame] | 6868 | if (FromQs.getObjCGCAttr() != ToQs.getObjCGCAttr()) { |
| 6869 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_gc) |
| 6870 | << (unsigned) FnKind << FnDesc |
| 6871 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6872 | << FromTy |
| 6873 | << FromQs.getObjCGCAttr() << ToQs.getObjCGCAttr() |
| 6874 | << (unsigned) isObjectArgument << I+1; |
| 6875 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 6876 | return; |
| 6877 | } |
| 6878 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6879 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 6880 | assert(CVR && "unexpected qualifiers mismatch"); |
| 6881 | |
| 6882 | if (isObjectArgument) { |
| 6883 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 6884 | << (unsigned) FnKind << FnDesc |
| 6885 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6886 | << FromTy << (CVR - 1); |
| 6887 | } else { |
| 6888 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 6889 | << (unsigned) FnKind << FnDesc |
| 6890 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6891 | << FromTy << (CVR - 1) << I+1; |
| 6892 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6893 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6894 | return; |
| 6895 | } |
| 6896 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 6897 | // Diagnose references or pointers to incomplete types differently, |
| 6898 | // since it's far from impossible that the incompleteness triggered |
| 6899 | // the failure. |
| 6900 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 6901 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 6902 | TempFromTy = PTy->getPointeeType(); |
| 6903 | if (TempFromTy->isIncompleteType()) { |
| 6904 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 6905 | << (unsigned) FnKind << FnDesc |
| 6906 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6907 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6908 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 6909 | return; |
| 6910 | } |
| 6911 | |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6912 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6913 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6914 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 6915 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 6916 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 6917 | FromPtrTy->getPointeeType()) && |
| 6918 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 6919 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6920 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6921 | FromPtrTy->getPointeeType())) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6922 | BaseToDerivedConversion = 1; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6923 | } |
| 6924 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 6925 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 6926 | if (const ObjCObjectPointerType *ToPtrTy |
| 6927 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 6928 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 6929 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 6930 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 6931 | FromPtrTy->getPointeeType()) && |
| 6932 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6933 | BaseToDerivedConversion = 2; |
| 6934 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
| 6935 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 6936 | !FromTy->isIncompleteType() && |
| 6937 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 6938 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) |
| 6939 | BaseToDerivedConversion = 3; |
| 6940 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6941 | |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6942 | if (BaseToDerivedConversion) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6943 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6944 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6945 | << (unsigned) FnKind << FnDesc |
| 6946 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6947 | << (BaseToDerivedConversion - 1) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6948 | << FromTy << ToTy << I+1; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6949 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6950 | return; |
| 6951 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6952 | |
Fariborz Jahanian | 909bcb3 | 2011-07-20 17:14:09 +0000 | [diff] [blame] | 6953 | if (isa<ObjCObjectPointerType>(CFromTy) && |
| 6954 | isa<PointerType>(CToTy)) { |
| 6955 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 6956 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 6957 | if (FromQs.getObjCLifetime() != ToQs.getObjCLifetime()) { |
| 6958 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_arc_conv) |
| 6959 | << (unsigned) FnKind << FnDesc |
| 6960 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6961 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 6962 | MaybeEmitInheritedConstructorNote(S, Fn); |
| 6963 | return; |
| 6964 | } |
| 6965 | } |
| 6966 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 6967 | // Emit the generic diagnostic and, optionally, add the hints to it. |
| 6968 | PartialDiagnostic FDiag = S.PDiag(diag::note_ovl_candidate_bad_conv); |
| 6969 | FDiag << (unsigned) FnKind << FnDesc |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6970 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 6971 | << FromTy << ToTy << (unsigned) isObjectArgument << I + 1 |
| 6972 | << (unsigned) (Cand->Fix.Kind); |
| 6973 | |
| 6974 | // If we can fix the conversion, suggest the FixIts. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6975 | for (SmallVector<FixItHint, 1>::iterator |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 6976 | HI = Cand->Fix.Hints.begin(), HE = Cand->Fix.Hints.end(); |
| 6977 | HI != HE; ++HI) |
| 6978 | FDiag << *HI; |
| 6979 | S.Diag(Fn->getLocation(), FDiag); |
| 6980 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6981 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6982 | } |
| 6983 | |
| 6984 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 6985 | unsigned NumFormalArgs) { |
| 6986 | // TODO: treat calls to a missing default constructor as a special case |
| 6987 | |
| 6988 | FunctionDecl *Fn = Cand->Function; |
| 6989 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 6990 | |
| 6991 | unsigned MinParams = Fn->getMinRequiredArguments(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6992 | |
Douglas Gregor | 439d3c3 | 2011-05-05 00:13:13 +0000 | [diff] [blame] | 6993 | // With invalid overloaded operators, it's possible that we think we |
| 6994 | // have an arity mismatch when it fact it looks like we have the |
| 6995 | // right number of arguments, because only overloaded operators have |
| 6996 | // the weird behavior of overloading member and non-member functions. |
| 6997 | // Just don't report anything. |
| 6998 | if (Fn->isInvalidDecl() && |
| 6999 | Fn->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 7000 | return; |
| 7001 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7002 | // at least / at most / exactly |
| 7003 | unsigned mode, modeCount; |
| 7004 | if (NumFormalArgs < MinParams) { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 7005 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 7006 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 7007 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7008 | if (MinParams != FnTy->getNumArgs() || |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 7009 | FnTy->isVariadic() || FnTy->isTemplateVariadic()) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7010 | mode = 0; // "at least" |
| 7011 | else |
| 7012 | mode = 2; // "exactly" |
| 7013 | modeCount = MinParams; |
| 7014 | } else { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 7015 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 7016 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 7017 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7018 | if (MinParams != FnTy->getNumArgs()) |
| 7019 | mode = 1; // "at most" |
| 7020 | else |
| 7021 | mode = 2; // "exactly" |
| 7022 | modeCount = FnTy->getNumArgs(); |
| 7023 | } |
| 7024 | |
| 7025 | std::string Description; |
| 7026 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 7027 | |
| 7028 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7029 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 7030 | << modeCount << NumFormalArgs; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7031 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7032 | } |
| 7033 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7034 | /// Diagnose a failed template-argument deduction. |
| 7035 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
| 7036 | Expr **Args, unsigned NumArgs) { |
| 7037 | FunctionDecl *Fn = Cand->Function; // pattern |
| 7038 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7039 | TemplateParameter Param = Cand->DeductionFailure.getTemplateParameter(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7040 | NamedDecl *ParamD; |
| 7041 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 7042 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 7043 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7044 | switch (Cand->DeductionFailure.Result) { |
| 7045 | case Sema::TDK_Success: |
| 7046 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 7047 | |
| 7048 | case Sema::TDK_Incomplete: { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7049 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| 7050 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_incomplete_deduction) |
| 7051 | << ParamD->getDeclName(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7052 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7053 | return; |
| 7054 | } |
| 7055 | |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 7056 | case Sema::TDK_Underqualified: { |
| 7057 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 7058 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 7059 | |
| 7060 | QualType Param = Cand->DeductionFailure.getFirstArg()->getAsType(); |
| 7061 | |
| 7062 | // Param will have been canonicalized, but it should just be a |
| 7063 | // qualified version of ParamD, so move the qualifiers to that. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 7064 | QualifierCollector Qs; |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 7065 | Qs.strip(Param); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 7066 | QualType NonCanonParam = Qs.apply(S.Context, TParam->getTypeForDecl()); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 7067 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 7068 | |
| 7069 | // Arg has also been canonicalized, but there's nothing we can do |
| 7070 | // about that. It also doesn't matter as much, because it won't |
| 7071 | // have any template parameters in it (because deduction isn't |
| 7072 | // done on dependent types). |
| 7073 | QualType Arg = Cand->DeductionFailure.getSecondArg()->getAsType(); |
| 7074 | |
| 7075 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_underqualified) |
| 7076 | << ParamD->getDeclName() << Arg << NonCanonParam; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7077 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 7078 | return; |
| 7079 | } |
| 7080 | |
| 7081 | case Sema::TDK_Inconsistent: { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7082 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7083 | int which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7084 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7085 | which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7086 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7087 | which = 1; |
| 7088 | else { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7089 | which = 2; |
| 7090 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7091 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7092 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_inconsistent_deduction) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7093 | << which << ParamD->getDeclName() |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7094 | << *Cand->DeductionFailure.getFirstArg() |
| 7095 | << *Cand->DeductionFailure.getSecondArg(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7096 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 7097 | return; |
| 7098 | } |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 7099 | |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7100 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7101 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7102 | if (ParamD->getDeclName()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7103 | S.Diag(Fn->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7104 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
| 7105 | << ParamD->getDeclName(); |
| 7106 | else { |
| 7107 | int index = 0; |
| 7108 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 7109 | index = TTP->getIndex(); |
| 7110 | else if (NonTypeTemplateParmDecl *NTTP |
| 7111 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 7112 | index = NTTP->getIndex(); |
| 7113 | else |
| 7114 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7115 | S.Diag(Fn->getLocation(), |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7116 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
| 7117 | << (index + 1); |
| 7118 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7119 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 7120 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7121 | |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 7122 | case Sema::TDK_TooManyArguments: |
| 7123 | case Sema::TDK_TooFewArguments: |
| 7124 | DiagnoseArityMismatch(S, Cand, NumArgs); |
| 7125 | return; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 7126 | |
| 7127 | case Sema::TDK_InstantiationDepth: |
| 7128 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_instantiation_depth); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7129 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 7130 | return; |
| 7131 | |
| 7132 | case Sema::TDK_SubstitutionFailure: { |
| 7133 | std::string ArgString; |
| 7134 | if (TemplateArgumentList *Args |
| 7135 | = Cand->DeductionFailure.getTemplateArgumentList()) |
| 7136 | ArgString = S.getTemplateArgumentBindingsText( |
| 7137 | Fn->getDescribedFunctionTemplate()->getTemplateParameters(), |
| 7138 | *Args); |
| 7139 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_substitution_failure) |
| 7140 | << ArgString; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7141 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 7142 | return; |
| 7143 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7144 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7145 | // TODO: diagnose these individually, then kill off |
| 7146 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7147 | case Sema::TDK_NonDeducedMismatch: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7148 | case Sema::TDK_FailedOverloadResolution: |
| 7149 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7150 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7151 | return; |
| 7152 | } |
| 7153 | } |
| 7154 | |
| 7155 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 7156 | /// already generated a primary error at the call site. |
| 7157 | /// |
| 7158 | /// It really does need to be a single diagnostic with its caret |
| 7159 | /// pointed at the candidate declaration. Yes, this creates some |
| 7160 | /// major challenges of technical writing. Yes, this makes pointing |
| 7161 | /// out problems with specific arguments quite awkward. It's still |
| 7162 | /// better than generating twenty screens of text for every failed |
| 7163 | /// overload. |
| 7164 | /// |
| 7165 | /// It would be great to be able to express per-candidate problems |
| 7166 | /// more richly for those diagnostic clients that cared, but we'd |
| 7167 | /// still have to be just as careful with the default diagnostics. |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7168 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| 7169 | Expr **Args, unsigned NumArgs) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7170 | FunctionDecl *Fn = Cand->Function; |
| 7171 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7172 | // Note deleted candidates, but only if they're viable. |
Argyrios Kyrtzidis | 572bbec | 2011-06-23 00:41:50 +0000 | [diff] [blame] | 7173 | if (Cand->Viable && (Fn->isDeleted() || |
| 7174 | S.isFunctionConsideredUnavailable(Fn))) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7175 | std::string FnDesc; |
| 7176 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 7177 | |
| 7178 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7179 | << FnKind << FnDesc << Fn->isDeleted(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7180 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7181 | return; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7182 | } |
| 7183 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7184 | // We don't really have anything else to say about viable candidates. |
| 7185 | if (Cand->Viable) { |
| 7186 | S.NoteOverloadCandidate(Fn); |
| 7187 | return; |
| 7188 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7189 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7190 | switch (Cand->FailureKind) { |
| 7191 | case ovl_fail_too_many_arguments: |
| 7192 | case ovl_fail_too_few_arguments: |
| 7193 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7194 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7195 | case ovl_fail_bad_deduction: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 7196 | return DiagnoseBadDeduction(S, Cand, Args, NumArgs); |
| 7197 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7198 | case ovl_fail_trivial_conversion: |
| 7199 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 7200 | case ovl_fail_final_conversion_not_exact: |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7201 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7202 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 7203 | case ovl_fail_bad_conversion: { |
| 7204 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
| 7205 | for (unsigned N = Cand->Conversions.size(); I != N; ++I) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7206 | if (Cand->Conversions[I].isBad()) |
| 7207 | return DiagnoseBadConversion(S, Cand, I); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7208 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 7209 | // FIXME: this currently happens when we're called from SemaInit |
| 7210 | // when user-conversion overload fails. Figure out how to handle |
| 7211 | // those conditions and diagnose them well. |
| 7212 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 7213 | } |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 7214 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7215 | } |
| 7216 | |
| 7217 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 7218 | // Desugar the type of the surrogate down to a function type, |
| 7219 | // retaining as many typedefs as possible while still showing |
| 7220 | // the function type (and, therefore, its parameter types). |
| 7221 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 7222 | bool isLValueReference = false; |
| 7223 | bool isRValueReference = false; |
| 7224 | bool isPointer = false; |
| 7225 | if (const LValueReferenceType *FnTypeRef = |
| 7226 | FnType->getAs<LValueReferenceType>()) { |
| 7227 | FnType = FnTypeRef->getPointeeType(); |
| 7228 | isLValueReference = true; |
| 7229 | } else if (const RValueReferenceType *FnTypeRef = |
| 7230 | FnType->getAs<RValueReferenceType>()) { |
| 7231 | FnType = FnTypeRef->getPointeeType(); |
| 7232 | isRValueReference = true; |
| 7233 | } |
| 7234 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 7235 | FnType = FnTypePtr->getPointeeType(); |
| 7236 | isPointer = true; |
| 7237 | } |
| 7238 | // Desugar down to a function type. |
| 7239 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 7240 | // Reconstruct the pointer/reference as appropriate. |
| 7241 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 7242 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 7243 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 7244 | |
| 7245 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 7246 | << FnType; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7247 | MaybeEmitInheritedConstructorNote(S, Cand->Surrogate); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7248 | } |
| 7249 | |
| 7250 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 7251 | const char *Opc, |
| 7252 | SourceLocation OpLoc, |
| 7253 | OverloadCandidate *Cand) { |
| 7254 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
| 7255 | std::string TypeStr("operator"); |
| 7256 | TypeStr += Opc; |
| 7257 | TypeStr += "("; |
| 7258 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 7259 | if (Cand->Conversions.size() == 1) { |
| 7260 | TypeStr += ")"; |
| 7261 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 7262 | } else { |
| 7263 | TypeStr += ", "; |
| 7264 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 7265 | TypeStr += ")"; |
| 7266 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 7267 | } |
| 7268 | } |
| 7269 | |
| 7270 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 7271 | OverloadCandidate *Cand) { |
| 7272 | unsigned NoOperands = Cand->Conversions.size(); |
| 7273 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 7274 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7275 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 7276 | if (!ICS.isAmbiguous()) continue; |
| 7277 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7278 | ICS.DiagnoseAmbiguousConversion(S, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7279 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7280 | } |
| 7281 | } |
| 7282 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 7283 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 7284 | if (Cand->Function) |
| 7285 | return Cand->Function->getLocation(); |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 7286 | if (Cand->IsSurrogate) |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 7287 | return Cand->Surrogate->getLocation(); |
| 7288 | return SourceLocation(); |
| 7289 | } |
| 7290 | |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 7291 | static unsigned |
| 7292 | RankDeductionFailure(const OverloadCandidate::DeductionFailureInfo &DFI) { |
Chandler Carruth | 78bf680 | 2011-09-10 00:51:24 +0000 | [diff] [blame] | 7293 | switch ((Sema::TemplateDeductionResult)DFI.Result) { |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 7294 | case Sema::TDK_Success: |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 7295 | assert(0 && "TDK_success while diagnosing bad deduction"); |
| 7296 | |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 7297 | case Sema::TDK_Incomplete: |
| 7298 | return 1; |
| 7299 | |
| 7300 | case Sema::TDK_Underqualified: |
| 7301 | case Sema::TDK_Inconsistent: |
| 7302 | return 2; |
| 7303 | |
| 7304 | case Sema::TDK_SubstitutionFailure: |
| 7305 | case Sema::TDK_NonDeducedMismatch: |
| 7306 | return 3; |
| 7307 | |
| 7308 | case Sema::TDK_InstantiationDepth: |
| 7309 | case Sema::TDK_FailedOverloadResolution: |
| 7310 | return 4; |
| 7311 | |
| 7312 | case Sema::TDK_InvalidExplicitArguments: |
| 7313 | return 5; |
| 7314 | |
| 7315 | case Sema::TDK_TooManyArguments: |
| 7316 | case Sema::TDK_TooFewArguments: |
| 7317 | return 6; |
| 7318 | } |
Benjamin Kramer | afc5b15 | 2011-09-10 21:52:04 +0000 | [diff] [blame] | 7319 | llvm_unreachable("Unhandled deduction result"); |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 7320 | } |
| 7321 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 7322 | struct CompareOverloadCandidatesForDisplay { |
| 7323 | Sema &S; |
| 7324 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7325 | |
| 7326 | bool operator()(const OverloadCandidate *L, |
| 7327 | const OverloadCandidate *R) { |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 7328 | // Fast-path this check. |
| 7329 | if (L == R) return false; |
| 7330 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7331 | // Order first by viability. |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 7332 | if (L->Viable) { |
| 7333 | if (!R->Viable) return true; |
| 7334 | |
| 7335 | // TODO: introduce a tri-valued comparison for overload |
| 7336 | // candidates. Would be more worthwhile if we had a sort |
| 7337 | // that could exploit it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7338 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation())) return true; |
| 7339 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation())) return false; |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 7340 | } else if (R->Viable) |
| 7341 | return false; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7342 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 7343 | assert(L->Viable == R->Viable); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7344 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 7345 | // Criteria by which we can sort non-viable candidates: |
| 7346 | if (!L->Viable) { |
| 7347 | // 1. Arity mismatches come after other candidates. |
| 7348 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 7349 | L->FailureKind == ovl_fail_too_few_arguments) |
| 7350 | return false; |
| 7351 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 7352 | R->FailureKind == ovl_fail_too_few_arguments) |
| 7353 | return true; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7354 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7355 | // 2. Bad conversions come first and are ordered by the number |
| 7356 | // of bad conversions and quality of good conversions. |
| 7357 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 7358 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 7359 | return true; |
| 7360 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7361 | // The conversion that can be fixed with a smaller number of changes, |
| 7362 | // comes first. |
| 7363 | unsigned numLFixes = L->Fix.NumConversionsFixed; |
| 7364 | unsigned numRFixes = R->Fix.NumConversionsFixed; |
| 7365 | numLFixes = (numLFixes == 0) ? UINT_MAX : numLFixes; |
| 7366 | numRFixes = (numRFixes == 0) ? UINT_MAX : numRFixes; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 7367 | if (numLFixes != numRFixes) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7368 | if (numLFixes < numRFixes) |
| 7369 | return true; |
| 7370 | else |
| 7371 | return false; |
Anna Zaks | ffe9edd | 2011-07-21 00:34:39 +0000 | [diff] [blame] | 7372 | } |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7373 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7374 | // If there's any ordering between the defined conversions... |
| 7375 | // FIXME: this might not be transitive. |
| 7376 | assert(L->Conversions.size() == R->Conversions.size()); |
| 7377 | |
| 7378 | int leftBetter = 0; |
John McCall | 3a81337 | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 7379 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
| 7380 | for (unsigned E = L->Conversions.size(); I != E; ++I) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7381 | switch (CompareImplicitConversionSequences(S, |
| 7382 | L->Conversions[I], |
| 7383 | R->Conversions[I])) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7384 | case ImplicitConversionSequence::Better: |
| 7385 | leftBetter++; |
| 7386 | break; |
| 7387 | |
| 7388 | case ImplicitConversionSequence::Worse: |
| 7389 | leftBetter--; |
| 7390 | break; |
| 7391 | |
| 7392 | case ImplicitConversionSequence::Indistinguishable: |
| 7393 | break; |
| 7394 | } |
| 7395 | } |
| 7396 | if (leftBetter > 0) return true; |
| 7397 | if (leftBetter < 0) return false; |
| 7398 | |
| 7399 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 7400 | return false; |
| 7401 | |
Kaelyn Uhrain | fd641f9 | 2011-09-09 21:58:49 +0000 | [diff] [blame] | 7402 | if (L->FailureKind == ovl_fail_bad_deduction) { |
| 7403 | if (R->FailureKind != ovl_fail_bad_deduction) |
| 7404 | return true; |
| 7405 | |
| 7406 | if (L->DeductionFailure.Result != R->DeductionFailure.Result) |
| 7407 | return RankDeductionFailure(L->DeductionFailure) |
| 7408 | <= RankDeductionFailure(R->DeductionFailure); |
| 7409 | } |
| 7410 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 7411 | // TODO: others? |
| 7412 | } |
| 7413 | |
| 7414 | // Sort everything else by location. |
| 7415 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 7416 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 7417 | |
| 7418 | // Put candidates without locations (e.g. builtins) at the end. |
| 7419 | if (LLoc.isInvalid()) return false; |
| 7420 | if (RLoc.isInvalid()) return true; |
| 7421 | |
| 7422 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7423 | } |
| 7424 | }; |
| 7425 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7426 | /// CompleteNonViableCandidate - Normally, overload resolution only |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7427 | /// computes up to the first. Produces the FixIt set if possible. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7428 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
| 7429 | Expr **Args, unsigned NumArgs) { |
| 7430 | assert(!Cand->Viable); |
| 7431 | |
| 7432 | // Don't do anything on failures other than bad conversion. |
| 7433 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 7434 | |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7435 | // We only want the FixIts if all the arguments can be corrected. |
| 7436 | bool Unfixable = false; |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 7437 | // Use a implicit copy initialization to check conversion fixes. |
| 7438 | Cand->Fix.setConversionChecker(TryCopyInitialization); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7439 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7440 | // Skip forward to the first bad conversion. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 7441 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7442 | unsigned ConvCount = Cand->Conversions.size(); |
| 7443 | while (true) { |
| 7444 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 7445 | ConvIdx++; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7446 | if (Cand->Conversions[ConvIdx - 1].isBad()) { |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 7447 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx - 1, S); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7448 | break; |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7449 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7450 | } |
| 7451 | |
| 7452 | if (ConvIdx == ConvCount) |
| 7453 | return; |
| 7454 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 7455 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 7456 | "remaining conversion is initialized?"); |
| 7457 | |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 7458 | // FIXME: this should probably be preserved from the overload |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7459 | // operation somehow. |
| 7460 | bool SuppressUserConversions = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7461 | |
| 7462 | const FunctionProtoType* Proto; |
| 7463 | unsigned ArgIdx = ConvIdx; |
| 7464 | |
| 7465 | if (Cand->IsSurrogate) { |
| 7466 | QualType ConvType |
| 7467 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 7468 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 7469 | ConvType = ConvPtrType->getPointeeType(); |
| 7470 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 7471 | ArgIdx--; |
| 7472 | } else if (Cand->Function) { |
| 7473 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 7474 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 7475 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 7476 | ArgIdx--; |
| 7477 | } else { |
| 7478 | // Builtin binary operator with a bad first conversion. |
| 7479 | assert(ConvCount <= 3); |
| 7480 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 7481 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7482 | = TryCopyInitialization(S, Args[ConvIdx], |
| 7483 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7484 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7485 | /*InOverloadResolution*/ true, |
| 7486 | /*AllowObjCWritebackConversion=*/ |
| 7487 | S.getLangOptions().ObjCAutoRefCount); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7488 | return; |
| 7489 | } |
| 7490 | |
| 7491 | // Fill in the rest of the conversions. |
| 7492 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 7493 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7494 | if (ArgIdx < NumArgsInProto) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7495 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7496 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7497 | SuppressUserConversions, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7498 | /*InOverloadResolution=*/true, |
| 7499 | /*AllowObjCWritebackConversion=*/ |
| 7500 | S.getLangOptions().ObjCAutoRefCount); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7501 | // Store the FixIt in the candidate if it exists. |
| 7502 | if (!Unfixable && Cand->Conversions[ConvIdx].isBad()) |
Anna Zaks | f3546ee | 2011-07-28 19:46:48 +0000 | [diff] [blame] | 7503 | Unfixable = !Cand->TryToFixBadConversion(ConvIdx, S); |
Anna Zaks | b89fe6b | 2011-07-19 19:49:12 +0000 | [diff] [blame] | 7504 | } |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7505 | else |
| 7506 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 7507 | } |
| 7508 | } |
| 7509 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7510 | } // end anonymous namespace |
| 7511 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7512 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 7513 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7514 | /// set. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7515 | void OverloadCandidateSet::NoteCandidates(Sema &S, |
| 7516 | OverloadCandidateDisplayKind OCD, |
| 7517 | Expr **Args, unsigned NumArgs, |
| 7518 | const char *Opc, |
| 7519 | SourceLocation OpLoc) { |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7520 | // Sort the candidates by viability and position. Sorting directly would |
| 7521 | // be prohibitive, so we make a set of pointers and sort those. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7522 | SmallVector<OverloadCandidate*, 32> Cands; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7523 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
| 7524 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7525 | if (Cand->Viable) |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7526 | Cands.push_back(Cand); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7527 | else if (OCD == OCD_AllCandidates) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7528 | CompleteNonViableCandidate(S, Cand, Args, NumArgs); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7529 | if (Cand->Function || Cand->IsSurrogate) |
| 7530 | Cands.push_back(Cand); |
| 7531 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 7532 | // want to list every possible builtin candidate. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7533 | } |
| 7534 | } |
| 7535 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 7536 | std::sort(Cands.begin(), Cands.end(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7537 | CompareOverloadCandidatesForDisplay(S)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7538 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7539 | bool ReportedAmbiguousConversions = false; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7540 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7541 | SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7542 | const Diagnostic::OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7543 | unsigned CandsShown = 0; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7544 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 7545 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 7546 | |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7547 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 7548 | // the user with. FIXME: This limit should depend on details of the |
| 7549 | // candidate list. |
| 7550 | if (CandsShown >= 4 && ShowOverloads == Diagnostic::Ovl_Best) { |
| 7551 | break; |
| 7552 | } |
| 7553 | ++CandsShown; |
| 7554 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7555 | if (Cand->Function) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7556 | NoteFunctionCandidate(S, Cand, Args, NumArgs); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7557 | else if (Cand->IsSurrogate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7558 | NoteSurrogateCandidate(S, Cand); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7559 | else { |
| 7560 | assert(Cand->Viable && |
| 7561 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7562 | // Generally we only see ambiguities including viable builtin |
| 7563 | // operators if overload resolution got screwed up by an |
| 7564 | // ambiguous user-defined conversion. |
| 7565 | // |
| 7566 | // FIXME: It's quite possible for different conversions to see |
| 7567 | // different ambiguities, though. |
| 7568 | if (!ReportedAmbiguousConversions) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7569 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7570 | ReportedAmbiguousConversions = true; |
| 7571 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7572 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7573 | // If this is a viable builtin, print it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7574 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7575 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7576 | } |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7577 | |
| 7578 | if (I != E) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7579 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7580 | } |
| 7581 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7582 | // [PossiblyAFunctionType] --> [Return] |
| 7583 | // NonFunctionType --> NonFunctionType |
| 7584 | // R (A) --> R(A) |
| 7585 | // R (*)(A) --> R (A) |
| 7586 | // R (&)(A) --> R (A) |
| 7587 | // R (S::*)(A) --> R (A) |
| 7588 | QualType Sema::ExtractUnqualifiedFunctionType(QualType PossiblyAFunctionType) { |
| 7589 | QualType Ret = PossiblyAFunctionType; |
| 7590 | if (const PointerType *ToTypePtr = |
| 7591 | PossiblyAFunctionType->getAs<PointerType>()) |
| 7592 | Ret = ToTypePtr->getPointeeType(); |
| 7593 | else if (const ReferenceType *ToTypeRef = |
| 7594 | PossiblyAFunctionType->getAs<ReferenceType>()) |
| 7595 | Ret = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 7596 | else if (const MemberPointerType *MemTypePtr = |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7597 | PossiblyAFunctionType->getAs<MemberPointerType>()) |
| 7598 | Ret = MemTypePtr->getPointeeType(); |
| 7599 | Ret = |
| 7600 | Context.getCanonicalType(Ret).getUnqualifiedType(); |
| 7601 | return Ret; |
| 7602 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7603 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7604 | // A helper class to help with address of function resolution |
| 7605 | // - allows us to avoid passing around all those ugly parameters |
| 7606 | class AddressOfFunctionResolver |
| 7607 | { |
| 7608 | Sema& S; |
| 7609 | Expr* SourceExpr; |
| 7610 | const QualType& TargetType; |
| 7611 | QualType TargetFunctionType; // Extracted function type from target type |
| 7612 | |
| 7613 | bool Complain; |
| 7614 | //DeclAccessPair& ResultFunctionAccessPair; |
| 7615 | ASTContext& Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7616 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7617 | bool TargetTypeIsNonStaticMemberFunction; |
| 7618 | bool FoundNonTemplateFunction; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7619 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7620 | OverloadExpr::FindResult OvlExprInfo; |
| 7621 | OverloadExpr *OvlExpr; |
| 7622 | TemplateArgumentListInfo OvlExplicitTemplateArgs; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7623 | SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7624 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7625 | public: |
| 7626 | AddressOfFunctionResolver(Sema &S, Expr* SourceExpr, |
| 7627 | const QualType& TargetType, bool Complain) |
| 7628 | : S(S), SourceExpr(SourceExpr), TargetType(TargetType), |
| 7629 | Complain(Complain), Context(S.getASTContext()), |
| 7630 | TargetTypeIsNonStaticMemberFunction( |
| 7631 | !!TargetType->getAs<MemberPointerType>()), |
| 7632 | FoundNonTemplateFunction(false), |
| 7633 | OvlExprInfo(OverloadExpr::find(SourceExpr)), |
| 7634 | OvlExpr(OvlExprInfo.Expression) |
| 7635 | { |
| 7636 | ExtractUnqualifiedFunctionTypeFromTargetType(); |
| 7637 | |
| 7638 | if (!TargetFunctionType->isFunctionType()) { |
| 7639 | if (OvlExpr->hasExplicitTemplateArgs()) { |
| 7640 | DeclAccessPair dap; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7641 | if (FunctionDecl* Fn = S.ResolveSingleFunctionTemplateSpecialization( |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7642 | OvlExpr, false, &dap) ) { |
Chandler Carruth | 9043423 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 7643 | |
| 7644 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 7645 | if (!Method->isStatic()) { |
| 7646 | // If the target type is a non-function type and the function |
| 7647 | // found is a non-static member function, pretend as if that was |
| 7648 | // the target, it's the only possible type to end up with. |
| 7649 | TargetTypeIsNonStaticMemberFunction = true; |
| 7650 | |
| 7651 | // And skip adding the function if its not in the proper form. |
| 7652 | // We'll diagnose this due to an empty set of functions. |
| 7653 | if (!OvlExprInfo.HasFormOfMemberPointer) |
| 7654 | return; |
| 7655 | } |
| 7656 | } |
| 7657 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7658 | Matches.push_back(std::make_pair(dap,Fn)); |
| 7659 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 7660 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7661 | return; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 7662 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7663 | |
| 7664 | if (OvlExpr->hasExplicitTemplateArgs()) |
| 7665 | OvlExpr->getExplicitTemplateArgs().copyInto(OvlExplicitTemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7666 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7667 | if (FindAllFunctionsThatMatchTargetTypeExactly()) { |
| 7668 | // C++ [over.over]p4: |
| 7669 | // If more than one function is selected, [...] |
| 7670 | if (Matches.size() > 1) { |
| 7671 | if (FoundNonTemplateFunction) |
| 7672 | EliminateAllTemplateMatches(); |
| 7673 | else |
| 7674 | EliminateAllExceptMostSpecializedTemplate(); |
| 7675 | } |
| 7676 | } |
| 7677 | } |
| 7678 | |
| 7679 | private: |
| 7680 | bool isTargetTypeAFunction() const { |
| 7681 | return TargetFunctionType->isFunctionType(); |
| 7682 | } |
| 7683 | |
| 7684 | // [ToType] [Return] |
| 7685 | |
| 7686 | // R (*)(A) --> R (A), IsNonStaticMemberFunction = false |
| 7687 | // R (&)(A) --> R (A), IsNonStaticMemberFunction = false |
| 7688 | // R (S::*)(A) --> R (A), IsNonStaticMemberFunction = true |
| 7689 | void inline ExtractUnqualifiedFunctionTypeFromTargetType() { |
| 7690 | TargetFunctionType = S.ExtractUnqualifiedFunctionType(TargetType); |
| 7691 | } |
| 7692 | |
| 7693 | // return true if any matching specializations were found |
| 7694 | bool AddMatchingTemplateFunction(FunctionTemplateDecl* FunctionTemplate, |
| 7695 | const DeclAccessPair& CurAccessFunPair) { |
| 7696 | if (CXXMethodDecl *Method |
| 7697 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
| 7698 | // Skip non-static function templates when converting to pointer, and |
| 7699 | // static when converting to member pointer. |
| 7700 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 7701 | return false; |
| 7702 | } |
| 7703 | else if (TargetTypeIsNonStaticMemberFunction) |
| 7704 | return false; |
| 7705 | |
| 7706 | // C++ [over.over]p2: |
| 7707 | // If the name is a function template, template argument deduction is |
| 7708 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 7709 | // resulting template argument list is used to generate a single |
| 7710 | // function template specialization, which is added to the set of |
| 7711 | // overloaded functions considered. |
| 7712 | FunctionDecl *Specialization = 0; |
| 7713 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
| 7714 | if (Sema::TemplateDeductionResult Result |
| 7715 | = S.DeduceTemplateArguments(FunctionTemplate, |
| 7716 | &OvlExplicitTemplateArgs, |
| 7717 | TargetFunctionType, Specialization, |
| 7718 | Info)) { |
| 7719 | // FIXME: make a note of the failed deduction for diagnostics. |
| 7720 | (void)Result; |
| 7721 | return false; |
| 7722 | } |
| 7723 | |
| 7724 | // Template argument deduction ensures that we have an exact match. |
| 7725 | // This function template specicalization works. |
| 7726 | Specialization = cast<FunctionDecl>(Specialization->getCanonicalDecl()); |
| 7727 | assert(TargetFunctionType |
| 7728 | == Context.getCanonicalType(Specialization->getType())); |
| 7729 | Matches.push_back(std::make_pair(CurAccessFunPair, Specialization)); |
| 7730 | return true; |
| 7731 | } |
| 7732 | |
| 7733 | bool AddMatchingNonTemplateFunction(NamedDecl* Fn, |
| 7734 | const DeclAccessPair& CurAccessFunPair) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 7735 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 7736 | // Skip non-static functions when converting to pointer, and static |
| 7737 | // when converting to member pointer. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7738 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 7739 | return false; |
| 7740 | } |
| 7741 | else if (TargetTypeIsNonStaticMemberFunction) |
| 7742 | return false; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7743 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 7744 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 7745 | QualType ResultTy; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7746 | if (Context.hasSameUnqualifiedType(TargetFunctionType, |
| 7747 | FunDecl->getType()) || |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 7748 | S.IsNoReturnConversion(FunDecl->getType(), TargetFunctionType, |
| 7749 | ResultTy)) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7750 | Matches.push_back(std::make_pair(CurAccessFunPair, |
| 7751 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 7752 | FoundNonTemplateFunction = true; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7753 | return true; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 7754 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7755 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7756 | |
| 7757 | return false; |
| 7758 | } |
| 7759 | |
| 7760 | bool FindAllFunctionsThatMatchTargetTypeExactly() { |
| 7761 | bool Ret = false; |
| 7762 | |
| 7763 | // If the overload expression doesn't have the form of a pointer to |
| 7764 | // member, don't try to convert it to a pointer-to-member type. |
| 7765 | if (IsInvalidFormOfPointerToMemberFunction()) |
| 7766 | return false; |
| 7767 | |
| 7768 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 7769 | E = OvlExpr->decls_end(); |
| 7770 | I != E; ++I) { |
| 7771 | // Look through any using declarations to find the underlying function. |
| 7772 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 7773 | |
| 7774 | // C++ [over.over]p3: |
| 7775 | // Non-member functions and static member functions match |
| 7776 | // targets of type "pointer-to-function" or "reference-to-function." |
| 7777 | // Nonstatic member functions match targets of |
| 7778 | // type "pointer-to-member-function." |
| 7779 | // Note that according to DR 247, the containing class does not matter. |
| 7780 | if (FunctionTemplateDecl *FunctionTemplate |
| 7781 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
| 7782 | if (AddMatchingTemplateFunction(FunctionTemplate, I.getPair())) |
| 7783 | Ret = true; |
| 7784 | } |
| 7785 | // If we have explicit template arguments supplied, skip non-templates. |
| 7786 | else if (!OvlExpr->hasExplicitTemplateArgs() && |
| 7787 | AddMatchingNonTemplateFunction(Fn, I.getPair())) |
| 7788 | Ret = true; |
| 7789 | } |
| 7790 | assert(Ret || Matches.empty()); |
| 7791 | return Ret; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7792 | } |
| 7793 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7794 | void EliminateAllExceptMostSpecializedTemplate() { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7795 | // [...] and any given function template specialization F1 is |
| 7796 | // eliminated if the set contains a second function template |
| 7797 | // specialization whose function template is more specialized |
| 7798 | // than the function template of F1 according to the partial |
| 7799 | // ordering rules of 14.5.5.2. |
| 7800 | |
| 7801 | // The algorithm specified above is quadratic. We instead use a |
| 7802 | // two-pass algorithm (similar to the one used to identify the |
| 7803 | // best viable function in an overload set) that identifies the |
| 7804 | // best function template (if it exists). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7805 | |
| 7806 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 7807 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 7808 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7809 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7810 | UnresolvedSetIterator Result = |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7811 | S.getMostSpecialized(MatchesCopy.begin(), MatchesCopy.end(), |
| 7812 | TPOC_Other, 0, SourceExpr->getLocStart(), |
| 7813 | S.PDiag(), |
| 7814 | S.PDiag(diag::err_addr_ovl_ambiguous) |
| 7815 | << Matches[0].second->getDeclName(), |
| 7816 | S.PDiag(diag::note_ovl_candidate) |
| 7817 | << (unsigned) oc_function_template, |
| 7818 | Complain); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7819 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7820 | if (Result != MatchesCopy.end()) { |
| 7821 | // Make it the first and only element |
| 7822 | Matches[0].first = Matches[Result - MatchesCopy.begin()].first; |
| 7823 | Matches[0].second = cast<FunctionDecl>(*Result); |
| 7824 | Matches.resize(1); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7825 | } |
| 7826 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7827 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7828 | void EliminateAllTemplateMatches() { |
| 7829 | // [...] any function template specializations in the set are |
| 7830 | // eliminated if the set also contains a non-template function, [...] |
| 7831 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
| 7832 | if (Matches[I].second->getPrimaryTemplate() == 0) |
| 7833 | ++I; |
| 7834 | else { |
| 7835 | Matches[I] = Matches[--N]; |
| 7836 | Matches.set_size(N); |
| 7837 | } |
| 7838 | } |
| 7839 | } |
| 7840 | |
| 7841 | public: |
| 7842 | void ComplainNoMatchesFound() const { |
| 7843 | assert(Matches.empty()); |
| 7844 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_no_viable) |
| 7845 | << OvlExpr->getName() << TargetFunctionType |
| 7846 | << OvlExpr->getSourceRange(); |
| 7847 | S.NoteAllOverloadCandidates(OvlExpr); |
| 7848 | } |
| 7849 | |
| 7850 | bool IsInvalidFormOfPointerToMemberFunction() const { |
| 7851 | return TargetTypeIsNonStaticMemberFunction && |
| 7852 | !OvlExprInfo.HasFormOfMemberPointer; |
| 7853 | } |
| 7854 | |
| 7855 | void ComplainIsInvalidFormOfPointerToMemberFunction() const { |
| 7856 | // TODO: Should we condition this on whether any functions might |
| 7857 | // have matched, or is it more appropriate to do that in callers? |
| 7858 | // TODO: a fixit wouldn't hurt. |
| 7859 | S.Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
| 7860 | << TargetType << OvlExpr->getSourceRange(); |
| 7861 | } |
| 7862 | |
| 7863 | void ComplainOfInvalidConversion() const { |
| 7864 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 7865 | << OvlExpr->getName() << TargetType; |
| 7866 | } |
| 7867 | |
| 7868 | void ComplainMultipleMatchesFound() const { |
| 7869 | assert(Matches.size() > 1); |
| 7870 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 7871 | << OvlExpr->getName() |
| 7872 | << OvlExpr->getSourceRange(); |
| 7873 | S.NoteAllOverloadCandidates(OvlExpr); |
| 7874 | } |
| 7875 | |
| 7876 | int getNumMatches() const { return Matches.size(); } |
| 7877 | |
| 7878 | FunctionDecl* getMatchingFunctionDecl() const { |
| 7879 | if (Matches.size() != 1) return 0; |
| 7880 | return Matches[0].second; |
| 7881 | } |
| 7882 | |
| 7883 | const DeclAccessPair* getMatchingFunctionAccessPair() const { |
| 7884 | if (Matches.size() != 1) return 0; |
| 7885 | return &Matches[0].first; |
| 7886 | } |
| 7887 | }; |
| 7888 | |
| 7889 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 7890 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 7891 | /// expression with overloaded function type and @p ToType is the type |
| 7892 | /// we're trying to resolve to. For example: |
| 7893 | /// |
| 7894 | /// @code |
| 7895 | /// int f(double); |
| 7896 | /// int f(int); |
| 7897 | /// |
| 7898 | /// int (*pfd)(double) = f; // selects f(double) |
| 7899 | /// @endcode |
| 7900 | /// |
| 7901 | /// This routine returns the resulting FunctionDecl if it could be |
| 7902 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 7903 | /// routine will emit diagnostics if there is an error. |
| 7904 | FunctionDecl * |
| 7905 | Sema::ResolveAddressOfOverloadedFunction(Expr *AddressOfExpr, QualType TargetType, |
| 7906 | bool Complain, |
| 7907 | DeclAccessPair &FoundResult) { |
| 7908 | |
| 7909 | assert(AddressOfExpr->getType() == Context.OverloadTy); |
| 7910 | |
| 7911 | AddressOfFunctionResolver Resolver(*this, AddressOfExpr, TargetType, Complain); |
| 7912 | int NumMatches = Resolver.getNumMatches(); |
| 7913 | FunctionDecl* Fn = 0; |
| 7914 | if ( NumMatches == 0 && Complain) { |
| 7915 | if (Resolver.IsInvalidFormOfPointerToMemberFunction()) |
| 7916 | Resolver.ComplainIsInvalidFormOfPointerToMemberFunction(); |
| 7917 | else |
| 7918 | Resolver.ComplainNoMatchesFound(); |
| 7919 | } |
| 7920 | else if (NumMatches > 1 && Complain) |
| 7921 | Resolver.ComplainMultipleMatchesFound(); |
| 7922 | else if (NumMatches == 1) { |
| 7923 | Fn = Resolver.getMatchingFunctionDecl(); |
| 7924 | assert(Fn); |
| 7925 | FoundResult = *Resolver.getMatchingFunctionAccessPair(); |
| 7926 | MarkDeclarationReferenced(AddressOfExpr->getLocStart(), Fn); |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 7927 | if (Complain) |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7928 | CheckAddressOfMemberAccess(AddressOfExpr, FoundResult); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 7929 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7930 | |
| 7931 | return Fn; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7932 | } |
| 7933 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7934 | /// \brief Given an expression that refers to an overloaded function, try to |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7935 | /// resolve that overloaded function expression down to a single function. |
| 7936 | /// |
| 7937 | /// This routine can only resolve template-ids that refer to a single function |
| 7938 | /// template, where that template-id refers to a single template whose template |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7939 | /// arguments are either provided by the template-id or have defaults, |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7940 | /// as described in C++0x [temp.arg.explicit]p3. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7941 | FunctionDecl * |
| 7942 | Sema::ResolveSingleFunctionTemplateSpecialization(OverloadExpr *ovl, |
| 7943 | bool Complain, |
| 7944 | DeclAccessPair *FoundResult) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7945 | // C++ [over.over]p1: |
| 7946 | // [...] [Note: any redundant set of parentheses surrounding the |
| 7947 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7948 | // C++ [over.over]p1: |
| 7949 | // [...] The overloaded function name can be preceded by the & |
| 7950 | // operator. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7951 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7952 | // If we didn't actually find any template-ids, we're done. |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7953 | if (!ovl->hasExplicitTemplateArgs()) |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7954 | return 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 7955 | |
| 7956 | TemplateArgumentListInfo ExplicitTemplateArgs; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7957 | ovl->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7958 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7959 | // Look through all of the overloaded functions, searching for one |
| 7960 | // whose type matches exactly. |
| 7961 | FunctionDecl *Matched = 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7962 | for (UnresolvedSetIterator I = ovl->decls_begin(), |
| 7963 | E = ovl->decls_end(); I != E; ++I) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7964 | // C++0x [temp.arg.explicit]p3: |
| 7965 | // [...] In contexts where deduction is done and fails, or in contexts |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7966 | // where deduction is not done, if a template argument list is |
| 7967 | // specified and it, along with any default template arguments, |
| 7968 | // identifies a single function template specialization, then the |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7969 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | 66a8c9a | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 7970 | FunctionTemplateDecl *FunctionTemplate |
| 7971 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7972 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7973 | // C++ [over.over]p2: |
| 7974 | // If the name is a function template, template argument deduction is |
| 7975 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 7976 | // resulting template argument list is used to generate a single |
| 7977 | // function template specialization, which is added to the set of |
| 7978 | // overloaded functions considered. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7979 | FunctionDecl *Specialization = 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7980 | TemplateDeductionInfo Info(Context, ovl->getNameLoc()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7981 | if (TemplateDeductionResult Result |
| 7982 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 7983 | Specialization, Info)) { |
| 7984 | // FIXME: make a note of the failed deduction for diagnostics. |
| 7985 | (void)Result; |
| 7986 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7987 | } |
| 7988 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7989 | assert(Specialization && "no specialization and no error?"); |
| 7990 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7991 | // Multiple matches; we can't resolve to a single declaration. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7992 | if (Matched) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7993 | if (Complain) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7994 | Diag(ovl->getExprLoc(), diag::err_addr_ovl_ambiguous) |
| 7995 | << ovl->getName(); |
| 7996 | NoteAllOverloadCandidates(ovl); |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7997 | } |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7998 | return 0; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7999 | } |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 8000 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8001 | Matched = Specialization; |
| 8002 | if (FoundResult) *FoundResult = I.getPair(); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8003 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8004 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 8005 | return Matched; |
| 8006 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8007 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8008 | |
| 8009 | |
| 8010 | |
| 8011 | // Resolve and fix an overloaded expression that |
| 8012 | // can be resolved because it identifies a single function |
| 8013 | // template specialization |
| 8014 | // Last three arguments should only be supplied if Complain = true |
| 8015 | ExprResult Sema::ResolveAndFixSingleFunctionTemplateSpecialization( |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8016 | Expr *SrcExpr, bool doFunctionPointerConverion, bool complain, |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8017 | const SourceRange& OpRangeForComplaining, |
| 8018 | QualType DestTypeForComplaining, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8019 | unsigned DiagIDForComplaining) { |
| 8020 | assert(SrcExpr->getType() == Context.OverloadTy); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8021 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8022 | OverloadExpr::FindResult ovl = OverloadExpr::find(SrcExpr); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8023 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8024 | DeclAccessPair found; |
| 8025 | ExprResult SingleFunctionExpression; |
| 8026 | if (FunctionDecl *fn = ResolveSingleFunctionTemplateSpecialization( |
| 8027 | ovl.Expression, /*complain*/ false, &found)) { |
| 8028 | if (DiagnoseUseOfDecl(fn, SrcExpr->getSourceRange().getBegin())) |
| 8029 | return ExprError(); |
| 8030 | |
| 8031 | // It is only correct to resolve to an instance method if we're |
| 8032 | // resolving a form that's permitted to be a pointer to member. |
| 8033 | // Otherwise we'll end up making a bound member expression, which |
| 8034 | // is illegal in all the contexts we resolve like this. |
| 8035 | if (!ovl.HasFormOfMemberPointer && |
| 8036 | isa<CXXMethodDecl>(fn) && |
| 8037 | cast<CXXMethodDecl>(fn)->isInstance()) { |
| 8038 | if (complain) { |
| 8039 | Diag(ovl.Expression->getExprLoc(), |
| 8040 | diag::err_invalid_use_of_bound_member_func) |
| 8041 | << ovl.Expression->getSourceRange(); |
| 8042 | // TODO: I believe we only end up here if there's a mix of |
| 8043 | // static and non-static candidates (otherwise the expression |
| 8044 | // would have 'bound member' type, not 'overload' type). |
| 8045 | // Ideally we would note which candidate was chosen and why |
| 8046 | // the static candidates were rejected. |
| 8047 | } |
| 8048 | |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 8049 | return ExprError(); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8050 | } |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 8051 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8052 | // Fix the expresion to refer to 'fn'. |
| 8053 | SingleFunctionExpression = |
| 8054 | Owned(FixOverloadedFunctionReference(SrcExpr, found, fn)); |
| 8055 | |
| 8056 | // If desired, do function-to-pointer decay. |
| 8057 | if (doFunctionPointerConverion) |
| 8058 | SingleFunctionExpression = |
| 8059 | DefaultFunctionArrayLvalueConversion(SingleFunctionExpression.take()); |
| 8060 | } |
| 8061 | |
| 8062 | if (!SingleFunctionExpression.isUsable()) { |
| 8063 | if (complain) { |
| 8064 | Diag(OpRangeForComplaining.getBegin(), DiagIDForComplaining) |
| 8065 | << ovl.Expression->getName() |
| 8066 | << DestTypeForComplaining |
| 8067 | << OpRangeForComplaining |
| 8068 | << ovl.Expression->getQualifierLoc().getSourceRange(); |
| 8069 | NoteAllOverloadCandidates(SrcExpr); |
| 8070 | } |
| 8071 | return ExprError(); |
| 8072 | } |
| 8073 | |
| 8074 | return SingleFunctionExpression; |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8075 | } |
| 8076 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8077 | /// \brief Add a single candidate to the overload set. |
| 8078 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8079 | DeclAccessPair FoundDecl, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 8080 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8081 | Expr **Args, unsigned NumArgs, |
| 8082 | OverloadCandidateSet &CandidateSet, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 8083 | bool PartialOverloading, |
| 8084 | bool KnownValid) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8085 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8086 | if (isa<UsingShadowDecl>(Callee)) |
| 8087 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 8088 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8089 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 8090 | if (ExplicitTemplateArgs) { |
| 8091 | assert(!KnownValid && "Explicit template arguments?"); |
| 8092 | return; |
| 8093 | } |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8094 | S.AddOverloadCandidate(Func, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 8095 | false, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8096 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8097 | } |
| 8098 | |
| 8099 | if (FunctionTemplateDecl *FuncTemplate |
| 8100 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8101 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
| 8102 | ExplicitTemplateArgs, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8103 | Args, NumArgs, CandidateSet); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8104 | return; |
| 8105 | } |
| 8106 | |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 8107 | assert(!KnownValid && "unhandled case in overloaded call candidate"); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8108 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8109 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8110 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 8111 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8112 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8113 | Expr **Args, unsigned NumArgs, |
| 8114 | OverloadCandidateSet &CandidateSet, |
| 8115 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8116 | |
| 8117 | #ifndef NDEBUG |
| 8118 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 8119 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8120 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8121 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 8122 | // and let Y be the lookup set produced by argument dependent |
| 8123 | // lookup (defined as follows). If X contains |
| 8124 | // |
| 8125 | // -- a declaration of a class member, or |
| 8126 | // |
| 8127 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8128 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8129 | // |
| 8130 | // -- a declaration that is neither a function or a function |
| 8131 | // template |
| 8132 | // |
| 8133 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8134 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8135 | if (ULE->requiresADL()) { |
| 8136 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 8137 | E = ULE->decls_end(); I != E; ++I) { |
| 8138 | assert(!(*I)->getDeclContext()->isRecord()); |
| 8139 | assert(isa<UsingShadowDecl>(*I) || |
| 8140 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 8141 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8142 | } |
| 8143 | } |
| 8144 | #endif |
| 8145 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8146 | // It would be nice to avoid this copy. |
| 8147 | TemplateArgumentListInfo TABuffer; |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 8148 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8149 | if (ULE->hasExplicitTemplateArgs()) { |
| 8150 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 8151 | ExplicitTemplateArgs = &TABuffer; |
| 8152 | } |
| 8153 | |
| 8154 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 8155 | E = ULE->decls_end(); I != E; ++I) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8156 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8157 | Args, NumArgs, CandidateSet, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 8158 | PartialOverloading, /*KnownValid*/ true); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8159 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8160 | if (ULE->requiresADL()) |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8161 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
| 8162 | Args, NumArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8163 | ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8164 | CandidateSet, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 8165 | PartialOverloading, |
| 8166 | ULE->isStdAssociatedNamespace()); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 8167 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 8168 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8169 | /// Attempt to recover from an ill-formed use of a non-dependent name in a |
| 8170 | /// template, where the non-dependent name was declared after the template |
| 8171 | /// was defined. This is common in code written for a compilers which do not |
| 8172 | /// correctly implement two-stage name lookup. |
| 8173 | /// |
| 8174 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 8175 | static bool |
| 8176 | DiagnoseTwoPhaseLookup(Sema &SemaRef, SourceLocation FnLoc, |
| 8177 | const CXXScopeSpec &SS, LookupResult &R, |
| 8178 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 8179 | Expr **Args, unsigned NumArgs) { |
| 8180 | if (SemaRef.ActiveTemplateInstantiations.empty() || !SS.isEmpty()) |
| 8181 | return false; |
| 8182 | |
| 8183 | for (DeclContext *DC = SemaRef.CurContext; DC; DC = DC->getParent()) { |
| 8184 | SemaRef.LookupQualifiedName(R, DC); |
| 8185 | |
| 8186 | if (!R.empty()) { |
| 8187 | R.suppressDiagnostics(); |
| 8188 | |
| 8189 | if (isa<CXXRecordDecl>(DC)) { |
| 8190 | // Don't diagnose names we find in classes; we get much better |
| 8191 | // diagnostics for these from DiagnoseEmptyLookup. |
| 8192 | R.clear(); |
| 8193 | return false; |
| 8194 | } |
| 8195 | |
| 8196 | OverloadCandidateSet Candidates(FnLoc); |
| 8197 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 8198 | AddOverloadedCallCandidate(SemaRef, I.getPair(), |
| 8199 | ExplicitTemplateArgs, Args, NumArgs, |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 8200 | Candidates, false, /*KnownValid*/ false); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8201 | |
| 8202 | OverloadCandidateSet::iterator Best; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 8203 | if (Candidates.BestViableFunction(SemaRef, FnLoc, Best) != OR_Success) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8204 | // No viable functions. Don't bother the user with notes for functions |
| 8205 | // which don't work and shouldn't be found anyway. |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 8206 | R.clear(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8207 | return false; |
Richard Smith | 2ced044 | 2011-06-26 22:19:54 +0000 | [diff] [blame] | 8208 | } |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8209 | |
| 8210 | // Find the namespaces where ADL would have looked, and suggest |
| 8211 | // declaring the function there instead. |
| 8212 | Sema::AssociatedNamespaceSet AssociatedNamespaces; |
| 8213 | Sema::AssociatedClassSet AssociatedClasses; |
| 8214 | SemaRef.FindAssociatedClassesAndNamespaces(Args, NumArgs, |
| 8215 | AssociatedNamespaces, |
| 8216 | AssociatedClasses); |
| 8217 | // Never suggest declaring a function within namespace 'std'. |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 8218 | Sema::AssociatedNamespaceSet SuggestedNamespaces; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8219 | if (DeclContext *Std = SemaRef.getStdNamespace()) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8220 | for (Sema::AssociatedNamespaceSet::iterator |
| 8221 | it = AssociatedNamespaces.begin(), |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 8222 | end = AssociatedNamespaces.end(); it != end; ++it) { |
| 8223 | if (!Std->Encloses(*it)) |
| 8224 | SuggestedNamespaces.insert(*it); |
| 8225 | } |
Chandler Carruth | 45cad4a | 2011-06-08 10:13:17 +0000 | [diff] [blame] | 8226 | } else { |
| 8227 | // Lacking the 'std::' namespace, use all of the associated namespaces. |
| 8228 | SuggestedNamespaces = AssociatedNamespaces; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8229 | } |
| 8230 | |
| 8231 | SemaRef.Diag(R.getNameLoc(), diag::err_not_found_by_two_phase_lookup) |
| 8232 | << R.getLookupName(); |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 8233 | if (SuggestedNamespaces.empty()) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8234 | SemaRef.Diag(Best->Function->getLocation(), |
| 8235 | diag::note_not_found_by_two_phase_lookup) |
| 8236 | << R.getLookupName() << 0; |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 8237 | } else if (SuggestedNamespaces.size() == 1) { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8238 | SemaRef.Diag(Best->Function->getLocation(), |
| 8239 | diag::note_not_found_by_two_phase_lookup) |
Chandler Carruth | 74d487e | 2011-06-05 23:36:55 +0000 | [diff] [blame] | 8240 | << R.getLookupName() << 1 << *SuggestedNamespaces.begin(); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8241 | } else { |
| 8242 | // FIXME: It would be useful to list the associated namespaces here, |
| 8243 | // but the diagnostics infrastructure doesn't provide a way to produce |
| 8244 | // a localized representation of a list of items. |
| 8245 | SemaRef.Diag(Best->Function->getLocation(), |
| 8246 | diag::note_not_found_by_two_phase_lookup) |
| 8247 | << R.getLookupName() << 2; |
| 8248 | } |
| 8249 | |
| 8250 | // Try to recover by calling this function. |
| 8251 | return true; |
| 8252 | } |
| 8253 | |
| 8254 | R.clear(); |
| 8255 | } |
| 8256 | |
| 8257 | return false; |
| 8258 | } |
| 8259 | |
| 8260 | /// Attempt to recover from ill-formed use of a non-dependent operator in a |
| 8261 | /// template, where the non-dependent operator was declared after the template |
| 8262 | /// was defined. |
| 8263 | /// |
| 8264 | /// Returns true if a viable candidate was found and a diagnostic was issued. |
| 8265 | static bool |
| 8266 | DiagnoseTwoPhaseOperatorLookup(Sema &SemaRef, OverloadedOperatorKind Op, |
| 8267 | SourceLocation OpLoc, |
| 8268 | Expr **Args, unsigned NumArgs) { |
| 8269 | DeclarationName OpName = |
| 8270 | SemaRef.Context.DeclarationNames.getCXXOperatorName(Op); |
| 8271 | LookupResult R(SemaRef, OpName, OpLoc, Sema::LookupOperatorName); |
| 8272 | return DiagnoseTwoPhaseLookup(SemaRef, OpLoc, CXXScopeSpec(), R, |
| 8273 | /*ExplicitTemplateArgs=*/0, Args, NumArgs); |
| 8274 | } |
| 8275 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 8276 | /// Attempts to recover from a call where no functions were found. |
| 8277 | /// |
| 8278 | /// Returns true if new candidates were found. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8279 | static ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 8280 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8281 | UnresolvedLookupExpr *ULE, |
| 8282 | SourceLocation LParenLoc, |
| 8283 | Expr **Args, unsigned NumArgs, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8284 | SourceLocation RParenLoc, |
| 8285 | bool EmptyLookup) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 8286 | |
| 8287 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 8288 | SS.Adopt(ULE->getQualifierLoc()); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 8289 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8290 | TemplateArgumentListInfo TABuffer; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8291 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8292 | if (ULE->hasExplicitTemplateArgs()) { |
| 8293 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 8294 | ExplicitTemplateArgs = &TABuffer; |
| 8295 | } |
| 8296 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 8297 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 8298 | Sema::LookupOrdinaryName); |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8299 | if (!DiagnoseTwoPhaseLookup(SemaRef, Fn->getExprLoc(), SS, R, |
| 8300 | ExplicitTemplateArgs, Args, NumArgs) && |
| 8301 | (!EmptyLookup || |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 8302 | SemaRef.DiagnoseEmptyLookup(S, SS, R, Sema::CTC_Expression, |
Kaelyn Uhrain | ace5e76 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 8303 | ExplicitTemplateArgs, Args, NumArgs))) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8304 | return ExprError(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 8305 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8306 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 8307 | |
| 8308 | // Build an implicit member call if appropriate. Just drop the |
| 8309 | // casts and such from the call, we don't really care. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8310 | ExprResult NewFn = ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8311 | if ((*R.begin())->isCXXClassMember()) |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8312 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, |
| 8313 | ExplicitTemplateArgs); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8314 | else if (ExplicitTemplateArgs) |
| 8315 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 8316 | else |
| 8317 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 8318 | |
| 8319 | if (NewFn.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8320 | return ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8321 | |
| 8322 | // This shouldn't cause an infinite loop because we're giving it |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8323 | // an expression with viable lookup results, which should never |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8324 | // end up here. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8325 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, NewFn.take(), LParenLoc, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 8326 | MultiExprArg(Args, NumArgs), RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 8327 | } |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 8328 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8329 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 8330 | /// (which eventually refers to the declaration Func) and the call |
| 8331 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 8332 | /// to a specific function. If overload resolution succeeds, returns |
| 8333 | /// the function declaration produced by overload |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 8334 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8335 | /// arguments and Fn, and returns NULL. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8336 | ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 8337 | Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, UnresolvedLookupExpr *ULE, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8338 | SourceLocation LParenLoc, |
| 8339 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 8340 | SourceLocation RParenLoc, |
| 8341 | Expr *ExecConfig) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8342 | #ifndef NDEBUG |
| 8343 | if (ULE->requiresADL()) { |
| 8344 | // To do ADL, we must have found an unqualified name. |
| 8345 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 8346 | |
| 8347 | // We don't perform ADL for implicit declarations of builtins. |
| 8348 | // Verify that this was correctly set up. |
| 8349 | FunctionDecl *F; |
| 8350 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 8351 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 8352 | F->getBuiltinID() && F->isImplicit()) |
| 8353 | assert(0 && "performing ADL for builtin"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8354 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8355 | // We don't perform ADL in C. |
| 8356 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 8357 | } else |
| 8358 | assert(!ULE->isStdAssociatedNamespace() && |
| 8359 | "std is associated namespace but not doing ADL"); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8360 | #endif |
| 8361 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8362 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 8363 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8364 | // Add the functions denoted by the callee to the set of candidate |
| 8365 | // functions, including those from argument-dependent lookup. |
| 8366 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 8367 | |
| 8368 | // If we found nothing, try to recover. |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8369 | // BuildRecoveryCallExpr diagnoses the error itself, so we just bail |
| 8370 | // out if it fails. |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 8371 | if (CandidateSet.empty()) { |
| 8372 | // In Microsoft mode, if we are inside a template class member function then
|
| 8373 | // create a type dependent CallExpr. The goal is to postpone name lookup
|
| 8374 | // to instantiation time to be able to search into type dependent base |
| 8375 | // classes.
|
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame^] | 8376 | if (getLangOptions().MicrosoftExt && CurContext->isDependentContext() &&
|
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 8377 | isa<CXXMethodDecl>(CurContext)) {
|
| 8378 | CallExpr *CE = new (Context) CallExpr(Context, Fn, Args, NumArgs,
|
| 8379 | Context.DependentTy, VK_RValue,
|
| 8380 | RParenLoc);
|
| 8381 | CE->setTypeDependent(true);
|
| 8382 | return Owned(CE);
|
| 8383 | }
|
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 8384 | return BuildRecoveryCallExpr(*this, S, Fn, ULE, LParenLoc, Args, NumArgs, |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8385 | RParenLoc, /*EmptyLookup=*/true); |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 8386 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 8387 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8388 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8389 | switch (CandidateSet.BestViableFunction(*this, Fn->getLocStart(), Best)) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8390 | case OR_Success: { |
| 8391 | FunctionDecl *FDecl = Best->Function; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8392 | MarkDeclarationReferenced(Fn->getExprLoc(), FDecl); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8393 | CheckUnresolvedLookupAccess(ULE, Best->FoundDecl); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8394 | DiagnoseUseOfDecl(FDecl? FDecl : Best->FoundDecl.getDecl(), |
| 8395 | ULE->getNameLoc()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8396 | Fn = FixOverloadedFunctionReference(Fn, Best->FoundDecl, FDecl); |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 8397 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 8398 | ExecConfig); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8399 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8400 | |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8401 | case OR_No_Viable_Function: { |
| 8402 | // Try to recover by looking for viable functions which the user might |
| 8403 | // have meant to call. |
| 8404 | ExprResult Recovery = BuildRecoveryCallExpr(*this, S, Fn, ULE, LParenLoc, |
| 8405 | Args, NumArgs, RParenLoc, |
| 8406 | /*EmptyLookup=*/false); |
| 8407 | if (!Recovery.isInvalid()) |
| 8408 | return Recovery; |
| 8409 | |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 8410 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8411 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8412 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8413 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8414 | break; |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8415 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8416 | |
| 8417 | case OR_Ambiguous: |
| 8418 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8419 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8420 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8421 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8422 | |
| 8423 | case OR_Deleted: |
Fariborz Jahanian | 2b982b7 | 2011-02-25 18:38:59 +0000 | [diff] [blame] | 8424 | { |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 8425 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 8426 | << Best->Function->isDeleted() |
| 8427 | << ULE->getName() |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 8428 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 8429 | << Fn->getSourceRange(); |
Fariborz Jahanian | 2b982b7 | 2011-02-25 18:38:59 +0000 | [diff] [blame] | 8430 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
| 8431 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8432 | break; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8433 | } |
| 8434 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 8435 | // Overload resolution failed. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 8436 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 8437 | } |
| 8438 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8439 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 8440 | return Functions.size() > 1 || |
| 8441 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 8442 | } |
| 8443 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8444 | /// \brief Create a unary operation that may resolve to an overloaded |
| 8445 | /// operator. |
| 8446 | /// |
| 8447 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 8448 | /// |
| 8449 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 8450 | /// operator. |
| 8451 | /// |
| 8452 | /// \param Functions The set of non-member functions that will be |
| 8453 | /// considered by overload resolution. The caller needs to build this |
| 8454 | /// set based on the context using, e.g., |
| 8455 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 8456 | /// set should not contain any member functions; those will be added |
| 8457 | /// by CreateOverloadedUnaryOp(). |
| 8458 | /// |
| 8459 | /// \param input The input argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8460 | ExprResult |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8461 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 8462 | const UnresolvedSetImpl &Fns, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8463 | Expr *Input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8464 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8465 | |
| 8466 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 8467 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 8468 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8469 | // TODO: provide better source location info. |
| 8470 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8471 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8472 | if (Input->getObjectKind() == OK_ObjCProperty) { |
| 8473 | ExprResult Result = ConvertPropertyForRValue(Input); |
| 8474 | if (Result.isInvalid()) |
| 8475 | return ExprError(); |
| 8476 | Input = Result.take(); |
| 8477 | } |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8478 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8479 | Expr *Args[2] = { Input, 0 }; |
| 8480 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8481 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8482 | // For post-increment and post-decrement, add the implicit '0' as |
| 8483 | // the second argument, so that we know this is a post-increment or |
| 8484 | // post-decrement. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8485 | if (Opc == UO_PostInc || Opc == UO_PostDec) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8486 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 8487 | Args[1] = IntegerLiteral::Create(Context, Zero, Context.IntTy, |
| 8488 | SourceLocation()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8489 | NumArgs = 2; |
| 8490 | } |
| 8491 | |
| 8492 | if (Input->isTypeDependent()) { |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 8493 | if (Fns.empty()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8494 | return Owned(new (Context) UnaryOperator(Input, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8495 | Opc, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 8496 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8497 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 8498 | OpLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8499 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8500 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8501 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 8502 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 8503 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 8504 | /*ADL*/ true, IsOverloaded(Fns), |
| 8505 | Fns.begin(), Fns.end()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8506 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 8507 | &Args[0], NumArgs, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8508 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8509 | VK_RValue, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8510 | OpLoc)); |
| 8511 | } |
| 8512 | |
| 8513 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8514 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8515 | |
| 8516 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8517 | AddFunctionCandidates(Fns, &Args[0], NumArgs, CandidateSet, false); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8518 | |
| 8519 | // Add operator candidates that are member functions. |
| 8520 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 8521 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8522 | // Add candidates from ADL. |
| 8523 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Douglas Gregor | dc81c88 | 2010-02-05 05:15:43 +0000 | [diff] [blame] | 8524 | Args, NumArgs, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8525 | /*ExplicitTemplateArgs*/ 0, |
| 8526 | CandidateSet); |
| 8527 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8528 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 8529 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8530 | |
| 8531 | // Perform overload resolution. |
| 8532 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8533 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8534 | case OR_Success: { |
| 8535 | // We found a built-in operator or an overloaded operator. |
| 8536 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8537 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8538 | if (FnDecl) { |
| 8539 | // We matched an overloaded operator. Build a call to that |
| 8540 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8541 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8542 | MarkDeclarationReferenced(OpLoc, FnDecl); |
| 8543 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8544 | // Convert the arguments. |
| 8545 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8546 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 8547 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8548 | ExprResult InputRes = |
| 8549 | PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 8550 | Best->FoundDecl, Method); |
| 8551 | if (InputRes.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8552 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8553 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8554 | } else { |
| 8555 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8556 | ExprResult InputInit |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 8557 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 8558 | Context, |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 8559 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8560 | SourceLocation(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8561 | Input); |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 8562 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8563 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8564 | Input = InputInit.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8565 | } |
| 8566 | |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8567 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 8568 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8569 | // Determine the result type. |
| 8570 | QualType ResultTy = FnDecl->getResultType(); |
| 8571 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 8572 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8573 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8574 | // Build the actual expression node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8575 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl); |
| 8576 | if (FnExpr.isInvalid()) |
| 8577 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8578 | |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 8579 | Args[0] = Input; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8580 | CallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8581 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8582 | Args, NumArgs, ResultTy, VK, OpLoc); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8583 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8584 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 8585 | FnDecl)) |
| 8586 | return ExprError(); |
| 8587 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8588 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8589 | } else { |
| 8590 | // We matched a built-in operator. Convert the arguments, then |
| 8591 | // break out so that we will build the appropriate built-in |
| 8592 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8593 | ExprResult InputRes = |
| 8594 | PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 8595 | Best->Conversions[0], AA_Passing); |
| 8596 | if (InputRes.isInvalid()) |
| 8597 | return ExprError(); |
| 8598 | Input = InputRes.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8599 | break; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8600 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8601 | } |
| 8602 | |
| 8603 | case OR_No_Viable_Function: |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8604 | // This is an erroneous use of an operator which can be overloaded by |
| 8605 | // a non-member function. Check for non-member operators which were |
| 8606 | // defined too late to be candidates. |
| 8607 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, Args, NumArgs)) |
| 8608 | // FIXME: Recover by calling the found function. |
| 8609 | return ExprError(); |
| 8610 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8611 | // No viable function; fall through to handling this as a |
| 8612 | // built-in operator, which will produce an error message for us. |
| 8613 | break; |
| 8614 | |
| 8615 | case OR_Ambiguous: |
| 8616 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 8617 | << UnaryOperator::getOpcodeStr(Opc) |
| 8618 | << Input->getType() |
| 8619 | << Input->getSourceRange(); |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 8620 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8621 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
| 8622 | return ExprError(); |
| 8623 | |
| 8624 | case OR_Deleted: |
| 8625 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 8626 | << Best->Function->isDeleted() |
| 8627 | << UnaryOperator::getOpcodeStr(Opc) |
| 8628 | << getDeletedOrUnavailableSuffix(Best->Function) |
| 8629 | << Input->getSourceRange(); |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 8630 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs, |
| 8631 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8632 | return ExprError(); |
| 8633 | } |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8634 | |
| 8635 | // Either we found no viable overloaded operator or we matched a |
| 8636 | // built-in operator. In either case, fall through to trying to |
| 8637 | // build a built-in operation. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8638 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8639 | } |
| 8640 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8641 | /// \brief Create a binary operation that may resolve to an overloaded |
| 8642 | /// operator. |
| 8643 | /// |
| 8644 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 8645 | /// |
| 8646 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 8647 | /// operator. |
| 8648 | /// |
| 8649 | /// \param Functions The set of non-member functions that will be |
| 8650 | /// considered by overload resolution. The caller needs to build this |
| 8651 | /// set based on the context using, e.g., |
| 8652 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 8653 | /// set should not contain any member functions; those will be added |
| 8654 | /// by CreateOverloadedBinOp(). |
| 8655 | /// |
| 8656 | /// \param LHS Left-hand argument. |
| 8657 | /// \param RHS Right-hand argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8658 | ExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8659 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8660 | unsigned OpcIn, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8661 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8662 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8663 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8664 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8665 | |
| 8666 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 8667 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 8668 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 8669 | |
| 8670 | // If either side is type-dependent, create an appropriate dependent |
| 8671 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8672 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8673 | if (Fns.empty()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8674 | // If there are no functions to store, just build a dependent |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8675 | // BinaryOperator or CompoundAssignment. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8676 | if (Opc <= BO_Assign || Opc > BO_OrAssign) |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8677 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8678 | Context.DependentTy, |
| 8679 | VK_RValue, OK_Ordinary, |
| 8680 | OpLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8681 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8682 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 8683 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8684 | VK_LValue, |
| 8685 | OK_Ordinary, |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8686 | Context.DependentTy, |
| 8687 | Context.DependentTy, |
| 8688 | OpLoc)); |
| 8689 | } |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8690 | |
| 8691 | // FIXME: save results of ADL from here? |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8692 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8693 | // TODO: provide better source location info in DNLoc component. |
| 8694 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8695 | UnresolvedLookupExpr *Fn |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 8696 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
| 8697 | NestedNameSpecifierLoc(), OpNameInfo, |
| 8698 | /*ADL*/ true, IsOverloaded(Fns), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 8699 | Fns.begin(), Fns.end()); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8700 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8701 | Args, 2, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8702 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8703 | VK_RValue, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8704 | OpLoc)); |
| 8705 | } |
| 8706 | |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8707 | // Always do property rvalue conversions on the RHS. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8708 | if (Args[1]->getObjectKind() == OK_ObjCProperty) { |
| 8709 | ExprResult Result = ConvertPropertyForRValue(Args[1]); |
| 8710 | if (Result.isInvalid()) |
| 8711 | return ExprError(); |
| 8712 | Args[1] = Result.take(); |
| 8713 | } |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8714 | |
| 8715 | // The LHS is more complicated. |
| 8716 | if (Args[0]->getObjectKind() == OK_ObjCProperty) { |
| 8717 | |
| 8718 | // There's a tension for assignment operators between primitive |
| 8719 | // property assignment and the overloaded operators. |
| 8720 | if (BinaryOperator::isAssignmentOp(Opc)) { |
| 8721 | const ObjCPropertyRefExpr *PRE = LHS->getObjCProperty(); |
| 8722 | |
| 8723 | // Is the property "logically" settable? |
| 8724 | bool Settable = (PRE->isExplicitProperty() || |
| 8725 | PRE->getImplicitPropertySetter()); |
| 8726 | |
| 8727 | // To avoid gratuitously inventing semantics, use the primitive |
| 8728 | // unless it isn't. Thoughts in case we ever really care: |
| 8729 | // - If the property isn't logically settable, we have to |
| 8730 | // load and hope. |
| 8731 | // - If the property is settable and this is simple assignment, |
| 8732 | // we really should use the primitive. |
| 8733 | // - If the property is settable, then we could try overloading |
| 8734 | // on a generic lvalue of the appropriate type; if it works |
| 8735 | // out to a builtin candidate, we would do that same operation |
| 8736 | // on the property, and otherwise just error. |
| 8737 | if (Settable) |
| 8738 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 8739 | } |
| 8740 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8741 | ExprResult Result = ConvertPropertyForRValue(Args[0]); |
| 8742 | if (Result.isInvalid()) |
| 8743 | return ExprError(); |
| 8744 | Args[0] = Result.take(); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8745 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8746 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 8747 | // If this is the assignment operator, we only perform overload resolution |
| 8748 | // if the left-hand side is a class or enumeration type. This is actually |
| 8749 | // a hack. The standard requires that we do overload resolution between the |
| 8750 | // various built-in candidates, but as DR507 points out, this can lead to |
| 8751 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 8752 | // Note that we go the traditional code path for compound assignment forms. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8753 | if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8754 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8755 | |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8756 | // If this is the .* operator, which is not overloadable, just |
| 8757 | // create a built-in binary operator. |
| 8758 | if (Opc == BO_PtrMemD) |
| 8759 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 8760 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8761 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8762 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8763 | |
| 8764 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8765 | AddFunctionCandidates(Fns, Args, 2, CandidateSet, false); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8766 | |
| 8767 | // Add operator candidates that are member functions. |
| 8768 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 8769 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8770 | // Add candidates from ADL. |
| 8771 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
| 8772 | Args, 2, |
| 8773 | /*ExplicitTemplateArgs*/ 0, |
| 8774 | CandidateSet); |
| 8775 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8776 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 8777 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8778 | |
| 8779 | // Perform overload resolution. |
| 8780 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8781 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 8782 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8783 | // We found a built-in operator or an overloaded operator. |
| 8784 | FunctionDecl *FnDecl = Best->Function; |
| 8785 | |
| 8786 | if (FnDecl) { |
| 8787 | // We matched an overloaded operator. Build a call to that |
| 8788 | // operator. |
| 8789 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8790 | MarkDeclarationReferenced(OpLoc, FnDecl); |
| 8791 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8792 | // Convert the arguments. |
| 8793 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 8794 | // Best->Access is only meaningful for class members. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8795 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 8796 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8797 | ExprResult Arg1 = |
| 8798 | PerformCopyInitialization( |
| 8799 | InitializedEntity::InitializeParameter(Context, |
| 8800 | FnDecl->getParamDecl(0)), |
| 8801 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8802 | if (Arg1.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8803 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8804 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8805 | ExprResult Arg0 = |
| 8806 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 8807 | Best->FoundDecl, Method); |
| 8808 | if (Arg0.isInvalid()) |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8809 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8810 | Args[0] = Arg0.takeAs<Expr>(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8811 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8812 | } else { |
| 8813 | // Convert the arguments. |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8814 | ExprResult Arg0 = PerformCopyInitialization( |
| 8815 | InitializedEntity::InitializeParameter(Context, |
| 8816 | FnDecl->getParamDecl(0)), |
| 8817 | SourceLocation(), Owned(Args[0])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8818 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8819 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8820 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8821 | ExprResult Arg1 = |
| 8822 | PerformCopyInitialization( |
| 8823 | InitializedEntity::InitializeParameter(Context, |
| 8824 | FnDecl->getParamDecl(1)), |
| 8825 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8826 | if (Arg1.isInvalid()) |
| 8827 | return ExprError(); |
| 8828 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 8829 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8830 | } |
| 8831 | |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8832 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 8833 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8834 | // Determine the result type. |
| 8835 | QualType ResultTy = FnDecl->getResultType(); |
| 8836 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 8837 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8838 | |
| 8839 | // Build the actual expression node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8840 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, OpLoc); |
| 8841 | if (FnExpr.isInvalid()) |
| 8842 | return ExprError(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8843 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8844 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8845 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr.take(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8846 | Args, 2, ResultTy, VK, OpLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8847 | |
| 8848 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 8849 | FnDecl)) |
| 8850 | return ExprError(); |
| 8851 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8852 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8853 | } else { |
| 8854 | // We matched a built-in operator. Convert the arguments, then |
| 8855 | // break out so that we will build the appropriate built-in |
| 8856 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8857 | ExprResult ArgsRes0 = |
| 8858 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 8859 | Best->Conversions[0], AA_Passing); |
| 8860 | if (ArgsRes0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8861 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8862 | Args[0] = ArgsRes0.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8863 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8864 | ExprResult ArgsRes1 = |
| 8865 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 8866 | Best->Conversions[1], AA_Passing); |
| 8867 | if (ArgsRes1.isInvalid()) |
| 8868 | return ExprError(); |
| 8869 | Args[1] = ArgsRes1.take(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8870 | break; |
| 8871 | } |
| 8872 | } |
| 8873 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8874 | case OR_No_Viable_Function: { |
| 8875 | // C++ [over.match.oper]p9: |
| 8876 | // If the operator is the operator , [...] and there are no |
| 8877 | // viable functions, then the operator is assumed to be the |
| 8878 | // built-in operator and interpreted according to clause 5. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8879 | if (Opc == BO_Comma) |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8880 | break; |
| 8881 | |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8882 | // For class as left operand for assignment or compound assigment |
| 8883 | // operator do not fall through to handling in built-in, but report that |
| 8884 | // no overloaded assignment operator found |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8885 | ExprResult Result = ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8886 | if (Args[0]->getType()->isRecordType() && |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8887 | Opc >= BO_Assign && Opc <= BO_OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 8888 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 8889 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8890 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8891 | } else { |
Richard Smith | f50e88a | 2011-06-05 22:42:48 +0000 | [diff] [blame] | 8892 | // This is an erroneous use of an operator which can be overloaded by |
| 8893 | // a non-member function. Check for non-member operators which were |
| 8894 | // defined too late to be candidates. |
| 8895 | if (DiagnoseTwoPhaseOperatorLookup(*this, Op, OpLoc, Args, 2)) |
| 8896 | // FIXME: Recover by calling the found function. |
| 8897 | return ExprError(); |
| 8898 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8899 | // No viable function; try to create a built-in operation, which will |
| 8900 | // produce an error. Then, show the non-viable candidates. |
| 8901 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 8902 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8903 | assert(Result.isInvalid() && |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8904 | "C++ binary operator overloading is missing candidates!"); |
| 8905 | if (Result.isInvalid()) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8906 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 8907 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8908 | return move(Result); |
| 8909 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8910 | |
| 8911 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 8912 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_binary) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8913 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 8914 | << Args[0]->getType() << Args[1]->getType() |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8915 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8916 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, 2, |
| 8917 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8918 | return ExprError(); |
| 8919 | |
| 8920 | case OR_Deleted: |
| 8921 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 8922 | << Best->Function->isDeleted() |
| 8923 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 8924 | << getDeletedOrUnavailableSuffix(Best->Function) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8925 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Eli Friedman | 1795d37 | 2011-08-26 19:46:22 +0000 | [diff] [blame] | 8926 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 8927 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8928 | return ExprError(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8929 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8930 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8931 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8932 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8933 | } |
| 8934 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8935 | ExprResult |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8936 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 8937 | SourceLocation RLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8938 | Expr *Base, Expr *Idx) { |
| 8939 | Expr *Args[2] = { Base, Idx }; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8940 | DeclarationName OpName = |
| 8941 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 8942 | |
| 8943 | // If either side is type-dependent, create an appropriate dependent |
| 8944 | // expression. |
| 8945 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 8946 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8947 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8948 | // CHECKME: no 'operator' keyword? |
| 8949 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
| 8950 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8951 | UnresolvedLookupExpr *Fn |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 8952 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 8953 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 8954 | /*ADL*/ true, /*Overloaded*/ false, |
| 8955 | UnresolvedSetIterator(), |
| 8956 | UnresolvedSetIterator()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 8957 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8958 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8959 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 8960 | Args, 2, |
| 8961 | Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8962 | VK_RValue, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8963 | RLoc)); |
| 8964 | } |
| 8965 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8966 | if (Args[0]->getObjectKind() == OK_ObjCProperty) { |
| 8967 | ExprResult Result = ConvertPropertyForRValue(Args[0]); |
| 8968 | if (Result.isInvalid()) |
| 8969 | return ExprError(); |
| 8970 | Args[0] = Result.take(); |
| 8971 | } |
| 8972 | if (Args[1]->getObjectKind() == OK_ObjCProperty) { |
| 8973 | ExprResult Result = ConvertPropertyForRValue(Args[1]); |
| 8974 | if (Result.isInvalid()) |
| 8975 | return ExprError(); |
| 8976 | Args[1] = Result.take(); |
| 8977 | } |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8978 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8979 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8980 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8981 | |
| 8982 | // Subscript can only be overloaded as a member function. |
| 8983 | |
| 8984 | // Add operator candidates that are member functions. |
| 8985 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 8986 | |
| 8987 | // Add builtin operator candidates. |
| 8988 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 8989 | |
| 8990 | // Perform overload resolution. |
| 8991 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8992 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8993 | case OR_Success: { |
| 8994 | // We found a built-in operator or an overloaded operator. |
| 8995 | FunctionDecl *FnDecl = Best->Function; |
| 8996 | |
| 8997 | if (FnDecl) { |
| 8998 | // We matched an overloaded operator. Build a call to that |
| 8999 | // operator. |
| 9000 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 9001 | MarkDeclarationReferenced(LLoc, FnDecl); |
| 9002 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9003 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 9004 | DiagnoseUseOfDecl(Best->FoundDecl, LLoc); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 9005 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9006 | // Convert the arguments. |
| 9007 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9008 | ExprResult Arg0 = |
| 9009 | PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
| 9010 | Best->FoundDecl, Method); |
| 9011 | if (Arg0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9012 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9013 | Args[0] = Arg0.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9014 | |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 9015 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9016 | ExprResult InputInit |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 9017 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 9018 | Context, |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 9019 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9020 | SourceLocation(), |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 9021 | Owned(Args[1])); |
| 9022 | if (InputInit.isInvalid()) |
| 9023 | return ExprError(); |
| 9024 | |
| 9025 | Args[1] = InputInit.takeAs<Expr>(); |
| 9026 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9027 | // Determine the result type |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9028 | QualType ResultTy = FnDecl->getResultType(); |
| 9029 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 9030 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9031 | |
| 9032 | // Build the actual expression node. |
Douglas Gregor | 5b8968c | 2011-07-15 16:25:15 +0000 | [diff] [blame] | 9033 | DeclarationNameLoc LocInfo; |
| 9034 | LocInfo.CXXOperatorName.BeginOpNameLoc = LLoc.getRawEncoding(); |
| 9035 | LocInfo.CXXOperatorName.EndOpNameLoc = RLoc.getRawEncoding(); |
| 9036 | ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, LLoc, LocInfo); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9037 | if (FnExpr.isInvalid()) |
| 9038 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9039 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9040 | CXXOperatorCallExpr *TheCall = |
| 9041 | new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9042 | FnExpr.take(), Args, 2, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9043 | ResultTy, VK, RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9044 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9045 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9046 | FnDecl)) |
| 9047 | return ExprError(); |
| 9048 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9049 | return MaybeBindToTemporary(TheCall); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9050 | } else { |
| 9051 | // We matched a built-in operator. Convert the arguments, then |
| 9052 | // break out so that we will build the appropriate built-in |
| 9053 | // operator node. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9054 | ExprResult ArgsRes0 = |
| 9055 | PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 9056 | Best->Conversions[0], AA_Passing); |
| 9057 | if (ArgsRes0.isInvalid()) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9058 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9059 | Args[0] = ArgsRes0.take(); |
| 9060 | |
| 9061 | ExprResult ArgsRes1 = |
| 9062 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 9063 | Best->Conversions[1], AA_Passing); |
| 9064 | if (ArgsRes1.isInvalid()) |
| 9065 | return ExprError(); |
| 9066 | Args[1] = ArgsRes1.take(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9067 | |
| 9068 | break; |
| 9069 | } |
| 9070 | } |
| 9071 | |
| 9072 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 9073 | if (CandidateSet.empty()) |
| 9074 | Diag(LLoc, diag::err_ovl_no_oper) |
| 9075 | << Args[0]->getType() << /*subscript*/ 0 |
| 9076 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 9077 | else |
| 9078 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 9079 | << Args[0]->getType() |
| 9080 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9081 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 9082 | "[]", LLoc); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 9083 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9084 | } |
| 9085 | |
| 9086 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 9087 | Diag(LLoc, diag::err_ovl_ambiguous_oper_binary) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9088 | << "[]" |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 9089 | << Args[0]->getType() << Args[1]->getType() |
| 9090 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9091 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, 2, |
| 9092 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9093 | return ExprError(); |
| 9094 | |
| 9095 | case OR_Deleted: |
| 9096 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 9097 | << Best->Function->isDeleted() << "[]" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 9098 | << getDeletedOrUnavailableSuffix(Best->Function) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9099 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9100 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 9101 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9102 | return ExprError(); |
| 9103 | } |
| 9104 | |
| 9105 | // We matched a built-in operator; build it. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9106 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 9107 | } |
| 9108 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9109 | /// BuildCallToMemberFunction - Build a call to a member |
| 9110 | /// function. MemExpr is the expression that refers to the member |
| 9111 | /// function (and includes the object parameter), Args/NumArgs are the |
| 9112 | /// arguments to the function call (not including the object |
| 9113 | /// parameter). The caller needs to validate that the member |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9114 | /// expression refers to a non-static member function or an overloaded |
| 9115 | /// member function. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9116 | ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9117 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 9118 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 9119 | unsigned NumArgs, SourceLocation RParenLoc) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9120 | assert(MemExprE->getType() == Context.BoundMemberTy || |
| 9121 | MemExprE->getType() == Context.OverloadTy); |
| 9122 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9123 | // Dig out the member expression. This holds both the object |
| 9124 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9125 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9126 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9127 | // Determine whether this is a call to a pointer-to-member function. |
| 9128 | if (BinaryOperator *op = dyn_cast<BinaryOperator>(NakedMemExpr)) { |
| 9129 | assert(op->getType() == Context.BoundMemberTy); |
| 9130 | assert(op->getOpcode() == BO_PtrMemD || op->getOpcode() == BO_PtrMemI); |
| 9131 | |
| 9132 | QualType fnType = |
| 9133 | op->getRHS()->getType()->castAs<MemberPointerType>()->getPointeeType(); |
| 9134 | |
| 9135 | const FunctionProtoType *proto = fnType->castAs<FunctionProtoType>(); |
| 9136 | QualType resultType = proto->getCallResultType(Context); |
| 9137 | ExprValueKind valueKind = Expr::getValueKindForType(proto->getResultType()); |
| 9138 | |
| 9139 | // Check that the object type isn't more qualified than the |
| 9140 | // member function we're calling. |
| 9141 | Qualifiers funcQuals = Qualifiers::fromCVRMask(proto->getTypeQuals()); |
| 9142 | |
| 9143 | QualType objectType = op->getLHS()->getType(); |
| 9144 | if (op->getOpcode() == BO_PtrMemI) |
| 9145 | objectType = objectType->castAs<PointerType>()->getPointeeType(); |
| 9146 | Qualifiers objectQuals = objectType.getQualifiers(); |
| 9147 | |
| 9148 | Qualifiers difference = objectQuals - funcQuals; |
| 9149 | difference.removeObjCGCAttr(); |
| 9150 | difference.removeAddressSpace(); |
| 9151 | if (difference) { |
| 9152 | std::string qualsString = difference.getAsString(); |
| 9153 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
| 9154 | << fnType.getUnqualifiedType() |
| 9155 | << qualsString |
| 9156 | << (qualsString.find(' ') == std::string::npos ? 1 : 2); |
| 9157 | } |
| 9158 | |
| 9159 | CXXMemberCallExpr *call |
| 9160 | = new (Context) CXXMemberCallExpr(Context, MemExprE, Args, NumArgs, |
| 9161 | resultType, valueKind, RParenLoc); |
| 9162 | |
| 9163 | if (CheckCallReturnType(proto->getResultType(), |
| 9164 | op->getRHS()->getSourceRange().getBegin(), |
| 9165 | call, 0)) |
| 9166 | return ExprError(); |
| 9167 | |
| 9168 | if (ConvertArgumentsForCall(call, op, 0, proto, Args, NumArgs, RParenLoc)) |
| 9169 | return ExprError(); |
| 9170 | |
| 9171 | return MaybeBindToTemporary(call); |
| 9172 | } |
| 9173 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9174 | MemberExpr *MemExpr; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9175 | CXXMethodDecl *Method = 0; |
John McCall | bb6fb46 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 9176 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 9177 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9178 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 9179 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9180 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9181 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 9182 | Qualifier = MemExpr->getQualifier(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9183 | } else { |
| 9184 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 9185 | Qualifier = UnresExpr->getQualifier(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9186 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 9187 | QualType ObjectType = UnresExpr->getBaseType(); |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 9188 | Expr::Classification ObjectClassification |
| 9189 | = UnresExpr->isArrow()? Expr::Classification::makeSimpleLValue() |
| 9190 | : UnresExpr->getBase()->Classify(Context); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9191 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9192 | // Add overload candidates |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9193 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9194 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9195 | // FIXME: avoid copy. |
| 9196 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 9197 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 9198 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 9199 | TemplateArgs = &TemplateArgsBuffer; |
| 9200 | } |
| 9201 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9202 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 9203 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 9204 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 9205 | NamedDecl *Func = *I; |
| 9206 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 9207 | if (isa<UsingShadowDecl>(Func)) |
| 9208 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 9209 | |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 9210 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 9211 | // Microsoft supports direct constructor calls. |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame^] | 9212 | if (getLangOptions().MicrosoftExt && isa<CXXConstructorDecl>(Func)) { |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 9213 | AddOverloadCandidate(cast<CXXConstructorDecl>(Func), I.getPair(), Args, NumArgs, |
| 9214 | CandidateSet); |
| 9215 | } else if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 9216 | // If explicit template arguments were provided, we can't call a |
| 9217 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9218 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 9219 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9220 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9221 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9222 | ObjectClassification, |
| 9223 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 9224 | /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 9225 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9226 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9227 | I.getPair(), ActingDC, TemplateArgs, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9228 | ObjectType, ObjectClassification, |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 9229 | Args, NumArgs, CandidateSet, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 9230 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 9231 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 9232 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9233 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9234 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 9235 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9236 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9237 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getLocStart(), |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 9238 | Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9239 | case OR_Success: |
| 9240 | Method = cast<CXXMethodDecl>(Best->Function); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 9241 | MarkDeclarationReferenced(UnresExpr->getMemberLoc(), Method); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9242 | FoundDecl = Best->FoundDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9243 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 9244 | DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9245 | break; |
| 9246 | |
| 9247 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9248 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9249 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 9250 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9251 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9252 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9253 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9254 | |
| 9255 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9256 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 9257 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9258 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9259 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9260 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9261 | |
| 9262 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9263 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9264 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 9265 | << DeclName |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 9266 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 9267 | << MemExprE->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9268 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9269 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9270 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9271 | } |
| 9272 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9273 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9274 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9275 | // If overload resolution picked a static member, build a |
| 9276 | // non-member call based on that function. |
| 9277 | if (Method->isStatic()) { |
| 9278 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 9279 | Args, NumArgs, RParenLoc); |
| 9280 | } |
| 9281 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9282 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9283 | } |
| 9284 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9285 | QualType ResultType = Method->getResultType(); |
| 9286 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 9287 | ResultType = ResultType.getNonLValueExprType(Context); |
| 9288 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9289 | assert(Method && "Member call to something that isn't a method?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9290 | CXXMemberCallExpr *TheCall = |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9291 | new (Context) CXXMemberCallExpr(Context, MemExprE, Args, NumArgs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9292 | ResultType, VK, RParenLoc); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9293 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 9294 | // Check for a valid return type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9295 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9296 | TheCall, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9297 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9298 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9299 | // Convert the object argument (for a non-static member function call). |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9300 | // We only need to do this if there was actually an overload; otherwise |
| 9301 | // it was done at lookup. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9302 | if (!Method->isStatic()) { |
| 9303 | ExprResult ObjectArg = |
| 9304 | PerformObjectArgumentInitialization(MemExpr->getBase(), Qualifier, |
| 9305 | FoundDecl, Method); |
| 9306 | if (ObjectArg.isInvalid()) |
| 9307 | return ExprError(); |
| 9308 | MemExpr->setBase(ObjectArg.take()); |
| 9309 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9310 | |
| 9311 | // Convert the rest of the arguments |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 9312 | const FunctionProtoType *Proto = |
| 9313 | Method->getType()->getAs<FunctionProtoType>(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9314 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9315 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9316 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9317 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9318 | if (CheckFunctionCall(Method, TheCall)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9319 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 9320 | |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 9321 | if ((isa<CXXConstructorDecl>(CurContext) || |
| 9322 | isa<CXXDestructorDecl>(CurContext)) && |
| 9323 | TheCall->getMethodDecl()->isPure()) { |
| 9324 | const CXXMethodDecl *MD = TheCall->getMethodDecl(); |
| 9325 | |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 9326 | if (isa<CXXThisExpr>(MemExpr->getBase()->IgnoreParenCasts())) { |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 9327 | Diag(MemExpr->getLocStart(), |
| 9328 | diag::warn_call_to_pure_virtual_member_function_from_ctor_dtor) |
| 9329 | << MD->getDeclName() << isa<CXXDestructorDecl>(CurContext) |
| 9330 | << MD->getParent()->getDeclName(); |
| 9331 | |
| 9332 | Diag(MD->getLocStart(), diag::note_previous_decl) << MD->getDeclName(); |
Chandler Carruth | ae19806 | 2011-06-27 08:31:58 +0000 | [diff] [blame] | 9333 | } |
Anders Carlsson | 2174d4c | 2011-05-06 14:25:31 +0000 | [diff] [blame] | 9334 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9335 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 9336 | } |
| 9337 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9338 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 9339 | /// type (C++ [over.call.object]), which can end up invoking an |
| 9340 | /// overloaded function call operator (@c operator()) or performing a |
| 9341 | /// user-defined conversion on the object argument. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9342 | ExprResult |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9343 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 9344 | SourceLocation LParenLoc, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9345 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9346 | SourceLocation RParenLoc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9347 | ExprResult Object = Owned(Obj); |
| 9348 | if (Object.get()->getObjectKind() == OK_ObjCProperty) { |
| 9349 | Object = ConvertPropertyForRValue(Object.take()); |
| 9350 | if (Object.isInvalid()) |
| 9351 | return ExprError(); |
| 9352 | } |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 9353 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9354 | assert(Object.get()->getType()->isRecordType() && "Requires object type argument"); |
| 9355 | const RecordType *Record = Object.get()->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9356 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9357 | // C++ [over.call.object]p1: |
| 9358 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 9359 | // 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] | 9360 | // candidate functions includes at least the function call |
| 9361 | // operators of T. The function call operators of T are obtained by |
| 9362 | // ordinary lookup of the name operator() in the context of |
| 9363 | // (E).operator(). |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9364 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 9365 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 9366 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9367 | if (RequireCompleteType(LParenLoc, Object.get()->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 9368 | PDiag(diag::err_incomplete_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9369 | << Object.get()->getSourceRange())) |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 9370 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9371 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9372 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 9373 | LookupQualifiedName(R, Record->getDecl()); |
| 9374 | R.suppressDiagnostics(); |
| 9375 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 9376 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 9377 | Oper != OperEnd; ++Oper) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9378 | AddMethodCandidate(Oper.getPair(), Object.get()->getType(), |
| 9379 | Object.get()->Classify(Context), Args, NumArgs, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 9380 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 9381 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9382 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9383 | // C++ [over.call.object]p2: |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 9384 | // In addition, for each (non-explicit in C++0x) conversion function |
| 9385 | // declared in T of the form |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9386 | // |
| 9387 | // operator conversion-type-id () cv-qualifier; |
| 9388 | // |
| 9389 | // where cv-qualifier is the same cv-qualification as, or a |
| 9390 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 9391 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 9392 | // R", or the type "reference to pointer to function of |
| 9393 | // (P1,...,Pn) returning R", or the type "reference to function |
| 9394 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9395 | // is also considered as a candidate function. Similarly, |
| 9396 | // surrogate call functions are added to the set of candidate |
| 9397 | // functions for each conversion function declared in an |
| 9398 | // accessible base class provided the function is not hidden |
| 9399 | // within T by another intervening declaration. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 9400 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 9401 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 9402 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9403 | E = Conversions->end(); I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 9404 | NamedDecl *D = *I; |
| 9405 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 9406 | if (isa<UsingShadowDecl>(D)) |
| 9407 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9408 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 9409 | // Skip over templated conversion functions; they aren't |
| 9410 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 9411 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 9412 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 9413 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 9414 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 9415 | if (!Conv->isExplicit()) { |
| 9416 | // Strip the reference type (if any) and then the pointer type (if |
| 9417 | // any) to get down to what might be a function type. |
| 9418 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 9419 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 9420 | ConvType = ConvPtrType->getPointeeType(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9421 | |
Douglas Gregor | bf6e317 | 2011-07-23 18:59:35 +0000 | [diff] [blame] | 9422 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
| 9423 | { |
| 9424 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
| 9425 | Object.get(), Args, NumArgs, CandidateSet); |
| 9426 | } |
| 9427 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9428 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9429 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9430 | // Perform overload resolution. |
| 9431 | OverloadCandidateSet::iterator Best; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9432 | switch (CandidateSet.BestViableFunction(*this, Object.get()->getLocStart(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9433 | Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9434 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9435 | // Overload resolution succeeded; we'll build the appropriate call |
| 9436 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9437 | break; |
| 9438 | |
| 9439 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 9440 | if (CandidateSet.empty()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9441 | Diag(Object.get()->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 9442 | << Object.get()->getType() << /*call*/ 1 |
| 9443 | << Object.get()->getSourceRange(); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 9444 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9445 | Diag(Object.get()->getSourceRange().getBegin(), |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 9446 | diag::err_ovl_no_viable_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9447 | << Object.get()->getType() << Object.get()->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9448 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9449 | break; |
| 9450 | |
| 9451 | case OR_Ambiguous: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9452 | Diag(Object.get()->getSourceRange().getBegin(), |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9453 | diag::err_ovl_ambiguous_object_call) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9454 | << Object.get()->getType() << Object.get()->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9455 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9456 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9457 | |
| 9458 | case OR_Deleted: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9459 | Diag(Object.get()->getSourceRange().getBegin(), |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9460 | diag::err_ovl_deleted_object_call) |
| 9461 | << Best->Function->isDeleted() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9462 | << Object.get()->getType() |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 9463 | << getDeletedOrUnavailableSuffix(Best->Function) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9464 | << Object.get()->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9465 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9466 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9467 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9468 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 9469 | if (Best == CandidateSet.end()) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9470 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9471 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9472 | if (Best->Function == 0) { |
| 9473 | // Since there is no function declaration, this is one of the |
| 9474 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9475 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9476 | = cast<CXXConversionDecl>( |
| 9477 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 9478 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9479 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 9480 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 9481 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9482 | // We selected one of the surrogate functions that converts the |
| 9483 | // object parameter to a function pointer. Perform the conversion |
| 9484 | // on the object argument, then let ActOnCallExpr finish the job. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9485 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 9486 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 9487 | // and then call it. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9488 | ExprResult Call = BuildCXXMemberCallExpr(Object.get(), Best->FoundDecl, Conv); |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 9489 | if (Call.isInvalid()) |
| 9490 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9491 | |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 9492 | return ActOnCallExpr(S, Call.get(), LParenLoc, MultiExprArg(Args, NumArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 9493 | RParenLoc); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9494 | } |
| 9495 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 9496 | MarkDeclarationReferenced(LParenLoc, Best->Function); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9497 | CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 9498 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 9499 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 9500 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 9501 | // that calls this method, using Object for the implicit object |
| 9502 | // parameter and passing along the remaining arguments. |
| 9503 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 9504 | const FunctionProtoType *Proto = |
| 9505 | Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9506 | |
| 9507 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 9508 | unsigned NumArgsToCheck = NumArgs; |
| 9509 | |
| 9510 | // Build the full argument list for the method call (the |
| 9511 | // implicit object parameter is placed at the beginning of the |
| 9512 | // list). |
| 9513 | Expr **MethodArgs; |
| 9514 | if (NumArgs < NumArgsInProto) { |
| 9515 | NumArgsToCheck = NumArgsInProto; |
| 9516 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 9517 | } else { |
| 9518 | MethodArgs = new Expr*[NumArgs + 1]; |
| 9519 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9520 | MethodArgs[0] = Object.get(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9521 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 9522 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9523 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9524 | ExprResult NewFn = CreateFunctionRefExpr(*this, Method); |
| 9525 | if (NewFn.isInvalid()) |
| 9526 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9527 | |
| 9528 | // Once we've built TheCall, all of the expressions are properly |
| 9529 | // owned. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9530 | QualType ResultTy = Method->getResultType(); |
| 9531 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 9532 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 9533 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9534 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9535 | new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn.take(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9536 | MethodArgs, NumArgs + 1, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9537 | ResultTy, VK, RParenLoc); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9538 | delete [] MethodArgs; |
| 9539 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9540 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall, |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 9541 | Method)) |
| 9542 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9543 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 9544 | // We may have default arguments. If so, we need to allocate more |
| 9545 | // slots in the call for them. |
| 9546 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 9547 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 9548 | else if (NumArgs > NumArgsInProto) |
| 9549 | NumArgsToCheck = NumArgsInProto; |
| 9550 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 9551 | bool IsError = false; |
| 9552 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9553 | // Initialize the implicit object parameter. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9554 | ExprResult ObjRes = |
| 9555 | PerformObjectArgumentInitialization(Object.get(), /*Qualifier=*/0, |
| 9556 | Best->FoundDecl, Method); |
| 9557 | if (ObjRes.isInvalid()) |
| 9558 | IsError = true; |
| 9559 | else |
| 9560 | Object = move(ObjRes); |
| 9561 | TheCall->setArg(0, Object.take()); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 9562 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9563 | // Check the argument types. |
| 9564 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9565 | Expr *Arg; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 9566 | if (i < NumArgs) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9567 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9568 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 9569 | // Pass the argument. |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 9570 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9571 | ExprResult InputInit |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 9572 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 9573 | Context, |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 9574 | Method->getParamDecl(i)), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9575 | SourceLocation(), Arg); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9576 | |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 9577 | IsError |= InputInit.isInvalid(); |
| 9578 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 9579 | } else { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9580 | ExprResult DefArg |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 9581 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 9582 | if (DefArg.isInvalid()) { |
| 9583 | IsError = true; |
| 9584 | break; |
| 9585 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9586 | |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 9587 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 9588 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9589 | |
| 9590 | TheCall->setArg(i + 1, Arg); |
| 9591 | } |
| 9592 | |
| 9593 | // If this is a variadic call, handle args passed through "...". |
| 9594 | if (Proto->isVariadic()) { |
| 9595 | // Promote the arguments (C99 6.5.2.2p7). |
| 9596 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9597 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0); |
| 9598 | IsError |= Arg.isInvalid(); |
| 9599 | TheCall->setArg(i + 1, Arg.take()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9600 | } |
| 9601 | } |
| 9602 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 9603 | if (IsError) return true; |
| 9604 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9605 | if (CheckFunctionCall(Method, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 9606 | return true; |
| 9607 | |
John McCall | 182f709 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 9608 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 9609 | } |
| 9610 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9611 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9612 | /// (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] | 9613 | /// @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] | 9614 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9615 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc) { |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 9616 | assert(Base->getType()->isRecordType() && |
| 9617 | "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9618 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9619 | if (Base->getObjectKind() == OK_ObjCProperty) { |
| 9620 | ExprResult Result = ConvertPropertyForRValue(Base); |
| 9621 | if (Result.isInvalid()) |
| 9622 | return ExprError(); |
| 9623 | Base = Result.take(); |
| 9624 | } |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 9625 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9626 | SourceLocation Loc = Base->getExprLoc(); |
| 9627 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9628 | // C++ [over.ref]p1: |
| 9629 | // |
| 9630 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 9631 | // for a class object x of type T if T::operator->() exists and if |
| 9632 | // the operator is selected as the best match function by the |
| 9633 | // overload resolution mechanism (13.3). |
Chandler Carruth | 6df868e | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 9634 | DeclarationName OpName = |
| 9635 | Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9636 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 9637 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 9638 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 9639 | if (RequireCompleteType(Loc, Base->getType(), |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 9640 | PDiag(diag::err_typecheck_incomplete_tag) |
| 9641 | << Base->getSourceRange())) |
| 9642 | return ExprError(); |
| 9643 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 9644 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 9645 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 9646 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 9647 | |
| 9648 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 9649 | Oper != OperEnd; ++Oper) { |
Douglas Gregor | 2c9a03f | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 9650 | AddMethodCandidate(Oper.getPair(), Base->getType(), Base->Classify(Context), |
| 9651 | 0, 0, CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 9652 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9653 | |
| 9654 | // Perform overload resolution. |
| 9655 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9656 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9657 | case OR_Success: |
| 9658 | // Overload resolution succeeded; we'll build the call below. |
| 9659 | break; |
| 9660 | |
| 9661 | case OR_No_Viable_Function: |
| 9662 | if (CandidateSet.empty()) |
| 9663 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 9664 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9665 | else |
| 9666 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 9667 | << "operator->" << Base->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9668 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 9669 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9670 | |
| 9671 | case OR_Ambiguous: |
Douglas Gregor | ae2cf76 | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 9672 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 9673 | << "->" << Base->getType() << Base->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9674 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 9675 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 9676 | |
| 9677 | case OR_Deleted: |
| 9678 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 9679 | << Best->Function->isDeleted() |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 9680 | << "->" |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 9681 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | 5e24f2a | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 9682 | << Base->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 9683 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 9684 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9685 | } |
| 9686 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 9687 | MarkDeclarationReferenced(OpLoc, Best->Function); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9688 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 9689 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 9690 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9691 | // Convert the object parameter. |
| 9692 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9693 | ExprResult BaseResult = |
| 9694 | PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 9695 | Best->FoundDecl, Method); |
| 9696 | if (BaseResult.isInvalid()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 9697 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9698 | Base = BaseResult.take(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 9699 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9700 | // Build the operator call. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9701 | ExprResult FnExpr = CreateFunctionRefExpr(*this, Method); |
| 9702 | if (FnExpr.isInvalid()) |
| 9703 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9704 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9705 | QualType ResultTy = Method->getResultType(); |
| 9706 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 9707 | ResultTy = ResultTy.getNonLValueExprType(Context); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9708 | CXXOperatorCallExpr *TheCall = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9709 | new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr.take(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9710 | &Base, 1, ResultTy, VK, OpLoc); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 9711 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9712 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 9713 | Method)) |
| 9714 | return ExprError(); |
Eli Friedman | d593190 | 2011-04-04 01:18:25 +0000 | [diff] [blame] | 9715 | |
| 9716 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 9717 | } |
| 9718 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9719 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 9720 | /// a C++ overloaded function (possibly with some parentheses and |
| 9721 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 9722 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 9723 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 9724 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9725 | FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9726 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9727 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 9728 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9729 | if (SubExpr == PE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9730 | return PE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9731 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9732 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9733 | } |
| 9734 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9735 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9736 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 9737 | Found, Fn); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9738 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9739 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 9740 | "Implicit cast type cannot be determined from overload"); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 9741 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9742 | if (SubExpr == ICE->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9743 | return ICE; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9744 | |
| 9745 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 9746 | ICE->getCastKind(), |
| 9747 | SubExpr, 0, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 9748 | ICE->getValueKind()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9749 | } |
| 9750 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9751 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9752 | assert(UnOp->getOpcode() == UO_AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9753 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 9754 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 9755 | if (Method->isStatic()) { |
| 9756 | // Do nothing: static member functions aren't any different |
| 9757 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9758 | } else { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 9759 | // Fix the sub expression, which really has to be an |
| 9760 | // UnresolvedLookupExpr holding an overloaded member function |
| 9761 | // or template. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9762 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 9763 | Found, Fn); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9764 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9765 | return UnOp; |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9766 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9767 | assert(isa<DeclRefExpr>(SubExpr) |
| 9768 | && "fixed to something other than a decl ref"); |
| 9769 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 9770 | && "fixed to a member ref with no nested name qualifier"); |
| 9771 | |
| 9772 | // We have taken the address of a pointer to member |
| 9773 | // function. Perform the computation here so that we get the |
| 9774 | // appropriate pointer to member type. |
| 9775 | QualType ClassType |
| 9776 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 9777 | QualType MemPtrType |
| 9778 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 9779 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9780 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, MemPtrType, |
| 9781 | VK_RValue, OK_Ordinary, |
| 9782 | UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 9783 | } |
| 9784 | } |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9785 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 9786 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9787 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9788 | return UnOp; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9789 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9790 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9791 | Context.getPointerType(SubExpr->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9792 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9793 | UnOp->getOperatorLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9794 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9795 | |
| 9796 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9797 | // FIXME: avoid copy. |
| 9798 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 9799 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9800 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 9801 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 9802 | } |
| 9803 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9804 | return DeclRefExpr::Create(Context, |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 9805 | ULE->getQualifierLoc(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9806 | Fn, |
| 9807 | ULE->getNameLoc(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9808 | Fn->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9809 | VK_LValue, |
Chandler Carruth | 3aa8140 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 9810 | Found.getDecl(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9811 | TemplateArgs); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9812 | } |
| 9813 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9814 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 9815 | // FIXME: avoid copy. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9816 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 9817 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 9818 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 9819 | TemplateArgs = &TemplateArgsBuffer; |
| 9820 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 9821 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9822 | Expr *Base; |
| 9823 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9824 | // If we're filling in a static method where we used to have an |
| 9825 | // implicit member access, rewrite to a simple decl ref. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9826 | if (MemExpr->isImplicitAccess()) { |
| 9827 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 9828 | return DeclRefExpr::Create(Context, |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 9829 | MemExpr->getQualifierLoc(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9830 | Fn, |
| 9831 | MemExpr->getMemberLoc(), |
| 9832 | Fn->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9833 | VK_LValue, |
Chandler Carruth | 3aa8140 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 9834 | Found.getDecl(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9835 | TemplateArgs); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 9836 | } else { |
| 9837 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 9838 | if (MemExpr->getQualifier()) |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 9839 | Loc = MemExpr->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 9840 | Base = new (Context) CXXThisExpr(Loc, |
| 9841 | MemExpr->getBaseType(), |
| 9842 | /*isImplicit=*/true); |
| 9843 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9844 | } else |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9845 | Base = MemExpr->getBase(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9846 | |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9847 | ExprValueKind valueKind; |
| 9848 | QualType type; |
| 9849 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 9850 | valueKind = VK_LValue; |
| 9851 | type = Fn->getType(); |
| 9852 | } else { |
| 9853 | valueKind = VK_RValue; |
| 9854 | type = Context.BoundMemberTy; |
| 9855 | } |
| 9856 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9857 | return MemberExpr::Create(Context, Base, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9858 | MemExpr->isArrow(), |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 9859 | MemExpr->getQualifierLoc(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9860 | Fn, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9861 | Found, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9862 | MemExpr->getMemberNameInfo(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9863 | TemplateArgs, |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9864 | type, valueKind, OK_Ordinary); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9865 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9866 | |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9867 | llvm_unreachable("Invalid reference to overloaded function"); |
| 9868 | return E; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9869 | } |
| 9870 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9871 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9872 | DeclAccessPair Found, |
| 9873 | FunctionDecl *Fn) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9874 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 9875 | } |
| 9876 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9877 | } // end namespace clang |